identified_vec
A collection of unique identifiable elements which retains insertion order, inspired by Pointfree's Swift Identified Collections.
Similar to the standard Vec, the IdentifiedVec maintain their elements in a particular user-specified order. However, unlike Vec, the IdentifiedVec introduce the ability to uniquely identify elements, using a hash table to ensure that no two elements have the same identity, and to efficiently look up elements corresponding to specific identifiers.
IdentifiedVec is a useful alternative to Vec when you need to be able to efficiently access unique elements by a stable identifier. It is also a useful alternative to BTreeSet, where the Ord trait requirement may be too strict, an a useful alternative to HashSet where Hash trait requirement may be too strict.
You can create an identified vec with any element type that implements the Identifiable trait.
Example
extern crate identified_vec;
use ;
use RefCell;
Identifiable
from_iter
let mut users = from_iter;
assert_eq!;
assert_eq!;
append & elements()
users.append;
assert_eq!;
// Element with same ID is not appended:
users.append;
assert_eq!;
update_or_insert
// Element with same ID replaces existing if an `update_*` method is used:
// e.g. `update_or_insert`:
users.update_or_insert;
assert_eq!;
update_or_append
// or `update_or_append`
users.update_or_append;
assert_eq!;
Or you can provide a closure that describes an element's identity:
let numbers = new_identifying_element;
Motivation
None of the std collections BTreeSet and HashSet retain insertion order, Vec retains insertion order, however, it allows for duplicates. So if you want a collection of unique elements (Set-like) that does retain insertion order, IdentifiedVec suits your needs. Even better, the elements does not need to be to impl Hash nor Ord.
Flags
This crate has the following Cargo features:
serde: Enables serde serialization support onIdentifiedVecOftype (whichElementimplIdentifiabletrait).id_prim: Get impl of traitIdentifiablefor primitives:i8,..,i128,u8, ...,u128andbool(not so useful, allows for only two elements inIdentifiedVecOf, but who am I to discriminate.)
Implementation Details
An identified vec consists of a Vec of IDs keeping insertion order and a HashMap of id-element pairs, for constant time lookup of element given an ID.
License
Licensed under MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)