-
Notifications
You must be signed in to change notification settings - Fork 5
/
queue.rs
141 lines (113 loc) · 3.18 KB
/
queue.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
/// Serves a collection of elements keeping the order of arrival.
/// Opposite to Stacks, a Queue is a FIFO datastructutre, which
/// means that the first element inserted will be the first element
/// to go out of the Queue.
///
/// This implementation of a Queue datastructure is naive and doesn't
/// achieves good performance. For real life situations a `VecDeque`
/// from `std::collections` is a better choice.
#[allow(dead_code)]
pub struct Queue<T>
where
T: Clone,
{
coll: Vec<T>,
}
impl<T> Queue<T>
where
T: Clone,
{
/// Creates a new instance of an empty Queue
pub fn new() -> Self {
Queue { coll: Vec::new() }
}
/// Appends an element to the Queue
pub fn enqueue(&mut self, item: T) {
self.coll.push(item);
}
pub fn enqueue_many(&mut self, coll: &Vec<T>) {
let coll = coll.to_owned();
for item in coll.into_iter() {
self.enqueue(item);
}
}
/// Takes the next element from the Queue
pub fn dequeue(&mut self) -> T {
self.coll.remove(0)
}
/// Retrieves the number of elements conforming the Queue
pub fn length(&self) -> usize {
self.coll.len()
}
/// Peeks the next element from the Queue.
/// This is similar to calling `dequeue` but
/// without moving the value from the Queue
pub fn peek(&self) -> Option<&T> {
self.coll.get(0)
}
/// Returns `true` if the Queue is empty otherwise
/// returns `false`
pub fn is_empty(&self) -> bool {
self.coll.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn creates_a_queue() {
let empty_vec: Vec<i32> = Vec::new();
let queue: Queue<i32> = Queue::new();
assert_eq!(queue.coll, empty_vec);
}
#[test]
fn enqueue_an_element() {
let mut queue: Queue<i32> = Queue::new();
queue.enqueue(9);
assert_eq!(queue.coll, vec![9]);
}
#[test]
fn dequeues_an_element() {
let mut queue: Queue<i32> = Queue::new();
queue.enqueue(9);
queue.enqueue(10);
queue.enqueue(11);
queue.enqueue(12);
assert_eq!(queue.dequeue(), 9);
assert_eq!(queue.dequeue(), 10);
assert_eq!(queue.dequeue(), 11);
assert_eq!(queue.dequeue(), 12);
}
#[test]
fn retrieves_queue_element_count() {
let mut queue: Queue<i32> = Queue::new();
queue.enqueue(9);
queue.enqueue(10);
queue.enqueue(11);
queue.enqueue(12);
assert_eq!(queue.length(), 4_usize);
}
#[test]
fn peeks_the_next_element_from_the_queue() {
let mut queue: Queue<i32> = Queue::new();
queue.enqueue(9);
queue.enqueue(10);
queue.enqueue(11);
queue.enqueue(12);
assert_eq!(queue.peek(), Some(&9));
}
#[test]
fn checks_if_queue_is_empty() {
let queue: Queue<i32> = Queue::new();
assert!(queue.is_empty());
}
#[test]
fn checks_if_queue_is_not_empty() {
let mut queue: Queue<i32> = Queue::new();
queue.enqueue(9);
queue.enqueue(10);
queue.enqueue(11);
queue.enqueue(12);
assert!(!queue.is_empty());
}
}