scoop:scoop.std:0.1.0
src/collections/linked_map.scoop
Selected production source · src/collections/linked_map.scoop
1
package scoop.collections3
/**4
* A mutable hash map whose iteration follows insertion order. Replacing an existing value retains5
* its position; removing and reinserting moves it to the end.6
*/7
public class LinkedHashMap<K, V> public constructor(): MutableMap<K, V>8
where K: Equals<K>, K: Hash {9
private val positions: HashMap<K, Long> = HashMap<K, Long>()10
private val storage: LinkedStorage<K, V> = LinkedStorage<K, V>()11
private val changes: ModificationCounter = ModificationCounter()12
private val structure: ModificationCounter = ModificationCounter()14
/**15
* The number of key-value associations.16
*/17
public override val size: Long get() = positions.size18
/**19
* A read-only view of the map's current keys.20
*/21
public override val keys: Set<K> get() = MapKeys<K, V>(this)22
/**23
* A read-only view of current values, retaining duplicates.24
*/25
public override val values: Collection<V> get() = MapValues<K, V>(this)26
/**27
* A read-only view of current key-value entries.28
*/29
public override val entries: Collection<Map.Entry<K, V>> get() = MapEntries<K, V>(this)31
private fun locate(key: K): HashProbe {32
val stamp = changes.snapshot()33
val location = positions.locate(key)34
changes.check(stamp)35
return location36
}38
/**39
* Returns the value associated with the key, or None when absent.40
*/41
public override operator fun get(key: K): V? {42
return when (val probe = locate(key)) {43
case HashProbe.Found(val index, _) -> Some(storage.read(positions.valueAt(index)).value)44
case HashProbe.Absent(_, _) -> None45
}46
}48
/**49
* Tests whether the key has an association.50
*/51
public override fun containsKey(key: K): Boolean =52
when (locate(key)) {53
case HashProbe.Found(_, _) -> true54
case HashProbe.Absent(_, _) -> false55
}57
/**58
* Associates the value with the key and returns the previous value, or None for a new key.59
*/60
public override fun put(key: K, value: V): V? = putEntry(key, value, true)62
internal fun putIfAbsent(key: K, value: V): V? = putEntry(key, value, false)64
private fun putEntry(key: K, value: V, replace: Boolean): V? {65
return when (val probe = locate(key)) {66
case HashProbe.Found(val position, _) -> {67
val index = positions.valueAt(position)68
val old = storage.read(index).value69
if (replace) {70
storage.replace(index, value)71
changes.advance()72
}73
Some(old)74
}75
case HashProbe.Absent(val vacant, val mixedHash) -> {76
val index = storage.append(key, value)77
positions.insertAbsent(key, index, vacant, mixedHash)78
changes.advance()79
structure.advance()80
None81
}82
}83
}85
/**86
* Removes the key and returns its former value, or None if absent.87
*/88
public override fun remove(key: K): V? {89
return when (val probe = locate(key)) {90
case HashProbe.Found(val position, _) -> {91
val index = positions.removeAt(position)92
val value = storage.remove(index)93
changes.advance()94
structure.advance()95
Some(value)96
}97
case HashProbe.Absent(_, _) -> None98
}99
}101
/**102
* Removes every association.103
*/104
public override fun clear() {105
if (size == 0L)106
return107
positions.clear()108
storage.clear()109
changes.advance()110
structure.advance()111
}113
/**114
* Iterates key-value entries in this map's order; structural changes invalidate the iterator.115
*/116
public override operator fun iterator(): Iterator<Map.Entry<K, V>> =117
LinkedIterator<K, V>(storage, structure)119
/**120
* Serialization factories using explicit element or key/value codecs.121
*/122
public companion object {123
/**124
* Encodes entries or elements in iteration order using the supplied component codecs.125
*/126
public fun encoder(key: ValueEncoder<K>, value: ValueEncoder<V>): ValueEncoder<127
LinkedHashMap<K, V>128
> =129
ValueEncoderFunction<LinkedHashMap<K, V>> {130
values,131
encoder -> encodeMap(values, key, value, encoder)132
}133
/**134
* Decodes entries or elements into a new collection using the supplied component codecs135
* and comparator where required.136
*/137
public fun decoder(key: ValueDecoder<K>, value: ValueDecoder<V>): ValueDecoder<138
LinkedHashMap<K, V>139
> =140
ValueDecoderFunction<LinkedHashMap<K, V>> {141
decoder ->142
val result = LinkedHashMap<K, V>()143
decodeMapInto(decoder, key, value, result)144
result145
}146
}147
}149
internal class LinkedIterator<K, V> internal constructor(150
private val storage: LinkedStorage<K, V>,151
private val structure: ModificationCounter152
): Iterator<Map.Entry<K, V>> {153
private val stamp: RevisionStamp = structure.snapshot()154
private var index: Long = storage.first155
private var exhausted: Boolean = false157
public override fun next(): Map.Entry<K, V>? {158
if (exhausted)159
return None160
structure.check(stamp)161
if (index < 0L) {162
exhausted = true163
return None164
}165
val entry = storage.read(index)166
index = entry.next167
return Some(Map.Entry<K, V>(entry.key, entry.value))168
}169
}