aboutsummaryrefslogtreecommitdiff
path: root/lib/arena/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/arena/src/lib.rs')
-rw-r--r--lib/arena/src/lib.rs61
1 files changed, 31 insertions, 30 deletions
diff --git a/lib/arena/src/lib.rs b/lib/arena/src/lib.rs
index 78a147c7d..230a50291 100644
--- a/lib/arena/src/lib.rs
+++ b/lib/arena/src/lib.rs
@@ -1,4 +1,4 @@
1//! Yet another ID-based arena. 1//! Yet another index-based arena.
2 2
3#![warn(missing_docs)] 3#![warn(missing_docs)]
4 4
@@ -10,39 +10,40 @@ use std::{
10 ops::{Index, IndexMut}, 10 ops::{Index, IndexMut},
11}; 11};
12 12
13pub mod map; 13mod map;
14pub use map::ArenaMap;
14 15
15/// The raw ID of a value in an arena. 16/// The raw index of a value in an arena.
16#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] 17#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
17pub struct RawId(u32); 18pub struct RawIdx(u32);
18 19
19impl From<RawId> for u32 { 20impl From<RawIdx> for u32 {
20 fn from(raw: RawId) -> u32 { 21 fn from(raw: RawIdx) -> u32 {
21 raw.0 22 raw.0
22 } 23 }
23} 24}
24 25
25impl From<u32> for RawId { 26impl From<u32> for RawIdx {
26 fn from(id: u32) -> RawId { 27 fn from(idx: u32) -> RawIdx {
27 RawId(id) 28 RawIdx(idx)
28 } 29 }
29} 30}
30 31
31impl fmt::Debug for RawId { 32impl fmt::Debug for RawIdx {
32 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 33 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33 self.0.fmt(f) 34 self.0.fmt(f)
34 } 35 }
35} 36}
36 37
37impl fmt::Display for RawId { 38impl fmt::Display for RawIdx {
38 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 39 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39 self.0.fmt(f) 40 self.0.fmt(f)
40 } 41 }
41} 42}
42 43
43/// The ID of a value allocated in an arena that holds `T`s. 44/// The index of a value allocated in an arena that holds `T`s.
44pub struct Idx<T> { 45pub struct Idx<T> {
45 raw: RawId, 46 raw: RawIdx,
46 _ty: PhantomData<fn() -> T>, 47 _ty: PhantomData<fn() -> T>,
47} 48}
48 49
@@ -77,18 +78,18 @@ impl<T> fmt::Debug for Idx<T> {
77} 78}
78 79
79impl<T> Idx<T> { 80impl<T> Idx<T> {
80 /// Creates a new ID from a [`RawId`]. 81 /// Creates a new index from a [`RawIdx`].
81 pub fn from_raw(raw: RawId) -> Self { 82 pub fn from_raw(raw: RawIdx) -> Self {
82 Idx { raw, _ty: PhantomData } 83 Idx { raw, _ty: PhantomData }
83 } 84 }
84 85
85 /// Converts this ID into the underlying [`RawId`]. 86 /// Converts this index into the underlying [`RawIdx`].
86 pub fn into_raw(self) -> RawId { 87 pub fn into_raw(self) -> RawIdx {
87 self.raw 88 self.raw
88 } 89 }
89} 90}
90 91
91/// Yet another ID-based arena. 92/// Yet another index-based arena.
92#[derive(Clone, PartialEq, Eq)] 93#[derive(Clone, PartialEq, Eq)]
93pub struct Arena<T> { 94pub struct Arena<T> {
94 data: Vec<T>, 95 data: Vec<T>,
@@ -160,37 +161,37 @@ impl<T> Arena<T> {
160 self.data.is_empty() 161 self.data.is_empty()
161 } 162 }
162 163
163 /// Allocates a new value on the arena, returning the value’s ID. 164 /// Allocates a new value on the arena, returning the value’s index.
164 /// 165 ///
165 /// ``` 166 /// ```
166 /// let mut arena = la_arena::Arena::new(); 167 /// let mut arena = la_arena::Arena::new();
167 /// let id = arena.alloc(50); 168 /// let idx = arena.alloc(50);
168 /// 169 ///
169 /// assert_eq!(arena[id], 50); 170 /// assert_eq!(arena[idx], 50);
170 /// ``` 171 /// ```
171 pub fn alloc(&mut self, value: T) -> Idx<T> { 172 pub fn alloc(&mut self, value: T) -> Idx<T> {
172 let id = RawId(self.data.len() as u32); 173 let idx = RawIdx(self.data.len() as u32);
173 self.data.push(value); 174 self.data.push(value);
174 Idx::from_raw(id) 175 Idx::from_raw(idx)
175 } 176 }
176 177
177 /// Returns an iterator over the arena’s elements. 178 /// Returns an iterator over the arena’s elements.
178 /// 179 ///
179 /// ``` 180 /// ```
180 /// let mut arena = la_arena::Arena::new(); 181 /// let mut arena = la_arena::Arena::new();
181 /// let id1 = arena.alloc(20); 182 /// let idx1 = arena.alloc(20);
182 /// let id2 = arena.alloc(40); 183 /// let idx2 = arena.alloc(40);
183 /// let id3 = arena.alloc(60); 184 /// let idx3 = arena.alloc(60);
184 /// 185 ///
185 /// let mut iterator = arena.iter(); 186 /// let mut iterator = arena.iter();
186 /// assert_eq!(iterator.next(), Some((id1, &20))); 187 /// assert_eq!(iterator.next(), Some((idx1, &20)));
187 /// assert_eq!(iterator.next(), Some((id2, &40))); 188 /// assert_eq!(iterator.next(), Some((idx2, &40)));
188 /// assert_eq!(iterator.next(), Some((id3, &60))); 189 /// assert_eq!(iterator.next(), Some((idx3, &60)));
189 /// ``` 190 /// ```
190 pub fn iter( 191 pub fn iter(
191 &self, 192 &self,
192 ) -> impl Iterator<Item = (Idx<T>, &T)> + ExactSizeIterator + DoubleEndedIterator { 193 ) -> impl Iterator<Item = (Idx<T>, &T)> + ExactSizeIterator + DoubleEndedIterator {
193 self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawId(idx as u32)), value)) 194 self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawIdx(idx as u32)), value))
194 } 195 }
195 196
196 /// Reallocates the arena to make it take up as little space as possible. 197 /// Reallocates the arena to make it take up as little space as possible.