Skip to content

Commit 3ce9dba

Browse files
committed
std: Use the new for protocol
1 parent 2825605 commit 3ce9dba

File tree

9 files changed

+433
-51
lines changed

9 files changed

+433
-51
lines changed

src/libstd/bitv.rs

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,20 @@ pub impl BigBitv {
143143
}
144144

145145
#[inline(always)]
146+
#[cfg(stage0)]
146147
fn each_storage(&mut self, op: &fn(v: &mut uint) -> bool) {
147148
for uint::range(0, self.storage.len()) |i| {
148149
let mut w = self.storage[i];
149150
let b = op(&mut w);
150151
self.storage[i] = w;
151152
if !b { break; }
152153
}
153-
}
154+
}
155+
#[inline(always)]
156+
#[cfg(not(stage0))]
157+
fn each_storage(&mut self, op: &fn(v: &mut uint) -> bool) -> bool {
158+
uint::range(0, self.storage.len(), |i| op(&mut self.storage[i]))
159+
}
154160

155161
#[inline(always)]
156162
fn invert(&mut self) { for self.each_storage |w| { *w = !*w } }
@@ -193,6 +199,7 @@ pub impl BigBitv {
193199
}
194200

195201
#[inline(always)]
202+
#[cfg(stage0)]
196203
fn equals(&self, b: &BigBitv, nbits: uint) -> bool {
197204
let len = b.storage.len();
198205
for uint::iterate(0, len) |i| {
@@ -203,6 +210,19 @@ pub impl BigBitv {
203210
}
204211
}
205212

213+
#[inline(always)]
214+
#[cfg(not(stage0))]
215+
fn equals(&self, b: &BigBitv, nbits: uint) -> bool {
216+
let len = b.storage.len();
217+
for uint::iterate(0, len) |i| {
218+
let mask = big_mask(nbits, i);
219+
if mask & self.storage[i] != mask & b.storage[i] {
220+
return false;
221+
}
222+
}
223+
return true;
224+
}
225+
206226
}
207227

