Live data from Hacker News

Ask HN: What are some cool but obscure data structures you know about?

news.ycombinator.com

371–380 of 772 posts

Re: Ask HN: What are some cool but obscure data structures you know about?

#371
Frugal streaming.

For estimating a median or percentile in a stream, using constant memory.

It's very simple: if the current value is higher than our estimate, then increase our estimate by one. Else decrease by one. It will converge over long enough time. This is called the Frugal-1U algorithm.

The Frugal-2U is a slightly more advanced version that modifies the step size along the way, plus other optimizations, to converge faster and not oscillate too much.

https://pastebin.com/wAcCS8WZ

Re: Ask HN: What are some cool but obscure data structures you know about?

#372

HAMT: Hash Array Mapped Trie. This data structure makes efficient immutable data possible. You can update a list of a million items, and keep a reference to the original list, by changing 3 or 4 references and some bytes. This should replace copy-on-write for scripting languages. I really want to see it in a JS spec soon. There are libraries that can do it, but they add translation penalties and extra steps. I’d comp…

This. Roughly a year ago I got interested in efficient immutability for my write-from-scratch-in-C Lisp [0] and started to write a HAMT implementation in C [1], along with a (somewhat hacky, you have been warned) benchmarking suite [2].

The docs are only 70% done (in particular the "putting it all together" part is missing) but it has been a really interesting and enlightening journey so far and can only recommend embarking on this path to everyone.

[0]: https://github.com/mkirchner/stutter [1]: https://github.com/mkirchner/hamt [2]: https://github.com/mkirchner/hamt-bench

Re: Ask HN: What are some cool but obscure data structures you know about?

#373
post #269

> Lets you test if a value is definitely NOT in a list of pre-stored values wouldn’t a hashmap / dict / object do this in 0(1)? Input goes in, is hashed, the key exists or doesn’t exist?

The point of a Bloom filter is that it takes constant memory. (The tradeoff is the possibility of false-positives, of course.)

Re: Ask HN: What are some cool but obscure data structures you know about?

#375

I like consistent hashing ( https://en.m.wikipedia.org/wiki/Consistent_hashing ). When a hash table (or load balancing pool) needs to be resized, it usually reduces the number of keys (clients) that need to be remapped.

Even simpler is Weighted Rendezvous Hashing ( https://www.snia.org/sites/default/files/SDC15_presentations... ). It's quite a bit easier to implement and verify than consistent hashing, and carries the same benefits (minimal reshuffling on ring resizes, etc)

I recently updated the Python example on the Wikipedia page for Weighted Rendezvous Hashing. (I'm amazed my edit seems to be still there.)

https://en.wikipedia.org/wiki/Rendezvous_hashing

Re: Ask HN: What are some cool but obscure data structures you know about?

#377

Not a very deep CS-y one, but still one of my favourite data structures: Promise Maps. It only works in languages where promises/futures/tasks are a first-class citizen. Eg JavaScript. When caching the result of an expensive computation or a network call, don't actually cache the result, but cache the promise that awaits the result. Ie don't make a Map but a Map > This way, if a new, uncached key gets requested twice…

I have written many tools that do something like this, very useful. Just gotta be careful with memory leaks, so (for a TS example) you might want to do something like this: const promiseMap > = new Map(); async function keyedDebounce (key: string, fn: () => R) { const existingPromise = promiseMap.get(key); if (existingPromise) return existingPromise; const promise = new Promise(async (resolve) => { const result = awa…

Could you use a WeakMap for this instead?

Re: Ask HN: What are some cool but obscure data structures you know about?

#378
post #344
post #331

Equality graphs (e-graphs) for theorem proving and equality saturation and other equality-related things. They're awesome data structures that efficiently maintain a congruence relation over many expressions > At a high level, e-graphs extend union-find to compactly represent equivalence classes of expressions while maintaining a key invariant: the equivalence relation is closed under congruence. e.g. If I were to re…

Isnt that the building blocks of logic based programming languages like Prolog?

I had a similar thought when I learned about e-graphs. I'm not sure yet, but I think congruence closure is a slightly different problem from the one Prolog unification solves. In particular, if you tell an e-graph that `f(x) = g(y)`, it will take you at your word -- while Prolog would give a unification error. (An e-graph should also handle `X = f(X)`, while this would fail Prolog's admittedly often-ignored occurs check.)

Re: Ask HN: What are some cool but obscure data structures you know about?

#379

Earlier quoted context omitted.

If it's not constant time on both ends, it's not a deque. You can already insert items at any index in an array with linear cost.

I invite you to think about the problem for a while, and try to think of a way to implement a performant deque on top of a resize-able array. I promise you it's possible.

I don't think the comment you replied to disagreed with that.

Re: Ask HN: What are some cool but obscure data structures you know about?

#380
Cuckoo filter

"A space-efficient probabilistic data structure that is used to test whether an element is a member of a set, like a Bloom filter does. False positive matches are possible, but false negatives are not – in other words, a query returns either "possibly in set" or "definitely not in set". A cuckoo filter can also delete existing items, which is not supported by Bloom filters. In addition, for applications that store many items and target moderately low false positive rates, cuckoo filters can achieve lower space overhead than space-optimized Bloom filters."

https://en.m.wikipedia.org/wiki/Cuckoo_filter

Post reply on HN