1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::borrow::Borrow;
use std::collections::{hash_map, HashMap};
use std::hash::Hash;
use std::ops::Index;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UnorderedHashMap<Key: Hash + Eq, Value>(HashMap<Key, Value>);
impl<Key: Hash + Eq, Value> UnorderedHashMap<Key, Value> {
pub fn get<Q: ?Sized>(&self, key: &Q) -> Option<&Value>
where
Key: Borrow<Q>,
Q: Hash + Eq,
{
self.0.get(key)
}
pub fn insert(&mut self, key: Key, value: Value) -> Option<Value> {
self.0.insert(key, value)
}
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<Value>
where
Key: Borrow<Q>,
Q: Hash + Eq,
{
self.0.remove(key)
}
pub fn entry(&mut self, key: Key) -> hash_map::Entry<'_, Key, Value> {
self.0.entry(key)
}
pub fn contains_key<Q>(&self, key: &Q) -> bool
where
Q: ?Sized,
Key: Borrow<Q>,
Q: Hash + Eq,
{
self.0.contains_key(key)
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl<Key: Hash + Eq, IndexType: Into<Key>, Value> Index<IndexType>
for UnorderedHashMap<Key, Value>
{
type Output = Value;
fn index(&self, index: IndexType) -> &Self::Output {
&self.0[&index.into()]
}
}
impl<Key: Hash + Eq, Value> Default for UnorderedHashMap<Key, Value> {
fn default() -> Self {
Self(Default::default())
}
}
impl<Key: Hash + Eq, Value> FromIterator<(Key, Value)> for UnorderedHashMap<Key, Value> {
fn from_iter<T: IntoIterator<Item = (Key, Value)>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}