Name the addition method as "add" within the unified API

This commit is contained in:
Revone 2023-08-14 20:39:10 +08:00
parent 51361aa7d2
commit 4da59257e8
30 changed files with 56 additions and 54 deletions

1
.gitignore vendored
View file

@ -11,6 +11,7 @@ dist/
__tests__/
notes/
docs/
#.expo/*
#web-build/
#.idea/

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
import {BST, BSTNode} from './bst';

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
export class BinaryIndexedTree {

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
import type {BinaryTreeNodeId, BinaryTreeNodePropertyName, BSTComparator, BSTDeletedResult} from '../types';

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
import {BST, BSTNode} from './bst';

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
import {arrayRemove, uuidV4} from '../../utils';
@ -540,7 +540,7 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
}
const heap = new PriorityQueue<{ id: number, val: V }>({comparator: (a, b) => a.id - b.id});
heap.offer({id: 0, val: srcVertex});
heap.add({id: 0, val: srcVertex});
distMap.set(srcVertex, 0);
preMap.set(srcVertex, null);
@ -587,7 +587,7 @@ export abstract class AbstractGraph<V extends AbstractVertex, E extends Abstract
const distSrcToNeighbor = distMap.get(neighbor);
if (distSrcToNeighbor) {
if (dist + weight < distSrcToNeighbor) {
heap.offer({id: dist + weight, val: neighbor});
heap.add({id: dist + weight, val: neighbor});
preMap.set(neighbor, cur);
distMap.set(neighbor, dist + weight);
}

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
import {arrayRemove} from '../../utils';

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
import {arrayRemove} from '../../utils';

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
export class CoordinateMap<V> extends Map<any, V> {

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
export class CoordinateSet extends Set {

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
import {PriorityQueue} from '../priority-queue';
@ -59,37 +59,37 @@ export abstract class Heap<T> {
}
/**
* The `offer` function adds an element to a priority queue with an optional priority value.
* The `add` function adds an element to a priority queue with an optional priority value.
* @param {T} element - The `element` parameter represents the value that you want to add to the heap. It can be of any
* type.
* @param {number} [priority] - The `priority` parameter is an optional number that represents the priority of the
* element being offered to the heap. If the `element` parameter is a number, then the `priority` parameter is set to
* element being added to the heap. If the `element` parameter is a number, then the `priority` parameter is set to
* the value of `element`. If the `element` parameter is not a number, then the
* @returns The `offer` method returns the instance of the `Heap` class.
* @returns The `add` method returns the instance of the `Heap` class.
* @throws {Error} if priority is not a valid number
*/
offer(element: T, priority?: number): Heap<T> {
add(element: T, priority?: number): Heap<T> {
if (typeof element === 'number') {
priority = element;
} else {
if (priority === undefined) {
throw new Error('.offer expects a numeric priority');
throw new Error('.add expects a numeric priority');
}
}
if (priority && Number.isNaN(+priority)) {
throw new Error('.offer expects a numeric priority');
throw new Error('.add expects a numeric priority');
}
if (Number.isNaN(+priority) && Number.isNaN(this._priorityCb(element))) {
throw new Error(
'.offer expects a numeric priority '
'.add expects a numeric priority '
+ 'or a constructor callback that returns a number'
);
}
const _priority = !Number.isNaN(+priority) ? priority : this._priorityCb(element);
this._pq.offer({priority: _priority, element});
this._pq.add({priority: _priority, element});
return this;
}

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
import type {DoublyLinkedListGetBy} from '../types';
@ -34,7 +34,7 @@ export class DoublyLinkedList<T> {
* the doubly linked list.
* @returns A boolean value is being returned.
*/
offerFirst(val: T): boolean {
addFirst(val: T): boolean {
const newNode = new DoublyLinkedListNode(val);
if (this._size === 0) {
this._first = newNode;
@ -54,7 +54,7 @@ export class DoublyLinkedList<T> {
* doubly linked list.
* @returns a boolean value, which is always true.
*/
offerLast(val: T): boolean {
addLast(val: T): boolean {
const newNode = new DoublyLinkedListNode(val);
if (this._size === 0) {
this._first = newNode;
@ -247,8 +247,8 @@ export class DoublyLinkedList<T> {
*/
insert(index: number, val: T): boolean {
if (index < 0 || index > this._size) return false;
if (index === 0) return !!this.offerFirst(val);
if (index === this._size) return !!this.offerLast(val);
if (index === 0) return !!this.addFirst(val);
if (index === this._size) return !!this.addLast(val);
const newNode = new DoublyLinkedListNode(val);
const prevNode = this.get(index - 1, 'node');

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
// todo need to be improved

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
import Vector2D from './vector2d'

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
import type {Direction, NavigatorParams, Turning} from '../types';

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
export class Vector2D {

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
import {PriorityQueue} from './priority-queue';

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
import {PriorityQueue} from './priority-queue';

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
import type {PriorityQueueComparator, PriorityQueueDFSOrderPattern, PriorityQueueOptions} from '../types';
@ -52,11 +52,11 @@ export class PriorityQueue<T = number> {
}
/**
* The "offer" function adds a node to the heap and ensures that the heap property is maintained.
* The "add" function adds a node to the heap and ensures that the heap property is maintained.
* @param {T} node - The parameter "node" is of type T, which means it can be any data type. It represents the node
* that needs to be added to the heap.
*/
offer(node: T) {
add(node: T) {
this.nodes.push(node);
this._heapifyUp(this.size - 1);
}

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
import {DoublyLinkedList} from '../linked-list';
@ -28,7 +28,7 @@ export class ObjectDeque<T> {
return this._size;
}
offerFirst(value: T) {
addFirst(value: T) {
if (this._size === 0) {
const mid = Math.floor(this._capacity / 2);
this._first = mid;
@ -40,7 +40,7 @@ export class ObjectDeque<T> {
this._size++;
}
offerLast(value: T) {
addLast(value: T) {
if (this._size === 0) {
const mid = Math.floor(this._capacity / 2);
this._first = mid;
@ -98,11 +98,11 @@ export class ArrayDeque<T> {
}
/**
* The function "offerLast" adds a value to the end of an array.
* The function "addLast" adds a value to the end of an array.
* @param {T} value - The value parameter represents the value that you want to add to the end of the array.
* @returns The return value is the new length of the array after the value has been added.
*/
offerLast(value: T) {
addLast(value: T) {
return this._nodes.push(value);
}
@ -124,12 +124,12 @@ export class ArrayDeque<T> {
}
/**
* The function "offerFirst" adds a value to the beginning of an array.
* The function "addFirst" adds a value to the beginning of an array.
* @param {T} value - The value parameter represents the value that you want to add to the beginning of the array.
* @returns The return value of the `offerFirst` function is the new length of the array `_nodes` after adding the
* @returns The return value of the `addFirst` function is the new length of the array `_nodes` after adding the
* `value` at the beginning.
*/
offerFirst(value: T) {
addFirst(value: T) {
return this._nodes.unshift(value);
}

View file

@ -1,6 +1,6 @@
/**
* @license MIT
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @class
*/
export class Queue<T> {
@ -31,11 +31,11 @@ export class Queue<T> {
}
/**
* The offer function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
* The add function adds an element to the end of the queue and returns the updated queue.Adds an element at the back of the queue.
* @param {T} element - The `element` parameter represents the element that you want to add to the queue.
* @returns The `offer` method is returning a `Queue<T>` object.
* @returns The `add` method is returning a `Queue<T>` object.
*/
offer(element: T): Queue<T> {
add(element: T): Queue<T> {
this._nodes.push(element);
return this;
}

View file

@ -1,6 +1,6 @@
/**
* @license MIT
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @class
*/
export class Stack<T> {

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
export class TrieNode {

View file

@ -1,5 +1,5 @@
/**
* @copyright 2030 Tyler Zeng <zrwusa@gmail.com>
* @copyright Tyler Zeng <zrwusa@gmail.com>
* @license MIT
*/
import {Thunk, ToThunkFn, TrlAsyncFn, TrlFn} from './types';

View file

@ -1,5 +1,6 @@
import { DirectedGraph, DirectedVertex, DirectedEdge } from '../../../../src';
// TODO too few unit tests
describe('DirectedGraph', () => {
let graph: DirectedGraph<DirectedVertex, DirectedEdge>;