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-05-21 20:24:42 -05:00
|
|
|
use core::prelude::*;
|
|
|
|
|
2013-04-24 19:35:49 -05:00
|
|
|
use core::old_iter;
|
|
|
|
use core::old_iter::BaseIter;
|
2013-02-14 23:50:03 -06:00
|
|
|
|
2013-05-15 17:55:57 -05:00
|
|
|
#[deriving(Encodable, Decodable)]
|
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-04-17 11:15:08 -05:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn map<U>(&self, op: &fn(&T) -> U) -> OptVec<U> {
|
|
|
|
match *self {
|
|
|
|
Empty => Empty,
|
|
|
|
Vec(ref v) => Vec(v.map(op))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-10 15:11:27 -05:00
|
|
|
fn get<'a>(&'a self, i: uint) -> &'a T {
|
|
|
|
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-03-22 13:09:13 -05:00
|
|
|
fn is_empty(&self) -> bool {
|
2013-02-14 23:50:03 -06:00
|
|
|
self.len() == 0
|
|
|
|
}
|
|
|
|
|
2013-03-22 13:09:13 -05:00
|
|
|
fn len(&self) -> uint {
|
2013-02-14 23:50:03 -06:00
|
|
|
match *self {
|
|
|
|
Empty => 0,
|
|
|
|
Vec(ref v) => v.len()
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T:Copy> OptVec<T> {
|
2013-04-17 11:15:08 -05:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn push_all<I: BaseIter<T>>(&mut self, from: &I) {
|
|
|
|
for from.each |e| {
|
|
|
|
self.push(copy *e);
|
|
|
|
}
|
|
|
|
}
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
fn mapi_to_vec<B>(&self, op: &fn(uint, &T) -> B) -> ~[B] {
|
|
|
|
let mut index = 0;
|
2013-04-24 19:35:49 -05:00
|
|
|
old_iter::map_to_vec(self, |a| {
|
Cleanup substitutions and treatment of generics around traits in a number of ways.
- In a TraitRef, use the self type consistently to refer to the Self type:
- trait ref in `impl Trait<A,B,C> for S` has a self type of `S`.
- trait ref in `A:Trait` has the self type `A`
- trait ref associated with a trait decl has self type `Self`
- trait ref associated with a supertype has self type `Self`
- trait ref in an object type `@Trait` has no self type
- Rewrite `each_bound_traits_and_supertraits` to perform
substitutions as it goes, and thus yield a series of trait refs
that are always in the same 'namespace' as the type parameter
bound given as input. Before, we left this to the caller, but
this doesn't work because the caller lacks adequare information
to perform the type substitutions correctly.
- For provided methods, substitute the generics involved in the provided
method correctly.
- Introduce TypeParameterDef, which tracks the bounds declared on a type
parameter and brings them together with the def_id and (in the future)
other information (maybe even the parameter's name!).
- Introduce Subst trait, which helps to cleanup a lot of the
repetitive code involved with doing type substitution.
- Introduce Repr trait, which makes debug printouts far more convenient.
Fixes #4183. Needed for #5656.
2013-04-09 00:54:49 -05:00
|
|
|
let i = index;
|
|
|
|
index += 1;
|
|
|
|
op(i, a)
|
|
|
|
})
|
|
|
|
}
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<A:Eq> Eq for OptVec<A> {
|
2013-03-22 13:09:13 -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-03-22 13:09:13 -05:00
|
|
|
fn ne(&self, other: &OptVec<A>) -> bool {
|
2013-02-14 23:50:03 -06:00
|
|
|
!self.eq(other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<A> BaseIter<A> for OptVec<A> {
|
2013-05-02 17:33:33 -05:00
|
|
|
fn each(&self, blk: &fn(v: &A) -> bool) -> bool {
|
|
|
|
match *self {
|
|
|
|
Empty => true,
|
|
|
|
Vec(ref v) => v.each(blk)
|
|
|
|
}
|
|
|
|
}
|
2013-02-14 23:50:03 -06:00
|
|
|
|
2013-03-22 13:09:13 -05:00
|
|
|
fn size_hint(&self) -> Option<uint> {
|
2013-02-14 23:50:03 -06:00
|
|
|
Some(self.len())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-24 19:35:49 -05:00
|
|
|
impl<A> old_iter::ExtendedIter<A> for OptVec<A> {
|
2013-02-14 23:50:03 -06:00
|
|
|
#[inline(always)]
|
2013-05-02 17:33:33 -05:00
|
|
|
fn eachi(&self, blk: &fn(v: uint, v: &A) -> bool) -> bool {
|
|
|
|
old_iter::eachi(self, blk)
|
|
|
|
}
|
|
|
|
#[inline(always)]
|
2013-03-22 13:09:13 -05:00
|
|
|
fn all(&self, blk: &fn(&A) -> bool) -> bool {
|
2013-04-24 19:35:49 -05:00
|
|
|
old_iter::all(self, blk)
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
|
|
|
#[inline(always)]
|
2013-03-22 13:09:13 -05:00
|
|
|
fn any(&self, blk: &fn(&A) -> bool) -> bool {
|
2013-04-24 19:35:49 -05:00
|
|
|
old_iter::any(self, blk)
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
|
|
|
#[inline(always)]
|
2013-04-17 11:15:08 -05:00
|
|
|
fn foldl<B>(&self, b0: B, blk: &fn(&B, &A) -> B) -> B {
|
2013-04-24 19:35:49 -05:00
|
|
|
old_iter::foldl(self, b0, blk)
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
|
|
|
#[inline(always)]
|
2013-03-22 13:09:13 -05:00
|
|
|
fn position(&self, f: &fn(&A) -> bool) -> Option<uint> {
|
2013-04-24 19:35:49 -05:00
|
|
|
old_iter::position(self, f)
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
|
|
|
#[inline(always)]
|
2013-03-22 13:09:13 -05:00
|
|
|
fn map_to_vec<B>(&self, op: &fn(&A) -> B) -> ~[B] {
|
2013-04-24 19:35:49 -05:00
|
|
|
old_iter::map_to_vec(self, op)
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
|
|
|
#[inline(always)]
|
2013-03-22 13:09:13 -05:00
|
|
|
fn flat_map_to_vec<B,IB:BaseIter<B>>(&self, op: &fn(&A) -> IB)
|
2013-02-14 23:50:03 -06:00
|
|
|
-> ~[B] {
|
2013-04-24 19:35:49 -05:00
|
|
|
old_iter::flat_map_to_vec(self, op)
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-04-24 19:35:49 -05:00
|
|
|
impl<A: Eq> old_iter::EqIter<A> for OptVec<A> {
|
2013-02-14 23:50:03 -06:00
|
|
|
#[inline(always)]
|
2013-04-24 19:35:49 -05:00
|
|
|
fn contains(&self, x: &A) -> bool { old_iter::contains(self, x) }
|
2013-02-14 23:50:03 -06:00
|
|
|
#[inline(always)]
|
2013-04-24 19:35:49 -05:00
|
|
|
fn count(&self, x: &A) -> uint { old_iter::count(self, x) }
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
|
|
|
|
2013-04-24 19:35:49 -05:00
|
|
|
impl<A: Copy> old_iter::CopyableIter<A> for OptVec<A> {
|
2013-02-14 23:50:03 -06:00
|
|
|
#[inline(always)]
|
2013-03-22 13:09:13 -05:00
|
|
|
fn filter_to_vec(&self, pred: &fn(&A) -> bool) -> ~[A] {
|
2013-04-24 19:35:49 -05:00
|
|
|
old_iter::filter_to_vec(self, pred)
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
|
|
|
#[inline(always)]
|
2013-04-24 19:35:49 -05:00
|
|
|
fn to_vec(&self) -> ~[A] { old_iter::to_vec(self) }
|
2013-02-14 23:50:03 -06:00
|
|
|
#[inline(always)]
|
2013-03-22 13:09:13 -05:00
|
|
|
fn find(&self, f: &fn(&A) -> bool) -> Option<A> {
|
2013-04-24 19:35:49 -05:00
|
|
|
old_iter::find(self, f)
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-24 19:35:49 -05:00
|
|
|
impl<A: Copy+Ord> old_iter::CopyableOrderedIter<A> for OptVec<A> {
|
2013-02-14 23:50:03 -06:00
|
|
|
#[inline(always)]
|
2013-04-24 19:35:49 -05:00
|
|
|
fn min(&self) -> A { old_iter::min(self) }
|
2013-02-14 23:50:03 -06:00
|
|
|
#[inline(always)]
|
2013-04-24 19:35:49 -05:00
|
|
|
fn max(&self) -> A { old_iter::max(self) }
|
2013-02-14 23:50:03 -06:00
|
|
|
}
|