All files / collections/src sortedMap.ts

91.42% Statements 64/70
100% Branches 15/15
80% Functions 16/20
92.64% Lines 63/68

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            2x 2x 2x   2x     31x 2x 2x     31x 31x 31x 67x             1x       3x 8x         3x   3x 3x 3x 3x 3x 5x         5x 2x 1x     2x   3x       3x                               6x 16x           3x         3x 3x 3x         2x   2x 3x   3x 1x 1x 2x 1x 1x     1x                 3x   3x 3x   3x 5x   5x         5x 3x 3x 2x 1x 1x     5x     3x 1x 1x     3x       9x       27x     31x    
import {
  clear,
  deleteAt,
  findIndex,
  insertAt,
  findAndDelete,
} from "@benneq/array";
import { Comparator, entryKeyComparator } from "@benneq/comparator";
import { distinct, every } from "@benneq/iterable";
import { Entry } from "@benneq/object";
import { AbstractMap } from "./abstractMap";
import { SortedSet } from "./sortedSet";
 
export class SortedMap<K, V> extends AbstractMap<K, V> {
  readonly #comparator;
  readonly #value;
 
  constructor(comparator: Comparator<K>, entries?: Iterable<Entry<K, V>>) {
    super();
    this.#comparator = comparator;
    this.#value = entries
      ? [...distinct<Entry<K, V>>((entry) => entry[0])(entries)].sort(
          entryKeyComparator(comparator)
        )
      : [];
  }
 
  clear(): void {
    clear(this.#value);
  }
 
  delete(key: K): boolean {
    return findAndDelete<Entry<K, V>>(
      (entry) => !this.#comparator(key, entry[0])
    )(this.#value);
  }
 
  deleteAll(keys: SortedSet<K>) {
    const iterator = keys[Symbol.iterator]();
 
    const values = this.#value;
    let i = 0;
    let next = iterator.next();
    let modified = false;
    while (i < values.length && !next.done) {
      const comparisonResult = this.#comparator(
        next.value,
        (values[i] as [K, V])[0]
      );
 
      if (comparisonResult <= 0) {
        if (!comparisonResult) {
          modified = deleteAt(i)(values);
        }
 
        next = iterator.next();
      } else {
        i++;
      }
    }
 
    return modified;
  }
 
  deleteAt(index: number, deleteCount?: number): boolean {
    return deleteAt(index, deleteCount)(this.#value);
  }
 
  at(index: number): [K, V] | undefined {
    return this.#value.at(index);
  }
 
  get(key: K): V | undefined {
    return this.#value.find((value) => !this.#comparator(value[0], key))?.[1];
  }
 
  indexOf(key: K, fromIndex?: number): number {
    return findIndex<Entry<K, V>>(
      (entry) => !this.#comparator(key, entry[0]),
      fromIndex
    )(this.#value);
  }
 
  has(key: K, fromIndex?: number): boolean {
    return this.indexOf(key, fromIndex) >= 0;
  }
 
  hasAll(keys: SortedSet<K>): boolean {
    let fromIndex: number | undefined;
    return every((key: K) => {
      fromIndex = this.indexOf(key, fromIndex);
      return fromIndex >= 0;
    })(keys);
  }
 
  set(key: K, value: V): this {
    let i = 0;
 
    for (const entry of this) {
      const comparisonResult = this.#comparator(key, entry[0]);
 
      if (!comparisonResult) {
        entry[1] = value;
        return this;
      } else if (comparisonResult < 0) {
        insertAt(i, [key, value])(this.#value);
        return this;
      }
 
      i++;
    }
 
    this.#value.push([key, value]);
 
    return this;
  }
 
  setAll(entries: SortedMap<K, V>): this {
    const iterator = entries[Symbol.iterator]();
 
    let i = 0;
    let next = iterator.next();
 
    while (i < this.#value.length && !next.done) {
      const [key, value] = next.value;
 
      const comparisonResult = this.#comparator(
        key,
        (this.#value[i] as Entry<K, V>)[0]
      );
 
      if (!comparisonResult) {
        (this.#value[i] as Entry<K, V>)[1] = value;
        next = iterator.next();
      } else if (comparisonResult < 0) {
        insertAt(i, [key, value])(this.#value);
        next = iterator.next();
      }
 
      i++;
    }
 
    while (!next.done) {
      this.#value.push(next.value);
      next = iterator.next();
    }
 
    return this;
  }
 
  get size(): number {
    return this.#value.length;
  }
 
  entries(): IterableIterator<[K, V]> {
    return this.#value[Symbol.iterator]();
  }
 
  [Symbol.toStringTag] = "SortedMap";
}