1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
use crate::ImpVec;
use orx_split_vec::{
CustomGrowth, DoublingGrowth, ExponentialGrowth, FixedCapacity, Fragment, LinearGrowth,
SplitVec, SplitVecGrowth,
};
use std::rc::Rc;
pub(crate) type GetCapacityOfNewFragment<T> = dyn Fn(&[Fragment<T>]) -> usize;
impl<T, G> ImpVec<T, G>
where
G: SplitVecGrowth<T>,
{
/// Creates an empty imp-vector with the given `growth` strategy.
pub fn with_growth(growth: G) -> Self {
SplitVec::with_growth(growth).into()
}
}
impl<T> ImpVec<T, LinearGrowth> {
/// Creates an imp-vector with linear growth and given `constant_fragment_capacity`.
///
/// Assuming it is the common case compared to empty vector scenarios,
/// it immediately allocates the first fragment to keep the underlying `SplitVec` struct smaller.
///
/// # Panics
/// Panics if `constant_fragment_capacity` is zero.
///
/// # Examples
///
/// ```
/// use orx_imp_vec::ImpVec;
///
/// // ImpVec<usize, LinearGrowth>
/// let vec = ImpVec::with_linear_growth(16);
///
/// assert_eq!(1, vec.fragments().len());
/// assert_eq!(Some(16), vec.fragments().first().map(|f| f.capacity()));
/// assert_eq!(Some(0), vec.fragments().first().map(|f| f.len()));
///
/// // push 160 elements
/// for i in 0..10 * 16 {
/// vec.push(i);
/// }
///
/// assert_eq!(10, vec.fragments().len());
/// for fragment in vec.fragments() {
/// assert_eq!(16, fragment.len());
/// assert_eq!(16, fragment.capacity());
/// }
///
/// // push the 161-st element
/// vec.push(42);
/// assert_eq!(11, vec.fragments().len());
/// assert_eq!(Some(16), vec.fragments().last().map(|f| f.capacity()));
/// assert_eq!(Some(1), vec.fragments().last().map(|f| f.len()));
/// ```
pub fn with_linear_growth(constant_fragment_capacity: usize) -> Self {
assert!(constant_fragment_capacity > 0);
SplitVec::with_linear_growth(constant_fragment_capacity).into()
}
}
impl<T> ImpVec<T, DoublingGrowth> {
/// Creates an imp-vector with doubling growth
/// which creates a fragment with double the capacity
/// of the prior fragment every time the split vector needs to expand.
///
/// Assuming it is the common case compared to empty vector scenarios,
/// it immediately allocates the first fragment to keep the underlying `SplitVec` struct smaller.
///
/// # Panics
/// Panics if `first_fragment_capacity` is zero.
///
/// # Examples
///
/// ```
/// use orx_imp_vec::ImpVec;
///
/// // ImpVec<usize, DoublingGrowth>
/// let vec = ImpVec::with_doubling_growth(2);
///
/// assert_eq!(1, vec.fragments().len());
/// assert_eq!(Some(2), vec.fragments().first().map(|f| f.capacity()));
/// assert_eq!(Some(0), vec.fragments().first().map(|f| f.len()));
///
/// // fill the first 5 fragments
/// let expected_fragment_capacities = vec![2, 4, 8, 16, 32];
/// let num_items: usize = expected_fragment_capacities.iter().sum();
/// for i in 0..num_items {
/// vec.push(i);
/// }
///
/// assert_eq!(
/// expected_fragment_capacities,
/// vec.fragments()
/// .iter()
/// .map(|f| f.capacity())
/// .collect::<Vec<_>>()
/// );
/// assert_eq!(
/// expected_fragment_capacities,
/// vec.fragments().iter().map(|f| f.len()).collect::<Vec<_>>()
/// );
///
/// // create the 6-th fragment doubling the capacity
/// vec.push(42);
/// assert_eq!(
/// vec.fragments().len(),
/// expected_fragment_capacities.len() + 1
/// );
///
/// assert_eq!(vec.fragments().last().map(|f| f.capacity()), Some(32 * 2));
/// assert_eq!(vec.fragments().last().map(|f| f.len()), Some(1));
/// ```
pub fn with_doubling_growth(first_fragment_capacity: usize) -> Self {
assert!(first_fragment_capacity > 0);
SplitVec::with_doubling_growth(first_fragment_capacity).into()
}
}
impl<T> ImpVec<T, ExponentialGrowth> {
/// Creates an imp-vector which allows new fragments grow exponentially.
///
/// The capacity of the n-th fragment is computed as
/// `cap0 * pow(growth_coefficient, n)`
/// where `cap0` is the capacity of the first fragment.
///
/// Note that `DoublingGrowth` is a special case of `ExponentialGrowth`
/// with `growth_coefficient` equal to 2,
/// while providing a faster access by index.
///
/// On the other hand, exponential growth allows for fitting growth strategies
/// for fitting situations which could be a better choice when memory allocation
/// is more important than index access complexity.
///
/// As you may see in the example below, it is especially useful in providing
/// exponential growth rates slower than the doubling.
///
/// Assuming it is the common case compared to empty vector scenarios,
/// it immediately allocates the first fragment to keep the `SplitVec` struct smaller.
///
/// # Panics
/// Panics if `first_fragment_capacity` is zero,
/// or if `growth_coefficient` is less than 1.0.
///
/// # Examples
///
/// ```
/// use orx_imp_vec::ImpVec;
///
/// // SplitVec<usize, ExponentialGrowth>
/// let mut vec = ImpVec::with_exponential_growth(2, 1.5);
///
/// assert_eq!(1, vec.fragments().len());
/// assert_eq!(Some(2), vec.fragments().first().map(|f| f.capacity()));
/// assert_eq!(Some(0), vec.fragments().first().map(|f| f.len()));
///
/// // fill the first 5 fragments
/// let expected_fragment_capacities = vec![2, 3, 4, 6, 9, 13];
/// let num_items: usize = expected_fragment_capacities.iter().sum();
/// for i in 0..num_items {
/// vec.push(i);
/// }
///
/// assert_eq!(
/// expected_fragment_capacities,
/// vec.fragments()
/// .iter()
/// .map(|f| f.capacity())
/// .collect::<Vec<_>>()
/// );
/// assert_eq!(
/// expected_fragment_capacities,
/// vec.fragments().iter().map(|f| f.len()).collect::<Vec<_>>()
/// );
///
/// // create the 6-th fragment doubling the capacity
/// vec.push(42);
/// assert_eq!(
/// vec.fragments().len(),
/// expected_fragment_capacities.len() + 1
/// );
///
/// assert_eq!(vec.fragments().last().map(|f| f.capacity()), Some((13 as f32 * 1.5) as usize));
/// assert_eq!(vec.fragments().last().map(|f| f.len()), Some(1));
/// ```
pub fn with_exponential_growth(
first_fragment_capacity: usize,
growth_coefficient: f32,
) -> Self {
assert!(first_fragment_capacity > 0);
assert!(growth_coefficient >= 1.0);
SplitVec::with_exponential_growth(first_fragment_capacity, growth_coefficient).into()
}
}
impl<T> ImpVec<T, CustomGrowth<T>> {
/// Creates an imp vector with the custom grwoth strategy
/// defined by the function `get_capacity_of_new_fragment`.
///
/// # Examples
/// ```
/// use orx_split_vec::Fragment;
/// use orx_imp_vec::ImpVec;
/// use std::rc::Rc;
///
/// // vec: SplitVec<usize, CustomGrowth<usize>>
/// let vec =
/// ImpVec::with_custom_growth_function(Rc::new(|fragments: &[Fragment<_>]| {
/// if fragments.len() % 2 == 0 {
/// 2
/// } else {
/// 8
/// }
/// }));
///
/// for i in 0..100 {
/// vec.push(i);
/// }
///
/// vec.into_iter().zip(0..100).all(|(l, r)| *l == r);
///
/// for (f, fragment) in vec.fragments().iter().enumerate() {
/// if f % 2 == 0 {
/// assert_eq!(2, fragment.capacity());
/// } else {
/// assert_eq!(8, fragment.capacity());
/// }
/// }
/// ```
pub fn with_custom_growth_function(
get_capacity_of_new_fragment: Rc<GetCapacityOfNewFragment<T>>,
) -> Self {
SplitVec::with_custom_growth_function(get_capacity_of_new_fragment).into()
}
}
impl<T> ImpVec<T, FixedCapacity> {
/// Creates an imp-vector with the given `fixed_capacity`.
///
/// This capacity is the hard limit and the vector cannot grow beyond it.
/// Attempts to exceed this limit will lead to the code to panic.
///
/// The benefit of this strategy, on the other hand,
/// is its faster access by index operations;
/// which must be inlined and have a comparable performance
/// with regular slice access by index.
///
/// Further, the pinned-memory-location of already
/// pushed elements feature is maintained.
///
/// # Examples
///
/// ```
/// use orx_imp_vec::ImpVec;
///
/// // SplitVec<usize, FixedCapacity>
/// let vec = ImpVec::with_fixed_capacity(4);
///
/// assert_eq!(1, vec.fragments().len());
/// assert_eq!(Some(4), vec.fragments().first().map(|f| f.capacity()));
/// assert_eq!(Some(0), vec.fragments().first().map(|f| f.len()));
///
/// // push 4 elements to fill the vector completely
/// for i in 0..4 {
/// vec.push(i);
/// }
/// assert_eq!(1, vec.fragments().len());
///
/// // the next push exceeding the fixed_capacity will panic.
/// // vec.push(4);
/// ```
pub fn with_fixed_capacity(fixed_capacity: usize) -> Self {
SplitVec::with_fixed_capacity(fixed_capacity).into()
}
}