208228
enum BitvVariant { Big(~BigBitv), Small(~SmallBitv) }
@@ -387,13 +407,24 @@ pub impl Bitv {
387407
}
388408
389409
#[inline(always)]
410+
#[cfg(stage0)]
390411
fn each(&self, f: &fn(bool) -> bool) {
391412
let mut i = 0;
392413
while i < self.nbits {
393414
if !f(self.get(i)) { break; }
394415
i += 1;
395416
}
396417
}
418+
#[inline(always)]
419+
#[cfg(not(stage0))]
420+
fn each(&self, f: &fn(bool) -> bool) -> bool {
421+
let mut i = 0;
422+
while i < self.nbits {
423+
if !f(self.get(i)) { return false; }
424+
i += 1;
425+
}
426+
return true;
427+
}
397428
398429
/// Returns true if all bits are 0
399430
fn is_false(&self) -> bool {
@@ -488,13 +519,18 @@ pub impl Bitv {
488519
true
489520
}
490521
522+
#[cfg(stage0)]
491523
fn ones(&self, f: &fn(uint) -> bool) {
492524
for uint::range(0, self.nbits) |i| {
493525
if self.get(i) {
494526
if !f(i) { break }
495527
}
496528
}
497529
}
530+
#[cfg(not(stage0))]
531+
fn ones(&self, f: &fn(uint) -> bool) -> bool {
532+
uint::range(0, self.nbits, |i| !self.get(i) || f(i))
533+
}
498534
499535
}
500536
@@ -661,18 +697,21 @@ pub impl BitvSet {
661697
}
662698
}
663699
700+
#[cfg(not(stage0))]
664701
impl BaseIter<uint> for BitvSet {
665702
fn size_hint(&self) -> Option<uint> { Some(self.len()) }
666703
667-
fn each(&self, blk: &fn(v: &uint) -> bool) {
704+
fn each(&self, blk: &fn(v: &uint) -> bool) -> bool {
668705
for self.bitv.storage.eachi |i, &w| {
669706
if !iterate_bits(i * uint::bits, w, |b| blk(&b)) {
670-
return;
707+
return false;
671708
}
672709
}
710+
return true;
673711
}
674712
}
675713
714+
#[cfg(not(stage0))]
676715
impl cmp::Eq for BitvSet {
677716
fn eq(&self, other: &BitvSet) -> bool {
678717
if self.size != other.size {
@@ -706,6 +745,7 @@ impl Mutable for BitvSet {
706745
}
707746
}
708747
748+
#[cfg(not(stage0))]
709749
impl Set<uint> for BitvSet {
710750
fn contains(&self, value: &uint) -> bool {
711751
*value < self.bitv.storage.len() * uint::bits && self.bitv.get(*value)
@@ -773,64 +813,55 @@ impl Set<uint> for BitvSet {
773813
other.is_subset(self)
774814
}
775815
776-
fn difference(&self, other: &BitvSet, f: &fn(&uint) -> bool) {
816+
fn difference(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
777817
for self.each_common(other) |i, w1, w2| {
778818
if !iterate_bits(i, w1 & !w2, |b| f(&b)) {
779-
return;
819+
return false;
780820
}
781821
}
782822
/* everything we have that they don't also shows up */
783823
self.each_outlier(other, |mine, i, w|
784824
!mine || iterate_bits(i, w, |b| f(&b))
785-
);
825+
)
786826
}
787827
788828
fn symmetric_difference(&self, other: &BitvSet,
789-
f: &fn(&uint) -> bool) {
829+
f: &fn(&uint) -> bool) -> bool {
790830
for self.each_common(other) |i, w1, w2| {
791831
if !iterate_bits(i, w1 ^ w2, |b| f(&b)) {
792-
return;
832+
return false;
793833
}
794834
}
795-
self.each_outlier(other, |_, i, w|
796-
iterate_bits(i, w, |b| f(&b))
797-
);
835+
self.each_outlier(other, |_, i, w| iterate_bits(i, w, |b| f(&b)))
798836
}
799837
800-
fn intersection(&self, other: &BitvSet, f: &fn(&uint) -> bool) {
801-
for self.each_common(other) |i, w1, w2| {
802-
if !iterate_bits(i, w1 & w2, |b| f(&b)) {
803-
return;
804-
}
805-
}
838+
fn intersection(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
839+
self.each_common(other, |i, w1, w2| iterate_bits(i, w1 & w2, |b| f(&b)))
806840
}
807841
808-
fn union(&self, other: &BitvSet, f: &fn(&uint) -> bool) {
842+
fn union(&self, other: &BitvSet, f: &fn(&uint) -> bool) -> bool {
809843
for self.each_common(other) |i, w1, w2| {
810844
if !iterate_bits(i, w1 | w2, |b| f(&b)) {
811-
return;
845+
return false;
812846
}
813847
}
814-
self.each_outlier(other, |_, i, w|
815-
iterate_bits(i, w, |b| f(&b))
816-
);
848+
self.each_outlier(other, |_, i, w| iterate_bits(i, w, |b| f(&b)))
817849
}
818850
}
819851
852+
#[cfg(not(stage0))]
820853
priv impl BitvSet {
821854
/// Visits each of the words that the two bit vectors (self and other)
822855
/// both have in common. The three yielded arguments are (bit location,
823856
/// w1, w2) where the bit location is the number of bits offset so far,
824857
/// and w1/w2 are the words coming from the two vectors self, other.
825858
fn each_common(&self, other: &BitvSet,
826-
f: &fn(uint, uint, uint) -> bool) {
859+
f: &fn(uint, uint, uint) -> bool) -> bool {
827860
let min = uint::min(self.bitv.storage.len(),
828861
other.bitv.storage.len());
829-
for self.bitv.storage.slice(0, min).eachi |i, &w| {
830-
if !f(i * uint::bits, w, other.bitv.storage[i]) {
831-
return;
832-
}
833-
}
862+
self.bitv.storage.slice(0, min).eachi(|i, &w| {
863+
f(i * uint::bits, w, other.bitv.storage[i])
864+
})
834865
}
835866
836867
/// Visits each word in self or other that extends beyond the other. This
@@ -841,22 +872,23 @@ priv impl BitvSet {
841872
/// is true if the word comes from 'self', and false if it comes from
842873
/// 'other'.
843874
fn each_outlier(&self, other: &BitvSet,
844-
f: &fn(bool, uint, uint) -> bool) {
875+
f: &fn(bool, uint, uint) -> bool) -> bool {
845876
let len1 = self.bitv.storage.len();
846877
let len2 = other.bitv.storage.len();
847878
let min = uint::min(len1, len2);
848879
849880
/* only one of these loops will execute and that's the point */
850881
for self.bitv.storage.slice(min, len1).eachi |i, &w| {
851882
if !f(true, (i + min) * uint::bits, w) {
852-
return;
883+
return false;
853884
}
854885
}
855886
for other.bitv.storage.slice(min, len2).eachi |i, &w| {
856887
if !f(false, (i + min) * uint::bits, w) {
857-
return;
888+
return false;
858889
}
859890
}
891+
return true;
860892
}
861893
}
862894

src/libstd/deque.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,25 @@ pub impl<T> Deque<T> {
6363
}
6464

6565
/// Iterate over the elements in the deque
66+
#[cfg(stage0)]
6667
fn each(&self, f: &fn(&T) -> bool) {
6768
self.eachi(|_i, e| f(e))
6869
}
70+
/// Iterate over the elements in the deque
71+
#[cfg(not(stage0))]
72+
fn each(&self, f: &fn(&T) -> bool) -> bool {
73+
self.eachi(|_i, e| f(e))
74+
}
6975

7076
/// Iterate over the elements in the deque by index
77+
#[cfg(stage0)]
7178
fn eachi(&self, f: &fn(uint, &T) -> bool) {
72-
for uint::range(0, self.nelts) |i| {
73-
if !f(i, self.get(i as int)) { return; }
74-
}
79+
uint::range(0, self.nelts, |i| f(i, self.get(i as int)))
80+
}
81+
/// Iterate over the elements in the deque by index
82+
#[cfg(not(stage0))]
83+
fn eachi(&self, f: &fn(uint, &T) -> bool) -> bool {
84+
uint::range(0, self.nelts, |i| f(i, self.get(i as int)))
7585
}
7686

7787
/// Remove and return the first element in the deque

src/libstd/dlist.rs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -492,12 +492,13 @@ pub impl<T:Copy> DList<T> {
492492
493493
impl<T> BaseIter<T> for @mut DList<T> {
494494
/**
495-
* Iterates through the current contents.
496-
*
497-
* Attempts to access this dlist during iteration are allowed (to
498-
* allow for e.g. breadth-first search with in-place enqueues), but
499-
* removing the current node is forbidden.
500-
*/
495+
* Iterates through the current contents.
496+
*
497+
* Attempts to access this dlist during iteration are allowed (to
498+
* allow for e.g. breadth-first search with in-place enqueues), but
499+
* removing the current node is forbidden.
500+
*/
501+
#[cfg(stage0)]
501502
fn each(&self, f: &fn(v: &T) -> bool) {
502503
let mut link = self.peek_n();
503504
while link.is_some() {
@@ -525,6 +526,42 @@ impl<T> BaseIter<T> for @mut DList<T> {
525526
link = nobe.next_link();
526527
}
527528
}
529+
/**
530+
* Iterates through the current contents.
531+
*
532+
* Attempts to access this dlist during iteration are allowed (to
533+
* allow for e.g. breadth-first search with in-place enqueues), but
534+
* removing the current node is forbidden.
535+
*/
536+
#[cfg(not(stage0))]
537+
fn each(&self, f: &fn(v: &T) -> bool) -> bool {
538+
let mut link = self.peek_n();
539+
while link.is_some() {
540+
let nobe = link.get();
541+
assert!(nobe.linked);
542+
543+
{
544+
let frozen_nobe = &*nobe;
545+
if !f(&frozen_nobe.data) { return false; }
546+
}
547+
548+
// Check (weakly) that the user didn't do a remove.
549+
if self.size == 0 {
550+
fail!("The dlist became empty during iteration??")
551+
}
552+
if !nobe.linked ||
553+
(!((nobe.prev.is_some()
554+
|| managed::mut_ptr_eq(self.hd.expect(~"headless dlist?"),
555+
nobe))
556+
&& (nobe.next.is_some()
557+
|| managed::mut_ptr_eq(self.tl.expect(~"tailless dlist?"),
558+
nobe)))) {
559+
fail!("Removing a dlist node during iteration is forbidden!")
560+
}
561+
link = nobe.next_link();
562+
}
563+
return true;
564+
}
528565

529566
#[inline(always)]
530567
fn size_hint(&self) -> Option<uint> { Some(self.len()) }

src/libstd/list.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ pub fn iter<T>(l: @List<T>, f: &fn(&T)) {
140140
}
141141

142142
/// Iterate over a list
143+
#[cfg(stage0)]
143144
pub fn each<T>(l: @List<T>, f: &fn(&T) -> bool) {
144145
let mut cur = l;
145146
loop {
@@ -152,9 +153,24 @@ pub fn each<T>(l: @List<T>, f: &fn(&T) -> bool) {
152153
}
153154
}
154155
}
156+
/// Iterate over a list
157+
#[cfg(not(stage0))]
158+
pub fn each<T>(l: @List<T>, f: &fn(&T) -> bool) -> bool {
159+
let mut cur = l;
160+
loop {
161+
cur = match *cur {
162+
Cons(ref hd, tl) => {
163+
if !f(hd) { return false; }
164+
tl
165+
}
166+
Nil => { return true; }
167+
}
168+
}
169+
}
155170

156171
impl<T> MutList<T> {
157172
/// Iterate over a mutable list
173+
#[cfg(stage0)]
158174
pub fn each(@mut self, f: &fn(&mut T) -> bool) {
159175
let mut cur = self;
160176
loop {
@@ -170,6 +186,24 @@ impl<T> MutList<T> {
170186
}
171187
}
172188
}
189+
/// Iterate over a mutable list
190+
#[cfg(not(stage0))]
191+
pub fn each(@mut self, f: &fn(&mut T) -> bool) -> bool {
192+
let mut cur = self;
193+
loop {
194+
let borrowed = &mut *cur;
195+
cur = match *borrowed {
196+
MutCons(ref mut hd, tl) => {
197+
if !f(hd) {
198+
return false;
199+
}
200+
tl
201+
}
202+
MutNil => break
203+
}
204+
}
205+
return true;
206+
}
173207
}
174208

175209
#[cfg(test)]

0 commit comments

Comments
 (0)