@gabrielrufino/cube 1.0.1
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.
- package/.github/workflows/cd.yml +26 -0
- package/.github/workflows/ci.yml +23 -0
- package/LICENSE +24 -0
- package/README.md +1695 -0
- package/build/Algorithms/index.d.ts +3 -0
- package/build/Algorithms/index.js +24 -0
- package/build/Array/IArray.d.ts +9 -0
- package/build/Array/IArray.js +2 -0
- package/build/Array/index.d.ts +12 -0
- package/build/Array/index.js +79 -0
- package/build/BinarySearchTree/BinarySearchTreeNode.d.ts +7 -0
- package/build/BinarySearchTree/BinarySearchTreeNode.js +22 -0
- package/build/BinarySearchTree/IBinarySearchNodeOptions.d.ts +5 -0
- package/build/BinarySearchTree/IBinarySearchNodeOptions.js +2 -0
- package/build/BinarySearchTree/IBinarySearchTree.d.ts +14 -0
- package/build/BinarySearchTree/IBinarySearchTree.js +2 -0
- package/build/BinarySearchTree/IBinarySearchTreeData.d.ts +7 -0
- package/build/BinarySearchTree/IBinarySearchTreeData.js +2 -0
- package/build/BinarySearchTree/index.d.ts +24 -0
- package/build/BinarySearchTree/index.js +199 -0
- package/build/DataStructure/index.d.ts +6 -0
- package/build/DataStructure/index.js +32 -0
- package/build/Deck/IDeck.d.ts +9 -0
- package/build/Deck/IDeck.js +2 -0
- package/build/Deck/index.d.ts +12 -0
- package/build/Deck/index.js +76 -0
- package/build/Dictionary/IDictionary.d.ts +16 -0
- package/build/Dictionary/IDictionary.js +2 -0
- package/build/Dictionary/IDictionaryData.d.ts +4 -0
- package/build/Dictionary/IDictionaryData.js +2 -0
- package/build/Dictionary/index.d.ts +21 -0
- package/build/Dictionary/index.js +110 -0
- package/build/DoublyLinkedList/IDoublyLinkedList.d.ts +16 -0
- package/build/DoublyLinkedList/IDoublyLinkedList.js +2 -0
- package/build/DoublyLinkedList/Node.d.ts +6 -0
- package/build/DoublyLinkedList/Node.js +11 -0
- package/build/DoublyLinkedList/index.d.ts +32 -0
- package/build/DoublyLinkedList/index.js +221 -0
- package/build/Graph/GraphNodeNotFoundError.d.ts +3 -0
- package/build/Graph/GraphNodeNotFoundError.js +27 -0
- package/build/Graph/GraphSearchNodeStates.d.ts +6 -0
- package/build/Graph/GraphSearchNodeStates.js +10 -0
- package/build/Graph/IGraph.d.ts +16 -0
- package/build/Graph/IGraph.js +2 -0
- package/build/Graph/IGraphOptions.d.ts +7 -0
- package/build/Graph/IGraphOptions.js +2 -0
- package/build/Graph/index.d.ts +20 -0
- package/build/Graph/index.js +187 -0
- package/build/HashTable/IHashTable.d.ts +9 -0
- package/build/HashTable/IHashTable.js +2 -0
- package/build/HashTable/IHashTableData.d.ts +4 -0
- package/build/HashTable/IHashTableData.js +2 -0
- package/build/HashTable/IHashTableInputs.d.ts +4 -0
- package/build/HashTable/IHashTableInputs.js +2 -0
- package/build/HashTable/IHashTableOptions.d.ts +4 -0
- package/build/HashTable/IHashTableOptions.js +2 -0
- package/build/HashTable/index.d.ts +16 -0
- package/build/HashTable/index.js +83 -0
- package/build/HashTableLinearProbing/HashTableLinearProbingElement.d.ts +6 -0
- package/build/HashTableLinearProbing/HashTableLinearProbingElement.js +19 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbing.d.ts +9 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbing.js +2 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingData.d.ts +5 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingData.js +2 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingInputs.d.ts +4 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingInputs.js +2 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingOptions.d.ts +4 -0
- package/build/HashTableLinearProbing/IHashTableLinearProbingOptions.js +2 -0
- package/build/HashTableLinearProbing/index.d.ts +20 -0
- package/build/HashTableLinearProbing/index.js +120 -0
- package/build/HashTableSeparateChaining/HashTableSeparateChainingElement.d.ts +6 -0
- package/build/HashTableSeparateChaining/HashTableSeparateChainingElement.js +21 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChaining.d.ts +10 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChaining.js +2 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingData.d.ts +5 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingData.js +2 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingInputs.d.ts +4 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingInputs.js +2 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingOptions.d.ts +4 -0
- package/build/HashTableSeparateChaining/IHashTableSeparateChainingOptions.js +2 -0
- package/build/HashTableSeparateChaining/index.d.ts +19 -0
- package/build/HashTableSeparateChaining/index.js +104 -0
- package/build/LinkedList/ILinkedList.d.ts +13 -0
- package/build/LinkedList/ILinkedList.js +2 -0
- package/build/LinkedList/ILinkedListItem.d.ts +5 -0
- package/build/LinkedList/ILinkedListItem.js +2 -0
- package/build/LinkedList/Node.d.ts +5 -0
- package/build/LinkedList/Node.js +10 -0
- package/build/LinkedList/index.d.ts +24 -0
- package/build/LinkedList/index.js +169 -0
- package/build/List/index.d.ts +1 -0
- package/build/List/index.js +8 -0
- package/build/MaxHeap/IMaxHeap.d.ts +9 -0
- package/build/MaxHeap/IMaxHeap.js +2 -0
- package/build/MaxHeap/IMaxHeapOptions.d.ts +5 -0
- package/build/MaxHeap/IMaxHeapOptions.js +2 -0
- package/build/MaxHeap/index.d.ts +19 -0
- package/build/MaxHeap/index.js +102 -0
- package/build/MinHeap/IMinHeap.d.ts +9 -0
- package/build/MinHeap/IMinHeap.js +2 -0
- package/build/MinHeap/IMinHeapOptions.d.ts +5 -0
- package/build/MinHeap/IMinHeapOptions.js +2 -0
- package/build/MinHeap/index.d.ts +19 -0
- package/build/MinHeap/index.js +102 -0
- package/build/Queue/IQueue.d.ts +8 -0
- package/build/Queue/IQueue.js +2 -0
- package/build/Queue/index.d.ts +11 -0
- package/build/Queue/index.js +74 -0
- package/build/Set/ISet.d.ts +15 -0
- package/build/Set/ISet.js +2 -0
- package/build/Set/index.d.ts +21 -0
- package/build/Set/index.js +132 -0
- package/build/Stack/IStack.d.ts +8 -0
- package/build/Stack/IStack.js +2 -0
- package/build/Stack/index.d.ts +11 -0
- package/build/Stack/index.js +64 -0
- package/build/index.d.ts +17 -0
- package/build/index.js +40 -0
- package/coverage/clover.xml +706 -0
- package/coverage/coverage-final.json +19 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +327 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +196 -0
- package/coverage/lcov-report/src/Algorithms/index.html +117 -0
- package/coverage/lcov-report/src/Algorithms/index.ts.html +149 -0
- package/coverage/lcov-report/src/Array/index.html +117 -0
- package/coverage/lcov-report/src/Array/index.ts.html +290 -0
- package/coverage/lcov-report/src/DataStructure/index.html +117 -0
- package/coverage/lcov-report/src/DataStructure/index.ts.html +131 -0
- package/coverage/lcov-report/src/Deck/index.html +117 -0
- package/coverage/lcov-report/src/Deck/index.ts.html +239 -0
- package/coverage/lcov-report/src/Dictionary/index.html +117 -0
- package/coverage/lcov-report/src/Dictionary/index.ts.html +350 -0
- package/coverage/lcov-report/src/DoublyLinkedList/Node.ts.html +113 -0
- package/coverage/lcov-report/src/DoublyLinkedList/index.html +132 -0
- package/coverage/lcov-report/src/DoublyLinkedList/index.ts.html +836 -0
- package/coverage/lcov-report/src/HashTable/index.html +117 -0
- package/coverage/lcov-report/src/HashTable/index.ts.html +320 -0
- package/coverage/lcov-report/src/HashTableLinearProbing/index.html +117 -0
- package/coverage/lcov-report/src/HashTableLinearProbing/index.ts.html +95 -0
- package/coverage/lcov-report/src/HashTableSeparateChaining/HashTableSeparateChainingElement.ts.html +149 -0
- package/coverage/lcov-report/src/HashTableSeparateChaining/index.html +132 -0
- package/coverage/lcov-report/src/HashTableSeparateChaining/index.ts.html +398 -0
- package/coverage/lcov-report/src/LinkedList/Node.ts.html +110 -0
- package/coverage/lcov-report/src/LinkedList/index.html +132 -0
- package/coverage/lcov-report/src/LinkedList/index.ts.html +641 -0
- package/coverage/lcov-report/src/List/index.html +117 -0
- package/coverage/lcov-report/src/List/index.ts.html +89 -0
- package/coverage/lcov-report/src/Queue/index.html +117 -0
- package/coverage/lcov-report/src/Queue/index.ts.html +218 -0
- package/coverage/lcov-report/src/Set/index.html +117 -0
- package/coverage/lcov-report/src/Set/index.ts.html +488 -0
- package/coverage/lcov-report/src/Stack/index.html +117 -0
- package/coverage/lcov-report/src/Stack/index.ts.html +215 -0
- package/coverage/lcov-report/src/index.html +117 -0
- package/coverage/lcov-report/src/index.ts.html +125 -0
- package/coverage/lcov.info +1346 -0
- package/package.json +42 -0
- package/src/Algorithms/index.spec.ts +15 -0
- package/src/Algorithms/index.ts +21 -0
- package/src/Array/IArray.ts +10 -0
- package/src/Array/index.spec.ts +151 -0
- package/src/Array/index.ts +68 -0
- package/src/BinarySearchTree/BinarySearchTreeNode.spec.ts +29 -0
- package/src/BinarySearchTree/BinarySearchTreeNode.ts +23 -0
- package/src/BinarySearchTree/IBinarySearchNodeOptions.ts +6 -0
- package/src/BinarySearchTree/IBinarySearchTree.ts +16 -0
- package/src/BinarySearchTree/IBinarySearchTreeData.ts +9 -0
- package/src/BinarySearchTree/index.spec.ts +486 -0
- package/src/BinarySearchTree/index.ts +198 -0
- package/src/DataStructure/index.spec.ts +25 -0
- package/src/DataStructure/index.ts +15 -0
- package/src/Deck/IDeck.ts +10 -0
- package/src/Deck/index.spec.ts +131 -0
- package/src/Deck/index.ts +51 -0
- package/src/Dictionary/IDictionary.ts +18 -0
- package/src/Dictionary/IDictionaryData.ts +5 -0
- package/src/Dictionary/index.spec.ts +310 -0
- package/src/Dictionary/index.ts +88 -0
- package/src/DoublyLinkedList/IDoublyLinkedList.ts +18 -0
- package/src/DoublyLinkedList/Node.ts +9 -0
- package/src/DoublyLinkedList/index.spec.ts +478 -0
- package/src/DoublyLinkedList/index.ts +250 -0
- package/src/Graph/GraphNodeNotFoundError.ts +7 -0
- package/src/Graph/GraphSearchNodeStates.ts +8 -0
- package/src/Graph/IGraph.ts +15 -0
- package/src/Graph/IGraphOptions.ts +6 -0
- package/src/Graph/index.spec.ts +318 -0
- package/src/Graph/index.ts +170 -0
- package/src/HashTable/IHashTable.ts +11 -0
- package/src/HashTable/IHashTableData.ts +5 -0
- package/src/HashTable/IHashTableInputs.ts +5 -0
- package/src/HashTable/IHashTableOptions.ts +5 -0
- package/src/HashTable/index.spec.ts +136 -0
- package/src/HashTable/index.ts +78 -0
- package/src/HashTableLinearProbing/HashTableLinearProbingElement.spec.ts +28 -0
- package/src/HashTableLinearProbing/HashTableLinearProbingElement.ts +18 -0
- package/src/HashTableLinearProbing/IHashTableLinearProbing.ts +11 -0
- package/src/HashTableLinearProbing/IHashTableLinearProbingData.ts +7 -0
- package/src/HashTableLinearProbing/IHashTableLinearProbingInputs.ts +5 -0
- package/src/HashTableLinearProbing/IHashTableLinearProbingOptions.ts +5 -0
- package/src/HashTableLinearProbing/index.spec.ts +266 -0
- package/src/HashTableLinearProbing/index.ts +128 -0
- package/src/HashTableSeparateChaining/HashTableSeparateChainingElement.ts +21 -0
- package/src/HashTableSeparateChaining/IHashTableSeparateChaining.ts +12 -0
- package/src/HashTableSeparateChaining/IHashTableSeparateChainingData.ts +7 -0
- package/src/HashTableSeparateChaining/IHashTableSeparateChainingInputs.ts +5 -0
- package/src/HashTableSeparateChaining/IHashTableSeparateChainingOptions.ts +5 -0
- package/src/HashTableSeparateChaining/index.spec.ts +173 -0
- package/src/HashTableSeparateChaining/index.ts +104 -0
- package/src/LinkedList/ILinkedList.ts +15 -0
- package/src/LinkedList/ILinkedListItem.ts +6 -0
- package/src/LinkedList/Node.ts +8 -0
- package/src/LinkedList/index.spec.ts +355 -0
- package/src/LinkedList/index.ts +185 -0
- package/src/List/index.ts +1 -0
- package/src/MaxHeap/IMaxHeap.ts +10 -0
- package/src/MaxHeap/IMaxHeapOptions.ts +6 -0
- package/src/MaxHeap/index.spec.ts +161 -0
- package/src/MaxHeap/index.ts +91 -0
- package/src/MinHeap/IMinHeap.ts +10 -0
- package/src/MinHeap/IMinHeapOptions.ts +6 -0
- package/src/MinHeap/index.spec.ts +161 -0
- package/src/MinHeap/index.ts +91 -0
- package/src/Queue/IQueue.ts +9 -0
- package/src/Queue/index.spec.ts +92 -0
- package/src/Queue/index.ts +44 -0
- package/src/Set/ISet.ts +17 -0
- package/src/Set/index.spec.ts +246 -0
- package/src/Set/index.ts +134 -0
- package/src/Stack/IStack.ts +9 -0
- package/src/Stack/index.spec.ts +108 -0
- package/src/Stack/index.ts +43 -0
- package/src/index.ts +17 -0
- package/tsconfig.json +104 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
var HashTableSeparateChainingElement_1 = __importDefault(require("./HashTableSeparateChainingElement"));
|
|
18
|
+
var LinkedList_1 = __importDefault(require("../LinkedList"));
|
|
19
|
+
var HashTableSeparateChaining = /** @class */ (function () {
|
|
20
|
+
function HashTableSeparateChaining(inputs, _a) {
|
|
21
|
+
if (inputs === void 0) { inputs = {}; }
|
|
22
|
+
var _b = _a === void 0 ? { maxSize: 100 } : _a, maxSize = _b.maxSize;
|
|
23
|
+
this._maxSize = maxSize;
|
|
24
|
+
this._data = new Array(this._maxSize);
|
|
25
|
+
for (var _i = 0, _c = Object.entries(inputs); _i < _c.length; _i++) {
|
|
26
|
+
var _d = _c[_i], key = _d[0], value = _d[1];
|
|
27
|
+
this.put(key, value);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
Object.defineProperty(HashTableSeparateChaining.prototype, "data", {
|
|
31
|
+
get: function () {
|
|
32
|
+
return this._data
|
|
33
|
+
.map(function (value, key) { return ({ key: key, value: value }); })
|
|
34
|
+
.reduce(function (accumulator, current) {
|
|
35
|
+
var _a;
|
|
36
|
+
return (__assign(__assign({}, accumulator), (_a = {}, _a[current.key] = current.value, _a)));
|
|
37
|
+
}, {});
|
|
38
|
+
},
|
|
39
|
+
enumerable: false,
|
|
40
|
+
configurable: true
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(HashTableSeparateChaining.prototype, "size", {
|
|
43
|
+
get: function () {
|
|
44
|
+
return Reflect.ownKeys(this.data).length;
|
|
45
|
+
},
|
|
46
|
+
enumerable: false,
|
|
47
|
+
configurable: true
|
|
48
|
+
});
|
|
49
|
+
HashTableSeparateChaining.prototype.put = function (key, value) {
|
|
50
|
+
var linkedList = this._getLinkedListByKey(key);
|
|
51
|
+
var element = new HashTableSeparateChainingElement_1.default(key, value);
|
|
52
|
+
if (linkedList) {
|
|
53
|
+
linkedList.push(element);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
var position = this._hashCode(key);
|
|
57
|
+
this._data[position] = new LinkedList_1.default(element);
|
|
58
|
+
}
|
|
59
|
+
return value;
|
|
60
|
+
};
|
|
61
|
+
HashTableSeparateChaining.prototype.get = function (key) {
|
|
62
|
+
var linkedList = this._getLinkedListByKey(key);
|
|
63
|
+
if (linkedList) {
|
|
64
|
+
var element = linkedList.data.find(function (element) { return element.value.key === key; });
|
|
65
|
+
return (element === null || element === void 0 ? void 0 : element.value) || null;
|
|
66
|
+
}
|
|
67
|
+
return null;
|
|
68
|
+
};
|
|
69
|
+
HashTableSeparateChaining.prototype.remove = function (key) {
|
|
70
|
+
var linkedList = this._getLinkedListByKey(key);
|
|
71
|
+
if (linkedList) {
|
|
72
|
+
var index = linkedList.data.findIndex(function (element) { return element.value.key === key; });
|
|
73
|
+
var element = linkedList.removeFromPosition(index);
|
|
74
|
+
if (linkedList.isEmpty) {
|
|
75
|
+
var position = this._hashCode(key);
|
|
76
|
+
Reflect.deleteProperty(this._data, position);
|
|
77
|
+
}
|
|
78
|
+
return element || null;
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
};
|
|
82
|
+
HashTableSeparateChaining.prototype._getLinkedListByKey = function (key) {
|
|
83
|
+
var position = this._hashCode(key);
|
|
84
|
+
return Reflect.get(this.data, position) || null;
|
|
85
|
+
};
|
|
86
|
+
HashTableSeparateChaining.prototype._hashCode = function (key) {
|
|
87
|
+
var code = key
|
|
88
|
+
.split('')
|
|
89
|
+
.map(function (character) { return character.charCodeAt(0); })
|
|
90
|
+
.reduce(function (previous, current) { return previous + current; }, 0);
|
|
91
|
+
return code % this._maxSize;
|
|
92
|
+
};
|
|
93
|
+
HashTableSeparateChaining.prototype[Symbol.toPrimitive] = function (type) {
|
|
94
|
+
if (type === 'string') {
|
|
95
|
+
return "[ ".concat(Object.values(this.data).map(String).join(', '), " ]");
|
|
96
|
+
}
|
|
97
|
+
if (type === 'number') {
|
|
98
|
+
return this.size;
|
|
99
|
+
}
|
|
100
|
+
return null;
|
|
101
|
+
};
|
|
102
|
+
return HashTableSeparateChaining;
|
|
103
|
+
}());
|
|
104
|
+
exports.default = HashTableSeparateChaining;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import ILinkedListItem from './ILinkedListItem';
|
|
2
|
+
interface ILinkedList<T> {
|
|
3
|
+
get data(): ILinkedListItem<T>[];
|
|
4
|
+
get size(): number;
|
|
5
|
+
get isEmpty(): boolean;
|
|
6
|
+
push(_element: T): T;
|
|
7
|
+
getFromPosition(_position: number): ILinkedListItem<T> | undefined;
|
|
8
|
+
positionOf(_element: T): number | undefined;
|
|
9
|
+
insertInPosition(_element: T, _position: number): T | undefined;
|
|
10
|
+
remove(_element: T): T | undefined;
|
|
11
|
+
removeFromPosition(_position: number): T | undefined;
|
|
12
|
+
}
|
|
13
|
+
export default ILinkedList;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import ILinkedList from './ILinkedList';
|
|
2
|
+
export default class LinkedList<T = number> implements ILinkedList<T> {
|
|
3
|
+
private readonly _FIRST_POSITION;
|
|
4
|
+
private _head;
|
|
5
|
+
private _size;
|
|
6
|
+
constructor(...inputs: T[]);
|
|
7
|
+
get data(): {
|
|
8
|
+
value: T;
|
|
9
|
+
next: T | null;
|
|
10
|
+
}[];
|
|
11
|
+
get size(): number;
|
|
12
|
+
get isEmpty(): boolean;
|
|
13
|
+
positionOf(element: T): number | undefined;
|
|
14
|
+
push(element: T): T;
|
|
15
|
+
remove(element: T): T | undefined;
|
|
16
|
+
insertInPosition(element: T, position: number): T | undefined;
|
|
17
|
+
getFromPosition(position: number): {
|
|
18
|
+
value: T;
|
|
19
|
+
next: T | null;
|
|
20
|
+
} | undefined;
|
|
21
|
+
removeFromPosition(position: number): T | undefined;
|
|
22
|
+
private getNodeFromPosition;
|
|
23
|
+
private [Symbol.toPrimitive];
|
|
24
|
+
}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
var Node_1 = __importDefault(require("./Node"));
|
|
7
|
+
var LinkedList = /** @class */ (function () {
|
|
8
|
+
function LinkedList() {
|
|
9
|
+
var inputs = [];
|
|
10
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
11
|
+
inputs[_i] = arguments[_i];
|
|
12
|
+
}
|
|
13
|
+
this._FIRST_POSITION = 0;
|
|
14
|
+
this._head = null;
|
|
15
|
+
this._size = 0;
|
|
16
|
+
if (inputs.length) {
|
|
17
|
+
var nodes = inputs.map(function (input) { return new Node_1.default(input); });
|
|
18
|
+
for (var i = 0; i < inputs.length - 1; i++) {
|
|
19
|
+
nodes[i].next = nodes[i + 1];
|
|
20
|
+
}
|
|
21
|
+
this._head = nodes[0];
|
|
22
|
+
this._size = nodes.length;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
Object.defineProperty(LinkedList.prototype, "data", {
|
|
26
|
+
get: function () {
|
|
27
|
+
var _a;
|
|
28
|
+
var data = [];
|
|
29
|
+
var current = this._head;
|
|
30
|
+
while (current) {
|
|
31
|
+
data.push({
|
|
32
|
+
value: current.value,
|
|
33
|
+
next: ((_a = current.next) === null || _a === void 0 ? void 0 : _a.value) || null,
|
|
34
|
+
});
|
|
35
|
+
current = current.next;
|
|
36
|
+
}
|
|
37
|
+
return data;
|
|
38
|
+
},
|
|
39
|
+
enumerable: false,
|
|
40
|
+
configurable: true
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(LinkedList.prototype, "size", {
|
|
43
|
+
get: function () {
|
|
44
|
+
return this._size;
|
|
45
|
+
},
|
|
46
|
+
enumerable: false,
|
|
47
|
+
configurable: true
|
|
48
|
+
});
|
|
49
|
+
Object.defineProperty(LinkedList.prototype, "isEmpty", {
|
|
50
|
+
get: function () {
|
|
51
|
+
return this.size === 0;
|
|
52
|
+
},
|
|
53
|
+
enumerable: false,
|
|
54
|
+
configurable: true
|
|
55
|
+
});
|
|
56
|
+
LinkedList.prototype.positionOf = function (element) {
|
|
57
|
+
var current = this._head;
|
|
58
|
+
var position = 0;
|
|
59
|
+
while (current && current.value !== element) {
|
|
60
|
+
current = current === null || current === void 0 ? void 0 : current.next;
|
|
61
|
+
position += 1;
|
|
62
|
+
}
|
|
63
|
+
if (current) {
|
|
64
|
+
return position;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
LinkedList.prototype.push = function (element) {
|
|
68
|
+
if (this._head) {
|
|
69
|
+
var current = this._head;
|
|
70
|
+
while (current.next) {
|
|
71
|
+
current = current.next;
|
|
72
|
+
}
|
|
73
|
+
current.next = new Node_1.default(element);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
this._head = new Node_1.default(element);
|
|
77
|
+
}
|
|
78
|
+
this._size += 1;
|
|
79
|
+
return element;
|
|
80
|
+
};
|
|
81
|
+
LinkedList.prototype.remove = function (element) {
|
|
82
|
+
var position = this.positionOf(element);
|
|
83
|
+
if (position) {
|
|
84
|
+
return this.removeFromPosition(position);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
LinkedList.prototype.insertInPosition = function (element, position) {
|
|
88
|
+
if (position < this._FIRST_POSITION || position > this.size) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
var node = new Node_1.default(element);
|
|
92
|
+
if (position === this._FIRST_POSITION) {
|
|
93
|
+
node.next = this._head;
|
|
94
|
+
this._head = node;
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
var before = this.getNodeFromPosition(position - 1);
|
|
98
|
+
var after = (before && before.next) || null;
|
|
99
|
+
if (before) {
|
|
100
|
+
before.next = node;
|
|
101
|
+
}
|
|
102
|
+
node.next = after;
|
|
103
|
+
}
|
|
104
|
+
this._size += 1;
|
|
105
|
+
return element;
|
|
106
|
+
};
|
|
107
|
+
LinkedList.prototype.getFromPosition = function (position) {
|
|
108
|
+
var _a;
|
|
109
|
+
if (position < this._FIRST_POSITION || position > this.size - 1) {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
var node = this._head;
|
|
113
|
+
for (var i = 0; i < position; i++) {
|
|
114
|
+
node = (node === null || node === void 0 ? void 0 : node.next) || null;
|
|
115
|
+
}
|
|
116
|
+
if (node === null || node === void 0 ? void 0 : node.value) {
|
|
117
|
+
return {
|
|
118
|
+
value: node.value,
|
|
119
|
+
next: ((_a = node.next) === null || _a === void 0 ? void 0 : _a.value) || null,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
return undefined;
|
|
123
|
+
};
|
|
124
|
+
LinkedList.prototype.removeFromPosition = function (position) {
|
|
125
|
+
if (position < this._FIRST_POSITION || position > (this.size - 1)) {
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
var current = this._head;
|
|
129
|
+
if (position === this._FIRST_POSITION) {
|
|
130
|
+
this._head = (current === null || current === void 0 ? void 0 : current.next) || null;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
var previous = void 0;
|
|
134
|
+
for (var i = 0; i < position; i++) {
|
|
135
|
+
previous = current;
|
|
136
|
+
current = (current === null || current === void 0 ? void 0 : current.next) || null;
|
|
137
|
+
}
|
|
138
|
+
if (previous) {
|
|
139
|
+
previous.next = (current === null || current === void 0 ? void 0 : current.next) || null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
this._size -= 1;
|
|
143
|
+
return current === null || current === void 0 ? void 0 : current.value;
|
|
144
|
+
};
|
|
145
|
+
LinkedList.prototype.getNodeFromPosition = function (position) {
|
|
146
|
+
if (position < this._FIRST_POSITION || position > this.size - 1) {
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
var node = this._head;
|
|
150
|
+
for (var i = 0; i < position; i++) {
|
|
151
|
+
node = (node === null || node === void 0 ? void 0 : node.next) || null;
|
|
152
|
+
}
|
|
153
|
+
return node || undefined;
|
|
154
|
+
};
|
|
155
|
+
LinkedList.prototype[Symbol.toPrimitive] = function (type) {
|
|
156
|
+
if (type === 'string') {
|
|
157
|
+
return "[Head] ".concat(this.data.map(function (_a) {
|
|
158
|
+
var value = _a.value;
|
|
159
|
+
return value;
|
|
160
|
+
}).join(' => '));
|
|
161
|
+
}
|
|
162
|
+
if (type === 'number') {
|
|
163
|
+
return this.size;
|
|
164
|
+
}
|
|
165
|
+
return null;
|
|
166
|
+
};
|
|
167
|
+
return LinkedList;
|
|
168
|
+
}());
|
|
169
|
+
exports.default = LinkedList;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default } from '../Array';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var Array_1 = require("../Array");
|
|
8
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return __importDefault(Array_1).default; } });
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import IMaxHeap from './IMaxHeap';
|
|
2
|
+
import IMaxHeapOptions from './IMaxHeapOptions';
|
|
3
|
+
export default class MaxHeap<T = number> implements IMaxHeap<T> {
|
|
4
|
+
private _data;
|
|
5
|
+
constructor({ greaterThanOrEqualTo, inputs }?: IMaxHeapOptions<T>);
|
|
6
|
+
get data(): T[];
|
|
7
|
+
get size(): number;
|
|
8
|
+
get isEmpty(): boolean;
|
|
9
|
+
get max(): T | null;
|
|
10
|
+
insert(value: T): T;
|
|
11
|
+
extract(): T | null;
|
|
12
|
+
private _greaterThanOrEqualTo;
|
|
13
|
+
private _getLeftIndex;
|
|
14
|
+
private _getRightIndex;
|
|
15
|
+
private _getParentIndex;
|
|
16
|
+
private _siftUp;
|
|
17
|
+
private _siftDown;
|
|
18
|
+
private [Symbol.toPrimitive];
|
|
19
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
3
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
4
|
+
if (ar || !(i in from)) {
|
|
5
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
6
|
+
ar[i] = from[i];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
var MaxHeap = /** @class */ (function () {
|
|
13
|
+
function MaxHeap(_a) {
|
|
14
|
+
var _b = _a === void 0 ? { inputs: [] } : _a, greaterThanOrEqualTo = _b.greaterThanOrEqualTo, _c = _b.inputs, inputs = _c === void 0 ? [] : _c;
|
|
15
|
+
this._data = [];
|
|
16
|
+
this._greaterThanOrEqualTo = function (value1, value2) { return value1 >= value2; };
|
|
17
|
+
this._getLeftIndex = function (index) { return (2 * index) + 1; };
|
|
18
|
+
this._getRightIndex = function (index) { return (2 * index) + 2; };
|
|
19
|
+
this._getParentIndex = function (index) { return Math.floor((index - 1) / 2); };
|
|
20
|
+
if (greaterThanOrEqualTo) {
|
|
21
|
+
this._greaterThanOrEqualTo = greaterThanOrEqualTo;
|
|
22
|
+
}
|
|
23
|
+
for (var _i = 0, inputs_1 = inputs; _i < inputs_1.length; _i++) {
|
|
24
|
+
var input = inputs_1[_i];
|
|
25
|
+
this.insert(input);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
Object.defineProperty(MaxHeap.prototype, "data", {
|
|
29
|
+
get: function () {
|
|
30
|
+
return __spreadArray([], this._data, true);
|
|
31
|
+
},
|
|
32
|
+
enumerable: false,
|
|
33
|
+
configurable: true
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(MaxHeap.prototype, "size", {
|
|
36
|
+
get: function () {
|
|
37
|
+
return this.data.length;
|
|
38
|
+
},
|
|
39
|
+
enumerable: false,
|
|
40
|
+
configurable: true
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(MaxHeap.prototype, "isEmpty", {
|
|
43
|
+
get: function () {
|
|
44
|
+
return this.size === 0;
|
|
45
|
+
},
|
|
46
|
+
enumerable: false,
|
|
47
|
+
configurable: true
|
|
48
|
+
});
|
|
49
|
+
Object.defineProperty(MaxHeap.prototype, "max", {
|
|
50
|
+
get: function () {
|
|
51
|
+
return this.data[0] || null;
|
|
52
|
+
},
|
|
53
|
+
enumerable: false,
|
|
54
|
+
configurable: true
|
|
55
|
+
});
|
|
56
|
+
MaxHeap.prototype.insert = function (value) {
|
|
57
|
+
this._data = __spreadArray(__spreadArray([], this.data, true), [value], false);
|
|
58
|
+
this._siftUp(this.size - 1);
|
|
59
|
+
return value;
|
|
60
|
+
};
|
|
61
|
+
MaxHeap.prototype.extract = function () {
|
|
62
|
+
if (!this.isEmpty) {
|
|
63
|
+
var _a = this.data, min = _a[0], rest = _a.slice(1);
|
|
64
|
+
this._data = __spreadArray([rest[rest.length - 1]], rest.slice(0, rest.length - 1), true).filter(function (value) { return value !== undefined; });
|
|
65
|
+
this._siftDown(0);
|
|
66
|
+
return min;
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
};
|
|
70
|
+
MaxHeap.prototype._siftUp = function (index) {
|
|
71
|
+
var _a;
|
|
72
|
+
var parent = this._getParentIndex(index);
|
|
73
|
+
if (index > 0 && this._greaterThanOrEqualTo(this.data[index], this.data[parent])) {
|
|
74
|
+
_a = [this.data[index], this.data[parent]], this._data[parent] = _a[0], this._data[index] = _a[1];
|
|
75
|
+
this._siftUp(parent);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
MaxHeap.prototype._siftDown = function (index) {
|
|
79
|
+
var _a, _b;
|
|
80
|
+
var left = this._getLeftIndex(index);
|
|
81
|
+
var right = this._getRightIndex(index);
|
|
82
|
+
if (left < this.size && this._greaterThanOrEqualTo(this.data[left], this.data[index])) {
|
|
83
|
+
_a = [this.data[index], this.data[left]], this._data[left] = _a[0], this._data[index] = _a[1];
|
|
84
|
+
this._siftDown(left);
|
|
85
|
+
}
|
|
86
|
+
if (right < this.size && this._greaterThanOrEqualTo(this.data[right], this.data[index])) {
|
|
87
|
+
_b = [this.data[index], this.data[right]], this._data[right] = _b[0], this._data[index] = _b[1];
|
|
88
|
+
this._siftDown(right);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
MaxHeap.prototype[Symbol.toPrimitive] = function (type) {
|
|
92
|
+
if (type === 'string') {
|
|
93
|
+
return this.data.join(', ');
|
|
94
|
+
}
|
|
95
|
+
if (type === 'number') {
|
|
96
|
+
return this.size;
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
};
|
|
100
|
+
return MaxHeap;
|
|
101
|
+
}());
|
|
102
|
+
exports.default = MaxHeap;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import IMinHeap from './IMinHeap';
|
|
2
|
+
import IMinHeapOptions from './IMinHeapOptions';
|
|
3
|
+
export default class MinHeap<T = number> implements IMinHeap<T> {
|
|
4
|
+
private _data;
|
|
5
|
+
constructor({ lessThanOrEqualTo, inputs }?: IMinHeapOptions<T>);
|
|
6
|
+
get data(): T[];
|
|
7
|
+
get size(): number;
|
|
8
|
+
get isEmpty(): boolean;
|
|
9
|
+
get min(): T | null;
|
|
10
|
+
insert(value: T): T;
|
|
11
|
+
extract(): T | null;
|
|
12
|
+
private _lessThanOrEqualTo;
|
|
13
|
+
private _getLeftIndex;
|
|
14
|
+
private _getRightIndex;
|
|
15
|
+
private _getParentIndex;
|
|
16
|
+
private _siftUp;
|
|
17
|
+
private _siftDown;
|
|
18
|
+
private [Symbol.toPrimitive];
|
|
19
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
3
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
4
|
+
if (ar || !(i in from)) {
|
|
5
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
6
|
+
ar[i] = from[i];
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
var MinHeap = /** @class */ (function () {
|
|
13
|
+
function MinHeap(_a) {
|
|
14
|
+
var _b = _a === void 0 ? { inputs: [] } : _a, lessThanOrEqualTo = _b.lessThanOrEqualTo, _c = _b.inputs, inputs = _c === void 0 ? [] : _c;
|
|
15
|
+
this._data = [];
|
|
16
|
+
this._lessThanOrEqualTo = function (value1, value2) { return value1 <= value2; };
|
|
17
|
+
this._getLeftIndex = function (index) { return (2 * index) + 1; };
|
|
18
|
+
this._getRightIndex = function (index) { return (2 * index) + 2; };
|
|
19
|
+
this._getParentIndex = function (index) { return Math.floor((index - 1) / 2); };
|
|
20
|
+
if (lessThanOrEqualTo) {
|
|
21
|
+
this._lessThanOrEqualTo = lessThanOrEqualTo;
|
|
22
|
+
}
|
|
23
|
+
for (var _i = 0, inputs_1 = inputs; _i < inputs_1.length; _i++) {
|
|
24
|
+
var input = inputs_1[_i];
|
|
25
|
+
this.insert(input);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
Object.defineProperty(MinHeap.prototype, "data", {
|
|
29
|
+
get: function () {
|
|
30
|
+
return __spreadArray([], this._data, true);
|
|
31
|
+
},
|
|
32
|
+
enumerable: false,
|
|
33
|
+
configurable: true
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(MinHeap.prototype, "size", {
|
|
36
|
+
get: function () {
|
|
37
|
+
return this.data.length;
|
|
38
|
+
},
|
|
39
|
+
enumerable: false,
|
|
40
|
+
configurable: true
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(MinHeap.prototype, "isEmpty", {
|
|
43
|
+
get: function () {
|
|
44
|
+
return this.size === 0;
|
|
45
|
+
},
|
|
46
|
+
enumerable: false,
|
|
47
|
+
configurable: true
|
|
48
|
+
});
|
|
49
|
+
Object.defineProperty(MinHeap.prototype, "min", {
|
|
50
|
+
get: function () {
|
|
51
|
+
return this.data[0] || null;
|
|
52
|
+
},
|
|
53
|
+
enumerable: false,
|
|
54
|
+
configurable: true
|
|
55
|
+
});
|
|
56
|
+
MinHeap.prototype.insert = function (value) {
|
|
57
|
+
this._data = __spreadArray(__spreadArray([], this.data, true), [value], false);
|
|
58
|
+
this._siftUp(this.size - 1);
|
|
59
|
+
return value;
|
|
60
|
+
};
|
|
61
|
+
MinHeap.prototype.extract = function () {
|
|
62
|
+
if (!this.isEmpty) {
|
|
63
|
+
var _a = this.data, min = _a[0], rest = _a.slice(1);
|
|
64
|
+
this._data = __spreadArray([rest[rest.length - 1]], rest.slice(0, rest.length - 1), true).filter(function (value) { return value !== undefined; });
|
|
65
|
+
this._siftDown(0);
|
|
66
|
+
return min;
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
};
|
|
70
|
+
MinHeap.prototype._siftUp = function (index) {
|
|
71
|
+
var _a;
|
|
72
|
+
var parent = this._getParentIndex(index);
|
|
73
|
+
if (index > 0 && this._lessThanOrEqualTo(this.data[index], this.data[parent])) {
|
|
74
|
+
_a = [this.data[index], this.data[parent]], this._data[parent] = _a[0], this._data[index] = _a[1];
|
|
75
|
+
this._siftUp(parent);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
MinHeap.prototype._siftDown = function (index) {
|
|
79
|
+
var _a, _b;
|
|
80
|
+
var left = this._getLeftIndex(index);
|
|
81
|
+
var right = this._getRightIndex(index);
|
|
82
|
+
if (left < this.size && this._lessThanOrEqualTo(this.data[left], this.data[index])) {
|
|
83
|
+
_a = [this.data[index], this.data[left]], this._data[left] = _a[0], this._data[index] = _a[1];
|
|
84
|
+
this._siftDown(left);
|
|
85
|
+
}
|
|
86
|
+
if (right < this.size && this._lessThanOrEqualTo(this.data[right], this.data[index])) {
|
|
87
|
+
_b = [this.data[index], this.data[right]], this._data[right] = _b[0], this._data[index] = _b[1];
|
|
88
|
+
this._siftDown(right);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
MinHeap.prototype[Symbol.toPrimitive] = function (type) {
|
|
92
|
+
if (type === 'string') {
|
|
93
|
+
return this.data.join(', ');
|
|
94
|
+
}
|
|
95
|
+
if (type === 'number') {
|
|
96
|
+
return this.size;
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
};
|
|
100
|
+
return MinHeap;
|
|
101
|
+
}());
|
|
102
|
+
exports.default = MinHeap;
|