1#[cfg(test)]
48mod test;
49
50use crate::{
51 basic::mapx_ord_rawkey::{
52 MapxOrdRawKey, MapxOrdRawKeyIter, MapxOrdRawKeyIterMut, ValueIterMut, ValueMut,
53 },
54 common::ende::ValueEnDe,
55};
56use ruc::*;
57use serde::{Deserialize, Serialize};
58use std::{borrow::Cow, cmp::Ordering};
59
60#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
64#[serde(bound = "")]
65pub struct Vecx<T> {
66 inner: MapxOrdRawKey<T>,
67}
68
69impl<T: ValueEnDe> Vecx<T> {
70 #[inline(always)]
76 pub unsafe fn shadow(&self) -> Self {
77 unsafe {
78 Self {
79 inner: self.inner.shadow(),
80 }
81 }
82 }
83
84 #[inline(always)]
90 pub unsafe fn from_bytes(s: impl AsRef<[u8]>) -> Self {
91 unsafe {
92 Self {
93 inner: MapxOrdRawKey::from_bytes(s),
94 }
95 }
96 }
97
98 #[inline(always)]
100 pub fn as_bytes(&self) -> &[u8] {
101 self.inner.as_bytes()
102 }
103
104 #[inline(always)]
106 pub fn new() -> Self {
107 Vecx {
108 inner: MapxOrdRawKey::new(),
109 }
110 }
111
112 #[inline(always)]
114 pub fn get(&self, idx: usize) -> Option<T> {
115 self.inner.get((idx as u64).to_be_bytes())
116 }
117
118 #[inline(always)]
120 pub fn get_mut(&mut self, idx: usize) -> Option<ValueMut<'_, T>> {
121 self.inner.get_mut((idx as u64).to_be_bytes())
122 }
123
124 #[inline(always)]
126 pub fn last(&self) -> Option<T> {
127 alt!(self.is_empty(), return None);
128 Some(
129 self.inner
130 .get((self.len() as u64 - 1).to_be_bytes())
131 .unwrap(),
132 )
133 }
134
135 #[inline(always)]
137 pub fn len(&self) -> usize {
138 self.inner.len()
139 }
140
141 #[inline(always)]
143 pub fn is_empty(&self) -> bool {
144 self.inner.is_empty()
145 }
146
147 #[inline(always)]
149 pub fn push(&mut self, v: &T) {
150 self.inner.insert((self.len() as u64).to_be_bytes(), v);
151 }
152
153 #[inline(always)]
159 pub fn insert(&mut self, idx: usize, v: &T) {
160 let idx = idx as u64;
161 match (self.len() as u64).cmp(&idx) {
162 Ordering::Greater => {
163 self.inner
164 .inner
165 .range_detached(
166 Cow::Borrowed(&idx.to_be_bytes()[..])
167 ..Cow::Borrowed(&(self.len() as u64).to_be_bytes()[..]),
168 )
169 .for_each(|(i, iv)| {
170 unsafe {
171 self.inner.insert_encoded_value(
172 (crate::parse_int!(i, u64) + 1).to_be_bytes(),
173 iv,
174 )
175 };
176 });
177 self.inner.insert(idx.to_be_bytes(), v);
178 }
179 Ordering::Equal => {
180 self.push(v);
181 }
182 Ordering::Less => {
183 panic!("out of index");
184 }
185 }
186 }
187
188 #[inline(always)]
190 pub fn pop(&mut self) -> Option<T> {
191 alt!(self.is_empty(), return None);
192 self.inner.remove((self.len() as u64 - 1).to_be_bytes())
193 }
194
195 #[inline(always)]
201 pub fn remove(&mut self, idx: usize) -> T {
202 let idx = idx as u64;
203 if !self.is_empty() && idx < self.len() as u64 {
204 let last_idx = self.len() as u64 - 1;
205 let ret = self.inner.remove(idx.to_be_bytes()).unwrap();
206 self.inner
207 .inner
208 .range_detached(Cow::Borrowed(&(1 + idx).to_be_bytes()[..])..)
209 .for_each(|(i, v)| {
210 unsafe {
211 self.inner.insert_encoded_value(
212 (crate::parse_int!(i, u64) - 1).to_be_bytes(),
213 v,
214 )
215 };
216 });
217 self.inner.remove(last_idx.to_be_bytes());
218 return ret;
219 }
220 panic!("out of index");
221 }
222
223 #[inline(always)]
229 pub fn swap_remove(&mut self, idx: usize) -> T {
230 let idx = idx as u64;
231 if !self.is_empty() && idx < self.len() as u64 {
232 let last_idx = self.len() as u64 - 1;
233 let ret = self.inner.remove(idx.to_be_bytes()).unwrap();
234 if let Some(v) = self.inner.remove(last_idx.to_be_bytes()) {
235 self.inner.insert(idx.to_be_bytes(), &v);
236 }
237 return ret;
238 }
239 panic!("out of index");
240 }
241
242 #[inline(always)]
248 pub fn update(&mut self, idx: usize, v: &T) -> Option<T> {
249 if idx < self.len() {
250 return self.inner.insert((idx as u64).to_be_bytes(), v);
251 }
252 panic!("out of index");
253 }
254
255 #[inline(always)]
257 pub fn iter(&self) -> VecxIter<'_, T> {
258 VecxIter(self.inner.iter())
259 }
260
261 #[inline(always)]
263 pub fn iter_mut(&mut self) -> VecxIterMut<'_, T> {
264 VecxIterMut(self.inner.iter_mut())
265 }
266
267 #[inline(always)]
269 pub fn clear(&mut self) {
270 self.inner.clear();
271 }
272
273 #[inline(always)]
275 pub fn is_the_same_instance(&self, other_hdr: &Self) -> bool {
276 self.inner.is_the_same_instance(&other_hdr.inner)
277 }
278}
279
280impl<T> Clone for Vecx<T> {
281 fn clone(&self) -> Self {
282 Self {
283 inner: self.inner.clone(),
284 }
285 }
286}
287
288impl<T: ValueEnDe> Default for Vecx<T> {
289 fn default() -> Self {
290 Self::new()
291 }
292}
293
294pub struct VecxIter<'a, T>(MapxOrdRawKeyIter<'a, T>);
299
300impl<T> Iterator for VecxIter<'_, T>
301where
302 T: ValueEnDe,
303{
304 type Item = T;
305 fn next(&mut self) -> Option<Self::Item> {
306 self.0.next().map(|(_, v)| v)
307 }
308}
309
310impl<V> DoubleEndedIterator for VecxIter<'_, V>
311where
312 V: ValueEnDe,
313{
314 fn next_back(&mut self) -> Option<Self::Item> {
315 self.0.next_back().map(|(_, v)| v)
316 }
317}
318
319pub struct VecxIterMut<'a, T>(MapxOrdRawKeyIterMut<'a, T>);
321
322impl<'a, T> Iterator for VecxIterMut<'a, T>
323where
324 T: ValueEnDe,
325{
326 type Item = ValueIterMut<'a, T>;
327 fn next(&mut self) -> Option<Self::Item> {
328 self.0.next().map(|(_, v)| v)
329 }
330}
331
332impl<V> DoubleEndedIterator for VecxIterMut<'_, V>
333where
334 V: ValueEnDe,
335{
336 fn next_back(&mut self) -> Option<Self::Item> {
337 self.0.next_back().map(|(_, v)| v)
338 }
339}
340
341