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
use generational_arena::{Arena, Index};
use std::marker::PhantomData;

/// Generational index for the `ContiguousArena` collection.
pub type ContiguousArenaIndex = Index;

#[derive(Clone)]
/// This is a `Vec` behind a generational arena.
///
/// The goal of this structure is to have unique identifiers for elements
/// stored contiguously on a `Vec`.
pub struct ContiguousArena<Idx, T> {
    objects: Vec<T>,
    rev_indices: Vec<Index>,
    indices: Arena<usize>,
    phantoms: PhantomData<Idx>,
}

impl<Idx, T> ContiguousArena<Idx, T> {
    /// Creates a new empty arena.
    pub fn new() -> Self {
        Self {
            objects: Vec::new(),
            indices: Arena::new(),
            rev_indices: Vec::new(),
            phantoms: PhantomData,
        }
    }

    #[inline]
    /// Get an element from its position on the contiguous array.
    pub fn get_from_contiguous_index(&self, index: usize) -> Option<(&T, Idx)>
    where
        Idx: From<ContiguousArenaIndex>,
    {
        if let (Some(elt), Some(handle)) = (self.objects.get(index), self.rev_indices.get(index)) {
            Some((elt, Idx::from(*handle)))
        } else {
            None
        }
    }

    #[inline]
    /// The number of objects on this arena.
    pub fn len(&self) -> usize {
        self.objects.len()
    }

    #[inline]
    /// Gets a reference to the object identified by `handle`.
    pub fn get(&self, handle: Idx) -> Option<&T>
    where
        Idx: Into<ContiguousArenaIndex>,
    {
        self.objects.get(*self.indices.get(handle.into())?)
    }

    #[inline]
    /// Gets a mutable reference to the object identified by `handle`.
    pub fn get_mut(&mut self, handle: Idx) -> Option<&mut T>
    where
        Idx: Into<ContiguousArenaIndex>,
    {
        self.objects.get_mut(*self.indices.get(handle.into())?)
    }

    #[inline]
    /// Gets references to all the objects on this set.
    pub fn values(&self) -> std::slice::Iter<T> {
        self.objects.iter()
    }

    #[inline]
    /// Gets mutable references to all the objects on this set.
    pub fn values_mut(&mut self) -> std::slice::IterMut<T> {
        self.objects.iter_mut()
    }

    #[inline]
    /// Iter through all the objects as well as their handle.
    pub fn iter<'a>(&'a self) -> impl Iterator<Item = (Idx, &'a T)> + 'a
    where
        Idx: From<ContiguousArenaIndex>,
    {
        let objects = &self.objects;
        self.indices
            .iter()
            .map(move |(i, val)| (Idx::from(i), &objects[*val]))
    }

    #[inline]
    /// Retrieves the set of objects as a slice.
    pub fn as_slice(&self) -> &[T] {
        &self.objects[..]
    }

    #[inline]
    /// Retrieves the set of objects as a mutable slice.
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        &mut self.objects[..]
    }

    #[inline]
    /// Insert an object into this set.
    pub fn insert(&mut self, object: T) -> Idx
    where
        Idx: From<ContiguousArenaIndex>,
    {
        let i = self.objects.len();
        self.objects.push(object);
        let idx = self.indices.insert(i);
        self.rev_indices.push(idx);
        Idx::from(idx)
    }

    #[inline]
    /// Remove an object from this set.
    pub fn remove(&mut self, handle: Idx) -> Option<T>
    where
        Idx: Into<ContiguousArenaIndex>,
    {
        let i = self.indices.remove(handle.into())?;
        let swapped_rev_id = self.rev_indices.last().cloned();
        let deleted_object = self.objects.swap_remove(i);
        let _ = self.rev_indices.swap_remove(i);

        if let Some(rev_id) = swapped_rev_id {
            self.indices[rev_id] = i;
        }

        Some(deleted_object)
    }
}

impl<Idx: Into<ContiguousArenaIndex>, T> std::ops::Index<Idx> for ContiguousArena<Idx, T> {
    type Output = T;

    #[inline]
    fn index(&self, i: Idx) -> &T {
        &self.objects[self.indices[i.into()]]
    }
}

impl<Idx: Into<ContiguousArenaIndex>, T> std::ops::IndexMut<Idx> for ContiguousArena<Idx, T> {
    #[inline]
    fn index_mut(&mut self, i: Idx) -> &mut T {
        &mut self.objects[self.indices[i.into()]]
    }
}

impl<Idx, T> AsRef<[T]> for ContiguousArena<Idx, T> {
    #[inline]
    fn as_ref(&self) -> &[T] {
        &self.objects
    }
}

impl<Idx, T> AsMut<[T]> for ContiguousArena<Idx, T> {
    #[inline]
    fn as_mut(&mut self) -> &mut [T] {
        &mut self.objects
    }
}