diff --git a/src/data-structures/binary-tree/binary-tree.ts b/src/data-structures/binary-tree/binary-tree.ts index b3494f4..72de2cd 100644 --- a/src/data-structures/binary-tree/binary-tree.ts +++ b/src/data-structures/binary-tree/binary-tree.ts @@ -768,21 +768,21 @@ export class BinaryTree< * Space Complexity: O(log n). */ - has>( + override has>( identifier: K, callback?: C, beginRoot?: KeyOrNodeOrEntry, iterationType?: IterationType ): boolean; - has>( + override has>( identifier: N | null | undefined, callback?: C, beginRoot?: KeyOrNodeOrEntry, iterationType?: IterationType ): boolean; - has>( + override has>( identifier: ReturnType | null | undefined, callback: C, beginRoot?: KeyOrNodeOrEntry, @@ -925,21 +925,21 @@ export class BinaryTree< } } - get>( + override get>( identifier: K, callback?: C, beginRoot?: KeyOrNodeOrEntry, iterationType?: IterationType ): V | undefined; - get>( + override get>( identifier: N | null | undefined, callback?: C, beginRoot?: KeyOrNodeOrEntry, iterationType?: IterationType ): V | undefined; - get>( + override get>( identifier: ReturnType, callback: C, beginRoot?: KeyOrNodeOrEntry, diff --git a/src/data-structures/hash/hash-map.ts b/src/data-structures/hash/hash-map.ts index 9d7a56e..59a271c 100644 --- a/src/data-structures/hash/hash-map.ts +++ b/src/data-structures/hash/hash-map.ts @@ -132,7 +132,7 @@ export class HashMap extends IterableEntryBase extends IterableEntryBase { return true; } - has(key: K): boolean { - if (isWeakKey(key)) { - const hash = this._objHashFn(key); - return this._objMap.has(hash); - } else { - const hash = this._hashFn(key); - return hash in this._noObjMap; - } - } - setMany(entries: Iterable<[K, V]>): boolean[] { const results: boolean[] = []; for (const entry of entries) { @@ -444,6 +434,16 @@ export class LinkedHashMap extends IterableEntryBase { return results; } + override has(key: K): boolean { + if (isWeakKey(key)) { + const hash = this._objHashFn(key); + return this._objMap.has(hash); + } else { + const hash = this._hashFn(key); + return hash in this._noObjMap; + } + } + /** * Time Complexity: O(1) * Space Complexity: O(1) @@ -473,14 +473,14 @@ export class LinkedHashMap extends IterableEntryBase { * Time Complexity: O(n), where n is the index. * Space Complexity: O(1) * - * The function `getAt` retrieves the key-value pair at a specified index in a linked list. + * The function `at` retrieves the key-value pair at a specified index in a linked list. * @param {number} index - The index parameter is a number that represents the position of the * element we want to retrieve from the data structure. - * @returns The method `getAt(index: number)` is returning an array containing the key-value pair at + * @returns The method `at(index: number)` is returning an array containing the key-value pair at * the specified index in the data structure. The key-value pair is represented as a tuple `[K, V]`, * where `K` is the key and `V` is the value. */ - getAt(index: number): V | undefined { + at(index: number): V | undefined { rangeCheck(index, 0, this._size - 1); let node = this._head; while (index--) { diff --git a/src/data-structures/linked-list/doubly-linked-list.ts b/src/data-structures/linked-list/doubly-linked-list.ts index 8298e0f..236eb9b 100644 --- a/src/data-structures/linked-list/doubly-linked-list.ts +++ b/src/data-structures/linked-list/doubly-linked-list.ts @@ -231,13 +231,13 @@ export class DoublyLinkedList extends IterableElementBase { * Time Complexity: O(n), where n is the number of elements in the linked list. * Space Complexity: O(1) * - * The `getAt` function returns the value at a specified index in a linked list, or undefined if the index is out of bounds. + * The `at` function returns the value at a specified index in a linked list, or undefined if the index is out of bounds. * @param {number} index - The index parameter is a number that represents the position of the element we want to * retrieve from the list. * @returns The method is returning the value at the specified index in the linked list. If the index is out of bounds * or the linked list is empty, it will return undefined. */ - getAt(index: number): E | undefined { + at(index: number): E | undefined { if (index < 0 || index >= this.size) return undefined; let current = this.head; for (let i = 0; i < index; i++) { diff --git a/src/data-structures/linked-list/singly-linked-list.ts b/src/data-structures/linked-list/singly-linked-list.ts index 8c064b3..963034e 100644 --- a/src/data-structures/linked-list/singly-linked-list.ts +++ b/src/data-structures/linked-list/singly-linked-list.ts @@ -258,13 +258,13 @@ export class SinglyLinkedList extends IterableElementBase { * Time Complexity: O(n) - Linear time, where n is the index, as it may need to traverse the list to find the desired node. * Space Complexity: O(1) - Constant space. * - * The function `getAt` returns the value at a specified index in a linked list, or undefined if the index is out of range. + * The function `at` returns the value at a specified index in a linked list, or undefined if the index is out of range. * @param {number} index - The index parameter is a number that represents the position of the element we want to * retrieve from the list. - * @returns The method `getAt(index: number): E | undefined` returns the value at the specified index in the linked list, or + * @returns The method `at(index: number): E | undefined` returns the value at the specified index in the linked list, or * `undefined` if the index is out of bounds. */ - getAt(index: number): E | undefined { + at(index: number): E | undefined { if (index < 0 || index >= this.size) return undefined; let current = this.head; for (let i = 0; i < index; i++) { diff --git a/src/data-structures/queue/deque.ts b/src/data-structures/queue/deque.ts index ac6c2c5..6f62b3c 100644 --- a/src/data-structures/queue/deque.ts +++ b/src/data-structures/queue/deque.ts @@ -239,7 +239,7 @@ export class Deque extends IterableElementBase { * begin(): Generator { let index = 0; while (index < this.size) { - yield this.getAt(index); + yield this.at(index); index++; } } @@ -251,7 +251,7 @@ export class Deque extends IterableElementBase { * reverseBegin(): Generator { let index = this.size - 1; while (index >= 0) { - yield this.getAt(index); + yield this.at(index); index--; } } @@ -265,13 +265,13 @@ export class Deque extends IterableElementBase { * Time Complexity: O(1) * Space Complexity: O(1) * - * The `getAt` function retrieves an element at a specified position in an array-like data structure. + * The `at` function retrieves an element at a specified position in an array-like data structure. * @param {number} pos - The `pos` parameter represents the position of the element that you want to * retrieve from the data structure. It is of type `number` and should be a valid index within the * range of the data structure. * @returns The element at the specified position in the data structure is being returned. */ - getAt(pos: number): E { + at(pos: number): E { rangeCheck(pos, 0, this.size - 1); const { bucketIndex, indexInBucket } = this._getBucketAndPosition(pos); return this._buckets[bucketIndex][indexInBucket]!; @@ -329,7 +329,7 @@ export class Deque extends IterableElementBase { } else { const arr: E[] = []; for (let i = pos; i < this.size; ++i) { - arr.push(this.getAt(i)); + arr.push(this.at(i)); } this.cut(pos - 1); for (let i = 0; i < num; ++i) this.push(element); @@ -420,7 +420,7 @@ export class Deque extends IterableElementBase { let i = 0; let index = 0; while (i < size) { - const oldElement = this.getAt(i); + const oldElement = this.at(i); if (oldElement !== element) { this.setAt(index, oldElement!); index += 1; @@ -475,9 +475,9 @@ export class Deque extends IterableElementBase { return this; } let index = 1; - let prev = this.getAt(0); + let prev = this.at(0); for (let i = 1; i < this.size; ++i) { - const cur = this.getAt(i); + const cur = this.at(i); if (cur !== prev) { prev = cur; this.setAt(index++, cur); @@ -505,7 +505,7 @@ export class Deque extends IterableElementBase { sort(comparator?: (x: E, y: E) => number): this { const arr: E[] = []; for (let i = 0; i < this.size; ++i) { - arr.push(this.getAt(i)); + arr.push(this.at(i)); } arr.sort(comparator); for (let i = 0; i < this.size; ++i) { @@ -567,7 +567,7 @@ export class Deque extends IterableElementBase { */ indexOf(element: E): number { for (let i = 0; i < this.size; ++i) { - if (this.getAt(i) === element) { + if (this.at(i) === element) { return i; } } @@ -734,7 +734,7 @@ export class Deque extends IterableElementBase { */ protected* _getIterator(): IterableIterator { for (let i = 0; i < this.size; ++i) { - yield this.getAt(i); + yield this.at(i); } } diff --git a/src/data-structures/queue/queue.ts b/src/data-structures/queue/queue.ts index e953ec6..3d0f1a3 100644 --- a/src/data-structures/queue/queue.ts +++ b/src/data-structures/queue/queue.ts @@ -219,7 +219,7 @@ export class Queue extends IterableElementBase { * * @param index */ - getAt(index: number): E | undefined { + at(index: number): E | undefined { return this.elements[index]; } diff --git a/test/unit/data-structures/hash/hash-map.test.ts b/test/unit/data-structures/hash/hash-map.test.ts index fe23133..47c262e 100644 --- a/test/unit/data-structures/hash/hash-map.test.ts +++ b/test/unit/data-structures/hash/hash-map.test.ts @@ -385,7 +385,7 @@ describe('LinkedHashMap', () => { expect(hashMap.last).toEqual([key, value]); expect(hashMap.reverseBegin().next().value).toEqual([key, value]); } else if (index <= 1000) { - expect(hashMap.getAt(index)).toBe(value); + expect(hashMap.at(index)).toBe(value); } expect(hashMap.get(key)).toEqual(value); index++; @@ -434,7 +434,7 @@ describe('LinkedHashMap', () => { test('should get element at specific index', () => { hashMap.set('key1', 'value1'); hashMap.set('key2', 'value2'); - expect(hashMap.getAt(1)).toBe('value2'); + expect(hashMap.at(1)).toBe('value2'); }); describe('LinkedHashMap basic', () => { diff --git a/test/unit/data-structures/linked-list/doubly-linked-list.test.ts b/test/unit/data-structures/linked-list/doubly-linked-list.test.ts index 49a0444..bc6cd6f 100644 --- a/test/unit/data-structures/linked-list/doubly-linked-list.test.ts +++ b/test/unit/data-structures/linked-list/doubly-linked-list.test.ts @@ -101,19 +101,19 @@ describe('DoublyLinkedList Operation Test', () => { // Inserting at the beginning list.addAt(0, 0); expect(list.size).toBe(4); - expect(list.getAt(0)).toBe(0); - expect(list.getAt(1)).toBe(1); + 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.getAt(2)).toBe(1.5); - expect(list.getAt(3)).toBe(2); + 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.getAt(5)).toBe(4); + expect(list.at(5)).toBe(4); expect(list.tail!.value).toBe(4); }); 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 d2dd260..ac7e7aa 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 @@ -72,14 +72,14 @@ describe('SinglyLinkedList Operation Test', () => { list.push(1); list.push(2); list.push(3); - const element = list.getAt(1); + const element = list.at(1); expect(element).toBe(2); expect(list.getNodeAt(2)?.value).toBe(3); }); it('should return undefined for an out-of-bounds index', () => { list.push(1); - const element = list.getAt(1); + const element = list.at(1); expect(element).toBeUndefined(); }); }); diff --git a/test/unit/data-structures/queue/deque.test.ts b/test/unit/data-structures/queue/deque.test.ts index a12135b..2e87965 100644 --- a/test/unit/data-structures/queue/deque.test.ts +++ b/test/unit/data-structures/queue/deque.test.ts @@ -40,14 +40,14 @@ describe('Deque - Basic Operations', () => { expect(deque.isEmpty()).toBeTruthy(); }); - test('getAt should retrieve the correct element', () => { - expect(deque.getAt(0)).toBe(1); - expect(deque.getAt(1)).toBe(2); + test('at should retrieve the correct element', () => { + expect(deque.at(0)).toBe(1); + expect(deque.at(1)).toBe(2); }); test('setAt should set the correct element', () => { deque.setAt(0, 3); - expect(deque.getAt(0)).toBe(3); + expect(deque.at(0)).toBe(3); }); }); describe('Deque - Complex Operations', () => { diff --git a/test/unit/data-structures/queue/queue.test.ts b/test/unit/data-structures/queue/queue.test.ts index 5e945fb..5502dc6 100644 --- a/test/unit/data-structures/queue/queue.test.ts +++ b/test/unit/data-structures/queue/queue.test.ts @@ -156,17 +156,17 @@ describe('Queue - Additional Methods', () => { expect(queue.peekLast()).toBeUndefined(); }); - test('getAt should return the element at the specified index', () => { + test('at should return the element at the specified index', () => { queue.push(1); queue.push(2); queue.push(3); - expect(queue.getAt(1)).toBe(2); + expect(queue.at(1)).toBe(2); }); - test('getAt should return undefined for an invalid index', () => { + test('at should return undefined for an invalid index', () => { queue.push(1); - expect(queue.getAt(3)).toBeUndefined(); - expect(queue.getAt(-1)).toBeUndefined(); + expect(queue.at(3)).toBeUndefined(); + expect(queue.at(-1)).toBeUndefined(); }); test('print should not throw any errors', () => {