sevec/lib.rs
1#![doc = include_str!("../README.md")]
2
3use std::{cmp::Ordering, ops::{Bound, RangeBounds}, ptr, sync::Arc};
4
5#[derive(Clone)]
6pub struct Sevec<T> {
7 /// The underlying data.
8 data: Vec<Arc<[T]>>,
9 /// The ordered references to read through.
10 refs: Vec<*const [T]>,
11}
12
13impl <T> Sevec<T> {
14
15 /// Creates a new [`Sevec`].
16 pub fn new() -> Self {
17 return Self {
18 data: Vec::new(),
19 refs: Vec::new(),
20 };
21 }
22
23 /// Gets the length of the inner data.
24 /// This function is actually O(n) because we don't store the length as part of our structure.
25 /// This may be done externally to improve performance, however, that is up to library consumers.
26 /// ```rust
27 /// # use sevec::Sevec;
28 /// let mut sevec: Sevec<u32> = vec![1, 2, 3].into();
29 /// assert_eq!(sevec.len(), 3);
30 /// ```
31 pub fn len(&self) -> usize {
32 return self.refs.iter()
33 .map(|v| v.len())
34 .sum()
35 ;
36 }
37
38 /// Gets a reference to some data.
39 /// ```rust
40 /// # use sevec::Sevec;
41 /// let mut sevec: Sevec<u32> = vec![1, 2, 3].into();
42 /// assert_eq!(sevec.get(0), Some(&1));
43 /// assert_eq!(sevec.get(1), Some(&2));
44 /// assert_eq!(sevec.get(2), Some(&3));
45 /// assert_eq!(sevec.get(3), None);
46 /// ```
47 pub fn get(&self, idx: usize) -> Option<&T> {
48 let (chunk_idx, total_len) = self.get_chunk_and_length_from_idx(idx)?;
49 let chunk = self.get_chunk(chunk_idx)?;
50 // Gets the result
51 let final_idx = idx - total_len;
52 let res = chunk.get(final_idx)?;
53 return Some(res);
54 }
55
56 /// Adds a value to the end of the array.
57 /// ```rust
58 /// # use sevec::Sevec;
59 /// let mut sevec = Sevec::new();
60 /// sevec.push(1);
61 /// assert_eq!(sevec.to_string(), "[1]");
62 /// assert_eq!(sevec.len(), 1);
63 /// ```
64 pub fn push(&mut self, value: T) -> () {
65
66 // Creates new ptr.
67 let mut arc_ptr = Arc::<[T]>::new_uninit_slice(1);
68 // Writes value to it
69 let arc_ptr_mut = Arc::get_mut(&mut arc_ptr).unwrap();
70 arc_ptr_mut[0].write(value);
71 // Removes the "maybe uninit" status
72 // This is safe because we literally just initialized the uninitialized value (to the
73 // passed value)
74 let arc_ptr = unsafe { arc_ptr.assume_init() };
75
76 // Pushes the value
77 // In theory, we could just hard-code adding the slice length of 1 but I don't really
78 // think this would make much of a difference.
79 self.push_arc_slice(arc_ptr);
80 return;
81
82 }
83
84 /// Joins two [`Sevec<T>`]'s together.
85 /// The `other` value gets added onto the end.
86 /// ```rust
87 /// # use sevec::Sevec;
88 /// let mut sevec: Sevec<u8> = vec![1, 2, 3].into();
89 /// sevec.extend(vec![4, 5, 6].into());
90 /// assert_eq!(sevec.to_string(), "[1, 2, 3, 4, 5, 6]");
91 /// ```
92 pub fn extend(&mut self, other: Self) -> () {
93 let Self { data, refs} = other;
94 // Pushes data
95 for v in data.into_iter() {
96 self.data.push(v);
97 }
98 // Pushes refs
99 for v in refs.into_iter() {
100 self.refs.push(v);
101 }
102 }
103
104}
105
106impl <T: Clone + Sized> Sevec<T> {
107
108 /// Copies and inserts a given slice.
109 /// ```rust
110 /// # use sevec::Sevec;
111 /// let mut sevec = Sevec::new();
112 /// sevec.push_slice(&[1, 2, 3, 4]);
113 /// assert_eq!(sevec.to_string(), "[1, 2, 3, 4]");
114 /// assert_eq!(sevec.len(), 4);
115 /// ```
116 pub fn push_slice(&mut self, value: &[T]) -> () {
117
118 let arc_ptr = Arc::<[T]>::new_uninit_slice(value.len());
119 let mut arc_ptr = unsafe { arc_ptr.assume_init() };
120 let arc_ptr_mut = Arc::get_mut(&mut arc_ptr).unwrap();
121
122 // Copies the data.
123 unsafe {
124 ptr::copy_nonoverlapping(value.as_ptr(), arc_ptr_mut.as_mut_ptr(), value.len());
125 }
126
127 self.push_arc_slice(arc_ptr);
128
129 return;
130
131 }
132
133 /// Creates a copy of the slice data and inserts it at a specified location.
134 /// This method uses [`Self::insert_arc_slice`] and if able, this method should be preferred
135 /// with direct data writing to the underlying [`Arc`] data structure.
136 /// ```rust
137 /// # use sevec::Sevec;
138 /// // Initializes the data
139 /// let mut sevec: Sevec<u32> = vec![1, 2, 3, 4].into();
140 ///
141 /// // Inserts numbers between the 1 and 2
142 /// sevec.insert_slice(1, &[100, 200]).unwrap();
143 ///
144 /// // Displays the result
145 /// assert_eq!(sevec.to_string(), "[1, 100, 200, 2, 3, 4]");
146 /// ```
147 pub fn insert_slice(&mut self, idx: usize, value: &[T]) -> Option<()> {
148
149 let arc_ptr = Arc::<[T]>::new_uninit_slice(value.len());
150 let mut arc_ptr = unsafe { arc_ptr.assume_init() };
151 let arc_ptr_mut = Arc::get_mut(&mut arc_ptr).unwrap();
152
153 // Copies the data.
154 unsafe {
155 ptr::copy_nonoverlapping(value.as_ptr(), arc_ptr_mut.as_mut_ptr(), value.len());
156 }
157
158 return self.insert_arc_slice(idx, arc_ptr);
159 }
160
161 /// Removes the end of a slice and copies to out buffer.
162 /// ```rust
163 /// # use sevec::Sevec;
164 /// // Initializes the array
165 /// let mut sevec: Sevec<u32> = vec![1, 2, 3].into();
166 ///
167 /// // Slices data
168 /// let res = sevec.remove_and_copy_slice_from_end(2).unwrap();
169 ///
170 /// // Checks result
171 /// assert_eq!(&sevec.to_string(), "[1]"); // We got the first allocation only.
172 /// assert_eq!(&*res, &[2, 3]); // We got the first allocation only.
173 /// ```
174 pub fn remove_and_copy_slice_from_end(&mut self, amnt: usize) -> Option<Arc<[T]>> {
175
176 let mut amnt_sliced = 0;
177 let mut current_idx = self.refs.len();
178
179 let out_data = Arc::new_uninit_slice(amnt);
180 let mut out_data: Arc<[T]> = unsafe { out_data.assume_init() };
181 let out_data_mut = Arc::get_mut(&mut out_data).unwrap();
182
183 let mut cur_ref_iter;
184
185 loop {
186
187 if current_idx == 0 {
188 return None;
189 }
190
191 current_idx -= 1;
192
193 let cur_ref = self.refs[current_idx];
194 amnt_sliced += cur_ref.len();
195 cur_ref_iter = unsafe { cur_ref.as_ref() }.unwrap();
196
197 match amnt_sliced.cmp(&amnt) {
198 Ordering::Less => {
199 for (i, v) in cur_ref_iter.iter().enumerate() {
200 out_data_mut[amnt - amnt_sliced + i] = v.clone();
201 }
202 },
203 Ordering::Equal => {
204 for (i, v) in cur_ref_iter.iter().enumerate() {
205 out_data_mut[amnt - amnt_sliced + i] = v.clone();
206 }
207 break;
208 },
209 Ordering::Greater => {
210 let diff = amnt_sliced - amnt;
211 for (i, v) in cur_ref_iter[diff..].iter().enumerate() {
212 out_data_mut[i] = v.clone();
213 }
214 break;
215 },
216 }
217
218 }
219
220 // Updates length
221 // SAFETY: This is done in this way because we know we are just shrinking the vec.
222 let _ = unsafe { self.refs.set_len(current_idx) };
223
224 // IF we cut one in half, we add back the start.
225 if amnt_sliced > amnt {
226 // If we need to cut one in half.
227 let diff = amnt_sliced - amnt;
228 self.refs.push(
229 ptr::slice_from_raw_parts(cur_ref_iter.as_ptr(), diff)
230 );
231 }
232
233 return Some(out_data);
234
235 }
236
237
238}
239
240impl <T: Clone> Into<Vec<T>> for Sevec<T> {
241 fn into(self) -> Vec<T> {
242 return (&self).into();
243 }
244}
245
246impl <T> Sevec<T> {
247
248 /// Adds a new slice.
249 /// This method is particularly well suited to situations where direct writing to the inner
250 /// value of an [`Arc`] pointer is available. This method moves the [`Arc`] pointer without
251 /// copying.
252 ///
253 /// The following example is relatively long on account of it showcasing how to create an
254 /// uninitialized [`Arc`] pointer with the purpose of minimizing copies.
255 /// ```rust
256 /// # use sevec::Sevec;
257 /// # use std::{mem::MaybeUninit, sync::Arc};
258 /// let mut sevec: Sevec<u32> = Sevec::new();
259 ///
260 /// let data_len = 6; // Example array size
261 ///
262 /// // Creates the pointer uninitialized to avoid zeroing.
263 /// let mut data = {
264 /// let ptr = Arc::<[u32]>::new_uninit_slice(data_len);
265 /// unsafe { ptr.assume_init() }
266 /// };
267 ///
268 /// // Here we get mutable access to the data. We call unwrap without
269 /// // worry because we know we have exclusive access to the pointer.
270 /// let data_mut = Arc::get_mut(&mut data).unwrap();
271 ///
272 /// // Writing the data directly to the [`Arc`] ptr.
273 /// for i in 0..data_mut.len() {
274 /// data_mut[i] = i as u32;
275 /// }
276 ///
277 /// // Calling this function to push the data into the [`Sevec`].
278 /// sevec.push_arc_slice(data);
279 ///
280 /// // We can see the values we wrote directly into the [`Arc`] get displayed.
281 /// assert_eq!(sevec.to_string(), "[0, 1, 2, 3, 4, 5]");
282 /// ```
283 pub fn push_arc_slice(&mut self, value: Arc<[T]>) -> () {
284 // Gets the reference
285 let data_inner_ref = ptr::slice_from_raw_parts(value.as_ptr(), value.len());
286 // Adds the data.
287 self.data.push(value);
288 // Adds the reference.
289 self.refs.push(data_inner_ref);
290 return ();
291 }
292
293 /// Inserts a slice into a specified location in the [`Sevec`].
294 /// ```rust
295 /// # use sevec::Sevec;
296 /// # use std::sync::Arc;
297 /// // Creates array
298 /// let mut sevec: Sevec<u32> = vec![1, 2, 3].into();
299 /// // Creates data
300 /// let data: Arc<[u32]> = vec![4, 5, 6].into_boxed_slice().into();
301 ///
302 /// // Inserts data between the `1` and `2`.
303 /// sevec.insert_arc_slice(1, data);
304 ///
305 /// // Shows result
306 /// assert_eq!(&sevec.to_string(), "[1, 4, 5, 6, 2, 3]");
307 /// ```
308 pub fn insert_arc_slice(&mut self, idx: usize, value: Arc<[T]>) -> Option<()> {
309 // Gets slice
310 let slice = ptr::slice_from_raw_parts(value.as_ptr(), value.len());
311 // Tries to insert.
312 unsafe { self.insert_raw_slice(idx, slice) }?;
313 // Inserts Data only if the slice was added.
314 self.data.push(value);
315 // Returns result.
316 return Some(());
317 }
318
319 /// Adds a slice to the array.
320 /// This should only be done with a slice which has a lifetime associated with the lifetime of
321 /// this object, in particular, either a slice referring to something with a static lifetime or
322 /// containing data within the data of this array is intended.
323 ///
324 /// This function is intended to be used in cases where repeated data is going to be added to
325 /// the list and therefore adding the data repeatedly to the inner data stores isn't needed.
326 /// ```rust
327 /// # use sevec::Sevec;
328 /// # use std::{ptr, sync::Arc};
329 /// // Creates array
330 /// let mut sevec: Sevec<u32> = vec![1, 2, 3].into();
331 /// // Creates data
332 /// static DATA: &'static [u32; 3] = &[4, 5, 6];
333 ///
334 /// // Adds the data between the `1` and `2`
335 /// unsafe {
336 /// sevec.insert_raw_slice(1, ptr::slice_from_raw_parts(DATA.as_ptr(), DATA.len()));
337 /// };
338 ///
339 /// // Checks result
340 /// assert_eq!(&sevec.to_string(), "[1, 4, 5, 6, 2, 3]");
341 /// ```
342 pub unsafe fn insert_raw_slice(&mut self, idx: usize, slice: *const [T]) -> Option<()> {
343
344 let (mut write_idx, left, right) = match self.get_chunk_and_length_from_idx(idx) {
345 Some((chunk_idx, prev_sum))=> {
346
347 let chunk = *self.refs.get(chunk_idx)?;
348 let offset = idx - prev_sum;
349
350 // Creates left and right sides
351 let left = ptr::slice_from_raw_parts(chunk as *const T, offset);
352 let right = ptr::slice_from_raw_parts((chunk as *const T).add(offset), chunk.len() - offset);
353
354 (chunk_idx, left, right)
355 },
356 None => {
357 // If we didn't find the chunk but the chunk index was 0, we continue.
358 if idx != 0 {
359 return None;
360 }
361 (0, ptr::slice_from_raw_parts(ptr::null(), 0), ptr::slice_from_raw_parts(ptr::null(), 0))
362 },
363 };
364
365 // Inserts values
366
367 // We write this to the left of the chunk (if there was a chunk)
368 if left.len() != 0 {
369 self.refs.insert(write_idx, left);
370 write_idx += 1;
371 }
372
373 // We write the actual data over-top of the original chunk each time.
374 if slice.len() != 0 {
375 if let Some(data) = self.refs.get_mut(write_idx) {
376 *data = slice;
377 }
378 else {
379 self.refs.insert(write_idx, slice);
380 }
381
382 // self.refs.insert(write_idx, slice);
383 write_idx += 1;
384 }
385
386 if right.len() != 0 {
387 self.refs.insert(write_idx, right);
388 // write_idx += 1;
389 }
390
391 return Some(());
392
393 }
394
395 /// Removes the element at a given index.
396 /// ```rust
397 /// # use sevec::Sevec;
398 /// let mut sevec: Sevec<u32> = vec![1, 2, 3].into();
399 /// sevec.remove(1); // Removes the middle `2`
400 /// assert_eq!(sevec.to_string(), "[1, 3]");
401 /// ```
402 pub fn remove(&mut self, idx: usize) -> Option<()> {
403 return self.remove_range(idx..=idx);
404 }
405
406 /// Removes all elements within the specified range.
407 /// Without explicit bounds, this function will either start at the start or go until the end
408 /// of all the data.
409 ///
410 /// This function is a more ergonomic way of calling [`Self::remove_between_start_and_end`],
411 /// that function is also available if the [`RangeBounds`] handling overhead is unwanted.
412 ///
413 /// ```rust
414 /// # use sevec::Sevec;
415 /// let mut sevec: Sevec<u32> = vec![1, 2, 3, 4].into();
416 /// sevec.remove_range(1..=2); // Removes both `2` and `3`
417 /// assert_eq!(sevec.to_string(), "[1, 4]");
418 /// assert_eq!(sevec.len(), 2);
419 /// ```
420 pub fn remove_range(&mut self, range: impl RangeBounds<usize>) -> Option<()> {
421
422 let range_start = match range.start_bound() {
423 Bound::Included(&n) => n,
424 Bound::Excluded(&n) => n + 1,
425 Bound::Unbounded => 0,
426 };
427
428 let range_end = match range.end_bound() {
429 Bound::Unbounded => {
430
431 let (starting_chunk_idx, starting_cumu_len) = self.get_chunk_and_length_from_idx(range_start)?;
432 // This is the index of the start of the bounds within the start chunk
433 let starting_chunk_rel_idx = range_start - starting_cumu_len;
434
435 // If the relative index is the start of a chunk.
436 if starting_chunk_rel_idx == 0 {
437 // We remove the starting chunk and everything after it.
438 unsafe { self.refs.set_len(starting_chunk_idx); };
439 return Some(());
440 }
441
442 let start_mut = self.refs.get_mut(starting_chunk_idx)?;
443 // Gets new location.
444 *start_mut = ptr::slice_from_raw_parts_mut(*start_mut as *mut _, starting_chunk_rel_idx);
445 // Updates length
446 unsafe { self.refs.set_len(starting_chunk_idx + 1); };
447 return Some(());
448 }
449
450 Bound::Included(&n) => n,
451 Bound::Excluded(&n) => n.checked_sub(1)?, // if n == 0
452 };
453
454 return self.remove_between_start_and_end(range_start, range_end);
455
456 }
457
458 /// Removes all elements within the specified range.
459 /// Both the start and end values are inclusive.
460 ///
461 /// For an ergonomic wrapper around this method, use [`Self::remove_range`].
462 ///
463 /// ```rust
464 /// # use sevec::Sevec;
465 /// let mut sevec: Sevec<u32> = vec![1, 2, 3, 4].into();
466 /// // Removes everything between index 1 and 2 (numbers `2` and `3`)
467 /// sevec.remove_between_start_and_end(1, 2);
468 /// assert_eq!(sevec.to_string(), "[1, 4]");
469 /// assert_eq!(sevec.len(), 2);
470 /// ```
471 pub fn remove_between_start_and_end(&mut self, range_start: usize, range_end: usize) -> Option<()> {
472
473 let (starting_chunk_idx, starting_cumu_len) = self.get_chunk_and_length_from_idx(range_start)?;
474 // This is the index of the start of the bounds within the start chunk
475 let starting_chunk_rel_idx = range_start - starting_cumu_len;
476
477 // This could be implemented a bit better considering we know starting_chunk_idx and
478 // starting_cumu_len
479 // Slight performance like this isn't a concern right now but should be considered in the
480 // future. (TODO!)
481 // We should have a function that works like
482 // "get_chunk_and_length_from_idx_and_other_idx_and_len".
483 // Very long name but this is okay because it would mainly be used internally (though
484 // exposed externally).
485 let (ending_chunk_idx, ending_cumu_len) = self.get_chunk_and_length_from_idx(range_end)?;
486 let ending_chunk_rel_idx = range_end - ending_cumu_len;
487
488 // This unwrap shouldn't really ever fail.
489 // This is between two indexes which are known good (or supposedly are).
490 // If this fails then there are some serious problems with the state of the code.
491 // Gets the updated first chunk.
492 let mut starting_chunk = *self.refs.get(starting_chunk_idx).unwrap();
493 starting_chunk = ptr::slice_from_raw_parts(starting_chunk as *const _, starting_chunk_rel_idx);
494
495 // Gets the updated end chunk
496 let mut ending_chunk = *self.refs.get(ending_chunk_idx).unwrap();
497 ending_chunk = ptr::slice_from_raw_parts(
498 unsafe {
499 (ending_chunk as *const T).add(ending_chunk_rel_idx + 1)
500 },
501 ending_chunk.len() - (ending_chunk_rel_idx + 1)
502 );
503
504 // // Creates new array.
505 // let mut out_refs = Vec::with_capacity(self.refs.len());
506
507 let mut running_length = starting_chunk_idx;
508
509 // Reserves enough room for one more element (we may add one more via splitting).
510 // We do this just to make the `len` one more and to re-allocate for extra capacity.
511 self.refs.push(ptr::slice_from_raw_parts(ptr::null(), 0));
512
513 // Adds the chunks
514 if starting_chunk.len() != 0 {
515 self.refs[running_length] = starting_chunk;
516 running_length += 1;
517 }
518 if ending_chunk.len() != 0 {
519 self.refs[running_length] = ending_chunk;
520 running_length += 1;
521 }
522
523 // This might be able to be replaced with a [`ptr::copy`] call however, in many cases this
524 // might just be shifting one element at a time where the speedups may be very little.
525 // We subtract 1 from the length because of the padded value added earlier.
526 for i in (ending_chunk_idx + 1)..(self.refs.len() - 1) {
527 self.refs[running_length] = self.refs[i];
528 running_length += 1;
529 }
530
531 // Update the length
532 unsafe { self.refs.set_len(running_length); };
533
534 return Some(());
535
536 }
537
538 /// Gets a specified chunk as a slice.
539 /// Note, this is the underlying chunk, not the actual data at a given index.
540 /// ```rust
541 /// # use sevec::Sevec;
542 /// // Initializes the array
543 /// let mut sevec: Sevec<u32> = vec![1, 2, 3].into();
544 ///
545 /// // Pushes more data
546 /// sevec.push_slice(&[4, 5, 6]);
547 ///
548 /// // Gets the initial data
549 /// let data = sevec.get_chunk(0).unwrap();
550 ///
551 /// // Checks result
552 /// assert_eq!(&data, &[1, 2, 3]); // We got the first allocation only.
553 /// ```
554 pub fn get_chunk(&self, chunk: usize) -> Option<&[T]> {
555 let chunk_ptr = self.refs.get(chunk)?;
556 return unsafe { chunk_ptr.as_ref() };
557 }
558
559 /// Gets a specified value from both a chunk index and a chunk sub index.
560 /// ```rust
561 /// # use sevec::Sevec;
562 /// // Initializes the array
563 /// let mut sevec: Sevec<u32> = vec![1, 2, 3].into();
564 /// assert_eq!(*sevec.get_from_chunk_and_idx(0, 0).unwrap(), 1); // Gets the first item
565 /// assert_eq!(sevec.get_from_chunk_and_idx(1, 0), None); // Doesn't Exist yet
566 ///
567 /// // Pushes more data
568 /// sevec.push_slice(&[4, 5, 6]);
569 /// // Gets the first item from the second allocation
570 /// assert_eq!(*sevec.get_from_chunk_and_idx(1, 0).unwrap(), 4);
571 /// ```
572 pub fn get_from_chunk_and_idx(&self, chunk: usize, idx: usize) -> Option<&T> {
573 let chunk_slice = self.get_chunk(chunk)?;
574 return chunk_slice.get(idx);
575 }
576
577 /// Gets the chunk index of a specified input index.
578 /// The first value in the result is the chunk index.
579 /// The second value is the sum of all the previous lengths up until the specified chunk.
580 /// ```rust
581 /// # use sevec::Sevec;
582 /// // Initializes the array
583 /// let mut sevec: Sevec<u32> = vec![1, 2, 3].into();
584 ///
585 /// // Adds more data
586 /// sevec.push_slice(&[4, 5, 6]);
587 /// assert_eq!(&sevec.to_string(), "[1, 2, 3, 4, 5, 6]");
588 ///
589 /// // Gets the location of index 3
590 /// let (chunk_idx, prev_sum) = sevec.get_chunk_and_length_from_idx(3).unwrap();
591 ///
592 /// // The chunk index is 1
593 /// assert_eq!(chunk_idx, 1);
594 ///
595 /// // prev_sum is the amount of data that appeared before the chunk that was discovered.
596 /// assert_eq!(prev_sum, 3); // Because the first allocation had 3 items.
597 /// ```
598 pub fn get_chunk_and_length_from_idx(&self, idx: usize) -> Option<(usize, usize)> {
599
600 // Initializes
601 let mut total_len = 0;
602
603 // Goes through the references.
604 for (i, ref_ptr) in self.refs.iter().enumerate() {
605
606 // Calculates the new length
607 let cur_length = ref_ptr.len();
608 total_len += cur_length;
609
610 // Checks if we passed it.
611 if total_len > idx {
612 // Returns the index of the chunk and the sum of previous lengths.
613 total_len -= cur_length; // Goes to the start of the selected chunk
614 return Some((i, total_len));
615 }
616
617 }
618
619 return None;
620
621 }
622
623 /// Inserts a new slice at a given chunk position.
624 /// This method is especially useful if data can be written directly into the [`Arc<[T]>`].
625 /// An example of how this can be done can be found in the docs of [`Self::push_arc_slice`].
626 /// It is important to note that this method works on the internal chunk position, rather than
627 /// the index of the array.
628 ///
629 /// It is also likely that the method wanted in this case is actually
630 /// [`Self::insert_arc_slice`].
631 /// ```rust
632 /// # use sevec::Sevec;
633 /// # use std::sync::Arc;
634 /// // Creating a sevec
635 /// let mut sevec: Sevec<u32> = vec![1, 2, 3].into();
636 ///
637 /// // Creating new data
638 /// let slice_data = Arc::new([4, 5, 6]);
639 ///
640 /// // Adding the data before anything else.
641 /// sevec.insert_arc_slice_to_chunk_pos(0, slice_data);
642 ///
643 /// // Checking the result.
644 /// assert_eq!(&sevec.to_string(), "[4, 5, 6, 1, 2, 3]");
645 /// ```
646 pub fn insert_arc_slice_to_chunk_pos(&mut self, chunk_index: usize, value: Arc<[T]>) -> () {
647 // Gets the reference
648 let data_inner_ref = ptr::slice_from_raw_parts(value.as_ptr(), value.len());
649 // Adds the data.
650 self.data.push(value);
651 // Adds the reference.
652 self.refs.insert(chunk_index, data_inner_ref);
653 return ();
654 }
655
656 /// Gets the inner reference values.
657 pub fn get_inner_refs(&self) -> &[*const [T]] {
658 return &self.refs;
659 }
660
661 /// Gets the inner reference values mutably.
662 /// Adding values that aren't in scope for the lifetime of this object will cause undefined
663 /// behavior.
664 /// Know what you're doing before using this method.
665 pub unsafe fn get_inner_refs_mut(&mut self) -> &mut Vec<*const [T]> {
666 return &mut self.refs;
667 }
668
669 /// Gets the inner data values.
670 pub fn get_inner_data(&self) -> &[Arc<[T]>] {
671 return &self.data;
672 }
673
674 /// Gets the inner data values mutably.
675 /// Removing values that aren't in scope for the lifetime of this object will cause undefined
676 /// behavior.
677 /// Know what you're doing before using this method.
678 pub unsafe fn get_inner_data_mut(&mut self) -> &mut Vec<Arc<[T]>> {
679 return &mut self.data;
680 }
681
682}
683
684impl <T: std::fmt::Debug> std::fmt::Debug for Sevec<T> {
685 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
686
687 // Writes open bracket
688 write!(f, "[")?;
689
690 // Flag for if the item being written is the first.
691 let mut first = true;
692
693 // Writes the inner data.
694 for &ref_ptr in self.refs.iter() {
695
696 // Goes through each slice
697 let ref_slice = unsafe { &*ref_ptr };
698
699 for entry in ref_slice.iter() {
700 // Checks if value is first item
701 match first {
702 true => { first = false; },
703 false => write!(f, ", ")?,
704 }
705 // Writes value
706 write!(f, "{:?}", entry)?;
707 }
708
709 }
710
711 // Writes closing bracket
712 write!(f, "]")?;
713
714 return Ok(());
715 }
716}
717
718impl <T: std::fmt::Debug> std::string::ToString for Sevec<T> {
719 fn to_string(&self) -> String {
720 return format!("{:?}", self);
721 }
722}
723
724impl <T> From<Arc<[T]>> for Sevec<T> {
725 fn from(value: Arc<[T]>) -> Self {
726 // Gets the length
727 let value_len = value.len();
728 let ptr = ptr::slice_from_raw_parts(value.as_ptr(), value_len);
729 return Self {
730 data: vec![ value, ],
731 refs: vec![ ptr, ],
732 };
733 }
734}
735
736impl <T: Clone + Sized> From<&[T]> for Sevec<T> {
737 fn from(value: &[T]) -> Self {
738 let mut data = Self::new();
739 data.push_slice(value);
740 return data;
741 }
742}
743
744impl <T: Clone + Sized> From<Vec<T>> for Sevec<T> {
745 fn from(value: Vec<T>) -> Self {
746 let slice = value.as_slice();
747 return slice.into();
748 }
749}
750
751impl <T: Clone> Into<Vec<T>> for &Sevec<T> {
752 fn into(self) -> Vec<T> {
753
754 // Technically self.len is O(n) but the O(n) is pretty fast.
755 // I imagine this is likely worth not re-allocating over and over but that is just an
756 // assumption.
757 let out_len = self.len();
758 let mut new_vec = Vec::<T>::with_capacity(out_len);
759 let new_ptr_addr = new_vec.as_mut_ptr();
760 let mut length_sum = 0;
761
762 for chunk in &self.refs {
763 // Copies Data
764 unsafe {
765 ptr::copy_nonoverlapping(
766 *chunk as *const T,
767 new_ptr_addr.add(length_sum),
768 chunk.len()
769 );
770 };
771 // Updates last byte
772 length_sum += chunk.len();
773 }
774
775 // Updates vec length
776 unsafe{ new_vec.set_len(out_len); }
777
778 return new_vec;
779
780 }
781}
782
783#[cfg(feature = "serde")]
784mod serde_impl {
785
786 use super::*;
787
788 #[derive(Default)]
789 pub struct SevecVisitor;
790
791 impl <'de> serde::de::Visitor<'de> for SevecVisitor {
792 type Value = Sevec<u8>;
793 fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
794 return formatter.write_str("Some Bytes.");
795 }
796 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
797 where
798 E: serde::de::Error, {
799 let res = Self::Value::from(v);
800 return Ok(res);
801 }
802 fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
803 where
804 E: serde::de::Error, {
805 return Ok(v.into());
806 }
807
808 fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
809 where
810 E: serde::de::Error, {
811 return Ok(Self::Value::from(v));
812 }
813
814 fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
815 where
816 A: serde::de::SeqAccess<'de>, {
817
818 // As it should be but implementations don't like me.
819 // let len = seq.size_hint().ok_or(serde::de::Error::custom("Failed to get `size_hint` for `Sevec<T>` visitor!"))?;
820
821 let res = match seq.size_hint() {
822 // Better path with pre-allocation with size-hint.
823 Some(len) => {
824 // Gets data
825 let mut data = {
826 let data = Arc::<[u8]>::new_uninit_slice(len);
827 unsafe { data.assume_init() }
828 };
829 let data_mut = Arc::get_mut(&mut data).unwrap();
830 // Gets running index
831 let mut count = 0;
832 // Writes the data
833 while let Ok(Some(v)) = seq.next_element() {
834
835 // This is a bad sign!
836 // We just break in this case but this does mean that the size hint
837 // lied to us.
838 if count >= len {
839 return Err(serde::de::Error::custom("size_hint doesn't match actual size (size_hint undershot), a full array cannot be created."));
840 }
841 // Updates the value
842 data_mut[count] = v;
843 count += 1;
844 }
845 // let mut value = Sevec::new();
846 // value.push_arc_slice(data);
847 // return value;
848 Self::Value::from(data)
849 },
850
851 // Worse path if we don't know the length.
852 None => {
853 let mut buf = Vec::new();
854 while let Ok(Some(v)) = seq.next_element() {
855 buf.push(v);
856 }
857
858 Self::Value::from(buf)
859 },
860
861 };
862
863 return Ok(res);
864
865 }
866
867
868 }
869
870 impl <'de> serde::Deserialize<'de> for Sevec<u8> {
871 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
872 where
873 D: serde::Deserializer<'de> {
874 return deserializer.deserialize_bytes(SevecVisitor);
875 }
876
877 }
878
879 impl serde::Serialize for Sevec<u8> {
880 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
881 where
882 S: serde::Serializer {
883 let data: Vec<_> = self.into();
884 return serializer.serialize_bytes(&data);
885 }
886 }
887
888 #[test]
889 fn test_serde() {
890
891 let data: Sevec<_> = vec![1, 2, 3, 4].into();
892
893 // Serializes
894 let string = serde_json::to_string(&data).unwrap();
895 // Deserializes
896 let deser: Sevec<u8> = serde_json::from_str(&string).unwrap();
897
898 // Is the same as the original
899 assert_eq!(deser.to_string(), data.to_string());
900
901 }
902
903}
904
905// SAFETY: Raw pointers reference locally owned `Arc<T>` data.
906// Sound if `T: Send + Sync`.
907// This impl is copied from Arc<T>'s impl of both of these traits.
908unsafe impl <T: Send + Sync> Send for Sevec<T> {}
909unsafe impl <T: Send + Sync> Sync for Sevec<T> {}
910
911#[cfg(test)]
912mod tests {
913
914 use super::*;
915
916 #[test]
917 fn test_display() {
918 let mut v = Sevec::new();
919 v.push("Hello There!");
920 let vec = vec![
921 "Hello There!",
922 ];
923 assert_eq!(format!("{:?}", vec), format!("{:?}", v));
924 v.insert_arc_slice_to_chunk_pos(0, vec!["Hello", "H"].into());
925 v.remove_range(0..2).unwrap();
926 assert_eq!(format!("{:?}", vec), format!("{:?}", v));
927 }
928
929 #[test]
930 fn test_insert_raw_slice() {
931 let mut sevec = Sevec::new();
932 sevec.push_slice(&[1, 2, 3]);
933 let data = &[4, 5, 6];
934 let data_ptr = ptr::slice_from_raw_parts(data.as_ptr(), data.len());
935 unsafe {
936 sevec.insert_raw_slice(1, data_ptr)
937 }.unwrap();
938 assert_eq!(sevec.to_string(), "[1, 4, 5, 6, 2, 3]");
939
940 sevec.remove_range(..);
941
942 assert!(unsafe { sevec.insert_raw_slice(1, &[1, 2, 3]) }.is_none());
943 assert_eq!(sevec.len(), 0);
944 assert_eq!(sevec.refs.len(), 0);
945
946 unsafe { sevec.insert_raw_slice(0, data_ptr) }.unwrap();
947 assert_eq!(&sevec.to_string(), "[4, 5, 6]");
948
949 unsafe { sevec.insert_raw_slice(0, data_ptr) }.unwrap();
950 assert_eq!(&sevec.to_string(), "[4, 5, 6, 4, 5, 6]");
951
952 }
953
954 #[test]
955 fn test_remove_basic() {
956 let mut data = Sevec::new();
957 data.push(1);
958 data.push(2);
959 data.push(3);
960 data.push(4);
961 data.remove_range(1..=2).unwrap();
962 assert_eq!(data.len(), 2);
963 assert_eq!(data.get(0), Some(&1));
964 assert_eq!(data.get(1), Some(&4));
965 }
966
967 #[test]
968 fn test_remove_out_of_range() {
969 let mut data = Sevec::new();
970 data.push(1);
971 data.push(2);
972 data.push(3);
973 data.push(4);
974
975 assert!(data.remove_range(0..5).is_none());
976 assert!(data.remove_range(0..100).is_none());
977
978 let out_data: Vec<_> = data.clone().into();
979 assert_eq!(out_data, vec![1, 2, 3, 4]);
980
981 let res = data.remove_range(0..4);
982 assert!(res.is_some());
983
984 assert_eq!(data.len(), 0);
985 }
986
987 #[test]
988 fn test_remove_other_range() {
989
990 let mut data: Sevec<_> = vec![1, 2, 3, 4].into();
991 let res = data.remove_range(1..=2);
992 assert!(res.is_some());
993
994 }
995
996 #[test]
997 fn test_remove_everything() {
998 let mut data = Sevec::new();
999 data.push(1);
1000 data.push(2);
1001 data.push(3);
1002 data.push(4);
1003 data.remove_range(0..data.len()).unwrap();
1004 assert_eq!(data.len(), 0);
1005 // This implies that no empty pointers exist.
1006 // Not really a required attribute but is nice to have.
1007 assert_eq!(data.refs.len(), 0);
1008 }
1009
1010 #[test]
1011 fn test_remove_slices() {
1012
1013 let mut data = Sevec::new();
1014 data.push_arc_slice(vec![5, 2, 1].into());
1015 data.push(1);
1016 data.push(2);
1017 data.push(3);
1018 data.push(4);
1019 data.push_arc_slice(vec![1, 2, 3].into());
1020
1021 assert_eq!(format!("{:?}", data), format!("{:?}",
1022 vec![5, 2, 1, 1, 2, 3, 4, 1, 2, 3]
1023 ));
1024
1025 data.remove_range(1..9).unwrap(); // Remove across two slices
1026 assert_eq!(format!("{:?}", data), format!("{:?}", vec![5, 3]));
1027 data.remove_range(1..).unwrap(); // Unbounded remove
1028 assert_eq!(format!("{:?}", data), format!("{:?}", vec![5]));
1029 data.push(3);
1030 data.remove_range(..data.len()).unwrap(); // Unbounded remove from front
1031 assert_eq!(format!("{:?}", data), format!("{:?}", Vec::<i32>::new()));
1032 }
1033
1034 #[test]
1035 fn test_getting_data() {
1036
1037 let mut data = Sevec::new();
1038
1039 // With Data Checks
1040 data.push(0);
1041 data.push(1);
1042 data.push(2);
1043
1044 assert_eq!(data.get(0), Some(&0));
1045 assert_eq!(data.get(1), Some(&1));
1046 assert_eq!(data.get(2), Some(&2));
1047 assert_eq!(data.get(3), None);
1048
1049 // Pushes new data
1050 data.push_arc_slice(vec![1, 2, 3].into());
1051
1052 // Previous Values
1053 assert_eq!(data.get(0), Some(&0));
1054 assert_eq!(data.get(1), Some(&1));
1055 assert_eq!(data.get(2), Some(&2));
1056
1057 // Extended values
1058 assert_eq!(data.get(3), Some(&1));
1059 assert_eq!(data.get(4), Some(&2));
1060 assert_eq!(data.get(5), Some(&3));
1061 assert_eq!(data.get(6), None);
1062
1063 // Data removed check
1064 data.remove(1).unwrap();
1065
1066 assert_eq!(data.get(0), Some(&0));
1067 assert_eq!(data.get(1), Some(&2));
1068 assert_eq!(data.get(5), None); // After removal the end would have moved.
1069
1070 data.remove_range(..).unwrap();
1071 assert_eq!(data.len(), 0);
1072
1073 }
1074
1075 #[test]
1076 fn test_push_slice() {
1077
1078 let mut data: Sevec<u32> = vec![1, 23, 3, 3].into();
1079 data.push_slice(&[1, 2, 3, 4]);
1080
1081 let reference_vec: Vec<_> = data.into();
1082 assert_eq!(
1083 reference_vec,
1084 vec![1, 23, 3, 3, 1, 2, 3, 4]
1085 );
1086
1087 }
1088
1089 #[test]
1090 fn test_remove_and_copy_from_end() {
1091
1092 let mut data_1: Sevec<u8> = Sevec::new();
1093 data_1.push(1);
1094 data_1.push(2);
1095 data_1.push(3);
1096 data_1.push(4);
1097
1098 assert!(data_1.remove_and_copy_slice_from_end(5).is_none());
1099
1100 let res_1 = data_1.remove_and_copy_slice_from_end(2).unwrap();
1101
1102 assert_eq!(data_1.to_string(), "[1, 2]");
1103 assert_eq!(format!("{:?}", res_1), "[3, 4]");
1104
1105 let res_2 = data_1.remove_and_copy_slice_from_end(0).unwrap();
1106 assert_eq!(data_1.to_string(), "[1, 2]");
1107 assert_eq!(format!("{:?}", res_2), "[]");
1108
1109 let res_3 = data_1.remove_and_copy_slice_from_end(1).unwrap();
1110 assert_eq!(data_1.to_string(), "[1]");
1111 assert_eq!(format!("{:?}", res_3), "[2]");
1112
1113 let mut data_2: Sevec<u8> = Sevec::new();
1114 data_2.push_slice(&[1, 2]);
1115 data_2.push(3);
1116
1117
1118 let res_4 = data_2.remove_and_copy_slice_from_end(2).unwrap();
1119 assert_eq!(data_1.to_string(), "[1]");
1120 assert_eq!(format!("{:?}", res_4), "[2, 3]");
1121
1122 }
1123
1124}