Skip to main content

velox_engine/arena/
small_vec.rs

1//! 自适应序列容器 `SmallVec<T, N>` 与自适应字符串 `SmallString<N>`。
2
3use core::ops::{Deref, DerefMut};
4
5
6/// 自适应序列容器:
7/// - 桌面/移动/Web 下编译为动态扩容 `std::vec::Vec<T>`。
8#[derive(Clone, PartialEq, Eq, Debug)]
9pub struct SmallVec<T, const N: usize> {
10    inner: std::vec::Vec<T>,
11}
12
13impl<T, const N: usize> Default for SmallVec<T, N> {
14    fn default() -> Self {
15        Self::new()
16    }
17}
18
19impl<T, const N: usize> SmallVec<T, N> {
20    /// 创建一个空容器。
21    #[inline]
22    pub const fn new() -> Self {
23        {
24            Self {
25                inner: std::vec::Vec::new(),
26            }
27        }
28    }
29
30    /// 向尾部压入一个元素。
31    #[inline]
32    pub fn push(&mut self, value: T) -> bool {
33        {
34            self.inner.push(value);
35            true
36        }
37    }
38
39    /// 弹出尾部元素。
40    #[inline]
41    pub fn pop(&mut self) -> Option<T> {
42        self.inner.pop()
43    }
44
45    /// 清空容器。
46    #[inline]
47    pub fn clear(&mut self) {
48        self.inner.clear();
49    }
50
51    /// 获取切片引用。
52    #[inline]
53    pub fn as_slice(&self) -> &[T] {
54        self.inner.as_slice()
55    }
56
57    /// 获取可变切片引用。
58    #[inline]
59    pub fn as_mut_slice(&mut self) -> &mut [T] {
60        self.inner.as_mut_slice()
61    }
62
63    /// 在指定索引插入一个元素(后续元素右移)。
64    #[inline]
65    pub fn insert(&mut self, index: usize, value: T) -> bool {
66        {
67            self.inner.insert(index, value);
68            true
69        }
70    }
71
72    /// 批量追加切片副本(`T: Copy`)。
73    #[inline]
74    pub fn extend_from_slice(&mut self, slice: &[T]) -> bool
75    where
76        T: Copy,
77    {
78        {
79            self.inner.extend_from_slice(slice);
80            true
81        }
82    }
83
84    /// 按谓词原地保留元素。
85    #[inline]
86    pub fn retain<F: FnMut(&T) -> bool>(&mut self, mut f: F) {
87        self.inner.retain(|v| f(v));
88    }
89
90    /// 按索引移除并返回元素(保持顺序)。
91    #[inline]
92    pub fn remove(&mut self, index: usize) -> T {
93        self.inner.remove(index)
94    }
95
96    /// 交换移除指定位置元素($O(1)$)。
97    #[inline]
98    pub fn swap_remove(&mut self, index: usize) -> T {
99        self.inner.swap_remove(index)
100    }
101
102    /// 截断到指定长度。
103    #[inline]
104    pub fn truncate(&mut self, len: usize) {
105        self.inner.truncate(len);
106    }
107}
108
109/// 自适应 UTF-8 字符串容器。
110#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default)]
111pub struct SmallString<const N: usize> {
112    inner: std::string::String,
113}
114
115impl<const N: usize> SmallString<N> {
116    #[inline]
117    pub const fn new() -> Self {
118        {
119            Self {
120                inner: std::string::String::new(),
121            }
122        }
123    }
124
125    #[inline]
126    pub fn from_str_trunc(s: &str) -> Self {
127        let mut out = Self::new();
128        out.push_str_trunc(s);
129        out
130    }
131
132    #[inline]
133    pub fn as_str(&self) -> &str {
134        {
135            self.inner.as_str()
136        }
137    }
138
139    #[inline]
140    pub fn as_bytes(&self) -> &[u8] {
141        self.as_str().as_bytes()
142    }
143
144    #[inline]
145    pub fn len(&self) -> usize {
146        self.inner.len()
147    }
148
149    #[inline]
150    pub fn is_empty(&self) -> bool {
151        self.inner.is_empty()
152    }
153
154    #[inline]
155    pub fn clear(&mut self) {
156        self.inner.clear();
157    }
158
159    #[inline]
160    pub fn push(&mut self, ch: char) -> bool {
161        {
162            self.inner.push(ch);
163            true
164        }
165    }
166
167    /// 追加字符串切片。
168    pub fn push_str_trunc(&mut self, s: &str) -> usize {
169        {
170            self.inner.push_str(s);
171            s.len()
172        }
173    }
174
175    #[inline]
176    pub fn truncate(&mut self, new_len: usize) {
177        let mut cut = new_len.min(self.len());
178        while cut > 0 && !self.as_str().is_char_boundary(cut) {
179            cut -= 1;
180        }
181        self.inner.truncate(cut);
182    }
183
184    /// 在字符边界 `at` 处拆分,保留 `[..at]` 并返回 `[at..]`。
185    pub fn split_off(&mut self, at: usize) -> Self {
186        let mut idx = at.min(self.len());
187        while idx > 0 && !self.as_str().is_char_boundary(idx) {
188            idx -= 1;
189        }
190        {
191            Self {
192                inner: self.inner.split_off(idx),
193            }
194        }
195    }
196
197    /// 替换字节区间 `start..end` 为 `replacement`(自动对齐 UTF-8 字符边界),返回实际写入字节数。
198    pub fn replace_range(&mut self, start: usize, end: usize, replacement: &str) -> usize {
199        let mut s = start.min(self.len());
200        while s > 0 && !self.as_str().is_char_boundary(s) {
201            s -= 1;
202        }
203        let mut e = end.min(self.len()).max(s);
204        while e > s && !self.as_str().is_char_boundary(e) {
205            e -= 1;
206        }
207        {
208            self.inner.replace_range(s..e, replacement);
209            replacement.len()
210        }
211    }
212}
213
214impl<const N: usize> Deref for SmallString<N> {
215    type Target = str;
216    #[inline]
217    fn deref(&self) -> &Self::Target {
218        self.as_str()
219    }
220}
221
222impl<const N: usize> AsRef<str> for SmallString<N> {
223    #[inline]
224    fn as_ref(&self) -> &str {
225        self.as_str()
226    }
227}
228
229impl<const N: usize> From<&str> for SmallString<N> {
230    #[inline]
231    fn from(s: &str) -> Self {
232        Self::from_str_trunc(s)
233    }
234}
235
236impl<T, const N: usize> Deref for SmallVec<T, N> {
237    type Target = [T];
238    #[inline]
239    fn deref(&self) -> &Self::Target {
240        self.inner.as_slice()
241    }
242}
243
244impl<T, const N: usize> DerefMut for SmallVec<T, N> {
245    #[inline]
246    fn deref_mut(&mut self) -> &mut Self::Target {
247        self.inner.as_mut_slice()
248    }
249}
250
251impl<'a, T, const N: usize> IntoIterator for &'a SmallVec<T, N> {
252    type Item = &'a T;
253    type IntoIter = core::slice::Iter<'a, T>;
254    #[inline]
255    fn into_iter(self) -> Self::IntoIter {
256        self.inner.iter()
257    }
258}