Heap, a.k.a. priority_queue, is a very common data structure in most languages.
LANG | LIB |
---|---|
C++ | std::priority_queue, Boost.Heap |
Python | Lib heapq.py |
Rust | binary_heap |
Go | /src/container/heap |
Dijkstra's algorithm for finding the shortest path in weighted graph
A randomlized serach tree. Fast in practice.
LANG | LIB |
---|---|
C++ | Boost.Intrusive.treap |
chapel-lang/chapel#15658 (comment)
Same as Skip List
Another record, TreapMap
, same as Map
but with Treap
as the underlying structure
Red-Black Tree alternative.
Used in many popular projects like LevelDB, Reddis, Bigtable
SkipList | Red-Black Tree |
---|---|
Larger constant factor, but fast in practice | |
Easy to implement | Very difficut to implement |
Could be lock-free | Need a lock |
With expected time complexity, not stable | With upper bound complexity |
And Skip List will on expectation use less space than all balanced trees.
In practice, it's fairly fast
So between Skip List and Treap, it's a time-space trade off.
eltType
record comparator
size
insert(x: eltType)
: insert an element.contains(x: eltType): bool
: return if x is in the listlower_bound(x: eltType, out result: eltType): bool
same asstd::lower_bound()
, return false if no occurence.upper_bound(x: eltType, out result: eltType): bool
same asstd::upper_bound()
, return false if no occurence.remove(x: eltType): bool
: remove x in the list. Does nothing if not presentthese()
: iterate over the listthis(idx: int)
: return k-th element in the sorted sequence, throw an error if out of rangekth(idx: int)
: return k-th element in the sorted sequence, throw an error if out of rangetoArray
: convert to an arrayclear()
predecessor(x: eltType, out result: eltType): bool
return the predecessor of one element, return false if it's the first one.successor(x: eltType, out result: eltType): bool
return the successor of one element, return false if it's the last one.
SkipListMap
same as Map
but with SkipList
as the underlying structure
A vector-based list is a list that store elements in continuous area. Vector can improve cache performane and have smaller constant factor.
Whether it's faster than list depends on the operation pattern and ratio. Vector
is faster at accessing but slower at inserting than List
.
With properly reserve
, Vector
can be much faster than List
.
LANG | LIB |
---|---|
C++ | std::vector |
Rust | Vec |
Vector | List.chpl |
---|---|
Better cache performance | |
Friendly for compiler optimization and prefetch | |
Expensive copy penalty for appending | No copy penalty for appending |
No memory overhead | Little memory overhead |
Fast random access | Slower random access since calling log2 |
After digging into the source of List.chpl
, I don't think it's a good idea to put them together because they have a very different logic.
However, an identical interface is possible.
same as List
No sight of it in other langauges or popular libraries.
The use case would be on a machine whose memory is small and cahce miss is expensive. Users can trade off between time and space by specifying the number of elements in one node.
Unrolled | Normal |
---|---|
Less memory overhead | |
Slower insertion and deletion in the same node | |
Better cache performance |
I think it could be more useful if support slicing/linking nodes.
No sight of it in other languages or popular libraries.
As for the use case, I have saw one of its variants used in algorithm contest, but haven't saw it elsewhere. Maybe users would like to have more control over it when solving a algorithm problem. So I suppose it should be implemented by users themselves.
I wonder are there other use cases and is Interval Tree module necessary?
I would like to get back to this if I have spare time after completing all above.
LANG | LIB |
---|---|
Java | org.apache.commons.collections4.trie |
A search tree. Similar to Treap.
A queue based on an fixed length array.
LANG | LIB |
---|---|
C++ | Boost.CircularBuffer |
- Need a queue, whose maximal length is known.
- Need random access
Buffer | LinkedList |
---|---|
Fixed space. Can't resize | Dynamic space |
Faster | |
Better cache performance | |
Random access | Sequential access |