/
ll-set.ts
174 lines (149 loc) · 4.21 KB
/
ll-set.ts
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
import { Fn3, Pair, Predicate2, SEMAPHORE } from "@thi.ng/api";
import { DCons } from "@thi.ng/dcons";
import { equiv } from "@thi.ng/equiv";
import type { EquivSetOpts, IEquivSet } from "./api";
import { dissoc } from "./dissoc";
import { equivSet } from "./internal/equiv";
import { inspectable } from "./internal/inspect";
import { into } from "./into";
interface SetProps<T> {
vals: DCons<T>;
equiv: Predicate2<T>;
}
const __private = new WeakMap<LLSet<any>, SetProps<any>>();
const __vals = (inst: LLSet<any>) => __private.get(inst)!.vals;
/**
* Similar to {@link ArraySet}, this class is an alternative
* implementation of the native ES6 Set API using a
* {@link @thi.ng/dcons#DCons} linked list as backing store and a
* customizable value equality / equivalence predicate. By the default
* uses {@link @thi.ng/equiv#equiv} for equivalence checking.
*
* Additionally, the type also implements the {@link @thi.ng/api#ICopy},
* {@link @thi.ng/api#IEmpty} and {@link @thi.ng/api#IEquiv} interfaces
* itself.
*/
@inspectable
export class LLSet<T> extends Set<T> implements IEquivSet<T> {
constructor(
vals?: Iterable<T> | null,
opts: Partial<EquivSetOpts<T>> = {}
) {
super();
__private.set(this, {
equiv: opts.equiv || equiv,
vals: new DCons<T>(),
});
vals && this.into(vals);
}
*[Symbol.iterator](): IterableIterator<T> {
yield* __vals(this);
}
get [Symbol.species]() {
return LLSet;
}
get [Symbol.toStringTag]() {
return "LLSet";
}
get size(): number {
return __vals(this).length;
}
copy() {
const $this = __private.get(this)!;
const s = new LLSet<T>(null, this.opts());
__private.get(s)!.vals = $this.vals.copy();
return s;
}
empty() {
return new LLSet<T>(null, this.opts());
}
clear() {
__vals(this).clear();
}
first(): T | undefined {
if (this.size) {
return __vals(this).head!.value;
}
}
add(key: T) {
!this.has(key) && __vals(this).push(key);
return this;
}
into(keys: Iterable<T>) {
return <this>into(this, keys);
}
has(key: T) {
return this.get(key, <any>SEMAPHORE) !== <any>SEMAPHORE;
}
/**
* Returns the canonical (stored) value for `key`, if present. If
* the set contains no equivalent for `key`, returns `notFound`.
*
* @param key - search key
* @param notFound - default value
*/
get(key: T, notFound?: T): T | undefined {
const $this = __private.get(this)!;
const eq = $this.equiv;
let i = $this.vals.head;
while (i) {
if (eq(i.value, key)) {
return i.value;
}
i = i.next;
}
return notFound;
}
delete(key: T) {
const $this = __private.get(this)!;
const eq = $this.equiv;
let i = $this.vals.head;
while (i) {
if (eq(i.value, key)) {
$this.vals.splice(i, 1);
return true;
}
i = i.next;
}
return false;
}
disj(keys: Iterable<T>) {
return <this>dissoc(this, keys);
}
equiv(o: any) {
return equivSet(this, o);
}
/**
* The value args given to the callback `fn` MUST be treated as
* readonly/immutable. This could be enforced via TS, but would
* break ES6 Set interface contract.
*
* @param fn
* @param thisArg
*/
forEach(fn: Fn3<T, T, Set<T>, void>, thisArg?: any) {
let i = __vals(this).head;
while (i) {
fn.call(thisArg, i.value, i.value, this);
i = i.next;
}
}
*entries(): IterableIterator<Pair<T, T>> {
for (let v of __vals(this)) {
yield [v, v];
}
}
*keys(): IterableIterator<T> {
yield* __vals(this);
}
*values(): IterableIterator<T> {
yield* __vals(this);
}
opts(): EquivSetOpts<T> {
return { equiv: __private.get(this)!.equiv };
}
}
export const defLLSet = <T>(
vals?: Iterable<T> | null,
opts?: Partial<EquivSetOpts<T>>
) => new LLSet(vals, opts);