use alloc::heap;
-static INITIAL_CAPACITY: uint = 7u; // 2^3 - 1
-static MINIMUM_CAPACITY: uint = 1u; // 2 - 1
+static INITIAL_CAPACITY: usize = 7; // 2^3 - 1
+static MINIMUM_CAPACITY: usize = 1; // 2 - 1
/// `RingBuf` is a circular buffer, which can be used as a double-ended queue efficiently.
#[stable(feature = "rust1", since = "1.0.0")]
// If tail == head the buffer is empty. The length of the ringbuf
// is defined as the distance between the two.
- tail: uint,
- head: uint,
- cap: uint,
+ tail: usize,
+ head: usize,
+ cap: usize,
ptr: *mut T
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<T: Clone> Clone for RingBuf<T> {
fn clone(&self) -> RingBuf<T> {
- self.iter().map(|t| t.clone()).collect()
+ self.iter().cloned().collect()
}
}
/// Moves an element out of the buffer
#[inline]
- unsafe fn buffer_read(&mut self, off: uint) -> T {
- ptr::read(self.ptr.offset(off as int))
+ unsafe fn buffer_read(&mut self, off: usize) -> T {
+ ptr::read(self.ptr.offset(off as isize))
}
/// Writes an element into the buffer, moving it.
#[inline]
- unsafe fn buffer_write(&mut self, off: uint, t: T) {
- ptr::write(self.ptr.offset(off as int), t);
+ unsafe fn buffer_write(&mut self, off: usize, t: T) {
+ ptr::write(self.ptr.offset(off as isize), t);
}
/// Returns true iff the buffer is at capacity
/// Returns the index in the underlying buffer for a given logical element index.
#[inline]
- fn wrap_index(&self, idx: uint) -> uint { wrap_index(idx, self.cap) }
+ fn wrap_index(&self, idx: usize) -> usize { wrap_index(idx, self.cap) }
/// Copies a contiguous block of memory len long from src to dst
#[inline]
- unsafe fn copy(&self, dst: uint, src: uint, len: uint) {
+ unsafe fn copy(&self, dst: usize, src: usize, len: usize) {
debug_assert!(dst + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len,
self.cap);
debug_assert!(src + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len,
self.cap);
ptr::copy_memory(
- self.ptr.offset(dst as int),
- self.ptr.offset(src as int),
+ self.ptr.offset(dst as isize),
+ self.ptr.offset(src as isize),
len);
}
/// Copies a contiguous block of memory len long from src to dst
#[inline]
- unsafe fn copy_nonoverlapping(&self, dst: uint, src: uint, len: uint) {
+ unsafe fn copy_nonoverlapping(&self, dst: usize, src: usize, len: usize) {
debug_assert!(dst + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len,
self.cap);
debug_assert!(src + len <= self.cap, "dst={} src={} len={} cap={}", dst, src, len,
self.cap);
ptr::copy_nonoverlapping_memory(
- self.ptr.offset(dst as int),
- self.ptr.offset(src as int),
+ self.ptr.offset(dst as isize),
+ self.ptr.offset(src as isize),
len);
}
}
/// Creates an empty `RingBuf` with space for at least `n` elements.
#[stable(feature = "rust1", since = "1.0.0")]
- pub fn with_capacity(n: uint) -> RingBuf<T> {
+ pub fn with_capacity(n: usize) -> RingBuf<T> {
// +1 since the ringbuffer always leaves one space empty
let cap = cmp::max(n + 1, MINIMUM_CAPACITY + 1).next_power_of_two();
assert!(cap > n, "capacity overflow");
/// assert_eq!(buf.get(1).unwrap(), &4);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
- pub fn get(&self, i: uint) -> Option<&T> {
+ pub fn get(&self, i: usize) -> Option<&T> {
if i < self.len() {
let idx = self.wrap_index(self.tail + i);
- unsafe { Some(&*self.ptr.offset(idx as int)) }
+ unsafe { Some(&*self.ptr.offset(idx as isize)) }
} else {
None
}
/// assert_eq!(buf[1], 7);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
- pub fn get_mut(&mut self, i: uint) -> Option<&mut T> {
+ pub fn get_mut(&mut self, i: usize) -> Option<&mut T> {
if i < self.len() {
let idx = self.wrap_index(self.tail + i);
- unsafe { Some(&mut *self.ptr.offset(idx as int)) }
+ unsafe { Some(&mut *self.ptr.offset(idx as isize)) }
} else {
None
}
/// assert_eq!(buf[2], 3);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
- pub fn swap(&mut self, i: uint, j: uint) {
+ pub fn swap(&mut self, i: usize, j: usize) {
assert!(i < self.len());
assert!(j < self.len());
let ri = self.wrap_index(self.tail + i);
let rj = self.wrap_index(self.tail + j);
unsafe {
- ptr::swap(self.ptr.offset(ri as int), self.ptr.offset(rj as int))
+ ptr::swap(self.ptr.offset(ri as isize), self.ptr.offset(rj as isize))
}
}
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
- pub fn capacity(&self) -> uint { self.cap - 1 }
+ pub fn capacity(&self) -> usize { self.cap - 1 }
/// Reserves the minimum capacity for exactly `additional` more elements to be inserted in the
/// given `RingBuf`. Does nothing if the capacity is already sufficient.
///
/// # Panics
///
- /// Panics if the new capacity overflows `uint`.
+ /// Panics if the new capacity overflows `usize`.
///
/// # Examples
///
/// assert!(buf.capacity() >= 11);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
- pub fn reserve_exact(&mut self, additional: uint) {
+ pub fn reserve_exact(&mut self, additional: usize) {
self.reserve(additional);
}
///
/// # Panics
///
- /// Panics if the new capacity overflows `uint`.
+ /// Panics if the new capacity overflows `usize`.
///
/// # Examples
///
/// assert!(buf.capacity() >= 11);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
- pub fn reserve(&mut self, additional: uint) {
+ pub fn reserve(&mut self, additional: usize) {
let new_len = self.len() + additional;
assert!(new_len + 1 > self.len(), "capacity overflow");
if new_len > self.capacity() {
/// use std::collections::RingBuf;
///
/// let mut buf = RingBuf::with_capacity(15);
- /// buf.extend(0u..4);
+ /// buf.extend(0..4);
/// assert_eq!(buf.capacity(), 15);
/// buf.shrink_to_fit();
/// assert!(buf.capacity() >= 4);
/// ```
#[unstable(feature = "collections",
reason = "matches collection reform specification; waiting on panic semantics")]
- pub fn truncate(&mut self, len: uint) {
+ pub fn truncate(&mut self, len: usize) {
for _ in len..self.len() {
self.pop_back();
}
/// assert_eq!(&buf.iter_mut().collect::<Vec<&mut i32>>()[], b);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
- pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, T> {
+ pub fn iter_mut(&mut self) -> IterMut<T> {
IterMut {
tail: self.tail,
head: self.head,
cap: self.cap,
ptr: self.ptr,
- marker: marker::ContravariantLifetime::<'a>,
+ marker: marker::ContravariantLifetime,
}
}
#[inline]
#[unstable(feature = "collections",
reason = "matches collection reform specification, waiting for dust to settle")]
- pub fn as_slices<'a>(&'a self) -> (&'a [T], &'a [T]) {
+ pub fn as_slices(&self) -> (&[T], &[T]) {
unsafe {
let contiguous = self.is_contiguous();
let buf = self.buffer_as_slice();
#[inline]
#[unstable(feature = "collections",
reason = "matches collection reform specification, waiting for dust to settle")]
- pub fn as_mut_slices<'a>(&'a mut self) -> (&'a mut [T], &'a mut [T]) {
+ pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T]) {
unsafe {
let contiguous = self.is_contiguous();
let head = self.head;
/// assert_eq!(v.len(), 1);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
- pub fn len(&self) -> uint { count(self.tail, self.head, self.cap) }
+ pub fn len(&self) -> usize { count(self.tail, self.head, self.cap) }
/// Returns true if the buffer contains no elements
///
/// ```
#[unstable(feature = "collections",
reason = "the naming of this function may be altered")]
- pub fn swap_back_remove(&mut self, index: uint) -> Option<T> {
+ pub fn swap_back_remove(&mut self, index: usize) -> Option<T> {
let length = self.len();
if length > 0 && index < length - 1 {
self.swap(index, length - 1);
/// ```
#[unstable(feature = "collections",
reason = "the naming of this function may be altered")]
- pub fn swap_front_remove(&mut self, index: uint) -> Option<T> {
+ pub fn swap_front_remove(&mut self, index: usize) -> Option<T> {
let length = self.len();
if length > 0 && index < length && index != 0 {
self.swap(index, 0);
/// buf.insert(1,11);
/// assert_eq!(Some(&11), buf.get(1));
/// ```
- pub fn insert(&mut self, i: uint, t: T) {
+ pub fn insert(&mut self, i: usize, t: T) {
assert!(i <= self.len(), "index out of bounds");
if self.is_full() {
self.reserve(1);
/// assert_eq!(Some(&15), buf.get(2));
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
- pub fn remove(&mut self, i: uint) -> Option<T> {
+ pub fn remove(&mut self, i: usize) -> Option<T> {
if self.is_empty() || self.len() <= i {
return None;
}
/// ```
#[unstable(feature = "collections",
reason = "matches collection reform specification; waiting on panic semantics")]
- pub fn resize(&mut self, new_len: uint, value: T) {
+ pub fn resize(&mut self, new_len: usize, value: T) {
let len = self.len();
if new_len > len {
/// Returns the index in the underlying buffer for a given logical element index.
#[inline]
-fn wrap_index(index: uint, size: uint) -> uint {
+fn wrap_index(index: usize, size: usize) -> usize {
// size is always a power of 2
index & (size - 1)
}
/// Calculate the number of elements left to be read in the buffer
#[inline]
-fn count(tail: uint, head: uint, size: uint) -> uint {
+fn count(tail: usize, head: usize, size: usize) -> usize {
// size is always a power of 2
(head - tail) & (size - 1)
}
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Iter<'a, T:'a> {
ring: &'a [T],
- tail: uint,
- head: uint
+ tail: usize,
+ head: usize
}
// FIXME(#19839) Remove in favor of `#[derive(Clone)]`
}
#[inline]
- fn size_hint(&self) -> (uint, Option<uint>) {
+ fn size_hint(&self) -> (usize, Option<usize>) {
let len = count(self.tail, self.head, self.ring.len());
(len, Some(len))
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> RandomAccessIterator for Iter<'a, T> {
#[inline]
- fn indexable(&self) -> uint {
+ fn indexable(&self) -> usize {
let (len, _) = self.size_hint();
len
}
#[inline]
- fn idx(&mut self, j: uint) -> Option<&'a T> {
+ fn idx(&mut self, j: usize) -> Option<&'a T> {
if j >= self.indexable() {
None
} else {
#[stable(feature = "rust1", since = "1.0.0")]
pub struct IterMut<'a, T:'a> {
ptr: *mut T,
- tail: uint,
- head: uint,
- cap: uint,
+ tail: usize,
+ head: usize,
+ cap: usize,
marker: marker::ContravariantLifetime<'a>,
}
self.tail = wrap_index(self.tail + 1, self.cap);
unsafe {
- Some(&mut *self.ptr.offset(tail as int))
+ Some(&mut *self.ptr.offset(tail as isize))
}
}
#[inline]
- fn size_hint(&self) -> (uint, Option<uint>) {
+ fn size_hint(&self) -> (usize, Option<usize>) {
let len = count(self.tail, self.head, self.cap);
(len, Some(len))
}
self.head = wrap_index(self.head - 1, self.cap);
unsafe {
- Some(&mut *self.ptr.offset(self.head as int))
+ Some(&mut *self.ptr.offset(self.head as isize))
}
}
}
}
#[inline]
- fn size_hint(&self) -> (uint, Option<uint>) {
+ fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.inner.len();
(len, Some(len))
}
}
#[inline]
- fn size_hint(&self) -> (uint, Option<uint>) {
+ fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.inner.len();
(len, Some(len))
}
impl<S: Writer + Hasher, A: Hash<S>> Hash<S> for RingBuf<A> {
fn hash(&self, state: &mut S) {
self.len().hash(state);
- for elt in self.iter() {
+ for elt in self {
elt.hash(state);
}
}
}
#[stable(feature = "rust1", since = "1.0.0")]
-impl<A> Index<uint> for RingBuf<A> {
+impl<A> Index<usize> for RingBuf<A> {
type Output = A;
#[inline]
- fn index<'a>(&'a self, i: &uint) -> &'a A {
+ fn index(&self, i: &usize) -> &A {
self.get(*i).expect("Out of bounds access")
}
}
#[stable(feature = "rust1", since = "1.0.0")]
-impl<A> IndexMut<uint> for RingBuf<A> {
- type Output = A;
-
+impl<A> IndexMut<usize> for RingBuf<A> {
#[inline]
- fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut A {
+ fn index_mut(&mut self, i: &usize) -> &mut A {
self.get_mut(*i).expect("Out of bounds access")
}
}
#[stable(feature = "rust1", since = "1.0.0")]
impl<A> Extend<A> for RingBuf<A> {
- fn extend<T: Iterator<Item=A>>(&mut self, mut iterator: T) {
+ fn extend<T: Iterator<Item=A>>(&mut self, iterator: T) {
for elt in iterator {
self.push_back(elt);
}
#[allow(deprecated)]
fn test_simple() {
let mut d = RingBuf::new();
- assert_eq!(d.len(), 0u);
+ assert_eq!(d.len(), 0);
d.push_front(17);
d.push_front(42);
d.push_back(137);
- assert_eq!(d.len(), 3u);
+ assert_eq!(d.len(), 3);
d.push_back(137);
- assert_eq!(d.len(), 4u);
+ assert_eq!(d.len(), 4);
assert_eq!(*d.front().unwrap(), 42);
assert_eq!(*d.back().unwrap(), 137);
let mut i = d.pop_front();
assert_eq!(i, Some(137));
i = d.pop_back();
assert_eq!(i, Some(17));
- assert_eq!(d.len(), 0u);
+ assert_eq!(d.len(), 0);
d.push_back(3);
- assert_eq!(d.len(), 1u);
+ assert_eq!(d.len(), 1);
d.push_front(2);
- assert_eq!(d.len(), 2u);
+ assert_eq!(d.len(), 2);
d.push_back(4);
- assert_eq!(d.len(), 3u);
+ assert_eq!(d.len(), 3);
d.push_front(1);
- assert_eq!(d.len(), 4u);
+ assert_eq!(d.len(), 4);
debug!("{}", d[0]);
debug!("{}", d[1]);
debug!("{}", d[2]);
#[test]
fn test_push_front_grow() {
let mut deq = RingBuf::new();
- for i in 0u..66 {
+ for i in 0..66 {
deq.push_front(i);
}
assert_eq!(deq.len(), 66);
- for i in 0u..66 {
+ for i in 0..66 {
assert_eq!(deq[i], 65 - i);
}
let mut deq = RingBuf::new();
- for i in 0u..66 {
+ for i in 0..66 {
deq.push_back(i);
}
- for i in 0u..66 {
+ for i in 0..66 {
assert_eq!(deq[i], i);
}
}
#[test]
fn test_index() {
let mut deq = RingBuf::new();
- for i in 1u..4 {
+ for i in 1..4 {
deq.push_front(i);
}
assert_eq!(deq[1], 2);
#[should_fail]
fn test_index_out_of_bounds() {
let mut deq = RingBuf::new();
- for i in 1u..4 {
+ for i in 1..4 {
deq.push_front(i);
}
deq[3];
#[bench]
fn bench_new(b: &mut test::Bencher) {
b.iter(|| {
- let ring: RingBuf<u64> = RingBuf::new();
+ let ring: RingBuf<i32> = RingBuf::new();
test::black_box(ring);
})
}
#[bench]
fn bench_pop_back_100(b: &mut test::Bencher) {
- let mut deq: RingBuf<i32> = RingBuf::with_capacity(101);
+ let mut deq= RingBuf::<i32>::with_capacity(101);
b.iter(|| {
deq.head = 100;
#[bench]
fn bench_pop_front_100(b: &mut test::Bencher) {
- let mut deq: RingBuf<i32> = RingBuf::with_capacity(101);
+ let mut deq = RingBuf::<i32>::with_capacity(101);
b.iter(|| {
deq.head = 100;
#[bench]
fn bench_iter_1000(b: &mut test::Bencher) {
- let ring: RingBuf<i32> = (0..1000).collect();
+ let ring: RingBuf<_> = (0..1000).collect();
b.iter(|| {
let mut sum = 0;
- for &i in ring.iter() {
+ for &i in &ring {
sum += i;
}
test::black_box(sum);
#[bench]
fn bench_mut_iter_1000(b: &mut test::Bencher) {
- let mut ring: RingBuf<i32> = (0..1000).collect();
+ let mut ring: RingBuf<_> = (0..1000).collect();
b.iter(|| {
let mut sum = 0;
- for i in ring.iter_mut() {
+ for i in &mut ring {
sum += *i;
}
test::black_box(sum);
#[test]
fn test_reserve_exact() {
let mut d = RingBuf::new();
- d.push_back(0u64);
- d.reserve_exact(50);
- assert!(d.capacity() >= 51);
- let mut d = RingBuf::new();
- d.push_back(0u32);
+ d.push_back(0);
d.reserve_exact(50);
assert!(d.capacity() >= 51);
}
#[test]
fn test_reserve() {
let mut d = RingBuf::new();
- d.push_back(0u64);
- d.reserve(50);
- assert!(d.capacity() >= 51);
- let mut d = RingBuf::new();
- d.push_back(0u32);
+ d.push_back(0);
d.reserve(50);
assert!(d.capacity() >= 51);
}
#[test]
fn test_swap() {
- let mut d: RingBuf<i32> = (0..5).collect();
+ let mut d: RingBuf<_> = (0..5).collect();
d.pop_front();
d.swap(0, 3);
- assert_eq!(d.iter().map(|&x|x).collect::<Vec<i32>>(), vec!(4, 2, 3, 1));
+ assert_eq!(d.iter().cloned().collect::<Vec<_>>(), vec!(4, 2, 3, 1));
}
#[test]
}
{
let b: &[_] = &[&0,&1,&2,&3,&4];
- assert_eq!(d.iter().collect::<Vec<&i32>>(), b);
+ assert_eq!(d.iter().collect::<Vec<_>>(), b);
}
for i in 6..9 {
}
{
let b: &[_] = &[&8,&7,&6,&0,&1,&2,&3,&4];
- assert_eq!(d.iter().collect::<Vec<&i32>>(), b);
+ assert_eq!(d.iter().collect::<Vec<_>>(), b);
}
let mut it = d.iter();
}
{
let b: &[_] = &[&4,&3,&2,&1,&0];
- assert_eq!(d.iter().rev().collect::<Vec<&i32>>(), b);
+ assert_eq!(d.iter().rev().collect::<Vec<_>>(), b);
}
for i in 6..9 {
d.push_front(i);
}
let b: &[_] = &[&4,&3,&2,&1,&0,&6,&7,&8];
- assert_eq!(d.iter().rev().collect::<Vec<&i32>>(), b);
+ assert_eq!(d.iter().rev().collect::<Vec<_>>(), b);
}
#[test]
assert_eq!(d.pop_front(), Some(1));
d.push_back(4);
- assert_eq!(d.iter_mut().rev().map(|x| *x).collect::<Vec<i32>>(),
- vec!(4, 3, 2));
+ assert_eq!(d.iter_mut().rev().cloned().collect::<Vec<_>>(),
+ vec![4, 3, 2]);
}
#[test]
let mut d = RingBuf::new();
assert!(d.iter_mut().next().is_none());
- for i in 0u..3 {
+ for i in 0..3 {
d.push_front(i);
}
let mut d = RingBuf::new();
assert!(d.iter_mut().rev().next().is_none());
- for i in 0u..3 {
+ for i in 0..3 {
d.push_front(i);
}
}
let b = vec![0,1,2,3,4];
- assert_eq!(d.into_iter().collect::<Vec<i32>>(), b);
+ assert_eq!(d.into_iter().collect::<Vec<_>>(), b);
}
// wrapped iter
}
let b = vec![8,7,6,0,1,2,3,4];
- assert_eq!(d.into_iter().collect::<Vec<i32>>(), b);
+ assert_eq!(d.into_iter().collect::<Vec<_>>(), b);
}
// partially used
// partially used
{
- let mut d: RingBuf<i32> = RingBuf::new();
+ let mut d: RingBuf<_> = RingBuf::new();
for i in 0..5 {
d.push_back(i);
}
fn test_from_iter() {
use core::iter;
let v = vec!(1,2,3,4,5,6,7);
- let deq: RingBuf<i32> = v.iter().map(|&x| x).collect();
- let u: Vec<i32> = deq.iter().map(|&x| x).collect();
+ let deq: RingBuf<_> = v.iter().cloned().collect();
+ let u: Vec<_> = deq.iter().cloned().collect();
assert_eq!(u, v);
- let seq = iter::count(0u, 2).take(256);
- let deq: RingBuf<uint> = seq.collect();
+ let seq = iter::count(0, 2).take(256);
+ let deq: RingBuf<_> = seq.collect();
for (i, &x) in deq.iter().enumerate() {
assert_eq!(2*i, x);
}
d.push_front(42);
d.push_back(137);
d.push_back(137);
- assert_eq!(d.len(), 4u);
+ assert_eq!(d.len(), 4);
let mut e = d.clone();
- assert_eq!(e.len(), 4u);
+ assert_eq!(e.len(), 4);
while !d.is_empty() {
assert_eq!(d.pop_back(), e.pop_back());
}
- assert_eq!(d.len(), 0u);
- assert_eq!(e.len(), 0u);
+ assert_eq!(d.len(), 0);
+ assert_eq!(e.len(), 0);
}
#[test]
#[test]
fn test_show() {
- let ringbuf: RingBuf<i32> = (0..10).collect();
+ let ringbuf: RingBuf<_> = (0..10).collect();
assert_eq!(format!("{:?}", ringbuf), "RingBuf [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]");
- let ringbuf: RingBuf<&str> = vec!["just", "one", "test", "more"].iter()
- .map(|&s| s)
+ let ringbuf: RingBuf<_> = vec!["just", "one", "test", "more"].iter()
+ .cloned()
.collect();
assert_eq!(format!("{:?}", ringbuf), "RingBuf [\"just\", \"one\", \"test\", \"more\"]");
}
#[test]
fn test_drop() {
- static mut drops: uint = 0;
+ static mut drops: i32 = 0;
struct Elem;
impl Drop for Elem {
fn drop(&mut self) {
#[test]
fn test_drop_with_pop() {
- static mut drops: uint = 0;
+ static mut drops: i32 = 0;
struct Elem;
impl Drop for Elem {
fn drop(&mut self) {
#[test]
fn test_drop_clear() {
- static mut drops: uint = 0;
+ static mut drops: i32 = 0;
struct Elem;
impl Drop for Elem {
fn drop(&mut self) {