All files / src/internal/engines flat-layout.ts

100% Statements 101/101
100% Branches 61/61
100% Functions 13/13
100% Lines 91/91

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                                                                                              107x   107x   107x   107x                             1931x 1931x 1931x 1931x 3581x 3581x   1931x 1931x                             611x 611x 611x 1110x   611x 611x         136x 133x 133x                                   192071x               192071x 192071x 192071x 192071x 192071x 192071x       1298526x 1298094x 1298094x 1298094x   432x         432x 431x 431x 431x   136x 136x   136x 133x 133x               136x 136x                 431x       1012x                                       183950x               183950x 183950x 183950x 183950x 183950x 183950x       739848x 739419x 739419x 739419x   429x         429x 407x 407x 407x 146x 146x 146x 146x 146x 269x 269x 94x 94x     146x   407x 407x 291x 291x   407x       70x                         314684x 314684x 314684x 314381x 312945x 312945x 312945x 312945x 308593x 308593x   4352x     314381x         33264x    
// The native layout of a default linked hash map or set: until a call needs linked order, its
// contents live directly in a native `Map` or `Set`, which already keeps insertion order and live
// iteration. The first call that needs more makes a linked hash table from it (`fromMap`,
// `fromSet`), once.
//
// An iteration started in the native layout walks the native collection. At its first step after
// the switch it continues over the table as an iteration over linked records would: the first
// member still ahead of it in the native order that was there at the switch (its record's origin is
// its rank, 1…n) tells how far it had come, and it resumes after the rank before that one.
//
// Reads at the front use one native iterator per layout, kept with the key it last gave: every key
// before that one has been deleted, so after a delete of that key (or a `clear`) the next step
// lands on the new first key.
 
import { sameValueZero } from '../equality'
import { done, IteratorBase } from '../iteration'
import type { HashEquality } from './hash-support'
import {
  LinkedEntryIterator,
  LinkedHashTable,
  LinkedKeyIterator,
  type LinkedRecord,
  LinkedRecordIterator,
  LinkedValueIterator,
} from './linked-hash-table'
import { MISSING, nativeMapStore } from './map-stores'
 
/**
 * What a linked collection keeps for its layout: the native contents, or the table once made, and
 * the front reader of the native layout.
 *
 * @internal
 */
export interface FlatLayout<T, K = unknown, V = unknown> {
  /** The native contents; `undefined` once the table is made. */
  flat: T | undefined
  /** The table; `undefined` while the contents are native. */
  table: LinkedHashTable<K, V> | undefined
  /** The front reader's native iterator, made on the first front read. */
  front: Iterator<unknown> | undefined
  /** The key the front reader last gave, while it is the first key; `MISSING` otherwise. */
  frontKey: unknown
  /** The value of `frontKey` in a map with an eviction callback. */
  frontValue: unknown
}
 
/** The iteration kinds of `FlatMapIterator`. @internal */
export const KEYS = 0
/** @internal */
export const VALUES = 1
/** @internal */
export const ENTRIES = 2
/** Keys in the native layout, records after the switch: the handles of a linked map. @internal */
export const HANDLES = 3
 
/**
 * A table of the mappings of `map`, in its order, ranked 1…n. Each value of `map` is replaced by
 * its record and `map` itself becomes the store, so the keys keep their native order and open
 * iterators of `map` stay valid. No user calls.
 *
 * Time: O(n).
 *
 * @internal
 */
export function tableFromMap<K, V>(
  map: Map<K, V>,
  equality: HashEquality<K>,
): LinkedHashTable<K, V> {
  const records = map as unknown as Map<K, LinkedRecord<K, V>>
  const table = new LinkedHashTable<K, V>(equality, nativeMapStore(records))
  const entries = map.entries()
  for (let step = entries.next(); step.done !== true; step = entries.next()) {
    const key = step.value[0]
    records.set(key, table.link(key, step.value[1], key, false))
  }
  table.promoted = table.high
  return table
}
 
/**
 * A table of the members of `set`, in its order, ranked 1…n, in a new store; `set` is left as it
 * is. No user calls.
 *
 * Time: O(n).
 *
 * @internal
 */
export function tableFromSet<E>(
  set: Set<E>,
  equality: HashEquality<E>,
): LinkedHashTable<E, undefined> {
  const table = new LinkedHashTable<E, undefined>(equality)
  const members = set.values()
  for (let step = members.next(); step.done !== true; step = members.next()) {
    table.insert(step.value, step.value, undefined, false)
  }
  table.promoted = table.high
  return table
}
 
/** Where a resumed walk starts: after the rank before `record`'s, or after every ranked record. */
function boundaryOf(record: LinkedRecord<unknown, unknown> | undefined, ranked: number): number {
  if (record === undefined) return ranked
  const origin = record.origin
  return origin >= 1 && origin <= ranked ? origin - 1 : ranked
}
 
/**
 * A live iteration of a linked map started in the native layout: native steps while the layout
 * holds, then the records of the table.
 *
 * Time: O(1) per native step; the switch costs O(1) amortised over the steps taken and the changes
 * made since.
 *
 * @internal
 */
export class FlatMapIterator<T> extends IteratorBase implements IterableIterator<T> {
  private readonly layout: FlatLayout<Map<unknown, unknown>>
  private readonly flat: Map<unknown, unknown>
  private readonly inner: Iterator<unknown>
  private readonly kind: number
  private records: Iterator<unknown> | undefined
  private finished = false
 
