A pure-Rust hash table which preserves (in a limited sense) insertion order.
This crate implements compact map and set data-structures, where the iteration order of the keys is independent from their hash or value. It preserves insertion order (except after removals), and it allows lookup of entries by either hash table key or numerical index.
Note: this crate was originally released under the name ordermap
,
but it was renamed to indexmap
to better reflect its features.
This was inspired by Python 3.6's new dict implementation (which remembers the insertion order and is fast to iterate, and is compact in memory).
Some of those features were translated to Rust, and some were not. The result was indexmap, a hash table that has following properties:
- Order is independent of hash function and hash values of keys.
- Fast to iterate.
- Indexed in compact space.
- Preserves insertion order as long as you don't call
.remove()
. - Uses hashbrown for the inner table, just like Rust's libstd
HashMap
does.
IndexMap
derives a couple of performance facts directly from how it is constructed,
which is roughly:
A raw hash table of key-value indices, and a vector of key-value pairs.
- Iteration is very fast since it is on the dense key-values.
- Removal is fast since it moves memory areas only in the table, and uses a single swap in the vector.
- Lookup is fast-ish because the initial 7-bit hash lookup uses SIMD, and indices are densely stored. Lookup also is slow-ish since the actual key-value pairs are stored separately. (Visible when cpu caches size is limiting.)
- In practice,
IndexMap
has been tested out as the hashmap in rustc in PR45282 and the performance was roughly on par across the whole workload. - If you want the properties of
IndexMap
, or its strongest performance points fits your workload, it might be the best hash table implementation.
- 1.6.2
- 1.6.1
- The new
serde_seq
module implementsIndexMap
serialization as a sequence to ensure order is preserved, by @cuviper in PR 158. - New methods on maps and sets work like the
Vec
/slice methods by the same name:truncate
,split_off
,first
,first_mut
,last
,last_mut
, andswap_indices
, by @cuviper in PR 160.
- The new
- 1.6.0
- MSRV: Rust 1.36 or later is now required.
- The
hashbrown
dependency has been updated to version 0.9.
- 1.5.2
- The new "std" feature will force the use of
std
for users that explicitly want the defaultS = RandomState
, bypassing the autodetection added in 1.3.0, by @cuviper in PR 145.
- The new "std" feature will force the use of
- 1.5.1
- 1.5.0
- MSRV: Rust 1.32 or later is now required.
- The inner hash table is now based on
hashbrown
by @cuviper in PR 131. This also completes the methodreserve
and addsshrink_to_fit
. - Add new methods
get_key_value
,remove_entry
,swap_remove_entry
, andshift_remove_entry
, by @cuviper in PR 136 Clone::clone_from
reuses allocations by @cuviper in PR 125- Add new method
reverse
by @linclelinkpart5 in PR 128
- 1.4.0
- Add new method
get_index_of
by @Thermatrix in PR 115 and 120 - Fix build script rebuild-if-changed configuration to use "build.rs"; fixes issue 123. Fix by @cuviper.
- Dev-dependencies (rand and quickcheck) have been updated. The crate's tests now run using Rust 1.32 or later (MSRV for building the crate has not changed). by @kjeremy and @bluss
- Add new method
- 1.3.2
- Maintenance update to regenerate the published Cargo.toml.
- 1.3.1
- Maintenance update for formatting and
autocfg
1.0.
- Maintenance update for formatting and
- 1.3.0
- The deprecation messages in the previous version have been removed. (The methods have not otherwise changed.) Docs for removal methods have been improved.
- From Rust 1.36, this crate supports being built without std, requiring
alloc
instead. This is enabled automatically when it is detected thatstd
is not available. There is no crate feature to enable/disable to trigger this. The new build-depautocfg
enables this.
- 1.2.0
- Plain
.remove()
now has a deprecation message, it informs the user about picking one of the removal functionsswap_remove
andshift_remove
which have different performance and order semantics. Plain.remove()
will not be removed, the warning message and method will remain until further. - Add new method
shift_remove
for order preserving removal on the map, andshift_take
for the corresponding operation on the set. - Add methods
swap_remove
,swap_remove_entry
toEntry
. - Fix indexset/indexmap to support full paths, like
indexmap::indexmap!()
- Internal improvements: fix warnings, deprecations and style lints
- Plain
- 1.1.0
- Added optional feature "rayon" that adds parallel iterator support to IndexMap and IndexSet using Rayon. This includes all the regular iterators in parallel versions, and parallel sort.
- Implemented
Clone
formap::{Iter, Keys, Values}
andset::{Difference, Intersection, Iter, SymmetricDifference, Union}
- Implemented
Debug
formap::{Entry, IntoIter, Iter, Keys, Values}
andset::{Difference, Intersection, IntoIter, Iter, SymmetricDifference, Union}
- Serde trait
IntoDeserializer
are implemented forIndexMap
andIndexSet
. - Minimum Rust version requirement increased to Rust 1.30 for development builds.
- 1.0.2
- The new methods
IndexMap::insert_full
andIndexSet::insert_full
are both likeinsert
with the index included in the return value. - The new method
Entry::and_modify
can be used to modify occupied entries, matching the new methods ofstd
maps in Rust 1.26. - The new method
Entry::or_default
inserts a default value in unoccupied entries, matching the new methods ofstd
maps in Rust 1.28.
- The new methods
- 1.0.1
- Document Rust version policy for the crate (see rustdoc)
- 1.0.0
- This is the 1.0 release for
indexmap
! (the crate and datastructure formerly known as “ordermap”) OccupiedEntry::insert
changed its signature, to use&mut self
for the method receiver, matching the equivalent method for a standardHashMap
. Thanks to @dtolnay for finding this bug.- The deprecated old names from ordermap were removed:
OrderMap
,OrderSet
,ordermap!{}
,orderset!{}
. Use the newIndexMap
etc names instead.
- This is the 1.0 release for
- 0.4.1
- Renamed crate to
indexmap
; theordermap
crate is now deprecated and the typesOrderMap/Set
now have a deprecation notice.
- Renamed crate to
- 0.4.0
- This is the last release series for this
ordermap
under that name, because the crate is going to be renamed toindexmap
(with typesIndexMap
,IndexSet
) and no change in functionality! - The map and its associated structs moved into the
map
submodule of the crate, so that the map and set are symmetric- The iterators,
Entry
and other structs are now underordermap::map::
- The iterators,
- Internally refactored
OrderMap<K, V, S>
so that all the main algorithms (insertion, lookup, removal etc) that don't use theS
parameter (the hasher) are compiled without depending onS
, which reduces generics bloat. Entry<K, V>
no longer has a type parameterS
, which is just like the standardHashMap
's entry.- Minimum Rust version requirement increased to Rust 1.18
- This is the last release series for this
- 0.3.5
- Documentation improvements
- 0.3.4
- The
.retain()
methods forOrderMap
andOrderSet
now traverse the elements in order, and the retained elements keep their order - Added new methods
.sort_by()
,.sort_keys()
toOrderMap
and.sort_by()
,.sort()
toOrderSet
. These methods allow you to sort the maps in place efficiently.
- The
- 0.3.3
- Document insertion behaviour better by @lucab
- Updated dependences (no feature changes) by @ignatenkobrain
- 0.3.2
- Add
OrderSet
by @cuviper! OrderMap::drain
is now (too) a double ended iterator.
- Add
- 0.3.1
- In all ordermap iterators, forward the
collect
method to the underlying iterator as well. - Add crates.io categories.
- In all ordermap iterators, forward the
- 0.3.0
- The methods
get_pair
,get_pair_index
were both replaced byget_full
(and the same for the mutable case). - Method
swap_remove_pair
replaced byswap_remove_full
. - Add trait
MutableKeys
for opt-in mutable key access. Mutable key access is only possible through the methods of this extension trait. - Add new trait
Equivalent
for key equivalence. This extends theBorrow
trait mechanism forOrderMap::get
in a backwards compatible way, just some minor type inference related issues may become apparent. See #10 for more information. - Implement
Extend<(&K, &V)>
by @xfix.
- The methods
- 0.2.13
- Fix deserialization to support custom hashers by @Techcable.
- Add methods
.index()
on the entry types by @garro95.
- 0.2.12
- Add methods
.with_hasher()
,.hasher()
.
- Add methods
- 0.2.11
- Support
ExactSizeIterator
for the iterators. By @Binero. - Use
Box<[Pos]>
internally, saving a word in theOrderMap
struct. - Serde support, with crate feature
"serde-1"
. By @xfix.
- Support
- 0.2.10
- Add iterator
.drain(..)
by @stevej.
- Add iterator
- 0.2.9
- Add method
.is_empty()
by @overvenus. - Implement
PartialEq, Eq
by @overvenus. - Add method
.sorted_by()
.
- Add method
- 0.2.8
- Add iterators
.values()
and.values_mut()
. - Fix compatibility with 32-bit platforms.
- Add iterators
- 0.2.7
- Add
.retain()
.
- Add
- 0.2.6
- Add
OccupiedEntry::remove_entry
and other minor entry methods, so that it now has all the features ofHashMap
's entries.
- Add
- 0.2.5
- Improved
.pop()
slightly.
- Improved
- 0.2.4
- Improved performance of
.insert()
(#3) by @pczarn.
- Improved performance of
- 0.2.3
- Generalize
Entry
for now, so that it works on hashmaps with non-default hasher. However, there's a lingering compat issue since libstdHashMap
does not parameterize its entries by the hasher (S
typarm). - Special case some iterator methods like
.nth()
.
- Generalize
- 0.2.2
- Disable the verbose
Debug
impl by default.
- Disable the verbose
- 0.2.1
- Fix doc links and clarify docs.
- 0.2.0
- Add more
HashMap
methods & compat with its API. - Experimental support for
.entry()
(the simplest parts of the API). - Add
.reserve()
(placeholder impl). - Add
.remove()
as synonym for.swap_remove()
. - Changed
.insert()
to swap value if the entry already exists, and returnOption
. - Experimental support as an indexed hash map! Added methods
.get_index()
,.get_index_mut()
,.swap_remove_index()
,.get_pair_index()
,.get_pair_index_mut()
.
- Add more
- 0.1.2
- Implement the 32/32 split idea for
Pos
which improves cache utilization and lookup performance.
- Implement the 32/32 split idea for
- 0.1.1
- Initial release.