This repository was archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbuffering.js
More file actions
364 lines (323 loc) · 13 KB
/
buffering.js
File metadata and controls
364 lines (323 loc) · 13 KB
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
var SharedBuffer = root.Internals.SharedBuffer = (function () {
inherits(SharedBuffer, Enumerable);
function SharedBuffer (source) {
this.disposed = false;
this.source = source;
}
SharedBuffer.prototype.getEnumerator = function () {
if (this.disposed) {
throw new Error('Object disposed');
}
var current, self = this;
return enumeratorCreate(
function () {
if (self.source.moveNext()) {
current = self.source.getCurrent();
return true;
}
return false;
},
function () { return current; });
};
SharedBuffer.prototype.dispose = function () {
if (!this.disposed) {
this.disposed = true;
this.source.dispose();
this.source = null;
}
};
return SharedBuffer;
}());
/**
* Shares the source sequence within a selector function where each enumerator can fetch the next element from the source sequence.
*
* var rng = Enumerable.range(0, 10).share();
*
* var e1 = rng.getEnumerator(); // Both e1 and e2 will consume elements from
* var e2 = rng.getEnumerator(); // the source sequence.
*
* ok(e1.moveNext());
* equal(0, e1.getCurrent());
*
* ok(e1.moveNext());
* equal(1, e1.getCurrent());
*
* ok(e2.moveNext()); // e2 "steals" element 2
* equal(2, e2.getCurrent());
*
* ok(e1.moveNext()); // e1 can't see element 2
* equal(3, e1.getCurrent());
*
* @param {Function} [selector] Selector function with shared access to the source sequence for each enumerator.
* @return Sequence resulting from applying the selector function to the shared view over the source sequence.
*/
EnumerablePrototype.share = function (selector) {
var source = this;
return !selector ?
new SharedBuffer(source.getEnumerator()) :
new Enumerable(function () { return selector(source.share()).getEnumerator(); });
};
function RefCountList(readerCount) {
this.readerCount = readerCount;
this.list = {};
this.length = 0;
}
root.Internals.RefCountList = RefCountList;
var RefCountListPrototype = RefCountList.prototype;
RefCountListPrototype.clear = function () {
this.list = {};
this.length = 0;
};
RefCountListPrototype.get = function (i) {
if (!this.list[i]) {
throw new Error('Element no longer available in the buffer.');
}
var res = this.list[i];
if (--res.length === 0) { delete this.list[i]; }
return res.value;
};
RefCountListPrototype.push = function (item) {
this.list[this.length] = { value: item, length: this.readerCount };
this.length++;
};
RefCountListPrototype.done = function (index) {
for (var i = index; i < this.length; i++) {
this.get(i);
}
this.readerCount--;
};
var PublishedBuffer = root.Internals.PublishedBuffer = (function () {
inherits(PublishedBuffer, Enumerable);
function PublishedBuffer(source) {
this.source = source;
this.buffer = new RefCountList(0);
this.disposed = false;
this.stopped = false;
this.error = null;
}
function getEnumerator(i) {
var currentValue, self = this, isDisposed = false, isFirst = true, isDone = false;
return enumeratorCreate(
function () {
if (self.disposed) { throw new Error('Object disposed'); }
if (!isFirst) { i++; }
var hasValue = false, current;
if (i >= self.buffer.length) {
if (!self.stopped) {
try {
hasValue = self.source.moveNext();
if (hasValue) { current = self.source.getCurrent(); }
} catch (e) {
self.stopped = true;
self.error = e;
self.source.dispose();
isDisposed = true;
}
}
if (self.stopped) {
if (self.error) {
self.buffer && self.buffer.done(i + 1);
isDone = true;
throw self.error;
} else {
self.buffer && self.buffer.done(i + 1);
isDone = true;
return false;
}
}
if (hasValue) {
self.buffer.push(current);
}
} else {
hasValue = true;
}
if (hasValue) {
currentValue = self.buffer.get(i);
isFirst = false;
return true;
} else {
self.buffer && self.buffer.done(i + 1);
isDone = true;
return false;
}
},
function () { return currentValue; },
function () { isDone && self.buffer && self.buffer.done(i); }
);
}
PublishedBuffer.prototype.getEnumerator = function () {
if (this.disposed) {
throw new Error('Object disposed');
}
var i = this.buffer.length;
this.buffer.readerCount++;
return getEnumerator.call(this, i);
};
PublishedBuffer.prototype.dispose = function () {
if (!this.disposed) {
this.source.dispose();
this.source = null;
this.buffer.clear();
this.buffer = null;
this.disposed = true;
}
};
return PublishedBuffer;
}());
/**
* Publishes the source sequence within a selector function where each enumerator can obtain a view over a tail of the source sequence.
*
* var rng = Enumerable.Range(0, 10).Publish();
*
* var e1 = rng.getEnumerator(); // e1 has a view on the source starting from element 0
*
* ok(e1.moveNext());
* equal(0, e1.getCurrent());
*
* ok(e1.moveNext());
* equal(1, e1.getCurrent());
*
* var e2 = rng.getEnumerator();
*
* ok(e2.moveNext()); // e2 has a view on the source starting from element 2
* equal(2, e2.getCurrent());
*
* ok(e1.moveNext()); // e1 continues to enumerate over its view
* equal(2, e1.getCurrent());
*
* @param selector Selector function with published access to the source sequence for each enumerator.
* @return Sequence resulting from applying the selector function to the published view over the source sequence.
*/
EnumerablePrototype.publish = function (selector) {
var source = this;
return !selector ?
new PublishedBuffer(source.getEnumerator()) :
new Enumerable(function () { return selector(source.publish()).getEnumerator(); });
};
function MaxRefCountList() {
this.list = [];
this.length = 0;
}
root.Internals.MaxRefCountList = MaxRefCountList;
var MaxRefCountListPrototype = MaxRefCountList.prototype;
MaxRefCountListPrototype.done = noop;
MaxRefCountListPrototype.push = function (item) {
this.list[this.length++] = item;
};
MaxRefCountListPrototype.clear = function () {
this.list = [];
this.length = 0;
};
MaxRefCountListPrototype.get = function (i) {
return this.list[i];
};
var MemoizedBuffer = root.Internals.MemoizedBuffer = (function () {
inherits(MemoizedBuffer, Enumerable);
function MemoizedBuffer(source, buffer) {
this.source = source;
this.buffer = buffer
this.stopped = false;
this.error = null;
this.disposed = false;
}
MemoizedBuffer.prototype.getEnumerator = function () {
if (this.disposed) {
throw new Error('Object disposed');
}
var i = 0, currentValue, self = this, isDisposed = false, isFirst = true, isDone = false;
return enumeratorCreate(
function () {
if (self.disposed) { throw new Error('Object disposed'); }
if (!isFirst) { i++; }
var hasValue = false, current;
if (i >= self.buffer.length) {
if (!self.stopped) {
try {
hasValue = self.source.moveNext();
if (hasValue) { current = self.source.getCurrent(); }
} catch (e) {
self.stopped = true;
self.error = e;
self.source.dispose();
isDisposed = true;
}
}
if (self.stopped) {
if (self.error) {
self.buffer && self.buffer.done(i + 1);
isDone = true;
throw self.error;
} else {
self.buffer && self.buffer.done(i + 1);
isDone = true;
return false;
}
}
if (hasValue) {
self.buffer.push(current);
}
} else {
hasValue = true;
}
if (hasValue) {
currentValue = self.buffer.get(i);
isFirst = false;
return true;
} else {
self.buffer && self.buffer.done(i + 1);
isDone = true;
return false;
}
},
function () { return currentValue; },
function () { isDone && self.buffer && self.buffer.done(i); }
);
};
MemoizedBuffer.prototype.dispose = function () {
if (!this.disposed) {
this.source.dispose();
this.source = null;
this.buffer.clear();
this.buffer = null;
this.disposed = true;
}
};
return MemoizedBuffer;
}());
/**
* Memoizes the source sequence within a selector function where a specified number of enumerators can get access to all of the sequence's elements without causing multiple enumerations over the source.
*
* var rng = Enumerable.range(0, 10).doAction(function (x) { console.log(x); }).memoize();
*
* var e1 = rng.getEnumerator();
*
* ok(e1.moveNext()); // Prints 0
* equal(0, e1.getCurrent());
*
* ok(e1.moveNext()); // Prints 1
* equal(1, e1.getCurrent());
*
* var e2 = rng.getEnumerator();
*
* ok(e2.moveNext()); // Doesn't print anything; the side-effect of Do
* equal(0, e2.getCurrent()); // has already taken place during e1's iteration.
*
* ok(e1.moveNext()); // Prints 2
* equal(2, e1.getCurrent());
*
* @param readerCount Number of enumerators that can access the underlying buffer. Once every enumerator has obtained an element from the buffer, the element is removed from the buffer.
* @param selector Selector function with memoized access to the source sequence for a specified number of enumerators.
* @return Sequence resulting from applying the selector function to the memoized view over the source sequence.
*/
EnumerablePrototype.memoize = function () {
var source = this;
if (arguments.length === 0) {
return new MemoizedBuffer(source.getEnumerator(), new MaxRefCountList());
} else if (arguments.length === 1 && typeof arguments[0] === 'function') {
return new Enumerable(function () { return arguments[1](source.memoize()).getEnumerator(); });
} else if (arguments.length === 1 && typeof arguments[0] === 'number') {
return new MemoizedBuffer(source.getEnumerator(), new RefCountList(arguments[0]));
} else {
return new Enumerable(function () { return arguments[1](source.memoize(arguments[0])).getEnumerator(); });
}
};