aboutsummaryrefslogtreecommitdiff
path: root/lib/arena
diff options
context:
space:
mode:
Diffstat (limited to 'lib/arena')
-rw-r--r--lib/arena/Cargo.toml2
-rw-r--r--lib/arena/src/lib.rs61
-rw-r--r--lib/arena/src/map.rs39
3 files changed, 52 insertions, 50 deletions
diff --git a/lib/arena/Cargo.toml b/lib/arena/Cargo.toml
index b9dbb7ef3..f4d2ecb6c 100644
--- a/lib/arena/Cargo.toml
+++ b/lib/arena/Cargo.toml
@@ -1,6 +1,6 @@
1[package] 1[package]
2name = "la-arena" 2name = "la-arena"
3version = "0.1.1" 3version = "0.2.0"
4description = "Simple index-based arena without deletion." 4description = "Simple index-based arena without deletion."
5license = "MIT OR Apache-2.0" 5license = "MIT OR Apache-2.0"
6repository = "https://github.com/rust-analyzer/rust-analyzer" 6repository = "https://github.com/rust-analyzer/rust-analyzer"
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.
diff --git a/lib/arena/src/map.rs b/lib/arena/src/map.rs
index 980198247..d8acfe051 100644
--- a/lib/arena/src/map.rs
+++ b/lib/arena/src/map.rs
@@ -1,33 +1,34 @@
1//! A map from arena IDs to some other type. Space requirement is O(highest ID).
2
3use std::marker::PhantomData; 1use std::marker::PhantomData;
4 2
5use crate::Idx; 3use crate::Idx;
6 4
7/// A map from arena IDs to some other type. Space requirement is O(highest ID). 5/// A map from arena indexes to some other type.
6/// Space requirement is O(highest index).
8#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] 7#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
9pub struct ArenaMap<ID, V> { 8pub struct ArenaMap<IDX, V> {
10 v: Vec<Option<V>>, 9 v: Vec<Option<V>>,
11 _ty: PhantomData<ID>, 10 _ty: PhantomData<IDX>,
12} 11}
13 12
14impl<T, V> ArenaMap<Idx<T>, V> { 13impl<T, V> ArenaMap<Idx<T>, V> {
15 /// Inserts a value associated with a given arena ID into the map. 14 /// Inserts a value associated with a given arena index into the map.
16 pub fn insert(&mut self, id: Idx<T>, t: V) { 15 pub fn insert(&mut self, idx: Idx<T>, t: V) {
17 let idx = Self::to_idx(id); 16 let idx = Self::to_idx(idx);
18 17
19 self.v.resize_with((idx + 1).max(self.v.len()), || None); 18 self.v.resize_with((idx + 1).max(self.v.len()), || None);
20 self.v[idx] = Some(t); 19 self.v[idx] = Some(t);
21 } 20 }
22 21
23 /// Returns a reference to the value associated with the provided ID if it is present. 22 /// Returns a reference to the value associated with the provided index
24 pub fn get(&self, id: Idx<T>) -> Option<&V> { 23 /// if it is present.
25 self.v.get(Self::to_idx(id)).and_then(|it| it.as_ref()) 24 pub fn get(&self, idx: Idx<T>) -> Option<&V> {
25 self.v.get(Self::to_idx(idx)).and_then(|it| it.as_ref())
26 } 26 }
27 27
28 /// Returns a mutable reference to the value associated with the provided ID if it is present. 28 /// Returns a mutable reference to the value associated with the provided index
29 pub fn get_mut(&mut self, id: Idx<T>) -> Option<&mut V> { 29 /// if it is present.
30 self.v.get_mut(Self::to_idx(id)).and_then(|it| it.as_mut()) 30 pub fn get_mut(&mut self, idx: Idx<T>) -> Option<&mut V> {
31 self.v.get_mut(Self::to_idx(idx)).and_then(|it| it.as_mut())
31 } 32 }
32 33
33 /// Returns an iterator over the values in the map. 34 /// Returns an iterator over the values in the map.
@@ -40,13 +41,13 @@ impl<T, V> ArenaMap<Idx<T>, V> {
40 self.v.iter_mut().filter_map(|o| o.as_mut()) 41 self.v.iter_mut().filter_map(|o| o.as_mut())
41 } 42 }
42 43
43 /// Returns an iterator over the arena IDs and values in the map. 44 /// Returns an iterator over the arena indexes and values in the map.
44 pub fn iter(&self) -> impl Iterator<Item = (Idx<T>, &V)> { 45 pub fn iter(&self) -> impl Iterator<Item = (Idx<T>, &V)> {
45 self.v.iter().enumerate().filter_map(|(idx, o)| Some((Self::from_idx(idx), o.as_ref()?))) 46 self.v.iter().enumerate().filter_map(|(idx, o)| Some((Self::from_idx(idx), o.as_ref()?)))
46 } 47 }
47 48
48 fn to_idx(id: Idx<T>) -> usize { 49 fn to_idx(idx: Idx<T>) -> usize {
49 u32::from(id.into_raw()) as usize 50 u32::from(idx.into_raw()) as usize
50 } 51 }
51 52
52 fn from_idx(idx: usize) -> Idx<T> { 53 fn from_idx(idx: usize) -> Idx<T> {
@@ -56,8 +57,8 @@ impl<T, V> ArenaMap<Idx<T>, V> {
56 57
57impl<T, V> std::ops::Index<Idx<V>> for ArenaMap<Idx<V>, T> { 58impl<T, V> std::ops::Index<Idx<V>> for ArenaMap<Idx<V>, T> {
58 type Output = T; 59 type Output = T;
59 fn index(&self, id: Idx<V>) -> &T { 60 fn index(&self, idx: Idx<V>) -> &T {
60 self.v[Self::to_idx(id)].as_ref().unwrap() 61 self.v[Self::to_idx(idx)].as_ref().unwrap()
61 } 62 }
62} 63}
63 64