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
use super::DoublyLinkedList;
impl<T> DoublyLinkedList<T> {
/// Retrieves a mutable reference to the element at the specified position in the `DoublyLinkedList`.
///
/// # Parameters
///
/// - `pos`: The zero-based index of the element to retrieve.
///
/// # Returns
///
/// - `Some(&mut T)`: A mutable reference to the element at the specified position, if it exists.
/// - `None`: If the position is out of bounds or the list is empty.
///
/// # Panics
///
/// This method will panic if `pos > self.len()`. Ensure that the provided position is within bounds.
///
/// # Examples
///
/// ```
/// use villa01_data_structures::doubly_linked_list::DoublyLinkedList;
///
/// let mut list = DoublyLinkedList::new();
/// list.insert_at_end(10);
/// list.insert_at_end(20);
/// list.insert_at_end(30);
///
/// assert_eq!(list.get_mut(0), Some(&mut 10)); // Get the first element
/// assert_eq!(list.get_mut(1), Some(&mut 20)); // Get the second element
/// assert_eq!(list.get_mut(2), Some(&mut 30)); // Get the last element
/// ```
pub fn get_mut(&self, pos: usize) -> Option<&mut T> {
assert!(pos < self.len());
// Get first and last are more efficient
if pos == 0 {
return self.get_first_mut();
}
if pos == self.len() - 1 {
return self.get_last_mut();
}
if pos < (self.len() / 2) {
return self.get_from_start_mut(pos);
} else {
return self.get_from_end_mut(pos);
}
}
fn get_from_start_mut(&self, pos: usize) -> Option<&mut T> {
let mut cursor = self.head;
let mut counter = 0;
unsafe {
while let Some(node) = cursor {
if counter == pos {
return Some(&mut (*node).data);
}
cursor = (*node).next;
counter += 1;
}
}
unreachable!()
}
fn get_from_end_mut(&self, pos: usize) -> Option<&mut T> {
let mut cursor = self.tail;
let mut counter = self.len() - 1;
unsafe {
while let Some(node) = cursor {
if counter == pos {
return Some(&mut (*node).data);
}
cursor = (*node).prev;
counter -= 1;
}
}
unreachable!()
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_at_zero() {
let mut list: DoublyLinkedList<i32> = DoublyLinkedList::new();
let mut value = 69;
let _ = list.insert_at_beginning(value);
let first = list.get_mut(0).unwrap();
assert_eq!(&mut value, first);
}
#[test]
fn test_at_len() {
let mut list: DoublyLinkedList<i32> = DoublyLinkedList::new();
let mut value = 69;
list.insert_at_end(10);
list.insert_at_end(value);
let first = list.get_mut(1).unwrap();
assert_eq!(&mut value, first);
}
#[test]
#[should_panic]
fn test_at_non_zero_out_of_bounds() {
let list: DoublyLinkedList<i32> = DoublyLinkedList::new();
let _ = list.get_mut(20).unwrap();
}
#[test]
fn test_with_several_nodes_from_start() {
let mut list: DoublyLinkedList<usize> = DoublyLinkedList::new();
let list_length: usize = 10;
for i in 1..list_length + 1 {
list.insert_at_position(i, 0).unwrap();
}
let pos = list.len() / 2 - 1;
let mut value = 69;
list.insert_at_position(value, pos).unwrap();
let get_value = list.get_mut(pos).unwrap();
assert_eq!(&mut value, get_value);
}
#[test]
fn test_with_several_nodes_from_end() {
let mut list: DoublyLinkedList<usize> = DoublyLinkedList::new();
let list_length: usize = 10;
for i in 1..list_length + 1 {
list.insert_at_position(i, 0).unwrap();
}
let pos = list.len() / 2;
let mut value = 69;
list.insert_at_position(value, pos).unwrap();
let get_value = list.get_mut(pos).unwrap();
assert_eq!(&mut value, get_value);
}
}