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
use {crate::CircularList, alloc::boxed::Box, core::ptr::NonNull};
pub(crate) struct Node<T> {
next: NonNull<Node<T>>,
prev: NonNull<Node<T>>,
value: T,
}
impl<T> Node<T> {
/// Creates a new element with value `val`.
/// The created element is its own previous and next element.
/// # Layout
/// ```text
/// ┌───┐
/// │ │
/// │ ┌─▼──┐
/// └─┤val ├─┐
/// └──▲─┘ │
/// │ │
/// └───┘
/// ```
pub(crate) fn new(value: T) -> NonNull<Self> {
let boxed = Box::new(Node {
next: NonNull::dangling(),
prev: NonNull::dangling(),
value,
});
let mut ptr = NonNull::from(Box::leak(boxed));
unsafe {
ptr.as_mut().next = ptr;
ptr.as_mut().prev = ptr;
}
ptr
}
/// Returns the next node.
pub(crate) unsafe fn next(this: NonNull<Node<T>>) -> NonNull<Node<T>> {
unsafe { (*this.as_ptr()).next }
}
unsafe fn set_next(this: NonNull<Node<T>>, next: NonNull<Node<T>>) {
unsafe {
(*this.as_ptr()).next = next;
}
}
/// Returns the previous node.
pub(crate) unsafe fn prev(this: NonNull<Node<T>>) -> NonNull<Node<T>> {
unsafe { (*this.as_ptr()).prev }
}
unsafe fn set_prev(this: NonNull<Node<T>>, prev: NonNull<Node<T>>) {
unsafe {
(*this.as_ptr()).prev = prev;
}
}
/// Returns a shared reference to the value carried by `this` node.
///
/// # Safety
/// The caller must ensure no exclusive reference lives by the `'a` lifetime.
pub(crate) unsafe fn value<'a>(this: NonNull<Node<T>>) -> &'a T {
unsafe { &(*this.as_ptr()).value }
}
/// Returns an exclusive reference to the value carried by `this` node.
///
/// # Safety
/// The caller must ensure no other reference lives by the `'a` lifetime.
pub(crate) unsafe fn value_mut<'a>(this: NonNull<Node<T>>) -> &'a mut T {
unsafe { &mut (*this.as_ptr()).value }
}
/// Returns the node next to `this`.
/// Returns `None` if `this` is its own next node.
pub(crate) unsafe fn next_distinct(this: NonNull<Node<T>>) -> Option<NonNull<Node<T>>> {
unsafe {
let next = (*this.as_ptr()).next;
if next != this { Some(next) } else { None }
}
}
/// Inserts a new node with value `val` between `this` and its previous node.
pub(crate) unsafe fn insert_prev(this: NonNull<Node<T>>, val: T) {
let new = Self::new(val);
unsafe {
let prev = Self::prev(this);
Self::set_next(prev, new);
Self::set_prev(new, prev);
Self::set_next(new, this);
Self::set_prev(this, new);
}
}
/// Isolate `this` from its connected nodes (if any).
/// Do nothing if `this` is only connected to itsef (as it is when constructed
/// by [`Self::new`]).
pub(crate) unsafe fn disconnect(this: NonNull<Node<T>>) {
unsafe {
let prev = Self::prev(this);
let next = Self::next(this);
if prev != next {
// 3 or more elements
Self::set_next(prev, next);
Self::set_prev(next, prev);
} else if this != prev {
// 2 elements
let next = Self::next(this);
Self::set_next(next, next);
Self::set_prev(next, next);
};
/* The case with 1 element is a no-op */
}
}
/// Sets the next node of `this` to `next` and the previous node of
/// `next` to `this`.
///
/// # Safety
/// When using this function, the caller must be careful and make sure
/// every node is in a group arranged in a circular fashion.
pub(crate) unsafe fn connect(this: NonNull<Node<T>>, next: NonNull<Node<T>>) {
unsafe {
Self::set_next(this, next);
Self::set_prev(next, this);
}
}
/// Disconnects the node, frees its memory and moves the carried value
/// by returning it.
pub(crate) unsafe fn remove(this: NonNull<Node<T>>) -> T {
unsafe {
Self::disconnect(this);
let this = Box::from_raw(this.as_ptr());
this.value
}
}
pub(super) unsafe fn half(list: &CircularList<T>) -> Option<(NonNull<Self>, usize)> {
let head = list.head?;
let mut mid_idx = 0;
unsafe {
let mut slow = head;
let mut fast = Self::next(head);
while fast != head && Self::next(fast) != head {
fast = Self::next(Self::next(fast));
if fast != head {
slow = Self::next(slow);
mid_idx += 1;
}
}
Some((Self::next(slow), (mid_idx + 1) % list.len))
}
}
pub(super) unsafe fn split(head: NonNull<Node<T>>, mid: NonNull<Node<T>>) {
unsafe {
// Assume mid is not head
let old_last = Node::prev(head);
let new_last = Node::prev(mid);
Self::set_prev(head, new_last);
Self::set_prev(mid, old_last);
Self::set_next(old_last, mid);
Self::set_next(new_last, head);
}
}
pub(super) unsafe fn merge(
head_a: NonNull<Node<T>>,
head_b: NonNull<Node<T>>,
) -> NonNull<Node<T>>
where
T: PartialOrd,
{
unsafe {
let lt = |a: NonNull<Node<T>>, b: NonNull<Node<T>>| Self::value(a) < Self::value(b);
let tail_a = Self::prev(head_a);
let tail_b = Self::prev(head_b);
let head = if lt(head_a, head_b) { head_a } else { head_b };
let tail = if lt(tail_a, tail_b) { tail_b } else { tail_a };
let mut next_a = if head == head_a {
Self::next(head_a)
} else {
head_a
};
let mut next_b = if head == head_b {
Self::next(head_b)
} else {
head_b
};
let mut current = head;
loop {
if next_a == tail_a && next_b == tail_b {
if next_a == tail {
Self::connect(current, next_b);
Self::connect(next_b, tail);
} else {
Self::connect(current, next_a);
Self::connect(next_a, tail);
}
break;
}
if lt(next_a, next_b) {
Self::connect(current, next_a);
if next_a == tail_a {
Self::connect(next_a, next_b);
break;
}
next_a = Self::next(next_a);
} else {
Self::connect(current, next_b);
if next_b == tail_b {
Self::connect(next_b, next_a);
break;
}
next_b = Self::next(next_b);
}
current = Self::next(current);
}
Node::connect(tail, head);
head
}
}
}