0.0.1 • Published 7 years ago

seqnext-map v0.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

seqnext-map - Map that supports seqnext

Map

In ES6 and beyond, you have this type:

// ES standard
// my forEach doesn't include thisArg as it seems generally silly
interface Map<K, V> {
  clear(): void;
  delete(key: K): boolean;
  // forEach(callbackfn: (value: V, key: K, map: Map<K, V>) => void, thisArg?: any): void;
  forEach(cb: (value: V, key: K) => void): void;
  get(key: K): V | undefined;
  has(key: K): boolean;
  set(key: K, value: V): Map<K, V>; // this;
  readonly size: number;
}

I needed that type, but I was ES5. Also, I wanted direct control over the storage and access.

The map here offers

interface SeqNextMap<K, V> extends Map<K, V> {
  toSeq(): Seq<[K, V]>;
}

Here we offer the ablity to specificy a comparison function

createMap<K,V>(comparer?: (a: K, b: K) => number, items?: [K, V][]): SeqNextMap<K, V>;

If no comparer is provided, we'll just make K a string and go from there.

This map uses a tree.

Tree

If you ever take a computer science data structures course, you'll learn to love or hate all kinds of abstract trees.

You needn't know how they're done, you need only know that they are the best way to store and retrieve ordered data.

When I loaded a few thousands attributes into a Javascript object and it cried for mercy before crashing horribly, I knew I needed something more specific than what is essentially a hack. And, since I know I want unique keys, a structure that allowed for the most efficient accesss made sense. I found a few implmentations, but none were quite right. So, here's another.

interface Tree<T> {
  readonly isEmpty: boolean;
  get(value: T): T | undefined;
  set(value: T): Tree<T>;
  has(value: T): boolean;
  delete(value: T): boolean;
  clear(): void;
  toSeq(): Seq<T>;
}

createTree<T>(comparer?: (a: T, b: T) => number, items?: T[);

Currently, that tree just is a brain dead binary tree; no balance. Self balancing is to come.

seqnext

This comes from another library. Indeed, it was the tree itself that lead to the seqnext, as I wanted a lazy way to iterate over the structure if needed. A sequence paradigm is even more useful than the tree, so it gets top billing.

Test Code

Coming soon

Licence

MIT