Skip to content

Rename Invert to Flip - Issue #10632 #11686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions doc/guide-container.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ The `DoubleEndedIterator` trait represents an iterator able to yield elements
from either end of a range. It inherits from the `Iterator` trait and extends
it with the `next_back` function.

A `DoubleEndedIterator` can be flipped with the `invert` adaptor, returning
another `DoubleEndedIterator` with `next` and `next_back` exchanged.
A `DoubleEndedIterator` can have its direction changed with the `rev` adaptor,
returning another `DoubleEndedIterator` with `next` and `next_back` exchanged.

~~~
let xs = [1, 2, 3, 4, 5, 6];
Expand All @@ -347,7 +347,7 @@ println!("{:?}", it.next()); // prints `Some(&2)`
println!("{:?}", it.next_back()); // prints `Some(&6)`

// prints `5`, `4` and `3`
for &x in it.invert() {
for &x in it.rev() {
println!("{}", x)
}
~~~
Expand All @@ -366,7 +366,7 @@ let mut it = xs.iter().chain(ys.iter()).map(|&x| x * 2);
println!("{:?}", it.next()); // prints `Some(2)`

// prints `16`, `14`, `12`, `10`, `8`, `6`, `4`
for x in it.invert() {
for x in it.rev() {
println!("{}", x);
}
~~~
Expand Down
8 changes: 4 additions & 4 deletions src/libextra/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use std::cmp;
use std::iter::RandomAccessIterator;
use std::iter::{Invert, Enumerate, Repeat, Map, Zip};
use std::iter::{Rev, Enumerate, Repeat, Map, Zip};
use std::num;
use std::ops;
use std::uint;
Expand Down Expand Up @@ -387,7 +387,7 @@ impl Bitv {
}
}

/// Invert all bits
/// Flip all bits
#[inline]
pub fn negate(&mut self) {
match self.rep {
Expand Down Expand Up @@ -428,8 +428,8 @@ impl Bitv {
}

#[inline]
pub fn rev_iter<'a>(&'a self) -> Invert<Bits<'a>> {
self.iter().invert()
pub fn rev_iter<'a>(&'a self) -> Rev<Bits<'a>> {
self.iter().rev()
}

/// Returns `true` if all bits are 0
Expand Down
14 changes: 7 additions & 7 deletions src/libextra/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use std::cast;
use std::ptr;
use std::util;
use std::iter::Invert;
use std::iter::Rev;
use std::iter;

use container::Deque;
Expand Down Expand Up @@ -368,8 +368,8 @@ impl<T> DList<T> {

/// Provide a reverse iterator
#[inline]
pub fn rev_iter<'a>(&'a self) -> Invert<Items<'a, T>> {
self.iter().invert()
pub fn rev_iter<'a>(&'a self) -> Rev<Items<'a, T>> {
self.iter().rev()
}

/// Provide a forward iterator with mutable references
Expand All @@ -388,8 +388,8 @@ impl<T> DList<T> {
}
/// Provide a reverse iterator with mutable references
#[inline]
pub fn mut_rev_iter<'a>(&'a mut self) -> Invert<MutItems<'a, T>> {
self.mut_iter().invert()
pub fn mut_rev_iter<'a>(&'a mut self) -> Rev<MutItems<'a, T>> {
self.mut_iter().rev()
}


Expand All @@ -401,8 +401,8 @@ impl<T> DList<T> {

/// Consume the list into an iterator yielding elements by value, in reverse
#[inline]
pub fn move_rev_iter(self) -> Invert<MoveItems<T>> {
self.move_iter().invert()
pub fn move_rev_iter(self) -> Rev<MoveItems<T>> {
self.move_iter().rev()
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/libextra/ringbuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use std::num;
use std::vec;
use std::iter::{Invert, RandomAccessIterator};
use std::iter::{Rev, RandomAccessIterator};

use container::Deque;

Expand Down Expand Up @@ -192,8 +192,8 @@ impl<T> RingBuf<T> {
}

/// Back-to-front iterator.
pub fn rev_iter<'a>(&'a self) -> Invert<Items<'a, T>> {
self.iter().invert()
pub fn rev_iter<'a>(&'a self) -> Rev<Items<'a, T>> {
self.iter().rev()
}

/// Front-to-back iterator which returns mutable values.
Expand Down Expand Up @@ -223,8 +223,8 @@ impl<T> RingBuf<T> {
}

