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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#![no_std] // portant!
use core::fmt::Debug;
use core::mem::MaybeUninit;
mod iterators;
pub use iterators::*;
mod simple;
pub use simple::*;
mod map;
pub use map::*;
#[cfg(test)] extern crate std;
#[cfg(test)] use std::*;
#[cfg(test)] mod tests;
#[derive(Debug)]
struct Node<T: Debug> {
element: Option<T>, // will be None if this node is free // // not needed for this to be an option but i like it. it can also be default and then we can save the cost of overwriting the bytes but idk.
prev: Option<usize>, // none if this is the first item
next: Option<usize>, // none if this is the last item
}
#[derive(Debug)]
pub struct StackStructure<T: Debug, const N: usize> {
pub(crate) main_memory: [Node<T>; N],
head_and_tail: Option<(usize, usize)>, // None if list is empty// index into the main_memory
free_list: Option<usize>, // points to the first free node. None if list is full.
len: usize,
}
impl<T: Debug, const N: usize> StackStructure<T, N> {
pub fn new() -> Self {
Self {
main_memory: {
let mut m: [MaybeUninit<Node<T>>; N] = [const { MaybeUninit::uninit() }; N];
for (i, element) in (&mut m[..]).into_iter().enumerate() {
element.write(
Node{
element: None,
prev: if i == 0 { None } else { Some(i-1) },
next: if i == N-1 { None } else { Some(i+1) },
}
);
}
//unsafe { core::mem::transmute::<_, [Node<T>; N]>(m) } // https://github.com/rust-lang/rust/issues/62875
let done = unsafe { core::ptr::read((&m as *const [MaybeUninit<Node<T>>; N]).cast::<[Node<T>; N]>()) };
core::mem::forget(m);
done
},
head_and_tail: None,
free_list: Some(0),
len: 0,
}
}
fn __get_new_node_from_free_list(&mut self) -> Option<usize/*internal-array-index*/> { // None if full
match self.free_list {
None => {
return None; // full
}
Some(new_node_i) => {
self.free_list = self.main_memory[new_node_i].next;
if let Some(i) = self.main_memory[new_node_i].next {
self.main_memory[i].prev = None; // now this one becomes the first free node // this might not be needed but i like it
}
Some(new_node_i)
}
}
}
fn __insert_node_after_node(&mut self, new_node_i: usize, current_node_i: usize) {
self.main_memory[new_node_i].prev = Some(current_node_i);
match self.main_memory[current_node_i].next {
None => {
// tail
self.main_memory[new_node_i].next = None;
self.head_and_tail.as_mut().unwrap().1 = new_node_i;
}
Some(next_node_i) => {
// in the middle
self.main_memory[new_node_i].next = Some(next_node_i);
self.main_memory[next_node_i].prev = Some(new_node_i);
}
}
self.main_memory[current_node_i].next = Some(new_node_i);
}
// optimize to start from tail if len - insertion_index < len / 2
pub fn insert(&mut self, insertion_index: usize, element: T) -> Result<(), ()> { // err if list is full or if index is out of bounds
match self.__get_new_node_from_free_list() {
None => {
return Err(()); // full
}
Some(new_node_i) => {
self.main_memory[new_node_i].element = Some(element);
match self.head_and_tail {
None => {
if insertion_index != 0 {
return Err(()); // out of bounds
}
self.head_and_tail = Some((new_node_i, new_node_i));
self.main_memory[new_node_i].prev = None; // i think it will always be already None since we using first free index.
self.main_memory[new_node_i].next = None;
}
Some((head, _tail)) => {
if insertion_index == 0 {
self.main_memory[new_node_i].next = Some(head);
self.main_memory[new_node_i].prev = None;
self.main_memory[head].prev = Some(new_node_i);
self.head_and_tail.as_mut().unwrap().0 = new_node_i; // update the head since we are inserting at the begining
} else {
let mut current_node_i = head;
for _ in 0..insertion_index-1 {
match self.main_memory[current_node_i].next {
None => return Err(()), // out of bounds
Some(next_i) => {
current_node_i = next_i;
}
}
}
self.__insert_node_after_node(new_node_i, current_node_i);
}
}
}
}
}
self.len += 1;
Ok(())
}
pub fn push(&mut self, element: T) -> Result<(), ()> { // err if list is full
self.insert(self.len, element) // later i can optimize the insert method to start from the tail if the index is closer to len than it is to 0.
}
fn __delete_node(&mut self, node_to_delete_i: usize) -> T {
match self.main_memory[node_to_delete_i].prev {
Some(prev_i) => {
self.main_memory[prev_i].next = self.main_memory[node_to_delete_i].next;
}
None => {
// node-to-delete is the head so we need to set a new head
match self.main_memory[node_to_delete_i].next {
Some(next_i) => {
self.head_and_tail.as_mut().unwrap().0 = next_i;
}
None => {
self.head_and_tail = None;
}
}
}
}
match self.main_memory[node_to_delete_i].next {
Some(next_i) => {
self.main_memory[next_i].prev = self.main_memory[node_to_delete_i].prev;
}
None => {
match self.main_memory[node_to_delete_i].prev {
Some(prev_i) => {
self.head_and_tail.as_mut().unwrap().1 = prev_i;
}
None => {
self.head_and_tail = None;
}
}
}
}
self.main_memory[node_to_delete_i].next = self.free_list;
self.free_list = Some(node_to_delete_i);
self.len -= 1;
self.main_memory[node_to_delete_i].element.take().unwrap()
}
// optimize to start from tail if len - insertion_index < len / 2
pub fn delete(&mut self, deletion_index: usize) -> Result<T, ()> { // error if index out of bounds
match self.head_and_tail {
None => return Err(()), // nothing to delete
Some((head, _tail)) => {
let mut node_to_delete_i = head;
for _ in 0..deletion_index {
node_to_delete_i = match self.main_memory[node_to_delete_i].next {
None => return Err(()), // index out of bounds
Some(i) => i,
};
}
Ok(self.__delete_node(node_to_delete_i))
}
}
}
// optimize to start from tail if len - insertion_index < len / 2
pub fn get(&self, get_index: usize) -> Option<&T> { // none if index out of bounds
match self.head_and_tail {
None => return None, // nothing to get
Some((head, _tail)) => { // i can optimize this by starting from the tail if get_index > (len/2)
let mut node_to_get_i = head;
for _ in 0..get_index {
node_to_get_i = match self.main_memory[node_to_get_i].next {
None => return None, // index out of bounds
Some(i) => i,
};
}
Some(self.main_memory[node_to_get_i].element.as_ref().unwrap()) // unwrap is safe here because each element in the list is with a Some value
}
}
}
// optimize to start from tail if len - insertion_index < len / 2
pub fn get_mut(&mut self, get_index: usize) -> Option<&mut T> { // none if index out of bounds
match self.head_and_tail {
None => return None, // nothing to get
Some((head, _tail)) => { // i can optimize this by starting from the tail if get_index > (len/2)
let mut node_to_get_i = head;
for _ in 0..get_index {
node_to_get_i = match self.main_memory[node_to_get_i].next {
None => return None, // index out of bounds
Some(i) => i,
};
}
Some(self.main_memory[node_to_get_i].element.as_mut().unwrap()) // unwrap is safe here because each element in the list is with a Some value
}
}
}
// optimize to start from tail if len - insertion_index < len / 2
pub fn set(&mut self, set_index: usize, value: T) -> Result<T, ()> { // error if index out of bounds // returns old value
match self.head_and_tail {
None => return Err(()), // nothing to get
Some((head, _tail)) => { // i can optimize this by starting from the tail if get_index > (len/2)
let mut node_to_get_i = head;
for _ in 0..set_index {
node_to_get_i = match self.main_memory[node_to_get_i].next {
None => return Err(()), // index out of bounds
Some(i) => i,
};
}
Ok(self.main_memory[node_to_get_i].element.replace(value).unwrap()) // unwrap is safe here because each element in the list is with a Some value
}
}
}
pub fn len(&self) -> usize {
self.len
}
pub(crate) fn __binary_search_by_key<'a, K: Ord, F: Fn(&'a T)->K>(&'a self, key: K, key_of_the_element: F)
-> Result<(usize/*virtual-index*/, usize/*internal-array-index*/), (usize/*virtual-index*/, Option<usize>/*None means insert at virtual-index-~0, Some means the node that comes before a potential sorted insert*/)> // ok is the item is found at this location
{
if self.len == 0 {
return Err((0, None));
}
let mut low: usize = 0;
let mut high: usize = self.len - 1;
// for the ficiency gains so we don't have to traverse the whole list from the begining on each loop
// the index into the main-memory-array
let mut main_mem_ptr: usize = self.head_and_tail.as_ref().unwrap().0; // we already returned if self.len == 0
// the (virtual) index into the user-facing list.
let mut main_mem_ptr_index: usize = 0;
while low <= high {
let mid: usize = (low + high) / 2;
let placement_difference: isize = (mid as isize) - (main_mem_ptr_index as isize);
let travel: &mut dyn FnMut(&Node<T>)->Option<usize> = if placement_difference >= 0 {
&mut |node: &Node<T>| { main_mem_ptr_index += 1; node.next }
} else {
&mut |node: &Node<T>| { main_mem_ptr_index -= 1; node.prev }
};
for _ in 0..placement_difference.abs() {
main_mem_ptr = travel(&(self.main_memory[main_mem_ptr])).unwrap(); // we are not going out of bounds here. we use the self.len as the starting highd
}
//#[cfg(test)] println!("main_mem_ptr before compare {:?}", main_mem_ptr);
use core::cmp::Ordering;
match key_of_the_element(self.main_memory[main_mem_ptr].element.as_ref().unwrap()).cmp(&key) { // unwrap because traveling the list is with the lements.
Ordering::Equal => {
//#[cfg(test)] println!("equal {:?}", ());
return Ok((mid, main_mem_ptr));
}
Ordering::Less => {
//#[cfg(test)] println!("less {:?}", ());
low = mid + 1; // while loop condition makes sure we don't use it if it goes out of bounds.
}
Ordering::Greater => {
//#[cfg(test)] println!("greater {:?}", ());
high = match mid.checked_sub(1) {
Some(good) => good,
None => return Err((0, None)),
};
if high < low {
return Err((low, Some(self.main_memory[main_mem_ptr].prev.unwrap())));
}
}
}
}
return Err((low, Some(main_mem_ptr)));
}
// the sequence of the elements in the list must be sorted already before calling this method. otherwise the result is meaningless.
pub fn binary_search_by_key<'a, K: Ord, F: Fn(&'a T)->K>(&'a self, key: K, key_of_the_element: F) -> Result<usize, usize>
{
self.__binary_search_by_key(key, key_of_the_element)
.map( |(virtual_i, _)| virtual_i)
.map_err(|(virtual_i, _)| virtual_i)
}
pub fn binary_search(&self, key: &T) -> Result<usize, usize>
where T: Ord {
//#[inline_always]
fn same<T>(t: &T) -> &T { t }
self.binary_search_by_key(key, same)
}
}