-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathLInQer.extra.js
369 lines (369 loc) · 13.8 KB
/
LInQer.extra.js
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
"use strict";
/// <reference path="./LInQer.Slim.ts" />
/// <reference path="./LInQer.Enumerable.ts" />
/// <reference path="./LInQer.OrderedEnumerable.ts" />
var Linqer;
/// <reference path="./LInQer.Slim.ts" />
/// <reference path="./LInQer.Enumerable.ts" />
/// <reference path="./LInQer.OrderedEnumerable.ts" />
(function (Linqer) {
/// randomizes the enumerable (partial Fisher-Yates)
Linqer.Enumerable.prototype.shuffle = function () {
const self = this;
function* gen() {
const arr = self.toArray();
const len = arr.length;
let n = 0;
while (n < len) {
let k = n + Math.floor(Math.random() * (len - n));
const value = arr[k];
arr[k] = arr[n];
arr[n] = value;
n++;
yield value;
}
}
const result = Linqer.Enumerable.from(gen);
result._count = () => self.count();
return result;
};
/// implements random reservoir sampling of k items, with the option to specify a maximum limit for the items
Linqer.Enumerable.prototype.randomSample = function (k, limit = Number.MAX_SAFE_INTEGER) {
let index = 0;
const sample = [];
Linqer._ensureInternalTryGetAt(this);
if (this._canSeek) { // L algorithm
const length = this.count();
let index = 0;
for (index = 0; index < k && index < limit && index < length; index++) {
sample.push(this.elementAt(index));
}
let W = Math.exp(Math.log(Math.random()) / k);
while (index < length && index < limit) {
index += Math.floor(Math.log(Math.random()) / Math.log(1 - W)) + 1;
if (index < length && index < limit) {
sample[Math.floor(Math.random() * k)] = this.elementAt(index);
W *= Math.exp(Math.log(Math.random()) / k);
}
}
}
else { // R algorithm
for (const item of this) {
if (index < k) {
sample.push(item);
}
else {
const j = Math.floor(Math.random() * index);
if (j < k) {
sample[j] = item;
}
}
index++;
if (index >= limit)
break;
}
}
return Linqer.Enumerable.from(sample);
};
/// returns the distinct values based on a hashing function
Linqer.Enumerable.prototype.distinctByHash = function (hashFunc) {
// this is much more performant than distinct with a custom comparer
const self = this;
const gen = function* () {
const distinctValues = new Set();
for (const item of self) {
const size = distinctValues.size;
distinctValues.add(hashFunc(item));
if (size < distinctValues.size) {
yield item;
}
}
};
return new Linqer.Enumerable(gen);
};
/// returns the values that have different hashes from the items of the iterable provided
Linqer.Enumerable.prototype.exceptByHash = function (iterable, hashFunc) {
// this is much more performant than except with a custom comparer
Linqer._ensureIterable(iterable);
const self = this;
const gen = function* () {
const distinctValues = Linqer.Enumerable.from(iterable).select(hashFunc).toSet();
for (const item of self) {
if (!distinctValues.has(hashFunc(item))) {
yield item;
}
}
};
return new Linqer.Enumerable(gen);
};
/// returns the values that have the same hashes as items of the iterable provided
Linqer.Enumerable.prototype.intersectByHash = function (iterable, hashFunc) {
// this is much more performant than intersect with a custom comparer
Linqer._ensureIterable(iterable);
const self = this;
const gen = function* () {
const distinctValues = Linqer.Enumerable.from(iterable).select(hashFunc).toSet();
for (const item of self) {
if (distinctValues.has(hashFunc(item))) {
yield item;
}
}
};
return new Linqer.Enumerable(gen);
};
/// returns the index of a value in an ordered enumerable or false if not found
/// WARNING: use the same comparer as the one used in the ordered enumerable. The algorithm assumes the enumerable is already sorted.
Linqer.Enumerable.prototype.binarySearch = function (value, comparer = Linqer._defaultComparer) {
let enumerable = this.toList();
let start = 0;
let end = enumerable.count() - 1;
while (start <= end) {
const mid = (start + end) >> 1;
const comp = comparer(enumerable.elementAt(mid), value);
if (comp == 0)
return mid;
if (comp < 0) {
start = mid + 1;
}
else {
end = mid - 1;
}
}
return false;
};
/// joins each item of the enumerable with previous items from the same enumerable
Linqer.Enumerable.prototype.lag = function (offset, zipper) {
if (!offset) {
throw new Error('offset has to be positive');
}
if (offset < 0) {
throw new Error('offset has to be positive. Use .lead if you want to join with next items');
}
if (!zipper) {
zipper = (i1, i2) => [i1, i2];
}
else {
Linqer._ensureFunction(zipper);
}
const self = this;
Linqer._ensureInternalTryGetAt(this);
// generator uses a buffer to hold all the items within the offset interval
const gen = function* () {
const buffer = Array(offset);
let index = 0;
for (const item of self) {
const index2 = index - offset;
const item2 = index2 < 0
? undefined
: buffer[index2 % offset];
yield zipper(item, item2);
buffer[index % offset] = item;
index++;
}
};
const result = new Linqer.Enumerable(gen);
// count is the same as of the original enumerable
result._count = () => {
const count = self.count();
if (!result._wasIterated)
result._wasIterated = self._wasIterated;
return count;
};
// seeking is possible only if the original was seekable
if (self._canSeek) {
result._canSeek = true;
result._tryGetAt = (index) => {
const val1 = self._tryGetAt(index);
const val2 = self._tryGetAt(index - offset);
if (val1) {
return {
value: zipper(val1.value, val2 ? val2.value : undefined)
};
}
return null;
};
}
return result;
};
/// joins each item of the enumerable with next items from the same enumerable
Linqer.Enumerable.prototype.lead = function (offset, zipper) {
if (!offset) {
throw new Error('offset has to be positive');
}
if (offset < 0) {
throw new Error('offset has to be positive. Use .lag if you want to join with previous items');
}
if (!zipper) {
zipper = (i1, i2) => [i1, i2];
}
else {
Linqer._ensureFunction(zipper);
}
const self = this;
Linqer._ensureInternalTryGetAt(this);
// generator uses a buffer to hold all the items within the offset interval
const gen = function* () {
const buffer = Array(offset);
let index = 0;
for (const item of self) {
const index2 = index - offset;
if (index2 >= 0) {
const item2 = buffer[index2 % offset];
yield zipper(item2, item);
}
buffer[index % offset] = item;
index++;
}
for (let i = 0; i < offset; i++) {
const item = buffer[(index + i) % offset];
yield zipper(item, undefined);
}
};
const result = new Linqer.Enumerable(gen);
// count is the same as of the original enumerable
result._count = () => {
const count = self.count();
if (!result._wasIterated)
result._wasIterated = self._wasIterated;
return count;
};
// seeking is possible only if the original was seekable
if (self._canSeek) {
result._canSeek = true;
result._tryGetAt = (index) => {
const val1 = self._tryGetAt(index);
const val2 = self._tryGetAt(index + offset);
if (val1) {
return {
value: zipper(val1.value, val2 ? val2.value : undefined)
};
}
return null;
};
}
return result;
};
/// returns an enumerable of at least minLength, padding the end with a value or the result of a function
Linqer.Enumerable.prototype.padEnd = function (minLength, filler) {
if (minLength <= 0) {
throw new Error('minLength has to be positive.');
}
let fillerFunc;
if (typeof filler !== 'function') {
fillerFunc = (index) => filler;
}
else {
fillerFunc = filler;
}
const self = this;
Linqer._ensureInternalTryGetAt(this);
// generator iterates all elements,
// then yields the result of the filler function until minLength items
const gen = function* () {
let index = 0;
for (const item of self) {
yield item;
index++;
}
for (; index < minLength; index++) {
yield fillerFunc(index);
}
};
const result = new Linqer.Enumerable(gen);
// count is the maximum between minLength and the original count
result._count = () => {
const count = Math.max(minLength, self.count());
if (!result._wasIterated)
result._wasIterated = self._wasIterated;
return count;
};
// seeking is possible if the original was seekable
if (self._canSeek) {
result._canSeek = true;
result._tryGetAt = (index) => {
const val = self._tryGetAt(index);
if (val)
return val;
if (index < minLength) {
return { value: fillerFunc(index) };
}
return null;
};
}
return result;
};
/// returns an enumerable of at least minLength, padding the start with a value or the result of a function
/// if the enumerable cannot seek, then it will be iterated minLength time
Linqer.Enumerable.prototype.padStart = function (minLength, filler) {
if (minLength <= 0) {
throw new Error('minLength has to be positive.');
}
let fillerFunc;
if (typeof filler !== 'function') {
fillerFunc = (index) => filler;
}
else {
fillerFunc = filler;
}
const self = this;
Linqer._ensureInternalTryGetAt(self);
// generator needs a buffer to hold offset values
// it yields values from the buffer when it overflows
// or filler function results if the buffer is not full
// after iterating the entire original enumerable
const gen = function* () {
const buffer = Array(minLength);
let index = 0;
const iterator = self[Symbol.iterator]();
let flushed = false;
let done = false;
do {
const val = iterator.next();
done = !!val.done;
if (!done) {
buffer[index] = val.value;
index++;
}
if (flushed && !done) {
yield val.value;
}
else {
if (done || index === minLength) {
for (let i = 0; i < minLength - index; i++) {
yield fillerFunc(i);
}
for (let i = 0; i < index; i++) {
yield buffer[i];
}
flushed = true;
}
}
} while (!done);
};
const result = new Linqer.Enumerable(gen);
// count is the max of minLength and the original count
result._count = () => {
const count = Math.max(minLength, self.count());
if (!result._wasIterated)
result._wasIterated = self._wasIterated;
return count;
};
// seeking is possible only if the original was seekable
if (self._canSeek) {
result._canSeek = true;
result._tryGetAt = (index) => {
const count = self.count();
const delta = minLength - count;
if (delta <= 0) {
return self._tryGetAt(index);
}
if (index < delta) {
return { value: fillerFunc(index) };
}
return self._tryGetAt(index - delta);
};
}
return result;
};
})(Linqer || (Linqer = {}));
//# sourceMappingURL=LInQer.extra.js.map