2013-02-14 23:50:03 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* Defines a type OptVec<T> that can be used in place of ~[T].
|
|
|
|
* OptVec avoids the need for allocation for empty vectors.
|
|
|
|
* OptVec implements the iterable interface as well as
|
|
|
|
* other useful things like `push()` and `len()`.
|
|
|
|
*/
|
|
|
|
|
2013-07-05 20:38:56 -05:00
|
|
|
use std::vec::{VecIterator};
|
2013-02-14 23:50:03 -06:00
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
#[deriving(Clone, Encodable, Decodable, IterBytes)]
|
2013-02-14 23:50:03 -06:00
|
|
|
pub enum OptVec<T> {
|
|
|
|
Empty,
|
|
|
|
Vec(~[T])
|
|
|
|
}
|
|
|
|
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn with<T>(t: T) -> OptVec<T> {
|
2013-02-14 23:50:03 -06:00
|
|
|
Vec(~[t])
|
|
|
|
}
|
|
|
|
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn from<T>(t: ~[T]) -> OptVec<T> {
|
2013-03-01 18:38:39 -06:00
|
|
|
if t.len() == 0 {
|
|
|
|
Empty
|
|
|
|
} else {
|
|
|
|
Vec(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 23:50:03 -06:00
|
|
|
impl<T> OptVec<T> {
|
2013-08-07 07:29:29 -05:00
|
|
|
pub fn push(&mut self, t: T) {
|
2013-02-14 23:50:03 -06:00
|
|
|
match *self {
|
|
|
|
Vec(ref mut v) => {
|
|
|
|
v.push(t);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Empty => {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(#5074): flow insensitive means we can't move
|
|
|
|
// assignment inside `match`
|
|
|
|
*self = Vec(~[t]);
|
|
|
|
}
|
|
|
|
|
2013-08-07 07:29:29 -05:00
|
|
|
pub fn map<U>(&self, op: &fn(&T) -> U) -> OptVec<U> {
|
2013-02-14 23:50:03 -06:00
|
|
|
match *self {
|
|
|
|
Empty => Empty,
|
|
|
|
Vec(ref v) => Vec(v.map(op))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-10 14:59:52 -05:00
|
|
|
pub fn map_move<U>(self, op: &fn(T) -> U) -> OptVec<U> {
|
2013-07-05 20:38:56 -05:00
|
|
|
match self {
|
|
|
|
Empty => Empty,
|
2013-08-09 22:09:47 -05:00
|
|
|
Vec(v) => Vec(v.move_iter().map(op).collect())
|
2013-07-05 20:38:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 07:29:29 -05:00
|
|
|
pub fn get<'a>(&'a self, i: uint) -> &'a T {
|
2013-04-10 15:11:27 -05:00
|
|
|
match *self {
|
2013-05-05 17:18:51 -05:00
|
|
|
Empty => fail!("Invalid index %u", i),
|
2013-04-10 15:11:27 -05:00
|
|
|
Vec(ref v) => &v[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 07:29:29 -05:00
|
|
|
pub fn is_empty(&self) -> bool {
|
2013-02-14 23:50:03 -06:00
|
|
|
self.len() == 0
|
|
|
|
}
|
|
|
|
|
2013-08-07 07:29:29 -05:00
|
|
|
pub fn len(&self) -> uint {
|
2013-02-14 23:50:03 -06:00
|
|
|
match *self {
|
|
|
|
Empty => 0,
|
|
|
|
Vec(ref v) => v.len()
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 16:57:39 -05:00
|
|
|
|
|
|
|
#[inline]
|
2013-08-07 07:29:29 -05:00
|
|
|
pub fn iter<'r>(&'r self) -> OptVecIterator<'r, T> {
|
2013-06-23 16:57:39 -05:00
|
|
|
match *self {
|
|
|
|
Empty => OptVecIterator{iter: None},
|
|
|
|
Vec(ref v) => OptVecIterator{iter: Some(v.iter())}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2013-08-07 07:29:29 -05:00
|
|
|
pub fn map_to_vec<B>(&self, op: &fn(&T) -> B) -> ~[B] {
|
2013-08-09 22:09:47 -05:00
|
|
|
self.iter().map(op).collect()
|
2013-06-23 16:57:39 -05:00
|
|
|
}
|
|
|
|
|
2013-08-07 07:29:29 -05:00
|
|
|
pub fn mapi_to_vec<B>(&self, op: &fn(uint, &T) -> B) -> ~[B] {
|
2013-06-23 16:57:39 -05:00
|
|
|
let mut index = 0;
|
|
|
|
self.map_to_vec(|a| {
|
|
|
|
let i = index;
|
|
|
|
index += 1;
|
|
|
|
op(i, a)
|
|
|
|
})
|
|
|
|
}
|
2013-03-01 18:38:39 -06:00
|
|
|
}
|
2013-02-14 23:50:03 -06:00
|
|
|
|
2013-04-17 11:15:08 -05:00
|
|
|
pub fn take_vec<T>(v: OptVec<T>) -> ~[T] {
|
2013-03-01 18:38:39 -06:00
|
|
|
match v {
|
|
|
|
Empty => ~[],
|
|
|
|
Vec(v) => v
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-02 14:47:32 -05:00
|
|
|
impl<T:Clone> OptVec<T> {
|
2013-08-07 07:29:29 -05:00
|
|
|
pub fn prepend(&self, t: T) -> OptVec<T> {
|
2013-02-14 23:50:03 -06:00
|
|
|
let mut v0 = ~[t];
|
|
|
|
match *self {
|
|
|
|
Empty => {}
|
2013-02-28 09:25:31 -06:00
|
|
|
Vec(ref v1) => { v0.push_all(*v1); }
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
|
|
|
return Vec(v0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A:Eq> Eq for OptVec<A> {
|
2013-08-09 03:25:24 -05:00
|
|
|
fn eq(&self, other: &OptVec<A>) -> bool {
|
2013-03-20 10:52:45 -05:00
|
|
|
// Note: cannot use #[deriving(Eq)] here because
|
2013-02-14 23:50:03 -06:00
|
|
|
// (Empty, Vec(~[])) ought to be equal.
|
|
|
|
match (self, other) {
|
|
|
|
(&Empty, &Empty) => true,
|
|
|
|
(&Empty, &Vec(ref v)) => v.is_empty(),
|
|
|
|
(&Vec(ref v), &Empty) => v.is_empty(),
|
|
|
|
(&Vec(ref v1), &Vec(ref v2)) => *v1 == *v2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-09 03:25:24 -05:00
|
|
|
fn ne(&self, other: &OptVec<A>) -> bool {
|
2013-02-14 23:50:03 -06:00
|
|
|
!self.eq(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 16:57:39 -05:00
|
|
|
pub struct OptVecIterator<'self, T> {
|
|
|
|
priv iter: Option<VecIterator<'self, T>>
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
|
|
|
|
2013-06-23 16:57:39 -05:00
|
|
|
impl<'self, T> Iterator<&'self T> for OptVecIterator<'self, T> {
|
2013-06-18 16:45:18 -05:00
|
|
|
#[inline]
|
2013-06-23 16:57:39 -05:00
|
|
|
fn next(&mut self) -> Option<&'self T> {
|
|
|
|
match self.iter {
|
|
|
|
Some(ref mut x) => x.next(),
|
|
|
|
None => None
|
|
|
|
}
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
2013-07-03 13:30:12 -05:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
match self.iter {
|
|
|
|
Some(ref x) => x.size_hint(),
|
|
|
|
None => (0, Some(0))
|
|
|
|
}
|
|
|
|
}
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|