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