forked from EddieCornelious/js_data-collections
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollections.js
3697 lines (3028 loc) · 102 KB
/
collections.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define("Collections", [], factory);
else if(typeof exports === 'object')
exports["Collections"] = factory();
else
root["Collections"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ // Flag the module as loaded
/******/ module.loaded = true;
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
exports.__esModule = true;
exports.MultiMap = exports.RBTree = exports.Set = exports.ArrayUtils = exports.Trie = exports.Graph = exports.BST = exports.HashSet = exports.HashMap = exports.PriorityQueue = exports.BHeap = exports.Queue = exports.Stack = exports.List = undefined;
var _List = __webpack_require__(1);
var _List2 = _interopRequireDefault(_List);
var _Stack = __webpack_require__(3);
var _Stack2 = _interopRequireDefault(_Stack);
var _Queue = __webpack_require__(4);
var _Queue2 = _interopRequireDefault(_Queue);
var _BHeap = __webpack_require__(5);
var _BHeap2 = _interopRequireDefault(_BHeap);
var _PriorityQueue = __webpack_require__(6);
var _PriorityQueue2 = _interopRequireDefault(_PriorityQueue);
var _HashMap = __webpack_require__(7);
var _HashMap2 = _interopRequireDefault(_HashMap);
var _HashSet = __webpack_require__(9);
var _HashSet2 = _interopRequireDefault(_HashSet);
var _BST = __webpack_require__(11);
var _BST2 = _interopRequireDefault(_BST);
var _Graph = __webpack_require__(14);
var _Graph2 = _interopRequireDefault(_Graph);
var _Trie = __webpack_require__(16);
var _Trie2 = _interopRequireDefault(_Trie);
var _ArrayUtils = __webpack_require__(17);
var _ArrayUtils2 = _interopRequireDefault(_ArrayUtils);
var _RedBlackTree = __webpack_require__(15);
var _RedBlackTree2 = _interopRequireDefault(_RedBlackTree);
var _Set = __webpack_require__(18);
var _Set2 = _interopRequireDefault(_Set);
var _MultiMap = __webpack_require__(19);
var _MultiMap2 = _interopRequireDefault(_MultiMap);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
exports.List = _List2['default'];
exports.Stack = _Stack2['default'];
exports.Queue = _Queue2['default'];
exports.BHeap = _BHeap2['default'];
exports.PriorityQueue = _PriorityQueue2['default'];
exports.HashMap = _HashMap2['default'];
exports.HashSet = _HashSet2['default'];
exports.BST = _BST2['default'];
exports.Graph = _Graph2['default'];
exports.Trie = _Trie2['default'];
exports.ArrayUtils = _ArrayUtils2['default'];
exports.Set = _Set2['default'];
exports.RBTree = _RedBlackTree2['default'];
exports.MultiMap = _MultiMap2['default'];
/***/ },
/* 1 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
exports.__esModule = true;
var _Util = __webpack_require__(2);
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Returns the node at given index in linked list
* @private
* @param {number} index - The index of the node to return
* @throws {TypeError} When @param index is not a number
* @returns {(Node|undefined)} Node @param index or undefined if not found
*/
function getNode(index) {
var head = this.head;
if (index < 0 || !head) {
return;
}
var i = 0;
while (i < index) {
head = head.next;
i += 1;
// index wanted is > than list size
if (!head) {
return;
}
}
return head;
}
/**
* Linked List Node
* @private
* @class
* @param {*} The data to assign to the node
*/
var Node = function Node(data) {
_classCallCheck(this, Node);
this.data = data;
this.next = null;
// previous node
this.prev = null;
};
/**
* Linked List representation
* @class
*
* @example
* const list = new Collections.LinkedList();
* // FOR ALL EXAMPLES BELOW. ASSUME list IS CLEARED BEFORE EACH EXAMPLE
*/
var List = function () {
function List() {
_classCallCheck(this, List);
this.head = null;
this.tail = null;
this.length = 0;
}
/**
* Adds the given data to left-most end of linked list
* @param {*} data - The data to insert
* @returns {List} The instance this method was called
*
* @example
* list.addToFront("a")
* .addToFront("b"); // list is <"b", "a">
*/
List.prototype.addToFront = function addToFront(data) {
var head = this.head,
length = this.length;
var newNode = new Node(data);
if (!head) {
this.head = newNode;
this.tail = this.head;
} else {
// non-empty list
this.head = newNode;
newNode.next = head;
head.prev = newNode;
}
this.length = length + 1;
return this;
};
/**
* Returns the data at given index
* @param {number} index - The index to look at
* @throws {TypeError} Will throw error if @param index is not number
* @returns {(*|undefined)} Index of element if @param index is in range
* or undefined
*
* @example
* list.addToFront("a")
* .addToFront("b")
* .addToFront("c");
* const getSomething = list.elementAtIndex(2); // "a"
* list.elementAtIndex(13); // undefined
*/
List.prototype.elementAtIndex = function elementAtIndex() {
var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var foundNode = getNode.call(this, index);
return foundNode ? foundNode.data : undefined;
};
/**
* Adds the given data to right-most end of linked list
* @param {*} data - the data to insert
* @returns {List} The instance this method was called
*
* @example
* list.addToBack("a")
* .addToBack("b"); // list is <"a", "b">
*/
List.prototype.addToBack = function addToBack(data) {
var tail = this.tail,
length = this.length;
var newNode = new Node(data);
if (!tail) {
this.head = newNode;
this.tail = this.head;
} else {
this.tail = newNode;
newNode.prev = tail;
tail.next = newNode;
}
this.length = length + 1;
return this;
};
/**
* Removes the left-most element in the linked list
* @returns {(*|undefined)} The removed data or undefined if nothing removed
*
* @example
* list.addToBack("a")
* .addToBack("b");
* const removedData = list.removeFront(); // "a"
* // list is now <"b">
*/
List.prototype.removeFront = function removeFront() {
var head = this.head,
length = this.length;
var removedData = void 0;
if (head) {
removedData = head.data;
this.length = length - 1;
this.head = head.next;
// current state after removal
var newHead = this.head;
// list is now empty...adjust tail
if (!newHead) {
this.tail = null;
this.head = this.tail;
} else {
// front of list rule
newHead.prev = null;
}
}
return removedData;
};
/**
* Removes the right-most element in the linked list
* @returns {(*|undefined)} The removed data or undefined if nothing removed
*
* @example
* list.addToBack("a")
* .addToBack("b");
* const removedData = list.removeBack(); // "b"
* // list is now <"a">
*/
List.prototype.removeBack = function removeBack() {
var tail = this.tail,
length = this.length;
var removedData = void 0;
if (tail) {
removedData = tail.data;
var prev = tail.prev;
this.length = length - 1;
// list now empty
if (!prev) {
this.tail = null;
this.head = this.tail;
} else {
prev.next = null;
this.tail = prev;
}
}
return removedData;
};
/**
* Inserts given data into specific position in the linked list
* @param {index} index - The index to insert data into
* @param {*} data - The data to insert into @param index
* @throws {TypeError} Will throw error if @param index is not number
* @returns {List} - The instance this method was called
*
* @example
* list.addToBack("a")
* .addToBack("b");
* list.insert(1, "$");
* // list is now <"a, "$, "b">
*/
List.prototype.insert = function insert() {
var index = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
var data = arguments[1];
var length = this.length;
if (index === 0) {
return this.addToFront(data);
} else if (index >= length) {
return this.addToBack(data);
}
// parent of wanted node
var parentNode = getNode.call(this, index - 1);
if (parentNode) {
var newNode = new Node(data);
var oldParentNext = parentNode.next;
newNode.next = oldParentNext;
oldParentNext.prev = newNode;
parentNode.next = newNode;
newNode.prev = parentNode;
this.length = length + 1;
}
return this;
};
/**
* Removes data at specific position in the linked list
* @param {index} index - The index to insert data into
* @throws {TypeError} Will throw error if @param index is not number
* @returns {(*|undefined)} The removed data or undefined if nothing removed
*
* @example
* list.addToBack("a")
* .addToBack("b");
* list.remove(1);
* // list is now <"a">
*/
List.prototype.remove = function remove(index) {
var length = this.length;
var removedData = void 0;
if (index === 0) {
return this.removeFront();
} else if (index >= length - 1) {
return this.removeBack();
}
// parent of wanted node
var parentNode = getNode.call(this, index - 1);
if (parentNode) {
var toRemove = parentNode.next;
removedData = toRemove.data;
var toRemoveNext = toRemove.next;
parentNode.next = toRemoveNext;
toRemoveNext.prev = parentNode;
this.length = length - 1;
}
return removedData;
};
/**
* Returns the index of the given data in the linked list
* @param {*} data - The data to find index of
* @param {function} comparator - function to compare for equality
* @returns {number} The index of @param data or -1 if not found
*
* @example
* const customComparator = function(a, b) {
* if(a.age < b.age) { return -1;}
* else if(a.age > b.age) { return 1:}
* else { return 0; }
* }
* list.addToBack({ age : 2})
* .addToBack({ age : 3});
* list.indexOf({ age : 2}, customComparator) // 0
*/
List.prototype.indexOf = function indexOf(data, comparator) {
var cmp = comparator || _Util.defaultComparator;
var index = 0;
var head = this.head;
while (head) {
if (cmp(data, head.data) === 0) {
return index;
}
head = head.next;
index += 1;
}
return -1;
};
/**
* Returns whether the linked list contains the given data
* @param {*} data - The data to insert into linked list
* @param {function} comparator - function to compare for equality
* @returns {number} The index of @param data or -1 if not found
*/
List.prototype.contains = function contains(data, comparator) {
return this.indexOf(data, comparator) !== -1;
};
/**
* Empties the List
* @returns {undefined}
*/
List.prototype.clear = function clear() {
this.head = null;
this.tail = null;
this.length = 0;
};
/**
* Returns the size of the List
* @returns {number} The size of the List
*/
List.prototype.size = function size() {
return this.length;
};
/**
* Calls a callback function for each element in the list
* @param {function} predicate - Function executed for each element
* (data, index)
* @returns {List} The instance that this method was called
*/
List.prototype.forEach = function forEach(predicate) {
var head = this.head;
var index = 0;
while (head) {
predicate(head.data, index);
head = head.next;
index += 1;
}
return this;
};
/**
* Returns a new list with only elements that return truthy when passed to the
* given callback
* @param {function(data)} predicate - The function used to evaluate elements
* @returns {List} A new list with filtered elements
*/
List.prototype.filter = function filter(predicate) {
var head = this.head;
var newList = new List();
var data = void 0;
while (head) {
data = head.data;
if (predicate(data)) {
newList.addToBack(data);
}
head = head.next;
}
return newList;
};
/**
* Reports if every element in the list passes a certain condition
* @param {function(data)} predicate - The function used for evaluations
* @returns {boolean} True if every element passes the test and false otherwise
*/
List.prototype.every = function every(predicate) {
var head = this.head;
while (head) {
if (!predicate(head.data)) {
return false;
}
head = head.next;
}
return true;
};
/**
* Reports if at least one element in the list passes a certain condition
* @param {function(data)} predicate - The function used for evaluations
* @returns {boolean} True if one or more elements passes the test and false otherwise
*/
List.prototype.some = function some(predicate) {
var head = this.head;
while (head) {
if (predicate(head.data)) {
return true;
}
head = head.next;
}
return false;
};
/**
* Transforms a linked list to an array
* @returns {Array} An array representation of 'this' List
*/
List.prototype.toArray = function toArray() {
var temp = [];
this.forEach(function (element) {
return temp.push(element);
});
return temp;
};
return List;
}();
exports['default'] = List;
/***/ },
/* 2 */
/***/ function(module, exports) {
'use strict';
exports.__esModule = true;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
exports.swap = swap;
exports.flat = flat;
exports.toString = toString;
exports.defaultComparator = defaultComparator;
exports.isNumber = isNumber;
exports.generateRandomInt = generateRandomInt;
/**
* swap method for Structs BHeap and Array
* @private
* @param {Array} array - array to swap certain elements
* @param {number} index1 - index to swap with @param index2
* @param {number} index2 - index to swap with @param index1
* @returns {undefined}
*/
function swap(array, index1, index2) {
var oldIndex1 = array[index1];
array[index1] = array[index2];
array[index2] = oldIndex1;
}
/**
* Makes a 1-D array from an n-D array
* @private
* @param {Array} array - The array to flatten
* @param {res} - The new flattened array
* @returns {undefined}
*/
function flat(array, res) {
var newArr = [];
var curValue = void 0;
for (var i = 0, len = array.length; i < len; i += 1) {
curValue = array[i];
if (Array.isArray(curValue)) {
flat(curValue, res);
} else {
res.push(curValue);
}
}
return newArr;
}
/**
* Converts a given value to a string
* @private
* @param {*} value - The value to convert to a string
* @returns {string} @param value to string or stringified by JSON
*/
function toString(value) {
var type = typeof value === 'undefined' ? 'undefined' : _typeof(value);
if (type === 'string') {
return value;
} else if (type === 'number' || type === 'boolean' || type === 'function') {
return value.toString();
}
return JSON.stringify(value);
}
/**
* default comparator for all Collections
* @function defaultComparator
* @param {(number|string)} a - first element to compare
* @param {(number|string)} b - second element to compare
* @returns {number} -1 if a < b, 1 if a > b, and 0 if equal
*
* @example
* function(a, b) {
if(a < b) {
return -1;
} else if(a > b) {
return 1;
}
return 0;
}
*/
function defaultComparator(a, b) {
if (a < b) {
return -1;
}
if (a === b) {
return 0;
}
return 1;
}
/**
* Custom comparator example for all Collections
* @function customComparator
* @param {*} a - first element to compare
* @param {*} b - second element to compare
* @returns {number} -1 if a < b, 1 if a > b, and 0 if equal
*
* @example
* // suppose data is of the form { age : 2 } , { age : 12 }....etc
* function(a, b) {
if(a.age < b.age) {
return -1;
} else if(a.age > b.age) {
return 1;
}
return 0;
}
*/
// eslint-disable-next-line no-unused-vars
function customComparator(a, b) {
// eslint-disable-line no-unused-vars
if (a < b) {
return -1;
}
if (a === b) {
return 0;
}
return 1;
}
/**
* Number.isNaN polyfill from
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference
* /Global_Objects/Number/isFinite
* @private
*/
function isNumber(value) {
// eslint-disable-next-line no-restricted-globals
if (typeof value !== 'number' || !isFinite(value)) {
throw new TypeError('Argument must be of type number or Number');
}
}
/**
* Generates a random integer between 0 and limit (exclusive)
* @private
* @param {number} limit - Upper bound on random number
* @returns {number} Random number in the range [0, @param limit)
*/
function generateRandomInt(limit) {
return Math.floor(Math.random() * limit);
}
/***/ },
/* 3 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
exports.__esModule = true;
var _List = __webpack_require__(1);
var _List2 = _interopRequireDefault(_List);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Stack representation
* @class
*
* @example
* const stack = new Collections.Stack();
* // FOR ALL EXAMPLES BELOW. ASSUME stack IS CLEARED BEFORE EACH EXAMPLE
*/
var Stack = function () {
function Stack() {
_classCallCheck(this, Stack);
this.stack = new _List2['default']();
}
/**
* Pushes the given data onto the stack
* @param {data} data - The data to push onto stack
* @returns {Stack} The instance this method was called
*
* @example
* stack.push(1).push(2); // <2, 1>
*/
Stack.prototype.push = function push(data) {
this.stack.addToFront(data);
return this;
};
/**
* Removes data from stack in a last in first out manner
* @returns {*} The reomved data
*
* @example
* stack.push(1).push(2).push(3);
* stack.pop(); // 3
*/
Stack.prototype.pop = function pop() {
return this.stack.removeFront();
};
/**
* Reports but does not remove the staged element to be removed next
* @returns {*} The element staged to be removed next
*
* @example
* stack.push(1);
* stack.peek() // returns 1 but does not remove it
*/
Stack.prototype.peek = function peek() {
return this.stack.elementAtIndex(0);
};
/**
* Empties the Stack
* @returns {undefined}
*/
Stack.prototype.clear = function clear() {
return this.stack.clear();
};
/**
* Reports the size of the queue
* @returns {number} The size of the queue
*/
Stack.prototype.size = function size() {
return this.stack.size();
};
return Stack;
}();
exports['default'] = Stack;
/***/ },
/* 4 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
exports.__esModule = true;
var _List = __webpack_require__(1);
var _List2 = _interopRequireDefault(_List);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Queue Representation
* @class
*
* @example
* const queue = new Collections.Queue();
* // FOR ALL EXAMPLES BELOW. ASSUME queue IS CLEARED BEFORE EACH EXAMPLE
*/
var Queue = function () {
function Queue() {
_classCallCheck(this, Queue);
this.queue = new _List2['default']();
}
/**
* Inserts given data into queue
* @param {*} data - The data to insert into queue
* @returns {Queue} The instance that this method was called
*
* @example
* queue.enqueue(1).enqueue(2);
*
*/
Queue.prototype.enqueue = function enqueue(data) {
this.queue.addToBack(data);
return this;
};
/**
* Removes from the queue in a first in first out manner
* @returns {*} The removed data
* @example
* queue.enqueue(1).enqueue(2);
* queue.dequeue() // 1
*/
Queue.prototype.dequeue = function dequeue() {
return this.queue.removeFront();
};
/**
* Reports but does not remove the staged element to be removed next
* @returns {*} Element staged to be removed next
*
* @example
* queue.enqueue(1);
* queue.peek() // returns 1 but does not remove it
*/
Queue.prototype.peek = function peek() {
return this.queue.elementAtIndex(0);
};
/**
* Empties the Queue
* @returns {undefined}
*/
Queue.prototype.clear = function clear() {
return this.queue.clear();
};
/**
* Reports the size of the queue
* @returns {number} The size of the queue
*/
Queue.prototype.size = function size() {
return this.queue.size();
};
return Queue;
}();
exports['default'] = Queue;
/***/ },
/* 5 */
/***/ function(module, exports, __webpack_require__) {
'use strict';
exports.__esModule = true;
var _Util = __webpack_require__(2);
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Sifts down (swaps elements downward) the given array
* @private
* @param {Array} array - The array to sift down on.
* @param {number} index - The index to start the sift down operation.
* @param {function} comparator - The comparator to use against parent and
* child elements.
* @returns {undefined}
*/
function heapify(array, index, comparator) {
var leftChildIndex = 2 * index;
var rightChildIndex = 2 * index + 1;
var numIndicies = array.length - 1;
var largest = void 0;
if (leftChildIndex <= numIndicies && comparator(array[leftChildIndex], array[index]) === 1) {
largest = leftChildIndex;
} else {
largest = index;
}
if (rightChildIndex <= numIndicies && comparator(array[rightChildIndex], array[largest]) === 1) {
largest = rightChildIndex;
}
if (largest !== index) {
(0, _Util.swap)(array, index, largest);
heapify(array, largest, comparator);
}
}
/**
* Sifts up (swaps elements upward) the given array
* @private
* @param {Array} array - The array to sift up on.
* @param {number} index - The index to start the sift up operation.
* @param {function} comparator - The comparator to use against parent
* and child elements
* @returns {undefined}
*/
function siftUp(array, index, comparator) {
if (index > 1) {
var parent = Math.floor(index / 2);
if (comparator(array[parent], array[index]) === -1) {
(0, _Util.swap)(array, parent, index);
siftUp(array, parent, comparator);
}
}
}
/**
* Binary heap representation