2017-11-21 12:16:59 -08:00
2017-11-21 12:16:59 -08:00
2017-04-20 13:21:57 -07:00
2017-11-19 18:33:05 +01:00
2017-08-01 20:37:49 +02:00
2017-08-01 20:37:49 +02:00
2017-11-19 18:33:05 +01:00

Experimental hash table implementation in just Rust (stable, no unsafe code).

Please read the `API documentation here`__

__ https://docs.rs/ordermap/

|build_status|_ |crates|_

.. |crates| image:: https://img.shields.io/crates/v/ordermap.svg
.. _crates: https://crates.io/crates/ordermap

.. |build_status| image:: https://travis-ci.org/bluss/ordermap.svg
.. _build_status: https://travis-ci.org/bluss/ordermap


Background
==========

This was inpired 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 ordermap, a hash table that is

- 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``.
- Using robin hood hashing just like Rust's libstd HashMap.

  - It's the usual backwards shift deletion, but only on the index vector, so
    it's cheaper because it's moving less memory around.

Does not implement
------------------


- ``.reserve`` exists but does not have a full implementation

Performance
-----------

(Figures from late 2016)

- Iteration is very fast
- Lookup is faster than libstd HashMap for "small" tables (below something like
  100 000 key-value pairs), but suffers under load more due
  to the index vec to entries vec indirection still.
- Growing the map is faster than libstd HashMap, doesn't need to move keys and values
  at all, only the index vec

Interesting Features
--------------------

- Insertion order is preserved (swap_remove perturbs the order, like the method name says)
- Implements .pop() -> Option<(K, V)> in O(1) time
- OrderMap::new() is empty and uses no allocation until you insert something
- Lookup key-value pairs by index and vice versa
- No ``unsafe``.
- Supports ``IndexMut``.


Where to go from here?
----------------------

- Ideas and PRs for how to implement insertion-order preserving remove (for example tombstones)
  are welcome. The plan is to split the crate into two hash table implementations
  a) the current compact index space version and b) the full insertion order version


Ideas that we already did
-------------------------

- It can be an *indexable* ordered map in the current fashion.
  (This was implemented in 0.2.0, for potential use as a graph datastructure).

- Idea for more cache efficient lookup (This was implemented in 0.1.2)

  Current ``indices: Vec<Pos>``. ``Pos`` is interpreted as ``(u32, u32)`` more
  or less when raw_capacity() fits in 32 bits.  Pos then stores both the lower
  half of the hash and the entry index.
  This means that the hash values in ``Bucket`` don't need to be accessed
  while scanning for an entry.


Recent Changes
==============

- 0.3.1

  - In all ordermap iterators, forward the ``collect`` method to the underlying
    iterator as well.
  - Add crates.io categories

- 0.3.0

  - The methods ``get_pair, get_pair_index`` were both replaced by
    ``get_full`` (and the same for the mutable case).
  - Method ``swap_remove_pair`` replaced by ``swap_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 the
    ``Borrow`` trait mechanism for ``OrderMap::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

__ https://github.com/bluss/ordermap/pull/10

- 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()``

- 0.2.11

  - Support ``ExactSizeIterator`` for the iterators. By @Binero
  - Use ``Box<[Pos]>`` internally, saving a word in the OrderMap struct.
  - Serde support, with crate feature ``"serde-1"``. By @xfix

- 0.2.10

  - Add iterator ``.drain(..)`` by @stevej

- 0.2.9

  - Add method ``.is_empty()`` by @overvenus
  - Implement ``PartialEq, Eq`` by @overvenus
  - Add method ``.sorted_by()``

- 0.2.8

  - Add iterators ``values()`` and ``.values_mut()``
  - Fix compatibility with 32-bit platforms

- 0.2.7

  - Add ``.retain()``

- 0.2.6

  - Add ``OccupiedEntry::remove_entry`` and other minor entry methods,
    so that it now has all the features of ``HashMap``'s entries.

- 0.2.5

  - Improved .pop() slightly

- 0.2.4

  - Improved performance of .insert() (#3) by pczarn

- 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 libstd HashMap
    does not parameterize its entries by the hasher (``S`` typarm).
  - Special case some iterator methods like ``.nth()``

- 0.2.2

  - Disable the verbose Debug impl by default.

- 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
    return Option.
  - Experimental support as an *indexed* hash map! Added methods
    ``.get_index(), .get_index_mut(), .swap_remove_index()``,
    ``.get_pair_index(), .get_pair_index_mut()``.

- 0.1.2

  - Implement the 32/32 split idea for ``Pos`` which improves cache utilization
    and lookup performance

- 0.1.1

  - Initial release
S
Description
保留插入顺序的纯Rust哈希表实现
Readme 1.4 MiB
Languages
Rust 100%