#data-structures

linked_hash_set

HashSet with insertion ordering

5 releases

0.1.4 Jul 21, 2020
0.1.3 Jun 7, 2018
0.1.2 Feb 24, 2018
0.1.1 Dec 4, 2017
0.1.0 Oct 25, 2017

#261 in Algorithms

Download history 32605/week @ 2021-10-07 32534/week @ 2021-10-14 27149/week @ 2021-10-21 25763/week @ 2021-10-28 28209/week @ 2021-11-04 25134/week @ 2021-11-11 27505/week @ 2021-11-18 25967/week @ 2021-11-25 27348/week @ 2021-12-02 26648/week @ 2021-12-09 23485/week @ 2021-12-16 12847/week @ 2021-12-23 18061/week @ 2021-12-30 24201/week @ 2022-01-06 25573/week @ 2022-01-13 25234/week @ 2022-01-20

95,521 downloads per month
Used in 140 crates (20 directly)

Apache-2.0

58KB
919 lines

linked_hash_set crates.io Documentation

This library provides an hashed set with predictable iteration order, based on the insertion order of elements. It is implemented as a linked_hash_map::LinkedHashMap where the value is (), in a similar way as HashSet is implemented from HashMap in stdlib.

Comparison with std HashSet

General usage is very similar to a traditional hashed set, but this structure also maintains insertion order.

Compared to HashSet, a LinkedHashSet uses an additional doubly-linked list running through its entries. As such methods front(), pop_front(), back(), pop_back() and refresh() are provided.

Comparison with IndexSet

Compared to indexmap::IndexSet, while both maintain insertion order a LinkedHashSet uses a linked list allowing performant removals that don't affect the order of the remaining elements. However, when this distinction is unimportant indexmap should be the faster option.

Example

let mut set = linked_hash_set::LinkedHashSet::new();
assert!(set.insert(234));
assert!(set.insert(123));
assert!(set.insert(345));
assert!(!set.insert(123)); // Also see `insert_if_absent` which won't change order

assert_eq!(set.into_iter().collect::<Vec<_>>(), vec![234, 345, 123]);

Minimum supported rust compiler

This crate is maintained with latest stable rust.

Dependencies

~110KB