refactor: rename method 'getAt' to 'at'

This commit is contained in:
Revone 2023-12-30 20:36:11 +08:00
parent ff8f907192
commit 831ad3bd98
11 changed files with 55 additions and 55 deletions

View file

@ -768,21 +768,21 @@ export class BinaryTree<
* Space Complexity: O(log n).
*/
has<C extends BTNCallback<N, K>>(
override has<C extends BTNCallback<N, K>>(
identifier: K,
callback?: C,
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
iterationType?: IterationType
): boolean;
has<C extends BTNCallback<N, N>>(
override has<C extends BTNCallback<N, N>>(
identifier: N | null | undefined,
callback?: C,
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
iterationType?: IterationType
): boolean;
has<C extends BTNCallback<N>>(
override has<C extends BTNCallback<N>>(
identifier: ReturnType<C> | null | undefined,
callback: C,
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
@ -925,21 +925,21 @@ export class BinaryTree<
}
}
get<C extends BTNCallback<N, K>>(
override get<C extends BTNCallback<N, K>>(
identifier: K,
callback?: C,
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
iterationType?: IterationType
): V | undefined;
get<C extends BTNCallback<N, N>>(
override get<C extends BTNCallback<N, N>>(
identifier: N | null | undefined,
callback?: C,
beginRoot?: KeyOrNodeOrEntry<K, V, N>,
iterationType?: IterationType
): V | undefined;
get<C extends BTNCallback<N>>(
override get<C extends BTNCallback<N>>(
identifier: ReturnType<C>,
callback: C,
beginRoot?: KeyOrNodeOrEntry<K, V, N>,

View file

@ -132,7 +132,7 @@ export class HashMap<K = any, V = any, R = [K, V]> extends IterableEntryBase<K,
* @returns The method `get(key: K)` returns a value of type `V` if the key exists in the `_objMap`
* or `_store`, otherwise it returns `undefined`.
*/
get(key: K): V | undefined {
override get(key: K): V | undefined {
if (this._isObjKey(key)) {
return this._objMap.get(key);
} else {
@ -425,16 +425,6 @@ export class LinkedHashMap<K = any, V = any> extends IterableEntryBase<K, V> {
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<K = any, V = any> extends IterableEntryBase<K, V> {
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<K = any, V = any> extends IterableEntryBase<K, V> {
* 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--) {

View file

@ -231,13 +231,13 @@ export class DoublyLinkedList<E = any> extends IterableElementBase<E> {
* 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++) {

View file

@ -258,13 +258,13 @@ export class SinglyLinkedList<E = any> extends IterableElementBase<E> {
* 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++) {

View file

@ -239,7 +239,7 @@ export class Deque<E> extends IterableElementBase<E> {
* begin(): Generator<E> {
let index = 0;
while (index < this.size) {
yield this.getAt(index);
yield this.at(index);
index++;
}
}
@ -251,7 +251,7 @@ export class Deque<E> extends IterableElementBase<E> {
* reverseBegin(): Generator<E> {
let index = this.size - 1;
while (index >= 0) {
yield this.getAt(index);
yield this.at(index);
index--;
}
}
@ -265,13 +265,13 @@ export class Deque<E> extends IterableElementBase<E> {
* 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<E> extends IterableElementBase<E> {
} 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<E> extends IterableElementBase<E> {
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<E> extends IterableElementBase<E> {
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<E> extends IterableElementBase<E> {
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<E> extends IterableElementBase<E> {
*/
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<E> extends IterableElementBase<E> {
*/
protected* _getIterator(): IterableIterator<E> {
for (let i = 0; i < this.size; ++i) {
yield this.getAt(i);
yield this.at(i);
}
}

View file

@ -219,7 +219,7 @@ export class Queue<E = any> extends IterableElementBase<E> {
*
* @param index
*/
getAt(index: number): E | undefined {
at(index: number): E | undefined {
return this.elements[index];
}

View file

@ -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', () => {

View file

@ -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);
});

View file

@ -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();
});
});

View file

@ -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', () => {

View file

@ -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', () => {