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
#![allow(unsafe_code)]
use std::ptr;
pub type Item = u64;
#[derive(Debug)]
pub(crate) struct Node {
inner: Item,
next: *mut Node,
prev: *mut Node,
}
impl Node {
fn unwire(&mut self) {
unsafe {
if !self.prev.is_null() {
(*self.prev).next = self.next;
}
if !self.next.is_null() {
(*self.next).prev = self.prev;
}
}
self.next = ptr::null_mut();
self.prev = ptr::null_mut();
}
}
pub struct DoublyLinkedList {
head: *mut Node,
tail: *mut Node,
len: usize,
}
impl Drop for DoublyLinkedList {
fn drop(&mut self) {
let mut cursor = self.head;
while !cursor.is_null() {
unsafe {
let node = Box::from_raw(cursor);
cursor = node.prev;
drop(node);
}
}
}
}
impl Default for DoublyLinkedList {
fn default() -> Self {
Self { head: ptr::null_mut(), tail: ptr::null_mut(), len: 0 }
}
}
impl DoublyLinkedList {
pub(crate) const fn len(&self) -> usize {
self.len
}
pub(crate) fn push_head(&mut self, item: Item) -> *mut Node {
self.len += 1;
let node = Node { inner: item, next: ptr::null_mut(), prev: self.head };
let ptr = Box::into_raw(Box::new(node));
if !self.head.is_null() {
unsafe {
(*self.head).next = ptr;
}
}
if self.tail.is_null() {
self.tail = ptr;
}
self.head = ptr;
ptr
}
#[cfg(test)]
pub(crate) fn push_tail(&mut self, item: Item) {
self.len += 1;
let node = Node { inner: item, next: self.tail, prev: ptr::null_mut() };
let ptr = Box::into_raw(Box::new(node));
if !self.tail.is_null() {
unsafe {
(*self.tail).prev = ptr;
}
}
if self.head.is_null() {
self.head = ptr;
}
self.tail = ptr;
}
pub(crate) fn promote(&mut self, ptr: *mut Node) -> *mut Node {
if self.head == ptr {
return ptr;
}
unsafe {
let pid = self.pop_ptr(ptr);
self.push_head(pid)
}
}
#[cfg(test)]
pub(crate) fn pop_head(&mut self) -> Option<Item> {
if self.head.is_null() {
return None;
}
self.len -= 1;
unsafe {
let mut head = Box::from_raw(self.head);
if self.head == self.tail {
self.tail = ptr::null_mut();
}
self.head = head.prev;
head.unwire();
Some(head.inner)
}
}
pub(crate) fn pop_tail(&mut self) -> Option<Item> {
if self.tail.is_null() {
return None;
}
self.len -= 1;
unsafe {
let mut tail = Box::from_raw(self.tail);
if self.head == self.tail {
self.head = ptr::null_mut();
}
self.tail = tail.next;
tail.unwire();
Some(tail.inner)
}
}
pub(crate) unsafe fn pop_ptr(&mut self, ptr: *mut Node) -> Item {
self.len -= 1;
let mut node = Box::from_raw(ptr);
if self.tail == ptr {
self.tail = node.next;
}
if self.head == ptr {
self.head = node.prev;
}
node.unwire();
node.inner
}
#[cfg(test)]
pub(crate) fn into_vec(mut self) -> Vec<Item> {
let mut res = vec![];
while let Some(val) = self.pop_head() {
res.push(val);
}
res
}
}
#[allow(unused_results)]
#[test]
fn test_dll() {
let mut dll = DoublyLinkedList::default();
dll.push_head(5);
dll.push_tail(6);
dll.push_head(4);
dll.push_tail(7);
dll.push_tail(8);
dll.push_head(3);
dll.push_tail(9);
dll.push_head(2);
dll.push_head(1);
assert_eq!(dll.len(), 9);
assert_eq!(dll.into_vec(), vec![1, 2, 3, 4, 5, 6, 7, 8, 9]);
}