smallintmap.rs 3.0 KB
Newer Older
B
Brian Anderson 已提交
1
#[doc = "
B
Brian Anderson 已提交
2 3
A simple map based on a vector for small integer keys. Space requirements
are O(highest integer key).
B
Brian Anderson 已提交
4
"];
5 6
import core::option;
import core::option::{some, none};
N
Niko Matsakis 已提交
7
import dvec::{dvec, extensions};
8

9
// FIXME: Should not be @; there's a bug somewhere in rustc that requires this
10
// to be. (#2347)
N
Niko Matsakis 已提交
11
type smallintmap<T: copy> = @{v: dvec<option<T>>};
12

B
Brian Anderson 已提交
13
#[doc = "Create a smallintmap"]
14
fn mk<T: copy>() -> smallintmap<T> {
N
Niko Matsakis 已提交
15
    ret @{v: dvec()};
16 17
}

B
Brian Anderson 已提交
18
#[doc = "
B
Brian Anderson 已提交
19 20
Add a value to the map. If the map already contains a value for
the specified key then the original value is replaced.
B
Brian Anderson 已提交
21
"]
E
Eric Holk 已提交
22
#[inline(always)]
N
Niko Matsakis 已提交
23 24
fn insert<T: copy>(self: smallintmap<T>, key: uint, val: T) {
    self.v.grow_set_elt(key, none, some(val));
25 26
}

B
Brian Anderson 已提交
27
#[doc = "
B
Brian Anderson 已提交
28
Get the value for the specified key. If the key does not exist
29
in the map then returns none
B
Brian Anderson 已提交
30
"]
N
Niko Matsakis 已提交
31 32
fn find<T: copy>(self: smallintmap<T>, key: uint) -> option<T> {
    if key < self.v.len() { ret self.v.get_elt(key); }
33
    ret none::<T>;
34 35
}

B
Brian Anderson 已提交
36
#[doc = "
B
Brian Anderson 已提交
37 38
Get the value for the specified key

B
Brian Anderson 已提交
39
# Failure
B
Brian Anderson 已提交
40 41

If the key does not exist in the map
B
Brian Anderson 已提交
42
"]
N
Niko Matsakis 已提交
43 44
fn get<T: copy>(self: smallintmap<T>, key: uint) -> T {
    alt find(self, key) {
45
      none { #error("smallintmap::get(): key not present"); fail; }
46
      some(v) { ret v; }
47 48 49
    }
}

B
Brian Anderson 已提交
50
#[doc = "
B
Brian Anderson 已提交
51
Returns true if the map contains a value for the specified key
B
Brian Anderson 已提交
52
"]
N
Niko Matsakis 已提交
53 54
fn contains_key<T: copy>(self: smallintmap<T>, key: uint) -> bool {
    ret !option::is_none(find(self, key));
M
Marijn Haverbeke 已提交
55
}
56

B
Brian Anderson 已提交
57
#[doc = "Implements the map::map interface for smallintmap"]
58 59
impl <V: copy> of map::map<uint, V> for smallintmap<V> {
    fn size() -> uint {
60
        let mut sz = 0u;
N
Niko Matsakis 已提交
61
        for self.v.each {|item|
62 63 64 65
            alt item { some(_) { sz += 1u; } _ {} }
        }
        sz
    }
E
Eric Holk 已提交
66
    #[inline(always)]
67
    fn insert(+key: uint, +value: V) -> bool {
68 69 70 71
        let exists = contains_key(self, key);
        insert(self, key, value);
        ret !exists;
    }
T
Tim Chevalier 已提交
72
    fn remove(&&key: uint) -> option<V> {
N
Niko Matsakis 已提交
73 74 75
        if key >= self.v.len() { ret none; }
        let old = self.v.get_elt(key);
        self.v.set_elt(key, none);
76 77 78 79 80 81
        old
    }
    fn contains_key(&&key: uint) -> bool {
        contains_key(self, key)
    }
    fn get(&&key: uint) -> V { get(self, key) }
T
Tim Chevalier 已提交
82
    fn find(&&key: uint) -> option<V> { find(self, key) }
83
    fn rehash() { fail }
84
    fn each(it: fn(&&uint, V) -> bool) {
85 86
        let mut idx = 0u, l = self.v.len();
        while idx < l {
N
Niko Matsakis 已提交
87
            alt self.v.get_elt(idx) {
88
              some(elt) {
N
Niko Matsakis 已提交
89
                if !it(idx, elt) { break; }
90
              }
91
              none { }
92 93 94 95
            }
            idx += 1u;
        }
    }
96
    fn each_key(it: fn(&&uint) -> bool) {
97 98
        let mut idx = 0u, l = self.v.len();
        while idx < l {
N
Niko Matsakis 已提交
99
            if self.v.get_elt(idx) != none && !it(idx) { ret; }
100 101 102
            idx += 1u;
        }
    }
103 104
    fn each_value(it: fn(V) -> bool) {
        self.each {|_i, v| it(v)}
105 106 107
    }
}

B
Brian Anderson 已提交
108
#[doc = "Cast the given smallintmap to a map::map"]
109
fn as_map<V: copy>(s: smallintmap<V>) -> map::map<uint, V> {
110 111
    s as map::map::<uint, V>
}