1
- use std:: borrow:: Borrow ;
2
1
use std:: cell:: { Cell , UnsafeCell } ;
3
2
use std:: collections:: hash_map:: RandomState ;
4
3
use std:: hash:: { BuildHasher , Hash } ;
@@ -8,6 +7,8 @@ use std::ops::Index;
8
7
use indexmap:: IndexMap ;
9
8
use stable_deref_trait:: StableDeref ;
10
9
10
+ pub use indexmap:: Equivalent ;
11
+
11
12
/// Append-only version of `indexmap::IndexMap` where
12
13
/// insertion does not require mutable access
13
14
pub struct FrozenIndexMap < K , V , S = RandomState > {
@@ -41,10 +42,6 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
41
42
///
42
43
/// Existing values are never overwritten.
43
44
///
44
- /// The key may be any borrowed form of the map's key type, but
45
- /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
46
- /// the key type.
47
- ///
48
45
/// # Example
49
46
/// ```
50
47
/// use elsa::index_map::FrozenIndexMap;
@@ -72,10 +69,6 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
72
69
///
73
70
/// Existing values are never overwritten.
74
71
///
75
- /// The key may be any borrowed form of the map's key type, but
76
- /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
77
- /// the key type.
78
- ///
79
72
/// # Example
80
73
/// ```
81
74
/// use elsa::index_map::FrozenIndexMap;
@@ -97,10 +90,9 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
97
90
}
98
91
99
92
/// Returns a reference to the value corresponding to the key.
100
- ///
101
- /// The key may be any borrowed form of the map's key type, but
102
- /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
103
- /// the key type.
93
+ ///
94
+ /// # Arguments
95
+ /// * `k` may be any type that implements [`Equivalent<K>`].
104
96
///
105
97
/// # Examples
106
98
///
@@ -112,10 +104,9 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
112
104
/// assert_eq!(map.get(&1), Some(&"a"));
113
105
/// assert_eq!(map.get(&2), None);
114
106
/// ```
115
- pub fn get < Q : ? Sized > ( & self , k : & Q ) -> Option < & V :: Target >
107
+ pub fn get < Q > ( & self , k : & Q ) -> Option < & V :: Target >
116
108
where
117
- K : Borrow < Q > ,
118
- Q : Hash + Eq ,
109
+ Q : ?Sized + Hash + Equivalent < K > ,
119
110
{
120
111
assert ! ( !self . in_use. get( ) ) ;
121
112
self . in_use . set ( true ) ;
@@ -127,11 +118,36 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
127
118
ret
128
119
}
129
120
130
- /// Returns a reference to the key-value mapping corresponding to an index.
121
+ /// Returns the index corresponding to the key
122
+ ///
123
+ /// # Arguments
124
+ /// * `k` may be any type that implements [`Equivalent<K>`].
125
+ ///
126
+ /// # Examples
131
127
///
132
- /// The key may be any borrowed form of the map's key type, but
133
- /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
134
- /// the key type.
128
+ /// ```
129
+ /// use elsa::index_map::FrozenIndexMap;
130
+ ///
131
+ /// let map = FrozenIndexMap::new();
132
+ /// map.insert(1, Box::new("a"));
133
+ /// assert_eq!(map.get_index_of(&1), Some(0));
134
+ /// assert_eq!(map.get_index_of(&2), None);
135
+ /// ```
136
+ pub fn get_index_of < Q > ( & self , k : & Q ) -> Option < usize >
137
+ where
138
+ Q : ?Sized + Hash + Equivalent < K > ,
139
+ {
140
+ assert ! ( !self . in_use. get( ) ) ;
141
+ self . in_use . set ( true ) ;
142
+ let ret = unsafe {
143
+ let map = self . map . get ( ) ;
144
+ ( * map) . get_index_of ( k)
145
+ } ;
146
+ self . in_use . set ( false ) ;
147
+ ret
148
+ }
149
+
150
+ /// Returns a reference to the key-value mapping corresponding to an index.
135
151
///
136
152
/// # Examples
137
153
///
@@ -158,11 +174,40 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
158
174
ret
159
175
}
160
176
177
+ /// Returns a reference to the key, along with its index and a reference to its value
178
+ ///
179
+ /// # Arguments
180
+ /// * `k` may be any type that implements [`Equivalent<K>`].
181
+ ///
182
+ /// # Examples
183
+ ///
184
+ /// ```
185
+ /// use elsa::index_map::FrozenIndexMap;
186
+ ///
187
+ /// let map = FrozenIndexMap::new();
188
+ /// map.insert("1", Box::new("a"));
189
+ /// assert_eq!(map.get_full("1"), Some((0, "1", &"a")));
190
+ /// assert_eq!(map.get_full("2"), None);
191
+ /// ```
192
+ pub fn get_full < Q > ( & self , k : & Q ) -> Option < ( usize , & K :: Target , & V :: Target ) >
193
+ where
194
+ Q : ?Sized + Hash + Equivalent < K > ,
195
+ K : StableDeref ,
196
+ {
197
+ assert ! ( !self . in_use. get( ) ) ;
198
+ self . in_use . set ( true ) ;
199
+ let ret = unsafe {
200
+ let map = self . map . get ( ) ;
201
+ ( * map) . get_full ( k) . map ( |( i, k, v) | ( i, & * * k, & * * v) )
202
+ } ;
203
+ self . in_use . set ( false ) ;
204
+ ret
205
+ }
206
+
161
207
/// Applies a function to the owner of the value corresponding to the key (if any).
162
208
///
163
- /// The key may be any borrowed form of the map's key type, but
164
- /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for
165
- /// the key type.
209
+ /// # Arguments
210
+ /// * `k` may be any type that implements [`Equivalent<K>`].
166
211
///
167
212
/// # Examples
168
213
///
@@ -174,10 +219,9 @@ impl<K: Eq + Hash, V: StableDeref, S: BuildHasher> FrozenIndexMap<K, V, S> {
174
219
/// assert_eq!(map.map_get(&1, Clone::clone), Some(Box::new("a")));
175
220
/// assert_eq!(map.map_get(&2, Clone::clone), None);
176
221
/// ```
177
- pub fn map_get < Q : ? Sized , T , F > ( & self , k : & Q , f : F ) -> Option < T >
222
+ pub fn map_get < Q , T , F > ( & self , k : & Q , f : F ) -> Option < T >
178
223
where
179
- K : Borrow < Q > ,
180
- Q : Hash + Eq ,
224
+ Q : ?Sized + Hash + Equivalent < K > ,
181
225
F : FnOnce ( & V ) -> T ,
182
226
{
183
227
assert ! ( !self . in_use. get( ) ) ;
@@ -246,10 +290,10 @@ impl<K, V, S> From<IndexMap<K, V, S>> for FrozenIndexMap<K, V, S> {
246
290
}
247
291
}
248
292
249
- impl < Q : ? Sized , K : Eq + Hash , V : StableDeref , S : BuildHasher > Index < & Q > for FrozenIndexMap < K , V , S >
293
+ impl < Q , K , V , S > Index < & Q > for FrozenIndexMap < K , V , S >
250
294
where
251
- Q : Eq + Hash ,
252
- K : Eq + Hash + Borrow < Q > ,
295
+ Q : ? Sized + Hash + Equivalent < K > ,
296
+ K : Eq + Hash ,
253
297
V : StableDeref ,
254
298
S : BuildHasher ,
255
299
{
@@ -265,7 +309,7 @@ where
265
309
/// assert_eq!(map[&1], "a");
266
310
/// ```
267
311
fn index ( & self , idx : & Q ) -> & V :: Target {
268
- self . get ( & idx)
312
+ self . get ( idx)
269
313
. expect ( "attempted to index FrozenIndexMap with unknown key" )
270
314
}
271
315
}
0 commit comments