orx_linked_list/list/new.rs
1use crate::{
2 DoublyList, DoublyListLazy, DoublyListThreshold, SinglyList, SinglyListLazy,
3 SinglyListThreshold, list::List, variant::ListVariant,
4};
5use orx_fixed_vec::FixedVec;
6use orx_pinned_vec::PinnedVec;
7use orx_selfref_col::{MemoryPolicy, Node, Refs, SelfRefCol};
8use orx_split_vec::{Doubling, Linear, Recursive, SplitVec};
9
10// singly
11
12impl<T> SinglyList<T> {
13 /// Creates an empty singly linked list with default memory reclaim policy.
14 pub fn new() -> Self {
15 Self(SelfRefCol::new())
16 }
17
18 /// Creates an empty singly linked list with custom memory reclaim on threshold policy:
19 /// * memory of removed nodes are automatically reclaimed when the ratio of closed nodes to all nodes exceeds one over 2^D:
20 /// * when D = 0: memory will be reclaimed when utilization is below 0.00% (equivalent to Lazy).
21 /// * when D = 1: memory will be reclaimed when utilization is below 50.00%.
22 /// * when D = 2: memory will be reclaimed when utilization is below 75.00%.
23 /// * when D = 3: memory will be reclaimed when utilization is below 87.50%.
24 /// * when D = 4: memory will be reclaimed when utilization is below 93.75%.
25 pub fn with_threshold_reclaimer<const D: usize>() -> SinglyListThreshold<D, T> {
26 List(SelfRefCol::new())
27 }
28}
29impl<T> Default for SinglyList<T> {
30 fn default() -> Self {
31 Self::new()
32 }
33}
34
35impl<T> SinglyListLazy<T> {
36 /// Creates an empty singly linked list with lazy memory reclaim policy.
37 ///
38 /// Memory of removed nodes are never reclaimed implicitly, the caller can explicitly reclaim by calling `reclaim_closed_nodes`.
39 ///
40 /// This also guarantees that indices will never be invalidated implicitly.
41 pub fn new() -> Self {
42 Self(SelfRefCol::new())
43 }
44}
45impl<T> Default for SinglyListLazy<T> {
46 fn default() -> Self {
47 Self::new()
48 }
49}
50
51// doubly
52
53impl<T> DoublyList<T> {
54 /// Creates an empty doubly linked list with default memory reclaim policy.
55 pub fn new() -> Self {
56 Self(SelfRefCol::new())
57 }
58
59 /// Creates an empty doubly linked list with custom memory reclaim on threshold policy:
60 /// * memory of removed nodes are automatically reclaimed when the ratio of closed nodes to all nodes exceeds one over 2^D:
61 /// * when D = 0: memory will be reclaimed when utilization is below 0.00% (equivalent to Lazy).
62 /// * when D = 1: memory will be reclaimed when utilization is below 50.00%.
63 /// * when D = 2: memory will be reclaimed when utilization is below 75.00%.
64 /// * when D = 3: memory will be reclaimed when utilization is below 87.50%.
65 /// * when D = 4: memory will be reclaimed when utilization is below 93.75%.
66 pub fn with_threshold_reclaimer<const D: usize>() -> DoublyListThreshold<D, T> {
67 List(SelfRefCol::new())
68 }
69}
70impl<T> Default for DoublyList<T> {
71 fn default() -> Self {
72 Self::new()
73 }
74}
75
76impl<T> DoublyListLazy<T> {
77 /// Creates an empty doubly linked list with lazy memory reclaim policy.
78 ///
79 /// Memory of removed nodes are never reclaimed implicitly, the caller can explicitly reclaim by calling `reclaim_closed_nodes`.
80 ///
81 /// This also guarantees that indices will never be invalidated implicitly.
82 pub fn new() -> Self {
83 Self(SelfRefCol::new())
84 }
85}
86impl<T> Default for DoublyListLazy<T> {
87 fn default() -> Self {
88 Self::new()
89 }
90}
91
92// pinned-vec variants
93
94impl<V, M, P> List<V, M, P>
95where
96 V: ListVariant,
97 M: MemoryPolicy<V>,
98 P: PinnedVec<Node<V>>,
99{
100 fn from_empty_pinned_vec(nodes: P) -> Self {
101 assert!(nodes.is_empty());
102 let ends = V::Ends::empty();
103 let col = SelfRefCol::from((nodes, ends));
104 Self(col)
105 }
106}
107
108impl<V, M> List<V, M, FixedVec<Node<V>>>
109where
110 V: ListVariant,
111 M: MemoryPolicy<V>,
112{
113 /// Creates a linked list that uses a [`FixedVec<T>`]((https://docs.rs/orx-fixed-vec/latest/orx_fixed_vec/)) as the underlying storage.
114 pub fn with_fixed_capacity(fixed_capacity: usize) -> Self {
115 Self::from_empty_pinned_vec(FixedVec::new(fixed_capacity))
116 }
117}
118
119impl<V, M> List<V, M, SplitVec<Node<V>, Doubling>>
120where
121 V: ListVariant,
122 M: MemoryPolicy<V>,
123{
124 /// Creates a linked list that uses a [`SplitVec<T, Doubling>`](https://docs.rs/orx-split-vec/latest/orx_split_vec/struct.Doubling.html) as the underlying storage.
125 pub fn with_doubling_growth() -> Self {
126 Self::from_empty_pinned_vec(SplitVec::with_doubling_growth())
127 }
128}
129
130impl<V, M> List<V, M, SplitVec<Node<V>, Recursive>>
131where
132 V: ListVariant,
133 M: MemoryPolicy<V>,
134{
135 /// Creates a linked list that uses a [`SplitVec<T, Recursive>`](https://docs.rs/orx-split-vec/latest/orx_split_vec/struct.Recursive.html) as the underlying storage.
136 pub fn with_recursive_growth() -> Self {
137 Self::from_empty_pinned_vec(SplitVec::with_recursive_growth())
138 }
139}
140
141impl<V, M> List<V, M, SplitVec<Node<V>, Linear>>
142where
143 V: ListVariant,
144 M: MemoryPolicy<V>,
145{
146 /// Creates a linked list that uses a [`SplitVec<T, Linear>`](https://docs.rs/orx-split-vec/latest/orx_split_vec/struct.Linear.html) as the underlying storage.
147 ///
148 /// Each fragment will have a capacity of 2 ^ constant_fragment_capacity_exponent.
149 pub fn with_linear_growth(constant_fragment_capacity_exponent: usize) -> Self {
150 Self::from_empty_pinned_vec(SplitVec::with_linear_growth(
151 constant_fragment_capacity_exponent,
152 ))
153 }
154}