Skip to content

Commit a417f2e

Browse files
committed
---
yaml --- r: 55701 b: refs/heads/master c: a0c2949 h: refs/heads/master i: 55699: fe5c7f0 v: v3
1 parent 18a75da commit a417f2e

File tree

3 files changed

+45
-60
lines changed

3 files changed

+45
-60
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: f2b0ef147a436f39d199a887e8b4c65f570a900d
2+
refs/heads/master: a0c2949e7c8c67fc7739482de8cee374a253b523
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 79a2b2eafc3c766cecec8a5f76317693bae9ed17
55
refs/heads/try: 8eb2bab100b42f0ba751552d8eff00eb2134c55a

trunk/src/libcore/iterator.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ pub trait IteratorUtil<A> {
3939
fn take_while<'r>(self, predicate: &'r fn(&A) -> bool) -> TakeWhileIterator<'r, A, Self>;
4040
fn skip(self, n: uint) -> SkipIterator<Self>;
4141
fn take(self, n: uint) -> TakeIterator<Self>;
42+
fn scan<'r, St, B>(self, initial_state: St, f: &'r fn(&mut St, A) -> Option<B>)
43+
-> ScanIterator<'r, A, B, Self, St>;
4244
fn advance(&mut self, f: &fn(A) -> bool);
4345
}
4446

@@ -93,6 +95,12 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
9395
TakeIterator{iter: self, n: n}
9496
}
9597

