1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
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.
12 * Defines a type OptVec<T> that can be used in place of ~[T].
13 * OptVec avoids the need for allocation for empty vectors.
14 * OptVec implements the iterable interface as well as
15 * other useful things like `push()` and `len()`.
18 use std::default::Default;
22 #[deriving(Clone, Encodable, Decodable, Hash)]
28 pub fn with<T>(t: T) -> OptVec<T> {
32 pub fn from<T>(t: Vec<T> ) -> OptVec<T> {
41 pub fn push(&mut self, t: T) {
53 pub fn pop(&mut self) -> Option<T> {
55 Vec(ref mut v) => v.pop(),
60 pub fn last<'a>(&'a self) -> Option<&'a T> {
62 Vec(ref v) => v.last(),
67 pub fn mut_last<'a>(&'a mut self) -> Option<&'a mut T> {
69 Vec(ref mut v) => v.mut_last(),
74 pub fn map<U>(&self, op: |&T| -> U) -> OptVec<U> {
77 Vec(ref v) => Vec(v.map(op))
81 pub fn map_move<U>(self, op: |T| -> U) -> OptVec<U> {
84 Vec(v) => Vec(v.move_iter().map(op).collect())
88 pub fn get<'a>(&'a self, i: uint) -> &'a T {
90 Empty => fail!("invalid index {}", i),
91 Vec(ref v) => v.get(i)
95 pub fn is_empty(&self) -> bool {
99 pub fn len(&self) -> uint {
102 Vec(ref v) => v.len()
106 pub fn swap_remove(&mut self, index: uint) {
108 Empty => { fail!("index out of bounds"); }
110 assert!(index < v.len());
111 v.swap_remove(index);
117 pub fn iter<'r>(&'r self) -> Items<'r, T> {
119 Empty => Items{iter: None},
120 Vec(ref v) => Items{iter: Some(v.iter())}
125 pub fn map_to_vec<B>(&self, op: |&T| -> B) -> Vec<B> {
126 self.iter().map(op).collect()
129 pub fn mapi_to_vec<B>(&self, op: |uint, &T| -> B) -> Vec<B> {
131 self.map_to_vec(|a| {
139 pub fn take_vec<T>(v: OptVec<T>) -> Vec<T> {
146 impl<T:Clone> OptVec<T> {
147 pub fn prepend(&self, t: T) -> OptVec<T> {
148 let mut v0 = vec!(t);
151 Vec(ref v1) => { v0.push_all(v1.as_slice()); }
157 impl<A:Eq> Eq for OptVec<A> {
158 fn eq(&self, other: &OptVec<A>) -> bool {
159 // Note: cannot use #[deriving(Eq)] here because
160 // (Empty, Vec(~[])) ought to be equal.
161 match (self, other) {
162 (&Empty, &Empty) => true,
163 (&Empty, &Vec(ref v)) => v.is_empty(),
164 (&Vec(ref v), &Empty) => v.is_empty(),
165 (&Vec(ref v1), &Vec(ref v2)) => *v1 == *v2
169 fn ne(&self, other: &OptVec<A>) -> bool {
174 impl<T> Default for OptVec<T> {
175 fn default() -> OptVec<T> { Empty }
178 pub struct Items<'a, T> {
179 priv iter: Option<vec::Items<'a, T>>
182 impl<'a, T> Iterator<&'a T> for Items<'a, T> {
184 fn next(&mut self) -> Option<&'a T> {
186 Some(ref mut x) => x.next(),
192 fn size_hint(&self) -> (uint, Option<uint>) {
194 Some(ref x) => x.size_hint(),
200 impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> {
202 fn next_back(&mut self) -> Option<&'a T> {
204 Some(ref mut x) => x.next_back(),
210 impl<A> FromIterator<A> for OptVec<A> {
211 fn from_iterator<T: Iterator<A>>(iterator: &mut T) -> OptVec<A> {