skiplist
Skip list collections with $O(\log n)$ average-case performance for access, insertion, and removal. Four variants cover the common use cases: positional sequences, sorted bags, sorted sets, and sorted maps, all with pluggable ordering via a Comparator trait.
[!NOTE]
Version 1.0.0 is a complete rewrite.
The 1.0.0 release shares no code with the 0.x series. The internal architecture, public API, and crate structure have all changed. If you are upgrading from 0.x, treat this as a new dependency and review the documentation from scratch rather than diffing against the old API.
Adding to your project
[]
= "1"
To use the PartialOrdComparator (for types that implement PartialOrd but not Ord, such as f64), enable the partial-ord feature:
[]
= { = "1", = ["partial-ord"] }
[!WARNING]
PartialOrdComparatorpanics at runtime if a comparison returnsNone(e.g. when aNaNis inserted or looked up). For floating-point keys, preferFnComparatorwithf64::total_cmp, which provides a true total order with no panics.
Basic usage
use ;
// Positional sequence - insertion order is preserved
let mut list: = new;
list.push_back;
list.push_back;
list.insert; // insert at index 1
assert_eq!;
// Sorted bag - elements are always kept in order, duplicates allowed
let mut ordered: = new;
ordered.insert;
ordered.insert;
ordered.insert; // duplicate is kept
assert_eq!;
// Sorted set - like OrderedSkipList but duplicates are rejected
let mut set: = new;
set.insert;
set.insert;
set.insert; // no-op: already present
assert_eq!;
// Sorted map - unique keys, sorted by key
let mut map: = new;
map.insert;
map.insert;
assert_eq!;
Custom ordering
Ordered collections accept any comparator via the FnComparator wrapper - no newtype required:
use ;
// Sort strings by length, then lexicographically
let cmp = FnComparator;
let mut list = with_comparator;
list.insert;
list.insert;
list.insert;
// Iteration order: "fig", "apple", "banana"
Collections
| Collection | Ordering | Duplicates | Docs |
|---|---|---|---|
SkipList |
Insertion order | Yes | docs.rs |
OrderedSkipList |
Sorted by comparator | Yes | docs.rs |
SkipSet |
Sorted by comparator | No | docs.rs |
SkipMap |
Sorted by key comparator | No (unique keys) | docs.rs |
SkipList stores elements in insertion order. It does not require elements to implement Ord. Use it when you need a positional sequence with cheap $O(\log n)$ insertion and removal anywhere in the list, not just at the ends.
OrderedSkipList always keeps elements in sorted order and tolerates duplicates. Unlike BTreeSet, inserting the same value twice places two adjacent entries in the list.
SkipSet wraps OrderedSkipList and enforces uniqueness: inserting a value that already exists is a no-op. It mirrors the BTreeSet API.
SkipMap stores unique key-value pairs sorted by key. Inserting a duplicate key replaces the existing value, identical to BTreeMap semantics.
Full API documentation is on docs.rs.
License
MIT