Crates.io | indexset |
lib.rs | indexset |
version | 0.5.0 |
source | src |
created_at | 2023-07-04 17:56:50.139555 |
updated_at | 2024-09-18 18:01:48.035674 |
description | A two-level BTree with fast iteration and indexing operations |
homepage | |
repository | https://github.com/brurucy/indexset |
max_upload_size | |
id | 908232 |
size | 198,233 |
A pure-Rust two-level dynamic order-statistic b-tree.
This crate implements a compact set data structure that preserves its elements' sorted order and allows lookups of entries by value or sorted order position.
Also, it is(mostly) a drop-in replacement for the stdlib BTree.
Under the feature concurrent
you can find a persistent
version of the BTree that can be fearlessly shared between
threads.
This was heavily inspired by indexmap
, and
python's sortedcontainers
.
It differs from both in that:
indexmap
is a hashmap that provides numerical lookups, but does not maintain order in case of removals, while
indexset
is a b-tree that always maintains order, irrespective of which mutating operation is run.sortecontainers
is similar in spirit, but utilizes a different routine for balancing the tree, and relies
on a heap for numerical lookups.indexset
provides the following features:
select
(lookups by position) and rank
operations in near constant time.BTreeSet
and BTreeMap
derive a couple of performance facts directly from how it is constructed, which is roughly:
A two-level B-Tree with a fenwick tree as a low-cost index for numerical lookups
run cargo bench
and see it for yourself.
On a lowest-specced M1 macbook pro I got the following numbers:
stdlib::collections::BTreeSet.insert(i)
: 8.25msindexset::BTreeSet.insert(i)
: 17.3msstdlib::collections::BTreeSet.contains(i)
: 7.5msindexset::BTreeSet.contains(i)
: 6.8msstdlib::collections::BTreeSet.iter.nth(i)
: 13.28s yes, seconds!indexset::BTreeSet.get_index(i)
: 3.93msVec::from_iter(stdlib::collections::BTreeSet.iter())
: 227.28usVec::from_iter(indexset::BTreeSet.iter())
: 123.21.usYes.
Getting the i-th element is 3400x faster than stdlib's btree, contains
is 10% faster, and iterating
is twice as fast, at the cost of insertions being half as fast.
If your use case of std::collections::BTreeSet
and BTreeMap
is more read-heavy, or if you really need to index by
sorted-order position, it might be worth checking out this indexset
instead.
BTreeMap
is less polished than BTreeSet
. This crate has been optimised for a leaner BTreeSet
.Concurrent
BTreeMap
and BtreeSet
are lock-free and wait-free. That doesn't mean it is fast however :)This library is called indexset
, because the base data structure is BTreeSet
. BTreeMap
is a BTreeSet
with
a Pair<K, V>
item type.
See CHANGELOG.md.