@mark1russell7/collections 0.9.1 → 0.9.3
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.
- package/README.md +16 -0
- package/build/functions.d.ts +161 -0
- package/build/functions.d.ts.map +1 -0
- package/build/functions.js +414 -0
- package/build/functions.js.map +1 -0
- package/build/index.d.ts +19 -64
- package/build/index.d.ts.map +1 -1
- package/build/index.js +103 -357
- package/build/index.js.map +1 -1
- package/build/summary-tree/index.d.ts +2 -0
- package/build/summary-tree/index.js +1 -0
- package/package.json +5 -1
- package/src/behaviors/readonly.ts +6 -0
- package/src/impl/hash-set.ts +30 -22
- package/src/impl/summary-tree.ts +56 -425
- package/src/impl/tree-set.ts +18 -18
- package/src/index.ts +10 -1
- package/src/summary-tree/functions.ts +581 -0
- package/src/summary-tree/index.ts +6 -0
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { C as split, D as toArray, E as summaryOf, S as splice, T as summary, _ as remove, a as SummarySpec, b as size, c as at, d as insert, f as isEmpty, g as prepend, h as of, i as SummarySearch, l as concat, m as locate, n as SummaryMonoid, o as SummaryTreeRecord, p as leaves, r as SummaryNode, s as append, t as SummaryLocation, u as height, v as search, w as splitAt, x as slice, y as set } from "../functions.js";
|
|
2
|
+
export { SummaryLocation, SummaryMonoid, SummaryNode, SummarySearch, SummarySpec, SummaryTreeRecord, append, at, concat, height, insert, isEmpty, leaves, locate, of, prepend, remove, search, set, size, slice, splice, split, splitAt, summary, summaryOf, toArray };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { t as append, n as at, r as concat, i as height, a as insert, o as isEmpty, s as leaves, c as locate, l as of, u as prepend, d as remove, f as search, p as set, m as size, h as slice, g as splice, _ as split, v as splitAt, y as summary, b as summaryOf, x as toArray } from "../functions.js";
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/package",
|
|
3
3
|
"name": "@mark1russell7/collections",
|
|
4
|
-
"version": "0.9.
|
|
4
|
+
"version": "0.9.3",
|
|
5
5
|
"description": "Collections with no dependencies: lists, maps, queues, a summary tree, channels, lazy operations and dependency graphs.",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"collections",
|
|
@@ -47,6 +47,10 @@
|
|
|
47
47
|
"types": "./build/core/layers.d.ts",
|
|
48
48
|
"default": "./build/core/layers.js"
|
|
49
49
|
},
|
|
50
|
+
"./summary-tree": {
|
|
51
|
+
"types": "./build/summary-tree/index.d.ts",
|
|
52
|
+
"default": "./build/summary-tree/index.js"
|
|
53
|
+
},
|
|
50
54
|
"./testing": {
|
|
51
55
|
"types": "./build/testing/index.d.ts",
|
|
52
56
|
"default": "./build/testing/index.js"
|
|
@@ -88,6 +88,9 @@ const MUTATING_METHODS = new Set([
|
|
|
88
88
|
// Queue changes that give undefined on an empty queue (C27: a readonly view removed an element)
|
|
89
89
|
"pollOrUndefined",
|
|
90
90
|
|
|
91
|
+
// The removal by a test of LRUCache (C31: a readonly view of an LRU cache emptied it)
|
|
92
|
+
"deleteWhere",
|
|
93
|
+
|
|
91
94
|
// The internal changes of the structures of this package. TypeScript marks them private, but a call at run
|
|
92
95
|
// time reaches them. `readonly.test.ts` puts each method of each structure in the reads or in this list.
|
|
93
96
|
"ensureCapacity",
|
|
@@ -108,6 +111,9 @@ const MUTATING_METHODS = new Set([
|
|
|
108
111
|
"fixAfterDeletion",
|
|
109
112
|
"rotateLeft",
|
|
110
113
|
"rotateRight",
|
|
114
|
+
"moveToHead",
|
|
115
|
+
"removeNode",
|
|
116
|
+
"evictLRU",
|
|
111
117
|
]);
|
|
112
118
|
|
|
113
119
|
/** Methods that return a view of the collection: the view is readonly too. */
|
package/src/impl/hash-set.ts
CHANGED
|
@@ -63,6 +63,7 @@ export interface HashSetOptions<T> {
|
|
|
63
63
|
export class HashSet<T> implements Set<T> {
|
|
64
64
|
private readonly _map: HashMap<T, symbol>;
|
|
65
65
|
private readonly _eq: Eq<T>;
|
|
66
|
+
private readonly _hash: Hash<T>;
|
|
66
67
|
|
|
67
68
|
constructor(options: HashSetOptions<T> = {}) {
|
|
68
69
|
const {
|
|
@@ -73,6 +74,7 @@ export class HashSet<T> implements Set<T> {
|
|
|
73
74
|
} = options;
|
|
74
75
|
|
|
75
76
|
this._eq = eq;
|
|
77
|
+
this._hash = hash;
|
|
76
78
|
this._map = new HashMap<T, symbol>({
|
|
77
79
|
initialCapacity,
|
|
78
80
|
loadFactor,
|
|
@@ -140,18 +142,11 @@ export class HashSet<T> implements Set<T> {
|
|
|
140
142
|
}
|
|
141
143
|
|
|
142
144
|
retainAll(other: Iterable<T>): boolean {
|
|
143
|
-
const toRetain =
|
|
145
|
+
const toRetain = this.setOf(other);
|
|
144
146
|
const toRemove: T[] = [];
|
|
145
147
|
|
|
146
148
|
for (const element of this) {
|
|
147
|
-
|
|
148
|
-
for (const retainElement of toRetain) {
|
|
149
|
-
if (this._eq(element, retainElement)) {
|
|
150
|
-
shouldRetain = true;
|
|
151
|
-
break;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
if (!shouldRetain) {
|
|
149
|
+
if (!toRetain.contains(element)) {
|
|
155
150
|
toRemove.push(element);
|
|
156
151
|
}
|
|
157
152
|
}
|
|
@@ -197,13 +192,12 @@ export class HashSet<T> implements Set<T> {
|
|
|
197
192
|
* The new set contains only elements present in both sets.
|
|
198
193
|
*/
|
|
199
194
|
intersection(other: Iterable<T>): HashSet<T> {
|
|
200
|
-
|
|
195
|
+
// The loop reads `other` one time, so a generator gives all of its elements
|
|
196
|
+
const others = this.setOf(other);
|
|
197
|
+
const result = this.empty();
|
|
201
198
|
for (const element of this) {
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
result.add(element);
|
|
205
|
-
break;
|
|
206
|
-
}
|
|
199
|
+
if (others.contains(element)) {
|
|
200
|
+
result.add(element);
|
|
207
201
|
}
|
|
208
202
|
}
|
|
209
203
|
return result;
|
|
@@ -224,8 +218,10 @@ export class HashSet<T> implements Set<T> {
|
|
|
224
218
|
* The new set contains elements in either set but not in both.
|
|
225
219
|
*/
|
|
226
220
|
symmetricDifference(other: Iterable<T>): HashSet<T> {
|
|
227
|
-
|
|
228
|
-
const
|
|
221
|
+
// The union and the intersection each read `other`, so read it one time first
|
|
222
|
+
const others = this.setOf(other);
|
|
223
|
+
const result = this.union(others);
|
|
224
|
+
const inter = this.intersection(others);
|
|
229
225
|
result.removeAll(inter);
|
|
230
226
|
return result;
|
|
231
227
|
}
|
|
@@ -235,8 +231,7 @@ export class HashSet<T> implements Set<T> {
|
|
|
235
231
|
* All elements in this set are also in the other set.
|
|
236
232
|
*/
|
|
237
233
|
isSubsetOf(other: Iterable<T>): boolean {
|
|
238
|
-
const otherSet =
|
|
239
|
-
otherSet.addAll(other);
|
|
234
|
+
const otherSet = this.setOf(other);
|
|
240
235
|
|
|
241
236
|
for (const element of this) {
|
|
242
237
|
if (!otherSet.contains(element)) {
|
|
@@ -300,9 +295,22 @@ export class HashSet<T> implements Set<T> {
|
|
|
300
295
|
* This method makes a shallow copy of this set.
|
|
301
296
|
*/
|
|
302
297
|
clone(): HashSet<T> {
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
298
|
+
return this.setOf(this);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* This method makes an empty set with the equality and the hash function of this set.
|
|
303
|
+
* Before, the inner sets kept only the equality, so they did not find an element with a custom hash.
|
|
304
|
+
*/
|
|
305
|
+
private empty(): HashSet<T> {
|
|
306
|
+
return new HashSet<T>({ eq: this._eq, hash: this._hash });
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** This method makes a set of the elements, with the equality and the hash function of this set. */
|
|
310
|
+
private setOf(elements: Iterable<T>): HashSet<T> {
|
|
311
|
+
const set = this.empty();
|
|
312
|
+
set.addAll(elements);
|
|
313
|
+
return set;
|
|
306
314
|
}
|
|
307
315
|
|
|
308
316
|
/**
|