/// Back-to-front iterator which returns mutable values.
pub fn mut_rev_iter<'a>(&'a mut self) -> Invert<MutItems<'a, T>> {
self.mut_iter().invert()
pub fn mut_rev_iter<'a>(&'a mut self) -> Rev<MutItems<'a, T>> {
self.mut_iter().rev()
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/libextra/smallintmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#[allow(missing_doc)];

use std::iter::{Enumerate, FilterMap, Invert};
use std::iter::{Enumerate, FilterMap, Rev};
use std::util::replace;
use std::vec;

Expand Down Expand Up @@ -140,14 +140,14 @@ impl<V> SmallIntMap<V> {
/// An iterator visiting all key-value pairs in descending order by the keys.
/// Iterator element type is (uint, &'r V)
pub fn rev_iter<'r>(&'r self) -> RevEntries<'r, V> {
self.iter().invert()
self.iter().rev()
}

/// An iterator visiting all key-value pairs in descending order by the keys,
/// with mutable references to the values
/// Iterator element type is (uint, &'r mut V)
pub fn mut_rev_iter<'r>(&'r mut self) -> RevMutEntries <'r, V> {
self.mut_iter().invert()
self.mut_iter().rev()
}

/// Empties the hash map, moving all values into the specified closure
Expand Down Expand Up @@ -241,7 +241,7 @@ pub struct Entries<'a, T> {

