orx_concurrent_vec/
new.rs

1use crate::{elem::ConcurrentElement, vec::ConcurrentVec};
2use orx_fixed_vec::FixedVec;
3use orx_pinned_vec::IntoConcurrentPinnedVec;
4use orx_split_vec::{Doubling, Linear, SplitVec};
5
6impl<T> Default for ConcurrentVec<T, SplitVec<ConcurrentElement<T>, Doubling>> {
7    /// Creates a new concurrent bag 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.
8    fn default() -> Self {
9        Self::with_doubling_growth()
10    }
11}
12
13impl<T> ConcurrentVec<T, SplitVec<ConcurrentElement<T>, Doubling>> {
14    /// Creates a new concurrent bag 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.
15    pub fn new() -> Self {
16        Self::with_doubling_growth()
17    }
18
19    /// Creates a new concurrent bag 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.
20    pub fn with_doubling_growth() -> Self {
21        Self::new_from_pinned(SplitVec::with_doubling_growth_and_max_concurrent_capacity())
22    }
23}
24
25impl<T> ConcurrentVec<T, SplitVec<ConcurrentElement<T>, Linear>> {
26    /// Creates a new concurrent bag 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.
27    ///
28    /// * Each fragment of the split vector will have a capacity of  `2 ^ constant_fragment_capacity_exponent`.
29    /// * Further, fragments collection of the split vector will have a capacity of `fragments_capacity` on initialization.
30    ///
31    /// This leads to a [`orx_pinned_concurrent_col::PinnedConcurrentCol::maximum_capacity`] of `fragments_capacity * 2 ^ constant_fragment_capacity_exponent`.
32    ///
33    /// Whenever this capacity is not sufficient, fragments capacity can be increased by using the  [`orx_pinned_concurrent_col::PinnedConcurrentCol::reserve_maximum_capacity`] method.
34    pub fn with_linear_growth(
35        constant_fragment_capacity_exponent: usize,
36        fragments_capacity: usize,
37    ) -> Self {
38        Self::new_from_pinned(SplitVec::with_linear_growth_and_fragments_capacity(
39            constant_fragment_capacity_exponent,
40            fragments_capacity,
41        ))
42    }
43}
44
45impl<T> ConcurrentVec<T, FixedVec<ConcurrentElement<T>>> {
46    /// Creates a new concurrent bag by creating and wrapping up a new [`FixedVec<T>`](https://docs.rs/orx-fixed-vec/latest/orx_fixed_vec/) as the underlying storage.
47    ///
48    /// # Safety
49    ///
50    /// Note that a `FixedVec` cannot grow; i.e., it has a hard upper bound on the number of elements it can hold, which is the `fixed_capacity`.
51    ///
52    /// Pushing to the vector beyond this capacity leads to "out-of-capacity" error.
53    ///
54    /// This maximum capacity can be accessed by [`orx_pinned_concurrent_col::PinnedConcurrentCol::capacity`] or [`orx_pinned_concurrent_col::PinnedConcurrentCol::maximum_capacity`] methods.
55    pub fn with_fixed_capacity(fixed_capacity: usize) -> Self {
56        Self::new_from_pinned(FixedVec::new(fixed_capacity))
57    }
58}
59
60// from
61impl<T, P> From<P> for ConcurrentVec<T, P>
62where
63    P: IntoConcurrentPinnedVec<ConcurrentElement<T>>,
64{
65    /// `ConcurrentVec<T>` uses any `PinnedVec<T>` implementation as the underlying storage.
66    ///
67    /// Therefore, without a cost
68    /// * `ConcurrentVec<T>` can be constructed from any `PinnedVec<T>`, and
69    /// * the underlying `PinnedVec<T>` can be obtained by `ConcurrentVec::into_inner(self)` method.
70    fn from(pinned_vec: P) -> Self {
71        Self::new_from_pinned(pinned_vec)
72    }
73}