sortedvec
A pure rust library that exposes a single data type, SortedVec
. Its raison d'ĂȘtre is to
provide a lookup table that has quicker lookups than regular Vec
s, O(log(n))
vs O(n)
,
and is simpler and more memory efficient than hashmaps. It is ideal for (very) small
lookup tables where additions and deletions are infrequent.
Example
use SortedVec;
let unsorted = vec!;
let sorted = from_vec;
// linear search (slow!)
let unsorted_contains_six: = unsorted.iter.find;
assert!;
// binary search (fast!)
let sorted_contains_six: = sorted.find;
assert!;
Benchmarks
The table below displays how lookups scale on the standard library's HashMap
,
SortedVec
and Vec
for string and integer keys.
key type | size | HashMap |
SortedVec |
Vec |
---|---|---|---|---|
int | 2 | 28 | 1 | 1 |
int | 6 | 29 | 3 | 2 |
int | 10 | 28 | 4 | 3 |
int | 50 | 28 | 6 | 13 |
int | 100 | 33 | 7 | 25 |
int | 500 | 28 | 9 | 130 |
int | 1000 | 28 | 10 | 245 |
string | 2 | 32 | 13 | 5 |
string | 6 | 31 | 24 | 13 |
string | 10 | 32 | 30 | 23 |
string | 50 | 33 | 44 | 123 |
string | 100 | 31 | 51 | 231 |
string | 500 | 32 | 67 | 1,149 |
string | 1000 | 32 | 73 | 2,328 |