News for hackers. Stuff that matters.
Re:Ask HN: What are some cool but obscure data structures yo... by okennedy on Thursday July 21, 2022 @07:36PM (#32186603)
Linked Hash/Tree Maps, simple, but elegant. A Map with its nodes connected in a linked list so you can traverse them in insertion order (and O(n) time). Very useful for window queries over sequential data and other cases where you want FIFO access, but also quick access by a field of the data.
Re:Ask HN: What are some cool but obscure data structures yo... by H8crilA on Thursday July 21, 2022 @07:59PM (#32186814)
You forgot the most important feature over normal hash maps: they offer a deterministic iteration order without incurring the cost of a tree-based ordered map.

(If you don't know why this is important then maybe you haven't worked on large systems that undergo rigorous evaluation)

Re:Ask HN: What are some cool but obscure data structures yo... by okennedy on Friday July 22, 2022 @01:23PM (#32194577)
Modulo the snark, you're completely right. Iteration order = insertion order is super valuable for a bunch of reasons (this one included).
Re:Ask HN: What are some cool but obscure data structures yo... by ztorkelson on Thursday July 21, 2022 @08:54PM (#32187200)
Is (non-)determinism really the right concern here? I’m aware that most hash tables do not have generally predictable iteration orders, but I nevertheless understood them to be deterministic.
Re:Ask HN: What are some cool but obscure data structures yo... by ilammy on Thursday July 21, 2022 @10:24PM (#32187746)
They are deterministic in the sense that provided everything else is the same, iteration order would be the same after each insertion.

However, it can change after insertion (if the hash bucket count changes). If the hash function is random-keyed, iteration order would be different for different instances of the hash table even if you insert the same items in the same order.

Sometimes you want the order to be consistent every time. Be it insertion order, or some natural order of the items.

Re:Ask HN: What are some cool but obscure data structures yo... by xxs on Friday July 22, 2022 @03:36AM (#32189520)
>Is (non-)determinism really the right concern here?

A massive one - there is a lot of code that implicitly depends on the order of iteration (think configurations/initialization). The issue might be invisible, and reproduce rarely in production only. The iteration order depends on the hash of the keys, plus the capacity of the underlying array.

In Java I advise to never use the standard HashMap in favor of LinkedHashMap. In cases where data density is important both are a terrible fit having nodes instead of a plain object array (and linear probe/search).

Re:Ask HN: What are some cool but obscure data structures yo... by andyferris on Thursday July 21, 2022 @10:53PM (#32187928)
Well, hard/impossible to predict perhaps. Iteration order can depend on the order things were inserted and deleted and may differ from computer to computer (for example in Julia the hash-based dictionary ordering differs between 32 and 64 bit systems, and might change between versions of Julia, etc - you’d see the same thing with C++ unordered maps, etc, etc).
Re:Ask HN: What are some cool but obscure data structures yo... by okennedy on Friday July 22, 2022 @01:26PM (#32194609)
The same thing was a huge issue in Python until somewhere around 3.6, until they canonicalized the sort order.
Re:Ask HN: What are some cool but obscure data structures yo... by andyferris on Thursday July 21, 2022 @10:55PM (#32187939)
You can do this without a linked list and it can be quite performant, like python’s newest dictionary implementation (appeared around Python 3.6 or 3.7 from memory).
Re:Ask HN: What are some cool but obscure data structures yo... by xxs on Friday July 22, 2022 @03:38AM (#32189535)
Why would Linked Hash Map be a obscure one? It's been a part of java collection framework for over 20y. It's a standard for LRU caches as well.
Re:Ask HN: What are some cool but obscure data structures yo... by okennedy on Friday July 22, 2022 @01:22PM (#32194563)
Hence the "simple but good". It's not really obscure, but also not on the common path. I've lost track of the number of times I've pointed this one out to a colleague or student.