Sweet Collections

Typescript implementations of in-memory cache data-structures for Node and Browser. These data-structures are:
- LruMap: A fixed size
Map which removes the least recently used entry.
- LruSet: A fixed size
Set which removes the least recently used entry. (Backed by a LruMap)
- LfuMap: A fixed size
Map which removes the least frequently used entry.
- LfuSet: A fixed size
Set which removes the least frequently used entry. (Backed by a LfuMap)
- SortedArray: An
Array that stays sorted after any modification.
- SortedMap: A
Map with entries stays sorted by key after any modification. (Backed by a SortedArray)
- SortedSet: A
Set with entries stays sorted after any modification. (Backed by a SortedArray)
- Heap: A collection that have always the largest, smallest or most relevant value on top.
- Stack: A collection that have always the last added value on top.
- Queue: A collection that have always the first added value on top.
Install
npm install --save sweet-collections
or
yarn add sweet-collections
Usage
LruMap
import { LruMap } from 'sweet-collections';
const map = new LruMap<number, number>(3);
map.set(1, 1);
map.set(2, 2);
map.set(3, 3);
console.log(map.has(1))
console.log(map.get(2))
map.set(4, 4);
console.log(map.has(3))
console.log(map.get(3))
console.log(map.isFull())
map.delete(1);
console.log(map.size)
map.clear();
console.log(map.size)
LruSet
import { LruSet } from 'sweet-collections';
const set = new LruSet<number>(3);
set.add(1);
set.add(2);
set.add(3);
console.log(set.has(1))
set.add(4);
console.log(set.has(2))
console.log(set.isFull())
set.delete(1);
console.log(set.size)
set.clear();
console.log(set.size)
LfuMap
import { LfuMap } from 'sweet-collections';
const map = new LfuMap<number, number>(3);
map.set(1, 1);
map.set(2, 2);
map.set(3, 3);
console.log(map.has(1))
console.log(map.get(2))
map.set(4, 4);
console.log(map.has(3))
console.log(map.get(3))
console.log(map.isFull())
map.delete(1);
console.log(map.size)
map.clear();
console.log(map.size)
LfuSet
import { LfuSet } from 'sweet-collections';
const set = new LfuSet<number>(3);
set.add(1);
set.add(2);
set.add(3);
console.log(set.has(1))
console.log(set.has(2))
set.add(4);
console.log(set.has(3))
console.log(set.isFull())
set.delete(1);
console.log(set.size)
set.clear();
console.log(set.size)
SortedArray
import { SortedArray } from 'sweet-collections';
const array = new SortedArray<number>((a: number, b: number) => a - b);
array.push(4);
array.push(1);
array.push(2);
array.push(3);
array.push(5);
console.log(array.toArray());
console.log(array.get(2));
console.log(array.get(4));
console.log(array.length);
array.delete(4);
console.log(array.toArray());
console.log(array.includes(4));
array.push(1);
console.log(array.toArray());
console.log(array.count(1));
console.log(array.firstIndexOf(1));
console.log(array.lastIndexOf(1));
console.log(array.shift());
console.log(array.pop());
console.log(array.min());
console.log(array.max());
console.log(array.toArray());
SortedMap
import { SortedMap } from 'sweet-collections';
const map = new SortedMap<number, string>((a: number, b: number) => a - b);
map.set(3, 'c');
map.set(2, 'd');
map.set(5, 'a');
map.set(4, 'b');
map.set(1, 'e');
console.log([...map.keys()]);
console.log([...map.values()]);
console.log(map.get(2));
console.log(map.get(4));
console.log(map.size);
map.delete(4);
console.log([...map.keys()]);
console.log([...map.values()]);
console.log(map.has(4));
SortedSet
import { SortedSet } from 'sweet-collections';
const set = new SortedSet<number>((a: number, b: number) => a - b);
set.add(3);
set.add(2);
set.add(5);
set.add(4);
set.add(1);
console.log([...set.keys()]);
console.log(set.has(2));
console.log(set.has(4));
console.log(set.size);
set.delete(4);
console.log([...set.keys()]);
console.log(set.has(4));
Heap
import { Heap } from 'sweet-collections';
const heap = new Heap<number>((a: number, b: number) => a > b);
heap.push(3);
heap.push(2);
heap.push(5);
heap.push(4);
heap.push(1);
console.log(heap.peek());
console.log(heap.pop());
console.log(heap.peek());
console.log(heap.replace(0));
console.log(heap.peek());
Stack
import { Stack } from 'sweet-collections';
const stack = new Stack<number>();
stack.push(3);
stack.push(2);
console.log(stack.top());
stack.push(5, 4, 1);
console.log(stack.pop());
console.log(stack.top());
console.log(stack.size);
Queue
import { Queue } from 'sweet-collections';
const queue = new Queue<number>();
queue.push(3);
queue.push(2);
console.log(queue.pop());
queue.push(5, 4, 1);
console.log(queue.pop());
console.log(queue.peek());
console.log(queue.size);
Author
👤 Nasreddine Bac Ali
Show your support
Give a ⭐️ if this project helped you!
📝 License
Copyright © 2021 Nasreddine Bac Ali. This project
is ISC licensed.