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
use std::collections::hash_map::{self, HashMap};

pub struct IdsContainer<T> {
    data: HashMap<u32, T>,
    next_id: u32
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Id {
    value: u32,
}

pub struct Iter<'a, T: 'a> {
    iter: hash_map::Iter<'a, u32, T>,
}

pub struct IterMut<'a, T: 'a> {
    iter: hash_map::IterMut<'a, u32, T>,
}

impl<T> IdsContainer<T> {
    pub fn new() -> IdsContainer<T> {
        IdsContainer {
            data: HashMap::new(),
            next_id: 1,
        }
    }

    /// Returns an iterator for all the keys and values in the container.
    pub fn get<'a>(&'a self, id: &Id) -> Option<&'a T> {
        self.data.get(&id.value)
    }

    /// Returns an iterator for all the keys and values in the container.
    pub fn get_mut<'a>(&'a mut self, id: &Id) -> Option<&'a mut T> {
        self.data.get_mut(&id.value)
    }

    /// Returns an iterator for all the keys and values in the container.
    pub fn iter(&self) -> Iter<T> {
        Iter {
            iter: self.data.iter()
        }
    }

    /// Returns an iterator for all the keys and values in the container.
    pub fn iter_mut(&mut self) -> IterMut<T> {
        IterMut {
            iter: self.data.iter_mut()
        }
    }

    /// Returns an iterator for all the values in the container.
    pub fn values(&self) -> hash_map::Values<u32, T> {
        self.data.values()
    }

    /// Returns the number of elements.
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Returns true if the container is empty.
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Inserts a new element and returns its identifier.
    pub fn insert(&mut self, value: T) -> Id {
        let new_id = self.next_id.clone();
        self.next_id += 1;

        self.data.insert(new_id, value);

        Id {
            value: new_id
        }
    }

    /// Removes an element from the container.
    ///
    /// # Panic
    ///
    /// Panics if the `id` was already removed.
    pub fn remove(&mut self, id: Id) {
        self.data.remove(&id.value).unwrap();
    }
}

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

    fn next(&mut self) -> Option<(Id, &'a T)> {
        self.iter.next().map(|(id, value)| (Id { value: id.clone() }, value))
    }
}

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

    fn next(&mut self) -> Option<(Id, &'a mut T)> {
        self.iter.next().map(|(id, value)| (Id { value: id.clone() }, value))
    }
}