Skip to content

some vector cleanup #7149

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

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion doc/tutorial-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ fn pnorm(nums: &~[float], p: uint) -> float {

fn main() {
let numbers = vec::from_fn(1000000, |_| rand::random::<float>());
println(fmt!("Inf-norm = %?", numbers.max()));
println(fmt!("Inf-norm = %?", *numbers.iter().max().unwrap()));

let numbers_arc = ARC(numbers);

Expand Down
9 changes: 2 additions & 7 deletions doc/tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -2039,17 +2039,12 @@ themselves contain type parameters. A trait for generalized sequence
types might look like the following:

~~~~
# use std::vec;
trait Seq<T> {
fn len(&self) -> uint;
fn iter(&self, b: &fn(v: &T));
fn length(&self) -> uint;
}

impl<T> Seq<T> for ~[T] {
fn len(&self) -> uint { self.len() }
fn iter(&self, b: &fn(v: &T)) {
for vec::each(*self) |elt| { b(elt); }
}
fn length(&self) -> uint { self.len() }
}
~~~~

Expand Down
8 changes: 3 additions & 5 deletions src/libextra/flatpipes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,12 +583,12 @@ pub mod bytepipes {

impl BytePort for PipeBytePort {
fn try_recv(&self, count: uint) -> Option<~[u8]> {
if vec::uniq_len(&const *self.buf) >= count {
if self.buf.len() >= count {
let mut bytes = ::core::util::replace(&mut *self.buf, ~[]);
*self.buf = bytes.slice(count, bytes.len()).to_owned();
bytes.truncate(count);
return Some(bytes);
} else if vec::uniq_len(&const *self.buf) > 0 {
} else if !self.buf.is_empty() {
let mut bytes = ::core::util::replace(&mut *self.buf, ~[]);
assert!(count > bytes.len());
match self.try_recv(count - bytes.len()) {
Expand All @@ -598,7 +598,7 @@ pub mod bytepipes {
}
None => return None
}
} else if vec::uniq_len(&const *self.buf) == 0 {
} else /* empty */ {
match self.port.try_recv() {
Some(buf) => {
assert!(!buf.is_empty());
Expand All @@ -607,8 +607,6 @@ pub mod bytepipes {
}
None => return None
}
} else {
::core::util::unreachable()
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/libextra/net_tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,8 +879,7 @@ impl io::Reader for TcpSocketBuf {

// If possible, copy up to `len` bytes from the internal
// `data.buf` into `buf`
let nbuffered = vec::uniq_len(&const self.data.buf) -
self.data.buf_off;
let nbuffered = self.data.buf.len() - self.data.buf_off;
let needed = len - count;
if nbuffered > 0 {
unsafe {
Expand Down Expand Up @@ -934,7 +933,7 @@ impl io::Reader for TcpSocketBuf {
}
fn read_byte(&self) -> int {
loop {
if vec::uniq_len(&const self.data.buf) > self.data.buf_off {
if self.data.buf.len() > self.data.buf_off {
let c = self.data.buf[self.data.buf_off];
self.data.buf_off += 1;
return c as int
Expand Down
4 changes: 2 additions & 2 deletions src/libextra/priority_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ impl<T:Ord> BaseIter<T> for PriorityQueue<T> {

impl<T:Ord> Container for PriorityQueue<T> {
/// Returns the length of the queue
fn len(&const self) -> uint { vec::uniq_len(&const self.data) }
fn len(&self) -> uint { self.data.len() }

/// Returns true if a queue contains no elements
fn is_empty(&const self) -> bool { self.len() == 0 }
fn is_empty(&self) -> bool { self.len() == 0 }
}

impl<T:Ord> Mutable for PriorityQueue<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/sha1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn sha1() -> @Sha1 {
}
fn process_msg_block(st: &mut Sha1State) {
assert_eq!(st.h.len(), digest_buf_len);
assert_eq!(vec::uniq_len(st.work_buf), work_buf_len);
assert_eq!(st.work_buf.len(), work_buf_len);
let mut t: int; // Loop counter
let w = st.work_buf;

Expand Down
10 changes: 5 additions & 5 deletions src/libextra/smallintmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ pub struct SmallIntMap<T> {

impl<V> Container for SmallIntMap<V> {
/// Return the number of elements in the map
fn len(&const self) -> uint {
fn len(&self) -> uint {
let mut sz = 0;
for uint::range(0, vec::uniq_len(&const self.v)) |i| {
for uint::range(0, self.v.len()) |i| {
match self.v[i] {
Some(_) => sz += 1,
None => {}
Expand All @@ -44,7 +44,7 @@ impl<V> Container for SmallIntMap<V> {
}

/// Return true if the map contains no elements
fn is_empty(&const self) -> bool { self.len() == 0 }
fn is_empty(&self) -> bool { self.len() == 0 }
}

impl<V> Mutable for SmallIntMap<V> {
Expand Down Expand Up @@ -199,12 +199,12 @@ pub struct SmallIntSet {

impl Container for SmallIntSet {
/// Return the number of elements in the map
fn len(&const self) -> uint {
fn len(&self) -> uint {
self.map.len()
}

/// Return true if the map contains no elements
fn is_empty(&const self) -> bool { self.len() == 0 }
fn is_empty(&self) -> bool { self.len() == 0 }
}

impl Mutable for SmallIntSet {
Expand Down
2 changes: 1 addition & 1 deletion src/libextra/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ pub fn run_tests_console(opts: &TestOpts,
fn print_failures(st: &ConsoleTestState) {
st.out.write_line("\nfailures:");
let mut failures = ~[];
for uint::range(0, vec::uniq_len(&const st.failures)) |i| {
for uint::range(0, st.failures.len()) |i| {
let name = copy st.failures[i].name;
failures.push(name.to_str());
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/trans/adt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ fn represent_type_uncached(cx: &mut CrateContext, t: ty::t) -> Repr {
if cases.all(|c| c.tys.len() == 0) {
// All bodies empty -> intlike
let discrs = cases.map(|c| c.discr);
return CEnum(discrs.min(), discrs.max());
return CEnum(*discrs.iter().min().unwrap(), *discrs.iter().max().unwrap());
}

if cases.len() == 1 {
Expand Down Expand Up @@ -509,7 +509,7 @@ pub fn trans_const(ccx: &mut CrateContext, r: &Repr, discr: int,
}
General(ref cases) => {
let case = &cases[discr as uint];
let max_sz = cases.map(|s| s.size).max();
let max_sz = cases.iter().transform(|x| x.size).max().unwrap();
let discr_ty = C_int(ccx, discr);
let contents = build_const_struct(ccx, case,
~[discr_ty] + vals);
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/comm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl<T: Owned> Peekable<T> for PortSet<T> {
fn peek(&self) -> bool {
// It'd be nice to use self.port.each, but that version isn't
// pure.
for uint::range(0, vec::uniq_len(&const self.ports)) |i| {
for uint::range(0, self.ports.len()) |i| {
let port: &pipesy::Port<T> = &self.ports[i];
if port.peek() {
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ use option::Option;
/// knowledge known is the number of elements contained within.
pub trait Container {
/// Return the number of elements in the container
fn len(&const self) -> uint;
fn len(&self) -> uint;

/// Return true if the container contains no elements
fn is_empty(&const self) -> bool;
fn is_empty(&self) -> bool;
}

/// A trait to represent mutable containers
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1667,7 +1667,7 @@ impl Writer for BytesWriter {

fn seek(&self, offset: int, whence: SeekStyle) {
let pos = *self.pos;
let len = vec::uniq_len(&const *self.bytes);
let len = self.bytes.len();
*self.pos = seek_in_buf(offset, pos, len, whence);
}

Expand Down
39 changes: 0 additions & 39 deletions src/libstd/old_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ pub trait CopyableIter<A:Copy> {
fn find(&self, p: &fn(&A) -> bool) -> Option<A>;
}

pub trait CopyableOrderedIter<A:Copy + Ord> {
fn min(&self) -> A;
fn max(&self) -> A;
}

// A trait for sequences that can be built by imperatively pushing elements
// onto them.
pub trait Buildable<A> {
Expand Down Expand Up @@ -192,40 +187,6 @@ pub fn position<A,IA:BaseIter<A>>(this: &IA, f: &fn(&A) -> bool)
return None;
}

// note: 'rposition' would only make sense to provide with a bidirectional
// iter interface, such as would provide "reach" in addition to "each". As is,
// it would have to be implemented with foldr, which is too inefficient.

#[inline(always)]
pub fn min<A:Copy + Ord,IA:BaseIter<A>>(this: &IA) -> A {
match do foldl::<A,Option<A>,IA>(this, None) |a, b| {
match a {
&Some(ref a_) if *a_ < *b => {
*(a)
}
_ => Some(*b)
}
} {
Some(val) => val,
None => fail!("min called on empty iterator")
}
}

#[inline(always)]
pub fn max<A:Copy + Ord,IA:BaseIter<A>>(this: &IA) -> A {
match do foldl::<A,Option<A>,IA>(this, None) |a, b| {
match a {
&Some(ref a_) if *a_ > *b => {
*(a)
}
_ => Some(*b)
}
} {
Some(val) => val,
None => fail!("max called on empty iterator")
}
}

#[inline(always)]
pub fn find<A:Copy,IA:BaseIter<A>>(this: &IA, f: &fn(&A) -> bool)
-> Option<A> {
Expand Down
7 changes: 4 additions & 3 deletions src/libstd/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1685,10 +1685,11 @@ mod tests {
assert!((ostream as uint != 0u));
let s = ~"hello";
let mut buf = s.as_bytes_with_null().to_owned();
let len = buf.len();
do vec::as_mut_buf(buf) |b, _len| {
assert!((libc::fwrite(b as *c_void, 1u as size_t,
(s.len() + 1u) as size_t, ostream)
== buf.len() as size_t))
assert_eq!(libc::fwrite(b as *c_void, 1u as size_t,
(s.len() + 1u) as size_t, ostream),
len as size_t)
}
assert_eq!(libc::fclose(ostream), (0u as c_int));
let in_mode = in.get_mode();
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ pub use char::Char;
pub use container::{Container, Mutable, Map, Set};
pub use hash::Hash;
pub use old_iter::{BaseIter, ReverseIter, ExtendedIter, EqIter};
pub use old_iter::{CopyableIter, CopyableOrderedIter};
pub use old_iter::CopyableIter;
pub use iter::{Times, FromIter};
pub use iterator::{Iterator, IteratorUtil};
pub use iterator::{Iterator, IteratorUtil, OrdIterator};
pub use num::{Num, NumCast};
pub use num::{Orderable, Signed, Unsigned, Round};
pub use num::{Algebraic, Trigonometric, Exponential, Hyperbolic};
Expand Down
5 changes: 3 additions & 2 deletions src/libstd/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ More runtime type reflection

use cast::transmute;
use char;
use container::Container;
use intrinsic;
use intrinsic::{TyDesc, TyVisitor, visit_tydesc};
use intrinsic::Opaque;
Expand Down Expand Up @@ -502,7 +503,7 @@ impl TyVisitor for ReprVisitor {
_offset: uint,
inner: *TyDesc)
-> bool {
match self.var_stk[vec::uniq_len(&const *self.var_stk) - 1] {
match self.var_stk[self.var_stk.len() - 1] {
Matched => {
if i != 0 {
self.writer.write_str(", ");
Expand All @@ -520,7 +521,7 @@ impl TyVisitor for ReprVisitor {
_disr_val: int,
n_fields: uint,
_name: &str) -> bool {
match self.var_stk[vec::uniq_len(&const *self.var_stk) - 1] {
match self.var_stk[self.var_stk.len() - 1] {
Matched => {
if n_fields > 0 {
self.writer.write_char(')');
Expand Down
3 changes: 2 additions & 1 deletion src/libstd/rt/io/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,8 @@ impl<T: Reader> ReaderUtil for T {

do (|| {
while total_read < len {
let slice = vec::mut_slice(*buf, start_len + total_read, buf.len());
let len = buf.len();
let slice = vec::mut_slice(*buf, start_len + total_read, len);
match self.read(slice) {
Some(nread) => {
total_read += nread;
Expand Down
45 changes: 16 additions & 29 deletions src/libstd/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,6 @@ pub fn capacity<T>(v: &const ~[T]) -> uint {
}
}

// A botch to tide us over until core and std are fully demuted.
#[allow(missing_doc)]
pub fn uniq_len<T>(v: &const ~[T]) -> uint {
unsafe {
let v: &~[T] = transmute(v);
as_const_buf(*v, |_p, len| len)
}
}

/**
* Creates and initializes an owned vector.
*
Expand Down Expand Up @@ -1767,19 +1758,32 @@ pub mod traits {
}
}

impl<'self,T> Container for &'self const [T] {
impl<'self, T> Container for &'self const [T] {
/// Returns true if a vector contains no elements
#[inline]
fn is_empty(&const self) -> bool {
fn is_empty(&self) -> bool {
as_const_buf(*self, |_p, len| len == 0u)
}

/// Returns the length of a vector
#[inline]
fn len(&const self) -> uint {
fn len(&self) -> uint {
as_const_buf(*self, |_p, len| len)
}
}

impl<T> Container for ~[T] {
/// Returns true if a vector contains no elements
#[inline]
fn is_empty(&self) -> bool {
as_const_buf(*self, |_p, len| len == 0u)
}

/// Returns the length of a vector
#[inline]
fn len(&self) -> uint {
as_const_buf(*self, |_p, len| len)
}
}

#[allow(missing_doc)]
Expand Down Expand Up @@ -2615,23 +2619,6 @@ impl<A:Copy> old_iter::CopyableIter<A> for @[A] {
}
}

impl<'self,A:Copy + Ord> old_iter::CopyableOrderedIter<A> for &'self [A] {
fn min(&self) -> A { old_iter::min(self) }
fn max(&self) -> A { old_iter::max(self) }
}

// FIXME(#4148): This should be redundant
impl<A:Copy + Ord> old_iter::CopyableOrderedIter<A> for ~[A] {
fn min(&self) -> A { old_iter::min(self) }
fn max(&self) -> A { old_iter::max(self) }
}

// FIXME(#4148): This should be redundant
impl<A:Copy + Ord> old_iter::CopyableOrderedIter<A> for @[A] {
fn min(&self) -> A { old_iter::min(self) }
fn max(&self) -> A { old_iter::max(self) }
}

impl<A:Clone> Clone for ~[A] {
#[inline]
fn clone(&self) -> ~[A] {
Expand Down
7 changes: 0 additions & 7 deletions src/libsyntax/opt_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,3 @@ impl<A: Copy> old_iter::CopyableIter<A> for OptVec<A> {
old_iter::find(self, f)
}
}

impl<A: Copy+Ord> old_iter::CopyableOrderedIter<A> for OptVec<A> {
#[inline(always)]
fn min(&self) -> A { old_iter::min(self) }
#[inline(always)]
fn max(&self) -> A { old_iter::max(self) }
}
Loading