iterator!(impl Entries -> (uint, &'a T), get_ref)
double_ended_iterator!(impl Entries -> (uint, &'a T), get_ref)
pub type RevEntries<'a, T> = Invert<Entries<'a, T>>;
pub type RevEntries<'a, T> = Rev<Entries<'a, T>>;

pub struct MutEntries<'a, T> {
priv front: uint,
Expand All @@ -251,7 +251,7 @@ pub struct MutEntries<'a, T> {

iterator!(impl MutEntries -> (uint, &'a mut T), get_mut_ref)
double_ended_iterator!(impl MutEntries -> (uint, &'a mut T), get_mut_ref)
pub type RevMutEntries<'a, T> = Invert<MutEntries<'a, T>>;
pub type RevMutEntries<'a, T> = Rev<MutEntries<'a, T>>;

#[cfg(test)]
mod test_map {
Expand Down
2 changes: 1 addition & 1 deletion src/libnative/io/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ fn spawn_process_os(prog: &str, args: &[~str],
fail!("failure in dup3(err_fd, 2): {}", os::last_os_error());
}
// close all other fds
for fd in range(3, getdtablesize()).invert() {
for fd in range(3, getdtablesize()).rev() {
if fd != output.fd() {
close(fd as c_int);
}
Expand Down
14 changes: 7 additions & 7 deletions src/librustc/middle/trans/cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl<'a> CleanupMethods<'a> for FunctionContext<'a> {
*/

let scopes = self.scopes.borrow();
for scope in scopes.get().iter().invert() {
for scope in scopes.get().iter().rev() {
match scope.kind {
LoopScopeKind(id, _) => {
return id;
Expand Down Expand Up @@ -325,7 +325,7 @@ impl<'a> CleanupMethods<'a> for FunctionContext<'a> {
cleanup_scope);

let mut scopes = self.scopes.borrow_mut();
for scope in scopes.get().mut_iter().invert() {
for scope in scopes.get().mut_iter().rev() {
if scope.kind.is_ast_with_id(cleanup_scope) {
scope.cleanups.push(cleanup);
scope.clear_cached_exits();
Expand Down Expand Up @@ -368,7 +368,7 @@ impl<'a> CleanupMethods<'a> for FunctionContext<'a> {
*/

let scopes = self.scopes.borrow();
scopes.get().iter().invert().any(|s| s.needs_invoke())
scopes.get().iter().rev().any(|s| s.needs_invoke())
}

fn get_landing_pad(&self) -> BasicBlockRef {
Expand Down Expand Up @@ -415,7 +415,7 @@ impl<'a> CleanupHelperMethods<'a> for FunctionContext<'a> {
* Returns the id of the current top-most AST scope, if any.
*/
let scopes = self.scopes.borrow();
for scope in scopes.get().iter().invert() {
for scope in scopes.get().iter().rev() {
match scope.kind {
CustomScopeKind | LoopScopeKind(..) => {}
AstScopeKind(i) => {
Expand All @@ -428,7 +428,7 @@ impl<'a> CleanupHelperMethods<'a> for FunctionContext<'a> {

fn top_nonempty_cleanup_scope(&self) -> Option<uint> {
let scopes = self.scopes.borrow();
scopes.get().iter().invert().position(|s| !s.cleanups.is_empty())
scopes.get().iter().rev().position(|s| !s.cleanups.is_empty())
}

fn is_valid_to_pop_custom_scope(&self, custom_scope: CustomScopeIndex) -> bool {
Expand All @@ -450,7 +450,7 @@ impl<'a> CleanupHelperMethods<'a> for FunctionContext<'a> {

let mut bcx = bcx;
if !bcx.unreachable.get() {
for cleanup in scope.cleanups.iter().invert() {
for cleanup in scope.cleanups.iter().rev() {
bcx = cleanup.trans(bcx);
}
}
Expand Down Expand Up @@ -619,7 +619,7 @@ impl<'a> CleanupHelperMethods<'a> for FunctionContext<'a> {
debug!("generating cleanups for {}", name);
let bcx_in = self.new_block(label.is_unwind(), name, None);
let mut bcx_out = bcx_in;
for cleanup in scope.cleanups.iter().invert() {
for cleanup in scope.cleanups.iter().rev() {
if cleanup_is_suitable_for(*cleanup, label) {
bcx_out = cleanup.trans(bcx_out);
}
Expand Down
54 changes: 27 additions & 27 deletions src/libstd/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,21 +670,21 @@ pub trait DoubleEndedIterator<A>: Iterator<A> {
/// Yield an element from the end of the range, returning `None` if the range is empty.
fn next_back(&mut self) -> Option<A>;

/// Flip the direction of the iterator
/// Change the direction of the iterator
///
/// The inverted iterator flips the ends on an iterator that can already
/// The flipped iterator swaps the ends on an iterator that can already
/// be iterated from the front and from the back.
///
///
/// If the iterator also implements RandomAccessIterator, the inverted
/// If the iterator also implements RandomAccessIterator, the flipped
/// iterator is also random access, with the indices starting at the back
/// of the original iterator.
///
/// Note: Random access with inverted indices still only applies to the first
/// Note: Random access with flipped indices still only applies to the first
/// `uint::max_value` elements of the original iterator.
#[inline]
fn invert(self) -> Invert<Self> {
Invert{iter: self}
fn rev(self) -> Rev<Self> {
Rev{iter: self}
}
}

Expand Down Expand Up @@ -759,30 +759,30 @@ pub trait ExactSize<A> : DoubleEndedIterator<A> {
// Adaptors that may overflow in `size_hint` are not, i.e. `Chain`.
impl<A, T: ExactSize<A>> ExactSize<(uint, A)> for Enumerate<T> {}
impl<'a, A, T: ExactSize<A>> ExactSize<A> for Inspect<'a, A, T> {}
impl<A, T: ExactSize<A>> ExactSize<A> for Invert<T> {}
impl<A, T: ExactSize<A>> ExactSize<A> for Rev<T> {}
impl<'a, A, B, T: ExactSize<A>> ExactSize<B> for Map<'a, A, B, T> {}
impl<A, B, T: ExactSize<A>, U: ExactSize<B>> ExactSize<(A, B)> for Zip<T, U> {}

/// An double-ended iterator with the direction inverted
#[deriving(Clone)]
pub struct Invert<T> {
pub struct Rev<T> {
priv iter: T
}

impl<A, T: DoubleEndedIterator<A>> Iterator<A> for Invert<T> {
impl<A, T: DoubleEndedIterator<A>> Iterator<A> for Rev<T> {
#[inline]
fn next(&mut self) -> Option<A> { self.iter.next_back() }
#[inline]
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
}

impl<A, T: DoubleEndedIterator<A>> DoubleEndedIterator<A> for Invert<T> {
impl<A, T: DoubleEndedIterator<A>> DoubleEndedIterator<A> for Rev<T> {
#[inline]
fn next_back(&mut self) -> Option<A> { self.iter.next() }
}

impl<A, T: DoubleEndedIterator<A> + RandomAccessIterator<A>> RandomAccessIterator<A>
for Invert<T> {
for Rev<T> {
#[inline]
fn indexable(&self) -> uint { self.iter.indexable() }
#[inline]
Expand Down Expand Up @@ -2590,12 +2590,12 @@ mod tests {
}

#[test]
fn test_invert() {
fn test_rev() {
let xs = [2, 4, 6, 8, 10, 12, 14, 16];
let mut it = xs.iter();
it.next();
it.next();
assert_eq!(it.invert().map(|&x| x).collect::<~[int]>(), ~[16, 14, 12, 10, 8, 6]);
assert_eq!(it.rev().map(|&x| x).collect::<~[int]>(), ~[16, 14, 12, 10, 8, 6]);
}

#[test]
Expand Down Expand Up @@ -2662,7 +2662,7 @@ mod tests {
fn test_double_ended_chain() {
let xs = [1, 2, 3, 4, 5];
let ys = ~[7, 9, 11];
let mut it = xs.iter().chain(ys.iter()).invert();
let mut it = xs.iter().chain(ys.iter()).rev();
assert_eq!(it.next().unwrap(), &11)
assert_eq!(it.next().unwrap(), &9)
assert_eq!(it.next_back().unwrap(), &1)
Expand Down Expand Up @@ -2764,10 +2764,10 @@ mod tests {
}

#[test]
fn test_random_access_invert() {
fn test_random_access_rev() {
let xs = [1, 2, 3, 4, 5];
check_randacc_iter(xs.iter().invert(), xs.len());
let mut it = xs.iter().invert();
check_randacc_iter(xs.iter().rev(), xs.len());
let mut it = xs.iter().rev();
it.next();
it.next_back();
it.next();
Expand Down Expand Up @@ -2833,13 +2833,13 @@ mod tests {

#[test]
fn test_double_ended_range() {
assert_eq!(range(11i, 14).invert().collect::<~[int]>(), ~[13i, 12, 11]);
for _ in range(10i, 0).invert() {
assert_eq!(range(11i, 14).rev().collect::<~[int]>(), ~[13i, 12, 11]);
for _ in range(10i, 0).rev() {
fail!("unreachable");
}

assert_eq!(range(11u, 14).invert().collect::<~[uint]>(), ~[13u, 12, 11]);
for _ in range(10u, 0).invert() {
assert_eq!(range(11u, 14).rev().collect::<~[uint]>(), ~[13u, 12, 11]);
for _ in range(10u, 0).rev() {
fail!("unreachable");
}
}
Expand Down Expand Up @@ -2886,11 +2886,11 @@ mod tests {

assert_eq!(range(0i, 5).collect::<~[int]>(), ~[0i, 1, 2, 3, 4]);
assert_eq!(range(-10i, -1).collect::<~[int]>(), ~[-10, -9, -8, -7, -6, -5, -4, -3, -2]);
assert_eq!(range(0i, 5).invert().collect::<~[int]>(), ~[4, 3, 2, 1, 0]);
assert_eq!(range(0i, 5).rev().collect::<~[int]>(), ~[4, 3, 2, 1, 0]);
assert_eq!(range(200, -5).collect::<~[int]>(), ~[]);
assert_eq!(range(200, -5).invert().collect::<~[int]>(), ~[]);
assert_eq!(range(200, -5).rev().collect::<~[int]>(), ~[]);
assert_eq!(range(200, 200).collect::<~[int]>(), ~[]);
assert_eq!(range(200, 200).invert().collect::<~[int]>(), ~[]);
assert_eq!(range(200, 200).rev().collect::<~[int]>(), ~[]);

assert_eq!(range(0i, 100).size_hint(), (100, Some(100)));
// this test is only meaningful when sizeof uint < sizeof u64
Expand All @@ -2902,11 +2902,11 @@ mod tests {
#[test]
fn test_range_inclusive() {
assert_eq!(range_inclusive(0i, 5).collect::<~[int]>(), ~[0i, 1, 2, 3, 4, 5]);
assert_eq!(range_inclusive(0i, 5).invert().collect::<~[int]>(), ~[5i, 4, 3, 2, 1, 0]);
assert_eq!(range_inclusive(0i, 5).rev().collect::<~[int]>(), ~[5i, 4, 3, 2, 1, 0]);
assert_eq!(range_inclusive(200, -5).collect::<~[int]>(), ~[]);
assert_eq!(range_inclusive(200, -5).invert().collect::<~[int]>(), ~[]);
assert_eq!(range_inclusive(200, -5).rev().collect::<~[int]>(), ~[]);
assert_eq!(range_inclusive(200, 200).collect::<~[int]>(), ~[200]);
assert_eq!(range_inclusive(200, 200).invert().collect::<~[int]>(), ~[200]);
assert_eq!(range_inclusive(200, 200).rev().collect::<~[int]>(), ~[200]);
}

#[test]
Expand Down
Loading