#b-tree #vec #vector #array

no-std btree-vec

A growable array (vector) implemented using a B-tree

6 releases

0.3.1 Jan 7, 2023
0.3.0 Dec 20, 2022
0.2.8 Jan 7, 2023
0.2.7 Dec 23, 2022
0.1.0 Oct 27, 2021

#671 in Data structures

Download history 4/week @ 2023-10-14 14/week @ 2023-10-21 19/week @ 2023-10-28 5/week @ 2023-11-04 6/week @ 2023-11-11 7/week @ 2023-11-18 84/week @ 2023-11-25 17/week @ 2023-12-02 20/week @ 2023-12-09 4/week @ 2023-12-16 45/week @ 2023-12-23 3/week @ 2023-12-30 4/week @ 2024-01-06 8/week @ 2024-01-13 16/week @ 2024-01-20 68/week @ 2024-01-27

97 downloads per month
Used in btreelist

GPL-3.0-or-later

82KB
2K SLoC

btree-vec

This crate provides a growable array (vector) implemented using a B-tree (more specifically, a B+ tree). It provides non-amortized O(log n) random accesses, insertions, and removals, as well as O(n) iteration. The branching factor is also customizable.

The design is similar to unsorted counted B-trees as described by Simon Tatham.

For now, the vector supports insertions and removals only of single elements, but bulk operations, including implementations of Extend and FromIterator, may be added in the future.

Example

let mut vec = BTreeVec::new();
for i in 0..20 {
    vec.push(i);
}
for i in 0..10 {
    assert!(vec.remove(i) == i * 2);
}
for i in 0..10 {
    assert!(vec[i] == i * 2 + 1);
}
for i in 0..10 {
    vec.insert(i * 2, i * 2);
}
assert!(vec.len() == 20);
for (i, n) in vec.iter().copied().enumerate() {
    assert!(i == n);
}

Crate features

If the crate feature dropck_eyepatch is enabled, items in a BTreeVec can contain references with the same life as the vector itself. This requires Rust nightly, as the unstable language feature dropck_eyepatch must be used.

If the crate feature allocator_api is enabled, you can configure BTreeVec with the unstable Allocator trait. Alternatively, if the feature allocator-fallback is enabled, this crate will use the allocator API provided by allocator-fallback instead of the standard library’s.

Dependencies

~40KB