idati
v1.2.1
Published
A set of common used data structure.
Downloads
4
Maintainers
Readme
idati
A set of data structures and algorithms commonly used in front-end development.
Install
npm install idati
# or
yarn add idati
What's in it?
Collection (Interface)
methods
clear()
Removes all the elements from this collection.contains(e: E): boolean
Returns true if this collection contains the specified element.isEmpty(): boolean
Returns true if this collection contains no elements.size(): number
Returns the number of elements in this collection.toArray(): T[]
Returns an array containing all the elements in this collection.
Stack<E>
Order elements in a LIFO (last-in-first-out) manner.
Collection
Symbol.iterator
Example
import {Stack} from 'idati';
const stack = new Stack();
stack.push(1);
stack.push(2);
stack.pop(); // 2
stack.pop(); // 1
Constructor
new Stack();
new Stack(['init', 'data']);
Methods
push(e: E)
Pushes an item onto the top of this stack.peek(): E
Looks at the object at the top of this stack without removing it from the stack.pop(): E
Retrieves and removes the top of this stack.
Queue<E>
Order elements in a FIFO (first-in-first-out) manner.
Collection
Symbol.iterator
Example
import {Queue} from 'idati';
const queue = new Queue();
queue.offer(1);
queue.offer(2);
queue.poll(); // 1
queue.poll(); // 2
Constructor
new Queue();
new Queue(['init', 'data']);
Methods
offer(e: E)
Inserts the specified element into this queue.peek(): E
Retrieves, but does not remove, the head of this queue.poll(): E
Retrieves and removes the head of this queue.
PriorityQueue<E>
The elements of the priority queue are ordered according to the Comparator provided at queue construction time.
Collection
Symbol.iterator
Example
import {PriorityQueue} from 'idati';
const queue = new PriorityQueue<{ value: number }>(
(a, b) => b.value - a.value,
);
queue.offer({value: 3});
queue.offer({value: 5});
queue.offer({value: 1});
queue.poll(); // {value: 5}
queue.poll(); // {value: 3}
queue.poll(); // {value: 1}
Constructor
new PriorityQueue();
new PriorityQueue(initData);
new PriorityQueue(comparator);
new PriorityQueue(initData, comparator);
Methods
offer(e: E)
Inserts the specified element into this priority queue.peek(): E
Retrieves, but does not remove, the head of this queue.poll(): E
Retrieves and removes the head of this queue.
LRUCache<V, K>
A cache that holds a limited number of values, When a value is added to a full cache, that will delete the "least-recently-used" items.
Symbol.iterator
Example
import {LRUCache} from 'idati';
const cache = new LRUCache<number>(2); // max size 2
cache.set('a', 1);
cache.set('b', 2);
cache.set('c', 3); // deletes 'a'
cache.get('a'); // undefined
cache.get('b');
cache.set('d', 4); // deletes 'c'
console.log(cache.get('c')); // undefined
Constructor
new cache(maxSize);
Methods
set(key: K, value: V)
Inserts entry into the cache and updates the " recently used".peek(key: K): V
Retrieves the value for key from this cache.get(key: K): V
Retrieves the value for key from this cache and updates the "recently used".remove(key: K)
Removes the entry for key if it exists.clear()
Removes all the entry in the cache.size()
Returns the number of entries in the cache.toArray(): V[]
Return an array of the values in the cache.