-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathArrayList.js
592 lines (537 loc) · 11 KB
/
ArrayList.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
var _ = require('underscore');
/**
* Creates a clone of Array
*
* @returns {Array}
* @constructor
*/
function ArrayList() {
var list = [];
list.__proto__ = ArrayList.prototype;
return list;
}
ArrayList.prototype = [];
/**
* ArrayList
*
* Create a subclass of Array.prototype
*/
_.extend(ArrayList.prototype, {
/**
* Returns an element by position
*
* @param index
* @returns {*}
*/
get: function (index) {
return this[index];
},
/**
* Set an element by position
*
* @param index
* @param value
* @returns {ArrayList}
*/
set: function (index, value) {
this[index] = value;
return this;
},
/**
* Check if an element is in the list
*
* @param element
* @returns {boolean}
*/
contains: function (element) {
for (var i = 0, len = this.length; i < len; i++) {
if (element === this[i]) {
return true;
}
}
return false;
},
/**
* Add elements to the list
*
* @param elements
* @returns {ArrayList}
*/
add: function (elements) {
if (!_.isArray(elements)) {
elements = [ elements ];
}
elements.forEach(function (e) {
this.push(e);
}.bind(this));
return this;
},
/**
* Check if the list contains no elements
*
* @returns {boolean}
*/
isEmpty: function () {
return !this.length;
},
/**
* Check if two lists are equals
*
* @param list
* @returns {boolean}
*/
equals: function (list) {
if (this.length !== list.length) {
return false;
}
for (var i = 0; i < this.length; i++) {
if (this[i] !== list[i]) {
return false;
}
}
return true;
},
clone: function () {
var list = new ArrayList();
return list.add(this);
},
/**
* Removes an element by position
*
* @param index
* @returns {boolean}
*/
remove: function (index) {
if (~index) {
this.splice(index, 1);
return true;
}
return false;
},
/**
* Removes a specified element
*
* @param element
* @returns {boolean}
*/
removeElement: function (element) {
return this.remove(this.indexOf(element));
},
/**
* Remove all the elements in another collection
*
* @param list
* @returns {boolean}
*/
removeAll: function (list) {
var result = false;
list.forEach(function (e) {
result |= this.removeElement(e);
}.bind(this));
return result;
},
/**
* Replace all the elements of the list
*
* @param list
*/
replaceAll: function (list) {
this.clear();
this.add(list);
},
/**
* Remove all the elements in the list
*/
clear: function () {
this.length = 0;
},
/**
* Return a new array with all the elements in the same order
*
* @returns {Array}
*/
toArray: function () {
var arr = [];
this.forEach(function (e) {
arr.push(e);
});
return arr;
},
/**
* Returns the first element of the list
*
* @param [n]
* @returns {*}
*/
first: function (n) {
return _.first(this, n);
},
/**
* Returns everything but the last entry of the list
*
* @param [n]
* @returns {ArrayList}
*/
initial: function (n) {
var list = new ArrayList;
return list.add(_.initial(this, n));
},
/**
* Returns the last element of the list
*
* @param [n]
* @returns {*}
*/
last: function (n) {
return _.last(this, n);
},
/**
* Returns the rest of the elements in the list
*
* @param index
* @returns {*}
*/
rest: function (index) {
var list = new ArrayList;
return list.add(_.rest(this, index));
},
/**
* Returns a new list with the false values removed
*
* @returns {ArrayList}
*/
compact: function () {
var list = new ArrayList;
return list.add(_.compact(this));
},
/**
* Returns a new list flattened
*
* @returns {ArrayList}
*/
flatten: function () {
var list = new ArrayList;
return list.add(_.flatten(this));
},
/**
* Returns a new list without the specified values
*
* @returns {ArrayList}
*/
without: function () {
var list = new ArrayList;
return list.add(_.without.apply(this, thisToArgs(this, arguments)));
},
/**
* Split the list into two lists
*
* @param predicate
* @returns {ArrayList}
*/
partition: function (predicate) {
var lists = new ArrayList;
var arrays = _.partition(this, predicate);
arrays.forEach(function (arr) {
var list = new ArrayList;
lists.push(list.add(arr));
});
return lists;
},
/**
* Returns the list joined with the arrays specified, the join is unique
*
* @returns {ArrayList}
*/
union: function () {
var list = new ArrayList;
return list.add(_.union.apply(this, thisToArgs(this, arguments)));
},
/**
* Returns the list intercepted with the arrays specified, the intersection is unique
*
* @returns {ArrayList}
*/
intersection: function () {
var list = new ArrayList;
return list.add(_.intersection.apply(this, thisToArgs(this, arguments)));
},
/**
* Returns the list minus the arrays specified, the difference is unique
*
* @returns {ArrayList}
*/
difference: function () {
var list = new ArrayList;
return list.add(_.difference.apply(this, thisToArgs(this, arguments)));
},
/**
* Returns a list with the duplicated values removed
*
* @param {boolean} isSorted
* @param iterator
* @returns {ArrayList}
*/
unique: function (isSorted, iterator) {
var list = new ArrayList;
return list.add(_.uniq(this, isSorted, iterator));
},
/**
* Alias for unique
*
* @param {boolean} isSorted
* @param iterator
* @returns {ArrayList}
*/
uniq: function (isSorted, iterator) {
return this.unique(isSorted, iterator);
},
/**
*
* @returns {ArrayList}
*/
zip: function () {
var list = new ArrayList;
return list.add(_.zip.apply(this, thisToArgs(this, arguments)));
},
/**
*
* @param values
* @returns {ArrayList}
*/
object: function (values) {
var list = new ArrayList;
return list.add(_.object(this, values));
},
/**
* Returns the index at which the value should be inserted into the list
*
* @param value
* @param iterator
* @param context
* @returns {Number}
*/
sortedIndex: function (value, iterator, context) {
return _.sortedIndex(this, value, iterator, context);
},
/**
* alias for forEach
*
* @returns {forEach}
*/
each: function () {
return this.forEach.apply(this, arguments);
},
/**
* Returns a new list with each value mapped through a transformation
*
* @param iterator
* @param context
* @returns {ArrayList}
*/
map: function (iterator, context) {
var list = new ArrayList;
return list.add(_.map(this, iterator, context));
},
/**
*
* @param iterator
* @param memo
* @param context
* @returns {*}
*/
reduce: function (iterator, memo, context) {
return _.reduce(this, iterator, memo, context);
},
/**
*
* @param iterator
* @param memo
* @param context
* @returns {*}
*/
reduceRight: function (iterator, memo, context) {
return _.reduceRight(this, iterator, memo, context);
},
/**
* Returns a new list with the occurrences that passes the test
*
* @param predicate
* @param context
* @returns {ArrayList}
*/
find: function (predicate, context) {
var list = new ArrayList;
return list.add(_.filter(this, predicate, context));
},
/**
* Returns the first occurrence that passes the test
*
* @param predicate
* @param context
* @returns {*}
*/
findOne: function (predicate, context) {
return _.find(this, predicate, context);
},
/**
*
* @param properties
* @returns {*}
*/
where: function (properties) {
return _.where(this, properties);
},
/**
*
* @param properties
* @returns {*}
*/
findWhere: function (properties) {
return _.findWhere(this, properties);
},
/**
*
* @param predicate
* @param context
* @returns {ArrayList}
*/
reject: function (predicate, context) {
var list = new ArrayList;
return list.add(_.reject(this, predicate, context));
},
/**
* Returns true if all of the values in the list pass the predicate truth test
*
* @param predicate
* @param context
* @returns {Boolean}
*/
every: function (predicate, context) {
return _.every(this, predicate, context);
},
/**
* Returns true if any of the values in the list pass the predicate truth test
*
* @param predicate
* @param context
* @returns {Boolean}
*/
some: function (predicate, context) {
return _.some(this, predicate, context);
},
/**
*
* @param methodName
* @returns {ArrayList}
*/
invoke: function (methodName) {
var list = new ArrayList;
return list.add(_.invoke.apply(this, thisToArgs(this, arguments)));
},
/**
*
* @param propertyName
* @returns {*}
*/
pluck: function (propertyName) {
return _.pluck(this, propertyName);
},
/**
* Returns the maximum value in list
*
* @param iterator
* @param context
* @returns {*}
*/
max: function (iterator, context) {
return _.max(this, iterator, context);
},
/**
* Returns the minimum value in list
*
* @param iterator
* @param context
* @returns {*}
*/
min: function (iterator, context) {
return _.min(this, iterator, context);
},
/**
* Returns a new list with the values sorted
*
* @param iterator
* @param context
* @returns {ArrayList}
*/
sortBy: function (iterator, context) {
var list = new ArrayList;
return list.add(_.sortBy(this, iterator, context));
},
/**
*
* @param iterator
* @param context
* @returns {*}
*/
groupBy: function (iterator, context) {
return _.groupBy(this, iterator, context);
},
/**
*
* @param iterator
* @param context
* @returns {*}
*/
indexBy: function (iterator, context) {
return _.indexBy(this, iterator, context);
},
/**
*
* @param iterator
* @param context
* @returns {*}
*/
countBy: function (iterator, context) {
return _.countBy(this, iterator, context);
},
/**
* Returns a shuffled copy of the list, using a version of the Fisher-Yates shuffle
*
* @link http://en.wikipedia.org/wiki/Fisher–Yates_shuffle
* @returns {ArrayList}
*/
shuffle: function () {
var list = new ArrayList;
return list.add(_.shuffle(this));
},
/**
* Returns a random sample from the list.
*
* @param n
* @returns {*}
*/
sample: function (n) {
return _.sample(this, n);
},
/**
* Returns the length of the list
*
* @returns {Number}
*/
size: function () {
return this.length;
}
});
/**
* Helper to use Function.prototype.call and prepend this to arguments
*
* @param that
* @param args
* @returns {Array}
*/
function thisToArgs(that, args) {
args = Array.prototype.slice.call(args, 0);
args.unshift(that);
return args;
}
module.exports = ArrayList;