polars_utils/
arena.rs

1use std::sync::atomic::{AtomicU32, Ordering};
2
3#[cfg(feature = "ir_serde")]
4use serde::{Deserialize, Serialize};
5
6use crate::error::*;
7
8unsafe fn index_of_unchecked<T>(slice: &[T], item: &T) -> usize {
9    (item as *const _ as usize - slice.as_ptr() as usize) / size_of::<T>()
10}
11
12fn index_of<T>(slice: &[T], item: &T) -> Option<usize> {
13    debug_assert!(size_of::<T>() > 0);
14    let ptr = item as *const T;
15    unsafe {
16        if slice.as_ptr() < ptr && slice.as_ptr().add(slice.len()) > ptr {
17            Some(index_of_unchecked(slice, item))
18        } else {
19            None
20        }
21    }
22}
23
24#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Ord, PartialOrd)]
25#[repr(transparent)]
26#[cfg_attr(feature = "ir_serde", derive(Serialize, Deserialize))]
27pub struct Node(pub usize);
28
29impl Default for Node {
30    fn default() -> Self {
31        Node(usize::MAX)
32    }
33}
34
35static ARENA_VERSION: AtomicU32 = AtomicU32::new(0);
36
37#[derive(Debug, Clone)]
38#[cfg_attr(feature = "ir_serde", derive(Serialize, Deserialize))]
39pub struct Arena<T> {
40    version: u32,
41    items: Vec<T>,
42}
43
44impl<T> Default for Arena<T> {
45    fn default() -> Self {
46        Self::new()
47    }
48}
49
50/// Simple Arena implementation
51/// Allocates memory and stores item in a Vec. Only deallocates when being dropped itself.
52impl<T> Arena<T> {
53    #[inline]
54    pub fn version(&self) -> u32 {
55        self.version
56    }
57
58    pub fn add(&mut self, val: T) -> Node {
59        let idx = self.items.len();
60        self.items.push(val);
61        Node(idx)
62    }
63
64    pub fn pop(&mut self) -> Option<T> {
65        self.items.pop()
66    }
67
68    pub fn last_node(&mut self) -> Option<Node> {
69        if self.is_empty() {
70            None
71        } else {
72            Some(Node(self.items.len() - 1))
73        }
74    }
75
76    pub fn len(&self) -> usize {
77        self.items.len()
78    }
79
80    pub fn is_empty(&self) -> bool {
81        self.items.is_empty()
82    }
83
84    pub fn new() -> Self {
85        Arena {
86            items: vec![],
87            version: ARENA_VERSION.fetch_add(1, Ordering::Relaxed),
88        }
89    }
90
91    pub fn with_capacity(cap: usize) -> Self {
92        Arena {
93            items: Vec::with_capacity(cap),
94            version: ARENA_VERSION.fetch_add(1, Ordering::Relaxed),
95        }
96    }
97
98    pub fn get_node(&self, val: &T) -> Option<Node> {
99        index_of(&self.items, val).map(Node)
100    }
101
102    pub fn swap(&mut self, idx_a: Node, idx_b: Node) {
103        self.items.swap(idx_a.0, idx_b.0)
104    }
105
106    #[inline]
107    pub fn get(&self, idx: Node) -> &T {
108        self.items.get(idx.0).unwrap()
109    }
110
111    #[inline]
112    /// # Safety
113    /// Doesn't do any bound checks
114    pub unsafe fn get_unchecked(&self, idx: Node) -> &T {
115        unsafe { self.items.get_unchecked(idx.0) }
116    }
117
118    #[inline]
119    pub fn get_mut(&mut self, idx: Node) -> &mut T {
120        self.items.get_mut(idx.0).unwrap()
121    }
122
123    #[inline]
124    /// Get mutable references to several items of the Arena
125    ///
126    /// The `idxs` is asserted to contain unique `Node` elements which are preferably (not
127    /// necessarily) in order.
128    pub fn get_many_mut<const N: usize>(&mut self, indices: [Node; N]) -> [&mut T; N] {
129        // @NOTE: This implementation is adapted from the Rust Nightly Standard Library. When
130        // `get_many_mut` gets stabilized we should use that.
131
132        let len = self.items.len();
133
134        // NB: The optimizer should inline the loops into a sequence
135        // of instructions without additional branching.
136        let mut valid = true;
137        for (i, &idx) in indices.iter().enumerate() {
138            valid &= idx.0 < len;
139            for &idx2 in &indices[..i] {
140                valid &= idx != idx2;
141            }
142        }
143
144        assert!(valid, "Duplicate index or out-of-bounds index");
145
146        // NB: This implementation is written as it is because any variation of
147        // `indices.map(|i| self.get_unchecked_mut(i))` would make miri unhappy,
148        // or generate worse code otherwise. This is also why we need to go
149        // through a raw pointer here.
150        let slice: *mut [T] = &mut self.items[..] as *mut _;
151        let mut arr: std::mem::MaybeUninit<[&mut T; N]> = std::mem::MaybeUninit::uninit();
152        let arr_ptr = arr.as_mut_ptr();
153
154        // SAFETY: We expect `indices` to contain disjunct values that are
155        // in bounds of `self`.
156        unsafe {
157            for i in 0..N {
158                let idx = *indices.get_unchecked(i);
159                *(*arr_ptr).get_unchecked_mut(i) = (*slice).get_unchecked_mut(idx.0);
160            }
161            arr.assume_init()
162        }
163    }
164
165    #[inline]
166    pub fn replace(&mut self, idx: Node, val: T) -> T {
167        let x = self.get_mut(idx);
168        std::mem::replace(x, val)
169    }
170
171    pub fn clear(&mut self) {
172        self.items.clear();
173        self.version = ARENA_VERSION.fetch_add(1, Ordering::Relaxed);
174    }
175}
176
177impl<T: Clone> Arena<T> {
178    pub fn duplicate(&mut self, node: Node) -> Node {
179        let item = self.items[node.0].clone();
180        self.add(item)
181    }
182}
183
184impl<T: Default> Arena<T> {
185    #[inline]
186    pub fn take(&mut self, idx: Node) -> T {
187        std::mem::take(self.get_mut(idx))
188    }
189
190    pub fn replace_with<F>(&mut self, idx: Node, f: F)
191    where
192        F: FnOnce(T) -> T,
193    {
194        let val = self.take(idx);
195        self.replace(idx, f(val));
196    }
197
198    pub fn try_replace_with<F>(&mut self, idx: Node, mut f: F) -> Result<()>
199    where
200        F: FnMut(T) -> Result<T>,
201    {
202        let val = self.take(idx);
203        self.replace(idx, f(val)?);
204        Ok(())
205    }
206}