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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | 1785x 11755x 85x 85x 85x 254x 11556x 831x 692x 144x 3125x 60x 173x 177x 351x 165x 1x 1785x 539x 539x 10552x 41496x 2571x 30976x 30976x 26525x 26525x 7915x 252x 6095x 759x 671x 143x 143x 3350x 21x 34x 73x 110x 162x 110x 539x 2x 54361x 713x 713x 713x 20649x 99230x 99113x 98927x 98927x 244646x 15272x 15272x 62060x 62060x 62060x 47764x 47764x 21846x 21846x 21846x 13901x 13901x 494x 7385x 1481x 1325x 274x 3385x 30x 52x 28x 28x 85x 85x 58x 5387x 5387x 5438x 5387x 713x 2x 3032x 1784x 537x 711x 3037x 3037x 3037x 3037x 3037x 1517x 140826x 41652x 25036x 137329x 24422x 24422x 17843x 122486x 122486x 33006x 33006x 33006x 33006x 89480x 89480x 1895x 1895x 1075x 1008540x 1008540x 1008540x 1075x 3507x 93121x 93121x 93067x 39378x 39711x 39711x 39711x 39708x 1304x 1000x 1000x 2947x 2947x 561x 228x 228x 333x 2355x 70x 70x 134x 52x 69x 66x 135x 251x 111x 111x 111x 331x 94x 145x 78x 78x 18x 18x 18x 9631x 9487x 9487x 18x 124x 124x 9983x 124x 5674x 5x | // The engine behind `HashSet`. The public class holds one engine and delegates to it, so nothing
// here reaches the published types.
//
// The engine picks one store at construction and keeps it: a native `Set` (SameValueZero), a
// native `Map` from derived key to member (a `key` function), or seeded buckets (an equivalence).
// Every store keeps insertion order in native storage, so iteration is live exactly as a native
// `Set`'s is. A member's derived key or hash is computed once, when it is stored, and reused for
// every later removal by stored entry.
import type { Cursor } from '../../interfaces/collection'
import type { SetCollection } from '../../interfaces/set'
import {
type HashFunction,
type KeyOf,
type ProtocolMethods,
protocolOf,
sameValueZero,
} from '../equality'
import type { CheckedEquivalence } from '../equivalence'
import { LiveCursor, MappedLiveIterator, NativeLiveIterator } from '../iteration'
import { deriveKey } from '../key'
import { AbstractSet } from '../skeletons/abstract-set'
import { type HashEquality, normalise, Operation } from './hash-support'
import { type BucketEntry, BucketTable } from './hash-table'
/**
* How a set decides that two values are equal.
*
* @internal
*/
export type HashSetEquality<E> = HashEquality<E>
/**
* Storage for one equality. Handles are what the store's live iteration yields (members, map
* entries or bucket entries); removing by handle never calls user code.
*
* @internal
*/
export interface Store<E> {
readonly size: number
has(value: E, op: Operation): boolean
/** Adds `value` unless an equal member is stored; returns whether it was added. */
add(value: E, op: Operation): boolean
delete(value: E, op: Operation): boolean
clear(): void
values(): IterableIterator<E>
handles(): Iterator<unknown>
member(handle: unknown): E
/** Whether the entry `handle` stands for is still stored. */
holds(handle: unknown): boolean
removeHandle(handle: unknown): void
/** A membership test of stored handles against `items`, under this store's equality. */
lookup(items: readonly E[], op: Operation): (handle: unknown) => boolean
memberHashes(hash: HashFunction): number
copy(): Store<E>
/**
* The native `Set` that holds the members, when they are compared with SameValueZero and no
* user code runs on a lookup; `undefined` for the keyed and bucket stores.
*/
nativeSet(): Set<E> | undefined
}
class NativeStore<E> implements Store<E> {
private readonly set: Set<E>
constructor(set: Set<E>) {
this.set = set
}
get size(): number {
return this.set.size
}
has(value: E): boolean {
return this.set.has(value)
}
add(value: E): boolean {
const before = this.set.size
this.set.add(value)
return this.set.size !== before
}
delete(value: E): boolean {
return this.set.delete(value)
}
clear(): void {
this.set.clear()
}
values(): IterableIterator<E> {
return new NativeLiveIterator(this.set.values())
}
handles(): Iterator<unknown> {
return this.set.values()
}
member(handle: unknown): E {
return handle as E
}
holds(handle: unknown): boolean {
return this.set.has(handle as E)
}
removeHandle(handle: unknown): void {
this.set.delete(handle as E)
}
lookup(items: readonly E[]): (handle: unknown) => boolean {
const set = new Set<E>(items)
return (handle) => set.has(handle as E)
}
memberHashes(hash: HashFunction): number {
let sum = 0
for (const element of this.set) sum = (sum + hash(element)) | 0
return sum
}
copy(): Store<E> {
return new NativeStore(new Set(this.set))
}
nativeSet(): Set<E> {
return this.set
}
}
type KeyedEntry<E> = [unknown, E]
class KeyedStore<E> implements Store<E> {
private readonly key: KeyOf<E>
private readonly map: Map<unknown, E>
constructor(key: KeyOf<E>, map: Map<unknown, E>) {
this.key = key
this.map = map
}
get size(): number {
return this.map.size
}
private keyOf(value: E, op: Operation): unknown {
return op.after(deriveKey(this.key, value, op.where))
}
has(value: E, op: Operation): boolean {
return this.map.has(this.keyOf(value, op))
}
add(value: E, op: Operation): boolean {
const key = this.keyOf(value, op)
if (this.map.has(key)) return false
this.map.set(key, value)
return true
}
delete(value: E, op: Operation): boolean {
return this.map.delete(this.keyOf(value, op))
}
clear(): void {
this.map.clear()
}
values(): IterableIterator<E> {
return new NativeLiveIterator(this.map.values())
}
handles(): Iterator<unknown> {
return this.map.entries()
}
member(handle: unknown): E {
return (handle as KeyedEntry<E>)[1]
}
holds(handle: unknown): boolean {
const [key, value] = handle as KeyedEntry<E>
return this.map.has(key) && sameValueZero(this.map.get(key), value)
}
removeHandle(handle: unknown): void {
this.map.delete((handle as KeyedEntry<E>)[0])
}
lookup(items: readonly E[], op: Operation): (handle: unknown) => boolean {
const keys = new Set<unknown>()
for (const item of items) keys.add(this.keyOf(item, op))
return (handle) => keys.has((handle as KeyedEntry<E>)[0])
}
memberHashes(hash: HashFunction): number {
let sum = 0
for (const key of this.map.keys()) sum = (sum + hash(key)) | 0
return sum
}
nativeSet(): undefined {
return undefined
}
copy(): Store<E> {
return new KeyedStore(this.key, new Map(this.map))
}
}
function entryValue<E>(entry: BucketEntry<E>): E {
return entry.value
}
class BucketStore<E> implements Store<E> {
private readonly equivalence: CheckedEquivalence
private readonly builtIn: boolean
private readonly table: BucketTable<E>
constructor(equivalence: CheckedEquivalence, builtIn: boolean, table: BucketTable<E>) {
this.equivalence = equivalence
this.builtIn = builtIn
this.table = table
}
get size(): number {
return this.table.size
}
/** The protocol methods of `value`, read once per operation, for the built-in equivalence. */
private methodsOf(value: E): ProtocolMethods | undefined {
return this.builtIn ? protocolOf(value) : undefined
}
private hashOf(value: E, op: Operation, methods: ProtocolMethods | undefined): number {
return op.after(this.equivalence.hash(value, op.where, methods))
}
private find(
value: E,
hash: number,
op: Operation,
methods: ProtocolMethods | undefined,
): BucketEntry<E> | undefined {
const equivalence = this.equivalence
return this.table.find(hash, (stored) =>
op.after(equivalence.equals(value, stored, op.where, methods)),
)
}
has(value: E, op: Operation): boolean {
const methods = this.methodsOf(value)
return this.find(value, this.hashOf(value, op, methods), op, methods) !== undefined
}
add(value: E, op: Operation): boolean {
const methods = this.methodsOf(value)
const hash = this.hashOf(value, op, methods)
if (this.find(value, hash, op, methods) !== undefined) return false
this.table.insert(hash, value)
return true
}
delete(value: E, op: Operation): boolean {
const methods = this.methodsOf(value)
const entry = this.find(value, this.hashOf(value, op, methods), op, methods)
if (entry === undefined) return false
this.table.remove(entry)
return true
}
clear(): void {
this.table.clear()
}
values(): IterableIterator<E> {
return new MappedLiveIterator(this.table.entries(), entryValue)
}
handles(): Iterator<unknown> {
return this.table.entries()
}
member(handle: unknown): E {
return (handle as BucketEntry<E>).value
}
holds(handle: unknown): boolean {
return this.table.contains(handle as BucketEntry<E>)
}
removeHandle(handle: unknown): void {
this.table.remove(handle as BucketEntry<E>)
}
lookup(items: readonly E[], op: Operation): (handle: unknown) => boolean {
const argument = new BucketTable<E>()
for (const item of items) argument.insert(this.hashOf(item, op, this.methodsOf(item)), item)
const equivalence = this.equivalence
return (handle) => {
const entry = handle as BucketEntry<E>
return (
argument.find(entry.hash, (item) =>
op.after(equivalence.equals(entry.value, item, op.where)),
) !== undefined
)
}
}
memberHashes(hash: HashFunction): number {
let sum = 0
for (const entry of this.table.entries()) {
sum = (sum + (this.builtIn ? hash(entry.value) : entry.hash)) | 0
}
return sum
}
nativeSet(): undefined {
return undefined
}
copy(): Store<E> {
return new BucketStore(this.equivalence, this.builtIn, this.table.copy())
}
}
function newStore<E>(equality: HashSetEquality<E>): Store<E> {
switch (equality.kind) {
case 'native':
return new NativeStore(new Set<E>())
case 'keyed':
return new KeyedStore(equality.key, new Map<unknown, E>())
default:
return new BucketStore(equality.equivalence, equality.builtIn, new BucketTable<E>())
}
}
/**
* A set whose members are compared with SameValueZero, by derived key, or by an equivalence, and
* kept in insertion order with live iteration.
*
* @internal
*/
export class HashSetEngine<E> extends AbstractSet<E> {
private readonly store: Store<E>
private readonly facade: SetCollection<E>
private readonly equality: HashSetEquality<E>
/**
* @param facade - The public set, for identity and callbacks.
* @param equality - How members are compared.
* @param store - The storage to use; a new empty one by default.
*/
constructor(
facade: SetCollection<E>,
equality: HashSetEquality<E>,
store: Store<E> = newStore(equality),
) {
super()
this.facade = facade
this.equality = equality
this.store = store
this.native = store.nativeSet()
}
/**
* The store's native `Set` when no user code runs on a lookup (the default SameValueZero set):
* `has`, `add` and `delete` then call it once, with no `Operation` and no label.
*/
private readonly native: Set<E> | undefined
get owner(): SetCollection<E> {
return this.facade
}
get [Symbol.toStringTag](): string {
return 'HashSet'
}
get size(): number {
return this.store.size
}
[Symbol.iterator](): IterableIterator<E> {
return this.store.values()
}
/** Starts an operation whose errors name `HashSet.<method>`. */
private operation(method: string): Operation {
return new Operation(this.changes, `${this.className}.${method}`)
}
protected hasMember(element: E, method: string): boolean {
const native = this.native
if (native !== undefined) return native.has(normalise(element))
return this.store.has(normalise(element), this.operation(method))
}
add(element: E): this {
const native = this.native
if (native !== undefined) {
const before = native.size
native.add(normalise(element))
if (native.size !== before) this.changes.structural()
return this
}
this.addElement(element, this.operation('add'))
return this
}
/** Adds each element of `elements` in order, naming `where` in errors (construction). */
addInitial(elements: Iterable<E>, where: string): void {
const native = this.native
if (native !== undefined) {
// One native call per element: the size tells a new member from a present one.
for (const element of elements) {
const before = native.size
native.add(normalise(element))
if (native.size !== before) this.changes.structural()
}
return
}
for (const element of elements) this.addElement(element, new Operation(this.changes, where))
}
private addElement(element: E, op: Operation): boolean {
const added = this.store.add(normalise(element), op)
if (added) this.changes.structural()
return added
}
override delete(element: E): boolean {
return this.deleteElement(element, 'delete')
}
private deleteElement(element: E, method: string): boolean {
const native = this.native
const removed =
native !== undefined
? native.delete(normalise(element))
: this.store.delete(normalise(element), this.operation(method))
if (removed) this.changes.structural()
return removed
}
override clear(): void {
if (this.store.size === 0) return
this.store.clear()
this.changes.structural()
}
cursor(): Cursor<E> {
const store = this.store
return new LiveCursor<E, unknown>(
store.handles(),
(handle) => {
if (store.holds(handle)) {
store.removeHandle(handle)
this.changes.structural()
} else {
this.deleteElement(store.member(handle), 'cursor')
}
},
`${this.className}.cursor`,
(handle) => store.member(handle),
)
}
override addAll(elements: Iterable<E>): boolean {
const items = this.readAll(elements, 'addAll')
let added = false
for (const item of items) if (this.addElement(item, this.operation('addAll'))) added = true
return added
}
override removeAll(elements: Iterable<E>): boolean {
return this.removeMatching('removeAll', elements, true)
}
override retainAll(elements: Iterable<E>): boolean {
return this.removeMatching('retainAll', elements, false)
}
/**
* Builds a lookup of `elements` under this set's equality (user code runs only here and while
* probing it), decides every member, then removes the decided ones by stored entry.
*/
private removeMatching(method: string, elements: Iterable<E>, remove: boolean): boolean {
const items = this.readAll(elements, method)
for (let i = 0; i < items.length; i++) items[i] = normalise(items[i] as E)
const op = this.operation(method)
const matches = this.store.lookup(items, op)
const doomed: unknown[] = []
for (const handle of this.handleArray()) if (matches(handle) === remove) doomed.push(handle)
if (doomed.length === 0) return false
for (const handle of doomed) this.store.removeHandle(handle)
this.changes.structural()
return true
}
/** Removes the members marked in `flags` (iteration order) by stored entry. */
protected override removeMarked(flags: readonly boolean[]): void {
const handles = this.handleArray()
let removed = false
for (let i = 0; i < handles.length; i++) {
if (flags[i] === true) {
this.store.removeHandle(handles[i])
removed = true
}
}
Eif (removed) this.changes.structural()
}
private handleArray(): unknown[] {
const out: unknown[] = []
const handles = this.store.handles()
for (let step = handles.next(); step.done !== true; step = handles.next()) out.push(step.value)
return out
}
protected memberHashes(hash: HashFunction): number {
return this.store.memberHashes(hash)
}
/** A new engine for `facade` with the same equality and the same stored keys and hashes. */
copy(facade: SetCollection<E>): HashSetEngine<E> {
return new HashSetEngine(facade, this.equality, this.store.copy())
}
}
|