-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathshim-array.js
360 lines (320 loc) · 9.34 KB
/
shim-array.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
"use strict";
/*
Based in part on extras from Motorola Mobility’s Montage
Copyright (c) 2012, Motorola Mobility LLC. All Rights Reserved.
3-Clause BSD License
https://github.com/motorola-mobility/montage/blob/master/LICENSE.md
*/
var Function = require("./shim-function");
var GenericCollection = require("./generic-collection");
var GenericOrder = require("./generic-order");
var WeakMap = require("weak-map");
module.exports = Array;
var array_splice = Array.prototype.splice;
var array_slice = Array.prototype.slice;
Array.empty = [];
if (Object.freeze) {
Object.freeze(Array.empty);
}
Array.from = function (values) {
var array = [];
array.addEach(values);
return array;
};
Array.unzip = function (table) {
var transpose = [];
var length = Infinity;
// compute shortest row
for (var i = 0; i < table.length; i++) {
var row = table[i];
table[i] = row.toArray();
if (row.length < length) {
length = row.length;
}
}
for (var i = 0; i < table.length; i++) {
var row = table[i];
for (var j = 0; j < row.length; j++) {
if (j < length && j in row) {
transpose[j] = transpose[j] || [];
transpose[j][i] = row[j];
}
}
}
return transpose;
};
function define(key, value) {
Object.defineProperty(Array.prototype, key, {
value: value,
writable: true,
configurable: true,
enumerable: false
});
}
define("addEach", GenericCollection.prototype.addEach);
define("deleteEach", GenericCollection.prototype.deleteEach);
define("toArray", GenericCollection.prototype.toArray);
define("toObject", GenericCollection.prototype.toObject);
define("all", GenericCollection.prototype.all);
define("any", GenericCollection.prototype.any);
define("min", GenericCollection.prototype.min);
define("max", GenericCollection.prototype.max);
define("sum", GenericCollection.prototype.sum);
define("average", GenericCollection.prototype.average);
define("only", GenericCollection.prototype.only);
define("flatten", GenericCollection.prototype.flatten);
define("zip", GenericCollection.prototype.zip);
define("enumerate", GenericCollection.prototype.enumerate);
define("group", GenericCollection.prototype.group);
define("sorted", GenericCollection.prototype.sorted);
define("reversed", GenericCollection.prototype.reversed);
define("constructClone", function (values) {
var clone = new this.constructor();
clone.addEach(values);
return clone;
});
define("has", function (value, equals) {
return this.find(value, equals) !== -1;
});
define("get", function (index, defaultValue) {
if (+index !== index)
throw new Error("Indicies must be numbers");
if (!index in this) {
return defaultValue;
} else {
return this[index];
}
});
define("set", function (index, value) {
this[index] = value;
return true;
});
define("add", function (value) {
this.push(value);
return true;
});
define("delete", function (value, equals) {
var index = this.find(value, equals);
if (index !== -1) {
this.splice(index, 1);
return true;
}
return false;
});
define("deleteAll", function (value, equals) {
equals = equals || this.contentEquals || Object.equals;
var count = 0;
for (var index = 0; index < this.length;) {
if (equals(value, this[index])) {
this.swap(index, 1);
count++;
} else {
index++;
}
}
return count;
});
define("find", function (value, equals) {
equals = equals || this.contentEquals || Object.equals;
for (var index = 0; index < this.length; index++) {
if (index in this && equals(value, this[index])) {
return index;
}
}
return -1;
});
define("findLast", function (value, equals) {
equals = equals || this.contentEquals || Object.equals;
var index = this.length;
do {
index--;
if (index in this && equals(this[index], value)) {
return index;
}
} while (index > 0);
return -1;
});
define("swap", function (start, length, plus) {
var args, plusLength, i, j, returnValue;
if (start > this.length) {
this.length = start;
}
if (typeof plus !== "undefined") {
args = [start, length];
if (!Array.isArray(plus)) {
plus = array_slice.call(plus);
}
i = 0;
plusLength = plus.length;
// 1000 is a magic number, presumed to be smaller than the remaining
// stack length. For swaps this small, we take the fast path and just
// use the underlying Array splice. We could measure the exact size of
// the remaining stack using a try/catch around an unbounded recursive
// function, but this would defeat the purpose of short-circuiting in
// the common case.
if (plusLength < 1000) {
for (i; i < plusLength; i++) {
args[i+2] = plus[i];
}
return array_splice.apply(this, args);
} else {
// Avoid maximum call stack error.
// First delete the desired entries.
returnValue = array_splice.apply(this, args);
// Second batch in 1000s.
for (i; i < plusLength;) {
args = [start+i, 0];
for (j = 2; j < 1002 && i < plusLength; j++, i++) {
args[j] = plus[i];
}
array_splice.apply(this, args);
}
return returnValue;
}
// using call rather than apply to cut down on transient objects
} else if (typeof length !== "undefined") {
return array_splice.call(this, start, length);
} else if (typeof start !== "undefined") {
return array_splice.call(this, start);
} else {
return [];
}
});
define("peek", function () {
return this[0];
});
define("poke", function (value) {
if (this.length > 0) {
this[0] = value;
}
});
define("peekBack", function () {
if (this.length > 0) {
return this[this.length - 1];
}
});
define("pokeBack", function (value) {
if (this.length > 0) {
this[this.length - 1] = value;
}
});
define("one", function () {
for (var i in this) {
if (Object.owns(this, i)) {
return this[i];
}
}
});
if (!Array.prototype.clear) {
define("clear", function () {
this.length = 0;
return this;
});
}
define("compare", function (that, compare) {
compare = compare || Object.compare;
var i;
var length;
var lhs;
var rhs;
var relative;
if (this === that) {
return 0;
}
if (!that || !Array.isArray(that)) {
return GenericOrder.prototype.compare.call(this, that, compare);
}
length = Math.min(this.length, that.length);
for (i = 0; i < length; i++) {
if (i in this) {
if (!(i in that)) {
return -1;
} else {
lhs = this[i];
rhs = that[i];
relative = compare(lhs, rhs);
if (relative) {
return relative;
}
}
} else if (i in that) {
return 1;
}
}
return this.length - that.length;
});
define("equals", function (that, equals) {
equals = equals || Object.equals;
var i = 0;
var length = this.length;
var left;
var right;
if (this === that) {
return true;
}
if (!that || !Array.isArray(that)) {
return GenericOrder.prototype.equals.call(this, that);
}
if (length !== that.length) {
return false;
} else {
for (; i < length; ++i) {
if (i in this) {
if (!(i in that)) {
return false;
}
left = this[i];
right = that[i];
if (!equals(left, right)) {
return false;
}
} else {
if (i in that) {
return false;
}
}
}
}
return true;
});
define("clone", function (depth, memo) {
if (depth == null) {
depth = Infinity;
} else if (depth === 0) {
return this;
}
memo = memo || new WeakMap();
if (memo.has(this)) {
return memo.get(this);
}
var clone = new Array(this.length);
memo.set(this, clone);
for (var i in this) {
clone[i] = Object.clone(this[i], depth - 1, memo);
};
return clone;
});
define("iterate", function (start, end) {
return new ArrayIterator(this, start, end);
});
define("Iterator", ArrayIterator);
function ArrayIterator(array, start, end) {
this.array = array;
this.start = start == null ? 0 : start;
this.end = end;
};
ArrayIterator.prototype.__iterationObject = null;
Object.defineProperty(ArrayIterator.prototype,"_iterationObject", {
get: function() {
return this.__iterationObject || (this.__iterationObject = { done: false, value:null});
}
});
ArrayIterator.prototype.next = function () {
if (this.start === (this.end == null ? this.array.length : this.end)) {
this._iterationObject.done = true;
this._iterationObject.value = void 0;
} else {
this._iterationObject.value = this.array[this.start++];
}
return this._iterationObject;
};