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 /// Returns a reference to an element with the given `index` returns None if the index is out of bounds.
180 fn get(&self, index: usize) -> Option<&T>;
181 /// Returns a mutable reference to an element with the given `index` returns None if the index is out of bounds.
182 fn get_mut(&mut self, index: usize) -> Option<&mut T>;
183 /// Returns a reference to an element without doing bounds checking.
184 ///
185 /// For a safe alternative see `get`.
186 ///
187 /// # Safety
188 ///
189 /// Calling this method with an out-of-bounds index is *[undefined behavior]*
190 /// even if the resulting reference is not used.
191 unsafe fn get_unchecked(&self, index: usize) -> &T;
192 /// Returns a mutable reference to an element without doing bounds checking.
193 ///
194 /// For a safe alternative see `get_mut`.
195 ///
196 /// # Safety
197 ///
198 /// Calling this method with an out-of-bounds index is *[undefined behavior]*
199 /// even if the resulting reference is not used.
200 unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T;
201
202 /// Returns a reference to the first element of the vector; returns None if the vector is empty.
203 fn first(&self) -> Option<&T>;
204 /// Returns a reference to the last element of the vector; returns None if the vector is empty.
205 fn last(&self) -> Option<&T>;
206
207 /// Returns a reference to the first element of the vector without bounds checking.
208 ///
209 /// For a safe alternative see `first`.
210 ///
211 /// # Safety
212 ///
213 /// Calling this method when the vector is empty is *[undefined behavior]* even if the resulting reference is not used.
214 unsafe fn first_unchecked(&self) -> &T;
215 /// Returns a reference to the last element of the vector without bounds checking.
216 ///
217 /// For a safe alternative see `last`.
218 ///
219 /// # Safety
220 ///
221 /// Calling this method when the vector is empty is *[undefined behavior]* even if the resulting reference is not used.
222 unsafe fn last_unchecked(&self) -> &T;
223
224 /// Returns true if the vector contains no elements.
225 fn is_empty(&self) -> bool {
226 self.len() == 0
227 }
228 /// Returns the number of elements in the vector, also referred to as its length.
229 fn len(&self) -> usize;
230 /// Appends an element to the back of a collection.
231 fn push(&mut self, value: T);
232
233 // vec but unsafe
234 /// Inserts an element at position `index` within the vector, shifting all elements after it to the right.
235 ///
236 /// # Panics
237 /// Panics if `index >= len`.
238 fn insert(&mut self, index: usize, element: T);
239 /// Removes and returns the element at position index within the vector, shifting all elements after it to the left.
240 ///
241 /// # Panics
242 ///
243 /// Panics if index is out of bounds.
244 fn remove(&mut self, index: usize) -> T;
245 /// Removes the last element from a vector and returns it, or None if it is empty.
246 fn pop(&mut self) -> Option<T>;
247 /// Swaps two elements in the slice.
248 ///
249 /// If `a` equals to `b`, it's guaranteed that elements won't change value.
250 ///
251 /// # Arguments
252 ///
253 /// * a - The index of the first element
254 /// * b - The index of the second element.
255 fn swap(&mut self, a: usize, b: usize);
256 /// Shortens the vector, keeping the first `len` elements and dropping
257 /// the rest.
258 ///
259 /// If `len` is greater than the vector's current length, this has no
260 /// effect.
261 fn truncate(&mut self, len: usize);
262
263 /// Returns a reversed back-to-front iterator to elements of the vector.
264 fn iter_rev(&self) -> Self::IterRev<'_>;
265 /// Returns a reversed back-to-front iterator mutable references to elements of the vector.
266 fn iter_mut_rev(&mut self) -> Self::IterMutRev<'_>;
267
268 /// Returns the view on the required `range` as an iterator of slices:
269 ///
270 /// * returns an empty iterator if the range is out of bounds;
271 /// * returns an iterator yielding ordered slices that forms the required range when chained.
272 fn slices<R: RangeBounds<usize>>(&self, range: R) -> Self::SliceIter<'_>;
273
274 /// Returns a mutable view on the required `range` as an iterator of mutable slices:
275 ///
276 /// * returns an empty iterator if the range is out of bounds;
277 /// * returns an iterator yielding ordered slices that forms the required range when chained.
278 fn slices_mut<R: RangeBounds<usize>>(&mut self, range: R) -> Self::SliceMutIter<'_>;
279
280 /// Creates an exact size iterator for elements over the given `range`.
281 ///
282 /// This method can be considered as a generalization of creating a slice of a vector
283 /// such that it does not necessarily return a contagious slice of elements. It might
284 /// as well return a sequence of multiple slices, as long as the elements are positioned
285 /// at the given `range` of indices.
286 fn iter_over<'a>(
287 &'a self,
288 range: impl RangeBounds<usize>,
289 ) -> impl ExactSizeIterator<Item = &'a T>
290 where
291 T: 'a;
292
293 /// Creates a mutable exact size iterator for elements over the given `range`.
294 ///
295 /// This method can be considered as a generalization of creating a mutable slice of a vector
296 /// such that it does not necessarily return a contagious slice of elements. It might
297 /// as well return a sequence of multiple slices, as long as the elements are positioned
298 /// at the given `range` of indices.
299 fn iter_mut_over<'a>(
300 &'a mut self,
301 range: impl RangeBounds<usize>,
302 ) -> impl ExactSizeIterator<Item = &'a mut T>
303 where
304 T: 'a;
305
306 /// Returns a pointer to the `index`-th element of the vector.
307 ///
308 /// Returns `None` if `index`-th position does not belong to the vector; i.e., if `index` is out of `capacity`.
309 fn get_ptr(&self, index: usize) -> Option<*const T>;
310
311 /// Returns a mutable pointer to the `index`-th element of the vector.
312 ///
313 /// Returns `None` if `index`-th position does not belong to the vector; i.e., if `index` is out of `capacity`.
314 fn get_ptr_mut(&mut self, index: usize) -> Option<*mut T>;
315
316 /// Forces the length of the vector to `new_len`.
317 ///
318 /// This is a low-level operation that maintains none of the normal invariants of the type.
319 ///
320 /// # Safety
321 ///
322 /// - `new_len` must be less than or equal to `capacity()`.
323 /// - The elements at `old_len..new_len` must be initialized.
324 unsafe fn set_len(&mut self, new_len: usize);
325
326 /// Binary searches vector slice with a comparator function.
327 ///
328 /// The comparator function `f` should return an order code that indicates whether its argument is Less, Equal or Greater the desired target.
329 /// 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.
330 ///
331 /// If the value is found then Result::Ok is returned, containing the index of the matching element.
332 /// If there are multiple matches, then any one of the matches could be returned.
333 ///
334 /// 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.
335 ///
336 /// See also binary_search and binary_search_by_key.
337 ///
338 /// # Examples
339 ///
340 /// Below example is taken from std::Vec since expected behavior of `PinnedVec` is exactly the same.
341 ///
342 /// Looks up a series of four elements.
343 /// 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].
344 ///
345 /// ```rust
346 /// let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
347 ///
348 /// let seek = 13;
349 /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Ok(9));
350 /// let seek = 4;
351 /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(7));
352 /// let seek = 100;
353 /// assert_eq!(s.binary_search_by(|probe| probe.cmp(&seek)), Err(13));
354 /// let seek = 1;
355 /// let r = s.binary_search_by(|probe| probe.cmp(&seek));
356 /// assert!(match r { Ok(1..=4) => true, _ => false, });
357 /// ```
358 fn binary_search_by<F>(&self, f: F) -> Result<usize, usize>
359 where
360 F: FnMut(&T) -> Ordering;
361
362 /// Binary searches this vector for the `search_value`.
363 /// If the vector is not sorted, the returned result is unspecified and
364 /// meaningless.
365 ///
366 /// If the value is found then [`Result::Ok`] is returned, containing the
367 /// index of the matching element. If there are multiple matches, then any
368 /// one of the matches could be returned
369 ///
370 /// If the value is not found then [`Result::Err`] is returned, containing
371 /// the index where a matching element could be inserted while maintaining
372 /// sorted order.
373 ///
374 /// # Examples
375 ///
376 /// Below examples are taken from std::Vec since expected behavior of `PinnedVec` is exactly the same.
377 ///
378 /// Looks up a series of four elements. The first is found, with a
379 /// uniquely determined position; the second and third are not
380 /// found; the fourth could match any position in `[1, 4]`.
381 ///
382 /// ```rust
383 /// let s = [0, 1, 1, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55];
384 ///
385 /// assert_eq!(s.binary_search(&13), Ok(9));
386 /// assert_eq!(s.binary_search(&4), Err(7));
387 /// assert_eq!(s.binary_search(&100), Err(13));
388 /// let r = s.binary_search(&1);
389 /// assert!(match r { Ok(1..=4) => true, _ => false, });
390 /// ```
391 fn binary_search(&self, search_value: &T) -> Result<usize, usize>
392 where
393 T: Ord,
394 {
395 self.binary_search_by(|p| p.cmp(search_value))
396 }
397
398 /// Binary searches this vector with a key extraction function.
399 ///
400 /// Assumes that the vector is sorted by the key, for instance with
401 /// `sort_by_key` using the same key extraction function.
402 /// If the vector is not sorted by the key, the returned result is
403 /// unspecified and meaningless.
404 ///
405 /// If the value is found then [`Result::Ok`] is returned, containing the
406 /// index of the matching element. If there are multiple matches, then any
407 /// one of the matches could be returned.
408 ///
409 /// If the value is not found then [`Result::Err`] is returned, containing
410 /// the index where a matching element could be inserted while maintaining
411 /// sorted order.
412 ///
413 /// # Examples
414 ///
415 /// Below examples are taken from std::Vec since expected behavior of `PinnedVec` is exactly the same.
416 ///
417 /// Looks up a series of four elements in a slice of pairs sorted by
418 /// their second elements. The first is found, with a uniquely
419 /// determined position; the second and third are not found; the
420 /// fourth could match any position in `[1, 4]`.
421 ///
422 /// ```
423 /// let s = [(0, 0), (2, 1), (4, 1), (5, 1), (3, 1),
424 /// (1, 2), (2, 3), (4, 5), (5, 8), (3, 13),
425 /// (1, 21), (2, 34), (4, 55)];
426 ///
427 /// assert_eq!(s.binary_search_by_key(&13, |&(a, b)| b), Ok(9));
428 /// assert_eq!(s.binary_search_by_key(&4, |&(a, b)| b), Err(7));
429 /// assert_eq!(s.binary_search_by_key(&100, |&(a, b)| b), Err(13));
430 /// let r = s.binary_search_by_key(&1, |&(a, b)| b);
431 /// assert!(match r { Ok(1..=4) => true, _ => false, });
432 /// ```
433 fn binary_search_by_key<B, F>(&self, b: &B, mut f: F) -> Result<usize, usize>
434 where
435 F: FnMut(&T) -> B,
436 B: Ord,
437 {
438 self.binary_search_by(|k| f(k).cmp(b))
439 }
440
441 /// Sorts the vector.
442 ///
443 /// This sort is stable.
444 fn sort(&mut self)
445 where
446 T: Ord;
447
448 /// Sorts the slice with a comparator function.
449 ///
450 /// This sort is stable.
451 ///
452 /// The comparator function must define a total ordering for the elements in the slice. If
453 /// the ordering is not total, the order of the elements is unspecified. An order is a
454 /// total order if it is (for all `a`, `b` and `c`):
455 ///
456 /// * total and antisymmetric: exactly one of `a < b`, `a == b` or `a > b` is true, and
457 /// * transitive, `a < b` and `b < c` implies `a < c`. The same must hold for both `==` and `>`.
458 ///
459 /// For example, while [`f64`] doesn't implement [`Ord`] because `NaN != NaN`, we can use
460 /// `partial_cmp` as our sort function when we know the slice doesn't contain a `NaN`.
461 fn sort_by<F>(&mut self, compare: F)
462 where
463 F: FnMut(&T, &T) -> Ordering;
464
465 /// Sorts the slice with a key extraction function.
466 ///
467 /// This sort is stable.
468 fn sort_by_key<K, F>(&mut self, f: F)
469 where
470 F: FnMut(&T) -> K,
471 K: Ord;
472
473 /// Returns the maximum possible capacity that the vector can grow to.
474 fn capacity_bound(&self) -> usize;
475}
476
477#[cfg(test)]
478mod tests {
479 use crate::{PinnedVec, pinned_vec_tests::testvec::TestVec};
480
481 #[test]
482 fn is_empty() {
483 let mut vec = TestVec::new(5);
484 assert!(vec.is_empty());
485
486 vec.push(1);
487 assert!(!vec.is_empty());
488
489 vec.push(2);
490 vec.push(3);
491 assert!(!vec.is_empty());
492
493 vec.clear();
494 assert!(vec.is_empty());
495 }
496}