1.2.1 • Published 4 years ago
idati v1.2.1
idati
A set of data structures and algorithms commonly used in front-end development.
Install
npm install idati
# or
yarn add idatiWhat's in it?
Collection (Interface)
methods
clear()Removes all the elements from this collection.contains(e: E): booleanReturns true if this collection contains the specified element.isEmpty(): booleanReturns true if this collection contains no elements.size(): numberReturns the number of elements in this collection.toArray(): T[]Returns an array containing all the elements in this collection.
Stack\
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(); // 1Constructor
new Stack();
new Stack(['init', 'data']);Methods
push(e: E)Pushes an item onto the top of this stack.peek(): ELooks at the object at the top of this stack without removing it from the stack.pop(): ERetrieves and removes the top of this stack.
Queue\
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(); // 2Constructor
new Queue();
new Queue(['init', 'data']);Methods
offer(e: E)Inserts the specified element into this queue.peek(): ERetrieves, but does not remove, the head of this queue.poll(): ERetrieves and removes the head of this queue.
PriorityQueue\
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(): ERetrieves, but does not remove, the head of this queue.poll(): ERetrieves 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')); // undefinedConstructor
new cache(maxSize);Methods
set(key: K, value: V)Inserts entry into the cache and updates the " recently used".peek(key: K): VRetrieves the value for key from this cache.get(key: K): VRetrieves 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.