nlptoolkit-datastructure 1.0.7 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/README.md +1 -1
  2. package/dist/CounterHashMap.js +108 -118
  3. package/dist/CounterHashMap.js.map +1 -1
  4. package/dist/LRUCache.js +55 -63
  5. package/dist/LRUCache.js.map +1 -1
  6. package/dist/Queue.d.ts +24 -0
  7. package/dist/Queue.js +57 -40
  8. package/dist/Queue.js.map +1 -1
  9. package/dist/Stack.d.ts +23 -0
  10. package/dist/Stack.js +42 -29
  11. package/dist/Stack.js.map +1 -1
  12. package/dist/heap/Heap.d.ts +53 -0
  13. package/dist/heap/Heap.js +115 -68
  14. package/dist/heap/Heap.js.map +1 -1
  15. package/dist/heap/HeapNode.d.ts +8 -0
  16. package/dist/heap/HeapNode.js +19 -20
  17. package/dist/heap/HeapNode.js.map +1 -1
  18. package/dist/heap/MaxHeap.js +11 -21
  19. package/dist/heap/MaxHeap.js.map +1 -1
  20. package/dist/heap/MinHeap.js +11 -21
  21. package/dist/heap/MinHeap.js.map +1 -1
  22. package/dist/index.d.ts +9 -0
  23. package/dist/index.js +26 -0
  24. package/dist/index.js.map +1 -0
  25. package/dist/tree/AvlTree.d.ts +81 -0
  26. package/dist/tree/AvlTree.js +163 -92
  27. package/dist/tree/AvlTree.js.map +1 -1
  28. package/dist/tree/AvlTreeNode.js +11 -20
  29. package/dist/tree/AvlTreeNode.js.map +1 -1
  30. package/dist/tree/BTree.d.ts +35 -0
  31. package/dist/tree/BTree.js +76 -49
  32. package/dist/tree/BTree.js.map +1 -1
  33. package/dist/tree/BTreeNode.d.ts +69 -0
  34. package/dist/tree/BTreeNode.js +168 -104
  35. package/dist/tree/BTreeNode.js.map +1 -1
  36. package/dist/tree/Tree.d.ts +37 -0
  37. package/dist/tree/Tree.js +83 -55
  38. package/dist/tree/Tree.js.map +1 -1
  39. package/dist/tree/TreeNode.js +11 -20
  40. package/dist/tree/TreeNode.js.map +1 -1
  41. package/package.json +5 -7
  42. package/source/Queue.ts +24 -0
  43. package/source/Stack.ts +23 -0
  44. package/source/heap/Heap.ts +53 -0
  45. package/source/heap/HeapNode.ts +8 -0
  46. package/source/heap/MaxHeap.ts +0 -1
  47. package/source/index.ts +9 -0
  48. package/source/tree/AvlTree.ts +81 -0
  49. package/source/tree/BTree.ts +35 -0
  50. package/source/tree/BTreeNode.ts +69 -0
  51. package/source/tree/Tree.ts +37 -0
  52. package/tsconfig.json +4 -5
  53. package/index.js +0 -9
  54. package/source/tsconfig.json +0 -13
package/README.md CHANGED
@@ -7,7 +7,7 @@ For Developers
7
7
  ============
8
8
 
