@noldova/context.collections 0.0.13 → 0.0.14

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 CHANGED
@@ -2,7 +2,8 @@
2
2
 
3
3
  Collections for CONTEXT libraries and apps, with queries over them:
4
4
  `List`, `Dictionary`, `HashSet`, `Queue` and `Stack`, each an `Enumerable` with
5
- the query operators; read-only views; equality and ordering comparers; and
5
+ the query operators; `ChunkedList`, a list held in chunks that sums its
6
+ elements' weights; read-only views; equality and ordering comparers; and
6
7
  `BinarySearchService`, which finds positions in ascending numeric arrays.
7
8
 
8
9
  ## Install
@@ -47,6 +48,22 @@ byId.tryGet(9); // undefined
47
48
  Enumerable.from([3, 1, 2]).orderByDescending(t => t).take(2).toArray(); // [3, 2]
48
49
  ```
49
50
 
51
+ A `ChunkedList` suits a long list that is mostly one value, such as a count
52
+ of matches for each line of a large text. A chunk that holds only that value
53
+ takes no storage until an element in it is set, and each chunk keeps the sum
54
+ of its elements' weights:
55
+
56
+ ```ts
57
+ import { ChunkedList } from "@noldova/context.collections";
58
+
59
+ const matches = new ChunkedList<number>(1024, 0, 100_000, t => t);
60
+ matches.set(10, 2);
61
+ matches.set(500, 3);
62
+ matches.totalWeight; // 5
63
+ matches.weightBefore(500); // 2
64
+ matches.nextWeightedIndex(11); // 500
65
+ ```
66
+
50
67
  Arrays stay native: `Enumerable.from` gives any iterable the operators, and
51
68
  `new List(array)` copies one into a list. Operators that return a sequence are
52
69
  deferred and run again on each enumeration. A method that needs an element
package/api/index.d.ts CHANGED
@@ -1181,6 +1181,203 @@ export declare class List<T extends {} | null> extends Enumerable<T> implements
1181
1181
  public asReadOnly(): ReadOnlyList<T>;
1182
1182
  }
1183
1183
 
1184
+ /**
1185
+ * A list held in chunks, for long lists that are mostly one value and for
1186
+ * sums of the elements' weights over a range.
1187
+ *
1188
+ * @template T The type of the elements; not `undefined`, which means no element.
1189
+ *
1190
+ * @remarks
1191
+ * The elements are kept in chunks of at most `chunkSize`. A chunk that holds
1192
+ * only the fill value it was created with takes no storage until an element
1193
+ * in it is set, so a list of a million fill values holds only its chunks'
1194
+ * lengths. Each chunk keeps the sum of its elements' weights, so the total
1195
+ * weight is O(1), and the weight before a position and the next or previous
1196
+ * weighted element take O(chunks) plus O(chunkSize), skipping chunks whose
1197
+ * weight is 0. Reading or setting a position is O(log chunks), plus
1198
+ * O(chunkSize) the first time a fill chunk is set. Inserting or removing
1199
+ * copies only the kept ends of the first and last chunks it touches, drops
1200
+ * the chunks it removes whole without reading them, and renumbers the chunks
1201
+ * in O(chunks). The rebuilt elements split into chunks of nearly equal size,
1202
+ * and a chunk left with fewer than half of `chunkSize` joins a neighbour, so
1203
+ * the chunk count stays near the length over `chunkSize`. A change that
1204
+ * weighs an element whose weight isn't a finite number that isn't negative
1205
+ * throws `ArgumentOutOfRangeException` and leaves the list as it was.
1206
+ * Changing the list while it is
1207
+ * being enumerated makes the enumeration throw `InvalidOperationException` at
1208
+ * its next step.
1209
+ *
1210
+ * @example
1211
+ * ```ts
1212
+ * import { ChunkedList } from "@noldova/context.collections";
1213
+ *
1214
+ * const matches = new ChunkedList<number>(1024, 0, 100_000, t => t);
1215
+ * matches.set(10, 2);
1216
+ * matches.set(500, 3);
1217
+ * matches.totalWeight; // 5
1218
+ * matches.weightBefore(500); // 2
1219
+ * matches.nextWeightedIndex(11); // 500
1220
+ * matches.previousWeightedIndex(500); // 10
1221
+ * matches.insertRange(0, [0, 0]);
1222
+ * matches.nextWeightedIndex(0); // 12
1223
+ * ```
1224
+ */
1225
+ export declare class ChunkedList<T extends {} | null> extends Enumerable<T> implements IReadOnlyList<T> {
1226
+ /**
1227
+ * Creates a list of `length` fill values.
1228
+ *
1229
+ * @param chunkSize The most elements a chunk holds.
1230
+ * @param fill The value of every element at the start; chunks that hold
1231
+ * only it take no storage.
1232
+ * @param length How many elements the list starts with. Omitted, it is 0.
1233
+ * @param weigh An element's weight, a finite number that isn't negative.
1234
+ * Omitted, every element weighs 0.
1235
+ * @throws {ArgumentOutOfRangeException} When `chunkSize` isn't a positive
1236
+ * integer, `length` isn't a non-negative integer, or `length` is positive
1237
+ * and the fill value's weight isn't a finite number that isn't negative.
1238
+ */
1239
+ public constructor(chunkSize: number, fill: T, length?: number, weigh?: (item: T) => number);
1240
+
1241
+ /**
1242
+ * The sum of every element's weight.
1243
+ */
1244
+ public get totalWeight(): number;
1245
+
1246
+ /**
1247
+ * Starts an enumeration of the elements in order.
1248
+ */
1249
+ public [Symbol.iterator](): Iterator<T>;
1250
+
1251
+ /**
1252
+ * The element at a zero-based position.
1253
+ *
1254
+ * @throws {ArgumentOutOfRangeException} When there is no element there,
1255
+ * including a negative or fractional position.
1256
+ */
1257
+ public get(index: number): T;
1258
+
1259
+ /**
1260
+ * The element at a zero-based position; `undefined` when there is none.
1261
+ */
1262
+ public tryGet(index: number): T | undefined;
1263
+
1264
+ /**
1265
+ * Replaces the element at a zero-based position, and its weight in the sums.
1266
+ *
1267
+ * @throws {ArgumentOutOfRangeException} When there is no element there, or
1268
+ * the element's weight isn't a finite number that isn't negative.
1269
+ */
1270
+ public set(index: number, item: T): void;
1271
+
1272
+ /**
1273
+ * Adds an element at the end.
1274
+ */
1275
+ public add(item: T): void;
1276
+
1277
+ /**
1278
+ * Adds elements at the end, in order.
1279
+ */
1280
+ public addRange(items: Iterable<T>): void;
1281
+
1282
+ /**
1283
+ * Inserts an element before a zero-based position; the list's length puts it at the end.
1284
+ *
1285
+ * @throws {ArgumentOutOfRangeException} When the position isn't an integer from
1286
+ * zero to the list's length.
1287
+ */
1288
+ public insert(index: number, item: T): void;
1289
+
1290
+ /**
1291
+ * Inserts elements, in order, before a zero-based position.
1292
+ *
1293
+ * @throws {ArgumentOutOfRangeException} When the position isn't an integer from
1294
+ * zero to the list's length.
1295
+ */
1296
+ public insertRange(index: number, items: Iterable<T>): void;
1297
+
1298
+ /**
1299
+ * Removes the first element equal to `item` by SameValueZero.
1300
+ *
1301
+ * @returns Whether an element was removed.
1302
+ */
1303
+ public remove(item: T): boolean;
1304
+
1305
+ /**
1306
+ * Removes the element at a zero-based position.
1307
+ *
1308
+ * @throws {ArgumentOutOfRangeException} When there is no element there.
1309
+ */
1310
+ public removeAt(index: number): void;
1311
+
1312
+ /**
1313
+ * Removes `count` elements from a zero-based position.
1314
+ *
1315
+ * @throws {ArgumentOutOfRangeException} When the position isn't an integer from
1316
+ * zero to the list's length, `count` isn't a non-negative integer, or the
1317
+ * range runs past the end.
1318
+ */
1319
+ public removeRange(index: number, count: number): void;
1320
+
1321
+ /**
1322
+ * Removes every element that matches. The elements left all hold storage.
1323
+ *
1324
+ * @returns How many were removed.
1325
+ */
1326
+ public removeAll(predicate: (item: T) => boolean): number;
1327
+
1328
+ /**
1329
+ * Removes every element.
1330
+ */
1331
+ public clear(): void;
1332
+
1333
+ /**
1334
+ * The position of the first element equal to `item` by SameValueZero, or `-1`.
1335
+ */
1336
+ public indexOf(item: T): number;
1337
+
1338
+ /**
1339
+ * The position of the last element equal to `item` by SameValueZero, or `-1`.
1340
+ */
1341
+ public lastIndexOf(item: T): number;
1342
+
1343
+ /**
1344
+ * Sorts the list in place, keeping the order of equal elements. The
1345
+ * elements then all hold storage.
1346
+ *
1347
+ * @param comparison The order. Omitted, it is {@link Comparer.compare}.
1348
+ */
1349
+ public sort(comparison?: Comparison<T>): void;
1350
+
1351
+ /**
1352
+ * Returns a view that reads this list, follows its changes and can't change it.
1353
+ */
1354
+ public asReadOnly(): ReadOnlyList<T>;
1355
+
1356
+ /**
1357
+ * The sum of the weights of the elements before a zero-based position.
1358
+ *
1359
+ * @throws {ArgumentOutOfRangeException} When the position isn't an integer from
1360
+ * zero to the list's length.
1361
+ */
1362
+ public weightBefore(index: number): number;
1363
+
1364
+ /**
1365
+ * The first position from `index` on whose element weighs more than 0, or `-1`.
1366
+ *
1367
+ * @throws {ArgumentOutOfRangeException} When the position isn't an integer from
1368
+ * zero to the list's length.
1369
+ */
1370
+ public nextWeightedIndex(index: number): number;
1371
+
1372
+ /**
1373
+ * The last position before `index` whose element weighs more than 0, or `-1`.
1374
+ *
1375
+ * @throws {ArgumentOutOfRangeException} When the position isn't an integer from
1376
+ * zero to the list's length.
1377
+ */
1378
+ public previousWeightedIndex(index: number): number;
1379
+ }
1380
+
1184
1381
  /**
1185
1382
  * Values stored by key.
1186
1383
  *
package/api/index.js CHANGED
@@ -6,6 +6,7 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  import "../extensions/enumerable.extensions.js";
9
+ export { ChunkedList } from "../models/chunked-list.js";
9
10
  export { DefaultEqualityComparer } from "../models/default-equality-comparer.js";
10
11
  export { Dictionary } from "../models/dictionary.js";
11
12
  export { Enumerable } from "../models/enumerable.js";
package/api/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/noldova.context.collections/src/api/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,wCAAwC,CAAC;AAMhD,OAAO,EAAE,uBAAuB,EAAE,MAAM,wCAAwC,CAAC;AACjF,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAC;AACrD,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAE,mBAAmB,EAAE,MAAM,oCAAoC,CAAC;AACzE,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAC3D,OAAO,EAAE,IAAI,EAAE,MAAM,mBAAmB,CAAC;AACzC,OAAO,EAAE,iBAAiB,EAAE,MAAM,iCAAiC,CAAC;AACpE,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,kBAAkB,EAAE,MAAM,mCAAmC,CAAC;AACvE,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC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EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC"}
@@ -0,0 +1,160 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) Noldova.
4
+ *
5
+ * This source code is licensed under the license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import "@noldova/context.core";
9
+ import { ArgumentOutOfRangeException } from "@noldova/context.exceptions";
10
+ import { Resources } from "../resources.js";
11
+ import { Comparer } from "../services/comparer.js";
12
+ import { EqualityComparers } from "../services/equality-comparers.js";
13
+ import { ChangeCounter } from "./change-counter.js";
14
+ import { Enumerable } from "./enumerable.js";
15
+ import { ListChunks } from "./list-chunks.js";
16
+ import { ReadOnlyList } from "./read-only-list.js";
17
+ export class ChunkedList extends Enumerable {
18
+ changes = new ChangeCounter();
19
+ chunks;
20
+ weigh;
21
+ constructor(chunkSize, fill, length = 0, weigh = ChunkedList.weighNothing) {
22
+ super();
23
+ ArgumentOutOfRangeException.throwIfNotPositiveInteger(chunkSize, Resources.chunkSizeParameterName);
24
+ ArgumentOutOfRangeException.throwIfNotInteger(length, Resources.lengthParameterName);
25
+ ArgumentOutOfRangeException.throwIfNegative(length, Resources.lengthParameterName);
26
+ this.chunks = new ListChunks(chunkSize, fill, length, weigh);
27
+ this.weigh = weigh;
28
+ }
29
+ get totalWeight() {
30
+ return this.chunks.totalWeight;
31
+ }
32
+ *[Symbol.iterator]() {
33
+ const version = this.changes.value;
34
+ for (const chunk of this.chunks)
35
+ for (const item of chunk) {
36
+ yield item;
37
+ this.changes.throwIfChangedSince(version);
38
+ }
39
+ }
40
+ count(predicate) {
41
+ return Object.isUndefined(predicate) ? this.chunks.length : super.count(predicate);
42
+ }
43
+ get(index) {
44
+ const item = this.tryGet(index);
45
+ if (Object.isUndefined(item))
46
+ throw new ArgumentOutOfRangeException(Resources.indexParameterName, index);
47
+ return item;
48
+ }
49
+ tryGet(index) {
50
+ const chunk = this.chunks.at(index);
51
+ return chunk?.get(index - chunk.start);
52
+ }
53
+ set(index, item) {
54
+ const chunk = this.chunks.at(index);
55
+ if (Object.isUndefined(chunk))
56
+ throw new ArgumentOutOfRangeException(Resources.indexParameterName, index);
57
+ this.chunks.set(chunk, index, item);
58
+ this.changes.increment();
59
+ }
60
+ add(item) {
61
+ this.replace(this.chunks.length, 0, [item]);
62
+ }
63
+ addRange(items) {
64
+ this.replace(this.chunks.length, 0, Array.from(items));
65
+ }
66
+ insert(index, item) {
67
+ this.insertRange(index, [item]);
68
+ }
69
+ insertRange(index, items) {
70
+ this.throwIfNotPosition(index);
71
+ this.replace(index, 0, Array.from(items));
72
+ }
73
+ remove(item) {
74
+ const index = this.indexOf(item);
75
+ if (index < 0)
76
+ return false;
77
+ this.removeAt(index);
78
+ return true;
79
+ }
80
+ removeAt(index) {
81
+ this.get(index);
82
+ this.replace(index, 1, []);
83
+ }
84
+ removeRange(index, count) {
85
+ this.throwIfNotPosition(index);
86
+ ArgumentOutOfRangeException.throwIfNotInteger(count, Resources.countParameterName);
87
+ ArgumentOutOfRangeException.throwIfNegative(count, Resources.countParameterName);
88
+ if (index + count > this.chunks.length)
89
+ throw new ArgumentOutOfRangeException(Resources.countParameterName, count);
90
+ this.replace(index, count, []);
91
+ }
92
+ removeAll(predicate) {
93
+ const kept = this.toArray().filter((item) => !predicate(item));
94
+ const removed = this.chunks.length - kept.length;
95
+ this.replace(0, this.chunks.length, kept);
96
+ return removed;
97
+ }
98
+ clear() {
99
+ this.replace(0, this.chunks.length, []);
100
+ }
101
+ indexOf(item) {
102
+ for (const chunk of this.chunks) {
103
+ const local = chunk.indexOf(item);
104
+ if (local >= 0)
105
+ return chunk.start + local;
106
+ }
107
+ return -1;
108
+ }
109
+ lastIndexOf(item) {
110
+ return this.toArray().findLastIndex((candidate) => EqualityComparers.default.equals(candidate, item));
111
+ }
112
+ sort(comparison = Comparer.compare) {
113
+ this.replace(0, this.chunks.length, this.toArray().sort(comparison));
114
+ }
115
+ asReadOnly() {
116
+ return new ReadOnlyList(this);
117
+ }
118
+ weightBefore(index) {
119
+ this.throwIfNotPosition(index);
120
+ let weight = 0;
121
+ for (const chunk of this.chunks) {
122
+ if (chunk.start + chunk.length > index)
123
+ return weight + chunk.weightBefore(index - chunk.start, this.weigh);
124
+ weight += chunk.sum;
125
+ }
126
+ return weight;
127
+ }
128
+ nextWeightedIndex(index) {
129
+ this.throwIfNotPosition(index);
130
+ for (const chunk of this.chunks.from(index)) {
131
+ const local = chunk.sum === 0 ? -1 : chunk.nextWeightedIndex(index - chunk.start, this.weigh);
132
+ if (local >= 0)
133
+ return chunk.start + local;
134
+ }
135
+ return -1;
136
+ }
137
+ previousWeightedIndex(index) {
138
+ this.throwIfNotPosition(index);
139
+ for (const chunk of this.chunks.backFrom(index - 1)) {
140
+ const local = chunk.sum === 0 ? -1 : chunk.previousWeightedIndex(index - chunk.start, this.weigh);
141
+ if (local >= 0)
142
+ return chunk.start + local;
143
+ }
144
+ return -1;
145
+ }
146
+ static weighNothing() {
147
+ return 0;
148
+ }
149
+ replace(index, removed, items) {
150
+ this.chunks.replace(index, removed, items);
151
+ this.changes.increment();
152
+ }
153
+ throwIfNotPosition(index) {
154
+ ArgumentOutOfRangeException.throwIfNotInteger(index, Resources.indexParameterName);
155
+ ArgumentOutOfRangeException.throwIfNegative(index, Resources.indexParameterName);
156
+ if (index > this.chunks.length)
157
+ throw new ArgumentOutOfRangeException(Resources.indexParameterName, index);
158
+ }
159
+ }
160
+ //# sourceMappingURL=chunked-list.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chunked-list.js","sourceRoot":"","sources":["../../../../src/noldova.context.collections/src/models/chunked-list.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,uBAAuB,CAAC;AAC/B,OAAO,EAAE,2BAA2B,EAAE,MAAM,6BAA6B,CAAC;AAG1E,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,QAAQ,E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AM,CAAC,QAAQ,CAAC,KAAK,GAAG,CAAC,CAAC,EAAE,CAAC;YACpD,MAAM,KAAK,GAAG,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,qBAAqB,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;YAClG,IAAI,KAAK,IAAI,CAAC;gBACZ,OAAO,KAAK,CAAC,KAAK,GAAG,KAAK,CAAC;QAC/B,CAAC;QAED,OAAO,CAAC,CAAC,CAAC;IACZ,CAAC;IAEO,MAAM,CAAC,YAAY;QACzB,OAAO,CAAC,CAAC;IACX,CAAC;IAEO,OAAO,CAAC,KAAa,EAAE,OAAe,EAAE,KAAmB;QACjE,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,EAAE,KAAK,CAAC,CAAC;QAC3C,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC;IAC3B,CAAC;IAEO,kBAAkB,CAAC,KAAa;QACtC,2BAA2B,CAAC,iBAAiB,CAAC,KAAK,EAAE,SAAS,CAAC,kBAAkB,CAAC,CAAC;QACnF,2BAA2B,CAAC,eAAe,CAAC,KAAK,EAAE,SAAS,CAAC,kBAAkB,CAAC,CAAC;QACjF,IAAI,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM;YAC5B,MAAM,IAAI,2BAA2B,CAAC,SAAS,CAAC,kBAAkB,EAAE,KAAK,CAAC,CAAC;IAC/E,CAAC;CACF"}
@@ -0,0 +1,95 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) Noldova.
4
+ *
5
+ * This source code is licensed under the license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import "@noldova/context.core";
9
+ import { ArgumentOutOfRangeException } from "@noldova/context.exceptions";
10
+ import { Resources } from "../resources.js";
11
+ import { EqualityComparers } from "../services/equality-comparers.js";
12
+ export class ListChunk {
13
+ fill;
14
+ items;
15
+ length;
16
+ start = 0;
17
+ sum;
18
+ constructor(fill, items, length, sum) {
19
+ this.fill = fill;
20
+ this.items = items;
21
+ this.length = length;
22
+ this.sum = sum;
23
+ }
24
+ static ofFill(fill, length, weigh) {
25
+ return new ListChunk(fill, null, length, length * ListChunk.weighOne(fill, weigh));
26
+ }
27
+ static of(fill, items, weigh) {
28
+ return new ListChunk(fill, items, items.length, ListChunk.weighAll(items, weigh));
29
+ }
30
+ *[Symbol.iterator]() {
31
+ if (Object.isNull(this.items)) {
32
+ for (let local = 0; local < this.length; local++)
33
+ yield this.fill;
34
+ }
35
+ else
36
+ yield* this.items;
37
+ }
38
+ get(local) {
39
+ return Object.isNull(this.items) ? this.fill : this.items[local];
40
+ }
41
+ set(local, item, weigh) {
42
+ const weight = ListChunk.weighOne(item, weigh);
43
+ this.items ??= new Array(this.length).fill(this.fill);
44
+ const change = weight - ListChunk.weighAll(this.items.splice(local, 1, item), weigh);
45
+ this.sum += change;
46
+ return change;
47
+ }
48
+ slice(start, end = this.length) {
49
+ return Object.isNull(this.items) ? new Array(end - start).fill(this.fill) : this.items.slice(start, end);
50
+ }
51
+ indexOf(item) {
52
+ let local = 0;
53
+ for (const candidate of this) {
54
+ if (EqualityComparers.default.equals(candidate, item))
55
+ return local;
56
+ local++;
57
+ }
58
+ return -1;
59
+ }
60
+ weightBefore(local, weigh) {
61
+ return Object.isNull(this.items) ? local * weigh(this.fill) : ListChunk.weighAll(this.items.slice(0, local), weigh);
62
+ }
63
+ nextWeightedIndex(from, weigh) {
64
+ let local = 0;
65
+ for (const item of this) {
66
+ if (local >= from && weigh(item) > 0)
67
+ return local;
68
+ local++;
69
+ }
70
+ return -1;
71
+ }
72
+ previousWeightedIndex(before, weigh) {
73
+ let found = -1;
74
+ let local = 0;
75
+ for (const item of this) {
76
+ if (local < before && weigh(item) > 0)
77
+ found = local;
78
+ local++;
79
+ }
80
+ return found;
81
+ }
82
+ static weighOne(item, weigh) {
83
+ const weight = weigh(item);
84
+ if (!Number.isFinite(weight) || weight < 0)
85
+ throw new ArgumentOutOfRangeException(Resources.weighParameterName, weight, Resources.weightInvalid);
86
+ return weight;
87
+ }
88
+ static weighAll(items, weigh) {
89
+ let sum = 0;
90
+ for (const item of items)
91
+ sum += ListChunk.weighOne(item, weigh);
92
+ return sum;
93
+ }
94
+ }
95
+ //# sourceMappingURL=list-chunk.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"list-chunk.js","sourceRoot":"","sources":["../../../../src/noldova.context.collections/src/models/list-chunk.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,uBAAuB,CAAC;AAC/B,OAAO,EAAE,2BAA2B,EAAE,MAAM,6BAA6B,CAAC;AAE1E,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,iBAAiB,EAAE,MAAM,mCAAmC,CAAC;AAEtE,MAAM,OAAO,SAAS;IACH,IAAI,CAAI;IACjB,KAAK,CAAa;IACV,MAAM,CAAS;IACxB,KAAK,GAAW,CAAC,CAAC;IAClB,GAAG,CAAS;IAEnB,YAAoB,IAAO,EAAE,KAAiB,EAAE,MAAc,EAAE,GAAW;QACzE,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,CAAC;IAEM,MAAM,CAAC,MAAM,CAAsB,IAAO,EAAE,MAAc,EAAE,KAA0B;QAC3F,OAAO,IAAI,SAAS,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;IACrF,CAAC;IAEM,MAAM,CAAC,EAAE,CAAsB,IAAO,EAAE,KAAU,EAAE,KAA0B;QACnF,OAAO,IAAI,SAA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M,IAAI,IAAI,IAAI,EAAE,CAAC;YACxB,IAAI,KAAK,GAAG,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC;gBACnC,KAAK,GAAG,KAAK,CAAC;YAChB,KAAK,EAAE,CAAC;QACV,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;IAEO,MAAM,CAAC,QAAQ,CAAI,IAAO,EAAE,KAA0B;QAC5D,MAAM,MAAM,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;QAC3B,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,MAAM,GAAG,CAAC;YACxC,MAAM,IAAI,2BAA2B,CAAC,SAAS,CAAC,kBAAkB,EAAE,MAAM,EAAE,SAAS,CAAC,aAAa,CAAC,CAAC;QAEvG,OAAO,MAAM,CAAC;IAChB,CAAC;IAEO,MAAM,CAAC,QAAQ,CAAI,KAAmB,EAAE,KAA0B;QACxE,IAAI,GAAG,GAAG,CAAC,CAAC;QACZ,KAAK,MAAM,IAAI,IAAI,KAAK;YACtB,GAAG,IAAI,SAAS,CAAC,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;QAEzC,OAAO,GAAG,CAAC;IACb,CAAC;CACF"}
@@ -0,0 +1,98 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) Noldova.
4
+ *
5
+ * This source code is licensed under the license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ import "@noldova/context.core";
9
+ import { BinarySearchService } from "../services/binary-search.service.js";
10
+ import { ListChunk } from "./list-chunk.js";
11
+ export class ListChunks {
12
+ chunkSize;
13
+ fill;
14
+ weigh;
15
+ chunks = [];
16
+ starts = [];
17
+ count = 0;
18
+ total = 0;
19
+ constructor(chunkSize, fill, length, weigh) {
20
+ this.chunkSize = chunkSize;
21
+ this.fill = fill;
22
+ this.weigh = weigh;
23
+ for (let start = 0; start < length; start += chunkSize)
24
+ this.chunks.push(ListChunk.ofFill(fill, Math.min(chunkSize, length - start), weigh));
25
+ this.reindex();
26
+ }
27
+ get length() {
28
+ return this.count;
29
+ }
30
+ get totalWeight() {
31
+ return this.total;
32
+ }
33
+ [Symbol.iterator]() {
34
+ return this.chunks[Symbol.iterator]();
35
+ }
36
+ at(index) {
37
+ const chunk = this.chunks[this.indexOf(index)];
38
+ if (!Number.isInteger(index) || index >= this.count || Object.isUndefined(chunk))
39
+ return undefined;
40
+ return chunk;
41
+ }
42
+ from(index) {
43
+ return this.chunks.values().drop(Math.max(this.indexOf(index), 0));
44
+ }
45
+ backFrom(index) {
46
+ return this.chunks.slice(0, this.indexOf(index) + 1).reverse();
47
+ }
48
+ set(chunk, index, item) {
49
+ this.total += chunk.set(index - chunk.start, item, this.weigh);
50
+ }
51
+ replace(index, removed, items) {
52
+ let first = Math.max(this.indexOf(index), 0);
53
+ let last = Math.max(this.indexOf(index + removed - 1), first);
54
+ const head = this.chunks[first];
55
+ const tail = this.chunks[last];
56
+ let values = [
57
+ ...head?.slice(0, index - head.start) ?? [],
58
+ ...items,
59
+ ...tail?.slice(index + removed - tail.start) ?? []
60
+ ];
61
+ const previous = this.chunks[first - 1];
62
+ const next = this.chunks[last + 1];
63
+ if (values.length > 0 && values.length < this.chunkSize / 2) {
64
+ if (!Object.isUndefined(previous)) {
65
+ values = [...previous.slice(0), ...values];
66
+ first--;
67
+ }
68
+ else if (!Object.isUndefined(next)) {
69
+ values = [...values, ...next.slice(0)];
70
+ last++;
71
+ }
72
+ }
73
+ const pieces = Math.ceil(values.length / this.chunkSize);
74
+ const replacing = [];
75
+ for (let piece = 0; piece < pieces; piece++)
76
+ replacing.push(ListChunk.of(this.fill, values.slice(Math.floor(piece * values.length / pieces), Math.floor((piece + 1) * values.length / pieces)), this.weigh));
77
+ this.chunks = [...this.chunks.slice(0, first), ...replacing, ...this.chunks.slice(last + 1)];
78
+ this.reindex();
79
+ }
80
+ indexOf(index) {
81
+ return BinarySearchService.floorIndex(this.starts, index);
82
+ }
83
+ reindex() {
84
+ const starts = [];
85
+ let count = 0;
86
+ let total = 0;
87
+ for (const chunk of this.chunks) {
88
+ chunk.start = count;
89
+ starts.push(count);
90
+ count += chunk.length;
91
+ total += chunk.sum;
92
+ }
93
+ this.starts = starts;
94
+ this.count = count;
95
+ this.total = total;
96
+ }
97
+ }
98
+ //# sourceMappingURL=list-chunks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"list-chunks.js","sourceRoot":"","sources":["../../../../src/noldova.context.collections/src/models/list-chunks.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,uBAAuB,CAAC;AAE/B,OAAO,EAAE,mBAAmB,EAAE,MAAM,sCAAsC,CAAC;AAC3E,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,MAAM,OAAO,UAAU;IACJ,SAAS,CAAS;IAClB,IAAI,CAAI;IACR,KAAK,CAAsB;IACpC,MAAM,GAAmB,EAAE,CAAC;IAC5B,MAAM,GAAa,EAAE,CAAC;IACtB,KAAK,GAAW,CAAC,CAAC;IAClB,KAAK,GAAW,CAAC,CAAC;IAE1B,YAAmB,SAAiB,EAAE,IAAO,EAAE,MAAc,EAAE,KAA0B;QACvF,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,EAAE,KAAK,IAAI,SAAS;YACpD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,MAAM,GAAG,KAAK,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC;QACvF,IAAI,CAAC,OAAO,EAAE,CAAC;IACjB,CAAC;IAED,IAAW,MAAM;QACf,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,I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C;QAED,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;CACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@noldova/context.collections",
3
- "version": "0.0.13",
3
+ "version": "0.0.14",
4
4
  "type": "module",
5
5
  "main": "api/index.js",
6
6
  "types": "api/index.d.ts",
@@ -19,8 +19,8 @@
19
19
  "registry": "https://registry.npmjs.org/"
20
20
  },
21
21
  "peerDependencies": {
22
- "@noldova/context.core": ">=0.0.13 <0.1.0",
23
- "@noldova/context.exceptions": ">=0.0.13 <0.1.0"
22
+ "@noldova/context.core": ">=0.0.14 <0.1.0",
23
+ "@noldova/context.exceptions": ">=0.0.14 <0.1.0"
24
24
  },
25
- "gitHead": "369ff7e46413dd5278f3623ead81e08137d609dd"
25
+ "gitHead": "d6670c582cf942a35496df9bed0170c854f63d29"
26
26
  }
package/resources.js CHANGED
@@ -6,6 +6,7 @@
6
6
  * LICENSE file in the root directory of this source tree.
7
7
  */
8
8
  export class Resources {
9
+ static chunkSizeParameterName = "chunkSize";
9
10
  static collectionEmpty = "The collection is empty.";
10
11
  static collectionModified = "The collection was changed while it was being enumerated.";
11
12
  static countParameterName = "count";
@@ -13,6 +14,7 @@ export class Resources {
13
14
  static keyExists = "An entry with the same key already exists.";
14
15
  static keyParameterName = "key";
15
16
  static leftParameterName = "left";
17
+ static lengthParameterName = "length";
16
18
  static moreThanOneElement = "The sequence contains more than one element.";
17
19
  static moreThanOneMatch = "The sequence contains more than one matching element.";
18
20
  static noMatch = "The sequence contains no matching element.";
@@ -22,5 +24,7 @@ export class Resources {
22
24
  static sequenceEmpty = "The sequence contains no elements.";
23
25
  static sizeParameterName = "size";
24
26
  static startParameterName = "start";
27
+ static weighParameterName = "weigh";
28
+ static weightInvalid = "An element's weight must be a finite number that isn't negative.";
25
29
  }
26
30
  //# sourceMappingURL=resources.js.map
package/resources.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"resources.js","sourceRoot":"","sources":["../../../src/noldova.context.collections/src/resources.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,OAAO,SAAS;IACb,MAAM,CAAU,eAAe,GAAW,0BAA0B,CAAC;IACrE,MAAM,CAAU,kBAAkB,GAAW,2DAA2D,CAAC;IACzG,MAAM,CAAU,kBAAkB,GAAW,OAAO,CAAC;IACrD,MAAM,CAAU,kBAAkB,GAAW,OAAO,CAAC;IACrD,MAAM,CAAU,SAAS,GAAW,4CAA4C,CAAC;IACjF,MAAM,CAAU,gBAAgB,GAAW,KAAK,CAAC;IACjD,MAAM,CAAU,iBAAiB,GAAW,MAAM,CAAC;IACnD,MAAM,CAAU,kBAAkB,GAAW,8CAA8C,CAAC;IAC5F,MAAM,CAAU,gBAAgB,GAAW,uDAAuD,CAAC;IACnG,MAAM,CAAU,OAAO,GAAW,4CAA4C,CAAC;IAC/E,MAAM,CAAU,aAAa,GAAW,+FAA+F,CAAC;IACxI,MAAM,CAAU,UAAU,GAAW,qDAAqD,CAAC;IAC3F,MAAM,CAAU,YAAY,GAAW,6GAA6G,CAAC;IACrJ,MAAM,CAAU,aAAa,GAAW,oCAAoC,CAAC;IAC7E,MAAM,CAAU,iBAAiB,GAAW,MAAM,CAAC;IACnD,MAAM,CAAU,kBAAkB,GAAW,OAAO,CAAC;CAC7D"}
1
+ {"version":3,"file":"resources.js","sourceRoot":"","sources":["../../../src/noldova.context.collections/src/resources.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,MAAM,OAAO,SAAS;IACb,MAAM,CAAU,sBAAsB,GAAW,WAAW,CAAC;IAC7D,MAAM,CAAU,eAAe,GAAW,0BAA0B,CAAC;IACrE,MAAM,CAAU,kBAAkB,GAAW,2DAA2D,CAAC;IACzG,MAAM,CAAU,kBAAkB,GAAW,OAAO,CAAC;IACrD,MAAM,CAAU,kB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