Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | 773092x 773092x 773092x 773092x 773092x 773092x 773092x 69119x 69119x 628931x 772935x 112847x 112847x 2321x 2321x 63154x 224133x | // The ordered tree behind `TreeMap`: the tree of `TreeSet` with a value in every node. Nodes are
// keyed by `value` (the key), so every search of the ordered tree applies unchanged.
//
// An entry, a cursor or a stop node can hold a node after its mapping leaves the map, so the tree
// keeps an epoch: a node is stored exactly while its epoch equals the tree's. Removing a node marks
// it with -1; clearing the tree moves the tree to the next epoch without visiting any node. A node
// is never linked again once removed (setting the key again makes a new node).
import type { Comparator } from '../../functions/comparators'
import type { ChangeCounter } from '../revision'
import { OrderedTree, type TreeNode } from './ordered-tree'
/**
* A node of a map's tree: `value` is the key, `mapped` its value.
*
* @internal
*/
export class MapNode<K, V> implements TreeNode<K> {
value: K
left: TreeNode<K> | null
right: TreeNode<K> | null
parent: TreeNode<K> | null
red: boolean
mapped: V
/** The epoch of the tree when the node was linked, or -1 once it is removed. */
epoch: number
// Every field in one constructor, with no base class to call: a map makes one node per mapping.
constructor(key: K, mapped: V, epoch: number) {
this.value = key
this.left = null
this.right = null
this.parent = null
this.red = true
this.mapped = mapped
this.epoch = epoch
}
}
/**
* The red-black tree of a map, with an epoch that tells stored nodes from removed ones in O(1).
*
* @internal
*/
export class MapTree<K, V> extends OrderedTree<K> {
/** The current epoch: nodes linked since the last `clear` carry it. */
epoch = 0
constructor(order: Comparator<K> | undefined, changes: ChangeCounter) {
super(order, changes, 'map')
}
/** Whether `node` is still in the tree. O(1), no comparisons. */
stored(node: TreeNode<K>): boolean {
return (node as MapNode<K, V>).epoch === this.epoch
}
/** A new node for `key` and `value`, of the current epoch, not yet linked. */
newNode(key: K, value: V): MapNode<K, V> {
return new MapNode(key, value, this.epoch)
}
override remove(node: TreeNode<K>): void {
super.remove(node)
;(node as MapNode<K, V>).epoch = -1
}
override clear(): void {
super.clear()
this.epoch++
}
/**
* Fills an empty tree with `keys`, in strictly ascending order, mapped to `values` at the same
* positions, as a balanced tree. No comparisons.
*/
buildPairs(keys: readonly K[], values: readonly V[]): void {
this.buildSorted(keys, (i) => this.newNode(keys[i] as K, values[i] as V))
}
/** Fills an empty tree with a node-for-node copy of `source`'s keys and values. No comparisons. */
copyPairs(source: MapTree<K, V>): void {
this.copyFrom(source, (node) => this.newNode(node.value, (node as MapNode<K, V>).mapped))
}
}
|