15
15
#[ forbid( deprecated_mode) ] ;
16
16
17
17
use core:: container:: { Container , Mutable , Map , Set } ;
18
- use core:: dvec:: DVec ;
19
- use core:: ops;
20
18
use core:: option:: { Some , None } ;
21
- use core:: option;
22
19
use core:: prelude:: * ;
23
20
24
- // FIXME (#2347): Should not be @; there's a bug somewhere in rustc that
25
- // requires this to be.
26
- struct SmallIntMap_ < T > {
27
- v : DVec < Option < T > > ,
28
- }
29
-
30
- pub enum SmallIntMap < T > {
31
- SmallIntMap_ ( @SmallIntMap_ < T > )
32
- }
33
-
34
- /// Create a smallintmap
35
- pub fn mk < T : Copy > ( ) -> SmallIntMap < T > {
36
- let v = DVec ( ) ;
37
- SmallIntMap_ ( @SmallIntMap_ { v : v } )
38
- }
39
-
40
- /**
41
- * Add a value to the map. If the map already contains a value for
42
- * the specified key then the original value is replaced.
43
- */
44
- #[ inline( always) ]
45
- pub fn insert < T : Copy > ( self : SmallIntMap < T > , key : uint , val : T ) {
46
- //io::println(fmt!("%?", key));
47
- self . v . grow_set_elt ( key, & None , Some ( val) ) ;
48
- }
49
-
50
- /**
51
- * Get the value for the specified key. If the key does not exist
52
- * in the map then returns none
53
- */
54
- pub pure fn find < T : Copy > ( self : SmallIntMap < T > , key : uint ) -> Option < T > {
55
- if key < self . v . len ( ) { return self . v . get_elt ( key) ; }
56
- return None :: < T > ;
57
- }
58
-
59
- /**
60
- * Get the value for the specified key
61
- *
62
- * # Failure
63
- *
64
- * If the key does not exist in the map
65
- */
66
- pub pure fn get < T : Copy > ( self : SmallIntMap < T > , key : uint ) -> T {
67
- match find ( self , key) {
68
- None => {
69
- error ! ( "smallintmap::get(): key not present" ) ;
70
- fail;
71
- }
72
- Some ( move v) => return v
73
- }
74
- }
75
-
76
- /// Returns true if the map contains a value for the specified key
77
- pub pure fn contains_key < T : Copy > ( self : SmallIntMap < T > , key : uint ) -> bool {
78
- return !find ( self , key) . is_none ( ) ;
21
+ pub struct SmallIntMap < T > {
22
+ priv v: ~[ Option < T > ] ,
79
23
}
80
24
81
25
impl < V > SmallIntMap < V > : Container {
82
26
/// Return the number of elements in the map
83
27
pure fn len ( & self ) -> uint {
84
- let mut sz = 0 u ;
28
+ let mut sz = 0 ;
85
29
for self . v. each |item| {
86
- match * item {
87
- Some ( _) => sz += 1 u,
88
- _ => ( )
30
+ if item. is_some ( ) {
31
+ sz += 1 ;
89
32
}
90
33
}
91
34
sz
@@ -96,118 +39,136 @@ impl<V> SmallIntMap<V>: Container {
96
39
}
97
40
98
41
impl < V > SmallIntMap < V > : Mutable {
99
- fn clear ( & mut self ) { self . v . set ( ~[ ] ) }
42
+ /// Clear the map, removing all key-value pairs.
43
+ fn clear ( & mut self ) { self . v . clear ( ) }
100
44
}
101
45
102
- /// Implements the map::map interface for smallintmap
103
- impl < V : Copy > SmallIntMap < V > {
104
- #[ inline( always) ]
105
- fn insert ( key : uint , value : V ) -> bool {
106
- let exists = contains_key ( self , key) ;
107
- insert ( self , key, value) ;
108
- return !exists;
46
+ impl < V > SmallIntMap < V > : Map < uint , V > {
47
+ /// Return true if the map contains a value for the specified key
48
+ pure fn contains_key ( & self , key : & uint ) -> bool {
49
+ self . find ( key) . is_some ( )
109
50
}
110
- fn remove ( key : uint ) -> bool {
111
- if key >= self . v . len ( ) {
112
- return false ;
51
+
52
+ /// Visit all key-value pairs
53
+ pure fn each ( & self , it : fn ( key : & uint , value : & V ) -> bool ) {
54
+ for uint:: range( 0 , self . v. len( ) ) |i| {
55
+ match self . v [ i] {
56
+ Some ( ref elt) => if !it ( & i, elt) { break } ,
57
+ None => ( )
58
+ }
113
59
}
114
- let old = self . v . get_elt ( key) ;
115
- self . v . set_elt ( key, None ) ;
116
- old. is_some ( )
117
60
}
118
- pure fn contains_key ( key : uint ) -> bool {
119
- contains_key ( self , key)
61
+
62
+ /// Visit all keys
63
+ pure fn each_key ( & self , blk : fn ( key : & uint ) -> bool ) {
64
+ self . each ( |k, _| blk ( k) )
120
65
}
121
- pure fn contains_key_ref ( key : & uint ) -> bool {
122
- contains_key ( self , * key)
66
+
67
+ /// Visit all values
68
+ pure fn each_value ( & self , blk : fn ( value : & V ) -> bool ) {
69
+ self . each ( |_, v| blk ( v) )
123
70
}
124
- pure fn get ( key : uint ) -> V { get ( self , key) }
125
- pure fn find ( key : uint ) -> Option < V > { find ( self , key) }
126
71
127
- fn update_with_key ( key : uint , val : V , ff : fn ( uint , V , V ) -> V ) -> bool {
128
- match self . find ( key) {
129
- None => return self . insert ( key, val) ,
130
- Some ( copy orig) => return self . insert ( key, ff ( key, orig, val) ) ,
72
+ /// Return the value corresponding to the key in the map
73
+ pure fn find ( & self , key : & uint ) -> Option < & self /V > {
74
+ if * key < self . v . len ( ) {
75
+ match self . v [ * key] {
76
+ Some ( ref value) => Some ( value) ,
77
+ None => None
78
+ }
79
+ } else {
80
+ None
131
81
}
132
82
}
133
83
134
- fn update ( key : uint , newval : V , ff : fn ( V , V ) -> V ) -> bool {
135
- return self . update_with_key ( key, newval, |_k, v, v1| ff ( v, v1) ) ;
84
+ /// Insert a key-value pair into the map. An existing value for a
85
+ /// key is replaced by the new value. Return true if the key did
86
+ /// not already exist in the map.
87
+ fn insert ( & mut self , key : uint , value : V ) -> bool {
88
+ let exists = self . contains_key ( & key) ;
89
+ let len = self . v . len ( ) ;
90
+ if len <= key {
91
+ vec:: grow_fn ( & mut self . v , key - len + 1 , |_| None ) ;
92
+ }
93
+ self . v [ key] = Some ( value) ;
94
+ !exists
136
95
}
137
96
138
- pure fn each ( it : fn ( key : uint , value : V ) -> bool ) {
139
- self . each_ref ( |k, v| it ( * k, * v) )
140
- }
141
- pure fn each_key ( it : fn ( key : uint ) -> bool ) {
142
- self . each_ref ( |k, _v| it ( * k) )
143
- }
144
- pure fn each_value ( it : fn ( value : V ) -> bool ) {
145
- self . each_ref ( |_k, v| it ( * v) )
146
- }
147
- pure fn each_ref ( it : fn ( key : & uint , value : & V ) -> bool ) {
148
- let mut idx = 0 u, l = self . v . len ( ) ;
149
- while idx < l {
150
- match self . v . get_elt ( idx) {
151
- Some ( ref elt) => if !it ( & idx, elt) { break } ,
152
- None => ( )
153
- }
154
- idx += 1 u;
97
+ /// Remove a key-value pair from the map. Return true if the key
98
+ /// was present in the map, otherwise false.
99
+ fn remove ( & mut self , key : & uint ) -> bool {
100
+ if * key >= self . v . len ( ) {
101
+ return false ;
155
102
}
103
+ let removed = self . v [ * key] . is_some ( ) ;
104
+ self . v [ * key] = None ;
105
+ removed
156
106
}
157
- pure fn each_key_ref ( blk : fn ( key : & uint ) -> bool ) {
158
- self . each_ref ( |k, _v| blk ( k) )
159
- }
160
- pure fn each_value_ref ( blk : fn ( value : & V ) -> bool ) {
161
- self . each_ref ( |_k, v| blk ( v) )
107
+ }
108
+
109
+ pub impl < V > SmallIntMap < V > {
110
+ /// Create an empty SmallIntMap
111
+ static pure fn new( ) -> SmallIntMap <V > { SmallIntMap { v: ~[ ] } }
112
+
113
+ pure fn get ( & self , key: & uint) -> & self /V {
114
+ self. find ( key) . expect ( "key not present" )
162
115
}
163
116
}
164
117
165
- impl < V : Copy > SmallIntMap < V > : ops:: Index < uint , V > {
166
- pure fn index ( & self , key : uint ) -> V {
167
- unsafe {
168
- get ( * self , key)
118
+ pub impl < V : Copy > SmallIntMap < V > {
119
+ // FIXME: #4733, remove after the next snapshot
120
+ #[ cfg( stage2) ]
121
+ fn update_with_key ( & mut self , key : uint , val : V ,
122
+ ff : fn ( uint , V , V ) -> V ) -> bool {
123
+ match self . find ( & key) {
124
+ None => self . insert ( key, val) ,
125
+ Some ( orig) => self . insert ( key, ff ( key, copy * orig, val) ) ,
169
126
}
170
127
}
128
+
129
+ // FIXME: #4733, remove after the next snapshot
130
+ #[ cfg( stage2) ]
131
+ fn update ( & mut self , key : uint , newval : V , ff : fn ( V , V ) -> V ) -> bool {
132
+ self . update_with_key ( key, newval, |_k, v, v1| ff ( v, v1) )
133
+ }
171
134
}
172
135
173
136
#[ cfg( test) ]
174
137
mod tests {
175
- use super :: { mk, SmallIntMap } ;
176
-
177
- use core:: option:: None ;
138
+ use super :: SmallIntMap ;
178
139
179
140
#[ test]
180
141
fn test_len ( ) {
181
- let mut map = mk ( ) ;
142
+ let mut map = SmallIntMap :: new ( ) ;
182
143
assert map. len ( ) == 0 ;
183
144
assert map. is_empty ( ) ;
184
- map. insert ( 5 , 20 ) ;
145
+ assert map. insert ( 5 , 20 ) ;
185
146
assert map. len ( ) == 1 ;
186
147
assert !map. is_empty ( ) ;
187
- map. insert ( 11 , 12 ) ;
148
+ assert map. insert ( 11 , 12 ) ;
188
149
assert map. len ( ) == 2 ;
189
150
assert !map. is_empty ( ) ;
190
- map. insert ( 14 , 22 ) ;
151
+ assert map. insert ( 14 , 22 ) ;
191
152
assert map. len ( ) == 3 ;
192
153
assert !map. is_empty ( ) ;
193
154
}
194
155
195
156
#[ test]
196
157
fn test_clear ( ) {
197
- let mut map = mk ( ) ;
198
- map. insert ( 5 , 20 ) ;
199
- map. insert ( 11 , 12 ) ;
200
- map. insert ( 14 , 22 ) ;
158
+ let mut map = SmallIntMap :: new ( ) ;
159
+ assert map. insert ( 5 , 20 ) ;
160
+ assert map. insert ( 11 , 12 ) ;
161
+ assert map. insert ( 14 , 22 ) ;
201
162
map. clear ( ) ;
202
163
assert map. is_empty ( ) ;
203
- assert map. find ( 5 ) . is_none ( ) ;
204
- assert map. find ( 11 ) . is_none ( ) ;
205
- assert map. find ( 14 ) . is_none ( ) ;
164
+ assert map. find ( & 5 ) . is_none ( ) ;
165
+ assert map. find ( & 11 ) . is_none ( ) ;
166
+ assert map. find ( & 14 ) . is_none ( ) ;
206
167
}
207
168
208
169
#[ test]
209
170
fn test_insert_with_key ( ) {
210
- let map: SmallIntMap < uint > = mk ( ) ;
171
+ let mut map = SmallIntMap :: new ( ) ;
211
172
212
173
// given a new key, initialize it with this new count, given
213
174
// given an existing key, add more to its count
@@ -227,11 +188,11 @@ mod tests {
227
188
map. update_with_key ( 3 , 2 , addMoreToCount) ;
228
189
229
190
// check the total counts
230
- assert map. find ( 3 ) . get ( ) == 10 ;
231
- assert map. find ( 5 ) . get ( ) == 3 ;
232
- assert map. find ( 9 ) . get ( ) == 1 ;
191
+ assert map. find ( & 3 ) . get ( ) == & 10 ;
192
+ assert map. find ( & 5 ) . get ( ) == & 3 ;
193
+ assert map. find ( & 9 ) . get ( ) == & 1 ;
233
194
234
195
// sadly, no sevens were counted
235
- assert None == map. find ( 7 ) ;
196
+ assert map. find ( & 7 ) . is_none ( ) ;
236
197
}
237
198
}
0 commit comments