zandaqo / structurae

Data structures for high-performance JavaScript applications.
MIT License
701 stars 21 forks source link

Feature request: Sorted Map #32

Open vanodevium opened 1 year ago

vanodevium commented 1 year ago

@zandaqo Thank you for your great work! These structures are awesome!

Could you please make a new data structure SortedMap? Or it is possible to do it from existing structures and I just didn't read it?

zandaqo commented 1 year ago

Wouldn't a sorted array of objects be effectively that? Something like this:

class SortedMap<K, T> extends SortedArray<{ key: K, value: T}> {
  static compare<K, T>(a: { key: K, value: T}, b: { key: K, value: T}): -1 | 0 | 1 {
    if (a.key > b.key) return 1;
    if (a.key < b.key) return -1;
    if (a.key === b.key) return 0;
    throw new RangeError("Unstable comparison.");
  }
}
vanodevium commented 1 year ago

This is a good choice, but not for all tasks. I have a business logic where I have to store over 3M items and I really need a key-value structure for my business cases.