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
use crate::{linked_list::LinkedList, mem::MemoryUtilization, node::LinkedListNode};
use orx_imp_vec::{
    prelude::{CustomGrowth, DoublingGrowth, ExponentialGrowth, Fragment, LinearGrowth, SplitVec},
    ImpVec,
};
use std::rc::Rc;

impl<'a, T> LinkedList<'a, T, SplitVec<LinkedListNode<'a, T>, DoublingGrowth>> {
    /// Creates an empty LinkedList where new allocations are doubled every time
    /// the vector reaches its capacity.
    ///
    /// * `first_fragment_capacity` determines the capacity of the first fragment
    /// of the underlying split vector.
    /// * `memory_utilization` defines the memory utilization strategy of the linked
    /// list, see `MemoryUtilization` for details.
    ///
    /// *The `ImpVec` of the linked list allowing to build internal links
    /// uses a `SplitVec: PinnedVec` with `DoublingGrowth` growth strategy.
    /// See [`SplitVec`](https://crates.io/crates/orx-split-vec) for details.*
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_linked_list::prelude::*;
    ///
    /// let mut list = LinkedList::with_doubling_growth(4, MemoryUtilization::Lazy);
    ///
    /// for i in 0..8 {
    ///     list.push_back(i);
    /// }
    /// assert_eq!(8, list.len());
    /// ```
    pub fn with_doubling_growth(
        first_fragment_capacity: usize,
        memory_utilization: MemoryUtilization,
    ) -> Self {
        memory_utilization.validate();
        let imp: ImpVec<_, _> = SplitVec::with_doubling_growth(first_fragment_capacity).into();
        imp.push(LinkedListNode::back_front_node());
        Self {
            imp,
            memory_utilization,
            len: 0,
        }
    }
}
impl<'a, T> LinkedList<'a, T, SplitVec<LinkedListNode<'a, T>, LinearGrowth>> {
    /// Creates an empty LinkedList where new allocations are always the same
    /// and equal to the initial capacity of the vector.
    ///
    /// * `constant_fragment_capacity` determines the capacity of the first fragment
    /// and every succeeding fragment of the underlying split vector.
    /// * `memory_utilization` defines the memory utilization strategy of the linked
    /// list, see `MemoryUtilization` for details.
    ///
    /// *The `ImpVec` of the linked list allowing to build internal links
    /// uses a `SplitVec: PinnedVec` with `LinearGrowth` growth strategy.
    /// See [`SplitVec`](https://crates.io/crates/orx-split-vec) for details.*
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_linked_list::prelude::*;
    ///
    /// let mut list = LinkedList::with_linear_growth(5, MemoryUtilization::Eager);
    ///
    /// for i in 0..8 {
    ///     list.push_back(i);
    /// }
    /// assert_eq!(8, list.len());
    /// ```
    pub fn with_linear_growth(
        constant_fragment_capacity: usize,
        memory_utilization: MemoryUtilization,
    ) -> Self {
        memory_utilization.validate();
        let imp: ImpVec<_, _> = SplitVec::with_linear_growth(constant_fragment_capacity).into();
        imp.push(LinkedListNode::back_front_node());
        Self {
            imp,
            memory_utilization,
            len: 0,
        }
    }
}
impl<'a, T> LinkedList<'a, T, SplitVec<LinkedListNode<'a, T>, ExponentialGrowth>> {
    /// Creates an empty LinkedList where new allocations are exponentially increased
    /// every time the vector reaches its capacity.
    ///
    /// * `first_fragment_capacity` determines the capacity of the first fragment
    /// of the underlying split vector.
    /// * `growth_coefficient` determines the exponential growth rate of the succeeding
    /// fragments of the split vector.
    /// * `memory_utilization` defines the memory utilization strategy of the linked
    /// list, see `MemoryUtilization` for details.
    ///
    /// *The `ImpVec` of the linked list allowing to build internal links
    /// uses a `SplitVec: PinnedVec` with `ExponentialGrowth` growth strategy.
    /// See [`SplitVec`](https://crates.io/crates/orx-split-vec) for details.*
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_linked_list::prelude::*;
    ///
    /// let mut list = LinkedList::with_exponential_growth(4, 1.5, MemoryUtilization::default());
    ///
    /// for i in 0..8 {
    ///     list.push_back(i);
    /// }
    /// assert_eq!(8, list.len());
    /// ```
    pub fn with_exponential_growth(
        first_fragment_capacity: usize,
        growth_coefficient: f32,
        memory_utilization: MemoryUtilization,
    ) -> Self {
        memory_utilization.validate();
        let imp: ImpVec<_, _> =
            SplitVec::with_exponential_growth(first_fragment_capacity, growth_coefficient).into();
        imp.push(LinkedListNode::back_front_node());
        Self {
            imp,
            memory_utilization,
            len: 0,
        }
    }
}

pub(crate) type GetCapacityOfNewFragment<T> = dyn Fn(&[Fragment<T>]) -> usize;
impl<'a, T>
    LinkedList<'a, T, SplitVec<LinkedListNode<'a, T>, CustomGrowth<LinkedListNode<'a, T>>>>
{
    /// Creates an empty LinkedList where new allocations are determined explicitly
    /// by the passed in function.
    ///
    /// * `get_capacity_of_new_fragment` determines the capacity of succeeding
    /// fragments as a function of already created and filled fragments.
    /// * `memory_utilization` defines the memory utilization strategy of the linked
    /// list, see `MemoryUtilization` for details.
    ///
    /// *The `ImpVec` of the linked list allowing to build internal links
    /// uses a `SplitVec: PinnedVec` with `CustomGrowth` growth strategy.
    /// See [`SplitVec`](https://crates.io/crates/orx-split-vec) for details.*
    ///
    /// # Examples
    ///
    /// ```
    /// use orx_linked_list::prelude::*;
    /// use std::rc::Rc;
    ///
    /// let mut list =
    ///     LinkedList::with_custom_growth_function(Rc::new(|fragments: &[Fragment<_>]| {
    ///         if fragments.len() % 2 == 0 {
    ///             2
    ///         } else {
    ///             8
    ///         }
    ///     }), MemoryUtilization::WithThreshold(0.25));
    ///
    /// for i in 0..8 {
    ///     list.push_back(i);
    /// }
    /// assert_eq!(8, list.len());
    /// ```
    pub fn with_custom_growth_function(
        get_capacity_of_new_fragment: Rc<GetCapacityOfNewFragment<LinkedListNode<'a, T>>>,
        memory_utilization: MemoryUtilization,
    ) -> Self {
        memory_utilization.validate();
        let imp: ImpVec<_, _> =
            SplitVec::with_custom_growth_function(get_capacity_of_new_fragment).into();
        imp.push(LinkedListNode::back_front_node());
        Self {
            imp,
            memory_utilization,
            len: 0,
        }
    }
}