scoop:scoop.std:0.1.0
src/collections/tree_map.scoop
Selected production source · src/collections/tree_map.scoop
1
package scoop.collections3
import scoop.comparisons.Comparator5
internal struct TreeLocation<K, V>(val node: BTreeNode<K, V>?, val index: Long, val found: Boolean)7
/**8
* A mutable ordered map using the supplied comparator for both order and key equivalence.9
* Iteration visits ascending keys; comparator-equivalent keys occupy one entry.10
*/11
public class TreeMap<K, V> public constructor(12
/**13
* The comparator used for ordering and, for ordered maps/sets, equivalence.14
*/15
public override val comparator: Comparator<K>16
) : MutableSortedMap<K, V> {17
private val tree: BTree<K, V> = BTree<K, V>()18
private val changes: ModificationCounter = ModificationCounter()19
private val structure: ModificationCounter = ModificationCounter()21
/**22
* The number of key-value associations.23
*/24
public override val size: Long get() = tree.size25
/**26
* A read-only view of the map's current keys.27
*/28
public override val keys: Set<K> get() = MapKeys<K, V>(this)29
/**30
* A read-only view of current values, retaining duplicates.31
*/32
public override val values: Collection<V> get() = MapValues<K, V>(this)33
/**34
* A read-only view of current key-value entries.35
*/36
public override val entries: Collection<Map.Entry<K, V>> get() = MapEntries<K, V>(this)38
private fun compare(key: K, node: BTreeNode<K, V>, index: Long, stamp: RevisionStamp): Long {39
val result = comparator.compare(key, node.entry(index).key)40
changes.check(stamp)41
return result42
}44
private fun locate(key: K): TreeLocation<K, V> {45
val stamp = changes.snapshot()46
var current = tree.root47
while (current != None) {48
val node = current!!49
var low = 0L50
var high = node.count51
while (low < high) {52
val middle = (low + high) / 2L53
val result = compare(key, node, middle, stamp)54
if (result == 0L) return TreeLocation<K, V>(Some(node), middle, true)55
if (result > 0L) low = middle + 1L else high = middle56
}57
if (node.leaf) return TreeLocation<K, V>(Some(node), low, false)58
current = node.child(low)59
}60
return TreeLocation<K, V>(None, 0L, false)61
}63
/**64
* Returns the value associated with the key, or None when absent.65
*/66
public override operator fun get(key: K): V? {67
val location = locate(key)68
return if (location.found) Some(location.node!!.entry(location.index).value) else None69
}71
/**72
* Tests whether the key has an association.73
*/74
public override fun containsKey(key: K): Boolean = locate(key).found75
/**76
* Associates the value with the key and returns the previous value, or None for a new key.77
*/78
public override fun put(key: K, value: V): V? = putEntry(key, value, true)79
internal fun putIfAbsent(key: K, value: V): V? = putEntry(key, value, false)81
private fun putEntry(key: K, value: V, replace: Boolean): V? {82
val location = locate(key)83
if (location.found) {84
val old = location.node!!.entry(location.index).value85
if (replace) {86
tree.replace(location.node!!, location.index, value)87
changes.advance()88
}89
return Some(old)90
}91
tree.insert(key, value, location.node, location.index)92
changes.advance()93
structure.advance()94
return None95
}97
/**98
* Removes the key and returns its former value, or None if absent.99
*/100
public override fun remove(key: K): V? {101
val location = locate(key)102
if (!location.found) return None103
val value = tree.remove(location.node!!, location.index)104
changes.advance()105
structure.advance()106
return Some(value)107
}109
/**110
* Removes every association.111
*/112
public override fun clear() {113
if (size == 0L) return114
tree.clear()115
changes.advance()116
structure.advance()117
}119
private fun bound(key: K, greater: Boolean, inclusive: Boolean): Map.Entry<K, V>? {120
val stamp = changes.snapshot()121
var current = tree.root122
var candidate: Map.Entry<K, V>? = None123
val strict = if (greater) !inclusive else inclusive124
while (current != None) {125
val node = current!!126
var low = 0L127
var high = node.count128
while (low < high) {129
val middle = (low + high) / 2L130
val result = compare(key, node, middle, stamp)131
if (result > 0L || (strict && result == 0L)) low = middle + 1L else high = middle132
}133
if (greater && low < node.count) candidate = Some(node.entry(low))134
if (!greater && low > 0L) candidate = Some(node.entry(low - 1L))135
current = node.child(low)136
}137
return candidate138
}140
/**141
* Returns the least-key entry, or None when empty.142
*/143
public override fun firstEntryOrNone(): Map.Entry<K, V>? {144
val node = tree.first(tree.root)145
return if (node == None) None else Some(node!!.entry(0L))146
}148
/**149
* Returns the greatest-key entry, or None when empty.150
*/151
public override fun lastEntryOrNone(): Map.Entry<K, V>? {152
val node = tree.last(tree.root)153
return if (node == None) None else Some(node!!.entry(node!!.count - 1L))154
}156
/**157
* Returns the greatest entry strictly below the key, or None.158
*/159
public override fun lowerEntry(key: K): Map.Entry<K, V>? = bound(key, false, false)160
/**161
* Returns the greatest entry at or below the key, or None.162
*/163
public override fun floorEntry(key: K): Map.Entry<K, V>? = bound(key, false, true)164
/**165
* Returns the least entry at or above the key, or None.166
*/167
public override fun ceilingEntry(key: K): Map.Entry<K, V>? = bound(key, true, true)168
/**169
* Returns the least entry strictly above the key, or None.170
*/171
public override fun higherEntry(key: K): Map.Entry<K, V>? = bound(key, true, false)172
/**173
* Iterates key-value entries in this map's order; structural changes invalidate the iterator.174
*/175
public override operator fun iterator(): Iterator<Map.Entry<K, V>> = TreeIterator<K, V>(tree, structure)177
/**178
* Serialization factories using explicit element or key/value codecs.179
*/180
public companion object {181
/**182
* Encodes entries or elements in iteration order using the supplied component codecs.183
*/184
public fun encoder(key: ValueEncoder<K>, value: ValueEncoder<V>): ValueEncoder<TreeMap<K, V>> =185
ValueEncoderFunction<TreeMap<K, V>> { values, encoder -> encodeMap(values, key, value, encoder) }186
/**187
* Decodes entries or elements into a new collection using the supplied component codecs188
* and comparator where required.189
*/190
public fun decoder(key: ValueDecoder<K>, value: ValueDecoder<V>, comparator: Comparator<K>): ValueDecoder<TreeMap<K, V>> =191
ValueDecoderFunction<TreeMap<K, V>> { decoder ->192
val result = TreeMap<K, V>(comparator)193
decodeMapInto(decoder, key, value, result)194
result195
}196
}197
}