orx_imp_vec/new.rs
1use crate::ImpVec;
2use orx_fixed_vec::FixedVec;
3use orx_split_vec::{Doubling, Linear, Recursive, SplitVec};
4
5impl<T> ImpVec<T> {
6 /// Creates a new empty imp-vec.
7 ///
8 /// Default underlying pinned vector is a new [`SplitVec<T, Doubling>`](https://docs.rs/orx-split-vec/latest/orx_split_vec/struct.Doubling.html).
9 ///
10 /// # Example
11 ///
12 /// ```rust
13 /// use orx_imp_vec::*;
14 ///
15 /// let imp_vec: ImpVec<char> = ImpVec::new();
16 /// assert!(imp_vec.is_empty());
17 /// ```
18 pub fn new() -> Self {
19 Self {
20 pinned_vec: SplitVec::default().into(),
21 phantom: Default::default(),
22 }
23 }
24}
25
26impl<T> ImpVec<T, SplitVec<T, Doubling>> {
27 /// Creates a new ImpVec by creating and wrapping up a new [`SplitVec<T, Doubling>`](https://docs.rs/orx-split-vec/latest/orx_split_vec/struct.Doubling.html) as the underlying storage.
28 pub fn with_doubling_growth() -> Self {
29 SplitVec::with_doubling_growth().into()
30 }
31}
32
33impl<T> ImpVec<T, SplitVec<T, Recursive>> {
34 /// Creates a new ImpVec by creating and wrapping up a new [`SplitVec<T, Recursive>`](https://docs.rs/orx-split-vec/latest/orx_split_vec/struct.Recursive.html) as the underlying storage.
35 pub fn with_recursive_growth() -> Self {
36 SplitVec::with_recursive_growth().into()
37 }
38}
39
40impl<T> ImpVec<T, SplitVec<T, Linear>> {
41 /// Creates a new ImpVec by creating and wrapping up a new [`SplitVec<T, Linear>`](https://docs.rs/orx-split-vec/latest/orx_split_vec/struct.Linear.html) as the underlying storage.
42 ///
43 /// * Each fragment of the underlying split vector will have a capacity of `2 ^ constant_fragment_capacity_exponent`.
44 pub fn with_linear_growth(constant_fragment_capacity_exponent: usize) -> Self {
45 SplitVec::with_linear_growth(constant_fragment_capacity_exponent).into()
46 }
47}
48
49impl<T> ImpVec<T, FixedVec<T>> {
50 /// Creates a new ImpVec by creating and wrapping up a new [`FixedVec<T>`]((https://docs.rs/orx-fixed-vec/latest/orx_fixed_vec/)) as the underlying storage.
51 ///
52 /// # Safety
53 ///
54 /// Note that a `FixedVec` cannot grow beyond the given `fixed_capacity`.
55 /// In other words, has a hard upper bound on the number of elements it can hold, which is the `fixed_capacity`.
56 ///
57 /// Pushing to the vector beyond this capacity leads to "out-of-capacity" error.
58 ///
59 /// This maximum capacity can be accessed by the `capacity`method.
60 pub fn with_fixed_capacity(fixed_capacity: usize) -> Self {
61 FixedVec::new(fixed_capacity).into()
62 }
63}