-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtqueue.rs
263 lines (232 loc) · 7.44 KB
/
rtqueue.rs
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
//! Real-time `O(1)` fully persistent FIFO queue implementation.
//!
//! **Note**: If you just want a queue data structure,
//! [`VecDeque`](std::collections::VecDeque) from [`std`] is the way to go. The
//! key here is providing *persistence*, which is explained below, and is rarely
//! if ever needed.
//!
//! 'Fully persistent' means that each operation on a queue creates a new queue
//! and does not invalidate the original one.
//!
//! For example:
//!
//! ```
//! use rust_ds_demo::rtqueue::Queue;
//! use std::iter::FromIterator;
//!
//! // A new queue with only one element 3:
//! let a: Queue<i32> = Queue::new().push_back(3);
//!
//! assert_eq!(Vec::from_iter(&a), vec![ 3 ]);
//!
//! // Use `a` twice here:
//! let b1 = a.push_back(4);
//! let b2 = a.push_back(5);
//!
//! // `a` is unchanged:
//! assert_eq!(Vec::from_iter(&a), vec![ 3 ]);
//!
//! // `b1` and `b2` have 4 and 5 pushed into it respectively:
//! assert_eq!(Vec::from_iter(&b1), vec![ 3, 4 ]);
//! assert_eq!(Vec::from_iter(&b2), vec![ 3, 5 ]);
//! ```
//!
//! Real-time `O(1)` means that each operation: [`new`](rtqueue::Queue::new),
//! [`push_back`](rtqueue::Queue::push_back),
//! [`pop_front`](rtqueue::Queue::pop_front) and [`clone`](Clone) on a
//! [`Queue<T>`](rtqueue::Queue) clones values of type `T` a number of times
//! bounded by a constant, and takes a further, bounded by a constant time doing
//! other organizational work. In other words, the maximum time taken by each
//! operation stays the same no matter how many items are contained in it.
//!
//! # Note on the `Clone` trait bound
//!
//! The data structure needs to clone items as part of its normal operation. If
//! cloning is not possible or too expensive, consider using
//! [`Rc<T>`](std::rc::Rc) as the item type.
//!
//! # References
//!
//! - Chris Okasaki, *Purely Functional Data Structures*
//! - Edsko de Vries, [*Efficient Amortised and Real-Time Queues in
//! Haskell*](https://www.well-typed.com/blog/2016/01/efficient-queues/)
//! - [*Queue*, subsection *Real-time queue* on
//! Wikipedia](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)#Real-time_queue)
use std::cell::{Cell, RefCell};
use std::fmt;
use std::iter::FromIterator;
use std::rc::Rc;
#[derive(Clone)]
enum Zipper<T> {
Clean,
Dirty(Rc<Node<T>>, Option<Rc<Node<T>>>),
}
struct Node<T> {
value: T,
next: RefCell<Option<Rc<Node<T>>>>,
zipper: Cell<Zipper<T>>,
}
impl<T: Clone> Node<T> {
fn create_lazy(x: &Option<Rc<Node<T>>>, y: Option<Rc<Node<T>>>) -> Option<Rc<Node<T>>> {
if let Some(x) = x {
let y = y.expect("create_lazy: imbalance");
Some(Rc::new(Node {
value: x.value.clone(),
next: x.next.clone(),
zipper: Cell::new(Zipper::Dirty(y, None)),
}))
} else {
y
}
}
fn rotate_zipper(&self) {
if let Zipper::Dirty(c, d) = self.zipper.replace(Zipper::Clean) {
let node = Rc::new(Node {
value: c.value.clone(),
next: RefCell::new(d),
zipper: Cell::new(Zipper::Clean),
});
let c_next = c.next.borrow().clone()
.expect("rotate_zipper: imbalance");
let new_next =
if let Some(next) = self.next.borrow().clone() {
Node {
value: next.value.clone(),
next: next.next.clone(),
zipper: Cell::new(
Zipper::Dirty(c_next, Some(node))),
}
} else {
Node {
value: c_next.value.clone(),
next: RefCell::new(Some(node)),
zipper: Cell::new(Zipper::Clean)
}
};
self.next.replace(Some(Rc::new(new_next)));
}
}
}
/// A real-time `O(1)` fully persistent FIFO queue.
///
/// See [module level documentation](crate::rtqueue) for usage details.
#[derive(Clone)]
pub struct Queue<T> {
front: Option<Rc<Node<T>>>,
back: Option<Rc<Node<T>>>,
jump: Option<Rc<Node<T>>>,
}
impl<T: Clone> Default for Queue<T> {
fn default() -> Queue<T> {
Queue {
front: None,
back: None,
jump: None,
}
}
}
impl<T: Clone> Queue<T> {
/// Create a new, empty queue.
pub fn new() -> Queue<T> {
Default::default()
}
/// Pop an item from the front of the queue.
///
/// Returns `None` if the queue is empty, and `Some` with new queue and
/// popped element otherwise.
pub fn pop_front(&self) -> Option<(Queue<T>, T)> {
let front = self.front.as_ref()?;
let res = front.value.clone();
let res_queue =
if let Some(jump) = &self.jump {
jump.rotate_zipper();
Queue {
front: front.next.borrow().clone(),
back: self.back.clone(),
jump: jump.next.borrow().clone(),
}
} else {
let zipper = Node::create_lazy(&front.next.borrow(), self.back.clone());
Queue {
front: zipper.clone(),
back: None,
jump: zipper,
}
};
Some((res_queue, res))
}
/// Push an item into the back of the queue.
pub fn push_back(&self, v: T) -> Queue<T> {
let new_node = Rc::new(Node {
value: v,
next: RefCell::new(self.back.clone()),
zipper: Cell::new(Zipper::Clean),
});
if let Some(jump) = &self.jump {
jump.rotate_zipper();
Queue {
front: self.front.clone(),
back: Some(new_node),
jump: jump.next.borrow().clone(),
}
} else {
let zipper = Node::create_lazy(&self.front, Some(new_node));
Queue {
front: zipper.clone(),
back: None,
jump: zipper,
}
}
}
}
pub struct QueueIter<T> {
dat: Queue<T>
}
impl<T: Clone> Iterator for QueueIter<T> {
type Item = T;
fn next(&mut self) -> Option<T> {
let (next, res) = self.dat.pop_front()?;
self.dat = next;
Some(res)
}
}
impl<T: Clone> IntoIterator for &'_ Queue<T> {
type Item = T;
type IntoIter = QueueIter<T>;
fn into_iter(self) -> QueueIter<T> {
QueueIter { dat: self.clone() }
}
}
impl<T: Clone> FromIterator<T> for Queue<T> {
fn from_iter<I: IntoIterator<Item=T>>(x: I) -> Queue<T> {
x.into_iter().fold(Queue::new(), |q, v| Queue::push_back(&q, v))
}
}
impl<T: fmt::Debug + Clone> fmt::Debug for Queue<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self).finish()
}
}
#[test]
fn test_queue() {
let a = Queue::new()
.push_back(1)
.push_back(2);
let b = a
.push_back(3)
.push_back(4);
let c = a
.push_back(5)
.push_back(6);
let b1 = b
.pop_front().unwrap().0
.pop_front().unwrap().0;
let c1 = c
.pop_front().unwrap().0
.pop_front().unwrap().0;
assert!(Vec::from_iter(&a) == vec![ 1, 2 ]);
assert!(Vec::from_iter(&b) == vec![ 1, 2, 3, 4 ]);
assert!(Vec::from_iter(&c) == vec![ 1, 2, 5, 6 ]);
assert!(Vec::from_iter(&b1) == vec![ 3, 4 ]);
assert!(Vec::from_iter(&c1) == vec![ 5, 6 ]);
}