la_arena/lib.rs
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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
//! Yet another index-based arena.
#![warn(rust_2018_idioms, unused_lifetimes)]
#![warn(missing_docs)]
use std::{
cmp, fmt,
hash::{Hash, Hasher},
iter::{Enumerate, FusedIterator},
marker::PhantomData,
ops::{Index, IndexMut, Range, RangeInclusive},
};
mod map;
pub use map::{ArenaMap, Entry, OccupiedEntry, VacantEntry};
/// The raw index of a value in an arena.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct RawIdx(u32);
impl RawIdx {
/// Constructs a [`RawIdx`] from a u32.
pub const fn from_u32(u32: u32) -> Self {
RawIdx(u32)
}
/// Deconstructs a [`RawIdx`] into the underlying u32.
pub const fn into_u32(self) -> u32 {
self.0
}
}
impl From<RawIdx> for u32 {
#[inline]
fn from(raw: RawIdx) -> u32 {
raw.0
}
}
impl From<u32> for RawIdx {
#[inline]
fn from(idx: u32) -> RawIdx {
RawIdx(idx)
}
}
impl fmt::Debug for RawIdx {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl fmt::Display for RawIdx {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
/// The index of a value allocated in an arena that holds `T`s.
pub struct Idx<T> {
raw: RawIdx,
_ty: PhantomData<fn() -> T>,
}
impl<T> Ord for Idx<T> {
fn cmp(&self, other: &Self) -> cmp::Ordering {
self.raw.cmp(&other.raw)
}
}
impl<T> PartialOrd for Idx<T> {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<T> Clone for Idx<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Copy for Idx<T> {}
impl<T> PartialEq for Idx<T> {
fn eq(&self, other: &Idx<T>) -> bool {
self.raw == other.raw
}
}
impl<T> Eq for Idx<T> {}
impl<T> Hash for Idx<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.raw.hash(state);
}
}
impl<T> fmt::Debug for Idx<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut type_name = std::any::type_name::<T>();
if let Some(idx) = type_name.rfind(':') {
type_name = &type_name[idx + 1..];
}
write!(f, "Idx::<{}>({})", type_name, self.raw)
}
}
impl<T> Idx<T> {
/// Creates a new index from a [`RawIdx`].
pub const fn from_raw(raw: RawIdx) -> Self {
Idx { raw, _ty: PhantomData }
}
/// Converts this index into the underlying [`RawIdx`].
pub const fn into_raw(self) -> RawIdx {
self.raw
}
}
/// A range of densely allocated arena values.
pub struct IdxRange<T> {
range: Range<u32>,
_p: PhantomData<T>,
}
impl<T> IdxRange<T> {
/// Creates a new index range
/// inclusive of the start value and exclusive of the end value.
///
/// ```
/// let mut arena = la_arena::Arena::new();
/// let a = arena.alloc("a");
/// let b = arena.alloc("b");
/// let c = arena.alloc("c");
/// let d = arena.alloc("d");
///
/// let range = la_arena::IdxRange::new(b..d);
/// assert_eq!(&arena[range], &["b", "c"]);
/// ```
pub fn new(range: Range<Idx<T>>) -> Self {
Self { range: range.start.into_raw().into()..range.end.into_raw().into(), _p: PhantomData }
}
/// Creates a new index range
/// inclusive of the start value and end value.
///
/// ```
/// let mut arena = la_arena::Arena::new();
/// let foo = arena.alloc("foo");
/// let bar = arena.alloc("bar");
/// let baz = arena.alloc("baz");
///
/// let range = la_arena::IdxRange::new_inclusive(foo..=baz);
/// assert_eq!(&arena[range], &["foo", "bar", "baz"]);
///
/// let range = la_arena::IdxRange::new_inclusive(foo..=foo);
/// assert_eq!(&arena[range], &["foo"]);
/// ```
pub fn new_inclusive(range: RangeInclusive<Idx<T>>) -> Self {
Self {
range: u32::from(range.start().into_raw())..u32::from(range.end().into_raw()) + 1,
_p: PhantomData,
}
}
/// Returns whether the index range is empty.
///
/// ```
/// let mut arena = la_arena::Arena::new();
/// let one = arena.alloc(1);
/// let two = arena.alloc(2);
///
/// assert!(la_arena::IdxRange::new(one..one).is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.range.is_empty()
}
/// Returns the start of the index range.
pub fn start(&self) -> Idx<T> {
Idx::from_raw(RawIdx::from(self.range.start))
}
/// Returns the end of the index range.
pub fn end(&self) -> Idx<T> {
Idx::from_raw(RawIdx::from(self.range.end))
}
}
impl<T> Iterator for IdxRange<T> {
type Item = Idx<T>;
fn next(&mut self) -> Option<Self::Item> {
self.range.next().map(|raw| Idx::from_raw(raw.into()))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.range.size_hint()
}
fn count(self) -> usize
where
Self: Sized,
{
self.range.count()
}
fn last(self) -> Option<Self::Item>
where
Self: Sized,
{
self.range.last().map(|raw| Idx::from_raw(raw.into()))
}
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.range.nth(n).map(|raw| Idx::from_raw(raw.into()))
}
}
impl<T> DoubleEndedIterator for IdxRange<T> {
fn next_back(&mut self) -> Option<Self::Item> {
self.range.next_back().map(|raw| Idx::from_raw(raw.into()))
}
}
impl<T> ExactSizeIterator for IdxRange<T> {}
impl<T> FusedIterator for IdxRange<T> {}
impl<T> fmt::Debug for IdxRange<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple(&format!("IdxRange::<{}>", std::any::type_name::<T>()))
.field(&self.range)
.finish()
}
}
impl<T> Clone for IdxRange<T> {
fn clone(&self) -> Self {
Self { range: self.range.clone(), _p: PhantomData }
}
}
impl<T> PartialEq for IdxRange<T> {
fn eq(&self, other: &Self) -> bool {
self.range == other.range
}
}
impl<T> Eq for IdxRange<T> {}
/// Yet another index-based arena.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Arena<T> {
data: Vec<T>,
}
impl<T: fmt::Debug> fmt::Debug for Arena<T> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct("Arena").field("len", &self.len()).field("data", &self.data).finish()
}
}
impl<T> Arena<T> {
/// Creates a new empty arena.
///
/// ```
/// let arena: la_arena::Arena<i32> = la_arena::Arena::new();
/// assert!(arena.is_empty());
/// ```
pub const fn new() -> Arena<T> {
Arena { data: Vec::new() }
}
/// Create a new empty arena with specific capacity.
///
/// ```
/// let arena: la_arena::Arena<i32> = la_arena::Arena::with_capacity(42);
/// assert!(arena.is_empty());
/// ```
pub fn with_capacity(capacity: usize) -> Arena<T> {
Arena { data: Vec::with_capacity(capacity) }
}
/// Empties the arena, removing all contained values.
///
/// ```
/// let mut arena = la_arena::Arena::new();
///
/// arena.alloc(1);
/// arena.alloc(2);
/// arena.alloc(3);
/// assert_eq!(arena.len(), 3);
///
/// arena.clear();
/// assert!(arena.is_empty());
/// ```
pub fn clear(&mut self) {
self.data.clear();
}
/// Returns the length of the arena.
///
/// ```
/// let mut arena = la_arena::Arena::new();
/// assert_eq!(arena.len(), 0);
///
/// arena.alloc("foo");
/// assert_eq!(arena.len(), 1);
///
/// arena.alloc("bar");
/// assert_eq!(arena.len(), 2);
///
/// arena.alloc("baz");
/// assert_eq!(arena.len(), 3);
/// ```
pub fn len(&self) -> usize {
self.data.len()
}
/// Returns whether the arena contains no elements.
///
/// ```
/// let mut arena = la_arena::Arena::new();
/// assert!(arena.is_empty());
///
/// arena.alloc(0.5);
/// assert!(!arena.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
/// Allocates a new value on the arena, returning the value’s index.
///
/// ```
/// let mut arena = la_arena::Arena::new();
/// let idx = arena.alloc(50);
///
/// assert_eq!(arena[idx], 50);
/// ```
pub fn alloc(&mut self, value: T) -> Idx<T> {
let idx = self.next_idx();
self.data.push(value);
idx
}
/// Densely allocates multiple values, returning the values’ index range.
///
/// ```
/// let mut arena = la_arena::Arena::new();
/// let range = arena.alloc_many(0..4);
///
/// assert_eq!(arena[range], [0, 1, 2, 3]);
/// ```
pub fn alloc_many<II: IntoIterator<Item = T>>(&mut self, iter: II) -> IdxRange<T> {
let start = self.next_idx();
self.extend(iter);
let end = self.next_idx();
IdxRange::new(start..end)
}
/// Returns an iterator over the arena’s elements.
///
/// ```
/// let mut arena = la_arena::Arena::new();
/// let idx1 = arena.alloc(20);
/// let idx2 = arena.alloc(40);
/// let idx3 = arena.alloc(60);
///
/// let mut iterator = arena.iter();
/// assert_eq!(iterator.next(), Some((idx1, &20)));
/// assert_eq!(iterator.next(), Some((idx2, &40)));
/// assert_eq!(iterator.next(), Some((idx3, &60)));
/// ```
pub fn iter(
&self,
) -> impl ExactSizeIterator<Item = (Idx<T>, &T)> + DoubleEndedIterator + Clone {
self.data.iter().enumerate().map(|(idx, value)| (Idx::from_raw(RawIdx(idx as u32)), value))
}
/// Returns an iterator over the arena’s mutable elements.
///
/// ```
/// let mut arena = la_arena::Arena::new();
/// let idx1 = arena.alloc(20);
///
/// assert_eq!(arena[idx1], 20);
///
/// let mut iterator = arena.iter_mut();
/// *iterator.next().unwrap().1 = 10;
/// drop(iterator);
///
/// assert_eq!(arena[idx1], 10);
/// ```
pub fn iter_mut(
&mut self,
) -> impl ExactSizeIterator<Item = (Idx<T>, &mut T)> + DoubleEndedIterator {
self.data
.iter_mut()
.enumerate()
.map(|(idx, value)| (Idx::from_raw(RawIdx(idx as u32)), value))
}
/// Returns an iterator over the arena’s values.
///
/// ```
/// let mut arena = la_arena::Arena::new();
/// let idx1 = arena.alloc(20);
/// let idx2 = arena.alloc(40);
/// let idx3 = arena.alloc(60);
///
/// let mut iterator = arena.values();
/// assert_eq!(iterator.next(), Some(&20));
/// assert_eq!(iterator.next(), Some(&40));
/// assert_eq!(iterator.next(), Some(&60));
/// ```
pub fn values(&self) -> impl ExactSizeIterator<Item = &T> + DoubleEndedIterator {
self.data.iter()
}
/// Returns an iterator over the arena’s mutable values.
///
/// ```
/// let mut arena = la_arena::Arena::new();
/// let idx1 = arena.alloc(20);
///
/// assert_eq!(arena[idx1], 20);
///
/// let mut iterator = arena.values_mut();
/// *iterator.next().unwrap() = 10;
/// drop(iterator);
///
/// assert_eq!(arena[idx1], 10);
/// ```
pub fn values_mut(&mut self) -> impl ExactSizeIterator<Item = &mut T> + DoubleEndedIterator {
self.data.iter_mut()
}
/// Reallocates the arena to make it take up as little space as possible.
pub fn shrink_to_fit(&mut self) {
self.data.shrink_to_fit();
}
/// Returns the index of the next value allocated on the arena.
///
/// This method should remain private to make creating invalid `Idx`s harder.
fn next_idx(&self) -> Idx<T> {
Idx::from_raw(RawIdx(self.data.len() as u32))
}
}
impl<T> AsMut<[T]> for Arena<T> {
fn as_mut(&mut self) -> &mut [T] {
self.data.as_mut()
}
}
impl<T> Default for Arena<T> {
fn default() -> Arena<T> {
Arena { data: Vec::new() }
}
}
impl<T> Index<Idx<T>> for Arena<T> {
type Output = T;
fn index(&self, idx: Idx<T>) -> &T {
let idx = idx.into_raw().0 as usize;
&self.data[idx]
}
}
impl<T> IndexMut<Idx<T>> for Arena<T> {
fn index_mut(&mut self, idx: Idx<T>) -> &mut T {
let idx = idx.into_raw().0 as usize;
&mut self.data[idx]
}
}
impl<T> Index<IdxRange<T>> for Arena<T> {
type Output = [T];
fn index(&self, range: IdxRange<T>) -> &[T] {
let start = range.range.start as usize;
let end = range.range.end as usize;
&self.data[start..end]
}
}
impl<T> FromIterator<T> for Arena<T> {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = T>,
{
Arena { data: Vec::from_iter(iter) }
}
}
/// An iterator over the arena’s elements.
pub struct IntoIter<T>(Enumerate<<Vec<T> as IntoIterator>::IntoIter>);
impl<T> Iterator for IntoIter<T> {
type Item = (Idx<T>, T);
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|(idx, value)| (Idx::from_raw(RawIdx(idx as u32)), value))
}
}
impl<T> IntoIterator for Arena<T> {
type Item = (Idx<T>, T);
type IntoIter = IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
IntoIter(self.data.into_iter().enumerate())
}
}
impl<T> Extend<T> for Arena<T> {
fn extend<II: IntoIterator<Item = T>>(&mut self, iter: II) {
for t in iter {
self.alloc(t);
}
}
}