-
Notifications
You must be signed in to change notification settings - Fork 3
/
shift.cjs
294 lines (288 loc) · 8.41 KB
/
shift.cjs
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
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __publicField = (obj, key, value) => {
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
return value;
};
var __accessCheck = (obj, member, msg) => {
if (!member.has(obj))
throw TypeError("Cannot " + msg);
};
var __privateAdd = (obj, member, value) => {
if (member.has(obj))
throw TypeError("Cannot add the same private member more than once");
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
};
var __privateMethod = (obj, member, method) => {
__accessCheck(obj, member, "access private method");
return method;
};
// src/shift.js
var shift_exports = {};
__export(shift_exports, {
PersistentVector: () => PersistentVector,
compose: () => compose,
curry: () => curry,
partial: () => partial,
partialObject: () => partialObject,
partialObjectFirst: () => partialObjectFirst,
partialObjectLast: () => partialObjectLast,
partialOne: () => partialOne,
partialReverse: () => partialReverse,
partialReverseOne: () => partialReverseOne,
pipe: () => pipe
});
module.exports = __toCommonJS(shift_exports);
// src/basic/partial.js
var partial = (fn, ...args) => fn.bind(null, ...args);
var partialOne = (fn, arg) => fn.bind(null, arg);
var partialReverse = (fn, ...args) => (...left) => fn(...left, ...args.reverse());
var partialReverseOne = (fn, arg) => (...rest) => fn(...rest, arg);
var partialObject = (fn, props) => (delta) => fn(Object.assign(props, delta));
var partialObjectLast = (fn, props) => (...args) => {
const last = args[args.length - 1];
Object.assign(last, props);
return fn(...args);
};
var partialObjectFirst = (fn, props) => (...args) => {
const first = args[0];
Object.assign(first, props);
return fn(...args);
};
// src/basic/curry.js
var curry = (fn) => (...args) => {
if (fn.length > args.length) {
const f = fn.bind(null, ...args);
return curry(f);
} else {
return fn(...args);
}
};
// src/basic/compose.js
var compose = (...fns) => (args) => {
if (fns.length === 0)
return args;
const fn = fns.pop();
const res = fn(args);
return compose(...fns)(res);
};
// src/basic/pipe.js
var pipe = (...fns) => (args) => {
if (fns.length === 0)
return args;
const fn = fns.shift();
const res = fn(args);
return pipe(...fns)(res);
};
// src/persistent/vector.js
var emptyNode = () => [];
var copyNode = (node) => [...node];
var doAssoc = (level, node, i, val) => {
const ret = [...node];
if (level === 0) {
ret[i & 31] = val;
} else {
const subIndex = i >>> level & 31;
ret[subIndex] = doAssoc(
level - 5,
node[subIndex],
i,
val
);
}
return ret;
};
var newPath = (level, node) => {
if (level === 0) {
return node;
}
const ret = emptyNode();
ret[0] = newPath(level - 5, node);
return ret;
};
var pushTail = (level, parent, tailNode, count) => {
const subIndex = count - 1 >>> level & 31;
const ret = copyNode(parent);
let nodeToInsert;
if (level === 5) {
nodeToInsert = tailNode;
} else {
const child = parent[subIndex];
nodeToInsert = child ? pushTail(level - 5, child, tailNode, count) : newPath(level - 5, tailNode);
}
ret[subIndex] = nodeToInsert;
return ret;
};
var popTail = (level, node, count) => {
const subidx = count - 2 >>> level & 31;
if (level > 5) {
const newchild = popTail(level - 5, node[subidx], count);
if (newchild.length === 0 && subidx === 0) {
return newchild;
} else {
const ret = copyNode(node);
ret[subidx] = newchild;
return ret;
}
} else if (subidx === 0) {
return emptyNode();
} else if (subidx === node.length - 1) {
return node.slice(0, -1);
} else {
const ret = copyNode(node);
ret.splice(subidx, 1);
return ret;
}
};
var _tailOffset, tailOffset_fn;
var _PersistentVector = class {
/**
* @param {number} count
* @param {number} shift
* @param {NodeArray32} root
* @param {NodeArray32} tail
* */
constructor(count, shift, root, tail) {
/** @type {() => number} */
__privateAdd(this, _tailOffset);
this.count = count;
this.shift = shift;
this.root = root;
this.tail = tail;
}
/** @type {(i: number) => NodeArray32} */
arrayFor(i) {
if (i >= 0 && i < this.count) {
if (i >= __privateMethod(this, _tailOffset, tailOffset_fn).call(this))
return this.tail;
let node = this.root;
for (let level = this.shift; level > 0; level -= 5) {
const index = i >>> level & 31;
const el = node[index];
node = el;
}
return node;
}
throw new Error("Index out of bounds");
}
/**
* get
* @type {(i: number) => any}
* */
nth(i) {
if (i >= 0 && i < this.count) {
const node = this.arrayFor(i);
return node[i & 31];
}
return void 0;
}
/**
* push
* @type {(val: number) => PersistentVector}
* */
cons(val) {
if (this.count - __privateMethod(this, _tailOffset, tailOffset_fn).call(this) < 32) {
const newTail = copyNode(this.tail);
newTail[this.tail.length] = val;
return new _PersistentVector(this.count + 1, this.shift, this.root, newTail);
}
let newRoot;
const tailNode = copyNode(this.tail);
let newShift = this.shift;
if (this.count >>> 5 > 1 << this.shift) {
newRoot = copyNode(this.root);
newRoot[0] = this.root;
newRoot[1] = newPath(this.shift, tailNode);
newShift += 5;
} else {
newRoot = pushTail(this.shift, this.root, tailNode, this.count);
}
return new _PersistentVector(this.count + 1, newShift, newRoot, [val]);
}
/**
* update
* @type {(i: number, val: number) => PersistentVector}
* */
assocN(i, val) {
if (i >= 0 && i < this.count) {
if (i >= __privateMethod(this, _tailOffset, tailOffset_fn).call(this)) {
const newTail = copyNode(this.tail);
newTail[i & 31] = val;
return new _PersistentVector(this.count, this.shift, this.root, newTail);
} else {
const newRoot = doAssoc(this.shift, this.root, i, val);
return new _PersistentVector(this.count, this.shift, newRoot, this.tail);
}
}
if (i === this.count) {
return this.cons(val);
}
throw new Error("Index out of bounds");
}
/**
* delete last
* @type {() => PersistentVector}
* */
pop() {
if (this.count === 0) {
throw new Error("Can't pop empty vector");
}
if (this.count === 1) {
return _PersistentVector.EMPTY;
}
if (this.count - __privateMethod(this, _tailOffset, tailOffset_fn).call(this) > 1) {
const newTail = this.tail.slice(0, -1);
return new _PersistentVector(this.count - 1, this.shift, this.root, newTail);
}
const newtail = this.arrayFor(this.count - 2);
let newroot = popTail(this.shift, this.root, this.count);
let newshift = this.shift;
if (this.shift > 5 && newroot[1] === null) {
newroot = newroot[0];
newshift -= 5;
}
return new _PersistentVector(this.count - 1, newshift, newroot, newtail);
}
};
var PersistentVector = _PersistentVector;
_tailOffset = new WeakSet();
tailOffset_fn = function() {
if (this.count < 32) {
return 0;
} else {
return this.count - 1 >>> 5 << 5;
}
};
/** @type {PersistentVector} */
__publicField(PersistentVector, "EMPTY", new _PersistentVector(0, 5, emptyNode(), emptyNode()));
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
PersistentVector,
compose,
curry,
partial,
partialObject,
partialObjectFirst,
partialObjectLast,
partialOne,
partialReverse,
partialReverseOne,
pipe
});