@@ -21,6 +21,33 @@ impl<T, V> ArenaMap<Idx<T>, V> {
21
21
Self { v : Vec :: with_capacity ( capacity) , _ty : PhantomData }
22
22
}
23
23
24
+ /// Reserves capacity for at least additional more elements to be inserted in the map.
25
+ pub fn reserve ( & mut self , additional : usize ) {
26
+ self . v . reserve ( additional) ;
27
+ }
28
+
29
+ /// Clears the map, removing all elements.
30
+ pub fn clear ( & mut self ) {
31
+ self . v . clear ( ) ;
32
+ }
33
+
34
+ /// Shrinks the capacity of the map as much as possible.
35
+ pub fn shrink_to_fit ( & mut self ) {
36
+ let min_len = self . v . iter ( ) . rposition ( |slot| slot. is_some ( ) ) . map_or ( 0 , |i| i + 1 ) ;
37
+ self . v . truncate ( min_len) ;
38
+ self . v . shrink_to_fit ( ) ;
39
+ }
40
+
41
+ /// Returns whether the map contains a value for the specified index.
42
+ pub fn contains_idx ( & self , idx : Idx < T > ) -> bool {
43
+ matches ! ( self . v. get( Self :: to_idx( idx) ) , Some ( Some ( _) ) )
44
+ }
45
+
46
+ /// Removes an index from the map, returning the value at the index if the index was previously in the map.
47
+ pub fn remove ( & mut self , idx : Idx < T > ) -> Option < V > {
48
+ self . v . get_mut ( Self :: to_idx ( idx) ) ?. take ( )
49
+ }
50
+
24
51
/// Inserts a value associated with a given arena index into the map.
25
52
pub fn insert ( & mut self , idx : Idx < T > , t : V ) {
26
53
let idx = Self :: to_idx ( idx) ;
@@ -94,6 +121,22 @@ impl<T, V> Default for ArenaMap<Idx<V>, T> {
94
121
}
95
122
}
96
123
124
+ impl < T , V > Extend < ( Idx < V > , T ) > for ArenaMap < Idx < V > , T > {
125
+ fn extend < I : IntoIterator < Item = ( Idx < V > , T ) > > ( & mut self , iter : I ) {
126
+ iter. into_iter ( ) . for_each ( move |( k, v) | {
127
+ self . insert ( k, v) ;
128
+ } ) ;
129
+ }
130
+ }
131
+
132
+ impl < T , V > FromIterator < ( Idx < V > , T ) > for ArenaMap < Idx < V > , T > {
133
+ fn from_iter < I : IntoIterator < Item = ( Idx < V > , T ) > > ( iter : I ) -> Self {
134
+ let mut this = Self :: new ( ) ;
135
+ this. extend ( iter) ;
136
+ this
137
+ }
138
+ }
139
+
97
140
/// A view into a single entry in a map, which may either be vacant or occupied.
98
141
///
99
142
/// This `enum` is constructed from the [`entry`] method on [`ArenaMap`].
0 commit comments