1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//! A container that gives each item a unique id. Adding and removing by index is O(1).

extern crate bit_set;
extern crate bit_vec;

#[cfg(test)] mod tests;

use std::iter::FromIterator;
use std::ops::{Index, IndexMut};
use std::{fmt, ptr};

use bit_set::BitSet;
use bit_vec::BitVec;

#[derive(Clone, Copy, Debug, Hash, Eq, Ord, PartialEq, PartialOrd)]
/// An identifier for an `IdMap` value.
pub struct Id(usize);

/// A container that gives each item a unique id.
pub struct IdMap<T> {
    // The set of valid indices for values.
    ids: BitSet,
    // The buffer of values. Indices not in ids are invalid.
    values: Vec<T>,
    // The index to start counting from when inserting.
    start: usize,
}

impl<T> IdMap<T> {
    /// Create an empty map.
    pub fn new() -> Self {
        IdMap {
            ids: BitSet::new(),
            values: Vec::new(),
            start: 0,
        }
    }

    /// Insert an value into the map and return its id.    
    pub fn insert(&mut self, val: T) -> Id {
        let end = self.values.len();
        for id in self.start..end {
            if self.ids.insert(id) {
                // The value in the vec is uninitialized.
                unsafe {
                    ptr::write(self.values.get_unchecked_mut(id), val);
                }
                self.start = id;
                return Id(id);
            }
        }
        self.ids.insert(end);
        self.values.push(val);
        self.start = end;
        Id(end)
    }

    /// Remove the value at the given index, returning it if it exists. 
    pub fn remove(&mut self, Id(id): Id) -> Option<T> {
        if self.ids.remove(id) {
            if id < self.start {
                self.start = id;
            }
            Some(if id + 1 == self.values.len() {
                // Note len() cannot be 0 here so unwrap cannot fail.
                self.values.pop().unwrap()
            } else {
                unsafe { ptr::read(self.values.get_unchecked(id)) }
            })
        } else {
            None
        }
    }

    /// Get an iterator over ids.
    pub fn ids(&self) -> Ids {
        Ids { ids: self.ids.iter(), start: self.start }
    }

    /// Get an iterator over values.
    pub fn values(&self) -> Values<T> {
        Values {
            ids: self.ids.iter(),
            values: &self.values,
            start: self.start,
        }
    }

    /// Get an iterator over id, value pairs.
    pub fn iter(&self) -> Iter<T> {
        Iter {
            ids: self.ids.iter(),
            values: &self.values,
            start: self.start,
        }
    }

    /// Check whether a given id has an entry in the map.
    pub fn contains(&self, Id(id): Id) -> bool {
        self.ids.contains(id)
    }

    #[cfg(test)]
    fn assert_invariant(&self) {
        // All elements less than start should be filled.
        for id in 0..self.start {
            assert!(self.ids.contains(id));
        }
        // All elements should be less than end.
        let end = self.values.len();
        for id in &self.ids {
            assert!(id < end)
        }
        // End should be minimal.
        assert!(end == 0 || self.ids.contains(end - 1));
    }
}

impl<T> Drop for IdMap<T> {
    fn drop(&mut self) {
        // Our vec contains uninitialized values so we must manually drop it.
        unsafe {
            for id in &self.ids {
                ptr::drop_in_place(self.values.get_unchecked_mut(id))
            }
            self.values.set_len(0);
        }
    }
}

impl<T: Clone> Clone for IdMap<T> {
    fn clone(&self) -> Self {
        self.values().cloned().collect()
    }
}

impl<T: fmt::Debug> fmt::Debug for IdMap<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{{")?;
        let mut iter = self.iter();
        if let Some((Id(id), val)) = iter.next() {
            write!(f, "{:?}: {:?}", id, val)?;
            for (Id(id), val) in iter {
                write!(f, ", {:?}: {:?}", id, val)?;
            }
        }
        write!(f, "}}")
    }
}

impl<T: Eq> Eq for IdMap<T> {}

impl<T: PartialEq> PartialEq for IdMap<T> {
    fn eq(&self, other: &Self) -> bool {
        let (mut lhs, mut rhs) = (self.values(), other.values());
        loop {
            match (lhs.next(), rhs.next()) {
                (Some(left), Some(right)) if left == right => continue,
                (None, None) => return true,
                _ => return false,
            }
        }
    }
}

impl<T> Extend<T> for IdMap<T> {
    fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
        for val in iter {
            self.insert(val);
        }
    }
}

impl<T> FromIterator<T> for IdMap<T> {
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let values = Vec::from_iter(iter);
        let start = values.len();
        let ids = BitSet::from_bit_vec(BitVec::from_elem(start, true));
        IdMap { values, start, ids }
    }
}

impl<'a, T> IntoIterator for &'a IdMap<T> {
    type Item = (Id, &'a T);
    type IntoIter = Iter<'a, T>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

impl<T> Index<Id> for IdMap<T> {
    type Output = T;

    fn index(&self, Id(id): Id) -> &Self::Output {
        assert!(self.ids.contains(id), "id {} out of bounds", id);
        unsafe { self.values.get_unchecked(id) }
    }
}

impl<T> IndexMut<Id> for IdMap<T> {
    fn index_mut(&mut self, Id(id): Id) -> &mut Self::Output {
        assert!(self.ids.contains(id), "id {} out of bounds", id);
        unsafe { self.values.get_unchecked_mut(id) }
    }
}

#[derive(Clone)]
/// Iterator over ids.
pub struct Ids<'a> {
    ids: bit_set::Iter<'a, u32>,
    start: usize,
}

impl<'a> Iterator for Ids<'a> {
    type Item = Id;

    fn next(&mut self) -> Option<Self::Item> {
        self.ids.next().map(Id)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.start, self.ids.size_hint().1)
    }
}

/// Iterator over values.
pub struct Values<'a, T: 'a> {
    ids: bit_set::Iter<'a, u32>,
    values: &'a [T],
    start: usize,
}

impl<'a, T: 'a> Iterator for Values<'a, T> {
    type Item = &'a T;

    fn next(&mut self) -> Option<Self::Item> {
        self.ids.next().map(|id| unsafe { self.values.get_unchecked(id) })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.start, self.ids.size_hint().1)
    }
}

impl<'a, T: 'a> Clone for Values<'a, T> {
    fn clone(&self) -> Self {
        Values {
            ids: self.ids.clone(),
            values: self.values,
            start: self.start,
        }
    }
}

/// Iterator over id, value pairs.
pub struct Iter<'a, T: 'a> {
    ids: bit_set::Iter<'a, u32>,
    values: &'a [T],
    start: usize,
}

impl<'a, T: 'a> Iterator for Iter<'a, T> {
    type Item = (Id, &'a T);

    fn next(&mut self) -> Option<Self::Item> {
        self.ids.next().map(|id| (Id(id), unsafe { self.values.get_unchecked(id) }))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.start, self.ids.size_hint().1)
    }
}

impl<'a, T: 'a> Clone for Iter<'a, T> {
    fn clone(&self) -> Self {
        Iter {
            ids: self.ids.clone(),
            values: self.values,
            start: self.start,
        }
    }
}