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 | 110x 110x 110x 1976x 110x 5157x 13539x 712800x 13539x 49756x 49756x 55176680x 49733x 3488x 3488x 3488x 3488x 3488x 3488x 3488x 188914x 12x 8x 1196865x 2x 2x 416869x 416869x 298737x 1464486x 1464486x 152894x 214349x 214349x 214349x 214349x 6592x 6592x 6592x 207757x 214349x 214349x 23x 23x 23x 23x 214349x 10851x 83955x 83954x 83954x 83954x 187326x 83954x 83954x 83954x 83954x 83954x 83954x 2508x 2508x 2508x 32912x 13x 13x 3535x 13x 13x 456491x 456491x 456491x 249331x 7543x 7543x 248734x | // Seeded hash buckets for values compared by an equivalence. Insertion order lives in a native
// `Set` of entries, so iterating the table has native live semantics (deleted entries not yet
// reached are skipped, added ones are visited, `clear` ends iteration); the bucket array is only an
// index, rebuilt on growth, shrinking and re-seeding without touching the order.
import { finish } from '../hashing'
import { random } from '../intrinsics'
/**
* One stored value with the hash it was stored under. The hash is computed once, by the caller,
* and reused on every growth, shrink and re-seed.
*
* @internal
*/
export interface BucketEntry<V> {
readonly hash: number
readonly value: V
}
/** The smallest bucket array, and the size `clear()` returns to. */
const MIN_BUCKETS = 8
/** The largest bucket array; past it chains simply grow longer. */
const MAX_BUCKETS = 1073741824 // 2^30
/** A chain longer than this may trigger one re-seed per growth step. */
const LONG_CHAIN = 8
function randomSeed(): number {
return (random() * 4294967296) | 0
}
let seedSource: () => number = randomSeed
/**
* Replaces the source of per-table seeds (or, with `undefined`, restores the random one). For
* tests only: it makes bucket layout and re-seeding reproducible.
*
* @internal
*/
export function setBucketSeedSourceForTesting(source: (() => number) | undefined): void {
seedSource = source === undefined ? randomSeed : source
}
function newBuckets<V>(count: number): Array<Array<BucketEntry<V>> | undefined> {
const buckets: Array<Array<BucketEntry<V>> | undefined> = []
for (let i = 0; i < count; i++) buckets.push(undefined)
return buckets
}
function hasDistinctHashes<V>(chain: ReadonlyArray<BucketEntry<V>>): boolean {
const first = (chain[0] as BucketEntry<V>).hash
for (let i = 1; i < chain.length; i++)
if ((chain[i] as BucketEntry<V>).hash !== first) return true
return false
}
/**
* A hash table of entries in power-of-two buckets whose index mixes the stored hash with a
* per-table random seed, so the layout cannot be predicted from outside. The table never calls
* user code: callers hash values once and pass a `matches` test to `find`.
*
* A chain longer than 8 after an insertion triggers a re-seed (a new seed and a rebuilt index)
* only when the chain holds at least two different hashes and no re-seed has happened since the
* last growth, so values that share one hash cannot cause endless re-seeding; they cost O(chain)
* per operation instead.
*
* Time: `find`, `insert`, `remove` O(1) average, O(chain) worst; growth, shrinking and re-seeding
* O(n), amortised O(1) per insertion; `clear` O(1); iteration O(1) per step.
*
* @internal
*/
export class BucketTable<V> {
private buckets: Array<Array<BucketEntry<V>> | undefined> = newBuckets(MIN_BUCKETS)
private mask = MIN_BUCKETS - 1
private currentSeed: number = seedSource()
private reseededSinceGrowth = false
private readonly order: Set<BucketEntry<V>> = new Set()
/** The number of times the bucket array has grown (read by tests). */
growths = 0
/** The number of re-seeds (read by tests). */
reseeds = 0
/** The number of entries. */
get size(): number {
return this.order.size
}
/** The current seed (read by tests). */
get seed(): number {
return this.currentSeed
}
/** The length of the bucket array (read by tests). */
get capacity(): number {
return this.buckets.length
}
/** The bucket index of `hash` under the current seed (read by tests). */
indexOf(hash: number): number {
return finish(hash ^ this.currentSeed) & this.mask
}
/** The length of the chain `hash` falls in (read by tests). */
chainLength(hash: number): number {
const chain = this.buckets[this.indexOf(hash)]
return chain === undefined ? 0 : chain.length
}
/**
* Returns the first entry stored under `hash` whose value `matches` accepts, or `undefined`.
* `matches` runs only for entries with exactly that hash, in the order they were placed.
*/
find(hash: number, matches: (value: V) => boolean): BucketEntry<V> | undefined {
const chain = this.buckets[this.indexOf(hash)]
if (chain === undefined) return undefined
for (let i = 0; i < chain.length; i++) {
const entry = chain[i] as BucketEntry<V>
if (entry.hash === hash && matches(entry.value)) return entry
}
return undefined
}
/** Stores `value` under `hash` at the end of the order, without checking for an equal one. */
insert(hash: number, value: V): BucketEntry<V> {
const entry: BucketEntry<V> = { hash, value }
this.order.add(entry)
const length = this.buckets.length
if (this.order.size > (length >>> 2) * 3 && length < MAX_BUCKETS) {
this.growths++
this.reseededSinceGrowth = false
this.rebuild(length * 2)
} else {
this.place(entry)
}
const chain = this.buckets[this.indexOf(hash)] as Array<BucketEntry<V>>
if (chain.length > LONG_CHAIN && !this.reseededSinceGrowth && hasDistinctHashes(chain)) {
this.reseededSinceGrowth = true
this.reseeds++
this.currentSeed = seedSource()
this.rebuild(this.buckets.length)
}
return entry
}
/** Returns whether `entry` is still stored. */
contains(entry: BucketEntry<V>): boolean {
return this.order.has(entry)
}
/** Removes `entry` by its stored hash; returns whether it was stored. */
remove(entry: BucketEntry<V>): boolean {
if (!this.order.delete(entry)) return false
const index = this.indexOf(entry.hash)
const chain = this.buckets[index] as Array<BucketEntry<V>>
for (let i = 0; i < chain.length; i++) {
if (chain[i] === entry) {
chain.splice(i, 1)
break
}
}
if (chain.length === 0) this.buckets[index] = undefined
const length = this.buckets.length
if (length > MIN_BUCKETS && this.order.size < length >>> 3) this.rebuild(length >>> 1)
return true
}
/** Removes every entry. Open iterators end, as a native `Set`'s do. */
clear(): void {
this.order.clear()
this.buckets = newBuckets(MIN_BUCKETS)
this.mask = MIN_BUCKETS - 1
}
/** A live iterator over the entries in insertion order. */
entries(): IterableIterator<BucketEntry<V>> {
return this.order.values()
}
/**
* A new table with the same hashes and values in the same order, under its own seed. The
* entries are laid out once at this table's capacity, without chain checks, so copying is O(n)
* even when many entries share one hash. `clone`, when given, copies each value.
*/
copy(clone?: (value: V) => V): BucketTable<V> {
const table = new BucketTable<V>()
for (const entry of this.order) {
table.order.add({
hash: entry.hash,
value: clone === undefined ? entry.value : clone(entry.value),
})
}
table.rebuild(this.buckets.length)
return table
}
private place(entry: BucketEntry<V>): void {
const index = this.indexOf(entry.hash)
const chain = this.buckets[index]
if (chain === undefined) this.buckets[index] = [entry]
else chain.push(entry)
}
private rebuild(capacity: number): void {
this.buckets = newBuckets(capacity)
this.mask = capacity - 1
for (const entry of this.order) this.place(entry)
}
}
|