@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,110 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
var Dictionary = /** @class */ (function () {
|
|
15
|
+
function Dictionary(inputs) {
|
|
16
|
+
if (inputs === void 0) { inputs = {}; }
|
|
17
|
+
this._data = {};
|
|
18
|
+
this._data = inputs;
|
|
19
|
+
}
|
|
20
|
+
Object.defineProperty(Dictionary.prototype, "data", {
|
|
21
|
+
get: function () {
|
|
22
|
+
return __assign({}, this._data);
|
|
23
|
+
},
|
|
24
|
+
enumerable: false,
|
|
25
|
+
configurable: true
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(Dictionary.prototype, "size", {
|
|
28
|
+
get: function () {
|
|
29
|
+
return Reflect.ownKeys(this.data).length;
|
|
30
|
+
},
|
|
31
|
+
enumerable: false,
|
|
32
|
+
configurable: true
|
|
33
|
+
});
|
|
34
|
+
Object.defineProperty(Dictionary.prototype, "isEmpty", {
|
|
35
|
+
get: function () {
|
|
36
|
+
return this.size === 0;
|
|
37
|
+
},
|
|
38
|
+
enumerable: false,
|
|
39
|
+
configurable: true
|
|
40
|
+
});
|
|
41
|
+
Object.defineProperty(Dictionary.prototype, "keys", {
|
|
42
|
+
get: function () {
|
|
43
|
+
return Reflect
|
|
44
|
+
.ownKeys(this.data)
|
|
45
|
+
.map(function (key) { return String(key); });
|
|
46
|
+
},
|
|
47
|
+
enumerable: false,
|
|
48
|
+
configurable: true
|
|
49
|
+
});
|
|
50
|
+
Object.defineProperty(Dictionary.prototype, "values", {
|
|
51
|
+
get: function () {
|
|
52
|
+
return Object.values(this.data);
|
|
53
|
+
},
|
|
54
|
+
enumerable: false,
|
|
55
|
+
configurable: true
|
|
56
|
+
});
|
|
57
|
+
Object.defineProperty(Dictionary.prototype, "pairs", {
|
|
58
|
+
get: function () {
|
|
59
|
+
return Object.entries(this.data);
|
|
60
|
+
},
|
|
61
|
+
enumerable: false,
|
|
62
|
+
configurable: true
|
|
63
|
+
});
|
|
64
|
+
Dictionary.prototype.set = function (key, value) {
|
|
65
|
+
Reflect.set(this._data, key, value);
|
|
66
|
+
return [key, value];
|
|
67
|
+
};
|
|
68
|
+
Dictionary.prototype.remove = function (key) {
|
|
69
|
+
if (this.hasKey(key)) {
|
|
70
|
+
var value = Reflect.get(this.data, key);
|
|
71
|
+
Reflect.deleteProperty(this._data, key);
|
|
72
|
+
return [key, value];
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
};
|
|
76
|
+
Dictionary.prototype.hasKey = function (key) {
|
|
77
|
+
return Reflect.has(this.data, key);
|
|
78
|
+
};
|
|
79
|
+
Dictionary.prototype.get = function (key) {
|
|
80
|
+
if (this.hasKey(key)) {
|
|
81
|
+
return Reflect.get(this.data, key);
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
};
|
|
85
|
+
Dictionary.prototype.clear = function () {
|
|
86
|
+
var dictionary = __assign({}, this.data);
|
|
87
|
+
this._data = {};
|
|
88
|
+
return dictionary;
|
|
89
|
+
};
|
|
90
|
+
Dictionary.prototype.forEach = function (func) {
|
|
91
|
+
for (var _i = 0, _a = this.pairs; _i < _a.length; _i++) {
|
|
92
|
+
var _b = _a[_i], key = _b[0], value = _b[1];
|
|
93
|
+
func(key, value);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
Dictionary.prototype[Symbol.toPrimitive] = function (type) {
|
|
97
|
+
if (type === 'string') {
|
|
98
|
+
return "{ ".concat(this.pairs.map(function (_a) {
|
|
99
|
+
var key = _a[0], value = _a[1];
|
|
100
|
+
return "".concat(key, " => ").concat(value);
|
|
101
|
+
}).join(', '), " }");
|
|
102
|
+
}
|
|
103
|
+
if (type === 'number') {
|
|
104
|
+
return this.size;
|
|
105
|
+
}
|
|
106
|
+
return null;
|
|
107
|
+
};
|
|
108
|
+
return Dictionary;
|
|
109
|
+
}());
|
|
110
|
+
exports.default = Dictionary;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface IDoublyLinkedListItem<T> {
|
|
2
|
+
previous: T | null;
|
|
3
|
+
value: T;
|
|
4
|
+
next: T | null;
|
|
5
|
+
}
|
|
6
|
+
interface IDoublyLinkedList<T> {
|
|
7
|
+
get data(): IDoublyLinkedListItem<T>[];
|
|
8
|
+
get size(): number;
|
|
9
|
+
push(_element: T): T;
|
|
10
|
+
getFromPosition(_position: number): IDoublyLinkedListItem<T> | undefined;
|
|
11
|
+
positionOf(_element: T): number | undefined;
|
|
12
|
+
insertInPosition(_element: T, _position: number): T | undefined;
|
|
13
|
+
remove(_element: T): T | undefined;
|
|
14
|
+
removeFromPosition(_position: number): T | undefined;
|
|
15
|
+
}
|
|
16
|
+
export default IDoublyLinkedList;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var Node = /** @class */ (function () {
|
|
4
|
+
function Node(value) {
|
|
5
|
+
this.previous = null;
|
|
6
|
+
this.next = null;
|
|
7
|
+
this.value = value;
|
|
8
|
+
}
|
|
9
|
+
return Node;
|
|
10
|
+
}());
|
|
11
|
+
exports.default = Node;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import IDoublyLinkedList, { IDoublyLinkedListItem } from './IDoublyLinkedList';
|
|
2
|
+
export default class DoublyLinkedList<T = number> implements IDoublyLinkedList<T> {
|
|
3
|
+
private _head;
|
|
4
|
+
private _tail;
|
|
5
|
+
private _size;
|
|
6
|
+
constructor(...inputs: T[]);
|
|
7
|
+
get data(): IDoublyLinkedListItem<T>[];
|
|
8
|
+
get size(): number;
|
|
9
|
+
/**
|
|
10
|
+
* Complexity: O(1)
|
|
11
|
+
*/
|
|
12
|
+
push(element: T): T;
|
|
13
|
+
/**
|
|
14
|
+
* Complexity: O(n/2)
|
|
15
|
+
*/
|
|
16
|
+
getFromPosition(position: number): IDoublyLinkedListItem<T> | undefined;
|
|
17
|
+
positionOf(element: T): number | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* Complexity: O(n)
|
|
20
|
+
*/
|
|
21
|
+
insertInPosition(element: T, position: number): T | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Complexity: O(n)
|
|
24
|
+
*/
|
|
25
|
+
remove(element: T): T | undefined;
|
|
26
|
+
/**
|
|
27
|
+
* Complexity: O(n)
|
|
28
|
+
*/
|
|
29
|
+
removeFromPosition(position: number): T | undefined;
|
|
30
|
+
private _findFasterWayToPosition;
|
|
31
|
+
private [Symbol.toPrimitive];
|
|
32
|
+
}
|
|
@@ -0,0 +1,221 @@
|
|
|
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 DoublyLinkedList = /** @class */ (function () {
|
|
8
|
+
function DoublyLinkedList() {
|
|
9
|
+
var inputs = [];
|
|
10
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
11
|
+
inputs[_i] = arguments[_i];
|
|
12
|
+
}
|
|
13
|
+
this._head = null;
|
|
14
|
+
this._tail = 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; i++) {
|
|
19
|
+
nodes[i].previous = nodes[i - 1] || null;
|
|
20
|
+
nodes[i].next = nodes[i + 1] || null;
|
|
21
|
+
}
|
|
22
|
+
this._head = nodes[0];
|
|
23
|
+
this._tail = nodes[nodes.length - 1];
|
|
24
|
+
this._size = inputs.length;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
Object.defineProperty(DoublyLinkedList.prototype, "data", {
|
|
28
|
+
get: function () {
|
|
29
|
+
var _a, _b;
|
|
30
|
+
var current = this._head;
|
|
31
|
+
var data = [];
|
|
32
|
+
while (current) {
|
|
33
|
+
data.push({
|
|
34
|
+
previous: ((_a = current.previous) === null || _a === void 0 ? void 0 : _a.value) || null,
|
|
35
|
+
value: current.value,
|
|
36
|
+
next: ((_b = current.next) === null || _b === void 0 ? void 0 : _b.value) || null,
|
|
37
|
+
});
|
|
38
|
+
current = current.next;
|
|
39
|
+
}
|
|
40
|
+
return data;
|
|
41
|
+
},
|
|
42
|
+
enumerable: false,
|
|
43
|
+
configurable: true
|
|
44
|
+
});
|
|
45
|
+
Object.defineProperty(DoublyLinkedList.prototype, "size", {
|
|
46
|
+
get: function () {
|
|
47
|
+
return this._size;
|
|
48
|
+
},
|
|
49
|
+
enumerable: false,
|
|
50
|
+
configurable: true
|
|
51
|
+
});
|
|
52
|
+
/**
|
|
53
|
+
* Complexity: O(1)
|
|
54
|
+
*/
|
|
55
|
+
DoublyLinkedList.prototype.push = function (element) {
|
|
56
|
+
var node = new Node_1.default(element);
|
|
57
|
+
if (this._tail) {
|
|
58
|
+
this._tail.next = node;
|
|
59
|
+
node.previous = this._tail;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
this._head = node;
|
|
63
|
+
this._tail = node;
|
|
64
|
+
}
|
|
65
|
+
this._size += 1;
|
|
66
|
+
return element;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Complexity: O(n/2)
|
|
70
|
+
*/
|
|
71
|
+
DoublyLinkedList.prototype.getFromPosition = function (position) {
|
|
72
|
+
var _a, _b;
|
|
73
|
+
if (position < 0 || position >= this._size) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
var distanceToTheHead = position;
|
|
77
|
+
var distanceToTheTail = this.size - position - 1;
|
|
78
|
+
var current;
|
|
79
|
+
if (distanceToTheTail > distanceToTheHead) {
|
|
80
|
+
current = this._head;
|
|
81
|
+
for (var i = 0; i < position; i++) {
|
|
82
|
+
current = (current === null || current === void 0 ? void 0 : current.next) || null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
current = this._tail;
|
|
87
|
+
for (var i = this.size - 1; i > position; i--) {
|
|
88
|
+
current = (current === null || current === void 0 ? void 0 : current.previous) || null;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (current === null || current === void 0 ? void 0 : current.value) {
|
|
92
|
+
return {
|
|
93
|
+
previous: ((_a = current === null || current === void 0 ? void 0 : current.previous) === null || _a === void 0 ? void 0 : _a.value) || null,
|
|
94
|
+
value: current === null || current === void 0 ? void 0 : current.value,
|
|
95
|
+
next: ((_b = current === null || current === void 0 ? void 0 : current.next) === null || _b === void 0 ? void 0 : _b.value) || null,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
DoublyLinkedList.prototype.positionOf = function (element) {
|
|
100
|
+
var current = this._head;
|
|
101
|
+
var position = 0;
|
|
102
|
+
while (current && current.value !== element) {
|
|
103
|
+
current = current.next;
|
|
104
|
+
position += 1;
|
|
105
|
+
}
|
|
106
|
+
return position === this.size ? undefined : position;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Complexity: O(n)
|
|
110
|
+
*/
|
|
111
|
+
DoublyLinkedList.prototype.insertInPosition = function (element, position) {
|
|
112
|
+
var _a;
|
|
113
|
+
if (position < 0 || position > this.size) {
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
var node = new Node_1.default(element);
|
|
117
|
+
if (position === 0 && this._head) {
|
|
118
|
+
node.next = this._head;
|
|
119
|
+
this._head.previous = node;
|
|
120
|
+
this._head = node;
|
|
121
|
+
this._size += 1;
|
|
122
|
+
return element;
|
|
123
|
+
}
|
|
124
|
+
if (position === this.size - 1 && this._tail && this._tail.previous) {
|
|
125
|
+
this._tail.previous.next = node;
|
|
126
|
+
node.previous = ((_a = this._tail) === null || _a === void 0 ? void 0 : _a.previous) || null;
|
|
127
|
+
node.next = this._tail;
|
|
128
|
+
this._tail.previous = node;
|
|
129
|
+
this._size += 1;
|
|
130
|
+
return element;
|
|
131
|
+
}
|
|
132
|
+
if (position === this.size && this._tail) {
|
|
133
|
+
node.previous = this._tail;
|
|
134
|
+
this._tail.next = node;
|
|
135
|
+
this._tail = node;
|
|
136
|
+
this._size += 1;
|
|
137
|
+
return element;
|
|
138
|
+
}
|
|
139
|
+
var way = this._findFasterWayToPosition(position);
|
|
140
|
+
var current;
|
|
141
|
+
if (way === 'ASC') {
|
|
142
|
+
current = this._head;
|
|
143
|
+
for (var i = 0; i < position; i++) {
|
|
144
|
+
current = (current === null || current === void 0 ? void 0 : current.next) || null;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
current = this._tail;
|
|
149
|
+
for (var i = this.size - 1; i > position; i--) {
|
|
150
|
+
current = (current === null || current === void 0 ? void 0 : current.previous) || null;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (current && current.previous) {
|
|
154
|
+
current.previous.next = node;
|
|
155
|
+
node.previous = current.previous;
|
|
156
|
+
node.next = current;
|
|
157
|
+
current.previous = node;
|
|
158
|
+
}
|
|
159
|
+
this._size += 1;
|
|
160
|
+
return element;
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* Complexity: O(n)
|
|
164
|
+
*/
|
|
165
|
+
DoublyLinkedList.prototype.remove = function (element) {
|
|
166
|
+
var current = this._head;
|
|
167
|
+
while (current && current.value !== element) {
|
|
168
|
+
current = current.next;
|
|
169
|
+
}
|
|
170
|
+
if (current) {
|
|
171
|
+
if (current.previous) {
|
|
172
|
+
current.previous.next = current.next;
|
|
173
|
+
}
|
|
174
|
+
if (current.next) {
|
|
175
|
+
current.next.previous = current.previous;
|
|
176
|
+
}
|
|
177
|
+
if (current === this._head) {
|
|
178
|
+
this._head = current.next;
|
|
179
|
+
}
|
|
180
|
+
if (current === this._tail) {
|
|
181
|
+
this._tail = current.previous;
|
|
182
|
+
}
|
|
183
|
+
this._size -= 1;
|
|
184
|
+
return current.value;
|
|
185
|
+
}
|
|
186
|
+
return undefined;
|
|
187
|
+
};
|
|
188
|
+
/**
|
|
189
|
+
* Complexity: O(n)
|
|
190
|
+
*/
|
|
191
|
+
DoublyLinkedList.prototype.removeFromPosition = function (position) {
|
|
192
|
+
var item = this.getFromPosition(position);
|
|
193
|
+
if (item) {
|
|
194
|
+
this.remove(item.value);
|
|
195
|
+
return item.value;
|
|
196
|
+
}
|
|
197
|
+
return undefined;
|
|
198
|
+
};
|
|
199
|
+
DoublyLinkedList.prototype._findFasterWayToPosition = function (position) {
|
|
200
|
+
var distanceToTheHead = position;
|
|
201
|
+
var distanceToTheTail = this.size - position - 1;
|
|
202
|
+
if (distanceToTheTail > distanceToTheHead) {
|
|
203
|
+
return 'ASC';
|
|
204
|
+
}
|
|
205
|
+
return 'DESC';
|
|
206
|
+
};
|
|
207
|
+
DoublyLinkedList.prototype[Symbol.toPrimitive] = function (type) {
|
|
208
|
+
if (type === 'string') {
|
|
209
|
+
return "[Head] ".concat(this.data.map(function (_a) {
|
|
210
|
+
var value = _a.value;
|
|
211
|
+
return value;
|
|
212
|
+
}).join(' <=> '), " [Tail]");
|
|
213
|
+
}
|
|
214
|
+
if (type === 'number') {
|
|
215
|
+
return this.size;
|
|
216
|
+
}
|
|
217
|
+
return null;
|
|
218
|
+
};
|
|
219
|
+
return DoublyLinkedList;
|
|
220
|
+
}());
|
|
221
|
+
exports.default = DoublyLinkedList;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
var GraphNodeNotFoundError = /** @class */ (function (_super) {
|
|
19
|
+
__extends(GraphNodeNotFoundError, _super);
|
|
20
|
+
function GraphNodeNotFoundError(node) {
|
|
21
|
+
var _this = _super.call(this, "Node ".concat(node, " not found")) || this;
|
|
22
|
+
_this.name = 'GraphNodeNotFoundError';
|
|
23
|
+
return _this;
|
|
24
|
+
}
|
|
25
|
+
return GraphNodeNotFoundError;
|
|
26
|
+
}(Error));
|
|
27
|
+
exports.default = GraphNodeNotFoundError;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/* eslint-disable no-unused-vars */
|
|
4
|
+
var GraphSearchNodeStates;
|
|
5
|
+
(function (GraphSearchNodeStates) {
|
|
6
|
+
GraphSearchNodeStates[GraphSearchNodeStates["UNEXPLORED"] = 0] = "UNEXPLORED";
|
|
7
|
+
GraphSearchNodeStates[GraphSearchNodeStates["DISCOVERED"] = 1] = "DISCOVERED";
|
|
8
|
+
GraphSearchNodeStates[GraphSearchNodeStates["EXPLORED"] = 2] = "EXPLORED";
|
|
9
|
+
})(GraphSearchNodeStates || (GraphSearchNodeStates = {}));
|
|
10
|
+
exports.default = GraphSearchNodeStates;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
interface IGraph {
|
|
2
|
+
get isDirected(): boolean;
|
|
3
|
+
get data(): {
|
|
4
|
+
[key: string]: string[];
|
|
5
|
+
};
|
|
6
|
+
get size(): number;
|
|
7
|
+
get nodes(): string[];
|
|
8
|
+
get edges(): [string, string][];
|
|
9
|
+
insert(_node: string): string | null;
|
|
10
|
+
connect(_node1: string, _node2: string): [string, string];
|
|
11
|
+
breadthFirstSearch(_startNode: string, _callback: (_node: string) => void): void;
|
|
12
|
+
getDistancesFrom(_node: string): {
|
|
13
|
+
[key: string]: number;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export default IGraph;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import IGraph from './IGraph';
|
|
2
|
+
import IGraphOptions from './IGraphOptions';
|
|
3
|
+
export default class Graph implements IGraph {
|
|
4
|
+
private _isDirected;
|
|
5
|
+
private _data;
|
|
6
|
+
constructor({ inputs, isDirected }?: IGraphOptions);
|
|
7
|
+
get isDirected(): boolean;
|
|
8
|
+
get data(): {
|
|
9
|
+
[key: string]: string[];
|
|
10
|
+
};
|
|
11
|
+
get size(): number;
|
|
12
|
+
get nodes(): string[];
|
|
13
|
+
get edges(): [string, string][];
|
|
14
|
+
insert(node: string): string | null;
|
|
15
|
+
connect(node1: string, node2: string): [string, string];
|
|
16
|
+
breadthFirstSearch(startNode: string, callback: (_node: string) => void): void;
|
|
17
|
+
getDistancesFrom(node: string): {
|
|
18
|
+
[key: string]: number;
|
|
19
|
+
};
|
|
20
|
+
}
|