@gabrielrufino/cube 1.0.1 → 1.0.6
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/package.json +6 -6
- package/.github/workflows/cd.yml +0 -26
- package/.github/workflows/ci.yml +0 -23
- package/coverage/clover.xml +0 -706
- package/coverage/coverage-final.json +0 -19
- package/coverage/lcov-report/base.css +0 -224
- package/coverage/lcov-report/block-navigation.js +0 -87
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +0 -327
- package/coverage/lcov-report/prettify.css +0 -1
- package/coverage/lcov-report/prettify.js +0 -2
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +0 -196
- package/coverage/lcov-report/src/Algorithms/index.html +0 -117
- package/coverage/lcov-report/src/Algorithms/index.ts.html +0 -149
- package/coverage/lcov-report/src/Array/index.html +0 -117
- package/coverage/lcov-report/src/Array/index.ts.html +0 -290
- package/coverage/lcov-report/src/DataStructure/index.html +0 -117
- package/coverage/lcov-report/src/DataStructure/index.ts.html +0 -131
- package/coverage/lcov-report/src/Deck/index.html +0 -117
- package/coverage/lcov-report/src/Deck/index.ts.html +0 -239
- package/coverage/lcov-report/src/Dictionary/index.html +0 -117
- package/coverage/lcov-report/src/Dictionary/index.ts.html +0 -350
- package/coverage/lcov-report/src/DoublyLinkedList/Node.ts.html +0 -113
- package/coverage/lcov-report/src/DoublyLinkedList/index.html +0 -132
- package/coverage/lcov-report/src/DoublyLinkedList/index.ts.html +0 -836
- package/coverage/lcov-report/src/HashTable/index.html +0 -117
- package/coverage/lcov-report/src/HashTable/index.ts.html +0 -320
- package/coverage/lcov-report/src/HashTableLinearProbing/index.html +0 -117
- package/coverage/lcov-report/src/HashTableLinearProbing/index.ts.html +0 -95
- package/coverage/lcov-report/src/HashTableSeparateChaining/HashTableSeparateChainingElement.ts.html +0 -149
- package/coverage/lcov-report/src/HashTableSeparateChaining/index.html +0 -132
- package/coverage/lcov-report/src/HashTableSeparateChaining/index.ts.html +0 -398
- package/coverage/lcov-report/src/LinkedList/Node.ts.html +0 -110
- package/coverage/lcov-report/src/LinkedList/index.html +0 -132
- package/coverage/lcov-report/src/LinkedList/index.ts.html +0 -641
- package/coverage/lcov-report/src/List/index.html +0 -117
- package/coverage/lcov-report/src/List/index.ts.html +0 -89
- package/coverage/lcov-report/src/Queue/index.html +0 -117
- package/coverage/lcov-report/src/Queue/index.ts.html +0 -218
- package/coverage/lcov-report/src/Set/index.html +0 -117
- package/coverage/lcov-report/src/Set/index.ts.html +0 -488
- package/coverage/lcov-report/src/Stack/index.html +0 -117
- package/coverage/lcov-report/src/Stack/index.ts.html +0 -215
- package/coverage/lcov-report/src/index.html +0 -117
- package/coverage/lcov-report/src/index.ts.html +0 -125
- package/coverage/lcov.info +0 -1346
- package/src/Algorithms/index.spec.ts +0 -15
- package/src/Algorithms/index.ts +0 -21
- package/src/Array/IArray.ts +0 -10
- package/src/Array/index.spec.ts +0 -151
- package/src/Array/index.ts +0 -68
- package/src/BinarySearchTree/BinarySearchTreeNode.spec.ts +0 -29
- package/src/BinarySearchTree/BinarySearchTreeNode.ts +0 -23
- package/src/BinarySearchTree/IBinarySearchNodeOptions.ts +0 -6
- package/src/BinarySearchTree/IBinarySearchTree.ts +0 -16
- package/src/BinarySearchTree/IBinarySearchTreeData.ts +0 -9
- package/src/BinarySearchTree/index.spec.ts +0 -486
- package/src/BinarySearchTree/index.ts +0 -198
- package/src/DataStructure/index.spec.ts +0 -25
- package/src/DataStructure/index.ts +0 -15
- package/src/Deck/IDeck.ts +0 -10
- package/src/Deck/index.spec.ts +0 -131
- package/src/Deck/index.ts +0 -51
- package/src/Dictionary/IDictionary.ts +0 -18
- package/src/Dictionary/IDictionaryData.ts +0 -5
- package/src/Dictionary/index.spec.ts +0 -310
- package/src/Dictionary/index.ts +0 -88
- package/src/DoublyLinkedList/IDoublyLinkedList.ts +0 -18
- package/src/DoublyLinkedList/Node.ts +0 -9
- package/src/DoublyLinkedList/index.spec.ts +0 -478
- package/src/DoublyLinkedList/index.ts +0 -250
- package/src/Graph/GraphNodeNotFoundError.ts +0 -7
- package/src/Graph/GraphSearchNodeStates.ts +0 -8
- package/src/Graph/IGraph.ts +0 -15
- package/src/Graph/IGraphOptions.ts +0 -6
- package/src/Graph/index.spec.ts +0 -318
- package/src/Graph/index.ts +0 -170
- package/src/HashTable/IHashTable.ts +0 -11
- package/src/HashTable/IHashTableData.ts +0 -5
- package/src/HashTable/IHashTableInputs.ts +0 -5
- package/src/HashTable/IHashTableOptions.ts +0 -5
- package/src/HashTable/index.spec.ts +0 -136
- package/src/HashTable/index.ts +0 -78
- package/src/HashTableLinearProbing/HashTableLinearProbingElement.spec.ts +0 -28
- package/src/HashTableLinearProbing/HashTableLinearProbingElement.ts +0 -18
- package/src/HashTableLinearProbing/IHashTableLinearProbing.ts +0 -11
- package/src/HashTableLinearProbing/IHashTableLinearProbingData.ts +0 -7
- package/src/HashTableLinearProbing/IHashTableLinearProbingInputs.ts +0 -5
- package/src/HashTableLinearProbing/IHashTableLinearProbingOptions.ts +0 -5
- package/src/HashTableLinearProbing/index.spec.ts +0 -266
- package/src/HashTableLinearProbing/index.ts +0 -128
- package/src/HashTableSeparateChaining/HashTableSeparateChainingElement.ts +0 -21
- package/src/HashTableSeparateChaining/IHashTableSeparateChaining.ts +0 -12
- package/src/HashTableSeparateChaining/IHashTableSeparateChainingData.ts +0 -7
- package/src/HashTableSeparateChaining/IHashTableSeparateChainingInputs.ts +0 -5
- package/src/HashTableSeparateChaining/IHashTableSeparateChainingOptions.ts +0 -5
- package/src/HashTableSeparateChaining/index.spec.ts +0 -173
- package/src/HashTableSeparateChaining/index.ts +0 -104
- package/src/LinkedList/ILinkedList.ts +0 -15
- package/src/LinkedList/ILinkedListItem.ts +0 -6
- package/src/LinkedList/Node.ts +0 -8
- package/src/LinkedList/index.spec.ts +0 -355
- package/src/LinkedList/index.ts +0 -185
- package/src/List/index.ts +0 -1
- package/src/MaxHeap/IMaxHeap.ts +0 -10
- package/src/MaxHeap/IMaxHeapOptions.ts +0 -6
- package/src/MaxHeap/index.spec.ts +0 -161
- package/src/MaxHeap/index.ts +0 -91
- package/src/MinHeap/IMinHeap.ts +0 -10
- package/src/MinHeap/IMinHeapOptions.ts +0 -6
- package/src/MinHeap/index.spec.ts +0 -161
- package/src/MinHeap/index.ts +0 -91
- package/src/Queue/IQueue.ts +0 -9
- package/src/Queue/index.spec.ts +0 -92
- package/src/Queue/index.ts +0 -44
- package/src/Set/ISet.ts +0 -17
- package/src/Set/index.spec.ts +0 -246
- package/src/Set/index.ts +0 -134
- package/src/Stack/IStack.ts +0 -9
- package/src/Stack/index.spec.ts +0 -108
- package/src/Stack/index.ts +0 -43
- package/src/index.ts +0 -17
package/src/MinHeap/index.ts
DELETED
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import IMinHeap from './IMinHeap';
|
|
2
|
-
import IMinHeapOptions from './IMinHeapOptions';
|
|
3
|
-
|
|
4
|
-
export default class MinHeap<T = number> implements IMinHeap<T> {
|
|
5
|
-
private _data: T[] = [];
|
|
6
|
-
|
|
7
|
-
constructor({lessThanOrEqualTo, inputs = []}: IMinHeapOptions<T> = {inputs: []}) {
|
|
8
|
-
if (lessThanOrEqualTo) {
|
|
9
|
-
this._lessThanOrEqualTo = lessThanOrEqualTo;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
for (const input of inputs) {
|
|
13
|
-
this.insert(input);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
get data(): T[] {
|
|
18
|
-
return [...this._data];
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
get size(): number {
|
|
22
|
-
return this.data.length;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
get isEmpty(): boolean {
|
|
26
|
-
return this.size === 0;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
get min(): T | null {
|
|
30
|
-
return this.data[0] || null;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
public insert(value: T): T {
|
|
34
|
-
this._data = [...this.data, value];
|
|
35
|
-
this._siftUp(this.size - 1);
|
|
36
|
-
return value;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public extract(): T | null {
|
|
40
|
-
if (!this.isEmpty) {
|
|
41
|
-
const [min, ...rest] = this.data;
|
|
42
|
-
this._data = [rest[rest.length - 1], ...rest.slice(0, rest.length - 1)].filter(value => value !== undefined);
|
|
43
|
-
this._siftDown(0);
|
|
44
|
-
|
|
45
|
-
return min;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return null;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
private _lessThanOrEqualTo = (value1: T, value2: T): boolean => value1 <= value2;
|
|
52
|
-
private _getLeftIndex = (index: number): number => (2 * index) + 1;
|
|
53
|
-
private _getRightIndex = (index: number): number => (2 * index) + 2;
|
|
54
|
-
private _getParentIndex = (index: number): number => Math.floor((index - 1) / 2);
|
|
55
|
-
|
|
56
|
-
private _siftUp(index: number): void {
|
|
57
|
-
const parent = this._getParentIndex(index);
|
|
58
|
-
|
|
59
|
-
if (index > 0 && this._lessThanOrEqualTo(this.data[index], this.data[parent])) {
|
|
60
|
-
[this._data[parent], this._data[index]] = [this.data[index], this.data[parent]];
|
|
61
|
-
this._siftUp(parent);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
private _siftDown(index: number): void {
|
|
66
|
-
const left = this._getLeftIndex(index);
|
|
67
|
-
const right = this._getRightIndex(index);
|
|
68
|
-
|
|
69
|
-
if (left < this.size && this._lessThanOrEqualTo(this.data[left], this.data[index])) {
|
|
70
|
-
[this._data[left], this._data[index]] = [this.data[index], this.data[left]];
|
|
71
|
-
this._siftDown(left);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (right < this.size && this._lessThanOrEqualTo(this.data[right], this.data[index])) {
|
|
75
|
-
[this._data[right], this._data[index]] = [this.data[index], this.data[right]];
|
|
76
|
-
this._siftDown(right);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
private [Symbol.toPrimitive](type: string): string | number | null {
|
|
81
|
-
if (type === 'string') {
|
|
82
|
-
return this.data.join(', ');
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (type === 'number') {
|
|
86
|
-
return this.size;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
return null;
|
|
90
|
-
}
|
|
91
|
-
}
|
package/src/Queue/IQueue.ts
DELETED
package/src/Queue/index.spec.ts
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import {describe, it, expect} from '@jest/globals';
|
|
2
|
-
|
|
3
|
-
import Queue from './';
|
|
4
|
-
|
|
5
|
-
describe('Queue', () => {
|
|
6
|
-
it('Should create an empty queue without problems', () => {
|
|
7
|
-
const queue = new Queue();
|
|
8
|
-
|
|
9
|
-
expect(queue.data).toEqual([]);
|
|
10
|
-
expect(queue.size).toBe(0);
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it('Should create a filled queue without problems', () => {
|
|
14
|
-
const queue = new Queue(1, 2, 3, 4);
|
|
15
|
-
|
|
16
|
-
expect(queue.data).toEqual([1, 2, 3, 4]);
|
|
17
|
-
expect(queue.size).toBe(4);
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
describe('.enqueue()', () => {
|
|
21
|
-
it('Should enqueue a new element', () => {
|
|
22
|
-
const queue = new Queue(1, 2, 3);
|
|
23
|
-
queue.enqueue(4);
|
|
24
|
-
|
|
25
|
-
expect(queue.data).toEqual([1, 2, 3, 4]);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it('Should return the enqueued element', () => {
|
|
29
|
-
const queue = new Queue(1, 2, 3);
|
|
30
|
-
const returned = queue.enqueue(4);
|
|
31
|
-
|
|
32
|
-
expect(returned).toBe(4);
|
|
33
|
-
});
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
describe('.dequeue()', () => {
|
|
37
|
-
it('Should dequeue the first element', () => {
|
|
38
|
-
const queue = new Queue(1, 2, 3, 4);
|
|
39
|
-
queue.dequeue();
|
|
40
|
-
|
|
41
|
-
expect(queue.data).toEqual([2, 3, 4]);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
it('Should return the dequeued element', () => {
|
|
45
|
-
const queue = new Queue(1, 2, 3, 4);
|
|
46
|
-
const returned = queue.dequeue();
|
|
47
|
-
|
|
48
|
-
expect(returned).toBe(1);
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
describe('.peek()', () => {
|
|
53
|
-
it('Should return the first element', () => {
|
|
54
|
-
const queue = new Queue(1, 2, 3, 4);
|
|
55
|
-
const returned = queue.peek();
|
|
56
|
-
|
|
57
|
-
expect(returned).toEqual(1);
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it('Should not dequeue the first element', () => {
|
|
61
|
-
const queue = new Queue(1, 2, 3, 4);
|
|
62
|
-
queue.peek();
|
|
63
|
-
|
|
64
|
-
expect(queue.data).toEqual([1, 2, 3, 4]);
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
describe('.clear()', () => {
|
|
69
|
-
it('Should clear a queue', () => {
|
|
70
|
-
const queue = new Queue(1, 2, 3, 4);
|
|
71
|
-
queue.clear();
|
|
72
|
-
|
|
73
|
-
expect(queue.data).toEqual([]);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
describe('Conversion to primitive', () => {
|
|
78
|
-
it('Should return comma-separated elements in string conversion', () => {
|
|
79
|
-
const queue = new Queue(1, 2, 3, 4);
|
|
80
|
-
const string = String(queue);
|
|
81
|
-
|
|
82
|
-
expect(string).toBe('[Front] 1, 2, 3, 4');
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it('Should return the queue size in number conversion', () => {
|
|
86
|
-
const queue = new Queue(1, 2, 3, 4);
|
|
87
|
-
const number = Number(queue);
|
|
88
|
-
|
|
89
|
-
expect(number).toBe(4);
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
});
|
package/src/Queue/index.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import DataStructure from '../DataStructure';
|
|
2
|
-
import IQueue from './IQueue';
|
|
3
|
-
|
|
4
|
-
export default class Queue<T = number> extends DataStructure<T> implements IQueue<T> {
|
|
5
|
-
constructor(...inputs: T[]) {
|
|
6
|
-
super(inputs);
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
get isEmpty(): boolean {
|
|
10
|
-
return this.size === 0;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
public enqueue(element: T): T {
|
|
14
|
-
this._data = [...this.data, element];
|
|
15
|
-
return element;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
public dequeue(): T | undefined {
|
|
19
|
-
const [element, ...rest] = this.data;
|
|
20
|
-
this._data = rest;
|
|
21
|
-
return element;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
public peek(): T | undefined {
|
|
25
|
-
const [element] = this.data;
|
|
26
|
-
return element;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public clear(): void {
|
|
30
|
-
this._data = [];
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
private [Symbol.toPrimitive](type: string): string | number | null {
|
|
34
|
-
if (type === 'string') {
|
|
35
|
-
return `[Front] ${this.data.join(', ')}`;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
if (type === 'number') {
|
|
39
|
-
return this.size;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
}
|
package/src/Set/ISet.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import Set from './';
|
|
2
|
-
|
|
3
|
-
interface ISet<T> {
|
|
4
|
-
get data(): T[];
|
|
5
|
-
get size(): number;
|
|
6
|
-
has(_elemet: T): boolean;
|
|
7
|
-
add(_element: T): T | null;
|
|
8
|
-
delete(_element: T): T | null;
|
|
9
|
-
clear(_element: T): T[];
|
|
10
|
-
union(_set: Set<T>): Set<T>;
|
|
11
|
-
intersection(_set: Set<T>): Set<T>;
|
|
12
|
-
difference(_set: Set<T>): Set<T>;
|
|
13
|
-
isSubsetOf(_set: Set<T>): boolean;
|
|
14
|
-
contains(_set: Set<T>): boolean;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export default ISet;
|
package/src/Set/index.spec.ts
DELETED
|
@@ -1,246 +0,0 @@
|
|
|
1
|
-
import {describe, it, expect} from '@jest/globals';
|
|
2
|
-
|
|
3
|
-
import Set from './';
|
|
4
|
-
|
|
5
|
-
const cube = {Set};
|
|
6
|
-
|
|
7
|
-
describe('Set', () => {
|
|
8
|
-
it('Should create an empty set without problems', () => {
|
|
9
|
-
const set = new cube.Set();
|
|
10
|
-
|
|
11
|
-
expect(set.size).toBe(0);
|
|
12
|
-
expect(set.data).toEqual([]);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
it('Should create an empty set without problems', () => {
|
|
16
|
-
const set = new cube.Set(1, 2, 3, 4);
|
|
17
|
-
|
|
18
|
-
expect(set.size).toBe(4);
|
|
19
|
-
expect(set.data.sort()).toEqual([1, 2, 3, 4].sort());
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
describe('Conversion to primitive', () => {
|
|
23
|
-
it('Should return comma-separated elements in string conversion', () => {
|
|
24
|
-
const set = new cube.Set(1, 2, 3, 4);
|
|
25
|
-
const string = String(set);
|
|
26
|
-
|
|
27
|
-
expect(string).toBe('{ 1, 2, 3, 4 }');
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
it('Should return the set size in number conversion', () => {
|
|
31
|
-
const set = new cube.Set(1, 2, 3, 4);
|
|
32
|
-
const number = Number(set);
|
|
33
|
-
|
|
34
|
-
expect(number).toBe(4);
|
|
35
|
-
});
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
describe('.has()', () => {
|
|
39
|
-
it('Should return true when the set has the specified element', () => {
|
|
40
|
-
const set = new cube.Set(1, 2, 3, 4);
|
|
41
|
-
const returned = set.has(3);
|
|
42
|
-
|
|
43
|
-
expect(returned).toBe(true);
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
it('Should return false when the set has not the specified element', () => {
|
|
47
|
-
const set = new cube.Set(1, 2, 4);
|
|
48
|
-
const returned = set.has(3);
|
|
49
|
-
|
|
50
|
-
expect(returned).toBe(false);
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
describe('.add()', () => {
|
|
55
|
-
it('Should add a new element in the set', () => {
|
|
56
|
-
const set = new cube.Set(1, 2, 3);
|
|
57
|
-
set.add(4);
|
|
58
|
-
|
|
59
|
-
expect(set.size).toBe(4);
|
|
60
|
-
expect(set.data.sort()).toEqual([1, 2, 3, 4].sort());
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it('Should return the inserted element when the element is not in the set', () => {
|
|
64
|
-
const set = new cube.Set(1, 2, 3);
|
|
65
|
-
const returned = set.add(4);
|
|
66
|
-
|
|
67
|
-
expect(returned).toEqual(4);
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
it('Should return null when the element is in the set', () => {
|
|
71
|
-
const set = new cube.Set(1, 2, 3, 4);
|
|
72
|
-
const returned = set.add(4);
|
|
73
|
-
|
|
74
|
-
expect(returned).toBeNull();
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
describe('.delete()', () => {
|
|
79
|
-
it('Should remove an element in the set', () => {
|
|
80
|
-
const set = new cube.Set(1, 2, 3, 4);
|
|
81
|
-
set.delete(3);
|
|
82
|
-
|
|
83
|
-
expect(set.size).toBe(3);
|
|
84
|
-
expect(set.data.sort()).toEqual([1, 2, 4].sort());
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
it('Should not change the set if the element is not in the set', () => {
|
|
88
|
-
const set = new cube.Set(1, 2, 3, 4);
|
|
89
|
-
set.delete(5);
|
|
90
|
-
|
|
91
|
-
expect(set.size).toBe(4);
|
|
92
|
-
expect(set.data.sort()).toEqual([1, 2, 3, 4].sort());
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('Should return the element if the element was in the set', () => {
|
|
96
|
-
const set = new cube.Set(1, 2, 3, 4);
|
|
97
|
-
const returned = set.delete(1);
|
|
98
|
-
|
|
99
|
-
expect(returned).toBe(1);
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
it('Should return null if the element was not in the set', () => {
|
|
103
|
-
const set = new cube.Set(1, 2, 3, 4);
|
|
104
|
-
const returned = set.delete(5);
|
|
105
|
-
|
|
106
|
-
expect(returned).toBeNull();
|
|
107
|
-
});
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
describe('.clear()', () => {
|
|
111
|
-
it('Should empty the set', () => {
|
|
112
|
-
const set = new cube.Set(1, 2, 3, 4);
|
|
113
|
-
set.clear();
|
|
114
|
-
|
|
115
|
-
expect(set.size).toBe(0);
|
|
116
|
-
expect(set.data).toEqual([]);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it('Should return the elements', () => {
|
|
120
|
-
const set = new cube.Set(1, 2, 3, 4);
|
|
121
|
-
const returned = set.clear();
|
|
122
|
-
|
|
123
|
-
expect(returned.sort()).toEqual([1, 2, 3, 4].sort());
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
describe('.union()', () => {
|
|
128
|
-
it('Should return the result of the union between the current set and the received set', () => {
|
|
129
|
-
const set1 = new cube.Set(1, 2, 3, 4);
|
|
130
|
-
const set2 = new cube.Set(3, 4, 5, 6);
|
|
131
|
-
const union = set1.union(set2);
|
|
132
|
-
|
|
133
|
-
expect(union.size).toBe(6);
|
|
134
|
-
expect(union.data.sort()).toEqual([1, 2, 3, 4, 5, 6].sort());
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
describe('.intersection()', () => {
|
|
139
|
-
it('Should return the result of the intersection between the current set and the received set', () => {
|
|
140
|
-
const set1 = new cube.Set(1, 2, 3, 4);
|
|
141
|
-
const set2 = new cube.Set(3, 4, 5, 6);
|
|
142
|
-
const intersection = set1.intersection(set2);
|
|
143
|
-
|
|
144
|
-
expect(intersection.size).toBe(2);
|
|
145
|
-
expect(intersection.data.sort()).toEqual([3, 4].sort());
|
|
146
|
-
});
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
describe('.difference()', () => {
|
|
150
|
-
it('Should return the result of the difference between the current set and the received set', () => {
|
|
151
|
-
const set1 = new cube.Set(1, 2, 3, 4);
|
|
152
|
-
const set2 = new cube.Set(3, 4, 5, 6);
|
|
153
|
-
const difference = set1.difference(set2);
|
|
154
|
-
|
|
155
|
-
expect(difference.size).toBe(2);
|
|
156
|
-
expect(difference.data.sort()).toEqual([1, 2].sort());
|
|
157
|
-
});
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
describe('.isSubsetOf()', () => {
|
|
161
|
-
it('Should return true when the current set is subset of the received set', () => {
|
|
162
|
-
const set1 = new cube.Set(1, 4);
|
|
163
|
-
const set2 = new cube.Set(1, 2, 3, 4);
|
|
164
|
-
const isSubset = set1.isSubsetOf(set2);
|
|
165
|
-
|
|
166
|
-
expect(isSubset).toBe(true);
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
it('Should return false whe the current set is not subset of the received set', () => {
|
|
170
|
-
const set1 = new cube.Set(4, 5);
|
|
171
|
-
const set2 = new cube.Set(1, 2, 3, 4);
|
|
172
|
-
const isSubset = set1.isSubsetOf(set2);
|
|
173
|
-
|
|
174
|
-
expect(isSubset).toBe(false);
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
describe('.contains()', () => {
|
|
179
|
-
it('Should return true when the current set contains the received set', () => {
|
|
180
|
-
const set1 = new cube.Set(1, 2, 3, 4);
|
|
181
|
-
const set2 = new cube.Set(1, 4);
|
|
182
|
-
const contains = set1.contains(set2);
|
|
183
|
-
|
|
184
|
-
expect(contains).toBe(true);
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
it('Should return false when the current set does not contains the received set', () => {
|
|
188
|
-
const set1 = new cube.Set(1, 2, 3, 4);
|
|
189
|
-
const set2 = new cube.Set(4, 5);
|
|
190
|
-
const contains = set1.contains(set2);
|
|
191
|
-
|
|
192
|
-
expect(contains).toBe(false);
|
|
193
|
-
});
|
|
194
|
-
});
|
|
195
|
-
|
|
196
|
-
describe('Set.union()', () => {
|
|
197
|
-
it('Should return an union set between two others', () => {
|
|
198
|
-
const set1 = new cube.Set(1, 2, 3, 4);
|
|
199
|
-
const set2 = new cube.Set(3, 4, 5, 6);
|
|
200
|
-
const union = cube.Set.union(set1, set2);
|
|
201
|
-
|
|
202
|
-
expect(union.size).toBe(6);
|
|
203
|
-
expect(union.data.sort()).toEqual([1, 2, 3, 4, 5, 6].sort());
|
|
204
|
-
});
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
describe('Set.intersection()', () => {
|
|
208
|
-
it('Should return an intersection set between two others', () => {
|
|
209
|
-
const set1 = new cube.Set(1, 2, 3, 4);
|
|
210
|
-
const set2 = new cube.Set(3, 4, 5, 6);
|
|
211
|
-
const intersection = cube.Set.intersection(set1, set2);
|
|
212
|
-
|
|
213
|
-
expect(intersection.size).toBe(2);
|
|
214
|
-
expect(intersection.data.sort()).toEqual([3, 4].sort());
|
|
215
|
-
});
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
describe('Set.difference()', () => {
|
|
219
|
-
it('Should return an difference set between two others', () => {
|
|
220
|
-
const set1 = new cube.Set(1, 2, 3, 4);
|
|
221
|
-
const set2 = new cube.Set(3, 4, 5, 6);
|
|
222
|
-
const difference = cube.Set.difference(set1, set2);
|
|
223
|
-
|
|
224
|
-
expect(difference.size).toBe(2);
|
|
225
|
-
expect(difference.data.sort()).toEqual([1, 2].sort());
|
|
226
|
-
});
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
describe('Set.isSubset()', () => {
|
|
230
|
-
it('Should return true when the first argument is a subset of the second argument', () => {
|
|
231
|
-
const set1 = new cube.Set(1, 4);
|
|
232
|
-
const set2 = new cube.Set(1, 2, 3, 4);
|
|
233
|
-
const isSubset = cube.Set.isSubset(set1, set2);
|
|
234
|
-
|
|
235
|
-
expect(isSubset).toBe(true);
|
|
236
|
-
});
|
|
237
|
-
|
|
238
|
-
it('Should return false when the first argument is not a subset of the second argument', () => {
|
|
239
|
-
const set1 = new cube.Set(4, 5);
|
|
240
|
-
const set2 = new cube.Set(1, 2, 3, 4);
|
|
241
|
-
const isSubset = cube.Set.isSubset(set1, set2);
|
|
242
|
-
|
|
243
|
-
expect(isSubset).toBe(false);
|
|
244
|
-
});
|
|
245
|
-
});
|
|
246
|
-
});
|
package/src/Set/index.ts
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import ISet from './ISet';
|
|
2
|
-
|
|
3
|
-
export default class Set<T = number> implements ISet<T> {
|
|
4
|
-
private _data: Map<T, T> = new Map<T, T>();
|
|
5
|
-
|
|
6
|
-
constructor(...inputs: T[]) {
|
|
7
|
-
for (const input of inputs) {
|
|
8
|
-
this.add(input);
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
public get data(): T[] {
|
|
13
|
-
return Array.from(this._data.values());
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
public get size(): number {
|
|
17
|
-
return this._data.size;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
public has(element: T): boolean {
|
|
21
|
-
return this._data.has(element);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
public add(element: T): T | null {
|
|
25
|
-
if (!this.has(element)) {
|
|
26
|
-
this._data.set(element, element);
|
|
27
|
-
return element;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
return null;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
public delete(element: T): T | null {
|
|
34
|
-
if (this.has(element)) {
|
|
35
|
-
this._data.delete(element);
|
|
36
|
-
return element;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
return null;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public clear(): T[] {
|
|
43
|
-
const elements = [...this.data];
|
|
44
|
-
this._data.clear();
|
|
45
|
-
return elements;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
public union(set: Set<T>): Set<T> {
|
|
49
|
-
return Set.union(this, set);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
public intersection(set: Set<T>): Set<T> {
|
|
53
|
-
return Set.intersection(this, set);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
public difference(set: Set<T>): Set<T> {
|
|
57
|
-
return Set.difference(this, set);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public isSubsetOf(set: Set<T>): boolean {
|
|
61
|
-
return Set.isSubset(this, set);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
public contains(set: Set<T>): boolean {
|
|
65
|
-
return Set.isSubset(set, this);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
static union<T>(set1: Set<T>, set2: Set<T>): Set<T> {
|
|
69
|
-
const union = new Set<T>();
|
|
70
|
-
|
|
71
|
-
for (const element of [...set1.data, ...set2.data]) {
|
|
72
|
-
union.add(element);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return union;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
static intersection<T>(set1: Set<T>, set2: Set<T>): Set<T> {
|
|
79
|
-
let biggestSet: Set<T>;
|
|
80
|
-
let smallestSet: Set<T>;
|
|
81
|
-
|
|
82
|
-
if (set1.size >= set2.size) {
|
|
83
|
-
biggestSet = set1;
|
|
84
|
-
smallestSet = set2;
|
|
85
|
-
} else {
|
|
86
|
-
biggestSet = set2;
|
|
87
|
-
smallestSet = set1;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const intersection = new Set<T>();
|
|
91
|
-
|
|
92
|
-
for (const element of smallestSet.data) {
|
|
93
|
-
if (biggestSet.has(element)) {
|
|
94
|
-
intersection.add(element);
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return intersection;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
static difference<T>(set1: Set<T>, set2: Set<T>): Set<T> {
|
|
102
|
-
const difference = new Set<T>();
|
|
103
|
-
|
|
104
|
-
for (const element of set1.data) {
|
|
105
|
-
if (!set2.has(element)) {
|
|
106
|
-
difference.add(element);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return difference;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
static isSubset<T>(set1: Set<T>, set2: Set<T>): boolean {
|
|
114
|
-
if (set1.size > set2.size) {
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return set1
|
|
119
|
-
.data
|
|
120
|
-
.every(element => set2.has(element));
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
private [Symbol.toPrimitive](type: string): string | number | null {
|
|
124
|
-
if (type === 'string') {
|
|
125
|
-
return `{ ${this.data.join(', ')} }`;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (type === 'number') {
|
|
129
|
-
return this.size;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return null;
|
|
133
|
-
}
|
|
134
|
-
}
|