diff --git a/src/data-structures/base/iterable-element-base.ts b/src/data-structures/base/iterable-element-base.ts index 49585ea..a12a367 100644 --- a/src/data-structures/base/iterable-element-base.ts +++ b/src/data-structures/base/iterable-element-base.ts @@ -14,7 +14,7 @@ export abstract class IterableElementBase { } } - abstract get size(): number; + // abstract get size(): number; protected _toElementFn?: (rawElement: R) => E; diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index 84a6e27..3fef22f 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -544,7 +544,7 @@ export class BinaryTree>([this._root]); let potentialParent: BinaryTreeNode | undefined; // Record the parent node of the potential insertion location - while (queue.size > 0) { + while (queue.length > 0) { const cur = queue.shift(); if (!cur) continue; @@ -1530,7 +1530,7 @@ export class BinaryTree { - if (queue.size === 0) return; + if (queue.length === 0) return; const current = queue.shift()!; ans.push(callback(current)); @@ -1549,8 +1549,8 @@ export class BinaryTree>>([startNode]); - while (queue.size > 0) { - const levelSize = queue.size; + while (queue.length > 0) { + const levelSize = queue.length; for (let i = 0; i < levelSize; i++) { const current = queue.shift()!; @@ -1609,7 +1609,7 @@ export class BinaryTree 0) { + while (queue.length > 0) { const cur = queue.shift(); if (this.isRealNode(cur)) { if (this.isLeaf(cur)) { diff --git a/src/data-structures/binary-tree/bst.ts b/src/data-structures/binary-tree/bst.ts index 072ff58..e760110 100644 --- a/src/data-structures/binary-tree/bst.ts +++ b/src/data-structures/binary-tree/bst.ts @@ -792,7 +792,7 @@ export class BST return ans; } else { const queue = new Queue>([this._root]); - while (queue.size > 0) { + while (queue.length > 0) { const cur = queue.shift(); if (this.isRealNode(cur)) { const compared = this._compare(cur.key, targetKey); diff --git a/src/data-structures/graph/abstract-graph.ts b/src/data-structures/graph/abstract-graph.ts index 4e856e5..7145e3d 100644 --- a/src/data-structures/graph/abstract-graph.ts +++ b/src/data-structures/graph/abstract-graph.ts @@ -356,8 +356,8 @@ export abstract class AbstractGraph< const queue = new Queue([vertex1]); visited.set(vertex1, true); let cost = 0; - while (queue.size > 0) { - for (let i = 0; i < queue.size; i++) { + while (queue.length > 0) { + for (let i = 0; i < queue.length; i++) { const cur = queue.shift(); if (cur === vertex2) { return cost; diff --git a/src/data-structures/linked-list/doubly-linked-list.ts b/src/data-structures/linked-list/doubly-linked-list.ts index 116a535..c48a597 100644 --- a/src/data-structures/linked-list/doubly-linked-list.ts +++ b/src/data-structures/linked-list/doubly-linked-list.ts @@ -159,7 +159,7 @@ export class DoublyLinkedListNode { * const initialNode = this.currentSong; * * // Loop through the playlist twice - * for (let i = 0; i < this.playlist.size * 2; i++) { + * for (let i = 0; i < this.playlist.length * 2; i++) { * playedSongs.push(this.currentSong!.value); * this.currentSong = this.currentSong!.next || this.playlist.head; // Loop back to the start if needed * } @@ -280,7 +280,7 @@ export class DoublyLinkedListNode { * } * * // Check capacity - * if (this.list.size >= this.capacity) { + * if (this.list.length >= this.capacity) { * // Delete the least recently used element (the tail of the linked list) * const removedNode = this.list.tail; * if (removedNode) { @@ -325,9 +325,9 @@ export class DoublyLinkedListNode { * this.map.clear(); * } * - * // Get the current cache size - * get size(): number { - * return this.list.size; + * // Get the current cache length + * get length(): number { + * return this.list.length; * } * * // Check if it is empty @@ -386,7 +386,7 @@ export class DoublyLinkedListNode { * * console.log(cache.delete('a')); // true * console.log(cache.get('a')); // undefined - * console.log(cache.size); // 1 + * console.log(cache.length); // 1 * * // Should support clearing cache * cache.clear(); @@ -394,7 +394,7 @@ export class DoublyLinkedListNode { * cache.set('b', 2); * cache.clear(); * - * console.log(cache.size); // 0 + * console.log(cache.length); // 0 * console.log(cache.isEmpty); // true * @example * // finding lyrics by timestamp in Coldplay's "Fix You" @@ -531,7 +531,7 @@ export class DoublyLinkedList extends IterableElementBase extends IterableElementBase extends IterableElementBase 0 && this.size > this._maxLen) this.shift(); + this._length++; + if (this._maxLen > 0 && this.length > this._maxLen) this.shift(); return true; } @@ -674,7 +674,7 @@ export class DoublyLinkedList extends IterableElementBase extends IterableElementBase extends IterableElementBase 0 && this._size > this._maxLen) this.pop(); + this._length++; + if (this._maxLen > 0 && this._length > this._maxLen) this.pop(); return true; } @@ -786,7 +786,7 @@ export class DoublyLinkedList extends IterableElementBase= this._size) return undefined; + if (index < 0 || index >= this._length) return undefined; let current = this.head; for (let i = 0; i < index; i++) { current = current!.next; @@ -806,7 +806,7 @@ export class DoublyLinkedList extends IterableElementBase | undefined { - if (index < 0 || index >= this._size) return undefined; + if (index < 0 || index >= this._length) return undefined; let current = this.head; for (let i = 0; i < index; i++) { current = current!.next; @@ -861,15 +861,15 @@ export class DoublyLinkedList extends IterableElementBase` object. * @returns The `addAt` method returns a boolean value. It returns `true` if the element or node was * successfully added at the specified index, and `false` if the index is out of bounds (less than 0 - * or greater than the size of the list). + * or greater than the length of the list). */ addAt(index: number, newElementOrNode: E | DoublyLinkedListNode): boolean { - if (index < 0 || index > this._size) return false; + if (index < 0 || index > this._length) return false; if (index === 0) { this.unshift(newElementOrNode); return true; } - if (index === this._size) { + if (index === this._length) { this.push(newElementOrNode); return true; } @@ -881,7 +881,7 @@ export class DoublyLinkedList extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase= this._size) return false; + if (index < 0 || index >= this._length) return false; if (index === 0) { this.shift(); return true; } - if (index === this._size - 1) { + if (index === this._length - 1) { this.pop(); return true; } @@ -991,7 +991,7 @@ export class DoublyLinkedList extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase 0 && this.size > this._maxLen) this.shift(); + this._length++; + if (this._maxLen > 0 && this.length > this._maxLen) this.shift(); return true; } @@ -185,7 +185,7 @@ export class SinglyLinkedList extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase= this._size) return undefined; + if (index < 0 || index >= this._length) return undefined; let current = this.head; for (let i = 0; i < index; i++) { current = current!.next; @@ -379,12 +379,12 @@ export class SinglyLinkedList extends IterableElementBase= this._size) return false; + if (index < 0 || index >= this._length) return false; if (index === 0) { this.shift(); return true; } - if (index === this._size - 1) { + if (index === this._length - 1) { this.pop(); return true; } @@ -392,7 +392,7 @@ export class SinglyLinkedList extends IterableElementBase extends IterableElementBase extends IterableElementBase): boolean { - if (index < 0 || index > this._size) return false; + if (index < 0 || index > this._length) return false; if (index === 0) { this.unshift(newElementOrNode); return true; } - if (index === this._size) { + if (index === this._length) { this.push(newElementOrNode); return true; } @@ -471,7 +471,7 @@ export class SinglyLinkedList extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase 0 && this._size > this._maxLen) this.shift(); + if (this._maxLen > 0 && this._length > this._maxLen) this.shift(); return true; } @@ -206,9 +206,9 @@ export class Deque extends IterableElementBase 0) { this._lastInBucket -= 1; } else if (this._bucketLast > 0) { @@ -219,7 +219,7 @@ export class Deque extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase 0) { this._firstInBucket -= 1; } else if (this._bucketFirst > 0) { @@ -273,9 +273,9 @@ export class Deque extends IterableElementBase 0 && this._size > this._maxLen) this.pop(); + if (this._maxLen > 0 && this._length > this._maxLen) this.pop(); return true; } @@ -338,7 +338,7 @@ export class Deque extends IterableElementBase extends IterableElementBase> 1; } @@ -360,7 +360,7 @@ export class Deque extends IterableElementBase { let index = 0; - while (index < this._size) { + while (index < this._length) { yield this.at(index); index++; } @@ -371,7 +371,7 @@ export class Deque extends IterableElementBase { - let index = this._size - 1; + let index = this._length - 1; while (index >= 0) { yield this.at(index); index--; @@ -389,7 +389,7 @@ export class Deque extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase([], { bucketSize: this._bucketSize }); @@ -501,12 +501,12 @@ export class Deque extends IterableElementBase([], { bucketSize: this._bucketSize }); if (pos < 0) pos = 0; - for (let i = pos; i < this._size; i++) { + for (let i = pos; i < this._length; i++) { newDeque.push(this.at(i)); } @@ -526,11 +526,11 @@ export class Deque extends IterableElementBase extends IterableElementBase extends IterableElementBase extends IterableElementBase number): this { const arr: E[] = []; - for (let i = 0; i < this._size; ++i) { + for (let i = 0; i < this._length; ++i) { arr.push(this.at(i)); } arr.sort(comparator); - for (let i = 0; i < this._size; ++i) { + for (let i = 0; i < this._length; ++i) { this.setAt(i, arr[i]); } return this; @@ -645,10 +645,10 @@ export class Deque extends IterableElementBase extends IterableElementBase extends IterableElementBase { - for (let i = 0; i < this._size; ++i) { + for (let i = 0; i < this._length; ++i) { yield this.at(i); } } diff --git a/src/data-structures/queue/queue.ts b/src/data-structures/queue/queue.ts index 09ad72c..a6cf6b5 100644 --- a/src/data-structures/queue/queue.ts +++ b/src/data-structures/queue/queue.ts @@ -52,10 +52,10 @@ export class Queue extends IterableElementBase extends IterableElementBase 0 ? this.elements[this.offset] : undefined; + return this.length > 0 ? this.elements[this.offset] : undefined; } /** @@ -80,7 +80,7 @@ export class Queue extends IterableElementBase 0 ? this.elements[this.elements.length - 1] : undefined; + return this.length > 0 ? this.elements[this.elements.length - 1] : undefined; } protected _autoCompactRatio: number = 0.5; @@ -126,7 +126,7 @@ export class Queue extends IterableElementBase 0 && this.size > this._maxLen) this.shift(); + if (this._maxLen > 0 && this.length > this._maxLen) this.shift(); return true; } @@ -159,7 +159,7 @@ export class Queue extends IterableElementBase extends IterableElementBase { it('should deleteAt', () => { expect(list.deleteAt(1)).toBe(true); expect(list.deleteAt(-1)).toBe(false); - expect(list.deleteAt(list.size)).toBe(false); - expect(list.size).toBe(4); + expect(list.deleteAt(list.length)).toBe(false); + expect(list.length).toBe(4); expect(list.deleteAt(4)).toBe(false); expect([...list]).toEqual([1, 3, 4, 5]); expect(list.isEmpty()).toBe(false); @@ -103,7 +103,7 @@ describe('DoublyLinkedList Operation Test', () => { }); it('should initialize an empty list', () => { - expect(list.size).toBe(0); + expect(list.length).toBe(0); expect(list.head).toBe(undefined); expect(list.tail).toBe(undefined); }); @@ -119,7 +119,7 @@ describe('DoublyLinkedList Operation Test', () => { list.push(1); list.push(2); list.push(3); - expect(list.size).toBe(3); + expect(list.length).toBe(3); expect(list.head!.value).toBe(1); expect(list.tail!.value).toBe(3); }); @@ -130,7 +130,7 @@ describe('DoublyLinkedList Operation Test', () => { list.push(i); } expect(list.maxLen).toBe(10); - expect(list.size).toBe(10); + expect(list.length).toBe(10); expect(list.first).toBe(990); list.clear(); @@ -139,7 +139,7 @@ describe('DoublyLinkedList Operation Test', () => { } expect(list.maxLen).toBe(10); - expect(list.size).toBe(10); + expect(list.length).toBe(10); expect(list.last).toBe(990); }); @@ -148,7 +148,7 @@ describe('DoublyLinkedList Operation Test', () => { list.push(2); const poppedValue = list.pop(); expect(poppedValue).toBe(2); - expect(list.size).toBe(1); + expect(list.length).toBe(1); expect(list.head!.value).toBe(1); expect(list.tail!.value).toBe(1); list.pop(); @@ -164,19 +164,19 @@ describe('DoublyLinkedList Operation Test', () => { // Inserting at the beginning list.addAt(0, 0); - expect(list.size).toBe(4); + expect(list.length).toBe(4); expect(list.at(0)).toBe(0); expect(list.at(1)).toBe(1); // Inserting in the middle list.addAt(2, 1.5); - expect(list.size).toBe(5); + expect(list.length).toBe(5); expect(list.at(2)).toBe(1.5); expect(list.at(3)).toBe(2); // Inserting at the end list.addAt(5, 4); - expect(list.size).toBe(6); + expect(list.length).toBe(6); expect(list.at(5)).toBe(4); expect(list.tail!.value).toBe(4); expect(list.at(-1)).toBe(undefined); @@ -191,17 +191,17 @@ describe('DoublyLinkedList Operation Test', () => { // Deleting from the beginning const deletedValue = list.deleteAt(0); expect(deletedValue).toBe(true); - expect(list.size).toBe(2); + expect(list.length).toBe(2); expect(list.head!.value).toBe(2); // Deleting from the middle list.deleteAt(0); // Deleting the second element - expect(list.size).toBe(1); + expect(list.length).toBe(1); expect(list.head!.value).toBe(3); // Deleting from the end list.deleteAt(0); - expect(list.size).toBe(0); + expect(list.length).toBe(0); expect(list.head).toBe(undefined); expect(list.tail).toBe(undefined); }); @@ -212,16 +212,16 @@ describe('DoublyLinkedList Operation Test', () => { list.push(3); list.delete(2); - expect(list.size).toBe(2); + expect(list.length).toBe(2); expect(list.head!.value).toBe(1); expect(list.tail!.value).toBe(3); list.delete(1); - expect(list.size).toBe(1); + expect(list.length).toBe(1); expect(list.head!.value).toBe(3); list.delete(3); - expect(list.size).toBe(0); + expect(list.length).toBe(0); expect(list.head).toBe(undefined); expect(list.tail).toBe(undefined); }); @@ -326,7 +326,7 @@ describe('DoublyLinkedList Operation Test', () => { list.clear(); - expect(list.size).toBe(0); + expect(list.length).toBe(0); expect(list.head).toBe(undefined); expect(list.tail).toBe(undefined); }); @@ -597,7 +597,7 @@ describe('classic use', () => { const initialNode = this.currentSong; // Loop through the playlist twice - for (let i = 0; i < this.playlist.size * 2; i++) { + for (let i = 0; i < this.playlist.length * 2; i++) { playedSongs.push(this.currentSong!.value); this.currentSong = this.currentSong!.next || this.playlist.head; // Loop back to the start if needed } @@ -719,7 +719,7 @@ describe('classic use', () => { } // Check capacity - if (this.list.size >= this.capacity) { + if (this.list.length >= this.capacity) { // Delete the least recently used element (the tail of the linked list) const removedNode = this.list.tail; if (removedNode) { @@ -764,9 +764,9 @@ describe('classic use', () => { this.map.clear(); } - // Get the current cache size - get size(): number { - return this.list.size; + // Get the current cache length + get length(): number { + return this.list.length; } // Check if it is empty @@ -825,7 +825,7 @@ describe('classic use', () => { expect(cache.delete('a')).toBe(true); expect(cache.get('a')).toBeUndefined(); - expect(cache.size).toBe(1); + expect(cache.length).toBe(1); // Should support clearing cache cache.clear(); @@ -833,7 +833,7 @@ describe('classic use', () => { cache.set('b', 2); cache.clear(); - expect(cache.size).toBe(0); + expect(cache.length).toBe(0); expect(cache.isEmpty).toBe(true); }); diff --git a/test/unit/data-structures/linked-list/singly-linked-list.test.ts b/test/unit/data-structures/linked-list/singly-linked-list.test.ts index b3ace4f..e1a432b 100644 --- a/test/unit/data-structures/linked-list/singly-linked-list.test.ts +++ b/test/unit/data-structures/linked-list/singly-linked-list.test.ts @@ -44,7 +44,7 @@ describe('SinglyLinkedList Operation Test', () => { list.push(i); } expect(list.maxLen).toBe(10); - expect(list.size).toBe(10); + expect(list.length).toBe(10); expect(list.first).toBe(990); }); }); @@ -197,7 +197,7 @@ describe('SinglyLinkedList Operation Test', () => { list.push(3); list.clear(); expect(list.toArray()).toEqual([]); - expect(list.size).toBe(0); + expect(list.length).toBe(0); expect(list.isEmpty()).toBe(true); }); }); @@ -287,10 +287,10 @@ describe('SinglyLinkedList Operation Test', () => { describe('getLength', () => { it('should return the correct length of the list', () => { - expect(list.size).toBe(0); + expect(list.length).toBe(0); list.push(1); list.push(2); - expect(list.size).toBe(2); + expect(list.length).toBe(2); }); }); @@ -468,7 +468,7 @@ describe('SinglyLinkedList', () => { it('should initialize an empty list', () => { expect(list.head).toBe(undefined); expect(list.tail).toBe(undefined); - expect(list.size).toBe(0); + expect(list.length).toBe(0); }); it('should push elements to the end of the list', () => { @@ -476,7 +476,7 @@ describe('SinglyLinkedList', () => { list.push(2); expect(list.head!.value).toBe(1); expect(list.tail!.value).toBe(2); - expect(list.size).toBe(2); + expect(list.length).toBe(2); }); it('should pop elements from the end of the list', () => { @@ -486,7 +486,7 @@ describe('SinglyLinkedList', () => { expect(popped).toBe(2); expect(list.head!.value).toBe(1); expect(list.tail!.value).toBe(1); - expect(list.size).toBe(1); + expect(list.length).toBe(1); }); it('should reverse the list', () => { diff --git a/test/unit/data-structures/queue/deque.test.ts b/test/unit/data-structures/queue/deque.test.ts index 06e98e0..1628d6d 100644 --- a/test/unit/data-structures/queue/deque.test.ts +++ b/test/unit/data-structures/queue/deque.test.ts @@ -11,13 +11,13 @@ describe('Deque - Basic Operations', () => { }); it('push should add elements to the end', () => { - expect(deque.size).toBe(2); + expect(deque.length).toBe(2); expect(deque.last).toBe(2); }); it('pop should remove elements from the end', () => { expect(deque.pop()).toBe(2); - expect(deque.size).toBe(1); + expect(deque.length).toBe(1); expect(deque.pop()).toBe(1); expect(deque.isEmpty()).toBeTruthy(); }); @@ -26,7 +26,7 @@ describe('Deque - Basic Operations', () => { deque.clear(); deque.unshift(1); deque.unshift(2); - expect(deque.size).toBe(2); + expect(deque.length).toBe(2); expect(deque.first).toBe(2); }); @@ -35,7 +35,7 @@ describe('Deque - Basic Operations', () => { deque.unshift(1); deque.unshift(2); expect(deque.shift()).toBe(2); - expect(deque.size).toBe(1); + expect(deque.length).toBe(1); expect(deque.shift()).toBe(1); expect(deque.isEmpty()).toBeTruthy(); }); @@ -88,24 +88,24 @@ describe('Deque - Basic Operations', () => { deque.push('0'); deque.push('5'); deque.push('9'); - expect(deque.size).toBe(6); + expect(deque.length).toBe(6); deque.delete('2'); - expect(deque.size).toBe(5); + expect(deque.length).toBe(5); expect([...deque]).toEqual(['1', '6', '0', '5', '9']); const cloned = deque.clone(); expect([...cloned]).toEqual(['1', '6', '0', '5', '9']); - expect(deque.size).toBe(5); + expect(deque.length).toBe(5); deque.delete('5'); - expect(deque.size).toBe(4); + expect(deque.length).toBe(4); expect([...deque]).toEqual(['1', '6', '0', '9']); expect([...cloned]).toEqual(['1', '6', '0', '5', '9']); - expect(cloned.size).toBe(5); + expect(cloned.length).toBe(5); cloned.push('8'); - expect(cloned.size).toBe(6); + expect(cloned.length).toBe(6); cloned.delete('6'); - expect(cloned.size).toBe(5); + expect(cloned.length).toBe(5); cloned.delete('6'); - expect(cloned.size).toBe(5); + expect(cloned.length).toBe(5); }); }); describe('Deque - Complex Operations', () => { @@ -126,15 +126,15 @@ describe('Deque - Complex Operations', () => { deque.push(1); deque.push(2); deque.push(3); - expect(deque.size).toBe(3); + expect(deque.length).toBe(3); deque.cut(1, true); - expect(deque.size).toBe(2); + expect(deque.length).toBe(2); expect(deque.toArray()).toEqual([1, 2]); const dq1 = new Deque([1, 2, 3, 4, 5, 6, 7]); - expect(dq1.size).toBe(7); + expect(dq1.length).toBe(7); expect([...dq1.cut(3, true)]).toEqual([1, 2, 3, 4]); - expect(dq1.size).toBe(4); + expect(dq1.length).toBe(4); expect([...dq1]).toEqual([1, 2, 3, 4]); const dqCut = dq1.cut(2); expect(dqCut.toArray()).toEqual([1, 2, 3]); @@ -228,7 +228,7 @@ describe('Deque - Complex Operations', () => { it('shrinkToFit should reduce the memory footprint', () => { deque.shrinkToFit(); - expect(deque.size).toBe(0); + expect(deque.length).toBe(0); expect(deque.has(1)).toBe(false); expect(deque.bucketFirst).toBe(0); expect(deque.bucketLast).toBe(0); @@ -241,7 +241,7 @@ describe('Deque - Complex Operations', () => { deque.shrinkToFit(); deque = new Deque([1, 2, 3, 4, 5], { bucketSize: 2 }); - expect(deque.size).toBe(5); + expect(deque.length).toBe(5); expect(deque.has(1)).toBe(true); expect(deque.bucketFirst).toBe(0); expect(deque.bucketLast).toBe(2); @@ -322,19 +322,19 @@ describe('Deque - Utility Operations', () => { it('should maxLen work well', () => { const dequeMaxLen = new Deque([3, 4, 5, 6, 7], { maxLen: 3 }); - expect(dequeMaxLen.size).toBe(3); + expect(dequeMaxLen.length).toBe(3); expect(dequeMaxLen.toArray()).toEqual([5, 6, 7]); dequeMaxLen.unshift(4); dequeMaxLen.unshift(3); - expect(dequeMaxLen.size).toBe(3); + expect(dequeMaxLen.length).toBe(3); expect(dequeMaxLen.toArray()).toEqual([3, 4, 5]); const dequeNoMaxLen = new Deque([3, 4, 5, 6, 7]); - expect(dequeNoMaxLen.size).toBe(5); + expect(dequeNoMaxLen.length).toBe(5); expect(dequeNoMaxLen.toArray()).toEqual([3, 4, 5, 6, 7]); dequeNoMaxLen.unshift(4); dequeNoMaxLen.unshift(3); - expect(dequeNoMaxLen.size).toBe(7); + expect(dequeNoMaxLen.length).toBe(7); expect(dequeNoMaxLen.toArray()).toEqual([3, 4, 3, 4, 5, 6, 7]); }); }); @@ -350,21 +350,21 @@ describe('Deque - Additional Operations', () => { deque.push(1); deque.push(2); expect(deque.last).toBe(2); - expect(deque.size).toBe(2); + expect(deque.length).toBe(2); }); it('pop should remove and return the last element', () => { deque.push(1); deque.push(2); expect(deque.pop()).toBe(2); - expect(deque.size).toBe(1); + expect(deque.length).toBe(1); }); it('unshift should add an element to the beginning', () => { deque.unshift(1); deque.unshift(2); expect(deque.first).toBe(2); - expect(deque.size).toBe(2); + expect(deque.length).toBe(2); }); it('shift should remove and return the first element', () => { @@ -372,13 +372,13 @@ describe('Deque - Additional Operations', () => { deque.unshift(1); deque.unshift(2); expect(deque.shift()).toBe(2); - expect(deque.size).toBe(1); + expect(deque.length).toBe(1); }); it('clear should reset the deque', () => { deque.unshift(1); deque.clear(); - expect(deque.size).toBe(0); + expect(deque.length).toBe(0); expect(deque.isEmpty()).toBeTruthy(); }); @@ -411,7 +411,7 @@ describe('Deque - push Method', () => { it('push should add an element when deque is empty', () => { deque.push(1); expect(deque.last).toBe(1); - expect(deque.size).toBe(1); + expect(deque.length).toBe(1); }); it('push should add an element when lastInBucket is not at max', () => { @@ -420,7 +420,7 @@ describe('Deque - push Method', () => { } deque.push(bucketSize); expect(deque.last).toBe(bucketSize); - expect(deque.size).toBe(bucketSize); + expect(deque.length).toBe(bucketSize); }); it('push should add an element and move to next bucket when last bucket is full', () => { @@ -429,7 +429,7 @@ describe('Deque - push Method', () => { } deque.push(bucketSize + 1); expect(deque.last).toBe(bucketSize + 1); - expect(deque.size).toBe(bucketSize + 1); + expect(deque.length).toBe(bucketSize + 1); }); it('push should add an element and reallocate when last bucket and lastInBucket are at max', () => { @@ -439,7 +439,7 @@ describe('Deque - push Method', () => { deque.push(100); expect(deque.last).toBe(100); - expect(deque.size).toBeGreaterThan(bucketSize); + expect(deque.length).toBeGreaterThan(bucketSize); }); }); describe('Deque - pop Method', () => { @@ -456,7 +456,7 @@ describe('Deque - pop Method', () => { deque.push(1); deque.push(2); expect(deque.pop()).toBe(2); - expect(deque.size).toBe(1); + expect(deque.length).toBe(1); }); it('pop should handle popping the only element', () => { @@ -488,7 +488,7 @@ describe('Deque - unshift Method', () => { it('unshift should add an element to the beginning when deque is empty', () => { deque.unshift(1); expect(deque.first).toBe(1); - expect(deque.size).toBe(1); + expect(deque.length).toBe(1); }); it('unshift should add an element to the beginning and adjust firstInBucket', () => { @@ -523,7 +523,7 @@ describe('Deque - shift Method', () => { deque.push(1); deque.push(2); expect(deque.shift()).toBe(1); - expect(deque.size).toBe(1); + expect(deque.length).toBe(1); }); it('shift should handle shifting the only element', () => { @@ -565,7 +565,7 @@ describe('Deque', () => { const numbers = new IterableNumbers([1, 6, 7, 3, 2, 4, 5]); const deque = new Deque(numbers, { bucketSize: 3 }); - expect(deque.size).toBe(7); + expect(deque.length).toBe(7); expect(deque.bucketSize).toBe(3); expect(deque.maxLen).toBe(-1); }); @@ -591,7 +591,7 @@ describe('Deque', () => { const numbers = new IterableNumbersWithSize([1, 6, 7, 3, 2, 4, 5]); const deque = new Deque(numbers, { bucketSize: 3 }); - expect(deque.size).toBe(7); + expect(deque.length).toBe(7); expect(deque.bucketSize).toBe(3); expect(deque.maxLen).toBe(-1); }); @@ -601,7 +601,7 @@ describe('Deque', () => { key: number; }> = [{ key: 1 }, { key: 6 }, { key: 7 }, { key: 3 }, { key: 2 }, { key: 4 }, { key: 5 }]; const deque = new Deque(objArr, { toElementFn: item => item.key }); - expect(deque.size).toBe(7); + expect(deque.length).toBe(7); expect(deque.has(1)).toBe(true); expect(deque.has(7)).toBe(true); expect(deque.has(8)).toBe(false); @@ -612,7 +612,7 @@ describe('Deque', () => { key: number; }> = [{ key: 1 }, { key: 6 }, { key: 7 }, { key: 3 }, { key: 2 }, { key: 4 }, { key: 5 }]; const deque = new Deque(objArr, { toElementFn: item => item.key, bucketSize: 3 }); - expect(deque.size).toBe(7); + expect(deque.length).toBe(7); expect(deque.has(1)).toBe(true); expect(deque.bucketFirst).toBe(0); expect(deque.bucketLast).toBe(2); @@ -628,7 +628,7 @@ describe('Deque', () => { it('should pop work well when bucket boundary is reached', () => { const deque = new Deque([1, 6, 7, 3, 2, 4, 5], { bucketSize: 3 }); - expect(deque.size).toBe(7); + expect(deque.length).toBe(7); expect(deque.has(1)).toBe(true); expect(deque.bucketFirst).toBe(0); expect(deque.bucketLast).toBe(2); @@ -641,7 +641,7 @@ describe('Deque', () => { [4, 5] ]); for (let i = 0; i < 3; ++i) deque.pop(); - expect(deque.size).toBe(4); + expect(deque.length).toBe(4); expect(deque.has(1)).toBe(true); expect(deque.bucketFirst).toBe(0); expect(deque.bucketLast).toBe(1); @@ -654,7 +654,7 @@ describe('Deque', () => { [4, 5] ]); // TODO may be a problem deque.pop(); - expect(deque.size).toBe(3); + expect(deque.length).toBe(3); expect(deque.has(1)).toBe(true); expect(deque.bucketFirst).toBe(0); expect(deque.bucketLast).toBe(1); @@ -670,7 +670,7 @@ describe('Deque', () => { it('should shift work well when bucket boundary is reached and should shrinkToFit', () => { const deque = new Deque([1, 6, 7, 3, 2, 4, 5], { bucketSize: 3 }); - expect(deque.size).toBe(7); + expect(deque.length).toBe(7); expect(deque.has(1)).toBe(true); expect(deque.bucketFirst).toBe(0); expect(deque.bucketLast).toBe(2); @@ -683,7 +683,7 @@ describe('Deque', () => { [4, 5] ]); for (let i = 0; i < 3; ++i) deque.shift(); - expect(deque.size).toBe(4); + expect(deque.length).toBe(4); expect(deque.has(1)).toBe(false); expect(deque.bucketFirst).toBe(1); expect(deque.bucketLast).toBe(2); @@ -696,7 +696,7 @@ describe('Deque', () => { [4, 5] ]); // TODO may be a problem deque.shift(); - expect(deque.size).toBe(3); + expect(deque.length).toBe(3); expect(deque.has(1)).toBe(false); expect(deque.bucketFirst).toBe(1); expect(deque.bucketLast).toBe(2); @@ -709,7 +709,7 @@ describe('Deque', () => { [4, 5] ]); // TODO may be a problem deque.shrinkToFit(); - expect(deque.size).toBe(3); + expect(deque.length).toBe(3); expect(deque.has(1)).toBe(false); expect(deque.bucketFirst).toBe(0); expect(deque.bucketLast).toBe(1); @@ -735,7 +735,7 @@ describe('classic uses', () => { // Rotate clockwise to the right (forward) rotateClockwise(steps: number): void { - const n = this.deque.size; + const n = this.deque.length; if (n === 0) return; for (let i = 0; i < steps; i++) { @@ -746,7 +746,7 @@ describe('classic uses', () => { // Rotate counterclockwise to the left (backward) rotateCounterClockwise(steps: number): void { - const n = this.deque.size; + const n = this.deque.length; if (n === 0) return; for (let i = 0; i < steps; i++) { diff --git a/test/unit/data-structures/queue/queue.test.ts b/test/unit/data-structures/queue/queue.test.ts index e726e14..e6d334f 100644 --- a/test/unit/data-structures/queue/queue.test.ts +++ b/test/unit/data-structures/queue/queue.test.ts @@ -11,21 +11,21 @@ describe('Queue', () => { }); it('new Queue() should create an empty queue', () => { - expect(queue.size).toBe(0); + expect(queue.length).toBe(0); expect(queue.isEmpty()).toBeTruthy(); }); it('push should add elements to the queue', () => { queue.push(1); queue.push(2); - expect(queue.size).toBe(2); + expect(queue.length).toBe(2); }); it('shift should remove the first element', () => { queue.push(1); queue.push(2); expect(queue.shift()).toBe(1); - expect(queue.size).toBe(1); + expect(queue.length).toBe(1); }); it('shift should return undefined if queue is empty', () => { @@ -36,17 +36,17 @@ describe('Queue', () => { queue.push(1); queue.push(2); expect(queue.first).toBe(1); - expect(queue.size).toBe(2); + expect(queue.length).toBe(2); }); it('first should return undefined if queue is empty', () => { expect(queue.first).toBeUndefined(); }); - it('size should return the number of elements', () => { + it('length should return the number of elements', () => { queue.push(1); queue.push(2); - expect(queue.size).toBe(2); + expect(queue.length).toBe(2); }); it('isEmpty should return true if the queue is empty', () => { @@ -68,7 +68,7 @@ describe('Queue', () => { queue.push(1); queue.push(2); queue.clear(); - expect(queue.size).toBe(0); + expect(queue.length).toBe(0); }); it('forEach should iterate over all elements', () => { @@ -96,7 +96,7 @@ describe('Queue', () => { queue.push(i); } expect(queue.maxLen).toBe(10); - expect(queue.size).toBe(10); + expect(queue.length).toBe(10); expect(queue.first).toBe(990); }); @@ -129,9 +129,9 @@ describe('Queue', () => { toElementFn: rawElement => rawElement.id }); - expect(queue.size).toBe(5); + expect(queue.length).toBe(5); queue.shift(); - expect(queue.size).toBe(4); + expect(queue.length).toBe(4); expect(queue.at(1)).toBe('3'); }); @@ -203,11 +203,11 @@ describe('Queue', () => { queue.push(9); queue.push(10); expect(queue.elements.length).toBe(10); - while (queue.size > 7) queue.shift(); - expect(queue.size).toBe(7); + while (queue.length > 7) queue.shift(); + expect(queue.length).toBe(7); expect(queue.elements.length).toBe(10); queue.shift(); - expect(queue.size).toBe(6); + expect(queue.length).toBe(6); expect(queue.elements.length).toBe(6); }); }); @@ -270,7 +270,7 @@ describe('Queue - Additional Methods', () => { queue.push(1); queue.push(2); expect(queue.last).toBe(2); - expect(queue.size).toBe(2); + expect(queue.length).toBe(2); }); it('peekLast should return undefined if queue is empty', () => { @@ -303,7 +303,7 @@ describe('Queue - Static and Clone Methods', () => { const array = [1, 2, 3]; const queue = Queue.fromArray(array); expect(queue.toArray()).toEqual(array); - expect(queue.size).toBe(array.length); + expect(queue.length).toBe(array.length); }); it('fromArray should create an empty queue from an empty array', () => { @@ -318,7 +318,7 @@ describe('Queue - Static and Clone Methods', () => { const clonedQueue = originalQueue.clone(); expect(clonedQueue.toArray()).toEqual(originalQueue.toArray()); - expect(clonedQueue.size).toBe(originalQueue.size); + expect(clonedQueue.length).toBe(originalQueue.length); }); it('clone should not affect the original queue when mutated', () => { @@ -329,7 +329,7 @@ describe('Queue - Static and Clone Methods', () => { const clonedQueue = originalQueue.clone(); clonedQueue.push(3); - expect(clonedQueue.size).not.toBe(originalQueue.size); + expect(clonedQueue.length).not.toBe(originalQueue.length); expect(originalQueue.toArray()).not.toContain(3); }); }); @@ -345,14 +345,14 @@ describe('LinkedListQueue', () => { it('should push elements to the end of the queue', () => { expect(queue.first).toBe('A'); - expect(queue.size).toBe(2); + expect(queue.length).toBe(2); }); it('should shift elements from the front of the queue', () => { const dequeued = queue.shift(); expect(dequeued).toBe('A'); expect(queue.first).toBe('B'); - expect(queue.size).toBe(1); + expect(queue.length).toBe(1); }); it('should peek at the front of the queue', () => { @@ -362,6 +362,6 @@ describe('LinkedListQueue', () => { it('should clone method work correctly', () => { const cloned = queue.clone(); expect(cloned instanceof LinkedListQueue).toBe(true); - expect(cloned.size).toBe(2); + expect(cloned.length).toBe(2); }); }); diff --git a/test/unit/unrestricted-interconversion.test.ts b/test/unit/unrestricted-interconversion.test.ts index 791690b..858feb8 100644 --- a/test/unit/unrestricted-interconversion.test.ts +++ b/test/unit/unrestricted-interconversion.test.ts @@ -263,7 +263,7 @@ describe('conversions', () => { 'triangle' ]); const dq = new Deque(heap); - expect(dq.size).toBe(10); + expect(dq.length).toBe(10); if (isDebug) dq.print(); expect([...dq]).toEqual([ 'transmit',