|
| 1 | +use std::collections::hash_map::{self, HashMap}; |
| 2 | + |
| 3 | +pub struct IdsContainer<T> { |
| 4 | + data: HashMap<u32, T>, |
| 5 | + next_id: u32 |
| 6 | +} |
| 7 | + |
| 8 | +#[derive(Copy, Clone, Show, PartialEq, Eq, Hash)] |
| 9 | +pub struct Id { |
| 10 | + value: u32, |
| 11 | +} |
| 12 | + |
| 13 | +pub struct Iter<'a, T: 'a> { |
| 14 | + iter: hash_map::Iter<'a, u32, T>, |
| 15 | +} |
| 16 | + |
| 17 | +pub struct IterMut<'a, T: 'a> { |
| 18 | + iter: hash_map::IterMut<'a, u32, T>, |
| 19 | +} |
| 20 | + |
| 21 | +impl<T> IdsContainer<T> { |
| 22 | + pub fn new() -> IdsContainer<T> { |
| 23 | + IdsContainer { |
| 24 | + data: HashMap::new(), |
| 25 | + next_id: 1, |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + /// Returns an iterator for all the keys and values in the container. |
| 30 | + pub fn get<'a>(&'a self, id: &Id) -> Option<&'a T> { |
| 31 | + self.data.get(&id.value) |
| 32 | + } |
| 33 | + |
| 34 | + /// Returns an iterator for all the keys and values in the container. |
| 35 | + pub fn get_mut<'a>(&'a mut self, id: &Id) -> Option<&'a mut T> { |
| 36 | + self.data.get_mut(&id.value) |
| 37 | + } |
| 38 | + |
| 39 | + /// Returns an iterator for all the keys and values in the container. |
| 40 | + pub fn iter(&self) -> Iter<T> { |
| 41 | + Iter { |
| 42 | + iter: self.data.iter() |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// Returns an iterator for all the keys and values in the container. |
| 47 | + pub fn iter_mut(&mut self) -> IterMut<T> { |
| 48 | + IterMut { |
| 49 | + iter: self.data.iter_mut() |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// Returns an iterator for all the values in the container. |
| 54 | + pub fn values(&self) -> hash_map::Values<u32, T> { |
| 55 | + self.data.values() |
| 56 | + } |
| 57 | + |
| 58 | + /// Returns the number of elements. |
| 59 | + pub fn len(&self) -> usize { |
| 60 | + self.data.len() |
| 61 | + } |
| 62 | + |
| 63 | + /// Returns true if the container is empty. |
| 64 | + pub fn is_empty(&self) -> bool { |
| 65 | + self.data.is_empty() |
| 66 | + } |
| 67 | + |
| 68 | + /// Inserts a new element and returns its identifier. |
| 69 | + pub fn insert(&mut self, value: T) -> Id { |
| 70 | + let new_id = self.next_id.clone(); |
| 71 | + self.next_id += 1; |
| 72 | + |
| 73 | + self.data.insert(new_id, value); |
| 74 | + |
| 75 | + Id { |
| 76 | + value: new_id |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /// Removes an element from the container. |
| 81 | + /// |
| 82 | + /// # Panic |
| 83 | + /// |
| 84 | + /// Panics if the `id` was already removed. |
| 85 | + pub fn remove(&mut self, id: Id) { |
| 86 | + self.data.remove(&id.value).unwrap(); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl<'a, T: 'a> Iterator for Iter<'a, T> { |
| 91 | + type Item = (Id, &'a T); |
| 92 | + |
| 93 | + fn next(&mut self) -> Option<(Id, &'a T)> { |
| 94 | + self.iter.next().map(|(id, value)| (Id { value: id.clone() }, value)) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +impl<'a, T: 'a> Iterator for IterMut<'a, T> { |
| 99 | + type Item = (Id, &'a mut T); |
| 100 | + |
| 101 | + fn next(&mut self) -> Option<(Id, &'a mut T)> { |
| 102 | + self.iter.next().map(|(id, value)| (Id { value: id.clone() }, value)) |
| 103 | + } |
| 104 | +} |
0 commit comments