  /**
   * @param layout - The collection's layout.
   * @param flat - Its native `Map`, which must be `layout.flat`.
   * @param kind - `KEYS`, `VALUES`, `ENTRIES` or `HANDLES`.
   */
  constructor(layout: object, flat: object, kind: number) {
    super()
    this.layout = layout as FlatLayout<Map<unknown, unknown>>
    const map = flat as Map<unknown, unknown>
    this.flat = map
    this.kind = kind
    this.inner = kind === VALUES ? map.values() : kind === ENTRIES ? map.entries() : map.keys()
  }
 
  next(): IteratorResult<T> {
    if (this.layout.flat === this.flat) {
      const step = this.inner.next()
      if (step.done === true) this.finished = true
      return step as IteratorResult<T>
    }
    return this.linked()
  }
 
  /** A step after the switch. */
  private linked(): IteratorResult<T> {
    if (this.finished) return done()
    const kind = this.kind
    let records = this.records
    if (records === undefined) {
      // The native `Map` is the table's store now: its values are the records.
      const table = this.layout.table as LinkedHashTable<unknown, unknown>
      const ahead = this.inner.next()
      let record: LinkedRecord<unknown, unknown> | undefined
      if (ahead.done !== true) {
        const value = ahead.value
        record = (
          kind === VALUES
            ? value
            : kind === ENTRIES
              ? (value as unknown[])[1]
              : table.findPlain(value)
        ) as LinkedRecord<unknown, unknown> | undefined
      }
      const walk = new LinkedRecordIterator(table, false, boundaryOf(record, table.promoted))
      records = this.records =
        kind === KEYS
          ? new LinkedKeyIterator(walk)
          : kind === VALUES
            ? new LinkedValueIterator(walk)
            : kind === ENTRIES
              ? new LinkedEntryIterator(walk)
              : walk
    }
    return records.next() as IteratorResult<T>
  }
 
  [Symbol.iterator](): this {
    return this
  }
}
 
/**
 * A live iteration of a linked set started in the native layout: native steps while the layout
 * holds, then the members of the table. The native `Set` is left as it was at the switch, so the
 * first of its members ahead that is still in the table with its rank tells where to resume.
 *
 * Time: O(1) per native step; the switch costs O(1) amortised over the steps taken and the changes
 * made since.
 *
 * @internal
 */
export class FlatSetIterator<T> extends IteratorBase implements IterableIterator<T> {
  private readonly layout: FlatLayout<Set<unknown>>
  private readonly flat: Set<unknown>
  private readonly inner: Iterator<unknown>
  private readonly pairs: boolean
  private records: LinkedRecordIterator<unknown, unknown> | undefined
  private finished = false
 
  /**
   * @param layout - The collection's layout.
   * @param flat - Its native `Set`, which must be `layout.flat`.
   * @param pairs - Whether each step is a `[member, member]` pair.
   */
  constructor(layout: object, flat: object, pairs: boolean) {
    super()
    const set = flat as Set<unknown>
    this.layout = layout as FlatLayout<Set<unknown>>
    this.flat = set
    this.pairs = pairs
    this.inner = pairs ? set.entries() : set.values()
  }
 
  next(): IteratorResult<T> {
    if (this.layout.flat === this.flat) {
      const step = this.inner.next()
      if (step.done === true) this.finished = true
      return step as IteratorResult<T>
    }
    return this.linked()
  }
 
  /** A step after the switch. */
  private linked(): IteratorResult<T> {
    if (this.finished) return done()
    const pairs = this.pairs
    let records = this.records
    if (records === undefined) {
      const table = this.layout.table as LinkedHashTable<unknown, unknown>
      const ranked = table.promoted
      const inner = this.inner
      let boundary = ranked
      for (let step = inner.next(); step.done !== true; step = inner.next()) {
        const record = table.findPlain(pairs ? (step.value as unknown[])[0] : step.value)
        if (record !== undefined && record.origin >= 1 && record.origin <= ranked) {
          boundary = record.origin - 1
          break
        }
      }
      records = this.records = new LinkedRecordIterator(table, false, boundary)
    }
    const step = records.next() as IteratorResult<unknown>
    if (step.done !== true) {
      const key = (step.value as LinkedRecord<unknown, unknown>).key
      step.value = pairs ? [key, key] : key
    }
    return step as IteratorResult<T>
  }
 
  [Symbol.iterator](): this {
    return this
  }
}
 
/**
 * The first key of the native layout, or `MISSING` when it is empty. With `pairs` (a map), the
 * reader walks `entries()` and keeps the value of the key it reads in `frontValue`.
 *
 * Time: O(1) amortised: the reader's steps never exceed the keys ever inserted plus the reads.
 *
 * @internal
 */
export function frontKey(layout: object, flat: object, pairs: boolean): unknown {
  const reader = layout as FlatLayout<unknown>
  const native = flat as Map<unknown, unknown> | Set<unknown>
  if (native.size === 0) return MISSING
  if (reader.frontKey === MISSING) {
    let front = reader.front
    if (front === undefined) front = reader.front = pairs ? native.entries() : native.values()
    const value = front.next().value
    if (pairs) {
      reader.frontKey = (value as unknown[])[0]
      reader.frontValue = (value as unknown[])[1]
    } else {
      reader.frontKey = value
    }
  }
  return reader.frontKey
}
 
/** Whether `key` is the key the front reader holds (SameValueZero). @internal */
export function isFront(layout: object, key: unknown): boolean {
  return sameValueZero((layout as FlatLayout<unknown>).frontKey, key)
}