]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/opt_vec.rs
Merge remote-tracking branch 'remotes/origin/master' into cleanup-iterators
[rust.git] / src / libsyntax / opt_vec.rs
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.
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
11 /*!
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()`.
16  */
17
18 use std::vec::{VecIterator};
19
20 #[deriving(Clone, Encodable, Decodable, IterBytes)]
21 pub enum OptVec<T> {
22     Empty,
23     Vec(~[T])
24 }
25
26 pub fn with<T>(t: T) -> OptVec<T> {
27     Vec(~[t])
28 }
29
30 pub fn from<T>(t: ~[T]) -> OptVec<T> {
31     if t.len() == 0 {
32         Empty
33     } else {
34         Vec(t)
35     }
36 }
37
38 impl<T> OptVec<T> {
39     fn push(&mut self, t: T) {
40         match *self {
41             Vec(ref mut v) => {
42                 v.push(t);
43                 return;
44             }
45             Empty => {}
46         }
47
48         // FIXME(#5074): flow insensitive means we can't move
49         // assignment inside `match`
50         *self = Vec(~[t]);
51     }
52
53     fn map<U>(&self, op: &fn(&T) -> U) -> OptVec<U> {
54         match *self {
55             Empty => Empty,
56             Vec(ref v) => Vec(v.map(op))
57         }
58     }
59
60     fn map_move<U>(self, op: &fn(T) -> U) -> OptVec<U> {
61         match self {
62             Empty => Empty,
63             Vec(v) => Vec(v.move_iter().map(op).collect())
64         }
65     }
66
67     fn get<'a>(&'a self, i: uint) -> &'a T {
68         match *self {
69             Empty => fail!("Invalid index %u", i),
70             Vec(ref v) => &v[i]
71         }
72     }
73
74     fn is_empty(&self) -> bool {
75         self.len() == 0
76     }
77
78     fn len(&self) -> uint {
79         match *self {
80             Empty => 0,
81             Vec(ref v) => v.len()
82         }
83     }
84
85     #[inline]
86     fn iter<'r>(&'r self) -> OptVecIterator<'r, T> {
87         match *self {
88             Empty => OptVecIterator{iter: None},
89             Vec(ref v) => OptVecIterator{iter: Some(v.iter())}
90         }
91     }
92
93     #[inline]
94     fn map_to_vec<B>(&self, op: &fn(&T) -> B) -> ~[B] {
95         self.iter().map(op).collect()
96     }
97
98     fn mapi_to_vec<B>(&self, op: &fn(uint, &T) -> B) -> ~[B] {
99         let mut index = 0;
100         self.map_to_vec(|a| {
101             let i = index;
102             index += 1;
103             op(i, a)
104         })
105     }
106 }
107
108 pub fn take_vec<T>(v: OptVec<T>) -> ~[T] {
109     match v {
110         Empty => ~[],
111         Vec(v) => v
112     }
113 }
114
115 impl<T:Clone> OptVec<T> {
116     fn prepend(&self, t: T) -> OptVec<T> {
117         let mut v0 = ~[t];
118         match *self {
119             Empty => {}
120             Vec(ref v1) => { v0.push_all(*v1); }
121         }
122         return Vec(v0);
123     }
124 }
125
126 impl<A:Eq> Eq for OptVec<A> {
127     fn eq(&self, other: &OptVec<A>) -> bool {
128         // Note: cannot use #[deriving(Eq)] here because
129         // (Empty, Vec(~[])) ought to be equal.
130         match (self, other) {
131             (&Empty, &Empty) => true,
132             (&Empty, &Vec(ref v)) => v.is_empty(),
133             (&Vec(ref v), &Empty) => v.is_empty(),
134             (&Vec(ref v1), &Vec(ref v2)) => *v1 == *v2
135         }
136     }
137
138     fn ne(&self, other: &OptVec<A>) -> bool {
139         !self.eq(other)
140     }
141 }
142
143 pub struct OptVecIterator<'self, T> {
144     priv iter: Option<VecIterator<'self, T>>
145 }
146
147 impl<'self, T> Iterator<&'self T> for OptVecIterator<'self, T> {
148     #[inline]
149     fn next(&mut self) -> Option<&'self T> {
150         match self.iter {
151             Some(ref mut x) => x.next(),
152             None => None
153         }
154     }
155
156     #[inline]
157     fn size_hint(&self) -> (uint, Option<uint>) {
158         match self.iter {
159             Some(ref x) => x.size_hint(),
160             None => (0, Some(0))
161         }
162     }
163 }