10 releases (2 stable)

1.0.1 Jul 2, 2020
0.6.0 Jun 16, 2020
0.5.2 Jun 15, 2020
0.5.1 Feb 23, 2020
0.5.0 Nov 15, 2019

#1521 in Data structures

Download history 604/week @ 2024-06-19 1196/week @ 2024-06-26 575/week @ 2024-07-03 539/week @ 2024-07-10 695/week @ 2024-07-17 1021/week @ 2024-07-24 1243/week @ 2024-07-31 632/week @ 2024-08-07 634/week @ 2024-08-14 866/week @ 2024-08-21 848/week @ 2024-08-28 879/week @ 2024-09-04 665/week @ 2024-09-11 645/week @ 2024-09-18 644/week @ 2024-09-25 629/week @ 2024-10-02

2,619 downloads per month
Used in 4 crates (2 directly)

MPL-2.0 license

48KB
1K SLoC

VecMap<K, V>: the Map API, but with linear search

A std::vec::Vec based Map, motivated by the fact that, for some key types, iterating over a vector can be faster than other methods for small maps.

Most of the operations on this map implementation work in O(n), including some of the ones that are O(1) in HashMap. However, optimizers can work magic with contiguous arrays like Vec, and so for small sets (up to 256 elements for integer keys, for example), iterating through a vector actually yields better performance than the less branch- and cache-predictable hash maps.

Features

VecMap provides similar guarantees as HashMap, but does have some light differences: for one, the keys need neither to be hashable nor sortable, just equality is enough.

Like HashMap, VecMap doesn't guarantee pointer stability: growing capacity may relocate the vector's content, and item removal WILL relocate the last element of the vector.

When to use it

You may want to use a typedef to allow yourself to experiment and validate that it's good for your use-case, but as a rule of thumb: if you don't plan on storing more than a hundred elements in your map, but still want to express in your code that it IS a map, you should probably go with a VecMap.

How does it compare with linear_map

While it is very similar (after all, we share the same API), there is one key difference: linear_map's internal structure is a Vec<(K, V)>, whereas vector_map uses struct {keys: Vec<K>, values: Vec<V>}.

Considering that the most common operation for both these implementations is the linear search for a key, VecMap has the advantage of packing its keys tighter, requiring fewer cache requests for the same number of keys tested.

This makes VecMap slightly faster than LinearMap for some operations, especially when V is much bigger than K. However, you should still test both for your own application to see which is more suited to your application.

You use contracts, do I pay for them?

Not unless you specifically enable them, using this crate's enable_contracts feature. Since most of the contracts need to check if the map contains a key, they would otherwise each run their own key search, which is not a very efficient thing to do.

Dependencies

~3MB
~58K SLoC