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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | 105x 710x 406x 1580x 1580x 1580x 1580x 155x 2626x 2329x 1541x 761x 29x 66x 24x 482x 22x 127x 230x 178x 124x 30x 18x 1103x 12x 12x 34x 139x 138x 97x 97x 86x 86x 1x 85x 85x 85x 215x 65x 65x 65x 173x 173x 65x 3x 71x 272x 242x 16x 241x 236x 278x 236x 236x 14x 250x 242x 28x 14x 14x 250x 250x 658x 14x 268x 45x 26x 30x 32x 97x 38x 7x 7x 7x 7x 408x 32x 551x 12x 85x 86x 85x 79x 36x 36x 17x 41x 74x | // Read-only maps over another map: plain, sequenced and sorted. Reads, equality, hashing,
// iteration and `forEach` are the source's; key, entry and value views are read-only in turn,
// entries wrap the source's, and every change is refused before its arguments are looked at.
import type { Comparator } from '../../../functions/comparators'
import type { Collection } from '../../../interfaces/collection'
import type { SequencedCollection } from '../../../interfaces/list'
import type { MapCollection, MapEntry, ReadonlyMapCollection } from '../../../interfaces/map'
import type { ReadonlySequencedMap, SequencedMap } from '../../../interfaces/sequenced-map'
import type { SequencedSet } from '../../../interfaces/sequenced-set'
import type { SetCollection } from '../../../interfaces/set'
import type { ReadonlySortedMap, SortedMap } from '../../../interfaces/sorted-map'
import type { BoundOptions, SortedSet, SubSetOptions } from '../../../interfaces/sorted-set'
import { isBranded, MAP_COLLECTION_BRAND, SEQUENCED_MAP_BRAND, SORTED_MAP_BRAND } from '../../brand'
import { requireFunction } from '../../checks'
import { createGuard, rememberEqual } from '../../cycle'
import { equals, equalsSymbol, type HashFunction, hashCodeSymbol } from '../../equality'
import { apply } from '../../intrinsics'
import { INSPECT, type InspectOptions, inspectMap, mapToString } from '../../output'
import { ReadOnlyCollection, ReadOnlySequencedCollection } from './read-only-collection'
import { ReadOnlyEntry } from './read-only-entry'
import { ReadOnlySequencedSet, ReadOnlySet, ReadOnlySortedSet } from './read-only-set'
import { refuse } from './refuse'
import { type ReadOnlyKind, registerReadOnly } from './registry'
import { hideSource, ReadOnlyIterator, SOURCE } from './source-key'
import type { ReadOnlyTags } from './tags'
const equalsGuard = /*#__PURE__*/ createGuard()
/** Wraps a source entry so it cannot be written. @internal */
export function readOnlyEntry<K, V>(entry: MapEntry<K, V>): MapEntry<K, V> {
return new ReadOnlyEntry(entry)
}
function readOnlyEntryOrUndefined<K, V>(
entry: MapEntry<K, V> | undefined,
): MapEntry<K, V> | undefined {
return entry === undefined ? undefined : new ReadOnlyEntry(entry)
}
/** The roles a read-only map can play. @internal */
export type MapRole = 'map' | 'sequencedMap' | 'sortedMap'
/**
* A read-only view of `source`, printed with the tag of its role.
*
* @internal
*/
export class ReadOnlyMap<K, V> implements MapCollection<K, V> {
declare readonly [SOURCE]: ReadonlyMapCollection<K, V>
protected readonly tags: ReadOnlyTags
private readonly role: MapRole
/**
* @param kind - The family the view registers as; each library subclass passes its own.
* @param exact - The class whose instances register as `kind`; an instance of any other class,
* such as a subclass defined outside the library, is not registered.
*/
constructor(
source: ReadonlyMapCollection<K, V>,
tags: ReadOnlyTags,
role: MapRole = 'map',
kind: ReadOnlyKind = 'map',
exact: unknown = ReadOnlyMap,
) {
hideSource(this, source)
this.tags = tags
this.role = role
registerReadOnly(this, new.target === exact ? kind : undefined, source, tags[role])
}
/** Marks every map for `isMapCollection`, across loaded copies. */
get [MAP_COLLECTION_BRAND](): true {
return true
}
get [Symbol.toStringTag](): string {
return this.tags[this.role]
}
/** The name errors start with. */
protected get className(): string {
return this[Symbol.toStringTag]
}
get size(): number {
return this[SOURCE].size
}
get revision(): number {
return this[SOURCE].revision
}
isEmpty(): boolean {
return this[SOURCE].isEmpty()
}
get(key: K): V | undefined {
return this[SOURCE].get(key)
}
getOrDefault<D>(key: K, fallback: D): V | D {
return this[SOURCE].getOrDefault(key, fallback)
}
has(key: K): boolean {
return this[SOURCE].has(key)
}
containsValue(value: V): boolean {
return this[SOURCE].containsValue(value)
}
keySet(): SetCollection<K> {
return new ReadOnlySet(this[SOURCE].keySet(), this.tags.keySet)
}
entrySet(): SetCollection<MapEntry<K, V>> {
return new ReadOnlySet(this[SOURCE].entrySet(), this.tags.entrySet, readOnlyEntry)
}
valueCollection(): Collection<V> {
return new ReadOnlyCollection(this[SOURCE].valueCollection(), this.tags.values)
}
keys(): IterableIterator<K> {
return new ReadOnlyIterator(this[SOURCE].keys())
}
values(): IterableIterator<V> {
return new ReadOnlyIterator(this[SOURCE].values())
}
entries(): IterableIterator<[K, V]> {
return new ReadOnlyIterator(this[SOURCE].entries())
}
[Symbol.iterator](): IterableIterator<[K, V]> {
return new ReadOnlyIterator(this[SOURCE].entries())
}
forEach(action: (value: V, key: K, map: MapCollection<K, V>) => void, thisArg?: unknown): void {
requireFunction(action, `${this.className}.forEach`, 'action')
this[SOURCE].forEach((value, key) => {
apply(action, thisArg, [value, key, this])
})
}
// Compares through this view's own read-only entries, never the source's, so `other` (which may
// be user code) is never handed an entry that could change the source.
equals(other: unknown): boolean {
if (other === this) return true
if (!isBranded(other, MAP_COLLECTION_BRAND)) return false
const that = other as ReadonlyMapCollection<K, V>
if (that.size !== this.size) return false
return rememberEqual(this, that, () =>
equalsGuard(
this,
() => false,
() => {
const theirs = that.entrySet()
const ours = this.entrySet()[Symbol.iterator]()
for (let step = ours.next(); step.done !== true; step = ours.next()) {
if (!theirs.has(step.value)) return false
}
const source = this[SOURCE]
const pairs = that[Symbol.iterator]()
for (let step = pairs.next(); step.done !== true; step = pairs.next()) {
const [key, value] = step.value
Iif (!source.has(key) || !equals(value, source.get(key))) return false
}
return true
},
),
)
}
[equalsSymbol](other: unknown): boolean {
return this.equals(other)
}
[hashCodeSymbol](hash: HashFunction): number {
return this[SOURCE][hashCodeSymbol](hash)
}
toString(): string {
return mapToString(this)
}
toJSON(): Array<[K, V]> {
return this[SOURCE].toJSON()
}
[INSPECT](
depth: number | null,
options: InspectOptions,
inspect: (value: unknown, options: InspectOptions) => string,
): string {
return inspectMap(this, depth, options, inspect)
}
// ── Every change is refused ──
set(_key: K, _value: V): this {
throw refuse(`${this.className}.set`)
}
put(_key: K, _value: V): V | undefined {
throw refuse(`${this.className}.put`)
}
putAll(_entries: Iterable<readonly [K, V] | MapEntry<K, V>>): void {
throw refuse(`${this.className}.putAll`)
}
delete(_key: K): boolean {
throw refuse(`${this.className}.delete`)
}
remove(_key: K): V | undefined {
throw refuse(`${this.className}.remove`)
}
replace(_key: K, _value: V): V | undefined {
throw refuse(`${this.className}.replace`)
}
replaceAll(_fn: (value: V, key: K, map: MapCollection<K, V>) => V): void {
throw refuse(`${this.className}.replaceAll`)
}
getOrInsert(_key: K, _value: V): V {
throw refuse(`${this.className}.getOrInsert`)
}
getOrInsertComputed(_key: K, _fn: (key: K) => V): V {
throw refuse(`${this.className}.getOrInsertComputed`)
}
compute(
_key: K,
_fn: (value: V | undefined, key: K, map: MapCollection<K, V>) => V | undefined,
): V | undefined {
throw refuse(`${this.className}.compute`)
}
computeIfPresent(
_key: K,
_fn: (value: V, key: K, map: MapCollection<K, V>) => V | undefined,
): V | undefined {
throw refuse(`${this.className}.computeIfPresent`)
}
merge(
_key: K,
_value: V,
_fn: (old: V, value: V, key: K, map: MapCollection<K, V>) => V | undefined,
): V | undefined {
throw refuse(`${this.className}.merge`)
}
clear(): void {
throw refuse(`${this.className}.clear`)
}
}
/**
* A read-only view of a sequenced map: the ends, a reversed view, and sequenced key, entry and
* value views.
*
* @internal
*/
export class ReadOnlySequencedMap<K, V> extends ReadOnlyMap<K, V> implements SequencedMap<K, V> {
constructor(
source: ReadonlySequencedMap<K, V>,
tags: ReadOnlyTags,
role: MapRole = 'sequencedMap',
kind: ReadOnlyKind = 'sequenced-map',
exact: unknown = ReadOnlySequencedMap,
) {
super(source, tags, role, kind, exact)
}
/** Marks every sequenced map, across loaded copies. */
get [SEQUENCED_MAP_BRAND](): true {
return true
}
private get sequenced(): ReadonlySequencedMap<K, V> {
return this[SOURCE] as ReadonlySequencedMap<K, V>
}
firstEntry(): MapEntry<K, V> | undefined {
return readOnlyEntryOrUndefined(this.sequenced.firstEntry())
}
lastEntry(): MapEntry<K, V> | undefined {
return readOnlyEntryOrUndefined(this.sequenced.lastEntry())
}
reversed(): SequencedMap<K, V> {
return new ReadOnlySequencedMap(this.sequenced.reversed(), this.tags)
}
override keySet(): SequencedSet<K> {
return new ReadOnlySequencedSet(this.sequenced.keySet(), this.tags.sequencedSet)
}
override entrySet(): SequencedSet<MapEntry<K, V>> {
return new ReadOnlySequencedSet(
this.sequenced.entrySet(),
this.tags.sequencedSet,
readOnlyEntry,
)
}
override valueCollection(): SequencedCollection<V> {
return new ReadOnlySequencedCollection(
this.sequenced.valueCollection(),
this.tags.sequencedCollection,
)
}
pollFirstEntry(): MapEntry<K, V> | undefined {
throw refuse(`${this.className}.pollFirstEntry`)
}
pollLastEntry(): MapEntry<K, V> | undefined {
throw refuse(`${this.className}.pollLastEntry`)
}
putFirst(_key: K, _value: V): this {
throw refuse(`${this.className}.putFirst`)
}
putLast(_key: K, _value: V): this {
throw refuse(`${this.className}.putLast`)
}
}
/**
* A read-only view of a sorted map: navigation entries, range views, a descending view and a
* sorted key set.
*
* @internal
*/
export class ReadOnlySortedMap<K, V> extends ReadOnlySequencedMap<K, V> implements SortedMap<K, V> {
constructor(source: ReadonlySortedMap<K, V>, tags: ReadOnlyTags) {
super(source, tags, 'sortedMap', 'sorted-map', ReadOnlySortedMap)
}
/** Marks every sorted map for `isSortedMap`, across loaded copies. */
get [SORTED_MAP_BRAND](): true {
return true
}
private get sorted(): ReadonlySortedMap<K, V> {
return this[SOURCE] as ReadonlySortedMap<K, V>
}
comparator(): Comparator<K> | undefined {
return this.sorted.comparator()
}
lowerEntry(key: K): MapEntry<K, V> | undefined {
return readOnlyEntryOrUndefined(this.sorted.lowerEntry(key))
}
floorEntry(key: K): MapEntry<K, V> | undefined {
return readOnlyEntryOrUndefined(this.sorted.floorEntry(key))
}
ceilingEntry(key: K): MapEntry<K, V> | undefined {
return readOnlyEntryOrUndefined(this.sorted.ceilingEntry(key))
}
higherEntry(key: K): MapEntry<K, V> | undefined {
return readOnlyEntryOrUndefined(this.sorted.higherEntry(key))
}
subMap(from: K, to: K, options?: SubSetOptions | null): SortedMap<K, V> {
return new ReadOnlySortedMap(this.sorted.subMap(from, to, options), this.tags)
}
headMap(to: K, options?: BoundOptions | null): SortedMap<K, V> {
return new ReadOnlySortedMap(this.sorted.headMap(to, options), this.tags)
}
tailMap(from: K, options?: BoundOptions | null): SortedMap<K, V> {
return new ReadOnlySortedMap(this.sorted.tailMap(from, options), this.tags)
}
override reversed(): SortedMap<K, V> {
return new ReadOnlySortedMap(this.sorted.reversed(), this.tags)
}
override keySet(): SortedSet<K> {
return new ReadOnlySortedSet(this.sorted.keySet(), this.tags.sortedSet)
}
}
|