Skip to main content

orx_pinned_vec/
pinned_vec.rs

1use crate::CapacityState;
2use core::{
3    cmp::Ordering,
4    ops::{Index, IndexMut, RangeBounds},
5};
6use orx_iterable::{Collection, CollectionMut};
7use orx_pseudo_default::PseudoDefault;
8
9/// Trait for vector representations differing from `std::vec::Vec` by the following:
10///
11/// => memory location of an element already pushed to the collection never changes unless any of the following `mut` methods is called:
12/// * `remove`, `pop`,
13/// * `insert`,
14/// * `clear`, `truncate`.
15///
16/// In other words,
17///
18/// => growth methods `push` or `extend_from_slice` do <ins>not</ins> change memory locations of already added elements.
19///
20/// # Pinned Elements Guarantee
21///
22/// A `PinnedVec` guarantees that positions of its elements **do not change implicitly**.
23///
24/// To be specific, let's assume that a pinned vector currently has `n` elements:
25///
26/// | Method    | Expected Behavior |
27/// | -------- | ------- |
28/// | `push(new_element)` | does not change the memory locations of the `n` elements |
29/// | `extend_from_slice(slice)` | does not change the memory locations of the first `n` elements |
30/// | `insert(a, new_element)` | does not change the memory locations of the first `a` elements, where `a <= n`; elements to the right of the inserted element might be changed, commonly shifted to right |
31/// | `pop()` | does not change the memory locations of the first `n-1` elements, the `n`-th element is removed |
32/// | `remove(a)` | does not change the memory locations of the first `a` elements, where `a < n`; elements to the right of the removed element might be changed, commonly shifted to left |
33/// | `truncate(a)` | does not change the memory locations of the first `a` elements, where `a < n` |
34pub trait PinnedVec<T>:
35    IntoIterator<Item = T>
36    + Collection<Item = T>
37    + CollectionMut<Item = T>
38    + PseudoDefault
39    + Index<usize, Output = T>
40    + IndexMut<usize, Output = T>
41{
42    /// Iterator yielding references to the elements of the vector.
43    type IterRev<'a>: Iterator<Item = &'a T>
44    where
45        T: 'a,
46        Self: 'a;
47
48    /// Iterator yielding mutable references to the elements of the vector.
49    type IterMutRev<'a>: Iterator<Item = &'a mut T>
50    where
51        T: 'a,
52        Self: 'a;
53
54    /// Iterator yielding slices corresponding to a range of indices, returned by the `slice` method.
55    type SliceIter<'a>: IntoIterator<Item = &'a [T]> + Default
56    where
57        T: 'a,
58        Self: 'a;
59
60    /// Iterator yielding mutable slices corresponding to a range of indices, returned by the `slice_mut` and `slice_mut_unchecked` methods.
61    type SliceMutIter<'a>: IntoIterator<Item = &'a mut [T]> + Default
62    where
63        T: 'a,
64        Self: 'a;
65
66    // pinned
67    /// Returns the index of the `element` with the given reference.
68    ///
69    /// Note that `T: Eq` is not required; reference equality is used.
70    ///
71    /// The complexity of this method depends on the particular `PinnedVec` implementation.
72    /// However, making use of referential equality, it possible to perform much better than *O(n)*,
73    /// where n is the vector length.
74    ///
75    /// For the two example implementations, complexity of this method:
76    /// * *O(1)* for [FixedVec](https://crates.io/crates/orx-fixed-vec);
77    /// * *O(f)* for [SplitVec](https://crates.io/crates/orx-split-vec) where f << n is the number of fragments.
78    fn index_of(&self, element: &T) -> Option<usize>;
79
80    /// Returns the index of the `element_ptr` pointing to an element of the vec.
81    ///
82    /// The complexity of this method depends on the particular `PinnedVec` implementation.
83    /// However, making use of referential equality, it possible to perform much better than *O(n)*,
84    /// where n is the vector length.
85    ///
86    /// For the two example implementations, complexity of this method:
87    /// * *O(1)* for [FixedVec](https://crates.io/crates/orx-fixed-vec);
88    /// * *O(f)* for [SplitVec](https://crates.io/crates/orx-split-vec) where f << n is the number of fragments.
89    fn index_of_ptr(&self, element_ptr: *const T) -> Option<usize>;
90
91    /// Appends an element to the back of a collection and returns a pointer to its position in the vector.
92    fn push_get_ptr(&mut self, value: T) -> *const T;
93
94    /// Creates an iterator of the pointers to the elements of the vec.
95    ///
96    /// # Safety
97    ///
98    /// The implementor guarantees that the pointers are valid and belong to the elements of the vector.
99    /// However, the lifetime of the pointers might be extended by the caller;
100    /// i.e., it is not bound to the lifetime of `&self`.
101    ///
102    /// Therefore, the caller is responsible for making sure that the obtained pointers are still
103    /// valid before accessing through the pointers.
104    unsafe fn iter_ptr<'v, 'i>(&'v self) -> impl Iterator<Item = *const T> + 'i
105    where
106        T: 'i;
107
108    /// Creates a reverse iterator of the pointers to the elements of the vec, starting from the last element to the first.
109    ///
110    /// # Safety
111    ///
112    /// The implementor guarantees that the pointers are valid and belong to the elements of the vector.
113    /// However, the lifetime of the pointers might be extended by the caller;
114    /// i.e., it is not bound to the lifetime of `&self`.
115    ///
116    /// Therefore, the caller is responsible for making sure that the obtained pointers are still
117    /// valid before accessing through the pointers.
118    unsafe fn iter_ptr_rev<'v, 'i>(&'v self) -> impl Iterator<Item = *const T> + 'i
119    where
120        T: 'i;
121
122    /// Returns whether or not of the `element` with the given reference belongs to this vector.
123    /// In other words, returns whether or not the reference to the `element` is valid.
124    ///
125    /// Note that `T: Eq` is not required; memory address is used.
126    ///
127    /// The complexity of this method depends on the particular `PinnedVec` implementation.
128    /// However, making use of pinned element guarantees, it possible to perform much better than *O(n)*,
129    /// where n is the vector length.
130    ///
131    /// For the two example implementations, complexity of this method:
132    /// * *O(1)* for [FixedVec](https://crates.io/crates/orx-fixed-vec);
133    /// * *O(f)* for [SplitVec](https://crates.io/crates/orx-split-vec) where f << n is the number of fragments.
134    fn contains_reference(&self, element: &T) -> bool;
135
136    /// Returns whether or not of the element with the given pointer belongs to this vector.
137    ///
138    /// Note that `T: Eq` is not required; memory address is used.
139    ///
140    /// The complexity of this method depends on the particular `PinnedVec` implementation.
141    /// However, making use of pinned element guarantees, it possible to perform much better than *O(n)*,
142    /// where n is the vector length.
143    ///
144    /// For the two example implementations, complexity of this method:
145    /// * *O(1)* for [FixedVec](https://crates.io/crates/orx-fixed-vec);
146    /// * *O(f)* for [SplitVec](https://crates.io/crates/orx-split-vec) where f << n is the number of fragments.
147    fn contains_ptr(&self, element_ptr: *const T) -> bool;
148
149    // vec
150    /// Clears the vector, removing all values.
151    ///
152    /// Note that this method has no effect on the allocated capacity of the vector.
153    ///
154    /// # Safety
155    ///
156    /// `clear` operation is **safe** both when `T: NotSelfRefVecItem` or not due to the following:
157    ///
158    /// * elements holding references to each other will be cleaned all together; hence,
159    ///   none of them can have an invalid reference;
160    /// * we cannot keep holding a reference to a vector element defined aliased the `clear` call,
161    ///   since `clear` requires a `mut` reference.
162    fn clear(&mut self);
163
164    /// Returns the total number of elements the vector can hold without reallocating.
165    fn capacity(&self) -> usize;
166
167    /// Provides detailed information of capacity state of the pinned vector.
168    ///
169    /// This information contains the current capacity which can be obtained by [`PinnedVec::capacity()`] method and extends with additional useful information.
170    fn capacity_state(&self) -> CapacityState;
171
172    /// Clones and appends all elements in a slice to the Vec.
173    ///
174    /// Iterates over `other`, clones each element, and then appends it to this vec. The other slice is traversed in-order.
175    fn extend_from_slice(&mut self, other: &[T])
176    where
177        T: Clone;
178
179    /// Extends this vector by copying `count` * `size_of::<T>()` bytes from src to self.
180    /// The source and destination may not overlap.
181    ///
182    /// This method can be considered as a combination of [`extend`] and `copy_from_nonoverlapping` methods
183    /// such that:
184    ///
185    /// * it takes the elements from `src` and writes them to this vector by `memcpy`;
186    /// * however, it does add these elements to the end of this vector which grows as needed.
187    ///
188    /// # SAFETY
189    ///
190    /// Behavior is undefined if any of the following conditions are violated:
191    ///
192    /// - (i) `src` must be valid for reads of `count * size_of::<T>()` bytes.
193    /// - (ii) `src` must be properly aligned.
194    /// - (iii) The region of memory beginning at `src` with a size of `count * size_of::<T>()`
195    ///   bytes must *not* overlap with the region of memory beginning at `dst` with the same size.
196    ///   This is automatically satisfied when it is used to extend the pinned vector.
197    ///
198    /// [`extend`]: core::iter::Extend::extend
199    unsafe fn extend_from_nonoverlapping(&mut self, src: *const T, count: usize);
200
201    /// Returns a reference to an element with the given `index` returns None if the index is out of bounds.
202    fn get(&self, index: usize) -> Option<&T>;
203    /// Returns a mutable reference to an element with the given `index` returns None if the index is out of bounds.
204    fn get_mut(&mut self, index: usize) -> Option<&mut T>;
205    /// Returns a reference to an element without doing bounds checking.
206    ///
207    /// For a safe alternative see `get`.
208    ///
209    /// # Safety
210    ///
211    /// Calling this method with an out-of-bounds index is *[undefined behavior]*
212    /// even if the resulting reference is not used.
213    unsafe fn get_unchecked(&self, index: usize) -> &T;
214    /// Returns a mutable reference to an element without doing bounds checking.
215    ///
216    /// For a safe alternative see `get_mut`.
217    ///
218    /// # Safety
219    ///
220    /// Calling this method with an out-of-bounds index is *[undefined behavior]*
221    /// even if the resulting reference is not used.
222    unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T;
223
224    /// Returns a reference to the first element of the vector; returns None if the vector is empty.
225    fn first(&self) -> Option<&T>;
226    /// Returns a reference to the last element of the vector; returns None if the vector is empty.
227    fn last(&self) -> Option<&T>;
228
229    /// Returns a reference to the first element of the vector without bounds checking.
230    ///
231    /// For a safe alternative see `first`.
232    ///
233    /// # Safety
234    ///
235    /// Calling this method when the vector is empty is *[undefined behavior]* even if the resulting reference is not used.
236    unsafe fn first_unchecked(&self) -> &T;
237    /// Returns a reference to the last element of the vector without bounds checking.
238    ///
239    /// For a safe alternative see `last`.
240    ///
241    /// # Safety
242    ///
243    /// Calling this method when the vector is empty is *[undefined behavior]* even if the resulting reference is not used.
244    unsafe fn last_unchecked(&self) -> &T;
245
246    /// Returns true if the vector contains no elements.
247    fn is_empty(&self) -> bool {
248        self.len() == 0
249    }
250    /// Returns the number of elements in the vector, also referred to as its length.
251    fn len(&self) -> usize;
252    /// Appends an element to the back of a collection.
253    fn push(&mut self, value: T);
254
255    // vec but unsafe
256    /// Inserts an element at position `index` within the vector, shifting all elements after it to the right.
257    ///
258    /// # Panics
259    /// Panics if `index >= len`.
260    fn insert(&mut self, index: usize, element: T);
261    /// Removes and returns the element at position index within the vector, shifting all elements after it to the left.
262    ///
263    /// # Panics
264    ///
265    /// Panics if index is out of bounds.
266    fn remove(&mut self, index: usize) -> T;
267    /// Removes the last element from a vector and returns it, or None if it is empty.
268    fn pop(&mut self) -> Option<T>;
269    /// Swaps two elements in the slice.
270    ///
271    /// If `a` equals to `b`, it's guaranteed that elements won't change value.
272    ///
273    /// # Arguments
274    ///
275    /// * a - The index of the first element
276    /// * b - The index of the second element.
277    fn swap(&mut self, a: usize, b: usize);
278    /// Shortens the vector, keeping the first `len` elements and dropping
279    /// the rest.
280    ///
281    /// If `len` is greater than the vector's current length, this has no
282    /// effect.
283    fn truncate(&mut self, len: usize);
284
285    /// Returns a reversed back-to-front iterator to elements of the vector.
286    fn iter_rev(&self) -> Self::IterRev<'_>;
287    /// Returns a reversed back-to-front iterator mutable references to elements of the vector.
288    fn iter_mut_rev(&mut self) -> Self::IterMutRev<'_>;
289
290    /// Returns the view on the required `range` as an iterator of slices:
291    ///
292    /// * returns an empty iterator if the range is out of bounds;
293    /// * returns an iterator yielding ordered slices that forms the required range when chained.
294    fn slices<R: RangeBounds<usize>>(&self, range: R) -> Self::SliceIter<'_>;
295
296    /// Returns a mutable view on the required `range` as an iterator of mutable slices:
297    ///
298    /// * returns an empty iterator if the range is out of bounds;
299    /// * returns an iterator yielding ordered slices that forms the required range when chained.
300    fn slices_mut<R: RangeBounds<usize>>(&mut self, range: R) -> Self::SliceMutIter<'_>;
301
302    /// Creates an exact size iterator for elements over the given `range`.
303    ///
304    /// This method can be considered as a generalization of creating a slice of a vector
305    /// such that it does not necessarily return a contagious slice of elements. It might
306    /// as well return a sequence of multiple slices, as long as the elements are positioned
307    /// at the given `range` of indices.
308    fn iter_over<'a>(
309        &'a self,
310        range: impl RangeBounds<usize>,
311    ) -> impl ExactSizeIterator<Item = &'a T>
312    where
313        T: 'a;
314
315    /// Creates a mutable exact size iterator for elements over the given `range`.
316    ///
317    /// This method can be considered as a generalization of creating a mutable slice of a vector
318    /// such that it does not necessarily return a contagious slice of elements. It might
319    /// as well return a sequence of multiple slices, as long as the elements are positioned
320    /// at the given `range` of indices.
321    fn iter_mut_over<'a>(
322        &'a mut self,
323        range: impl RangeBounds<usize>,
324    ) -> impl ExactSizeIterator<Item = &'a mut T>
325    where
326        T: 'a;
327
328    /// Returns a pointer to the `index`-th element of the vector.
329    ///
330    /// Returns `None` if `index`-th position does not belong to the vector; i.e., if `index` is out of `capacity`.
331    fn get_ptr(&self, index: usize) -> Option<*const T>;
332
333    /// Returns a mutable pointer to the `index`-th element of the vector.
334    ///
335    /// Returns `None` if `index`-th position does not belong to the vector; i.e., if `index` is out of `capacity`.
336    fn get_ptr_mut(&mut self, index: usize) -> Option<*mut T>;
337
338    /// Forces the length of the vector to `new_len`.
339    ///
340    /// This is a low-level operation that maintains none of the normal invariants of the type.
341    ///
342    /// # Safety
343    ///
344    /// - `new_len` must be less than or equal to `capacity()`.
345    /// - The elements at `old_len..new_len` must be initialized.
346    unsafe fn set_len(&mut self, new_len: usize);
347
348    /// Binary searches vector slice with a comparator function.
349    ///
350    /// The comparator function `f` should return an order code that indicates whether its argument is Less, Equal or Greater the desired target.
351    /// If the vector is not sorted or if the comparator function does not implement an order consistent with the sort order of the underlying slice, the returned result is unspecified and meaningless.
352    ///
353    /// If the value is found then Result::Ok is returned, containing the index of the matching element.
354    /// If there are multiple matches, then any one of the matches could be returned.
355    ///
356    /// If the value is not found then Result::Err is returned, containing the index where a matching element could be inserted while maintaining sorted order.
357    ///
358    /// See also binary_search and binary_search_by_key.
359    ///
360    /// # Examples
361    ///
362    /// Below example is taken from std::Vec since expected behavior of `PinnedVec` is exactly the same.
363    ///
364    /// Looks up a series of four elements.
365    /// The first is found, with a uniquely determined position; the second and third are not found; the fourth could match any position in [1, 4].
366    ///
367    /// ```rust
368    /// let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
369    ///
370    /// let seek = 13;
371    /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Ok(9));
372    /// let seek = 4;
373    /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(7));
374    /// let seek = 100;
375    /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(13));
376    /// let seek = 1;
377    /// let r = s.binary_search_by(|probe| probe.cmp(&seek));
378    /// assert!(match r { Ok(1..=4) => true, _ => false, });
379    /// ```
380    fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
381    where
382        F: FnMut(&T) -> Ordering;
383
384    /// Binary searches this vector for the `search_value`.
385    /// If the vector is not sorted, the returned result is unspecified and
386    /// meaningless.
387    ///
388    /// If the value is found then [`Result::Ok`] is returned, containing the
389    /// index of the matching element. If there are multiple matches, then any
390    /// one of the matches could be returned
391    ///
392    /// If the value is not found then [`Result::Err`] is returned, containing
393    /// the index where a matching element could be inserted while maintaining
394    /// sorted order.
395    ///
396    /// # Examples
397    ///
398    /// Below examples are taken from std::Vec since expected behavior of `PinnedVec` is exactly the same.
399    ///
400    /// Looks up a series of four elements. The first is found, with a
401    /// uniquely determined position; the second and third are not
402    /// found; the fourth could match any position in `[1, 4]`.
403    ///
404    /// ```rust
405    /// let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
406    ///
407    /// assert_eq!(s.binary_search(&13),  Ok(9));
408    /// assert_eq!(s.binary_search(&4),   Err(7));
409    /// assert_eq!(s.binary_search(&100), Err(13));
410    /// let r = s.binary_search(&1);
411    /// assert!(match r { Ok(1..=4) => true, _ => false, });
412    /// ```
413    fn binary_search(&self, search_value: &T) -> Result<usize, usize>
414    where
415        T: Ord,
416    {
417        self.binary_search_by(|p| p.cmp(search_value))
418    }
419
420    /// Binary searches this vector with a key extraction function.
421    ///
422    /// Assumes that the vector is sorted by the key, for instance with
423    /// `sort_by_key` using the same key extraction function.
424    /// If the vector is not sorted by the key, the returned result is
425    /// unspecified and meaningless.
426    ///
427    /// If the value is found then [`Result::Ok`] is returned, containing the
428    /// index of the matching element. If there are multiple matches, then any
429    /// one of the matches could be returned.
430    ///
431    /// If the value is not found then [`Result::Err`] is returned, containing
432    /// the index where a matching element could be inserted while maintaining
433    /// sorted order.
434    ///
435    /// # Examples
436    ///
437    /// Below examples are taken from std::Vec since expected behavior of `PinnedVec` is exactly the same.
438    ///
439    /// Looks up a series of four elements in a slice of pairs sorted by
440    /// their second elements. The first is found, with a uniquely
441    /// determined position; the second and third are not found; the
442    /// fourth could match any position in `[1, 4]`.
443    ///
444    /// ```
445    /// let s = [(0, 0), (2, 1), (4, 1), (5, 1), (3, 1),
446    ///          (1, 2), (2, 3), (4, 5), (5, 8), (3, 13),
447    ///          (1, 21), (2, 34), (4, 55)];
448    ///
449    /// assert_eq!(s.binary_search_by_key(&13, |&(a, b)| b),  Ok(9));
450    /// assert_eq!(s.binary_search_by_key(&4, |&(a, b)| b),   Err(7));
451    /// assert_eq!(s.binary_search_by_key(&100, |&(a, b)| b), Err(13));
452    /// let r = s.binary_search_by_key(&1, |&(a, b)| b);
453    /// assert!(match r { Ok(1..=4) => true, _ => false, });
454    /// ```
455    fn binary_search_by_key<B, F>(&self, b: &B, mut f: F) -> Result<usize, usize>
456    where
457        F: FnMut(&T) -> B,
458        B: Ord,
459    {
460        self.binary_search_by(|k| f(k).cmp(b))
461    }
462
463    /// Sorts the vector.
464    ///
465    /// This sort is stable.
466    fn sort(&mut self)
467    where
468        T: Ord;
469
470    /// Sorts the slice with a comparator function.
471    ///
472    /// This sort is stable.
473    ///
474    /// The comparator function must define a total ordering for the elements in the slice. If
475    /// the ordering is not total, the order of the elements is unspecified. An order is a
476    /// total order if it is (for all `a`, `b` and `c`):
477    ///
478    /// * total and antisymmetric: exactly one of `a < b`, `a == b` or `a > b` is true, and
479    /// * transitive, `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
480    ///
481    /// For example, while [`f64`] doesn't implement [`Ord`] because `NaN != NaN`, we can use
482    /// `partial_cmp` as our sort function when we know the slice doesn't contain a `NaN`.
483    fn sort_by<F>(&mut self, compare: F)
484    where
485        F: FnMut(&T, &T) -> Ordering;
486
487    /// Sorts the slice with a key extraction function.
488    ///
489    /// This sort is stable.
490    fn sort_by_key<K, F>(&mut self, f: F)
491    where
492        F: FnMut(&T) -> K,
493        K: Ord;
494
495    /// Returns the maximum possible capacity that the vector can grow to.
496    fn capacity_bound(&self) -> usize;
497}
498
499#[cfg(test)]
500mod tests {
501    use crate::{PinnedVec, pinned_vec_tests::testvec::TestVec};
502
503    #[test]
504    fn is_empty() {
505        let mut vec = TestVec::new(5);
506        assert!(vec.is_empty());
507
508        vec.push(1);
509        assert!(!vec.is_empty());
510
511        vec.push(2);
512        vec.push(3);
513        assert!(!vec.is_empty());
514
515        vec.clear();
516        assert!(vec.is_empty());
517    }
518}