@rhombus-toolkit/collections 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,8 @@
1
1
  /**
2
- * Extending answers a new list sharing everything already there, so whoever holds one reads
3
- * exactly what it held however much is added or removed afterwards.
2
+ * A persistent singly-linked list — extending shares everything already there.
4
3
  *
5
4
  * @remarks
6
- * Both ends are known. {@link push} adds at the head and shares the whole list after it, which
7
- * leaves the tail the same link it already was — so a list knows what it ends with without ever
8
- * walking to find out. {@link tailToHead} is settled once per list and answered from then on, which
9
- * is what lets a list be read from its tail as often as wanted without paying to reverse it.
5
+ * {@link tail} is O(1); {@link tailToHead} reverses once and caches the result.
10
6
  */
11
7
  declare class ImmutableLinkedList<T> implements Iterable<T> {
12
8
  #private;
@@ -32,7 +28,7 @@ declare class ImmutableLinkedList<T> implements Iterable<T> {
32
28
  tailToHead(): readonly T[];
33
29
  }
34
30
 
35
- declare class KindaWeakMap<K, V extends object> {
31
+ declare class KindaWeakMap<K, V extends WeakKey> {
36
32
  #private;
37
33
  set(key: K, value: V): this;
38
34
  delete(key: K): boolean;
@@ -40,10 +36,11 @@ declare class KindaWeakMap<K, V extends object> {
40
36
  has(key: K): boolean;
41
37
  clear(): void;
42
38
  forEach(callbackfn: (value: V, key: K, map: this) => void, thisArg?: any): void;
39
+ /** Counts only entries whose value is still alive, so it walks every ref. */
43
40
  get size(): number;
44
41
  entries(): MapIterator<[K, V]>;
45
42
  keys(): MapIterator<K>;
46
- values(): MapIterator<V | undefined>;
43
+ values(): MapIterator<V>;
47
44
  [Symbol.iterator](): MapIterator<[K, V]>;
48
45
  readonly [Symbol.toStringTag]: "KindaWeakMap";
49
46
  }
@@ -66,28 +66,28 @@ class KindaWeakMap {
66
66
  }
67
67
  });
68
68
  set(key, value) {
69
- if (this.has(key)) {
70
- this.delete(key);
71
- }
69
+ this.delete(key);
72
70
  this.#registry.register(value, key, value);
73
71
  this.#map.set(key, new WeakRef(value));
74
72
  return this;
75
73
  }
76
74
  delete(key) {
77
- const val = this.get(key);
78
- if (val !== undefined) {
79
- this.#registry.unregister(val);
75
+ const value = this.get(key);
76
+ this.#map.delete(key);
77
+ if (value === undefined) {
78
+ return false;
80
79
  }
81
- return this.#map.delete(key);
80
+ this.#registry.unregister(value);
81
+ return true;
82
82
  }
83
83
  get(key) {
84
84
  return this.#map.get(key)?.deref();
85
85
  }
86
86
  has(key) {
87
- return this.#map.has(key);
87
+ return this.get(key) !== undefined;
88
88
  }
89
89
  clear() {
90
- this.values().filter(Boolean).forEach((p) => this.#registry.unregister(p));
90
+ this.values().forEach((value) => this.#registry.unregister(value));
91
91
  this.#map.clear();
92
92
  }
93
93
  forEach(callbackfn, thisArg) {
@@ -96,16 +96,17 @@ class KindaWeakMap {
96
96
  }
97
97
  }
98
98
  get size() {
99
- return this.#map.size;
99
+ return this.entries().reduce((count) => count + 1, 0);
100
100
  }
101
101
  entries() {
102
- return this.#map.entries().map(([key, ref]) => [key, ref.deref()]);
102
+ const held = this.#map.entries().map(([key, ref]) => [key, ref.deref()]);
103
+ return held.filter((entry) => entry[1] !== undefined);
103
104
  }
104
105
  keys() {
105
- return this.#map.keys();
106
+ return this.entries().map(([key]) => key);
106
107
  }
107
108
  values() {
108
- return this.#map.values().map((ref) => ref.deref());
109
+ return this.entries().map(([, value]) => value);
109
110
  }
110
111
  [Symbol.iterator]() {
111
112
  return this.entries();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rhombus-toolkit/collections",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Collection data structures: ImmutableLinkedList (persistent, both ends known), KindaWeakMap (strong keys, weak values).",
5
5
  "repository": {
6
6
  "type": "git",