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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 | 471173x 190x 190x 190x 143x 264024x 264024x 264024x 123660x 373x 12x 98x 66x 36x 29x 12x 26x 26x 29483x 29483x 29483x 88x 118x 50x 40x 30x 13x 15x 28x 28x 28x 28x 19x 19x 432572x 342x 10x 7673x 7673x 8590x 1634x 4321x 422887x 422885x 422885x 422885x 422885x 422885x 1x 422884x 422884x 422823x 422823x 2x 422812x 422844x 11x 2x 1x 29x 29x 10x 10x 10x 10x 3x 13x 13x 13x 13x 14x 14x 12x 12x 12x 41x 41x 12x 34x 7707x 7707x 7707x 122x 44x 32x 26x 18x 16x 10x 10x 27351x 322x 14x 25185x 1688x 15x 26x 9x 20x | // The live views of a sorted map's keys, entries and values. Each works on the map's own tree, range
// and direction and shares its change counter, so changes show everywhere and open iterators of any
// of them fail fast. None can add: mappings are set on the map.
import { ConcurrentModificationError, NoSuchElementError } from '../../errors/errors'
import type { Comparator } from '../../functions/comparators'
import type { Cursor } from '../../interfaces/collection'
import type { SequencedCollection } from '../../interfaces/list'
import type { MapEntry } from '../../interfaces/map'
import type { SequencedSet } from '../../interfaces/sequenced-set'
import type { SortedSet } from '../../interfaces/sorted-set'
import { isBranded, MAP_ENTRY_BRAND, SEQUENCED_COLLECTION_BRAND } from '../brand'
import type { MapNode } from '../engines/map-tree'
import type { TreeNode } from '../engines/ordered-tree'
import { equals, type HashFunction } from '../equality'
import { AbstractCollection } from '../skeletons/abstract-collection'
import { AbstractSet } from '../skeletons/abstract-set'
import type { AbstractSortedMap } from '../skeletons/abstract-sorted-map'
import { AbstractSortedSet, type Bound } from '../skeletons/abstract-sorted-set'
import { unsupportedAdd } from './map-entry'
function mappedOf<K, V>(node: TreeNode<K>): V {
return (node as MapNode<K, V>).mapped
}
/**
* The node at the front (`back` false) or back of `map`, in its order, or `NoSuchElementError`
* naming `<tag>.<method>` and what the view is called.
*/
function endNode<K, V>(
map: AbstractSortedMap<K, V>,
back: boolean,
where: string,
noun: string,
): TreeNode<K> {
const expected = map.changes.structure
const node = back ? map.nav.lastNode(where, expected) : map.nav.firstNode(where, expected)
if (node === null) throw new NoSuchElementError(`${where}: the ${noun} is empty`)
return node
}
/**
* The live view of a sorted map's keys: a sorted set over the map's tree, range and direction.
* Removing a key removes its mapping; adding throws. Its range and reversed views are the key
* sets of the matching map views.
*
* @internal
*/
export class TreeMapKeySet<K, V> extends AbstractSortedSet<K> {
private readonly map: AbstractSortedMap<K, V>
/** The key set this one was made from by `reversed()`, which reversing it again returns. */
private readonly source: TreeMapKeySet<K, V> | undefined
constructor(map: AbstractSortedMap<K, V>, source?: TreeMapKeySet<K, V>) {
super(map.tree, map.low, map.high, map.descending)
this.map = map
this.source = source
}
get [Symbol.toStringTag](): string {
return 'TreeMapKeySet'
}
get owner(): SortedSet<K> {
return this
}
comparator(): Comparator<K> | undefined {
return this.map.comparator()
}
reversed(): SortedSet<K> {
return this.source !== undefined ? this.source : new TreeMapKeySet(this.map.reverseView(), this)
}
protected openRange(low: Bound<K> | undefined, high: Bound<K> | undefined): SortedSet<K> {
return this.map.openRange(low, high).keySet()
}
override add(_key: K): this {
throw unsupportedAdd(this.className)
}
override addAll(elements: Iterable<K>): boolean {
if (this.readAll(elements, 'addAll').length === 0) return false
throw unsupportedAdd(this.className, 'addAll')
}
override addFirst(_key: K): this {
throw unsupportedAdd(this.className, 'addFirst')
}
override addLast(_key: K): this {
throw unsupportedAdd(this.className, 'addLast')
}
}
/**
* The live view of a sorted map's mappings as entries, in the map's order. A value is a member
* when it is a map entry whose key is in range and present with a value equal under `equals`.
*
* @internal
*/
export class TreeMapEntrySet<K, V>
extends AbstractSet<MapEntry<K, V>>
implements SequencedSet<MapEntry<K, V>>
{
protected override readonly iterationMode = 'fail-fast' as const
private readonly map: AbstractSortedMap<K, V>
constructor(map: AbstractSortedMap<K, V>) {
super(map.changes)
this.map = map
}
get owner(): SequencedSet<MapEntry<K, V>> {
return this
}
/** Marks every sequenced collection for `isSequencedCollection`, across loaded copies. */
get [SEQUENCED_COLLECTION_BRAND](): true {
return true
}
/** The entry view of the reversed map. */
reversed(): SequencedSet<MapEntry<K, V>> {
return new TreeMapEntrySet(this.map.reverseView())
}
getFirst(): MapEntry<K, V> {
return this.map.entryAt(endNode(this.map, false, `${this.className}.getFirst`, 'set'))
}
getLast(): MapEntry<K, V> {
return this.map.entryAt(endNode(this.map, true, `${this.className}.getLast`, 'set'))
}
removeFirst(): MapEntry<K, V> {
return this.removeEnd(false, 'removeFirst')
}
removeLast(): MapEntry<K, V> {
return this.removeEnd(true, 'removeLast')
}
/** Makes the entry first, so it reads the value the mapping had, then unlinks the node. */
private removeEnd(back: boolean, method: string): MapEntry<K, V> {
const node = endNode(this.map, back, `${this.className}.${method}`, 'set')
const entry = this.map.entryAt(node)
this.map.unlink(node)
return entry
}
addFirst(_entry: MapEntry<K, V>): this {
throw unsupportedAdd(this.className, 'addFirst')
}
addLast(_entry: MapEntry<K, V>): this {
throw unsupportedAdd(this.className, 'addLast')
}
get [Symbol.toStringTag](): string {
return 'TreeMapEntrySet'
}
get size(): number {
return this.map.size
}
override isEmpty(): boolean {
return this.map.isEmpty()
}
[Symbol.iterator](): IterableIterator<MapEntry<K, V>> {
const map = this.map
return map.nodeIterator(`${this.className}[Symbol.iterator]().next`, (node) =>
map.entryAt(node),
)
}
cursor(): Cursor<MapEntry<K, V>> {
const map = this.map
return map.nodeCursor(`${this.className}.cursor`, (node) => map.entryAt(node))
}
/** The node of the mapping `value` stands for, or `null` when it is not a member. */
private locate(
value: unknown,
method: string,
start: number = this.changes.structure,
): TreeNode<K> | null {
if (!isBranded(value, MAP_ENTRY_BRAND)) return null
const where = `${this.className}.${method}`
const entry = value as MapEntry<K, V>
const key = entry.key
const expected = entry.value
if (this.changes.structure !== start) {
throw new ConcurrentModificationError(`${where}: the map was changed while reading an entry`)
}
const node = this.map.nav.lookup(key, where, start)
if (node === null) return null
const same = equals(mappedOf<K, V>(node), expected)
if (this.changes.structure !== start) {
throw new ConcurrentModificationError(`${where}: the map was changed while comparing values`)
}
return same ? node : null
}
protected hasMember(entry: MapEntry<K, V>, method: string): boolean {
return this.locate(entry, method) !== null
}
add(_entry: MapEntry<K, V>): this {
throw unsupportedAdd(this.className)
}
override addAll(elements: Iterable<MapEntry<K, V>>): boolean {
if (this.readAll(elements, 'addAll').length === 0) return false
throw unsupportedAdd(this.className, 'addAll')
}
override delete(entry: MapEntry<K, V>): boolean {
const node = this.locate(entry, 'delete')
if (node === null) return false
this.map.unlink(node)
return true
}
override clear(): void {
this.map.clear()
}
override removeAll(entries: Iterable<MapEntry<K, V>>): boolean {
return this.removeMatching('removeAll', entries, true)
}
override retainAll(entries: Iterable<MapEntry<K, V>>): boolean {
return this.removeMatching('retainAll', entries, false)
}
/** Finds the mapping of every argument first (user code runs only here), then removes. */
private removeMatching(
method: string,
entries: Iterable<MapEntry<K, V>>,
remove: boolean,
): boolean {
const items = this.readAll(entries, method)
const matched = new Set<TreeNode<K>>()
const start = this.changes.structure
for (let i = 0; i < items.length; i++) {
const node = this.locate(items[i], method, start)
if (node !== null) matched.add(node)
}
const doomed: TreeNode<K>[] = []
const all = this.map.nav.nodes(`${this.className}.${method}`, this.changes.structure)
for (let i = 0; i < all.length; i++) {
const node = all[i] as TreeNode<K>
if (matched.has(node) === remove) doomed.push(node)
}
return this.map.removeNodes(doomed)
}
protected memberHashes(hash: HashFunction): number {
return this.map.mappingHashes(hash)
}
}
/**
* The live view of a sorted map's values, in the map's order, compared with SameValueZero.
* Removing a value removes the first mapping with that value. It equals only itself.
*
* @internal
*/
export class TreeMapValues<K, V> extends AbstractCollection<V> implements SequencedCollection<V> {
protected readonly iterationMode = 'fail-fast' as const
private readonly map: AbstractSortedMap<K, V>
constructor(map: AbstractSortedMap<K, V>) {
super(map.changes)
this.map = map
}
/** Marks every sequenced collection for `isSequencedCollection`, across loaded copies. */
get [SEQUENCED_COLLECTION_BRAND](): true {
return true
}
/** The value view of the reversed map. */
reversed(): SequencedCollection<V> {
return new TreeMapValues(this.map.reverseView())
}
getFirst(): V {
return mappedOf<K, V>(endNode(this.map, false, `${this.className}.getFirst`, 'collection'))
}
getLast(): V {
return mappedOf<K, V>(endNode(this.map, true, `${this.className}.getLast`, 'collection'))
}
removeFirst(): V {
return this.map.unlink(endNode(this.map, false, `${this.className}.removeFirst`, 'collection'))
}
removeLast(): V {
return this.map.unlink(endNode(this.map, true, `${this.className}.removeLast`, 'collection'))
}
addFirst(_value: V): this {
throw unsupportedAdd(this.className, 'addFirst')
}
addLast(_value: V): this {
throw unsupportedAdd(this.className, 'addLast')
}
get [Symbol.toStringTag](): string {
return 'TreeMapValues'
}
get size(): number {
return this.map.size
}
override isEmpty(): boolean {
return this.map.isEmpty()
}
[Symbol.iterator](): IterableIterator<V> {
return this.map.nodeIterator(`${this.className}[Symbol.iterator]().next`, mappedOf<K, V>)
}
cursor(): Cursor<V> {
return this.map.nodeCursor(`${this.className}.cursor`, mappedOf<K, V>)
}
add(_value: V): this {
throw unsupportedAdd(this.className)
}
override addAll(elements: Iterable<V>): boolean {
if (this.readAll(elements, 'addAll').length === 0) return false
throw unsupportedAdd(this.className, 'addAll')
}
override clear(): void {
this.map.clear()
}
}
|