9
9
  You can also see [Java](https://github.com/starlangsoftware/DataStructure), [Python](https://github.com/starlangsoftware/DataStructure-Py),
10
- [Cython](https://github.com/starlangsoftware/DataStructure-Cy), [Swift](https://github.com/starlangsoftware/DataStructure-Swift), [C](https://github.com/starlangsoftware/DataStructure-C),
10
+ [Cython](https://github.com/starlangsoftware/DataStructure-Cy), [Swift](https://github.com/starlangsoftware/DataStructure-Swift), [Php](https://github.com/starlangsoftware/DataStructure-Php), [C](https://github.com/starlangsoftware/DataStructure-C),
11
11
  [C#](https://github.com/starlangsoftware/DataStructure-CS), or [C++](https://github.com/starlangsoftware/DataStructure-CPP) repository.
12
12
 
13
13
  ## Requirements
@@ -1,131 +1,121 @@
1
- (function (factory) {
2
- if (typeof module === "object" && typeof module.exports === "object") {
3
- var v = factory(require, exports);
4
- if (v !== undefined) module.exports = v;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CounterHashMap = void 0;
4
+ class CounterHashMap extends Map {
5
+ constructor() {
6
+ super();
5
7
  }
6
- else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports"], factory);
8
+ /**
9
+ * The put method takes a K type input. If this map contains a mapping for the key, it puts this key after
10
+ * incrementing its value by one. If his map does not contain a mapping, then it directly puts key with the value of 1.
11
+ *
12
+ * @param key to put.
13
+ */
14
+ put(key) {
15
+ if (this.has(key)) {
16
+ this.set(key, this.get(key) + 1);
17
+ }
18
+ else {
19
+ this.set(key, 1);
20
+ }
8
21
  }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.CounterHashMap = void 0;
13
- class CounterHashMap extends Map {
14
- constructor() {
15
- super();
22
+ /**
23
+ * The putNTimes method takes a K and an integer N as inputs. If this map contains a mapping for the key, it puts this key after
24
+ * incrementing its value by N. If his map does not contain a mapping, then it directly puts key with the value of N.
25
+ *
26
+ * @param key to put.
27
+ * @param N to increment value.
28
+ */
29
+ putNTimes(key, N) {
30
+ if (this.has(key)) {
31
+ this.set(key, this.get(key) + N);
16
32
  }
17
- /**
18
- * The put method takes a K type input. If this map contains a mapping for the key, it puts this key after
19
- * incrementing its value by one. If his map does not contain a mapping, then it directly puts key with the value of 1.
20
- *
21
- * @param key to put.
22
- */
23
- put(key) {
24
- if (this.has(key)) {
25
- this.set(key, this.get(key) + 1);
26
- }
27
- else {
28
- this.set(key, 1);
29
- }
33
+ else {
34
+ this.set(key, N);
30
35
  }
31
- /**
32
- * The putNTimes method takes a K and an integer N as inputs. If this map contains a mapping for the key, it puts this key after
33
- * incrementing its value by N. If his map does not contain a mapping, then it directly puts key with the value of N.
34
- *
35
- * @param key to put.
36
- * @param N to increment value.
37
- */
38
- putNTimes(key, N) {
39
- if (this.has(key)) {
40
- this.set(key, this.get(key) + N);
41
- }
42
- else {
43
- this.set(key, N);
44
- }
36
+ }
37
+ /**
38
+ * The count method takes a K as input, if this map contains a mapping for the key, it returns the value corresponding
39
+ * this key, 0 otherwise.
40
+ *
41
+ * @param key to get value.
42
+ * @return the value corresponding given key, 0 if it is not mapped.
43
+ */
44
+ count(key) {
45
+ if (this.has(key)) {
46
+ return this.get(key);
45
47
  }
46
- /**
47
- * The count method takes a K as input, if this map contains a mapping for the key, it returns the value corresponding
48
- * this key, 0 otherwise.
49
- *
50
- * @param key to get value.
51
- * @return the value corresponding given key, 0 if it is not mapped.
52
- */
53
- count(key) {
54
- if (this.has(key)) {
55
- return this.get(key);
56
- }
57
- else {
58
- return 0;
59
- }
48
+ else {
49
+ return 0;
60
50
  }
61
- /**
62
- * The sumOfCounts method loops through the values contained in this map and accumulates the counts of this values.
63
- *
64
- * @return accumulated counts.
65
- */
66
- sumOfCounts() {
67
- let sum = 0;
68
- for (let count of this.values()) {
69
- sum += count;
70
- }
71
- return sum;
51
+ }
52
+ /**
53
+ * The sumOfCounts method loops through the values contained in this map and accumulates the counts of this values.
54
+ *
55
+ * @return accumulated counts.
56
+ */
57
+ sumOfCounts() {
58
+ let sum = 0;
59
+ for (let count of this.values()) {
60
+ sum += count;
72
61
  }
73
- /**
74
- * The max method takes a threshold as input and loops through the mappings contained in this map. It accumulates the
75
- * count values and if the current entry's count value is greater than maxCount, which is initialized as 0,
76
- * it updates the maxCount as current count and maxKey as the current count's key.
77
- * <p>
78
- * At the end of the loop, if the ratio of maxCount/total is greater than the given threshold it returns maxKey, else null.
79
- *
80
- * @param threshold double value.
81
- * @return K type maxKey if greater than the given threshold, null otherwise.
82
- */
83
- max(threshold = 0.0) {
84
- let maxCount = 0;
85
- let total = 0;
86
- let maxKey;
87
- for (const [key, value] of this.entries()) {
88
- let count = value;
89
- total += count;
90
- if (count > maxCount) {
91
- maxCount = count;
92
- maxKey = key;
93
- }
94
- }
95
- if (maxCount / total > threshold) {
96
- return maxKey;
97
- }
98
- else {
99
- return undefined;
62
+ return sum;
63
+ }
64
+ /**
65
+ * The max method takes a threshold as input and loops through the mappings contained in this map. It accumulates the
66
+ * count values and if the current entry's count value is greater than maxCount, which is initialized as 0,
67
+ * it updates the maxCount as current count and maxKey as the current count's key.
68
+ * <p>
69
+ * At the end of the loop, if the ratio of maxCount/total is greater than the given threshold it returns maxKey, else null.
70
+ *
71
+ * @param threshold double value.
72
+ * @return K type maxKey if greater than the given threshold, null otherwise.
73
+ */
74
+ max(threshold = 0.0) {
75
+ let maxCount = 0;
76
+ let total = 0;
77
+ let maxKey;
78
+ for (const [key, value] of this.entries()) {
79
+ let count = value;
80
+ total += count;
81
+ if (count > maxCount) {
82
+ maxCount = count;
83
+ maxKey = key;
100
84
  }
101
85
  }
102
- /**
103
- * The add method adds value of each key of toBeAdded to the current counterHashMap.
104
- *
105
- * @param toBeAdded CounterHashMap to be added to this counterHashMap.
106
- */
107
- add(toBeAdded) {
108
- for (let value of toBeAdded.keys()) {
109
- this.putNTimes(value, toBeAdded.get(value));
110
- }
86
+ if (maxCount / total > threshold) {
87
+ return maxKey;
111
88
  }
112
- /**
113
- * The topN method takes an integer N as inout. It creates an {@link Array} result and loops through the the
114
- * mappings contained in this map and adds each entry to the result {@link Array}. Then sort this {@link Array}
115
- * according to their values and returns an {@link Array} which is a sublist of result with N elements.
116
- *
117
- * @param N Integer value for defining size of the sublist.
118
- * @return a sublist of N element.
119
- */
120
- topN(N) {
121
- var result = [];
122
- for (let entry of this.entries()) {
123
- result.push(entry);
124
- }
125
- result.sort((a, b) => (a[1] > b[1]) ? -1 : ((b[1] > a[1]) ? 1 : 0));
126
- return result;
89
+ else {
90
+ return undefined;
91
+ }
92
+ }
93
+ /**
94
+ * The add method adds value of each key of toBeAdded to the current counterHashMap.
95
+ *
96
+ * @param toBeAdded CounterHashMap to be added to this counterHashMap.
97
+ */
98
+ add(toBeAdded) {
99
+ for (let value of toBeAdded.keys()) {
100
+ this.putNTimes(value, toBeAdded.get(value));
101
+ }
102
+ }
103
+ /**
104
+ * The topN method takes an integer N as inout. It creates an {@link Array} result and loops through the the
105
+ * mappings contained in this map and adds each entry to the result {@link Array}. Then sort this {@link Array}
106
+ * according to their values and returns an {@link Array} which is a sublist of result with N elements.
107
+ *
108
+ * @param N Integer value for defining size of the sublist.
109
+ * @return a sublist of N element.
110
+ */
111
+ topN(N) {
112
+ var result = [];
113
+ for (let entry of this.entries()) {
114
+ result.push(entry);
127
115
  }
116
+ result.sort((a, b) => (a[1] > b[1]) ? -1 : ((b[1] > a[1]) ? 1 : 0));
117
+ return result;
128
118
  }
129
- exports.CounterHashMap = CounterHashMap;
130
- });
119
+ }
120
+ exports.CounterHashMap = CounterHashMap;
131
121
  //# sourceMappingURL=CounterHashMap.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"CounterHashMap.js","sourceRoot":"","sources":["../source/CounterHashMap.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,MAAa,cAAkB,SAAQ,GAAc;QAEjD;YACI,KAAK,EAAE,CAAC;QACZ,CAAC;QAED;;;;;WAKG;QACH,GAAG,CAAC,GAAM;YACN,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAC;gBACd,IAAI,CAAC,GAAG,CAAC,GAAG,EAAU,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAA;aAC3C;iBAAM;gBACH,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;aACnB;QACL,CAAC;QAED;;;;;;WAMG;QACH,SAAS,CAAC,GAAM,EAAE,CAAS;YACvB,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAC;gBACd,IAAI,CAAC,GAAG,CAAC,GAAG,EAAU,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAA;aAC3C;iBAAM;gBACH,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,CAAA;aACnB;QACL,CAAC;QAED;;;;;;WAMG;QACH,KAAK,CAAC,GAAM;YACR,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,EAAC;gBACd,OAAe,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;aAC/B;iBAAM;gBACH,OAAO,CAAC,CAAA;aACX;QACL,CAAC;QAED;;;;WAIG;QACH,WAAW;YACP,IAAI,GAAG,GAAG,CAAC,CAAA;YACX,KAAK,IAAI,KAAK,IAAI,IAAI,CAAC,MAAM,EAAE,EAAC;g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AAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAA;QACnE,OAAO,MAAM,CAAA;IACjB,CAAC;CAEJ;AAxHD,wCAwHC"}
package/dist/LRUCache.js CHANGED
@@ -1,69 +1,61 @@
1
- (function (factory) {
2
- if (typeof module === "object" && typeof module.exports === "object") {
3
- var v = factory(require, exports);
4
- if (v !== undefined) module.exports = v;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LRUCache = void 0;
4
+ class LRUCache {
5
+ cacheSize;
6
+ map;
7
+ /**
8
+ * A constructor of {@link LRUCache} class which takes cacheSize as input. It creates new {@link Map} and
9
+ * {@link Map}.
10
+ *
11
+ * @param cacheSize Integer input defining cache size.
12
+ */
13
+ constructor(cacheSize) {
14
+ this.cacheSize = cacheSize;
15
+ this.map = new Map();
5
16
  }
6
- else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports"], factory);
17
+ /**
18
+ * The contains method takes a K type input key and returns true if the {@link Map} has the given key, false otherwise.
19
+ *
20
+ * @param key K type input key.
21
+ * @return true if the {@link Map} has the given key, false otherwise.
22
+ */
23
+ contains(key) {
24
+ return this.map.has(key);
8
25
  }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.LRUCache = void 0;
13
- class LRUCache {
14
- /**
15
- * A constructor of {@link LRUCache} class which takes cacheSize as input. It creates new {@link Map} and
16
- * {@link Map}.
17
- *
18
- * @param cacheSize Integer input defining cache size.
19
- */
20
- constructor(cacheSize) {
21
- this.cacheSize = cacheSize;
22
- this.map = new Map();
26
+ /**
27
+ * The get method takes K type input key and returns the least recently used value. First it checks whether the {@link Map}
28
+ * has the given key, if so it gets the corresponding cacheNode. It removes that element from map
29
+ * and adds it again to the beginning of the map since it is more likely to be used again. At the end, returns the
30
+ * data value.
31
+ *
32
+ * @param key K type input key.
33
+ * @return data value if the {@link Map} has the given key, null otherwise.
34
+ */
35
+ get(key) {
36
+ if (this.map.has(key)) {
37
+ let value = this.map.get(key);
38
+ this.map.delete(key);
39
+ this.map.set(key, value);
40
+ return value;
23
41
  }
24
- /**
25
- * The contains method takes a K type input key and returns true if the {@link Map} has the given key, false otherwise.
26
- *
27
- * @param key K type input key.
28
- * @return true if the {@link Map} has the given key, false otherwise.
29
- */
30
- contains(key) {
31
- return this.map.has(key);
32
- }
33
- /**
34
- * The get method takes K type input key and returns the least recently used value. First it checks whether the {@link Map}
35
- * has the given key, if so it gets the corresponding cacheNode. It removes that element from map
36
- * and adds it again to the beginning of the map since it is more likely to be used again. At the end, returns the
37
- * data value.
38
- *
39
- * @param key K type input key.
40
- * @return data value if the {@link Map} has the given key, null otherwise.
41
- */
42
- get(key) {
43
- if (this.map.has(key)) {
44
- let value = this.map.get(key);
45
- this.map.delete(key);
46
- this.map.set(key, value);
47
- return value;
48
- }
49
- return null;
50
- }
51
- /**
52
- * The add method take a key and a data as inputs. First it checks the size of the {@link Map}, if it is full (i.e
53
- * equal to the given cacheSize) then it removes the last node. If it has space for new entries,
54
- * it creates new node with given inputs and puts it to the {@link Map}.
55
- *
56
- * @param key K type input.
57
- * @param data T type input
58
- */
59
- add(key, data) {
60
- if (this.map.size == this.cacheSize) {
61
- let firstKey = this.map.entries().next().value;
62
- this.map.delete(firstKey);
63
- }
64
- this.map.set(key, data);
42
+ return null;
43
+ }
44
+ /**
45
+ * The add method take a key and a data as inputs. First it checks the size of the {@link Map}, if it is full (i.e
46
+ * equal to the given cacheSize) then it removes the last node. If it has space for new entries,
47
+ * it creates new node with given inputs and puts it to the {@link Map}.
48
+ *
49
+ * @param key K type input.
50
+ * @param data T type input
51
+ */
52
+ add(key, data) {
53
+ if (this.map.size == this.cacheSize) {
54
+ let firstKey = this.map.entries().next().value;
55
+ this.map.delete(firstKey);
65
56
  }
57
+ this.map.set(key, data);
66
58
  }
67
- exports.LRUCache = LRUCache;
68
- });
59
+ }
60
+ exports.LRUCache = LRUCache;
69
61
  //# sourceMappingURL=LRUCache.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"LRUCache.js","sourceRoot":"","sources":["../source/LRUCache.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,MAAa,QAAQ;QAKjB;;;;;WAKG;QACH,YAAY,SAAiB;YACzB,IAAI,CAAC,SAAS,GAAG,SAAS,CAAA;YAC1B,IAAI,CAAC,GAAG,GAAG,IAAI,GAAG,EAAQ,CAAA;QAC9B,CAAC;QAED;;;;;WAKG;QACH,QAAQ,CAAC,GAAM;YACX,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;QAC5B,CAAC;QAED;;;;;;;;WAQG;QACH,GAAG,CAAC,GAAM;YACN,IAAI,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE;gBACnB,IAAI,KAAK,GAAW,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;gBACrC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;gBACpB,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAA;gBACxB,OAAO,KAAK,CAAC;aAChB;YACD,OAAO,IAAI,CAAA;QACf,CAAC;QAED;;;;;;;WAOG;QACH,GAAG,CAAC,GAAM,EAAE,IAAO;YACf,IAAI,IAAI,CAAC,GAAG,CAAC,IAAI,IAAI,IAAI,CAAC,SAAS,EAAE;gBACjC,IAAI,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAA;gBAC9C,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAA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C,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA;IAC3B,CAAC;CACJ;AA5DD,4BA4DC"}
package/dist/Queue.d.ts CHANGED
@@ -3,10 +3,34 @@ export declare class Queue<T> {
3
3
  private head;
4
4
  private tail;
5
5
  private maxSize;
6
+ /**
7
+ * Constructor of the queue data structure
8
+ * @param maxSize Maximum size of the queue
9
+ */
6
10
  constructor(maxSize: number);
11
+ /**
12
+ * Adds a set of elements to the end of the queue
13
+ * @param items Elements to be inserted into the queue
14
+ */
7
15
  enqueueAll(items: Array<T>): void;
16
+ /**
17
+ * Adds an element to the end of the queue.
18
+ * @param item Element added to the queue.
19
+ */
8
20
  enqueue(item: T): void;
21
+ /**
22
+ * Removes an element from the start of the queue.
23
+ * @return Removed item
24
+ */
9
25
  dequeue(): T;
26
+ /**
27
+ * Returns head of the queue.
28
+ * @return Head of the queue
29
+ */
10
30
  peek(): T;
31
+ /**
32
+ * Checks if the queue is empty or not.
33
+ * @return Returns true if the queue is empty, false otherwise.
34
+ */
11
35
  isEmpty(): boolean;
12
36
  }
package/dist/Queue.js CHANGED
@@ -1,44 +1,61 @@
1
- (function (factory) {
2
- if (typeof module === "object" && typeof module.exports === "object") {
3
- var v = factory(require, exports);
4
- if (v !== undefined) module.exports = v;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Queue = void 0;
4
+ class Queue {
5
+ list = [];
6
+ head;
7
+ tail;
8
+ maxSize;
9
+ /**
10
+ * Constructor of the queue data structure
11
+ * @param maxSize Maximum size of the queue
12
+ */
13
+ constructor(maxSize) {
14
+ this.head = 0;
15
+ this.tail = 0;
16
+ this.maxSize = maxSize;
17
+ this.list = new Array(maxSize);
5
18
  }
6
- else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports"], factory);
8
- }
9
- })(function (require, exports) {
10
- "use strict";
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.Queue = void 0;
13
- class Queue {
14
- constructor(maxSize) {
15
- this.list = [];
16
- this.head = 0;
17
- this.tail = 0;
18
- this.maxSize = maxSize;
19
- this.list = new Array(maxSize);
20
- }
21
- enqueueAll(items) {
22
- for (let item of items) {
23
- this.enqueue(item);
24
- }
25
- }
26
- enqueue(item) {
27
- this.list[this.tail] = item;
28
- this.tail = (this.tail + 1) % this.maxSize;
29
- }
30
- dequeue() {
31
- let item = this.list[this.head];
32
- this.head = (this.head + 1) % this.maxSize;
33
- return item;
34
- }
35
- peek() {
36
- return this.list[this.head];
37
- }
38
- isEmpty() {
39
- return this.head == this.tail;
19
+ /**
20
+ * Adds a set of elements to the end of the queue
21
+ * @param items Elements to be inserted into the queue
22
+ */
23
+ enqueueAll(items) {
24
+ for (let item of items) {
25
+ this.enqueue(item);
40
26
  }
41
27
  }
42
- exports.Queue = Queue;
43
- });
28
+ /**
29
+ * Adds an element to the end of the queue.
30
+ * @param item Element added to the queue.
31
+ */
32
+ enqueue(item) {
33
+ this.list[this.tail] = item;
34
+ this.tail = (this.tail + 1) % this.maxSize;
35
+ }
36
+ /**
37
+ * Removes an element from the start of the queue.
38
+ * @return Removed item
39
+ */
40
+ dequeue() {
41
+ let item = this.list[this.head];
42
+ this.head = (this.head + 1) % this.maxSize;
43
+ return item;
44
+ }
45
+ /**
46
+ * Returns head of the queue.
47
+ * @return Head of the queue
48
+ */
49
+ peek() {
50
+ return this.list[this.head];
51
+ }
52
+ /**
53
+ * Checks if the queue is empty or not.
54
+ * @return Returns true if the queue is empty, false otherwise.
55
+ */
56
+ isEmpty() {
57
+ return this.head == this.tail;
58
+ }
59
+ }
60
+ exports.Queue = Queue;
44
61
  //# sourceMappingURL=Queue.js.map
package/dist/Queue.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Queue.js","sourceRoot":"","sources":["../source/Queue.ts"],"names":[],"mappings":";;;;;;;;;;;;IAAA,MAAa,KAAK;QAOd,YAAY,OAAe;YALnB,SAAI,GAAc,EAAE,CAAA;YAMxB,IAAI,CAAC,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AAC,CAAA;QACb,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;QACtB,IAAI,CAAC,IAAI,GAAG,IAAI,KAAK,CAAI,OAAO,CAAC,CAAA;IACrC,CAAC;IAED;;;OAGG;IACI,UAAU,CAAC,KAAe;QAC7B,KAAK,IAAI,IAAI,IAAI,KAAK,EAAC,CAAC;YACpB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAA;QACtB,CAAC;IACL,CAAC;IAED;;;OAGG;IACI,OAAO,CAAC,IAAO;QAClB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;QAC3B,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,OAAO,CAAA;IAC9C,CAAC;IAED;;;OAGG;IACI,OAAO;QACV,IAAI,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;QAC/B,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,OAAO,CAAA;QAC1C,OAAO,IAAI,CAAA;IACf,CAAC;IAED;;;OAGG;IACI,IAAI;QACP,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IAC/B,CAAC;IAED;;;OAGG;IACI,OAAO;QACV,OAAO,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,CAAA;IACjC,CAAC;CAEJ;AA/DD,sBA+DC"}
package/dist/Stack.d.ts CHANGED
@@ -1,7 +1,30 @@
1
+ /**
2
+ * Stack is a list data structure consisting of many elements. There are two types of operations defined for the
3
+ * elements of the stack: Adding an element to the stack (push) and removing an element from the stack (pop). In a
4
+ * stack, to be popped element is always the last pushed element. Also, when an element is pushed on to the stack, it
5
+ * is placed on top of the stack (at the end of the list).
6
+ * @param T Type of the data stored in the stack node.
7
+ */
1
8
  export declare class Stack<T> {
2
9
  private list;
3
10
  constructor();
11
+ /**
12
+ * When we push an element on top of the stack, we only need to increase the field top by 1 and place the new
13
+ * element on this new position. If the stack is full before this push operation, we can not push.
14
+ * @param item Item to insert.
15
+ */
4
16
  push(item: T): void;
17
+ /**
18
+ * When we remove an element from the stack (the function also returns that removed element), we need to be careful
19
+ * if the stack was empty or not. If the stack is not empty, the topmost element of the stack is returned and the
20
+ * field top is decreased by 1. If the stack is empty, the function will return null.
21
+ * @return The removed element
22
+ */
5
23
  pop(): T | null;
24
+ /**
25
+ * The function checks whether an array-implemented stack is empty or not. The function returns true if the stack is
26
+ * empty, false otherwise.
27
+ * @return True if the stack is empty, false otherwise.
28
+ */
6
29
  isEmpty(): boolean;
7
30
  }