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 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 | 105x 105x 630009x 630009x 626529x 3479x 3479x 3479x 6x 3473x 3473x 335x 9x 326x 12x 3452x 3452x 6x 3446x 529988x 529988x 529988x 24512x 23568x 944x 939x 5x 529963x 529963x 100073x 100073x 100073x 100073x 100073x 100073x 432x 47x 123766x 174x 1516069x 1846583x 16x 318x 250004x 51971x 84580x 6932x 798648x 798648x 63960x 10864x 10174x 10174x 12545x 12545x 115187x 12485x 12787x 2454x 51442x 11573x 44708x 11113x 51227x 5138x 46145x 44655x 10336x 5875x 73108x 53978x 127793x 24599x 804824x 1194x 2574x 2524x 145164x 402347x 402347x 402347x 122779x 420x 273x 120425x 549049x 10x | /**
* `LinkedHashMap`: a map in insertion or access order that can be read and changed at both ends and
* bounded in size.
*
* @packageDocumentation
*/
import { IllegalArgumentError } from '../errors/errors'
import type { SequencedCollection } from '../interfaces/list'
import type { MapCollection, MapEntry } from '../interfaces/map'
import type { SequencedMap } from '../interfaces/sequenced-map'
import type { SequencedSet } from '../interfaces/sequenced-set'
import { brandedHasInstance, MAP_COLLECTION_BRAND, SEQUENCED_MAP_BRAND } from '../internal/brand'
import { describeValue, isIterable } from '../internal/checks'
import { readEquality } from '../internal/engines/hash-support'
import {
LinkedHashMapEngine,
type LinkedMapSettings,
} from '../internal/engines/linked-hash-map-engine'
import {
type Equivalence,
equalsSymbol,
type HashFunction,
hashCodeSymbol,
type KeyOf,
} from '../internal/equality'
import { readPairs } from '../internal/map-input'
import { INSPECT } from '../internal/output'
const LINKED_HASH_MAP_BRAND: unique symbol = /*#__PURE__*/ Symbol.for(
'javascript-collections-framework/1/LinkedHashMap',
)
/** The largest integer a `number` holds exactly. */
const MAX_SAFE_INTEGER = 9007199254740991
/**
* Options for a `LinkedHashMap`. At most one of `key` and `equivalence` may be given; with neither,
* keys compare with SameValueZero, as in native `Map`. With `accessOrder: true` and a `maxSize`,
* the map is a least-recently-used cache.
*
* @example
* ```ts
* const lru: LinkedHashMapOptions<string, Response> = {
* accessOrder: true,
* maxSize: 100,
* onEvict: (response, url) => console.log('dropped', url),
* }
* ```
*/
export interface LinkedHashMapOptions<K, V> {
/**
* Derives the primitive key a key is found by; two keys are the same key when their derived keys
* are SameValueZero-equal. Called once per key per operation, never again for a stored key.
*/
key?: KeyOf<K> | undefined
/**
* Decides key equality by its `equals` and `hash`. `valueEquivalence` honours the
* `equalsSymbol`/`hashCodeSymbol` protocol; `sameValueZeroEquivalence` is the default behaviour.
*/
equivalence?: Equivalence<K> | undefined
/**
* When `true`, reading or writing a present key moves its mapping to the end, so the first
* mapping is the least recently used. Default `false`: insertion order.
*/
accessOrder?: boolean | undefined
/**
* The most mappings the map keeps: inserting a new key past it removes mappings from the front
* (never the one just inserted). A positive safe integer; no bound when omitted.
*/
maxSize?: number | undefined
/**
* Called as `onEvict(value, key, map)`, with `this` undefined, once per mapping removed by
* `maxSize`, in eviction order, after the call that evicted it has finished its changes.
*/
onEvict?: ((value: V, key: K, map: LinkedHashMap<K, V>) => void) | undefined
}
/** Reads and checks the options a `LinkedHashMap` adds to the key options, each property once. */
function readSettings<K, V>(options: unknown, where: string): LinkedMapSettings<K, V> {
const equality = readEquality<K>(options, where)
if (options === undefined || options === null) {
return { equality, accessOrder: false, maxSize: undefined, onEvict: undefined }
}
const record = options as { accessOrder?: unknown; maxSize?: unknown; onEvict?: unknown }
const accessOrder = record.accessOrder
if (accessOrder !== undefined && typeof accessOrder !== 'boolean') {
throw new TypeError(`${where}: accessOrder is not a boolean (${describeValue(accessOrder)})`)
}
const maxSize = record.maxSize
if (maxSize !== undefined) {
if (typeof maxSize !== 'number') {
throw new TypeError(`${where}: maxSize is not a number (${describeValue(maxSize)})`)
}
if (!(maxSize > 0 && maxSize <= MAX_SAFE_INTEGER && maxSize % 1 === 0)) {
throw new IllegalArgumentError(
`${where}: maxSize must be a positive safe integer (${describeValue(maxSize)})`,
)
}
}
const onEvict = record.onEvict
if (onEvict !== undefined && typeof onEvict !== 'function') {
throw new TypeError(`${where}: onEvict is not a function (${describeValue(onEvict)})`)
}
return {
equality,
accessOrder: accessOrder === true,
maxSize: maxSize as number | undefined,
onEvict: onEvict as ((value: V, key: K, map: never) => void) | undefined,
}
}
/**
* A map that behaves like native `Map` and `HashMap` — insertion order, SameValueZero keys (`NaN`
* is one key, `-0` is stored as `0`), `set` returning the map and keeping a present key where it
* is, live iteration, keys compared by value on request — and can also be read and changed at both
* ends: `firstEntry` and `lastEntry`, `pollFirstEntry` and `pollLastEntry`, `putFirst` and
* `putLast`, which insert an absent key at that end or move the present mapping there, and a live
* `reversed()` view. Each takes O(1) amortised (average for those that look up or remove).
*
* A map without the `key`, `equivalence` or `accessOrder` option keeps its mappings in a native
* `Map` until the first call that needs linked order: `putFirst`, `putLast` of a present key,
* `lastEntry`, `pollLastEntry`, any call through `reversed()`, or any call through `keySet()`,
* `entrySet()` or `valueCollection()` other than `size`, `isEmpty` and `has`. That call costs
* O(n), once; this is why the ends, `putFirst`, `putLast`, and the inserting methods of a map
* with `maxSize` (each eviction) are amortised.
*
* A map with `accessOrder: true` and neither `key` nor `equivalence` keeps its mappings in a
* compact form until the first call on it other than `get`, `has`, `set`, `put` (unless both
* `maxSize` and `onEvict` are given), `delete`, `size`, `isEmpty` and `clear`: any other method,
* iteration, an entry, or a call through `reversed()` or a view other than `size` and `isEmpty`.
* That call costs O(n), once, which is why those other operations are amortised for such a map.
* A map built with entries, or made by `copy`, starts in the linked form.
*
* With `accessOrder: true`, every read or write of a present key (`get`, `getOrDefault`, `set`,
* `put`, `replace`, `getOrInsert`, `getOrInsertComputed`, `compute`, `computeIfPresent`, `merge`,
* and `putAll`, the constructor and `from` for keys already present) moves its mapping to the end,
* so the first mapping is the least recently used. `has`, iteration, the views, the ends, entries,
* `replaceAll`, `equals`, hashing and printing never move anything. With `maxSize`, inserting a
* new key past the bound removes mappings from the front, never the one just inserted, and
* `onEvict(value, key, map)` is told about each after the call that evicted it; together they make
* an LRU cache.
*
* Iteration is live, as with native `Map`: mappings deleted before they are reached are skipped,
* mappings inserted at the end the loop is heading to are visited, those inserted at the end it
* started from are not, and nothing throws `ConcurrentModificationError`. `putFirst` and `putLast`
* act as a delete followed by an insertion at the new end. A move made by an access never makes an
* open loop return a mapping twice — on a forward loop an unvisited mapping is visited once at its
* new place, on a loop over `reversed()` it is skipped — so `for (const k of map.keys()) map.get(k)`
* ends; such a forward loop keeps a record of the mappings it returned until it is dropped.
*
* Changing a key's derived key or hash while it is in the map leaves lookups for it unspecified;
* the mapping can still be removed through the ends, a view's cursor, `removeIf`, `removeAll`,
* `retainAll`, or `clear`. Two maps are equal when they have the same size and each maps every
* key of the other to an equal value, in any order. A `LinkedHashMap` is not a `HashMap`
* (`instanceof HashMap` is `false`); both are maps. The class is not designed to be subclassed.
*
* @example
* ```ts
* const cache = new LinkedHashMap<string, number>({ accessOrder: true, maxSize: 2 })
* cache.set('a', 1).set('b', 2)
* cache.get('a') // 1; 'a' is now the most recently used
* cache.set('c', 3) // evicts 'b', the least recently used
* const keys = [...cache.keys()] // ['a', 'c']
*
* const recent = new LinkedHashMap([['x', 1], ['y', 2]])
* recent.putFirst('y', 3).toString() // "LinkedHashMap(2) {'y' => 3, 'x' => 1}"
* ```
*
* @sealed
*/
export class LinkedHashMap<K, V> implements SequencedMap<K, V> {
private engine: LinkedHashMapEngine<K, V>
/**
* Creates a map holding each mapping of `entries`, set one by one in order with the options'
* access moves and evictions; empty when `entries` is omitted, `undefined` or `null`. Every item
* and option is checked before anything is stored; options are never taken from `entries`.
*
* Time: O(m) average for `m` items, plus the `key` or `equivalence` calls.
*
* @param entries - `[key, value]` arrays, map entries, a native `Map` or another map.
* @param options - `key` or `equivalence`, `accessOrder`, `maxSize`, `onEvict`; `null` means
* none.
* @throws TypeError when `entries` is neither iterable nor `null`/`undefined`, an item is not an
* array of length 2 or a map entry, `options` is not an object, both `key` and `equivalence`
* are given, `key` is not a function, `equivalence` lacks an `equals` or `hash` function,
* `accessOrder` is not a boolean, `maxSize` is not a number or `onEvict` is not a function.
* @throws IllegalArgumentError when `maxSize` is not a positive safe integer.
* @throws Error whatever `onEvict` throws, after every mapping is set.
*
* @example
* ```ts
* new LinkedHashMap([['a', 1], ['b', 2]]) // LinkedHashMap(2) {'a' => 1, 'b' => 2}
* new LinkedHashMap([['a', 1], ['b', 2], ['c', 3]], { maxSize: 2 }) // {'b' => 2, 'c' => 3}
* ```
*/
constructor(
entries?: Iterable<readonly [K, V] | MapEntry<K, V>> | null,
options?: LinkedHashMapOptions<K, V> | null,
)
/**
* Creates an empty map with the given options. An object that is not iterable is read as the
* options.
*
* Time: O(1).
*
* @param options - `key` or `equivalence`, `accessOrder`, `maxSize`, `onEvict`.
* @throws TypeError when an option has the wrong type, as for the constructor with entries.
* @throws IllegalArgumentError when `maxSize` is not a positive safe integer.
*
* @example
* ```ts
* const lru = new LinkedHashMap<string, string>({ accessOrder: true, maxSize: 100 })
* ```
*/
constructor(options: LinkedHashMapOptions<K, V>)
constructor(
entriesOrOptions?:
| Iterable<readonly [K, V] | MapEntry<K, V>>
| LinkedHashMapOptions<K, V>
| null,
options?: LinkedHashMapOptions<K, V> | null,
) {
let pairs: Array<[K, V]> | undefined
let given: unknown = options
const first: unknown = entriesOrOptions
if (first === undefined || first === null) {
// empty
} else if (isIterable(first)) {
pairs = readPairs<K, V>(first, 'LinkedHashMap')
} else if (typeof first === 'object' && (options === undefined || options === null)) {
given = first
} else {
throw new TypeError(`LinkedHashMap: entries is not iterable (${describeValue(first)})`)
}
this.engine = new LinkedHashMapEngine<K, V>(this, readSettings<K, V>(given, 'LinkedHashMap'))
if (pairs !== undefined) this.engine.build(pairs, 'LinkedHashMap')
}
/**
* Returns a new map of the mappings of `entries`, set one by one in order with the options'
* access moves and evictions. Every item and option is checked before anything is stored.
*
* Time: O(m) average for `m` items, plus the `key` or `equivalence` calls.
*
* @param entries - `[key, value]` arrays, map entries, a native `Map` or another map.
* @param options - As for the constructor; `null` means none.
* @returns A new map.
* @throws TypeError when `entries` is not iterable (`null` and `undefined` included), an item is
* not a pair or an entry, or an option has the wrong type.
* @throws IllegalArgumentError when `maxSize` is not a positive safe integer.
* @throws Error whatever `onEvict` throws, after every mapping is set.
*
* @example
* ```ts
* LinkedHashMap.from(Object.entries({ a: 1, b: 2 })) // LinkedHashMap(2) {'a' => 1, 'b' => 2}
* ```
*/
static from<K, V>(
entries: Iterable<readonly [K, V] | MapEntry<K, V>>,
options?: LinkedHashMapOptions<K, V> | null,
): LinkedHashMap<K, V> {
const pairs = readPairs<K, V>(entries, 'LinkedHashMap.from')
const settings = readSettings<K, V>(options, 'LinkedHashMap.from')
const map = new LinkedHashMap<K, V>()
map.engine = new LinkedHashMapEngine<K, V>(map, settings)
map.engine.build(pairs, 'LinkedHashMap.from')
return map
}
/**
* Returns whether `value` is a `LinkedHashMap`, including one created by another copy of the
* library with the same major version. Its reversed view is not one.
*
* Time: O(1).
*
* @param value - The value on the left of `instanceof`.
* @returns `true` when `value` is a `LinkedHashMap`.
*
* @example
* ```ts
* new LinkedHashMap() instanceof LinkedHashMap // true
* ```
*/
static [Symbol.hasInstance](value: unknown): boolean {
// biome-ignore lint/complexity/noThisInStatic: `this` is the class on the right of `instanceof`
return brandedHasInstance(this, LinkedHashMap, LINKED_HASH_MAP_BRAND, value)
}
/**
* Marks instances of this class so `instanceof` recognises them across copies of the package.
* Its key is not exported, so the getter is not called directly. Time: O(1).
*/
get [LINKED_HASH_MAP_BRAND](): true {
return true
}
/**
* Marks maps so `isMapCollection` recognises them across copies of the package. Its key is not
* exported, so the getter is not called directly. Time: O(1).
*/
get [MAP_COLLECTION_BRAND](): true {
return true
}
/**
* Marks sequenced maps across copies of the package. Its key is not exported, so the getter is
* not called directly. Time: O(1).
*/
get [SEQUENCED_MAP_BRAND](): true {
return true
}
/** The number of mappings. Time: O(1). */
get size(): number {
return this.engine.size
}
/**
* A number that changes when a mapping is added, removed or moved or a value is written (even
* the same value) — in access order also when a read moves a mapping — and never on a call that
* changes nothing. Its exact values are unspecified. Time: O(1).
*/
get revision(): number {
return this.engine.revision
}
/** `'LinkedHashMap'`, for `Object.prototype.toString`. Time: O(1). */
get [Symbol.toStringTag](): string {
return 'LinkedHashMap'
}
/**
* Returns whether the map has no mappings.
*
* Time: O(1).
*
* @returns `size === 0`.
*
* @example
* ```ts
* new LinkedHashMap().isEmpty() // true
* ```
*/
isEmpty(): boolean {
return this.engine.isEmpty()
}
/**
* Returns the value mapped to a key equal to `key`, or `undefined` when there is none. In access
* order, a present mapping moves to the end.
*
* Time: O(1) average, plus one `key` or `hash` call and the `equals` calls.
*
* @param key - The key to look up (`-0` is read as `0`).
* @returns The value, or `undefined`.
* @throws TypeError when a `key`, `equals` or `hash` function returns an invalid result.
* @throws ConcurrentModificationError when such a function adds or removes mappings.
*
* @example
* ```ts
* new LinkedHashMap([['a', 1]]).get('a') // 1
* ```
*/
get(key: K): V | undefined {
return this.engine.get(key)
}
/**
* Returns the value mapped to `key` when the key is present (even when that value is
* `undefined`), and `fallback` otherwise. In access order, a present mapping moves to the end.
*
* Time: as `get`; O(1) amortised average in access order (the first call that needs linked records costs O(n), once).
*
* @param key - The key to look up.
* @param fallback - The value to return when the key is absent.
* @returns The mapped value, or `fallback`.
* @throws TypeError when a `key`, `equals` or `hash` function returns an invalid result.
* @throws ConcurrentModificationError when such a function adds or removes mappings.
*
* @example
* ```ts
* new LinkedHashMap([['a', 1]]).getOrDefault('b', 0) // 0
* ```
*/
getOrDefault<D>(key: K, fallback: D): V | D {
return this.engine.getOrDefault(key, fallback)
}
/**
* Returns whether a key equal to `key` is present. Never moves a mapping.
*
* Time: as `get`.
*
* @param key - The key to look for.
* @returns `true` when the key is present.
* @throws TypeError when a `key`, `equals` or `hash` function returns an invalid result.
* @throws ConcurrentModificationError when such a function adds or removes mappings.
*
* @example
* ```ts
* new LinkedHashMap([[Number.NaN, 1]]).has(Number.NaN) // true
* ```
*/
has(key: K): boolean {
return this.engine.has(key)
}
/**
* Returns whether some value is SameValueZero-equal to `value`. Never moves a mapping.
*
* Time: O(n).
*
* @param value - The value to look for.
* @returns `true` when a mapping has that value.
*
* @example
* ```ts
* new LinkedHashMap([['a', 1]]).containsValue(1) // true
* ```
*/
containsValue(value: V): boolean {
return this.engine.containsValue(value)
}
/**
* Maps `key` to `value` and returns the map, so calls chain. A new key goes to the end; a
* present key keeps its stored key object and its position (in access order it moves to the
* end) and only the value is replaced. With `maxSize`, a new key can evict from the front.
*
* Time: O(1) average, plus one `key` or `hash` call and the `equals` calls; O(1) amortised
* average with `maxSize`.
*
* @param key - The key (`-0` is stored as `0`).
* @param value - The value.
* @returns The map.
* @throws TypeError when a `key`, `equals` or `hash` function returns an invalid result.
* @throws ConcurrentModificationError when such a function adds or removes mappings; the map
* keeps those changes and stores nothing itself.
* @throws Error whatever `onEvict` throws, after the mapping is set.
*
* @example
* ```ts
* new LinkedHashMap<string, number>().set('a', 1).set('b', 2) // LinkedHashMap(2) {'a' => 1, 'b' => 2}
* ```
*/
set(key: K, value: V): this {
this.engine.set(key, value)
return this
}
/**
* Maps `key` to `value`, as `set` does, and returns the previous value, or `undefined` when the
* key was absent.
*
* Time: as `set`; O(1) amortised average with `accessOrder`, `maxSize` and `onEvict` (the first call that needs linked records costs O(n), once).
*
* @param key - The key.
* @param value - The value.
* @returns The previous value, or `undefined`.
* @throws TypeError when a `key`, `equals` or `hash` function returns an invalid result.
* @throws ConcurrentModificationError when such a function adds or removes mappings.
* @throws Error whatever `onEvict` throws, after the mapping is set.
*
* @example
* ```ts
* new LinkedHashMap([['a', 1]]).put('a', 2) // 1
* ```
*/
put(key: K, value: V): V | undefined {
return this.engine.put(key, value)
}
/**
* Sets every mapping of `entries` in order, each as one `set` with its access move and eviction.
* The whole argument is read and checked first, so `map.putAll(map)` is safe; evictions are
* reported to `onEvict` once, after the last pair.
*
* Time: O(m) average for `m` items, plus the calls; O(m) amortised average with `maxSize` or in access order (the first call that needs linked records costs O(n), once).
*
* @param entries - `[key, value]` arrays, map entries, a native `Map` or a map.
* @throws TypeError when `entries` is not iterable, an item is not an array of length 2 or a map
* entry, or a `key`, `equals` or `hash` function returns an invalid result.
* @throws ConcurrentModificationError when such a function adds or removes mappings.
* @throws Error whatever `onEvict` throws, after the mappings are set.
*
* @example
* ```ts
* map.putAll(new Map([['a', 1], ['b', 2]]))
* ```
*/
putAll(entries: Iterable<readonly [K, V] | MapEntry<K, V>>): void {
this.engine.putAll(entries)
}
/**
* Maps `key` to `value` at the front: inserts the mapping there when the key is absent, or moves
* the present mapping there (keeping its stored key) and writes its value. Not an access. With
* `maxSize`, a new key can evict from the front, never itself.
*
* Time: O(1) amortised average, plus one `key` or `hash` call and the `equals` calls (the first
* call that needs linked order costs O(n), once).
*
* @param key - The key.
* @param value - The value.
* @returns The map.
* @throws TypeError when a `key`, `equals` or `hash` function returns an invalid result.
* @throws ConcurrentModificationError when such a function adds or removes mappings; nothing is
* inserted or moved.
* @throws Error whatever `onEvict` throws, after the mapping is placed.
*
* @example
* ```ts
* new LinkedHashMap([['a', 1], ['b', 2]]).putFirst('b', 3) // LinkedHashMap(2) {'b' => 3, 'a' => 1}
* ```
*/
putFirst(key: K, value: V): this {
this.engine.putFirst(key, value)
return this
}
/**
* Maps `key` to `value` at the end: inserts the mapping there when the key is absent, or moves
* the present mapping there (keeping its stored key) and writes its value. Not an access.
*
* Time: O(1) amortised average, plus one `key` or `hash` call and the `equals` calls (the first
* call that needs linked order costs O(n), once).
*
* @param key - The key.
* @param value - The value.
* @returns The map.
* @throws TypeError when a `key`, `equals` or `hash` function returns an invalid result.
* @throws ConcurrentModificationError when such a function adds or removes mappings; nothing is
* inserted or moved.
* @throws Error whatever `onEvict` throws, after the mapping is placed.
*
* @example
* ```ts
* new LinkedHashMap([['a', 1], ['b', 2]]).putLast('a', 3) // LinkedHashMap(2) {'b' => 2, 'a' => 3}
* ```
*/
putLast(key: K, value: V): this {
this.engine.putLast(key, value)
return this
}
/**
* Removes the mapping of `key` and returns whether there was one. `onEvict` is not called.
*
* Time: as `set`.
*
* @param key - The key.
* @returns `true` when a mapping was removed.
* @throws TypeError when a `key`, `equals` or `hash` function returns an invalid result.
* @throws ConcurrentModificationError when such a function adds or removes mappings.
*
* @example
* ```ts
* new LinkedHashMap([['a', 1]]).delete('a') // true
* ```
*/
delete(key: K): boolean {
return this.engine.delete(key)
}
/**
* Removes the mapping of `key` and returns its value, or `undefined` when there was none.
*
* Time: as `set`; O(1) amortised average in access order (the first call that needs linked records costs O(n), once).
*
* @param key - The key.
* @returns The removed value, or `undefined`.
* @throws TypeError when a `key`, `equals` or `hash` function returns an invalid result.
* @throws ConcurrentModificationError when such a function adds or removes mappings.
*
* @example
* ```ts
* new LinkedHashMap([['a', 1]]).remove('a') // 1
* ```
*/
remove(key: K): V | undefined {
return this.engine.remove(key)
}
/**
* Replaces the value of `key` only when the key is present (in access order, moving it to the
* end), and returns the previous value, or `undefined`.
*
* Time: as `set`; O(1) amortised average in access order (the first call that needs linked records costs O(n), once).
*
* @param key - The key.
* @param value - The new value.
* @returns The previous value, or `undefined`.
* @throws TypeError when a `key`, `equals` or `hash` function returns an invalid result.
* @throws ConcurrentModificationError when such a function adds or removes mappings.
*
* @example
* ```ts
* new LinkedHashMap([['a', 1]]).replace('b', 2) // undefined; nothing is added
* ```
*/
replace(key: K, value: V): V | undefined {
return this.engine.replace(key, value)
}
/**
* Replaces each value with `fn(value, key, map)`, in order. Never moves a mapping. Values
* replaced before an error stay.
*
* Time: O(n) plus the calls.
*
* @param fn - Returns the new value.
* @throws TypeError when `fn` is not a function.
* @throws ConcurrentModificationError when `fn` adds or removes mappings.
*
* @example
* ```ts
* prices.replaceAll((price) => price * 2)
* ```
*/
replaceAll(fn: (value: V, key: K, map: MapCollection<K, V>) => V): void {
this.engine.replaceAll(fn)
}
/**
* Returns the value of `key` when present (in access order, moving it to the end); otherwise maps
* `key` to `value` at the end and returns `value`, as native `Map.prototype.getOrInsert` does.
*
* Time: as `set`; O(1) amortised average in access order (the first call that needs linked records costs O(n), once).
*
* @param key - The key.
* @param value - The value to insert when the key is absent.
* @returns The mapped value.
* @throws TypeError when a `key`, `equals` or `hash` function returns an invalid result.
* @throws ConcurrentModificationError when such a function adds or removes mappings.
* @throws Error whatever `onEvict` throws, after the mapping is set.
*
* @example
* ```ts
* groups.getOrInsert('a', []).push(1)
* ```
*/
getOrInsert(key: K, value: V): V {
return this.engine.getOrInsert(key, value)
}
/**
* Returns the value of `key` when present (in access order, moving it to the end), without
* calling `fn`; otherwise calls `fn(key)`, maps `key` to its result and returns it. If `fn`
* inserted the key itself, that mapping's value is overwritten in place, as an access.
*
* Time: as `set`, plus the call; O(1) amortised average in access order (the first call that needs linked records costs O(n), once).
*
* @param key - The key.
* @param fn - Computes the value to insert, called with `this` undefined.
* @returns The mapped value.
* @throws TypeError when `fn` is not a function (even when the key is present), or a `key`,
* `equals` or `hash` function returns an invalid result.
* @throws ConcurrentModificationError when a `key`, `equals` or `hash` function adds or removes
* mappings.
* @throws Error whatever `onEvict` throws, after the mapping is set.
* @throws Error whatever `fn` throws; the call inserts and changes nothing, and changes `fn`
* made stay.
*
* @example
* ```ts
* cache.getOrInsertComputed(id, (id) => load(id))
* ```
*/
getOrInsertComputed(key: K, fn: (key: K) => V): V {
return this.engine.getOrInsertComputed(key, fn)
}
/**
* Maps `key` to `fn(currentValueOrUndefined, key, map)`, or removes the mapping when the result
* is `undefined`, and returns the result. Writing a present key is an access.
*
* Time: as `set`, plus the call; O(1) amortised average in access order (the first call that needs linked records costs O(n), once).
*
* @param key - The key.
* @param fn - Computes the new value.
* @returns The result of `fn`.
* @throws TypeError when `fn` is not a function, or a `key`, `equals` or `hash` function returns
* an invalid result.
* @throws ConcurrentModificationError when `fn` adds or removes mappings; its changes stay and
* the call makes none of its own.
* @throws Error whatever `onEvict` throws, after the call's changes.
* @throws Error whatever `fn` throws; the call inserts and changes nothing, and changes `fn`
* made stay.
*
* @example
* ```ts
* counts.compute('a', (n) => (n ?? 0) + 1)
* ```
*/
compute(
key: K,
fn: (value: V | undefined, key: K, map: MapCollection<K, V>) => V | undefined,
): V | undefined {
return this.engine.compute(key, fn)
}
/**
* When `key` is present, maps it to `fn(value, key, map)`, or removes the mapping when the
* result is `undefined`, and returns the result; otherwise returns `undefined` without calling
* `fn`. Writing the value is an access.
*
* Time: as `set`, plus the call; O(1) amortised average in access order (the first call that needs linked records costs O(n), once).
*
* @param key - The key.
* @param fn - Computes the new value.
* @returns The result of `fn`, or `undefined`.
* @throws TypeError when `fn` is not a function, or a `key`, `equals` or `hash` function returns
* an invalid result.
* @throws ConcurrentModificationError when `fn` adds or removes mappings.
* @throws Error whatever `fn` throws; the call inserts and changes nothing, and changes `fn`
* made stay.
*
* @example
* ```ts
* stock.computeIfPresent('apple', (n) => (n > 1 ? n - 1 : undefined))
* ```
*/
computeIfPresent(
key: K,
fn: (value: V, key: K, map: MapCollection<K, V>) => V | undefined,
): V | undefined {
return this.engine.computeIfPresent(key, fn)
}
/**
* Maps `key` to `value` at the end when it is absent; otherwise to `fn(old, value, key, map)`,
* removing the mapping when that result is `undefined`. Returns the stored value, or `undefined`
* when the mapping was removed. Writing a present key is an access.
*
* Time: as `set`, plus the call; O(1) amortised average in access order (the first call that needs linked records costs O(n), once).
*
* @param key - The key.
* @param value - The value to store, or to combine with the present one.
* @param fn - Combines the present value with `value`.
* @returns The stored value, or `undefined`.
* @throws TypeError when `fn` is not a function, or a `key`, `equals` or `hash` function returns
* an invalid result.
* @throws ConcurrentModificationError when `fn` adds or removes mappings.
* @throws Error whatever `onEvict` throws, after the mapping is set.
* @throws Error whatever `fn` throws; the call inserts and changes nothing, and changes `fn`
* made stay.
*
* @example
* ```ts
* for (const word of words) counts.merge(word, 1, (n, one) => n + one)
* ```
*/
merge(
key: K,
value: V,
fn: (old: V, value: V, key: K, map: MapCollection<K, V>) => V | undefined,
): V | undefined {
return this.engine.merge(key, value, fn)
}
/**
* Removes every mapping. `onEvict` is not called. An open iteration starts over before the first
* mapping added afterwards.
*
* Time: O(1) with an `equivalence`; one native `clear` otherwise (at most O(n)).
*
* @example
* ```ts
* map.clear()
* map.size // 0
* ```
*/
clear(): void {
this.engine.clear()
}
/**
* Returns the entry of the first mapping, or `undefined` when the map is empty. Never moves a
* mapping and calls no user function. The entry is live: it reads the map's current value of
* its key.
*
* Time: O(1) amortised.
*
* @returns A live entry, or `undefined`.
*
* @example
* ```ts
* new LinkedHashMap([['a', 1], ['b', 2]]).firstEntry()?.key // 'a'
* ```
*/
firstEntry(): MapEntry<K, V> | undefined {
return this.engine.firstEntry()
}
/**
* Returns the entry of the last mapping, or `undefined` when the map is empty. Never moves a
* mapping and calls no user function.
*
* Time: O(1) amortised (the first call that needs linked order costs O(n), once).
*
* @returns A live entry, or `undefined`.
*
* @example
* ```ts
* new LinkedHashMap([['a', 1], ['b', 2]]).lastEntry()?.value // 2
* ```
*/
lastEntry(): MapEntry<K, V> | undefined {
return this.engine.lastEntry()
}
/**
* Removes the first mapping and returns its entry, or returns `undefined` when the map is empty.
* The entry still reads its value; `setValue` on it throws `IllegalStateError` until the key is
* set again. No user function is called and `onEvict` is not.
*
* Time: O(1) amortised average.
*
* @returns The entry of the removed mapping, or `undefined`.
*
* @example
* ```ts
* const oldest = cache.pollFirstEntry()
* ```
*/
pollFirstEntry(): MapEntry<K, V> | undefined {
return this.engine.pollFirstEntry()
}
/**
* Removes the last mapping and returns its entry, or returns `undefined` when the map is empty.
* No user function is called and `onEvict` is not.
*
* Time: O(1) amortised average (the first call that needs linked order costs O(n), once).
*
* @returns The entry of the removed mapping, or `undefined`.
*
* @example
* ```ts
* new LinkedHashMap([['a', 1], ['b', 2]]).pollLastEntry()?.key // 'b'
* ```
*/
pollLastEntry(): MapEntry<K, V> | undefined {
return this.engine.pollLastEntry()
}
/**
* Returns a live view of this map in the opposite order, tagged `ReversedLinkedHashMap`. Changes
* through either show in both; its ends are this map's ends swapped, and a key it inserts goes to
* its end, this map's front. Access moves and evictions act on this map as usual. Reversing the
* view returns this map.
*
* Time: O(1). Every call through the view needs linked order: the first such call on the map
* costs O(n), once, and the view's operations cost what the map's cost, amortised.
*
* @returns The reversed view.
*
* @example
* ```ts
* const newestFirst = map.reversed()
* for (const [key] of newestFirst) console.log(key)
* ```
*/
reversed(): SequencedMap<K, V> {
return this.engine.reversed()
}
/**
* Returns a live view of the keys in this map's order, under its key equality, with this map's
* ends. Removing a key removes its mapping; `add`, `addFirst` and `addLast` throw
* `UnsupportedOperationError`. Each call returns a new view.
*
* Time: O(1). A call through the view other than `size`, `isEmpty` and `has` (in access order
* without `key` or `equivalence`, other than `size` and `isEmpty`) needs linked order: the first
* such call on the map costs O(n), once, and the view's ends cost what the map's cost, amortised.
*
* @returns A new view, tagged `LinkedHashMapKeySet`.
*
* @example
* ```ts
* map.keySet().getLast() // the key of the last mapping
* ```
*/
keySet(): SequencedSet<K> {
return this.engine.keySet()
}
/**
* Returns a live view of the mappings as entries, in this map's order, with this map's ends. An
* entry is a member when its key is present with an equal value; removing it removes the
* mapping; adding throws `UnsupportedOperationError`. Each call returns a new view.
*
* Time: O(1). A call through the view other than `size`, `isEmpty` and `has` (in access order
* without `key` or `equivalence`, other than `size` and `isEmpty`) needs linked order: the first
* such call on the map costs O(n), once, and the view's ends cost what the map's cost, amortised.
*
* @returns A new view, tagged `LinkedHashMapEntrySet`.
*
* @example
* ```ts
* for (const entry of map.entrySet()) if (entry.value < 0) entry.setValue(0)
* ```
*/
entrySet(): SequencedSet<MapEntry<K, V>> {
return this.engine.entrySet()
}
/**
* Returns a live view of the values in this map's order, compared with SameValueZero, with this
* map's ends. `delete(v)` removes the first mapping with that value; `removeAll`, `retainAll`
* and `removeIf` every matching mapping; adding throws `UnsupportedOperationError`. The view
* equals only itself. Each call returns a new view.
*
* Time: O(1). A call through the view other than `size`, `isEmpty` and `has` (in access order
* without `key` or `equivalence`, other than `size` and `isEmpty`) needs linked order: the first
* such call on the map costs O(n), once, and the view's ends cost what the map's cost, amortised.
*
* @returns A new view, tagged `LinkedHashMapValues`.
*
* @example
* ```ts
* map.valueCollection().removeIf((v) => v === 0)
* ```
*/
valueCollection(): SequencedCollection<V> {
return this.engine.valueCollection()
}
/**
* Returns a live iterator over the keys in order.
*
* Time: O(1) to create (in access order without `key` or `equivalence`, the first call that
* needs linked records costs O(n), once); O(1) per step, amortised over the steps and the
* changes made while it is open.
*
* @returns A new iterator.
*
* @example
* ```ts
* [...new LinkedHashMap([['a', 1]]).keys()] // ['a']
* ```
*/
keys(): IterableIterator<K> {
return this.engine.keys()
}
/**
* Returns a live iterator over the values in order.
*
* Time: O(1) to create (in access order without `key` or `equivalence`, the first call that
* needs linked records costs O(n), once); O(1) per step, amortised over the steps and the
* changes made while it is open.
*
* @returns A new iterator.
*
* @example
* ```ts
* [...new LinkedHashMap([['a', 1]]).values()] // [1]
* ```
*/
values(): IterableIterator<V> {
return this.engine.values()
}
/**
* Returns a live iterator over a new `[key, value]` array per mapping, in order.
*
* Time: O(1) to create (in access order without `key` or `equivalence`, the first call that
* needs linked records costs O(n), once); O(1) per step, amortised over the steps and the
* changes made while it is open.
*
* @returns A new iterator.
*
* @example
* ```ts
* [...new LinkedHashMap([['a', 1]]).entries()] // [['a', 1]]
* ```
*/
entries(): IterableIterator<[K, V]> {
return this.engine.entries()
}
/**
* Calls `action(value, key, map)` for each mapping in order, with `this` set to `thisArg`, as
* native `Map` does. Mappings deleted before they are reached are skipped; mappings added at the
* end during the loop are visited.
*
* Time: O(n) plus the calls.
*
* @param action - Called with each value, its key and the map.
* @param thisArg - The `this` value for `action`.
* @throws TypeError when `action` is not a function.
* @throws Error whatever `onEvict` throws for mappings evicted by `action`, after the loop.
*
* @example
* ```ts
* map.forEach((value, key) => console.log(key, value))
* ```
*/
forEach(action: (value: V, key: K, map: MapCollection<K, V>) => void, thisArg?: unknown): void {
this.engine.forEach(action, thisArg)
}
/**
* Returns a live iterator over a new `[key, value]` array per mapping, in order, as `entries()`
* does. `new Map(linkedHashMap)` copies the map this way.
*
* Time: O(1) to create (in access order without `key` or `equivalence`, the first call that
* needs linked records costs O(n), once); O(1) per step, amortised over the steps and the
* changes made while it is open.
*
* @returns A new iterator.
*
* @example
* ```ts
* for (const [key, value] of map) console.log(key, value)
* ```
*/
[Symbol.iterator](): IterableIterator<[K, V]> {
return this.engine.entries()
}
/**
* Returns a new `LinkedHashMap` with the same mappings, order and options — `key` or
* `equivalence`, `accessOrder`, `maxSize` and the same `onEvict` function — independent of this
* one. Stored keys and hashes are copied; no user function is called.
*
* Time: O(n).
*
* @returns The copy.
*
* @example
* ```ts
* const copy = map.copy()
* copy.set('z', 0) // map is unchanged
* ```
*/
copy(): LinkedHashMap<K, V> {
const map = new LinkedHashMap<K, V>()
map.engine = this.engine.copy(map)
return map
}
/**
* Returns whether `other` is a map of the same size in which every key of this map is present
* with a value equal under `equals`, and which has no key this map lacks — whatever the order,
* and the same both ways round. Never moves a mapping of either map.
*
* Time: O(n) average, plus the lookups on both maps and the protocol methods.
*
* @param other - Any value.
* @returns `true` when `other` is an equal map.
* @throws ConcurrentModificationError when a value's `equals` changes the structure of `other`
* while it is compared (values are compared as `equals(otherValue, thisValue)`).
*
* @example
* ```ts
* new LinkedHashMap([['a', 1], ['b', 2]]).equals(new HashMap([['b', 2], ['a', 1]])) // true
* ```
*/
equals(other: unknown): boolean {
return this.engine.equals(other)
}
/** The value-equality protocol; delegates to `equals`. Time: as `equals`. */
[equalsSymbol](other: unknown): boolean {
return this.engine.equals(other)
}
/**
* The value-equality protocol: an order-independent combination of the mappings' hashes, the
* same as an equal `HashMap` with the same `key` function, the same `equivalence`, or neither.
* Values are unspecified. Time: O(n) plus the `hash` calls.
*/
[hashCodeSymbol](hash: HashFunction): number {
return this.engine[hashCodeSymbol](hash)
}
/**
* Returns the class name, size and mappings in order, e.g.
* `LinkedHashMap(2) {'a' => 1, 'b' => 2}`; a map that contains itself prints as `[Circular]`.
*
* Time: O(length of the output) for flat contents.
*
* @returns The text.
*
* @example
* ```ts
* String(new LinkedHashMap([['a', 1]])) // "LinkedHashMap(1) {'a' => 1}"
* ```
*/
toString(): string {
return this.engine.toString()
}
/**
* Returns a new array of `[key, value]` arrays in order, so `JSON.stringify` writes the map as
* pairs.
*
* Time: O(n).
*
* @returns A new array.
*
* @example
* ```ts
* JSON.stringify(new LinkedHashMap([['a', 1]])) // '[["a",1]]'
* ```
*/
toJSON(): Array<[K, V]> {
return this.engine.toJSON()
}
/** Node's inspect hook: `LinkedHashMap(1) { 'a' => 1 }` in `console.log`. Time: as `toString`. */
[INSPECT](
depth: number | null,
options: object,
inspect: (value: unknown, options: object) => string,
): string {
return this.engine[INSPECT](depth, options, inspect)
}
}
|