/
list.rs
326 lines (290 loc) · 8.32 KB
/
list.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// 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.
//! A standard, garbage-collected linked list.
#[deriving(Clone, Eq)]
#[allow(missing_doc)]
pub enum List<T> {
Cons(T, @List<T>),
Nil,
}
pub struct Items<'a, T> {
priv head: &'a List<T>,
priv next: Option<&'a @List<T>>
}
impl<'a, T> Iterator<&'a T> for Items<'a, T> {
fn next(&mut self) -> Option<&'a T> {
match self.next {
None => match *self.head {
Nil => None,
Cons(ref value, ref tail) => {
self.next = Some(tail);
Some(value)
}
},
Some(next) => match **next {
Nil => None,
Cons(ref value, ref tail) => {
self.next = Some(tail);
Some(value)
}
}
}
}
}
impl<T> List<T> {
/// Returns a forward iterator
pub fn iter<'a>(&'a self) -> Items<'a, T> {
Items {
head: self,
next: None
}
}
}
/**
* Left fold
*
* Applies `f` to `u` and the first element in the list, then applies `f` to
* the result of the previous call and the second element, and so on,
* returning the accumulated result.
*
* # Arguments
*
* * list - The list to fold
* * z - The initial value
* * f - The function to apply
*/
pub fn foldl<T:Clone,U>(z: T, list: @List<U>, f: |&T, &U| -> T) -> T {
let mut accum: T = z;
iter(list, |element| accum = f(&accum, element));
accum
}
/**
* Search for an element that matches a given predicate
*
* Apply function `f` to each element of `list`, starting from the first.
* When function `f` returns true then an option containing the element
* is returned. If `f` matches no elements then none is returned.
*/
pub fn find<T:Clone>(list: @List<T>, f: |&T| -> bool) -> Option<T> {
let mut list = list;
loop {
list = match *list {
Cons(ref head, tail) => {
if f(head) { return Some((*head).clone()); }
tail
}
Nil => return None
}
};
}
/**
* Returns true if a list contains an element that matches a given predicate
*
* Apply function `f` to each element of `list`, starting from the first.
* When function `f` returns true then it also returns true. If `f` matches no
* elements then false is returned.
*/
pub fn any<T>(list: @List<T>, f: |&T| -> bool) -> bool {
let mut list = list;
loop {
list = match *list {
Cons(ref head, tail) => {
if f(head) { return true; }
tail
}
Nil => return false
}
};
}
/// Returns true if a list contains an element with the given value
pub fn has<T:Eq>(list: @List<T>, element: T) -> bool {
let mut found = false;
each(list, |e| {
if *e == element { found = true; false } else { true }
});
return found;
}
/// Returns true if the list is empty
pub fn is_empty<T>(list: @List<T>) -> bool {
match *list {
Nil => true,
_ => false
}
}
/// Returns the length of a list
pub fn len<T>(list: @List<T>) -> uint {
let mut count = 0u;
iter(list, |_e| count += 1u);
count
}
/// Returns all but the first element of a list
pub fn tail<T>(list: @List<T>) -> @List<T> {
match *list {
Cons(_, tail) => return tail,
Nil => fail!("list empty")
}
}
/// Returns the first element of a list
pub fn head<T:Clone>(list: @List<T>) -> T {
match *list {
Cons(ref head, _) => (*head).clone(),
// makes me sad
_ => fail!("head invoked on empty list")
}
}
/// Appends one list to another
pub fn append<T:Clone + 'static>(list: @List<T>, other: @List<T>) -> @List<T> {
match *list {
Nil => return other,
Cons(ref head, tail) => {
let rest = append(tail, other);
return @Cons((*head).clone(), rest);
}
}
}
impl<T:'static + Clone> List<T> {
/// Create a list from a vector
pub fn from_vec(v: &[T]) -> List<T> {
match v.len() {
0 => Nil,
_ => v.rev_iter().fold(Nil, |tail, value: &T| Cons(value.clone(), @tail))
}
}
}
/*
/// Push one element into the front of a list, returning a new list
/// THIS VERSION DOESN'T ACTUALLY WORK
fn push<T:Clone>(ll: &mut @list<T>, vv: T) {
ll = &mut @cons(vv, *ll)
}
*/
/// Iterate over a list
pub fn iter<T>(list: @List<T>, f: |&T|) {
let mut cur = list;
loop {
cur = match *cur {
Cons(ref head, tail) => {
f(head);
tail
}
Nil => break
}
}
}
/// Iterate over a list
pub fn each<T>(list: @List<T>, f: |&T| -> bool) -> bool {
let mut cur = list;
loop {
cur = match *cur {
Cons(ref head, tail) => {
if !f(head) { return false; }
tail
}
Nil => { return true; }
}
}
}
#[cfg(test)]
mod tests {
use list::{List, Nil, head, is_empty, tail};
use list;
use std::option;
#[test]
fn test_iter() {
let list = List::from_vec([0, 1, 2]);
let mut iter = list.iter();
assert_eq!(&0, iter.next().unwrap());
assert_eq!(&1, iter.next().unwrap());
assert_eq!(&2, iter.next().unwrap());
assert_eq!(None, iter.next());
}
#[test]
fn test_is_empty() {
let empty : @list::List<int> = @List::from_vec([]);
let full1 = @List::from_vec([1]);
let full2 = @List::from_vec(['r', 'u']);
assert!(is_empty(empty));
assert!(!is_empty(full1));
assert!(!is_empty(full2));
}
#[test]
fn test_from_vec() {
let list = @List::from_vec([0, 1, 2]);
assert_eq!(head(list), 0);
let tail_l = tail(list);
assert_eq!(head(tail_l), 1);
let tail_tail_l = tail(tail_l);
assert_eq!(head(tail_tail_l), 2);
}
#[test]
fn test_from_vec_empty() {
let empty : list::List<int> = List::from_vec([]);
assert_eq!(empty, Nil::<int>);
}
#[test]
fn test_foldl() {
fn add(a: &uint, b: &int) -> uint { return *a + (*b as uint); }
let list = @List::from_vec([0, 1, 2, 3, 4]);
let empty = @list::Nil::<int>;
assert_eq!(list::foldl(0u, list, add), 10u);
assert_eq!(list::foldl(0u, empty, add), 0u);
}
#[test]
fn test_foldl2() {
fn sub(a: &int, b: &int) -> int {
*a - *b
}
let list = @List::from_vec([1, 2, 3, 4]);
assert_eq!(list::foldl(0, list, sub), -10);
}
#[test]
fn test_find_success() {
fn match_(i: &int) -> bool { return *i == 2; }
let list = @List::from_vec([0, 1, 2]);
assert_eq!(list::find(list, match_), option::Some(2));
}
#[test]
fn test_find_fail() {
fn match_(_i: &int) -> bool { return false; }
let list = @List::from_vec([0, 1, 2]);
let empty = @list::Nil::<int>;
assert_eq!(list::find(list, match_), option::None::<int>);
assert_eq!(list::find(empty, match_), option::None::<int>);
}
#[test]
fn test_any() {
fn match_(i: &int) -> bool { return *i == 2; }
let list = @List::from_vec([0, 1, 2]);
let empty = @list::Nil::<int>;
assert_eq!(list::any(list, match_), true);
assert_eq!(list::any(empty, match_), false);
}
#[test]
fn test_has() {
let list = @List::from_vec([5, 8, 6]);
let empty = @list::Nil::<int>;
assert!((list::has(list, 5)));
assert!((!list::has(list, 7)));
assert!((list::has(list, 8)));
assert!((!list::has(empty, 5)));
}
#[test]
fn test_len() {
let list = @List::from_vec([0, 1, 2]);
let empty = @list::Nil::<int>;
assert_eq!(list::len(list), 3u);
assert_eq!(list::len(empty), 0u);
}
#[test]
fn test_append() {
assert!(@List::from_vec([1,2,3,4])
== list::append(@List::from_vec([1,2]), @List::from_vec([3,4])));
}
}