smallintmap.rs 2.9 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
"]
N
Niko Matsakis 已提交
22 23
fn insert<T: copy>(self: smallintmap<T>, key: uint, val: T) {
    self.v.grow_set_elt(key, none, some(val));
24 25
}

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

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

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

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

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

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

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