@mark1russell7/collections 0.9.2 → 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 +6 -64
- package/build/index.d.ts.map +1 -1
- package/build/index.js +53 -326
- 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/impl/summary-tree.ts +56 -425
- package/src/index.ts +10 -1
- package/src/summary-tree/functions.ts +581 -0
- package/src/summary-tree/index.ts +6 -0
package/build/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { compose as compose$1 } from "./core/layers.js";
|
|
2
|
+
import { _ as split, a as insert, b as summaryOf, c as locate, d as remove, f as search, g as splice, h as slice, i as height, l as of, m as size, n as at, o as isEmpty, p as set, r as concat$1, s as leaves, t as append, u as prepend, v as splitAt, x as toArray$2, y as summary } from "./functions.js";
|
|
2
3
|
import { none as none$1, some as some$1, toOptional, tryCatch } from "@mark1russell7/optional";
|
|
3
4
|
// src/core/policies.ts
|
|
4
5
|
/**
|
|
@@ -4075,283 +4076,81 @@ function priorityQueue(optionsOrElementsOrComparator) {
|
|
|
4075
4076
|
return new PriorityQueue(optionsOrElementsOrComparator);
|
|
4076
4077
|
}
|
|
4077
4078
|
// src/impl/summary-tree.ts
|
|
4078
|
-
|
|
4079
|
-
|
|
4080
|
-
|
|
4081
|
-
|
|
4082
|
-
|
|
4083
|
-
|
|
4084
|
-
|
|
4085
|
-
|
|
4086
|
-
|
|
4087
|
-
|
|
4088
|
-
|
|
4089
|
-
|
|
4090
|
-
|
|
4091
|
-
|
|
4092
|
-
|
|
4093
|
-
|
|
4094
|
-
|
|
4095
|
-
if (inner === void 0) inner = left === null ? own : this.spec.monoid.combine(left.summary, own);
|
|
4096
|
-
return {
|
|
4097
|
-
left,
|
|
4098
|
-
leaf,
|
|
4099
|
-
own,
|
|
4100
|
-
inner,
|
|
4101
|
-
right,
|
|
4102
|
-
height: 1 + Math.max(heightOf(left), heightOf(right)),
|
|
4103
|
-
size: sizeOf(left) + 1 + sizeOf(right),
|
|
4104
|
-
summary: right === null ? inner : this.spec.monoid.combine(inner, right.summary)
|
|
4105
|
-
};
|
|
4106
|
-
}
|
|
4107
|
-
/** This function copies a node with a new right subtree. It keeps the summary `inner`, so it calls `combine` once at most. */
|
|
4108
|
-
withRight(n, right) {
|
|
4109
|
-
return this.node(n.left, n.leaf, n.own, right, n.inner);
|
|
4110
|
-
}
|
|
4111
|
-
rotateLeft(n) {
|
|
4112
|
-
const r = n.right;
|
|
4113
|
-
return this.node(this.withRight(n, r.left), r.leaf, r.own, r.right);
|
|
4114
|
-
}
|
|
4115
|
-
rotateRight(n) {
|
|
4116
|
-
const l = n.left;
|
|
4117
|
-
return this.withRight(l, this.node(l.right, n.leaf, n.own, n.right));
|
|
4118
|
-
}
|
|
4119
|
-
/** This function joins a tree that is higher by more than 1 with a leaf and a lower tree on its right. */
|
|
4120
|
-
joinRight(left, leaf, own, right) {
|
|
4121
|
-
const c = left.right;
|
|
4122
|
-
if (heightOf(c) <= heightOf(right) + 1) {
|
|
4123
|
-
const t = this.node(c, leaf, own, right);
|
|
4124
|
-
if (t.height <= heightOf(left.left) + 1) return this.withRight(left, t);
|
|
4125
|
-
return this.rotateLeft(this.withRight(left, this.rotateRight(t)));
|
|
4126
|
-
}
|
|
4127
|
-
const t = this.joinRight(c, leaf, own, right);
|
|
4128
|
-
const joined = this.withRight(left, t);
|
|
4129
|
-
return t.height <= heightOf(left.left) + 1 ? joined : this.rotateLeft(joined);
|
|
4130
|
-
}
|
|
4131
|
-
/** This function joins a lower tree on the left with a leaf and a tree that is higher by more than 1. */
|
|
4132
|
-
joinLeft(left, leaf, own, right, inner) {
|
|
4133
|
-
const c = right.left;
|
|
4134
|
-
if (heightOf(c) <= heightOf(left) + 1) {
|
|
4135
|
-
const t = this.node(left, leaf, own, c, inner);
|
|
4136
|
-
if (t.height <= heightOf(right.right) + 1) return this.node(t, right.leaf, right.own, right.right);
|
|
4137
|
-
return this.rotateRight(this.node(this.rotateLeft(t), right.leaf, right.own, right.right));
|
|
4138
|
-
}
|
|
4139
|
-
const t = this.joinLeft(left, leaf, own, c, inner);
|
|
4140
|
-
const joined = this.node(t, right.leaf, right.own, right.right);
|
|
4141
|
-
return t.height <= heightOf(right.right) + 1 ? joined : this.rotateRight(joined);
|
|
4142
|
-
}
|
|
4143
|
-
/**
|
|
4144
|
-
* This function gives the tree of the leaves of `left`, then `leaf`, then the leaves of `right`. Give `inner` only
|
|
4145
|
-
* when it is the summary of `left` and `own`.
|
|
4146
|
-
*/
|
|
4147
|
-
join(left, leaf, own, right, inner) {
|
|
4148
|
-
if (heightOf(left) > heightOf(right) + 1) return this.joinRight(left, leaf, own, right);
|
|
4149
|
-
if (heightOf(right) > heightOf(left) + 1) return this.joinLeft(left, leaf, own, right, inner);
|
|
4150
|
-
return this.node(left, leaf, own, right, inner);
|
|
4151
|
-
}
|
|
4152
|
-
/** This function removes the last leaf of a tree, and gives the rest and the node of that leaf. */
|
|
4153
|
-
splitLast(tree) {
|
|
4154
|
-
if (tree.right === null) return [tree.left, tree];
|
|
4155
|
-
const [rest, last] = this.splitLast(tree.right);
|
|
4156
|
-
return [this.join(tree.left, tree.leaf, tree.own, rest, tree.inner), last];
|
|
4157
|
-
}
|
|
4158
|
-
concat(left, right) {
|
|
4159
|
-
if (left === null) return right;
|
|
4160
|
-
if (right === null) return left;
|
|
4161
|
-
const [rest, last] = this.splitLast(left);
|
|
4162
|
-
return this.join(rest, last.leaf, last.own, right);
|
|
4163
|
-
}
|
|
4164
|
-
/** This function splits a tree into its first `index` leaves and the other leaves. */
|
|
4165
|
-
splitAt(tree, index) {
|
|
4166
|
-
if (tree === null) return [null, null];
|
|
4167
|
-
const before = sizeOf(tree.left);
|
|
4168
|
-
if (index <= before) {
|
|
4169
|
-
const [a, b] = this.splitAt(tree.left, index);
|
|
4170
|
-
return [a, this.join(b, tree.leaf, tree.own, tree.right)];
|
|
4171
|
-
}
|
|
4172
|
-
const [a, b] = this.splitAt(tree.right, index - before - 1);
|
|
4173
|
-
return [this.join(tree.left, tree.leaf, tree.own, a, tree.inner), b];
|
|
4174
|
-
}
|
|
4175
|
-
/** This function splits a tree before the first leaf whose prefix summary, after `acc`, satisfies the predicate. */
|
|
4176
|
-
splitBy(tree, predicate, acc) {
|
|
4177
|
-
if (tree === null) return [null, null];
|
|
4178
|
-
const { combine } = this.spec.monoid;
|
|
4179
|
-
const throughLeft = tree.left === null ? acc : combine(acc, tree.left.summary);
|
|
4180
|
-
if (tree.left !== null && predicate(throughLeft)) {
|
|
4181
|
-
const [a, b] = this.splitBy(tree.left, predicate, acc);
|
|
4182
|
-
return [a, this.join(b, tree.leaf, tree.own, tree.right)];
|
|
4183
|
-
}
|
|
4184
|
-
const throughLeaf = combine(throughLeft, tree.own);
|
|
4185
|
-
if (predicate(throughLeaf)) return [tree.left, this.join(null, tree.leaf, tree.own, tree.right)];
|
|
4186
|
-
const [a, b] = this.splitBy(tree.right, predicate, throughLeaf);
|
|
4187
|
-
return [this.join(tree.left, tree.leaf, tree.own, a, tree.inner), b];
|
|
4188
|
-
}
|
|
4189
|
-
/** This function replaces the leaf at an index in the tree. It copies only the path to the leaf. */
|
|
4190
|
-
replaceAt(tree, index, leaf, own) {
|
|
4191
|
-
const before = sizeOf(tree.left);
|
|
4192
|
-
if (index === before) return this.node(tree.left, leaf, own, tree.right);
|
|
4193
|
-
if (index < before) return this.node(this.replaceAt(tree.left, index, leaf, own), tree.leaf, tree.own, tree.right);
|
|
4194
|
-
return this.withRight(tree, this.replaceAt(tree.right, index - before - 1, leaf, own));
|
|
4195
|
-
}
|
|
4196
|
-
/** This function puts one leaf before the leaf at an index. It copies only the path, and `join` keeps the balance. */
|
|
4197
|
-
insertAt(tree, index, leaf, own) {
|
|
4198
|
-
if (tree === null) return this.node(null, leaf, own, null);
|
|
4199
|
-
const before = sizeOf(tree.left);
|
|
4200
|
-
if (index <= before) return this.join(this.insertAt(tree.left, index, leaf, own), tree.leaf, tree.own, tree.right);
|
|
4201
|
-
return this.join(tree.left, tree.leaf, tree.own, this.insertAt(tree.right, index - before - 1, leaf, own), tree.inner);
|
|
4202
|
-
}
|
|
4203
|
-
/**
|
|
4204
|
-
* This function replaces the leaf at an index with one or more leaves. It descends once.
|
|
4205
|
-
* At the node of the leaf, it puts the first new leaf in the node.
|
|
4206
|
-
* It puts the other new leaves at the start of the right subtree of that node.
|
|
4207
|
-
*/
|
|
4208
|
-
replaceSpan(tree, index, leaves) {
|
|
4209
|
-
const before = sizeOf(tree.left);
|
|
4210
|
-
if (index < before) return this.join(this.replaceSpan(tree.left, index, leaves), tree.leaf, tree.own, tree.right);
|
|
4211
|
-
if (index > before) return this.join(tree.left, tree.leaf, tree.own, this.replaceSpan(tree.right, index - before - 1, leaves), tree.inner);
|
|
4212
|
-
let right = tree.right;
|
|
4213
|
-
for (let i = 1; i < leaves.length; i++) right = this.insertAt(right, i - 1, leaves[i], this.spec.measure(leaves[i]));
|
|
4214
|
-
return this.join(tree.left, leaves[0], this.spec.measure(leaves[0]), right);
|
|
4215
|
-
}
|
|
4216
|
-
/** This function removes the leaf at an index. It copies only the path, and `join` keeps the balance. */
|
|
4217
|
-
removeAt(tree, index) {
|
|
4218
|
-
const before = sizeOf(tree.left);
|
|
4219
|
-
if (index === before) return this.concat(tree.left, tree.right);
|
|
4220
|
-
if (index < before) return this.join(this.removeAt(tree.left, index), tree.leaf, tree.own, tree.right);
|
|
4221
|
-
return this.join(tree.left, tree.leaf, tree.own, this.removeAt(tree.right, index - before - 1), tree.inner);
|
|
4222
|
-
}
|
|
4223
|
-
/** This function gives the leaf at an index of the tree. The index must be in the tree. */
|
|
4224
|
-
leafAt(tree, index) {
|
|
4225
|
-
let node = tree;
|
|
4226
|
-
for (;;) {
|
|
4227
|
-
const before = sizeOf(node.left);
|
|
4228
|
-
if (index === before) return node.leaf;
|
|
4229
|
-
if (index < before) node = node.left;
|
|
4230
|
-
else {
|
|
4231
|
-
index -= before + 1;
|
|
4232
|
-
node = node.right;
|
|
4233
|
-
}
|
|
4234
|
-
}
|
|
4235
|
-
}
|
|
4236
|
-
/** This function joins the leaves at `index` and `index + 1` when `spec.merge` gives one leaf for them. */
|
|
4237
|
-
seam(tree, index) {
|
|
4238
|
-
const { merge } = this.spec;
|
|
4239
|
-
if (merge === void 0 || tree === null || index < 0 || index + 1 >= tree.size) return tree;
|
|
4240
|
-
const merged = merge(this.leafAt(tree, index), this.leafAt(tree, index + 1));
|
|
4241
|
-
if (merged === void 0) return tree;
|
|
4242
|
-
return this.removeAt(this.replaceAt(tree, index, merged, this.spec.measure(merged)), index + 1);
|
|
4243
|
-
}
|
|
4244
|
-
/** This function makes a balanced tree of the leaves `items[lo]` to `items[hi - 1]`. */
|
|
4245
|
-
build(items, lo, hi) {
|
|
4246
|
-
if (lo >= hi) return null;
|
|
4247
|
-
const mid = lo + hi >>> 1;
|
|
4248
|
-
const leaf = items[mid];
|
|
4249
|
-
return this.node(this.build(items, lo, mid), leaf, this.spec.measure(leaf), this.build(items, mid + 1, hi));
|
|
4250
|
-
}
|
|
4251
|
-
};
|
|
4252
|
-
/** The largest number of removed and inserted leaves for which `splice` copies paths instead of splitting the tree. */
|
|
4253
|
-
const SMALL_EDIT = 8;
|
|
4254
|
-
/** The builders of each spec. Each spec has one builder, so that two trees of the same spec can be joined. */
|
|
4255
|
-
const builders = /* @__PURE__ */ new WeakMap();
|
|
4256
|
-
function builderOf(spec) {
|
|
4257
|
-
let builder = builders.get(spec);
|
|
4258
|
-
if (builder === void 0) {
|
|
4259
|
-
builder = new Builder(spec);
|
|
4260
|
-
builders.set(spec, builder);
|
|
4261
|
-
}
|
|
4262
|
-
return builder;
|
|
4263
|
-
}
|
|
4264
|
-
function* walk(tree) {
|
|
4265
|
-
const stack = [];
|
|
4266
|
-
let node = tree;
|
|
4267
|
-
while (node !== null || stack.length > 0) {
|
|
4268
|
-
while (node !== null) {
|
|
4269
|
-
stack.push(node);
|
|
4270
|
-
node = node.left;
|
|
4271
|
-
}
|
|
4272
|
-
const top = stack.pop();
|
|
4273
|
-
yield top.leaf;
|
|
4274
|
-
node = top.right;
|
|
4275
|
-
}
|
|
4276
|
-
}
|
|
4277
|
-
/** This function gives the index of a leaf, with a negative index counted from the end, or -1 when it is not in the tree. */
|
|
4278
|
-
function indexOf(index, size) {
|
|
4279
|
-
if (!Number.isInteger(index)) return -1;
|
|
4280
|
-
const at = index < 0 ? size + index : index;
|
|
4281
|
-
return at >= 0 && at < size ? at : -1;
|
|
4282
|
-
}
|
|
4283
|
-
function clamp$1(value, size) {
|
|
4284
|
-
if (Number.isNaN(value)) return 0;
|
|
4285
|
-
const whole = Math.trunc(value);
|
|
4286
|
-
return whole < 0 ? Math.max(0, size + whole) : Math.min(whole, size);
|
|
4287
|
-
}
|
|
4079
|
+
/**
|
|
4080
|
+
* SummaryTree: the class form of the summary tree, a persistent sequence of leaves with a summary of each part.
|
|
4081
|
+
*
|
|
4082
|
+
* The class wraps the plain functions of the entry `@mark1russell7/collections/summary-tree`. Its `record` is the
|
|
4083
|
+
* plain record that those functions take. A bundle that uses the class keeps each of its methods, so a program that
|
|
4084
|
+
* calls only a few operations imports the functions instead.
|
|
4085
|
+
*
|
|
4086
|
+
* @example
|
|
4087
|
+
* // The number of leaves and the total length of the strings
|
|
4088
|
+
* const spec = {
|
|
4089
|
+
* monoid: { empty: { count: 0, length: 0 }, combine: (a, b) => ({ count: a.count + b.count, length: a.length + b.length }) },
|
|
4090
|
+
* measure: (leaf: string) => ({ count: 1, length: leaf.length }),
|
|
4091
|
+
* };
|
|
4092
|
+
* const tree = summaryTree(spec, ["ab", "cde", "f"]);
|
|
4093
|
+
* tree.summary; // { count: 3, length: 6 }
|
|
4094
|
+
* const [left, right] = tree.split((prefix) => prefix.length > 2); // left: ["ab"], right: ["cde", "f"]
|
|
4095
|
+
*/
|
|
4288
4096
|
/**
|
|
4289
4097
|
* A persistent sequence of leaves with a summary of each part. Make one with `summaryTree`. Each method that changes
|
|
4290
4098
|
* the sequence gives a new tree.
|
|
4291
4099
|
*/
|
|
4292
4100
|
var SummaryTree = class SummaryTree {
|
|
4293
|
-
|
|
4294
|
-
|
|
4295
|
-
constructor(
|
|
4296
|
-
this.
|
|
4297
|
-
this.root = root;
|
|
4101
|
+
/** The plain record of the tree: its spec and its root. The functions of the entry `./summary-tree` take it. */
|
|
4102
|
+
record;
|
|
4103
|
+
constructor(record) {
|
|
4104
|
+
this.record = record;
|
|
4298
4105
|
}
|
|
4299
4106
|
/** This function makes a tree of a spec from leaves, as `summaryTree` does. */
|
|
4300
4107
|
static of(spec, leaves = []) {
|
|
4301
|
-
|
|
4302
|
-
const items = [...leaves];
|
|
4303
|
-
return new SummaryTree(builder, builder.build(items, 0, items.length));
|
|
4108
|
+
return new SummaryTree(of(spec, leaves));
|
|
4304
4109
|
}
|
|
4305
|
-
|
|
4306
|
-
|
|
4110
|
+
/** This function wraps a plain record of the entry `./summary-tree` in a tree. It does not copy the nodes. */
|
|
4111
|
+
static fromRecord(record) {
|
|
4112
|
+
return new SummaryTree(record);
|
|
4307
4113
|
}
|
|
4308
|
-
|
|
4309
|
-
|
|
4310
|
-
if (leaves.builder !== this.builder) throw new TypeError("SummaryTree: the two trees have different specs");
|
|
4311
|
-
return leaves.root;
|
|
4312
|
-
}
|
|
4313
|
-
const items = [...leaves];
|
|
4314
|
-
return this.builder.build(items, 0, items.length);
|
|
4114
|
+
wrap(record) {
|
|
4115
|
+
return record === this.record ? this : new SummaryTree(record);
|
|
4315
4116
|
}
|
|
4316
4117
|
/** The spec of the tree: its monoid and its measure. */
|
|
4317
4118
|
get spec() {
|
|
4318
|
-
return this.
|
|
4119
|
+
return this.record.spec;
|
|
4319
4120
|
}
|
|
4320
4121
|
/** The number of leaves. */
|
|
4321
4122
|
get size() {
|
|
4322
|
-
return
|
|
4123
|
+
return size(this.record);
|
|
4323
4124
|
}
|
|
4324
4125
|
get isEmpty() {
|
|
4325
|
-
return this.
|
|
4126
|
+
return isEmpty(this.record);
|
|
4326
4127
|
}
|
|
4327
4128
|
/** The summary of all the leaves, in their order. An empty tree has the summary `monoid.empty`. */
|
|
4328
4129
|
get summary() {
|
|
4329
|
-
return
|
|
4130
|
+
return summary(this.record);
|
|
4330
4131
|
}
|
|
4331
4132
|
/** The height of the tree: 0 for an empty tree. The height is at most 1.45 log2(size + 2). */
|
|
4332
4133
|
get height() {
|
|
4333
|
-
return
|
|
4134
|
+
return height(this.record);
|
|
4334
4135
|
}
|
|
4335
4136
|
/**
|
|
4336
4137
|
* This function gives the leaf at an index, or `undefined` when the index is not in the tree. A negative index counts
|
|
4337
4138
|
* from the end, as `Array.prototype.at` does.
|
|
4338
4139
|
*/
|
|
4339
4140
|
at(index) {
|
|
4340
|
-
|
|
4341
|
-
return index === -1 ? void 0 : this.builder.leafAt(this.root, index);
|
|
4141
|
+
return at(this.record, index);
|
|
4342
4142
|
}
|
|
4343
4143
|
/** This function gives the summary of the leaves from `start` to `end`, with `end` not included. */
|
|
4344
4144
|
summaryOf(start = 0, end = this.size) {
|
|
4345
|
-
return this.
|
|
4145
|
+
return summaryOf(this.record, start, end);
|
|
4346
4146
|
}
|
|
4347
4147
|
/** This function gives the tree of the leaves of this tree, then the leaves of `other`. The two trees must have the same spec. */
|
|
4348
4148
|
concat(other) {
|
|
4349
|
-
|
|
4350
|
-
return this.wrap(this.builder.seam(joined, this.size - 1));
|
|
4149
|
+
return this.wrap(concat$1(this.record, recordOf(other)));
|
|
4351
4150
|
}
|
|
4352
4151
|
/** This function splits the tree into its first `index` leaves and the other leaves. */
|
|
4353
4152
|
splitAt(index) {
|
|
4354
|
-
const [left, right] =
|
|
4153
|
+
const [left, right] = splitAt(this.record, index);
|
|
4355
4154
|
return [this.wrap(left), this.wrap(right)];
|
|
4356
4155
|
}
|
|
4357
4156
|
/**
|
|
@@ -4360,7 +4159,7 @@ var SummaryTree = class SummaryTree {
|
|
|
4360
4159
|
* prefix. When no prefix satisfies it, the right tree is empty.
|
|
4361
4160
|
*/
|
|
4362
4161
|
split(predicate) {
|
|
4363
|
-
const [left, right] =
|
|
4162
|
+
const [left, right] = split(this.record, predicate);
|
|
4364
4163
|
return [this.wrap(left), this.wrap(right)];
|
|
4365
4164
|
}
|
|
4366
4165
|
/**
|
|
@@ -4368,27 +4167,7 @@ var SummaryTree = class SummaryTree {
|
|
|
4368
4167
|
* nothing. It gives `undefined` when no prefix satisfies the predicate.
|
|
4369
4168
|
*/
|
|
4370
4169
|
search(predicate) {
|
|
4371
|
-
|
|
4372
|
-
let node = this.root;
|
|
4373
|
-
let acc = this.builder.spec.monoid.empty;
|
|
4374
|
-
let index = 0;
|
|
4375
|
-
while (node !== null) {
|
|
4376
|
-
const throughLeft = node.left === null ? acc : combine(acc, node.left.summary);
|
|
4377
|
-
if (node.left !== null && predicate(throughLeft)) {
|
|
4378
|
-
node = node.left;
|
|
4379
|
-
continue;
|
|
4380
|
-
}
|
|
4381
|
-
const through = combine(throughLeft, node.own);
|
|
4382
|
-
if (predicate(through)) return {
|
|
4383
|
-
index: index + sizeOf(node.left),
|
|
4384
|
-
leaf: node.leaf,
|
|
4385
|
-
before: throughLeft,
|
|
4386
|
-
through
|
|
4387
|
-
};
|
|
4388
|
-
index += sizeOf(node.left) + 1;
|
|
4389
|
-
acc = through;
|
|
4390
|
-
node = node.right;
|
|
4391
|
-
}
|
|
4170
|
+
return search(this.record, predicate);
|
|
4392
4171
|
}
|
|
4393
4172
|
/**
|
|
4394
4173
|
* This function finds the leaf that holds a position of an additive key, and it calls no `combine`. The key must be
|
|
@@ -4401,80 +4180,26 @@ var SummaryTree = class SummaryTree {
|
|
|
4401
4180
|
* const at = rope.locate((s) => s.length, 1000);
|
|
4402
4181
|
*/
|
|
4403
4182
|
locate(key, target) {
|
|
4404
|
-
|
|
4405
|
-
let node = this.root;
|
|
4406
|
-
let index = 0;
|
|
4407
|
-
let rest = target;
|
|
4408
|
-
while (node !== null) {
|
|
4409
|
-
if (node.left !== null) {
|
|
4410
|
-
const left = key(node.left.summary);
|
|
4411
|
-
if (rest < left) {
|
|
4412
|
-
node = node.left;
|
|
4413
|
-
continue;
|
|
4414
|
-
}
|
|
4415
|
-
rest -= left;
|
|
4416
|
-
index += node.left.size;
|
|
4417
|
-
}
|
|
4418
|
-
const own = key(node.own);
|
|
4419
|
-
if (rest < own) return {
|
|
4420
|
-
index,
|
|
4421
|
-
leaf: node.leaf,
|
|
4422
|
-
offset: rest
|
|
4423
|
-
};
|
|
4424
|
-
rest -= own;
|
|
4425
|
-
index += 1;
|
|
4426
|
-
node = node.right;
|
|
4427
|
-
}
|
|
4183
|
+
return locate(this.record, key, target);
|
|
4428
4184
|
}
|
|
4429
4185
|
/** This function gives the tree of the leaves from `start` to `end`, with `end` not included. A negative index counts from the end. */
|
|
4430
4186
|
slice(start = 0, end = this.size) {
|
|
4431
|
-
|
|
4432
|
-
const to = clamp$1(end, this.size);
|
|
4433
|
-
if (to <= from) return this.wrap(null);
|
|
4434
|
-
const [, rest] = this.builder.splitAt(this.root, from);
|
|
4435
|
-
const [middle] = this.builder.splitAt(rest, to - from);
|
|
4436
|
-
return this.wrap(middle);
|
|
4187
|
+
return this.wrap(slice(this.record, start, end));
|
|
4437
4188
|
}
|
|
4438
4189
|
/**
|
|
4439
4190
|
* This function removes `deleteCount` leaves at `start` and puts the leaves of `inserted` there, as
|
|
4440
4191
|
* `Array.prototype.splice` does. It gives the new tree, not the removed leaves.
|
|
4441
4192
|
*/
|
|
4442
4193
|
splice(start, deleteCount = this.size, inserted = []) {
|
|
4443
|
-
|
|
4444
|
-
const count = Math.max(0, Math.min(Number.isNaN(deleteCount) ? 0 : Math.trunc(deleteCount), this.size - from));
|
|
4445
|
-
const builder = this.builder;
|
|
4446
|
-
const leaves = inserted instanceof SummaryTree ? void 0 : [...inserted];
|
|
4447
|
-
const added = leaves === void 0 ? inserted.size : leaves.length;
|
|
4448
|
-
if (count === 0 && added === 0) return this;
|
|
4449
|
-
let root = this.root;
|
|
4450
|
-
if (leaves !== void 0 && count + added <= SMALL_EDIT) {
|
|
4451
|
-
const replaced = Math.min(count, added);
|
|
4452
|
-
if (count > 0 && added > count) {
|
|
4453
|
-
for (let i = 0; i < count - 1; i++) root = builder.replaceAt(root, from + i, leaves[i], builder.spec.measure(leaves[i]));
|
|
4454
|
-
root = builder.replaceSpan(root, from + count - 1, leaves.slice(count - 1));
|
|
4455
|
-
} else for (let i = 0; i < added; i++) {
|
|
4456
|
-
const leaf = leaves[i];
|
|
4457
|
-
const own = builder.spec.measure(leaf);
|
|
4458
|
-
root = i < replaced ? builder.replaceAt(root, from + i, leaf, own) : builder.insertAt(root, from + i, leaf, own);
|
|
4459
|
-
}
|
|
4460
|
-
for (let i = replaced; i < count; i++) root = builder.removeAt(root, from + added);
|
|
4461
|
-
} else {
|
|
4462
|
-
const middle = leaves === void 0 ? this.treeOf(inserted) : builder.build(leaves, 0, leaves.length);
|
|
4463
|
-
const [left, rest] = builder.splitAt(root, from);
|
|
4464
|
-
const [, right] = builder.splitAt(rest, count);
|
|
4465
|
-
root = builder.concat(builder.concat(left, middle), right);
|
|
4466
|
-
}
|
|
4467
|
-
if (added > 0) root = builder.seam(root, from + added - 1);
|
|
4468
|
-
return this.wrap(builder.seam(root, from - 1));
|
|
4194
|
+
return this.wrap(splice(this.record, start, deleteCount, recordOf(inserted)));
|
|
4469
4195
|
}
|
|
4470
4196
|
/** This function puts the leaves of `inserted` before the leaf at `index`. */
|
|
4471
4197
|
insert(index, inserted) {
|
|
4472
|
-
return this.
|
|
4198
|
+
return this.wrap(insert(this.record, index, recordOf(inserted)));
|
|
4473
4199
|
}
|
|
4474
4200
|
/** This function removes the leaves from `start` to `end`, with `end` not included. */
|
|
4475
4201
|
remove(start, end = this.size) {
|
|
4476
|
-
|
|
4477
|
-
return this.splice(from, Math.max(0, clamp$1(end, this.size) - from));
|
|
4202
|
+
return this.wrap(remove(this.record, start, end));
|
|
4478
4203
|
}
|
|
4479
4204
|
/**
|
|
4480
4205
|
* This function replaces the leaf at an index. A negative index counts from the end, as `Array.prototype.with` does.
|
|
@@ -4482,27 +4207,29 @@ var SummaryTree = class SummaryTree {
|
|
|
4482
4207
|
* `spec.merge`.
|
|
4483
4208
|
*/
|
|
4484
4209
|
set(index, leaf) {
|
|
4485
|
-
|
|
4486
|
-
return at === -1 ? this : this.wrap(this.builder.replaceAt(this.root, at, leaf, this.builder.spec.measure(leaf)));
|
|
4210
|
+
return this.wrap(set(this.record, index, leaf));
|
|
4487
4211
|
}
|
|
4488
4212
|
/** This function adds leaves after the last leaf. */
|
|
4489
4213
|
append(...leaves) {
|
|
4490
|
-
return this.
|
|
4214
|
+
return this.wrap(append(this.record, ...leaves));
|
|
4491
4215
|
}
|
|
4492
4216
|
/** This function adds leaves before the first leaf. */
|
|
4493
4217
|
prepend(...leaves) {
|
|
4494
|
-
|
|
4495
|
-
return this.wrap(this.builder.seam(joined, leaves.length - 1));
|
|
4218
|
+
return this.wrap(prepend(this.record, ...leaves));
|
|
4496
4219
|
}
|
|
4497
4220
|
/** This function gives the leaves in their order. */
|
|
4498
4221
|
[Symbol.iterator]() {
|
|
4499
|
-
return
|
|
4222
|
+
return leaves(this.record);
|
|
4500
4223
|
}
|
|
4501
4224
|
/** This function gives the leaves in an array. */
|
|
4502
4225
|
toArray() {
|
|
4503
|
-
return
|
|
4226
|
+
return toArray$2(this.record);
|
|
4504
4227
|
}
|
|
4505
4228
|
};
|
|
4229
|
+
/** This function gives the record of a tree, or the leaves when they are not a tree. */
|
|
4230
|
+
function recordOf(leaves) {
|
|
4231
|
+
return leaves instanceof SummaryTree ? leaves.record : leaves;
|
|
4232
|
+
}
|
|
4506
4233
|
/**
|
|
4507
4234
|
* This function makes a persistent tree of leaves with a summary of each part. Use the same `spec` object for each
|
|
4508
4235
|
* tree that `concat` or `splice` joins.
|