Skip to main content

oxilean_std/vec/
types.rs

1//! Auto-generated module
2//!
3//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
4use super::functions::*;
5
6/// Difference list: a representation for O(1)-append list concatenation.
7#[allow(dead_code)]
8pub struct DList<T: 'static> {
9    run: Box<dyn Fn(Vec<T>) -> Vec<T>>,
10    size_hint: usize,
11}
12impl<T: Clone + 'static> DList<T> {
13    /// Create an empty DList.
14    #[allow(dead_code)]
15    pub fn empty() -> Self {
16        Self {
17            run: Box::new(|rest| rest),
18            size_hint: 0,
19        }
20    }
21    /// Create a singleton DList.
22    #[allow(dead_code)]
23    pub fn singleton(x: T) -> Self {
24        Self {
25            run: Box::new(move |mut rest| {
26                rest.insert(0, x.clone());
27                rest
28            }),
29            size_hint: 1,
30        }
31    }
32    /// Convert to a Vec.
33    #[allow(dead_code)]
34    pub fn to_vec(self) -> Vec<T> {
35        (self.run)(Vec::new())
36    }
37    /// Return the size hint.
38    #[allow(dead_code)]
39    pub fn size_hint(&self) -> usize {
40        self.size_hint
41    }
42}
43/// A circular buffer backed by a fixed-capacity ring array.
44#[allow(dead_code)]
45pub struct CircularBuffer<T> {
46    data: Vec<T>,
47    head: usize,
48    tail: usize,
49    len: usize,
50    capacity: usize,
51}
52impl<T> CircularBuffer<T> {
53    /// Create an empty circular buffer with the given capacity.
54    #[allow(dead_code)]
55    pub fn new(capacity: usize) -> Self {
56        Self {
57            data: Vec::with_capacity(capacity),
58            head: 0,
59            tail: 0,
60            len: 0,
61            capacity,
62        }
63    }
64    /// Return the number of elements currently in the buffer.
65    #[allow(dead_code)]
66    pub fn len(&self) -> usize {
67        self.len
68    }
69    /// Return true if the buffer is empty.
70    #[allow(dead_code)]
71    pub fn is_empty(&self) -> bool {
72        self.len == 0
73    }
74    /// Return the capacity of the buffer.
75    #[allow(dead_code)]
76    pub fn capacity(&self) -> usize {
77        self.capacity
78    }
79    /// Return true if the buffer is full.
80    #[allow(dead_code)]
81    pub fn is_full(&self) -> bool {
82        self.len == self.capacity
83    }
84}
85/// Prefix scan result container.
86#[allow(dead_code)]
87pub struct PrefixScan<T> {
88    pub values: Vec<T>,
89    pub inclusive: bool,
90    pub direction: &'static str,
91}
92impl<T: Clone> PrefixScan<T> {
93    /// Create an inclusive prefix scan result.
94    #[allow(dead_code)]
95    pub fn inclusive(values: Vec<T>) -> Self {
96        Self {
97            values,
98            inclusive: true,
99            direction: "left",
100        }
101    }
102    /// Create an exclusive prefix scan result.
103    #[allow(dead_code)]
104    pub fn exclusive(values: Vec<T>) -> Self {
105        Self {
106            values,
107            inclusive: false,
108            direction: "left",
109        }
110    }
111    /// Return the values.
112    #[allow(dead_code)]
113    pub fn values(&self) -> &[T] {
114        &self.values
115    }
116    /// Return the length of the scan result.
117    #[allow(dead_code)]
118    pub fn len(&self) -> usize {
119        self.values.len()
120    }
121    /// Return true if empty.
122    #[allow(dead_code)]
123    pub fn is_empty(&self) -> bool {
124        self.values.is_empty()
125    }
126}
127/// A Fenwick tree (Binary Indexed Tree) for prefix sums.
128#[allow(dead_code)]
129pub struct FenwickTree {
130    data: Vec<i64>,
131    n: usize,
132}
133impl FenwickTree {
134    /// Create a FenwickTree of size n (all zeros).
135    #[allow(dead_code)]
136    pub fn new(n: usize) -> Self {
137        Self {
138            data: vec![0; n + 1],
139            n,
140        }
141    }
142    /// Add `delta` to position `i` (1-indexed).
143    #[allow(dead_code)]
144    pub fn update(&mut self, mut i: usize, delta: i64) {
145        while i <= self.n {
146            self.data[i] += delta;
147            i += i & i.wrapping_neg();
148        }
149    }
150    /// Compute prefix sum [1..=i].
151    #[allow(dead_code)]
152    pub fn query(&self, mut i: usize) -> i64 {
153        let mut sum = 0i64;
154        while i > 0 {
155            sum += self.data[i];
156            i -= i & i.wrapping_neg();
157        }
158        sum
159    }
160    /// Return the size.
161    #[allow(dead_code)]
162    pub fn len(&self) -> usize {
163        self.n
164    }
165    /// Return true if empty.
166    #[allow(dead_code)]
167    pub fn is_empty(&self) -> bool {
168        self.n == 0
169    }
170}
171/// A sparse vector storing only non-zero/non-default entries.
172#[allow(dead_code)]
173pub struct SparseVec<T> {
174    entries: Vec<(usize, T)>,
175    length: usize,
176    default_val: T,
177}
178impl<T: Clone + PartialEq> SparseVec<T> {
179    /// Create a sparse vec with given length and default value.
180    #[allow(dead_code)]
181    pub fn new(length: usize, default_val: T) -> Self {
182        Self {
183            entries: Vec::new(),
184            length,
185            default_val,
186        }
187    }
188    /// Set position `i` to `value`.
189    #[allow(dead_code)]
190    pub fn set(&mut self, i: usize, value: T) {
191        if i < self.length {
192            if let Some(entry) = self.entries.iter_mut().find(|(idx, _)| *idx == i) {
193                entry.1 = value;
194            } else {
195                self.entries.push((i, value));
196            }
197        }
198    }
199    /// Get the value at position `i`.
200    #[allow(dead_code)]
201    pub fn get(&self, i: usize) -> &T {
202        self.entries
203            .iter()
204            .find(|(idx, _)| *idx == i)
205            .map(|(_, v)| v)
206            .unwrap_or(&self.default_val)
207    }
208    /// Return the logical length.
209    #[allow(dead_code)]
210    pub fn len(&self) -> usize {
211        self.length
212    }
213    /// Return true if logical length is zero.
214    #[allow(dead_code)]
215    pub fn is_empty(&self) -> bool {
216        self.length == 0
217    }
218    /// Return the number of non-default entries.
219    #[allow(dead_code)]
220    pub fn nnz(&self) -> usize {
221        self.entries.len()
222    }
223}
224/// A wrapper around a fixed-length boxed slice, representing a length-checked
225/// array.
226#[allow(dead_code)]
227#[derive(Clone, Debug, PartialEq, Eq)]
228pub struct FixedVec<T> {
229    data: Box<[T]>,
230}
231impl<T: Clone> FixedVec<T> {
232    /// Create a `FixedVec` from a `Vec`.
233    pub fn from_vec(v: Vec<T>) -> Self {
234        Self {
235            data: v.into_boxed_slice(),
236        }
237    }
238    /// Return the length.
239    pub fn len(&self) -> usize {
240        self.data.len()
241    }
242    /// Return `true` if empty.
243    pub fn is_empty(&self) -> bool {
244        self.data.is_empty()
245    }
246    /// Index into the array, returning `None` for out-of-bounds.
247    pub fn get(&self, i: usize) -> Option<&T> {
248        self.data.get(i)
249    }
250    /// Convert back to a `Vec`.
251    pub fn to_vec(&self) -> Vec<T> {
252        self.data.to_vec()
253    }
254    /// Iterate over references.
255    pub fn iter(&self) -> std::slice::Iter<'_, T> {
256        self.data.iter()
257    }
258}