@@ -4,31 +4,31 @@ are O(highest integer key).
4
4
" ] ;
5
5
import core:: option;
6
6
import core:: option:: { some, none} ;
7
+ import dvec:: { dvec, extensions} ;
7
8
8
9
// FIXME: Should not be @; there's a bug somewhere in rustc that requires this
9
10
// to be. (#2347)
10
- type smallintmap < T : copy > = @{ mut v : [ mut option < T > ] } ;
11
+ type smallintmap < T : copy > = @{ v : dvec < option < T > > } ;
11
12
12
13
#[ doc = "Create a smallintmap" ]
13
14
fn mk < T : copy > ( ) -> smallintmap < T > {
14
- let v: [ mut option < T > ] = [ mut] ;
15
- ret @{ mut v: v} ;
15
+ ret @{ v : dvec ( ) } ;
16
16
}
17
17
18
18
#[ doc = "
19
19
Add a value to the map. If the map already contains a value for
20
20
the specified key then the original value is replaced.
21
21
" ]
22
- fn insert < T : copy > ( m : smallintmap < T > , key : uint , val : T ) {
23
- vec :: grow_set :: < option < T > > ( m . v , key, none :: < T > , some :: < T > ( val) ) ;
22
+ fn insert < T : copy > ( self : smallintmap < T > , key : uint , val : T ) {
23
+ self . v . grow_set_elt ( key, none, some ( val) ) ;
24
24
}
25
25
26
26
#[ doc = "
27
27
Get the value for the specified key. If the key does not exist
28
28
in the map then returns none
29
29
" ]
30
- fn find < T : copy > ( m : smallintmap < T > , key : uint ) -> option < T > {
31
- if key < vec :: len :: < option < T > > ( m . v ) { ret m . v [ key] ; }
30
+ fn find < T : copy > ( self : smallintmap < T > , key : uint ) -> option < T > {
31
+ if key < self . v . len ( ) { ret self . v . get_elt ( key) ; }
32
32
ret none:: < T > ;
33
33
}
34
34
@@ -39,8 +39,8 @@ Get the value for the specified key
39
39
40
40
If the key does not exist in the map
41
41
" ]
42
- fn get < T : copy > ( m : smallintmap < T > , key : uint ) -> T {
43
- alt find ( m , key) {
42
+ fn get < T : copy > ( self : smallintmap < T > , key : uint ) -> T {
43
+ alt find ( self , key) {
44
44
none { #error( "smallintmap::get(): key not present" ) ; fail; }
45
45
some ( v) { ret v; }
46
46
}
@@ -49,25 +49,15 @@ fn get<T: copy>(m: smallintmap<T>, key: uint) -> T {
49
49
#[ doc = "
50
50
Returns true if the map contains a value for the specified key
51
51
" ]
52
- fn contains_key < T : copy > ( m : smallintmap < T > , key : uint ) -> bool {
53
- ret !option:: is_none ( find :: < T > ( m, key) ) ;
54
- }
55
-
56
- // FIXME: Are these really useful?
57
-
58
- fn truncate < T : copy > ( m : smallintmap < T > , len : uint ) {
59
- m. v = vec:: to_mut ( vec:: slice :: < option < T > > ( m. v , 0 u, len) ) ;
60
- }
61
-
62
- fn max_key < T : copy > ( m : smallintmap < T > ) -> uint {
63
- ret vec:: len :: < option < T > > ( m. v ) ;
52
+ fn contains_key < T : copy > ( self : smallintmap < T > , key : uint ) -> bool {
53
+ ret !option:: is_none ( find ( self , key) ) ;
64
54
}
65
55
66
56
#[ doc = "Implements the map::map interface for smallintmap" ]
67
57
impl < V : copy > of map:: map < uint , V > for smallintmap < V > {
68
58
fn size ( ) -> uint {
69
59
let mut sz = 0 u;
70
- for vec :: each ( self . v) { |item|
60
+ for self . v. each { |item|
71
61
alt item { some( _) { sz += 1 u; } _ { } }
72
62
}
73
63
sz
@@ -78,9 +68,9 @@ impl <V: copy> of map::map<uint, V> for smallintmap<V> {
78
68
ret !exists;
79
69
}
80
70
fn remove ( & & key: uint ) -> option < V > {
81
- if key >= vec :: len ( self . v ) { ret none; }
82
- let old = self . v [ key] ;
83
- self . v [ key] = none;
71
+ if key >= self . v . len ( ) { ret none; }
72
+ let old = self . v . get_elt ( key) ;
73
+ self . v . set_elt ( key, none) ;
84
74
old
85
75
}
86
76
fn contains_key ( & & key: uint ) -> bool {
@@ -92,9 +82,9 @@ impl <V: copy> of map::map<uint, V> for smallintmap<V> {
92
82
fn each ( it : fn ( & & uint , V ) -> bool ) {
93
83
let mut idx = 0 u, l = self . v . len ( ) ;
94
84
while idx < l {
95
- alt self . v [ idx] {
85
+ alt self . v . get_elt ( idx) {
96
86
some ( elt) {
97
- if !it ( idx, copy elt) { break ; }
87
+ if !it ( idx, elt) { break ; }
98
88
}
99
89
none { }
100
90
}
@@ -104,7 +94,7 @@ impl <V: copy> of map::map<uint, V> for smallintmap<V> {
104
94
fn each_key ( it : fn ( & & uint ) -> bool ) {
105
95
let mut idx = 0 u, l = self . v . len ( ) ;
106
96
while idx < l {
107
- if self . v [ idx] != none && !it ( idx) { ret; }
97
+ if self . v . get_elt ( idx) != none && !it ( idx) { ret; }
108
98
idx += 1 u;
109
99
}
110
100
}
0 commit comments