Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 1 | // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT |
Graydon Hoare | 00c856c | 2012-12-04 00:48:01 | [diff] [blame] | 2 | // file at the top-level directory of this distribution and at |
| 3 | // http://rust-lang.org/COPYRIGHT. |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | // option. This file may not be copied, modified, or distributed |
| 9 | // except according to those terms. |
| 10 | |
Daniel Micay | b206017 | 2013-03-21 23:14:02 | [diff] [blame] | 11 | //! A double-ended queue implemented as a circular buffer |
Patrick Walton | f3723cf | 2013-05-17 22:28:44 | [diff] [blame] | 12 | use core::prelude::*; |
| 13 | |
Patrick Walton | 206ab89 | 2013-05-25 02:35:29 | [diff] [blame] | 14 | use core::uint; |
Alex Crichton | 998fece | 2013-05-06 04:42:54 | [diff] [blame] | 15 | use core::util::replace; |
Patrick Walton | 206ab89 | 2013-05-25 02:35:29 | [diff] [blame] | 16 | use core::vec; |
Jed Estep | 35314c9 | 2013-06-26 15:38:29 | [diff] [blame] | 17 | use core::cast::transmute; |
Alex Crichton | 998fece | 2013-05-06 04:42:54 | [diff] [blame] | 18 | |
Patrick Walton | 85c9fc6 | 2013-03-22 21:00:15 | [diff] [blame] | 19 | static initial_capacity: uint = 32u; // 2^5 |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 20 | |
Alex Crichton | 3956850 | 2013-05-29 03:11:41 | [diff] [blame] | 21 | #[allow(missing_doc)] |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 22 | pub struct Deque<T> { |
| 23 | priv nelts: uint, |
| 24 | priv lo: uint, |
| 25 | priv hi: uint, |
| 26 | priv elts: ~[Option<T>] |
Marijn Haverbeke | 26610db | 2012-01-11 11:49:33 | [diff] [blame] | 27 | } |
Roy Frostig | 9c81889 | 2010-07-21 01:03:09 | [diff] [blame] | 28 | |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 29 | impl<T> Container for Deque<T> { |
Daniel Micay | b3ee49c | 2013-03-15 17:41:02 | [diff] [blame] | 30 | /// Return the number of elements in the deque |
Alex Crichton | b29c368 | 2013-06-24 03:44:11 | [diff] [blame^] | 31 | fn len(&self) -> uint { self.nelts } |
Daniel Micay | b3ee49c | 2013-03-15 17:41:02 | [diff] [blame] | 32 | |
| 33 | /// Return true if the deque contains no elements |
Alex Crichton | b29c368 | 2013-06-24 03:44:11 | [diff] [blame^] | 34 | fn is_empty(&self) -> bool { self.len() == 0 } |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 35 | } |
Roy Frostig | 9c81889 | 2010-07-21 01:03:09 | [diff] [blame] | 36 | |
Daniel Micay | ed7c9c4 | 2013-02-16 23:55:25 | [diff] [blame] | 37 | impl<T> Mutable for Deque<T> { |
Daniel Micay | b3ee49c | 2013-03-15 17:41:02 | [diff] [blame] | 38 | /// Clear the deque, removing all values. |
Daniel Micay | ed7c9c4 | 2013-02-16 23:55:25 | [diff] [blame] | 39 | fn clear(&mut self) { |
Daniel Micay | d68be89 | 2013-06-13 01:56:16 | [diff] [blame] | 40 | for self.elts.mut_iter().advance |x| { *x = None } |
Daniel Micay | ed7c9c4 | 2013-02-16 23:55:25 | [diff] [blame] | 41 | self.nelts = 0; |
| 42 | self.lo = 0; |
| 43 | self.hi = 0; |
| 44 | } |
| 45 | } |
| 46 | |
Patrick Walton | 5fb2546 | 2013-05-31 22:17:22 | [diff] [blame] | 47 | impl<T> Deque<T> { |
Niko Matsakis | 0339647 | 2013-04-10 20:14:06 | [diff] [blame] | 48 | /// Create an empty Deque |
Patrick Walton | 5fb2546 | 2013-05-31 22:17:22 | [diff] [blame] | 49 | pub fn new() -> Deque<T> { |
Niko Matsakis | 0339647 | 2013-04-10 20:14:06 | [diff] [blame] | 50 | Deque{nelts: 0, lo: 0, hi: 0, |
| 51 | elts: vec::from_fn(initial_capacity, |_| None)} |
| 52 | } |
| 53 | |
| 54 | /// Return a reference to the first element in the deque |
| 55 | /// |
| 56 | /// Fails if the deque is empty |
Patrick Walton | 5fb2546 | 2013-05-31 22:17:22 | [diff] [blame] | 57 | pub fn peek_front<'a>(&'a self) -> &'a T { get(self.elts, self.lo) } |
Niko Matsakis | 0339647 | 2013-04-10 20:14:06 | [diff] [blame] | 58 | |
| 59 | /// Return a reference to the last element in the deque |
| 60 | /// |
| 61 | /// Fails if the deque is empty |
Patrick Walton | 5fb2546 | 2013-05-31 22:17:22 | [diff] [blame] | 62 | pub fn peek_back<'a>(&'a self) -> &'a T { get(self.elts, self.hi - 1u) } |
Niko Matsakis | 0339647 | 2013-04-10 20:14:06 | [diff] [blame] | 63 | |
| 64 | /// Retrieve an element in the deque by index |
| 65 | /// |
| 66 | /// Fails if there is no element with the given index |
Patrick Walton | 5fb2546 | 2013-05-31 22:17:22 | [diff] [blame] | 67 | pub fn get<'a>(&'a self, i: int) -> &'a T { |
Niko Matsakis | 0339647 | 2013-04-10 20:14:06 | [diff] [blame] | 68 | let idx = (self.lo + (i as uint)) % self.elts.len(); |
| 69 | get(self.elts, idx) |
| 70 | } |
| 71 | |
Erick Tryzelaar | 909d8f0 | 2013-03-30 01:02:44 | [diff] [blame] | 72 | /// Iterate over the elements in the deque |
Patrick Walton | 5fb2546 | 2013-05-31 22:17:22 | [diff] [blame] | 73 | pub fn each(&self, f: &fn(&T) -> bool) -> bool { |
Alex Crichton | 3ce9dba | 2013-05-02 22:33:27 | [diff] [blame] | 74 | self.eachi(|_i, e| f(e)) |
| 75 | } |
Erick Tryzelaar | 909d8f0 | 2013-03-30 01:02:44 | [diff] [blame] | 76 | |
| 77 | /// Iterate over the elements in the deque by index |
Patrick Walton | 5fb2546 | 2013-05-31 22:17:22 | [diff] [blame] | 78 | pub fn eachi(&self, f: &fn(uint, &T) -> bool) -> bool { |
Alex Crichton | 3ce9dba | 2013-05-02 22:33:27 | [diff] [blame] | 79 | uint::range(0, self.nelts, |i| f(i, self.get(i as int))) |
Erick Tryzelaar | 909d8f0 | 2013-03-30 01:02:44 | [diff] [blame] | 80 | } |
| 81 | |
Daniel Micay | b3ee49c | 2013-03-15 17:41:02 | [diff] [blame] | 82 | /// Remove and return the first element in the deque |
| 83 | /// |
| 84 | /// Fails if the deque is empty |
Patrick Walton | 5fb2546 | 2013-05-31 22:17:22 | [diff] [blame] | 85 | pub fn pop_front(&mut self) -> T { |
Alex Crichton | 7d317fe | 2013-04-12 05:10:12 | [diff] [blame] | 86 | let result = self.elts[self.lo].swap_unwrap(); |
Daniel Micay | 6190661 | 2013-02-17 00:43:29 | [diff] [blame] | 87 | self.lo = (self.lo + 1u) % self.elts.len(); |
| 88 | self.nelts -= 1u; |
| 89 | result |
| 90 | } |
| 91 | |
Daniel Micay | b3ee49c | 2013-03-15 17:41:02 | [diff] [blame] | 92 | /// Remove and return the last element in the deque |
| 93 | /// |
| 94 | /// Fails if the deque is empty |
Patrick Walton | 5fb2546 | 2013-05-31 22:17:22 | [diff] [blame] | 95 | pub fn pop_back(&mut self) -> T { |
Daniel Micay | 6190661 | 2013-02-17 00:43:29 | [diff] [blame] | 96 | if self.hi == 0u { |
| 97 | self.hi = self.elts.len() - 1u; |
| 98 | } else { self.hi -= 1u; } |
Alex Crichton | 7d317fe | 2013-04-12 05:10:12 | [diff] [blame] | 99 | let result = self.elts[self.hi].swap_unwrap(); |
Daniel Micay | 6190661 | 2013-02-17 00:43:29 | [diff] [blame] | 100 | self.elts[self.hi] = None; |
| 101 | self.nelts -= 1u; |
| 102 | result |
| 103 | } |
Marijn Haverbeke | 26610db | 2012-01-11 11:49:33 | [diff] [blame] | 104 | |
Daniel Micay | b3ee49c | 2013-03-15 17:41:02 | [diff] [blame] | 105 | /// Prepend an element to the deque |
Patrick Walton | 5fb2546 | 2013-05-31 22:17:22 | [diff] [blame] | 106 | pub fn add_front(&mut self, t: T) { |
Daniel Micay | 373c072 | 2013-02-17 00:10:10 | [diff] [blame] | 107 | let oldlo = self.lo; |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 108 | if self.lo == 0u { |
| 109 | self.lo = self.elts.len() - 1u; |
| 110 | } else { self.lo -= 1u; } |
| 111 | if self.lo == self.hi { |
| 112 | self.elts = grow(self.nelts, oldlo, self.elts); |
| 113 | self.lo = self.elts.len() - 1u; |
| 114 | self.hi = self.nelts; |
| 115 | } |
| 116 | self.elts[self.lo] = Some(t); |
| 117 | self.nelts += 1u; |
Erick Tryzelaar | e84576b | 2013-01-22 16:44:24 | [diff] [blame] | 118 | } |
Marijn Haverbeke | 26610db | 2012-01-11 11:49:33 | [diff] [blame] | 119 | |
Daniel Micay | b3ee49c | 2013-03-15 17:41:02 | [diff] [blame] | 120 | /// Append an element to the deque |
Patrick Walton | 5fb2546 | 2013-05-31 22:17:22 | [diff] [blame] | 121 | pub fn add_back(&mut self, t: T) { |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 122 | if self.lo == self.hi && self.nelts != 0u { |
| 123 | self.elts = grow(self.nelts, self.lo, self.elts); |
| 124 | self.lo = 0u; |
| 125 | self.hi = self.nelts; |
Graydon Hoare | 2880ecd | 2010-09-22 22:44:13 | [diff] [blame] | 126 | } |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 127 | self.elts[self.hi] = Some(t); |
| 128 | self.hi = (self.hi + 1u) % self.elts.len(); |
| 129 | self.nelts += 1u; |
Graydon Hoare | ce72993 | 2011-06-15 18:19:50 | [diff] [blame] | 130 | } |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 131 | |
| 132 | /// Reserve capacity for exactly `n` elements in the given deque, |
| 133 | /// doing nothing if `self`'s capacity is already equal to or greater |
| 134 | /// than the requested capacity |
| 135 | /// |
| 136 | /// # Arguments |
| 137 | /// |
| 138 | /// * n - The number of elements to reserve space for |
Patrick Walton | 5fb2546 | 2013-05-31 22:17:22 | [diff] [blame] | 139 | pub fn reserve(&mut self, n: uint) { |
Huon Wilson | 32d6559 | 2013-06-27 14:40:47 | [diff] [blame] | 140 | self.elts.reserve(n); |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /// Reserve capacity for at least `n` elements in the given deque, |
| 144 | /// over-allocating in case the caller needs to reserve additional |
| 145 | /// space. |
| 146 | /// |
| 147 | /// Do nothing if `self`'s capacity is already equal to or greater |
| 148 | /// than the requested capacity. |
| 149 | /// |
| 150 | /// # Arguments |
| 151 | /// |
| 152 | /// * n - The number of elements to reserve space for |
Patrick Walton | 5fb2546 | 2013-05-31 22:17:22 | [diff] [blame] | 153 | pub fn reserve_at_least(&mut self, n: uint) { |
Huon Wilson | 32d6559 | 2013-06-27 14:40:47 | [diff] [blame] | 154 | self.elts.reserve_at_least(n); |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 155 | } |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 156 | |
| 157 | /// Front-to-back iterator. |
| 158 | pub fn iter<'a>(&'a self) -> DequeIterator<'a, T> { |
Jed Estep | dfc9392 | 2013-06-26 02:33:02 | [diff] [blame] | 159 | DequeIterator { idx: self.lo, nelts: self.nelts, used: 0, vec: self.elts } |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 160 | } |
Corey Richardson | f8ae9b0 | 2013-06-26 22:14:35 | [diff] [blame] | 161 | |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 162 | /// Front-to-back iterator which returns mutable values. |
| 163 | pub fn mut_iter<'a>(&'a mut self) -> DequeMutIterator<'a, T> { |
Jed Estep | dfc9392 | 2013-06-26 02:33:02 | [diff] [blame] | 164 | DequeMutIterator { idx: self.lo, nelts: self.nelts, used: 0, vec: self.elts } |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /// Back-to-front iterator. |
| 168 | pub fn rev_iter<'a>(&'a self) -> DequeRevIterator<'a, T> { |
Jed Estep | dfc9392 | 2013-06-26 02:33:02 | [diff] [blame] | 169 | DequeRevIterator { idx: self.hi - 1u, nelts: self.nelts, used: 0, vec: self.elts } |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /// Back-to-front iterator which returns mutable values. |
| 173 | pub fn mut_rev_iter<'a>(&'a mut self) -> DequeMutRevIterator<'a, T> { |
Jed Estep | dfc9392 | 2013-06-26 02:33:02 | [diff] [blame] | 174 | DequeMutRevIterator { idx: self.hi - 1u, nelts: self.nelts, used: 0, vec: self.elts } |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Jed Estep | 35314c9 | 2013-06-26 15:38:29 | [diff] [blame] | 178 | macro_rules! iterator { |
| 179 | (impl $name:ident -> $elem:ty, $step:expr) => { |
| 180 | impl<'self, T> Iterator<$elem> for $name<'self, T> { |
| 181 | #[inline] |
| 182 | fn next(&mut self) -> Option<$elem> { |
| 183 | if self.used >= self.nelts { |
| 184 | return None; |
| 185 | } |
Corey Richardson | f8ae9b0 | 2013-06-26 22:14:35 | [diff] [blame] | 186 | let ret = unsafe { |
Jed Estep | 35314c9 | 2013-06-26 15:38:29 | [diff] [blame] | 187 | match self.vec[self.idx % self.vec.len()] { |
| 188 | Some(ref e) => Some(transmute(e)), |
| 189 | None => None |
| 190 | } |
| 191 | }; |
| 192 | self.idx += $step; |
| 193 | self.used += 1; |
| 194 | ret |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 200 | /// Deque iterator |
| 201 | pub struct DequeIterator<'self, T> { |
Jed Estep | dfc9392 | 2013-06-26 02:33:02 | [diff] [blame] | 202 | priv idx: uint, |
| 203 | priv nelts: uint, |
| 204 | priv used: uint, |
| 205 | priv vec: &'self [Option<T>] |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 206 | } |
Jed Estep | 35314c9 | 2013-06-26 15:38:29 | [diff] [blame] | 207 | iterator!{impl DequeIterator -> &'self T, 1} |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 208 | |
| 209 | /// Deque reverse iterator |
| 210 | pub struct DequeRevIterator<'self, T> { |
Jed Estep | dfc9392 | 2013-06-26 02:33:02 | [diff] [blame] | 211 | priv idx: uint, |
| 212 | priv nelts: uint, |
| 213 | priv used: uint, |
| 214 | priv vec: &'self [Option<T>] |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 215 | } |
Jed Estep | 35314c9 | 2013-06-26 15:38:29 | [diff] [blame] | 216 | iterator!{impl DequeRevIterator -> &'self T, -1} |
| 217 | |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 218 | /// Deque mutable iterator |
| 219 | pub struct DequeMutIterator<'self, T> { |
Jed Estep | dfc9392 | 2013-06-26 02:33:02 | [diff] [blame] | 220 | priv idx: uint, |
| 221 | priv nelts: uint, |
| 222 | priv used: uint, |
| 223 | priv vec: &'self mut [Option<T>] |
| 224 | |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 225 | } |
Jed Estep | 35314c9 | 2013-06-26 15:38:29 | [diff] [blame] | 226 | iterator!{impl DequeMutIterator -> &'self mut T, 1} |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 227 | |
| 228 | /// Deque mutable reverse iterator |
| 229 | pub struct DequeMutRevIterator<'self, T> { |
Jed Estep | dfc9392 | 2013-06-26 02:33:02 | [diff] [blame] | 230 | priv idx: uint, |
| 231 | priv nelts: uint, |
| 232 | priv used: uint, |
| 233 | priv vec: &'self mut [Option<T>] |
Jed Estep | 4f7a742 | 2013-06-25 19:08:47 | [diff] [blame] | 234 | } |
Jed Estep | 35314c9 | 2013-06-26 15:38:29 | [diff] [blame] | 235 | iterator!{impl DequeMutRevIterator -> &'self mut T, -1} |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 236 | |
| 237 | /// Grow is only called on full elts, so nelts is also len(elts), unlike |
| 238 | /// elsewhere. |
Daniel Micay | 5aa0463 | 2013-02-17 02:34:09 | [diff] [blame] | 239 | fn grow<T>(nelts: uint, lo: uint, elts: &mut [Option<T>]) -> ~[Option<T>] { |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 240 | assert_eq!(nelts, elts.len()); |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 241 | let mut rv = ~[]; |
| 242 | |
Daniel Micay | 6956e81 | 2013-02-17 19:08:04 | [diff] [blame] | 243 | do rv.grow_fn(nelts + 1) |i| { |
Alex Crichton | 998fece | 2013-05-06 04:42:54 | [diff] [blame] | 244 | replace(&mut elts[(lo + i) % nelts], None) |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | rv |
| 248 | } |
| 249 | |
Patrick Walton | 8b56a83 | 2013-03-25 20:21:04 | [diff] [blame] | 250 | fn get<'r, T>(elts: &'r [Option<T>], i: uint) -> &'r T { |
Daniel Micay | 5929f15 | 2013-02-17 00:03:27 | [diff] [blame] | 251 | match elts[i] { Some(ref t) => t, _ => fail!() } |
Roy Frostig | 9c81889 | 2010-07-21 01:03:09 | [diff] [blame] | 252 | } |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 253 | |
| 254 | #[cfg(test)] |
| 255 | mod tests { |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 256 | use super::*; |
Alex Crichton | 2df07dd | 2013-02-25 19:11:21 | [diff] [blame] | 257 | use core::cmp::Eq; |
Brian Anderson | ccc4c1a | 2013-05-07 21:54:42 | [diff] [blame] | 258 | use core::kinds::Copy; |
Corey Richardson | ab428b6 | 2013-06-27 00:02:34 | [diff] [blame] | 259 | use core; |
Patrick Walton | fa5ee93 | 2012-12-28 02:24:18 | [diff] [blame] | 260 | |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 261 | #[test] |
| 262 | fn test_simple() { |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 263 | let mut d = Deque::new(); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 264 | assert_eq!(d.len(), 0u); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 265 | d.add_front(17); |
| 266 | d.add_front(42); |
| 267 | d.add_back(137); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 268 | assert_eq!(d.len(), 3u); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 269 | d.add_back(137); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 270 | assert_eq!(d.len(), 4u); |
Brian Anderson | 82f1903 | 2013-03-08 20:39:42 | [diff] [blame] | 271 | debug!(d.peek_front()); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 272 | assert_eq!(*d.peek_front(), 42); |
Brian Anderson | 82f1903 | 2013-03-08 20:39:42 | [diff] [blame] | 273 | debug!(d.peek_back()); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 274 | assert_eq!(*d.peek_back(), 137); |
Niko Matsakis | dc07280 | 2012-03-22 15:39:41 | [diff] [blame] | 275 | let mut i: int = d.pop_front(); |
Brian Anderson | 82f1903 | 2013-03-08 20:39:42 | [diff] [blame] | 276 | debug!(i); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 277 | assert_eq!(i, 42); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 278 | i = d.pop_back(); |
Brian Anderson | 82f1903 | 2013-03-08 20:39:42 | [diff] [blame] | 279 | debug!(i); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 280 | assert_eq!(i, 137); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 281 | i = d.pop_back(); |
Brian Anderson | 82f1903 | 2013-03-08 20:39:42 | [diff] [blame] | 282 | debug!(i); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 283 | assert_eq!(i, 137); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 284 | i = d.pop_back(); |
Brian Anderson | 82f1903 | 2013-03-08 20:39:42 | [diff] [blame] | 285 | debug!(i); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 286 | assert_eq!(i, 17); |
| 287 | assert_eq!(d.len(), 0u); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 288 | d.add_back(3); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 289 | assert_eq!(d.len(), 1u); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 290 | d.add_front(2); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 291 | assert_eq!(d.len(), 2u); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 292 | d.add_back(4); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 293 | assert_eq!(d.len(), 3u); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 294 | d.add_front(1); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 295 | assert_eq!(d.len(), 4u); |
Brian Anderson | 82f1903 | 2013-03-08 20:39:42 | [diff] [blame] | 296 | debug!(d.get(0)); |
| 297 | debug!(d.get(1)); |
| 298 | debug!(d.get(2)); |
| 299 | debug!(d.get(3)); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 300 | assert_eq!(*d.get(0), 1); |
| 301 | assert_eq!(*d.get(1), 2); |
| 302 | assert_eq!(*d.get(2), 3); |
| 303 | assert_eq!(*d.get(3), 4); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 304 | } |
| 305 | |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 306 | #[test] |
| 307 | fn test_boxes() { |
| 308 | let a: @int = @5; |
| 309 | let b: @int = @72; |
| 310 | let c: @int = @64; |
| 311 | let d: @int = @175; |
| 312 | |
Daniel Micay | b47e1e9 | 2013-02-16 22:55:55 | [diff] [blame] | 313 | let mut deq = Deque::new(); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 314 | assert_eq!(deq.len(), 0); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 315 | deq.add_front(a); |
| 316 | deq.add_front(b); |
| 317 | deq.add_back(c); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 318 | assert_eq!(deq.len(), 3); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 319 | deq.add_back(d); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 320 | assert_eq!(deq.len(), 4); |
| 321 | assert_eq!(*deq.peek_front(), b); |
| 322 | assert_eq!(*deq.peek_back(), d); |
| 323 | assert_eq!(deq.pop_front(), b); |
| 324 | assert_eq!(deq.pop_back(), d); |
| 325 | assert_eq!(deq.pop_back(), c); |
| 326 | assert_eq!(deq.pop_back(), a); |
| 327 | assert_eq!(deq.len(), 0); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 328 | deq.add_back(c); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 329 | assert_eq!(deq.len(), 1); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 330 | deq.add_front(b); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 331 | assert_eq!(deq.len(), 2); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 332 | deq.add_back(d); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 333 | assert_eq!(deq.len(), 3); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 334 | deq.add_front(a); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 335 | assert_eq!(deq.len(), 4); |
| 336 | assert_eq!(*deq.get(0), a); |
| 337 | assert_eq!(*deq.get(1), b); |
| 338 | assert_eq!(*deq.get(2), c); |
| 339 | assert_eq!(*deq.get(3), d); |
Daniel Micay | 373c072 | 2013-02-17 00:10:10 | [diff] [blame] | 340 | } |
| 341 | |
Felix S. Klock II | a636f51 | 2013-05-01 23:32:37 | [diff] [blame] | 342 | #[cfg(test)] |
Brian Anderson | ccc4c1a | 2013-05-07 21:54:42 | [diff] [blame] | 343 | fn test_parameterized<T:Copy + Eq>(a: T, b: T, c: T, d: T) { |
Daniel Micay | 373c072 | 2013-02-17 00:10:10 | [diff] [blame] | 344 | let mut deq = Deque::new(); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 345 | assert_eq!(deq.len(), 0); |
Niko Matsakis | eb48c29 | 2013-06-16 00:26:59 | [diff] [blame] | 346 | deq.add_front(copy a); |
| 347 | deq.add_front(copy b); |
| 348 | deq.add_back(copy c); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 349 | assert_eq!(deq.len(), 3); |
Niko Matsakis | eb48c29 | 2013-06-16 00:26:59 | [diff] [blame] | 350 | deq.add_back(copy d); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 351 | assert_eq!(deq.len(), 4); |
Niko Matsakis | eb48c29 | 2013-06-16 00:26:59 | [diff] [blame] | 352 | assert_eq!(copy *deq.peek_front(), copy b); |
| 353 | assert_eq!(copy *deq.peek_back(), copy d); |
| 354 | assert_eq!(deq.pop_front(), copy b); |
| 355 | assert_eq!(deq.pop_back(), copy d); |
| 356 | assert_eq!(deq.pop_back(), copy c); |
| 357 | assert_eq!(deq.pop_back(), copy a); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 358 | assert_eq!(deq.len(), 0); |
Niko Matsakis | eb48c29 | 2013-06-16 00:26:59 | [diff] [blame] | 359 | deq.add_back(copy c); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 360 | assert_eq!(deq.len(), 1); |
Niko Matsakis | eb48c29 | 2013-06-16 00:26:59 | [diff] [blame] | 361 | deq.add_front(copy b); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 362 | assert_eq!(deq.len(), 2); |
Niko Matsakis | eb48c29 | 2013-06-16 00:26:59 | [diff] [blame] | 363 | deq.add_back(copy d); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 364 | assert_eq!(deq.len(), 3); |
Niko Matsakis | eb48c29 | 2013-06-16 00:26:59 | [diff] [blame] | 365 | deq.add_front(copy a); |
Corey Richardson | cc57ca0 | 2013-05-19 02:02:45 | [diff] [blame] | 366 | assert_eq!(deq.len(), 4); |
Niko Matsakis | eb48c29 | 2013-06-16 00:26:59 | [diff] [blame] | 367 | assert_eq!(copy *deq.get(0), copy a); |
| 368 | assert_eq!(copy *deq.get(1), copy b); |
| 369 | assert_eq!(copy *deq.get(2), copy c); |
| 370 | assert_eq!(copy *deq.get(3), copy d); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 371 | } |
| 372 | |
Andrew Paseltiner | 4055fe8 | 2013-03-20 15:36:16 | [diff] [blame] | 373 | #[deriving(Eq)] |
Ben Striegel | a605fd0 | 2012-08-11 14:08:42 | [diff] [blame] | 374 | enum Taggy { One(int), Two(int, int), Three(int, int, int), } |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 375 | |
Andrew Paseltiner | 4055fe8 | 2013-03-20 15:36:16 | [diff] [blame] | 376 | #[deriving(Eq)] |
Ben Striegel | a605fd0 | 2012-08-11 14:08:42 | [diff] [blame] | 377 | enum Taggypar<T> { |
| 378 | Onepar(int), Twopar(int, int), Threepar(int, int, int), |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 379 | } |
| 380 | |
Andrew Paseltiner | 4055fe8 | 2013-03-20 15:36:16 | [diff] [blame] | 381 | #[deriving(Eq)] |
Erick Tryzelaar | e84576b | 2013-01-22 16:44:24 | [diff] [blame] | 382 | struct RecCy { |
| 383 | x: int, |
| 384 | y: int, |
Patrick Walton | eb4d39e | 2013-01-26 00:57:39 | [diff] [blame] | 385 | t: Taggy |
Patrick Walton | 9117dcb | 2012-09-20 01:00:26 | [diff] [blame] | 386 | } |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 387 | |
| 388 | #[test] |
| 389 | fn test_param_int() { |
| 390 | test_parameterized::<int>(5, 72, 64, 175); |
| 391 | } |
| 392 | |
| 393 | #[test] |
| 394 | fn test_param_at_int() { |
| 395 | test_parameterized::<@int>(@5, @72, @64, @175); |
| 396 | } |
| 397 | |
| 398 | #[test] |
| 399 | fn test_param_taggy() { |
Corey Richardson | f8ae9b0 | 2013-06-26 22:14:35 | [diff] [blame] | 400 | test_parameterized::<Taggy>(One(1), Two(1, 2), Three(1, 2, 3), Two(17, 42)); |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | #[test] |
| 404 | fn test_param_taggypar() { |
| 405 | test_parameterized::<Taggypar<int>>(Onepar::<int>(1), |
Ben Striegel | a605fd0 | 2012-08-11 14:08:42 | [diff] [blame] | 406 | Twopar::<int>(1, 2), |
| 407 | Threepar::<int>(1, 2, 3), |
| 408 | Twopar::<int>(17, 42)); |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 409 | } |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 410 | |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 411 | #[test] |
| 412 | fn test_param_reccy() { |
Erick Tryzelaar | e84576b | 2013-01-22 16:44:24 | [diff] [blame] | 413 | let reccy1 = RecCy { x: 1, y: 2, t: One(1) }; |
| 414 | let reccy2 = RecCy { x: 345, y: 2, t: Two(1, 2) }; |
| 415 | let reccy3 = RecCy { x: 1, y: 777, t: Three(1, 2, 3) }; |
| 416 | let reccy4 = RecCy { x: 19, y: 252, t: Two(17, 42) }; |
Kevin Cantu | c43426e | 2012-09-13 05:09:55 | [diff] [blame] | 417 | test_parameterized::<RecCy>(reccy1, reccy2, reccy3, reccy4); |
Brian Anderson | 6e27b27 | 2012-01-18 03:05:07 | [diff] [blame] | 418 | } |
Erick Tryzelaar | 909d8f0 | 2013-03-30 01:02:44 | [diff] [blame] | 419 | |
| 420 | #[test] |
| 421 | fn test_eachi() { |
| 422 | let mut deq = Deque::new(); |
| 423 | deq.add_back(1); |
| 424 | deq.add_back(2); |
| 425 | deq.add_back(3); |
| 426 | |
| 427 | for deq.eachi |i, e| { |
| 428 | assert_eq!(*e, i + 1); |
| 429 | } |
| 430 | |
| 431 | deq.pop_front(); |
| 432 | |
| 433 | for deq.eachi |i, e| { |
| 434 | assert_eq!(*e, i + 2); |
| 435 | } |
| 436 | |
| 437 | } |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 438 | |
| 439 | #[test] |
| 440 | fn test_reserve() { |
| 441 | let mut d = Deque::new(); |
| 442 | d.add_back(0u64); |
| 443 | d.reserve(50); |
Huon Wilson | 32d6559 | 2013-06-27 14:40:47 | [diff] [blame] | 444 | assert_eq!(d.elts.capacity(), 50); |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 445 | let mut d = Deque::new(); |
| 446 | d.add_back(0u32); |
| 447 | d.reserve(50); |
Huon Wilson | 32d6559 | 2013-06-27 14:40:47 | [diff] [blame] | 448 | assert_eq!(d.elts.capacity(), 50); |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | #[test] |
| 452 | fn test_reserve_at_least() { |
| 453 | let mut d = Deque::new(); |
| 454 | d.add_back(0u64); |
| 455 | d.reserve_at_least(50); |
Huon Wilson | 32d6559 | 2013-06-27 14:40:47 | [diff] [blame] | 456 | assert_eq!(d.elts.capacity(), 64); |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 457 | let mut d = Deque::new(); |
| 458 | d.add_back(0u32); |
| 459 | d.reserve_at_least(50); |
Huon Wilson | 32d6559 | 2013-06-27 14:40:47 | [diff] [blame] | 460 | assert_eq!(d.elts.capacity(), 64); |
Tim Chevalier | 77de84b | 2013-05-27 18:47:38 | [diff] [blame] | 461 | } |
| 462 | |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 463 | #[test] |
| 464 | fn test_iter() { |
| 465 | let mut d = Deque::new(); |
Corey Richardson | ab428b6 | 2013-06-27 00:02:34 | [diff] [blame] | 466 | for core::int::range(0,5) |i| { |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 467 | d.add_back(i); |
| 468 | } |
Corey Richardson | f8ae9b0 | 2013-06-26 22:14:35 | [diff] [blame] | 469 | assert_eq!(d.iter().collect::<~[&int]>(), ~[&0,&1,&2,&3,&4]); |
| 470 | |
Corey Richardson | ab428b6 | 2013-06-27 00:02:34 | [diff] [blame] | 471 | for core::int::range(6,9) |i| { |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 472 | d.add_front(i); |
| 473 | } |
Corey Richardson | f8ae9b0 | 2013-06-26 22:14:35 | [diff] [blame] | 474 | assert_eq!(d.iter().collect::<~[&int]>(), ~[&8,&7,&6,&0,&1,&2,&3,&4]); |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | #[test] |
| 478 | fn test_rev_iter() { |
| 479 | let mut d = Deque::new(); |
Corey Richardson | ab428b6 | 2013-06-27 00:02:34 | [diff] [blame] | 480 | for core::int::range(0,5) |i| { |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 481 | d.add_back(i); |
| 482 | } |
Corey Richardson | f8ae9b0 | 2013-06-26 22:14:35 | [diff] [blame] | 483 | assert_eq!(d.rev_iter().collect::<~[&int]>(), ~[&4,&3,&2,&1,&0]); |
| 484 | |
Corey Richardson | ab428b6 | 2013-06-27 00:02:34 | [diff] [blame] | 485 | for core::int::range(6,9) |i| { |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 486 | d.add_front(i); |
| 487 | } |
Corey Richardson | f8ae9b0 | 2013-06-26 22:14:35 | [diff] [blame] | 488 | assert_eq!(d.rev_iter().collect::<~[&int]>(), ~[&4,&3,&2,&1,&0,&6,&7,&8]); |
Jed Estep | 096fb79 | 2013-06-26 14:04:44 | [diff] [blame] | 489 | } |
Michael Sullivan | c854d6e | 2012-07-03 17:52:32 | [diff] [blame] | 490 | } |