98+
#[inline(always)]
99+
fn scan<'r, St, B>(self, initial_state: St, f: &'r fn(&mut St, A) -> Option<B>)
100+
-> ScanIterator<'r, A, B, T, St> {
101+
ScanIterator{iter: self, f: f, state: initial_state}
102+
}
103+
96104
/// A shim implementing the `for` loop iteration protocol for iterator objects
97105
#[inline]
98106
fn advance(&mut self, f: &fn(A) -> bool) {
@@ -306,12 +314,13 @@ impl<A, T: Iterator<A>> Iterator<A> for TakeIterator<T> {
306314

307315
pub struct UnfoldrIterator<'self, A, St> {
308316
priv f: &'self fn(&mut St) -> Option<A>,
309-
priv state: St
317+
state: St
310318
}
311319

312320
pub impl<'self, A, St> UnfoldrIterator<'self, A, St> {
313321
#[inline]
314-
fn new(f: &'self fn(&mut St) -> Option<A>, initial_state: St) -> UnfoldrIterator<'self, A, St> {
322+
fn new(f: &'self fn(&mut St) -> Option<A>, initial_state: St)
323+
-> UnfoldrIterator<'self, A, St> {
315324
UnfoldrIterator {
316325
f: f,
317326
state: initial_state
@@ -326,6 +335,19 @@ impl<'self, A, St> Iterator<A> for UnfoldrIterator<'self, A, St> {
326335
}
327336
}
328337

338+
pub struct ScanIterator<'self, A, B, T, St> {
339+
priv iter: T,
340+
priv f: &'self fn(&mut St, A) -> Option<B>,
341+
state: St
342+
}
343+
344+
impl<'self, A, B, T: Iterator<A>, St> Iterator<B> for ScanIterator<'self, A, B, T, St> {
345+
#[inline]
346+
fn next(&mut self) -> Option<B> {
347+
self.iter.next().chain(|a| (self.f)(&mut self.state, a))
348+
}
349+
}
350+
329351
#[cfg(test)]
330352
mod tests {
331353
use super::*;
@@ -406,6 +428,25 @@ mod tests {
406428
assert_eq!(i, ys.len());
407429
}
408430

431+
#[test]
432+
fn test_iterator_scan() {
433+
// test the type inference
434+
fn add(old: &mut int, new: &uint) -> Option<float> {
435+
*old += *new as int;
436+
Some(*old as float)
437+
}
438+
let xs = [0u, 1, 2, 3, 4];
439+
let ys = [0f, 1f, 3f, 6f, 10f];
440+
441+
let mut it = xs.iter().scan(0, add);
442+
let mut i = 0;
443+
for it.advance |x| {
444+
assert_eq!(x, ys[i]);
445+
i += 1;
446+
}
447+
assert_eq!(i, ys.len());
448+
}
449+
409450
#[test]
410451
fn test_unfoldr() {
411452
fn count(st: &mut uint) -> Option<uint> {

trunk/src/libcore/sys.rs

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -83,24 +83,12 @@ pub fn get_type_desc<T>() -> *TypeDesc {
8383
unsafe { rusti::get_tydesc::<T>() as *TypeDesc }
8484
}
8585

86-
/// Returns a pointer to a type descriptor.
87-
#[inline(always)]
88-
pub fn get_type_desc_val<T>(_val: &T) -> *TypeDesc {
89-
get_type_desc::<T>()
90-
}
91-
9286
/// Returns the size of a type
9387
#[inline(always)]
9488
pub fn size_of<T>() -> uint {
9589
unsafe { rusti::size_of::<T>() }
9690
}
9791

98-
/// Returns the size of the type that `_val` points to
99-
#[inline(always)]
100-
pub fn size_of_val<T>(_val: &T) -> uint {
101-
size_of::<T>()
102-
}
103-
10492
/**
10593
* Returns the size of a type, or 1 if the actual size is zero.
10694
*
@@ -112,13 +100,6 @@ pub fn nonzero_size_of<T>() -> uint {
112100
if s == 0 { 1 } else { s }
113101
}
114102

115-
/// Returns the size of the type of the value that `_val` points to
116-
#[inline(always)]
117-
pub fn nonzero_size_of_val<T>(_val: &T) -> uint {
118-
nonzero_size_of::<T>()
119-
}
120-
121-
122103
/**
123104
* Returns the ABI-required minimum alignment of a type
124105
*
@@ -130,26 +111,12 @@ pub fn min_align_of<T>() -> uint {
130111
unsafe { rusti::min_align_of::<T>() }
131112
}
132113

133-
/// Returns the ABI-required minimum alignment of the type of the value that
134-
/// `_val` points to
135-
#[inline(always)]
136-
pub fn min_align_of_val<T>(_val: &T) -> uint {
137-
min_align_of::<T>()
138-
}
139-
140114
/// Returns the preferred alignment of a type
141115
#[inline(always)]
142116
pub fn pref_align_of<T>() -> uint {
143117
unsafe { rusti::pref_align_of::<T>() }
144118
}
145119

146-
/// Returns the preferred alignment of the type of the value that
147-
/// `_val` points to
148-
#[inline(always)]
149-
pub fn pref_align_of_val<T>(_val: &T) -> uint {
150-
pref_align_of::<T>()
151-
}
152-
153120
/// Returns the refcount of a shared box (as just before calling this)
154121
#[inline(always)]
155122
pub fn refcount<T>(t: @T) -> uint {
@@ -195,7 +162,7 @@ pub fn fail_assert(msg: &str, file: &str, line: uint) -> ! {
195162
#[cfg(test)]
196163
mod tests {
197164
use cast;
198-
use sys::*;
165+
use sys::{Closure, pref_align_of, size_of, nonzero_size_of};
199166
200167
#[test]
201168
fn size_of_basic() {
@@ -221,14 +188,6 @@ mod tests {
221188
assert!(size_of::<*uint>() == 8u);
222189
}
223190

224-
#[test]
225-
fn size_of_val_basic() {
226-
assert_eq!(size_of_val(&1u8), 1);
227-
assert_eq!(size_of_val(&1u16), 2);
228-
assert_eq!(size_of_val(&1u32), 4);
229-
assert_eq!(size_of_val(&1u64), 8);
230-
}
231-
232191
#[test]
233192
fn nonzero_size_of_basic() {
234193
type Z = [i8, ..0];
@@ -237,14 +196,6 @@ mod tests {
237196
assert!(nonzero_size_of::<uint>() == size_of::<uint>());
238197
}
239198

240-
#[test]
241-
fn nonzero_size_of_val_basic() {
242-
let z = [0u8, ..0];
243-
assert_eq!(size_of_val(&z), 0u);
244-
assert_eq!(nonzero_size_of_val(&z), 1u);
245-
assert_eq!(nonzero_size_of_val(&1u), size_of_val(&1u));
246-
}
247-
248199
#[test]
249200
fn align_of_basic() {
250201
assert!(pref_align_of::<u8>() == 1u);
@@ -268,13 +219,6 @@ mod tests {
268219
assert!(pref_align_of::<*uint>() == 8u);
269220
}
270221

271-
#[test]
272-
fn align_of_val_basic() {
273-
assert_eq!(pref_align_of_val(&1u8), 1u);
274-
assert_eq!(pref_align_of_val(&1u16), 2u);
275-
assert_eq!(pref_align_of_val(&1u32), 4u);
276-
}
277-
278222
#[test]
279223
fn synthesize_closure() {
280224
unsafe {

0 commit comments

Comments
 (0)