mirror of
https://github.com/rust-lang/rust.git
synced 2024-12-02 19:53:46 +00:00
stabilize more of collections
This commit is contained in:
parent
10d99a9734
commit
8dbaa7105e
@ -161,6 +161,7 @@ use vec::{mod, Vec};
|
||||
///
|
||||
/// This will be a max-heap.
|
||||
#[deriving(Clone)]
|
||||
#[stable]
|
||||
pub struct BinaryHeap<T> {
|
||||
data: Vec<T>,
|
||||
}
|
||||
@ -168,7 +169,6 @@ pub struct BinaryHeap<T> {
|
||||
#[stable]
|
||||
impl<T: Ord> Default for BinaryHeap<T> {
|
||||
#[inline]
|
||||
#[stable]
|
||||
fn default() -> BinaryHeap<T> { BinaryHeap::new() }
|
||||
}
|
||||
|
||||
@ -182,7 +182,7 @@ impl<T: Ord> BinaryHeap<T> {
|
||||
/// let mut heap = BinaryHeap::new();
|
||||
/// heap.push(4u);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn new() -> BinaryHeap<T> { BinaryHeap { data: vec![] } }
|
||||
|
||||
/// Creates an empty `BinaryHeap` with a specific capacity.
|
||||
@ -197,7 +197,7 @@ impl<T: Ord> BinaryHeap<T> {
|
||||
/// let mut heap = BinaryHeap::with_capacity(10);
|
||||
/// heap.push(4u);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn with_capacity(capacity: uint) -> BinaryHeap<T> {
|
||||
BinaryHeap { data: Vec::with_capacity(capacity) }
|
||||
}
|
||||
@ -235,7 +235,7 @@ impl<T: Ord> BinaryHeap<T> {
|
||||
/// println!("{}", x);
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn iter(&self) -> Iter<T> {
|
||||
Iter { iter: self.data.iter() }
|
||||
}
|
||||
@ -256,7 +256,7 @@ impl<T: Ord> BinaryHeap<T> {
|
||||
/// println!("{}", x);
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn into_iter(self) -> IntoIter<T> {
|
||||
IntoIter { iter: self.data.into_iter() }
|
||||
}
|
||||
@ -291,7 +291,7 @@ impl<T: Ord> BinaryHeap<T> {
|
||||
/// assert!(heap.capacity() >= 100);
|
||||
/// heap.push(4u);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn capacity(&self) -> uint { self.data.capacity() }
|
||||
|
||||
/// Reserves the minimum capacity for exactly `additional` more elements to be inserted in the
|
||||
@ -314,7 +314,7 @@ impl<T: Ord> BinaryHeap<T> {
|
||||
/// assert!(heap.capacity() >= 100);
|
||||
/// heap.push(4u);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn reserve_exact(&mut self, additional: uint) {
|
||||
self.data.reserve_exact(additional);
|
||||
}
|
||||
@ -335,13 +335,13 @@ impl<T: Ord> BinaryHeap<T> {
|
||||
/// assert!(heap.capacity() >= 100);
|
||||
/// heap.push(4u);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn reserve(&mut self, additional: uint) {
|
||||
self.data.reserve(additional);
|
||||
}
|
||||
|
||||
/// Discards as much additional capacity as possible.
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn shrink_to_fit(&mut self) {
|
||||
self.data.shrink_to_fit();
|
||||
}
|
||||
@ -359,7 +359,7 @@ impl<T: Ord> BinaryHeap<T> {
|
||||
/// assert_eq!(heap.pop(), Some(1));
|
||||
/// assert_eq!(heap.pop(), None);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn pop(&mut self) -> Option<T> {
|
||||
self.data.pop().map(|mut item| {
|
||||
if !self.is_empty() {
|
||||
@ -384,7 +384,7 @@ impl<T: Ord> BinaryHeap<T> {
|
||||
/// assert_eq!(heap.len(), 3);
|
||||
/// assert_eq!(heap.peek(), Some(&5));
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn push(&mut self, item: T) {
|
||||
let old_len = self.len();
|
||||
self.data.push(item);
|
||||
@ -539,11 +539,11 @@ impl<T: Ord> BinaryHeap<T> {
|
||||
}
|
||||
|
||||
/// Returns the length of the binary heap.
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn len(&self) -> uint { self.data.len() }
|
||||
|
||||
/// Checks if the binary heap is empty.
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_empty(&self) -> bool { self.len() == 0 }
|
||||
|
||||
/// Clears the binary heap, returning an iterator over the removed elements.
|
||||
@ -554,7 +554,7 @@ impl<T: Ord> BinaryHeap<T> {
|
||||
}
|
||||
|
||||
/// Drops all items from the binary heap.
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn clear(&mut self) { self.drain(); }
|
||||
}
|
||||
|
||||
@ -570,6 +570,7 @@ impl<'a, T> Clone for Iter<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T> Iterator<&'a T> for Iter<'a, T> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<&'a T> { self.iter.next() }
|
||||
@ -578,11 +579,13 @@ impl<'a, T> Iterator<&'a T> for Iter<'a, T> {
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T> DoubleEndedIterator<&'a T> for Iter<'a, T> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<&'a T> { self.iter.next_back() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T> ExactSizeIterator<&'a T> for Iter<'a, T> {}
|
||||
|
||||
/// An iterator that moves out of a `BinaryHeap`.
|
||||
@ -590,6 +593,7 @@ pub struct IntoIter<T> {
|
||||
iter: vec::IntoIter<T>,
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<T> Iterator<T> for IntoIter<T> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<T> { self.iter.next() }
|
||||
@ -598,11 +602,13 @@ impl<T> Iterator<T> for IntoIter<T> {
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<T> DoubleEndedIterator<T> for IntoIter<T> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<T> { self.iter.next_back() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<T> ExactSizeIterator<T> for IntoIter<T> {}
|
||||
|
||||
/// An iterator that drains a `BinaryHeap`.
|
||||
@ -610,6 +616,7 @@ pub struct Drain<'a, T: 'a> {
|
||||
iter: vec::Drain<'a, T>,
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T: 'a> Iterator<T> for Drain<'a, T> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<T> { self.iter.next() }
|
||||
@ -618,19 +625,23 @@ impl<'a, T: 'a> Iterator<T> for Drain<'a, T> {
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T: 'a> DoubleEndedIterator<T> for Drain<'a, T> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<T> { self.iter.next_back() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T: 'a> ExactSizeIterator<T> for Drain<'a, T> {}
|
||||
|
||||
#[stable]
|
||||
impl<T: Ord> FromIterator<T> for BinaryHeap<T> {
|
||||
fn from_iter<Iter: Iterator<T>>(iter: Iter) -> BinaryHeap<T> {
|
||||
BinaryHeap::from_vec(iter.collect())
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<T: Ord> Extend<T> for BinaryHeap<T> {
|
||||
fn extend<Iter: Iterator<T>>(&mut self, mut iter: Iter) {
|
||||
let (lower, _) = iter.size_hint();
|
||||
|
@ -88,14 +88,15 @@ use core::fmt;
|
||||
use core::iter::{Cloned, Chain, Enumerate, Repeat, Skip, Take, repeat};
|
||||
use core::iter;
|
||||
use core::num::Int;
|
||||
use core::slice::{Iter, IterMut};
|
||||
use core::slice;
|
||||
use core::{u8, u32, uint};
|
||||
use bitv_set; //so meta
|
||||
|
||||
use core::hash;
|
||||
use Vec;
|
||||
|
||||
type Blocks<'a> = Cloned<Iter<'a, u32>>;
|
||||
type MutBlocks<'a> = IterMut<'a, u32>;
|
||||
type Blocks<'a> = Cloned<slice::Iter<'a, u32>>;
|
||||
type MutBlocks<'a> = slice::IterMut<'a, u32>;
|
||||
type MatchWords<'a> = Chain<Enumerate<Blocks<'a>>, Skip<Take<Enumerate<Repeat<u32>>>>>;
|
||||
|
||||
fn reverse_bits(byte: u8) -> u8 {
|
||||
@ -152,6 +153,7 @@ static FALSE: bool = false;
|
||||
/// println!("{}", bv.to_string());
|
||||
/// println!("total bits set to true: {}", bv.iter().filter(|x| *x).count());
|
||||
/// ```
|
||||
#[stable]
|
||||
pub struct Bitv {
|
||||
/// Internal representation of the bit vector
|
||||
storage: Vec<u32>,
|
||||
@ -162,7 +164,7 @@ pub struct Bitv {
|
||||
// FIXME(Gankro): NopeNopeNopeNopeNope (wait for IndexGet to be a thing)
|
||||
impl Index<uint,bool> for Bitv {
|
||||
#[inline]
|
||||
fn index<'a>(&'a self, i: &uint) -> &'a bool {
|
||||
fn index(&self, i: &uint) -> &bool {
|
||||
if self.get(*i).expect("index out of bounds") {
|
||||
&TRUE
|
||||
} else {
|
||||
@ -245,7 +247,7 @@ impl Bitv {
|
||||
/// use std::collections::Bitv;
|
||||
/// let mut bv = Bitv::new();
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn new() -> Bitv {
|
||||
Bitv { storage: Vec::new(), nbits: 0 }
|
||||
}
|
||||
@ -281,7 +283,7 @@ impl Bitv {
|
||||
///
|
||||
/// It is important to note that this function does not specify the
|
||||
/// *length* of the returned bitvector, but only the *capacity*.
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn with_capacity(nbits: uint) -> Bitv {
|
||||
Bitv {
|
||||
storage: Vec::with_capacity(blocks_for_bits(nbits)),
|
||||
@ -367,7 +369,7 @@ impl Bitv {
|
||||
/// assert_eq!(bv[1], true);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "panic semantics are likely to change in the future"]
|
||||
#[stable]
|
||||
pub fn get(&self, i: uint) -> Option<bool> {
|
||||
if i >= self.nbits {
|
||||
return None;
|
||||
@ -578,9 +580,9 @@ impl Bitv {
|
||||
/// assert_eq!(bv.iter().filter(|x| *x).count(), 7);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
pub fn iter<'a>(&'a self) -> Bits<'a> {
|
||||
Bits { bitv: self, next_idx: 0, end_idx: self.nbits }
|
||||
#[stable]
|
||||
pub fn iter(&self) -> Iter {
|
||||
Iter { bitv: self, next_idx: 0, end_idx: self.nbits }
|
||||
}
|
||||
|
||||
/// Returns `true` if all bits are 0.
|
||||
@ -705,7 +707,7 @@ impl Bitv {
|
||||
/// bv.truncate(2);
|
||||
/// assert!(bv.eq_vec(&[false, true]));
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn truncate(&mut self, len: uint) {
|
||||
if len < self.len() {
|
||||
self.nbits = len;
|
||||
@ -732,7 +734,7 @@ impl Bitv {
|
||||
/// assert_eq!(bv.len(), 3);
|
||||
/// assert!(bv.capacity() >= 13);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn reserve(&mut self, additional: uint) {
|
||||
let desired_cap = self.len().checked_add(additional).expect("capacity overflow");
|
||||
let storage_len = self.storage.len();
|
||||
@ -762,7 +764,7 @@ impl Bitv {
|
||||
/// assert_eq!(bv.len(), 3);
|
||||
/// assert!(bv.capacity() >= 13);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn reserve_exact(&mut self, additional: uint) {
|
||||
let desired_cap = self.len().checked_add(additional).expect("capacity overflow");
|
||||
let storage_len = self.storage.len();
|
||||
@ -784,7 +786,7 @@ impl Bitv {
|
||||
/// assert!(bv.capacity() >= 10);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn capacity(&self) -> uint {
|
||||
self.storage.capacity().checked_mul(u32::BITS).unwrap_or(uint::MAX)
|
||||
}
|
||||
@ -855,7 +857,7 @@ impl Bitv {
|
||||
/// assert_eq!(bv.pop(), Some(false));
|
||||
/// assert_eq!(bv.len(), 6);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn pop(&mut self) -> Option<bool> {
|
||||
if self.is_empty() {
|
||||
None
|
||||
@ -885,7 +887,7 @@ impl Bitv {
|
||||
/// bv.push(false);
|
||||
/// assert!(bv.eq_vec(&[true, false]));
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn push(&mut self, elem: bool) {
|
||||
if self.nbits % u32::BITS == 0 {
|
||||
self.storage.push(0);
|
||||
@ -897,17 +899,17 @@ impl Bitv {
|
||||
|
||||
/// Return the total number of bits in this vector
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn len(&self) -> uint { self.nbits }
|
||||
|
||||
/// Returns true if there are no bits in this vector
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_empty(&self) -> bool { self.len() == 0 }
|
||||
|
||||
/// Clears all bits in this vector.
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn clear(&mut self) {
|
||||
for w in self.storage.iter_mut() { *w = 0u32; }
|
||||
}
|
||||
@ -928,10 +930,10 @@ pub fn from_fn<F>(len: uint, f: F) -> Bitv where F: FnMut(uint) -> bool {
|
||||
#[stable]
|
||||
impl Default for Bitv {
|
||||
#[inline]
|
||||
#[stable]
|
||||
fn default() -> Bitv { Bitv::new() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl FromIterator<bool> for Bitv {
|
||||
fn from_iter<I:Iterator<bool>>(iterator: I) -> Bitv {
|
||||
let mut ret = Bitv::new();
|
||||
@ -940,6 +942,7 @@ impl FromIterator<bool> for Bitv {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl Extend<bool> for Bitv {
|
||||
#[inline]
|
||||
fn extend<I: Iterator<bool>>(&mut self, mut iterator: I) {
|
||||
@ -981,6 +984,7 @@ impl Ord for Bitv {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl fmt::Show for Bitv {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
for bit in self.iter() {
|
||||
@ -990,6 +994,7 @@ impl fmt::Show for Bitv {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<S: hash::Writer> hash::Hash<S> for Bitv {
|
||||
fn hash(&self, state: &mut S) {
|
||||
self.nbits.hash(state);
|
||||
@ -1014,14 +1019,16 @@ impl cmp::PartialEq for Bitv {
|
||||
impl cmp::Eq for Bitv {}
|
||||
|
||||
/// An iterator for `Bitv`.
|
||||
#[stable]
|
||||
#[deriving(Clone)]
|
||||
pub struct Bits<'a> {
|
||||
pub struct Iter<'a> {
|
||||
bitv: &'a Bitv,
|
||||
next_idx: uint,
|
||||
end_idx: uint,
|
||||
}
|
||||
|
||||
impl<'a> Iterator<bool> for Bits<'a> {
|
||||
#[stable]
|
||||
impl<'a> Iterator<bool> for Iter<'a> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<bool> {
|
||||
if self.next_idx != self.end_idx {
|
||||
@ -1039,7 +1046,8 @@ impl<'a> Iterator<bool> for Bits<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> DoubleEndedIterator<bool> for Bits<'a> {
|
||||
#[stable]
|
||||
impl<'a> DoubleEndedIterator<bool> for Iter<'a> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<bool> {
|
||||
if self.next_idx != self.end_idx {
|
||||
@ -1051,9 +1059,11 @@ impl<'a> DoubleEndedIterator<bool> for Bits<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> ExactSizeIterator<bool> for Bits<'a> {}
|
||||
#[stable]
|
||||
impl<'a> ExactSizeIterator<bool> for Iter<'a> {}
|
||||
|
||||
impl<'a> RandomAccessIterator<bool> for Bits<'a> {
|
||||
#[stable]
|
||||
impl<'a> RandomAccessIterator<bool> for Iter<'a> {
|
||||
#[inline]
|
||||
fn indexable(&self) -> uint {
|
||||
self.end_idx - self.next_idx
|
||||
@ -1108,15 +1118,18 @@ impl<'a> RandomAccessIterator<bool> for Bits<'a> {
|
||||
/// assert!(bv[3]);
|
||||
/// ```
|
||||
#[deriving(Clone)]
|
||||
#[stable]
|
||||
pub struct BitvSet {
|
||||
bitv: Bitv,
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl Default for BitvSet {
|
||||
#[inline]
|
||||
fn default() -> BitvSet { BitvSet::new() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl FromIterator<uint> for BitvSet {
|
||||
fn from_iter<I:Iterator<uint>>(iterator: I) -> BitvSet {
|
||||
let mut ret = BitvSet::new();
|
||||
@ -1125,6 +1138,7 @@ impl FromIterator<uint> for BitvSet {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl Extend<uint> for BitvSet {
|
||||
#[inline]
|
||||
fn extend<I: Iterator<uint>>(&mut self, mut iterator: I) {
|
||||
@ -1175,7 +1189,7 @@ impl BitvSet {
|
||||
/// let mut s = BitvSet::new();
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn new() -> BitvSet {
|
||||
BitvSet { bitv: Bitv::new() }
|
||||
}
|
||||
@ -1192,7 +1206,7 @@ impl BitvSet {
|
||||
/// assert!(s.capacity() >= 100);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn with_capacity(nbits: uint) -> BitvSet {
|
||||
let bitv = Bitv::from_elem(nbits, false);
|
||||
BitvSet::from_bitv(bitv)
|
||||
@ -1230,7 +1244,7 @@ impl BitvSet {
|
||||
/// assert!(s.capacity() >= 100);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn capacity(&self) -> uint {
|
||||
self.bitv.capacity()
|
||||
}
|
||||
@ -1251,7 +1265,7 @@ impl BitvSet {
|
||||
/// s.reserve_len(10);
|
||||
/// assert!(s.capacity() >= 10);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn reserve_len(&mut self, len: uint) {
|
||||
let cur_len = self.bitv.len();
|
||||
if len >= cur_len {
|
||||
@ -1277,7 +1291,7 @@ impl BitvSet {
|
||||
/// s.reserve_len_exact(10);
|
||||
/// assert!(s.capacity() >= 10);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn reserve_len_exact(&mut self, len: uint) {
|
||||
let cur_len = self.bitv.len();
|
||||
if len >= cur_len {
|
||||
@ -1320,7 +1334,7 @@ impl BitvSet {
|
||||
/// assert_eq!(bv[0], true);
|
||||
/// ```
|
||||
#[inline]
|
||||
pub fn get_ref<'a>(&'a self) -> &'a Bitv {
|
||||
pub fn get_ref(&self) -> &Bitv {
|
||||
&self.bitv
|
||||
}
|
||||
|
||||
@ -1371,7 +1385,7 @@ impl BitvSet {
|
||||
/// println!("new capacity: {}", s.capacity());
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn shrink_to_fit(&mut self) {
|
||||
let bitv = &mut self.bitv;
|
||||
// Obtain original length
|
||||
@ -1399,9 +1413,9 @@ impl BitvSet {
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
pub fn iter<'a>(&'a self) -> BitPositions<'a> {
|
||||
BitPositions {set: self, next_idx: 0u}
|
||||
#[stable]
|
||||
pub fn iter(&self) -> bitv_set::Iter {
|
||||
SetIter {set: self, next_idx: 0u}
|
||||
}
|
||||
|
||||
/// Iterator over each u32 stored in `self` union `other`.
|
||||
@ -1421,17 +1435,17 @@ impl BitvSet {
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
pub fn union<'a>(&'a self, other: &'a BitvSet) -> TwoBitPositions<'a> {
|
||||
#[stable]
|
||||
pub fn union<'a>(&'a self, other: &'a BitvSet) -> Union<'a> {
|
||||
fn or(w1: u32, w2: u32) -> u32 { w1 | w2 }
|
||||
|
||||
TwoBitPositions {
|
||||
Union(TwoBitPositions {
|
||||
set: self,
|
||||
other: other,
|
||||
merge: or,
|
||||
current_word: 0u32,
|
||||
next_idx: 0u
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Iterator over each uint stored in `self` intersect `other`.
|
||||
@ -1451,17 +1465,17 @@ impl BitvSet {
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
pub fn intersection<'a>(&'a self, other: &'a BitvSet) -> Take<TwoBitPositions<'a>> {
|
||||
#[stable]
|
||||
pub fn intersection<'a>(&'a self, other: &'a BitvSet) -> Intersection<'a> {
|
||||
fn bitand(w1: u32, w2: u32) -> u32 { w1 & w2 }
|
||||
let min = cmp::min(self.bitv.len(), other.bitv.len());
|
||||
TwoBitPositions {
|
||||
Intersection(TwoBitPositions {
|
||||
set: self,
|
||||
other: other,
|
||||
merge: bitand,
|
||||
current_word: 0u32,
|
||||
next_idx: 0
|
||||
}.take(min)
|
||||
}.take(min))
|
||||
}
|
||||
|
||||
/// Iterator over each uint stored in the `self` setminus `other`.
|
||||
@ -1488,17 +1502,17 @@ impl BitvSet {
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
pub fn difference<'a>(&'a self, other: &'a BitvSet) -> TwoBitPositions<'a> {
|
||||
#[stable]
|
||||
pub fn difference<'a>(&'a self, other: &'a BitvSet) -> Difference<'a> {
|
||||
fn diff(w1: u32, w2: u32) -> u32 { w1 & !w2 }
|
||||
|
||||
TwoBitPositions {
|
||||
Difference(TwoBitPositions {
|
||||
set: self,
|
||||
other: other,
|
||||
merge: diff,
|
||||
current_word: 0u32,
|
||||
next_idx: 0
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Iterator over each u32 stored in the symmetric difference of `self` and `other`.
|
||||
@ -1519,17 +1533,17 @@ impl BitvSet {
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
pub fn symmetric_difference<'a>(&'a self, other: &'a BitvSet) -> TwoBitPositions<'a> {
|
||||
#[stable]
|
||||
pub fn symmetric_difference<'a>(&'a self, other: &'a BitvSet) -> SymmetricDifference<'a> {
|
||||
fn bitxor(w1: u32, w2: u32) -> u32 { w1 ^ w2 }
|
||||
|
||||
TwoBitPositions {
|
||||
SymmetricDifference(TwoBitPositions {
|
||||
set: self,
|
||||
other: other,
|
||||
merge: bitxor,
|
||||
current_word: 0u32,
|
||||
next_idx: 0
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/// Unions in-place with the specified other bit vector.
|
||||
@ -1636,28 +1650,28 @@ impl BitvSet {
|
||||
|
||||
/// Return the number of set bits in this set.
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn len(&self) -> uint {
|
||||
self.bitv.blocks().fold(0, |acc, n| acc + n.count_ones())
|
||||
}
|
||||
|
||||
/// Returns whether there are no bits set in this set
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.bitv.none()
|
||||
}
|
||||
|
||||
/// Clears all bits in this set
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn clear(&mut self) {
|
||||
self.bitv.clear();
|
||||
}
|
||||
|
||||
/// Returns `true` if this set contains the specified integer.
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn contains(&self, value: &uint) -> bool {
|
||||
let bitv = &self.bitv;
|
||||
*value < bitv.nbits && bitv[*value]
|
||||
@ -1666,14 +1680,14 @@ impl BitvSet {
|
||||
/// Returns `true` if the set has no elements in common with `other`.
|
||||
/// This is equivalent to checking for an empty intersection.
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_disjoint(&self, other: &BitvSet) -> bool {
|
||||
self.intersection(other).next().is_none()
|
||||
}
|
||||
|
||||
/// Returns `true` if the set is a subset of another.
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_subset(&self, other: &BitvSet) -> bool {
|
||||
let self_bitv = &self.bitv;
|
||||
let other_bitv = &other.bitv;
|
||||
@ -1687,14 +1701,14 @@ impl BitvSet {
|
||||
|
||||
/// Returns `true` if the set is a superset of another.
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_superset(&self, other: &BitvSet) -> bool {
|
||||
other.is_subset(self)
|
||||
}
|
||||
|
||||
/// Adds a value to the set. Returns `true` if the value was not already
|
||||
/// present in the set.
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn insert(&mut self, value: uint) -> bool {
|
||||
if self.contains(&value) {
|
||||
return false;
|
||||
@ -1712,7 +1726,7 @@ impl BitvSet {
|
||||
|
||||
/// Removes a value from the set. Returns `true` if the value was
|
||||
/// present in the set.
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn remove(&mut self, value: &uint) -> bool {
|
||||
if !self.contains(value) {
|
||||
return false;
|
||||
@ -1749,14 +1763,15 @@ impl<S: hash::Writer> hash::Hash<S> for BitvSet {
|
||||
|
||||
/// An iterator for `BitvSet`.
|
||||
#[deriving(Clone)]
|
||||
pub struct BitPositions<'a> {
|
||||
#[stable]
|
||||
pub struct SetIter<'a> {
|
||||
set: &'a BitvSet,
|
||||
next_idx: uint
|
||||
}
|
||||
|
||||
/// An iterator combining two `BitvSet` iterators.
|
||||
#[deriving(Clone)]
|
||||
pub struct TwoBitPositions<'a> {
|
||||
struct TwoBitPositions<'a> {
|
||||
set: &'a BitvSet,
|
||||
other: &'a BitvSet,
|
||||
merge: fn(u32, u32) -> u32,
|
||||
@ -1764,7 +1779,17 @@ pub struct TwoBitPositions<'a> {
|
||||
next_idx: uint
|
||||
}
|
||||
|
||||
impl<'a> Iterator<uint> for BitPositions<'a> {
|
||||
#[stable]
|
||||
pub struct Union<'a>(TwoBitPositions<'a>);
|
||||
#[stable]
|
||||
pub struct Intersection<'a>(Take<TwoBitPositions<'a>>);
|
||||
#[stable]
|
||||
pub struct Difference<'a>(TwoBitPositions<'a>);
|
||||
#[stable]
|
||||
pub struct SymmetricDifference<'a>(TwoBitPositions<'a>);
|
||||
|
||||
#[stable]
|
||||
impl<'a> Iterator<uint> for SetIter<'a> {
|
||||
fn next(&mut self) -> Option<uint> {
|
||||
while self.next_idx < self.set.bitv.len() {
|
||||
let idx = self.next_idx;
|
||||
@ -1784,6 +1809,7 @@ impl<'a> Iterator<uint> for BitPositions<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a> Iterator<uint> for TwoBitPositions<'a> {
|
||||
fn next(&mut self) -> Option<uint> {
|
||||
while self.next_idx < self.set.bitv.len() ||
|
||||
@ -1819,8 +1845,29 @@ impl<'a> Iterator<uint> for TwoBitPositions<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a> Iterator<uint> for Union<'a> {
|
||||
#[inline] fn next(&mut self) -> Option<uint> { self.0.next() }
|
||||
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.0.size_hint() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a> Iterator<uint> for Intersection<'a> {
|
||||
#[inline] fn next(&mut self) -> Option<uint> { self.0.next() }
|
||||
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.0.size_hint() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a> Iterator<uint> for Difference<'a> {
|
||||
#[inline] fn next(&mut self) -> Option<uint> { self.0.next() }
|
||||
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.0.size_hint() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a> Iterator<uint> for SymmetricDifference<'a> {
|
||||
#[inline] fn next(&mut self) -> Option<uint> { self.0.next() }
|
||||
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.0.size_hint() }
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -80,6 +80,7 @@ use self::Continuation::{Continue, Finished};
|
||||
/// done on each operation isn't *catastrophic*, and *is* still bounded by O(B log<sub>B</sub>n),
|
||||
/// it is certainly much slower when it does.
|
||||
#[deriving(Clone)]
|
||||
#[stable]
|
||||
pub struct BTreeMap<K, V> {
|
||||
root: Node<K, V>,
|
||||
length: uint,
|
||||
@ -96,26 +97,31 @@ struct AbsIter<T> {
|
||||
}
|
||||
|
||||
/// An iterator over a BTreeMap's entries.
|
||||
#[stable]
|
||||
pub struct Iter<'a, K: 'a, V: 'a> {
|
||||
inner: AbsIter<Traversal<'a, K, V>>
|
||||
}
|
||||
|
||||
/// A mutable iterator over a BTreeMap's entries.
|
||||
#[stable]
|
||||
pub struct IterMut<'a, K: 'a, V: 'a> {
|
||||
inner: AbsIter<MutTraversal<'a, K, V>>
|
||||
}
|
||||
|
||||
/// An owning iterator over a BTreeMap's entries.
|
||||
#[stable]
|
||||
pub struct IntoIter<K, V> {
|
||||
inner: AbsIter<MoveTraversal<K, V>>
|
||||
}
|
||||
|
||||
/// An iterator over a BTreeMap's keys.
|
||||
#[stable]
|
||||
pub struct Keys<'a, K: 'a, V: 'a> {
|
||||
inner: Map<(&'a K, &'a V), &'a K, Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>
|
||||
}
|
||||
|
||||
/// An iterator over a BTreeMap's values.
|
||||
#[stable]
|
||||
pub struct Values<'a, K: 'a, V: 'a> {
|
||||
inner: Map<(&'a K, &'a V), &'a V, Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>
|
||||
}
|
||||
@ -141,7 +147,7 @@ pub struct OccupiedEntry<'a, K:'a, V:'a> {
|
||||
|
||||
impl<K: Ord, V> BTreeMap<K, V> {
|
||||
/// Makes a new empty BTreeMap with a reasonable choice for B.
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn new() -> BTreeMap<K, V> {
|
||||
//FIXME(Gankro): Tune this as a function of size_of<K/V>?
|
||||
BTreeMap::with_b(6)
|
||||
@ -172,7 +178,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
|
||||
/// a.clear();
|
||||
/// assert!(a.is_empty());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn clear(&mut self) {
|
||||
let b = self.b;
|
||||
// avoid recursive destructors by manually traversing the tree
|
||||
@ -208,7 +214,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
|
||||
/// assert_eq!(map.get(&1), Some(&"a"));
|
||||
/// assert_eq!(map.get(&2), None);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn get<Sized? Q>(&self, key: &Q) -> Option<&V> where Q: BorrowFrom<K> + Ord {
|
||||
let mut cur_node = &self.root;
|
||||
loop {
|
||||
@ -240,7 +246,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
|
||||
/// assert_eq!(map.contains_key(&1), true);
|
||||
/// assert_eq!(map.contains_key(&2), false);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn contains_key<Sized? Q>(&self, key: &Q) -> bool where Q: BorrowFrom<K> + Ord {
|
||||
self.get(key).is_some()
|
||||
}
|
||||
@ -270,7 +276,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
|
||||
/// assert_eq!(map[1], "b");
|
||||
/// ```
|
||||
// See `get` for implementation notes, this is basically a copy-paste with mut's added
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn get_mut<Sized? Q>(&mut self, key: &Q) -> Option<&mut V> where Q: BorrowFrom<K> + Ord {
|
||||
// temp_node is a Borrowck hack for having a mutable value outlive a loop iteration
|
||||
let mut temp_node = &mut self.root;
|
||||
@ -337,7 +343,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
|
||||
/// assert_eq!(map.insert(37, "c"), Some("b"));
|
||||
/// assert_eq!(map[37], "c");
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn insert(&mut self, mut key: K, mut value: V) -> Option<V> {
|
||||
// This is a stack of rawptrs to nodes paired with indices, respectively
|
||||
// representing the nodes and edges of our search path. We have to store rawptrs
|
||||
@ -452,7 +458,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
|
||||
/// assert_eq!(map.remove(&1), Some("a"));
|
||||
/// assert_eq!(map.remove(&1), None);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn remove<Sized? Q>(&mut self, key: &Q) -> Option<V> where Q: BorrowFrom<K> + Ord {
|
||||
// See `swap` for a more thorough description of the stuff going on in here
|
||||
let mut stack = stack::PartialSearchStack::new(self);
|
||||
@ -810,6 +816,7 @@ mod stack {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<K: Ord, V> FromIterator<(K, V)> for BTreeMap<K, V> {
|
||||
fn from_iter<T: Iterator<(K, V)>>(iter: T) -> BTreeMap<K, V> {
|
||||
let mut map = BTreeMap::new();
|
||||
@ -818,6 +825,7 @@ impl<K: Ord, V> FromIterator<(K, V)> for BTreeMap<K, V> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<K: Ord, V> Extend<(K, V)> for BTreeMap<K, V> {
|
||||
#[inline]
|
||||
fn extend<T: Iterator<(K, V)>>(&mut self, mut iter: T) {
|
||||
@ -827,6 +835,7 @@ impl<K: Ord, V> Extend<(K, V)> for BTreeMap<K, V> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<S: Writer, K: Hash<S>, V: Hash<S>> Hash<S> for BTreeMap<K, V> {
|
||||
fn hash(&self, state: &mut S) {
|
||||
for elt in self.iter() {
|
||||
@ -870,6 +879,7 @@ impl<K: Ord, V: Ord> Ord for BTreeMap<K, V> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<K: Show, V: Show> Show for BTreeMap<K, V> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
try!(write!(f, "{{"));
|
||||
@ -883,6 +893,7 @@ impl<K: Show, V: Show> Show for BTreeMap<K, V> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<K: Ord, Sized? Q, V> Index<Q, V> for BTreeMap<K, V>
|
||||
where Q: BorrowFrom<K> + Ord
|
||||
{
|
||||
@ -891,6 +902,7 @@ impl<K: Ord, Sized? Q, V> Index<Q, V> for BTreeMap<K, V>
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<K: Ord, Sized? Q, V> IndexMut<Q, V> for BTreeMap<K, V>
|
||||
where Q: BorrowFrom<K> + Ord
|
||||
{
|
||||
@ -1036,53 +1048,64 @@ impl<K, V, E, T: Traverse<E> + DoubleEndedIterator<TraversalItem<K, V, E>>>
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, K, V> Iterator<(&'a K, &'a V)> for Iter<'a, K, V> {
|
||||
fn next(&mut self) -> Option<(&'a K, &'a V)> { self.inner.next() }
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
|
||||
}
|
||||
#[stable]
|
||||
impl<'a, K, V> DoubleEndedIterator<(&'a K, &'a V)> for Iter<'a, K, V> {
|
||||
fn next_back(&mut self) -> Option<(&'a K, &'a V)> { self.inner.next_back() }
|
||||
}
|
||||
#[stable]
|
||||
impl<'a, K, V> ExactSizeIterator<(&'a K, &'a V)> for Iter<'a, K, V> {}
|
||||
|
||||
|
||||
#[stable]
|
||||
impl<'a, K, V> Iterator<(&'a K, &'a mut V)> for IterMut<'a, K, V> {
|
||||
fn next(&mut self) -> Option<(&'a K, &'a mut V)> { self.inner.next() }
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
|
||||
}
|
||||
#[stable]
|
||||
impl<'a, K, V> DoubleEndedIterator<(&'a K, &'a mut V)> for IterMut<'a, K, V> {
|
||||
fn next_back(&mut self) -> Option<(&'a K, &'a mut V)> { self.inner.next_back() }
|
||||
}
|
||||
#[stable]
|
||||
impl<'a, K, V> ExactSizeIterator<(&'a K, &'a mut V)> for IterMut<'a, K, V> {}
|
||||
|
||||
|
||||
#[stable]
|
||||
impl<K, V> Iterator<(K, V)> for IntoIter<K, V> {
|
||||
fn next(&mut self) -> Option<(K, V)> { self.inner.next() }
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
|
||||
}
|
||||
#[stable]
|
||||
impl<K, V> DoubleEndedIterator<(K, V)> for IntoIter<K, V> {
|
||||
fn next_back(&mut self) -> Option<(K, V)> { self.inner.next_back() }
|
||||
}
|
||||
#[stable]
|
||||
impl<K, V> ExactSizeIterator<(K, V)> for IntoIter<K, V> {}
|
||||
|
||||
|
||||
#[stable]
|
||||
impl<'a, K, V> Iterator<&'a K> for Keys<'a, K, V> {
|
||||
fn next(&mut self) -> Option<(&'a K)> { self.inner.next() }
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
|
||||
}
|
||||
#[stable]
|
||||
impl<'a, K, V> DoubleEndedIterator<&'a K> for Keys<'a, K, V> {
|
||||
fn next_back(&mut self) -> Option<(&'a K)> { self.inner.next_back() }
|
||||
}
|
||||
#[stable]
|
||||
impl<'a, K, V> ExactSizeIterator<&'a K> for Keys<'a, K, V> {}
|
||||
|
||||
|
||||
#[stable]
|
||||
impl<'a, K, V> Iterator<&'a V> for Values<'a, K, V> {
|
||||
fn next(&mut self) -> Option<(&'a V)> { self.inner.next() }
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
|
||||
}
|
||||
#[stable]
|
||||
impl<'a, K, V> DoubleEndedIterator<&'a V> for Values<'a, K, V> {
|
||||
fn next_back(&mut self) -> Option<(&'a V)> { self.inner.next_back() }
|
||||
}
|
||||
#[stable]
|
||||
impl<'a, K, V> ExactSizeIterator<&'a V> for Values<'a, K, V> {}
|
||||
|
||||
|
||||
@ -1143,8 +1166,8 @@ impl<K, V> BTreeMap<K, V> {
|
||||
/// let (first_key, first_value) = map.iter().next().unwrap();
|
||||
/// assert_eq!((*first_key, *first_value), (1u, "a"));
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
pub fn iter<'a>(&'a self) -> Iter<'a, K, V> {
|
||||
#[stable]
|
||||
pub fn iter(&self) -> Iter<K, V> {
|
||||
let len = self.len();
|
||||
Iter {
|
||||
inner: AbsIter {
|
||||
@ -1175,8 +1198,8 @@ impl<K, V> BTreeMap<K, V> {
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, K, V> {
|
||||
#[stable]
|
||||
pub fn iter_mut(&mut self) -> IterMut<K, V> {
|
||||
let len = self.len();
|
||||
IterMut {
|
||||
inner: AbsIter {
|
||||
@ -1204,7 +1227,7 @@ impl<K, V> BTreeMap<K, V> {
|
||||
/// println!("{}: {}", key, value);
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn into_iter(self) -> IntoIter<K, V> {
|
||||
let len = self.len();
|
||||
IntoIter {
|
||||
@ -1231,7 +1254,7 @@ impl<K, V> BTreeMap<K, V> {
|
||||
/// let keys: Vec<uint> = a.keys().cloned().collect();
|
||||
/// assert_eq!(keys, vec![1u,2,]);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
|
||||
fn first<A, B>((a, _): (A, B)) -> A { a }
|
||||
let first: fn((&'a K, &'a V)) -> &'a K = first; // coerce to fn pointer
|
||||
@ -1253,7 +1276,7 @@ impl<K, V> BTreeMap<K, V> {
|
||||
/// let values: Vec<&str> = a.values().cloned().collect();
|
||||
/// assert_eq!(values, vec!["a","b"]);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn values<'a>(&'a self) -> Values<'a, K, V> {
|
||||
fn second<A, B>((_, b): (A, B)) -> B { b }
|
||||
let second: fn((&'a K, &'a V)) -> &'a V = second; // coerce to fn pointer
|
||||
@ -1273,7 +1296,7 @@ impl<K, V> BTreeMap<K, V> {
|
||||
/// a.insert(1u, "a");
|
||||
/// assert_eq!(a.len(), 1);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn len(&self) -> uint { self.length }
|
||||
|
||||
/// Return true if the map contains no elements.
|
||||
@ -1288,7 +1311,7 @@ impl<K, V> BTreeMap<K, V> {
|
||||
/// a.insert(1u, "a");
|
||||
/// assert!(!a.is_empty());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_empty(&self) -> bool { self.len() == 0 }
|
||||
}
|
||||
|
||||
|
@ -28,39 +28,46 @@ use core::fmt::Show;
|
||||
/// See BTreeMap's documentation for a detailed discussion of this collection's performance
|
||||
/// benefits and drawbacks.
|
||||
#[deriving(Clone, Hash, PartialEq, Eq, Ord, PartialOrd)]
|
||||
#[stable]
|
||||
pub struct BTreeSet<T>{
|
||||
map: BTreeMap<T, ()>,
|
||||
}
|
||||
|
||||
/// An iterator over a BTreeSet's items.
|
||||
#[stable]
|
||||
pub struct Iter<'a, T: 'a> {
|
||||
iter: Keys<'a, T, ()>
|
||||
}
|
||||
|
||||
/// An owning iterator over a BTreeSet's items.
|
||||
#[stable]
|
||||
pub struct IntoIter<T> {
|
||||
iter: Map<(T, ()), T, ::btree_map::IntoIter<T, ()>, fn((T, ())) -> T>
|
||||
}
|
||||
|
||||
/// A lazy iterator producing elements in the set difference (in-order).
|
||||
#[stable]
|
||||
pub struct Difference<'a, T:'a> {
|
||||
a: Peekable<&'a T, Iter<'a, T>>,
|
||||
b: Peekable<&'a T, Iter<'a, T>>,
|
||||
}
|
||||
|
||||
/// A lazy iterator producing elements in the set symmetric difference (in-order).
|
||||
#[stable]
|
||||
pub struct SymmetricDifference<'a, T:'a> {
|
||||
a: Peekable<&'a T, Iter<'a, T>>,
|
||||
b: Peekable<&'a T, Iter<'a, T>>,
|
||||
}
|
||||
|
||||
/// A lazy iterator producing elements in the set intersection (in-order).
|
||||
#[stable]
|
||||
pub struct Intersection<'a, T:'a> {
|
||||
a: Peekable<&'a T, Iter<'a, T>>,
|
||||
b: Peekable<&'a T, Iter<'a, T>>,
|
||||
}
|
||||
|
||||
/// A lazy iterator producing elements in the set union (in-order).
|
||||
#[stable]
|
||||
pub struct Union<'a, T:'a> {
|
||||
a: Peekable<&'a T, Iter<'a, T>>,
|
||||
b: Peekable<&'a T, Iter<'a, T>>,
|
||||
@ -76,7 +83,7 @@ impl<T: Ord> BTreeSet<T> {
|
||||
///
|
||||
/// let mut set: BTreeSet<int> = BTreeSet::new();
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn new() -> BTreeSet<T> {
|
||||
BTreeSet { map: BTreeMap::new() }
|
||||
}
|
||||
@ -84,6 +91,7 @@ impl<T: Ord> BTreeSet<T> {
|
||||
/// Makes a new BTreeSet with the given B.
|
||||
///
|
||||
/// B cannot be less than 2.
|
||||
#[unstable = "probably want this to be on the type, eventually"]
|
||||
pub fn with_b(b: uint) -> BTreeSet<T> {
|
||||
BTreeSet { map: BTreeMap::with_b(b) }
|
||||
}
|
||||
@ -106,8 +114,8 @@ impl<T> BTreeSet<T> {
|
||||
/// let v: Vec<uint> = set.iter().map(|&x| x).collect();
|
||||
/// assert_eq!(v, vec![1u,2,3,4]);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
pub fn iter<'a>(&'a self) -> Iter<'a, T> {
|
||||
#[stable]
|
||||
pub fn iter(&self) -> Iter<T> {
|
||||
Iter { iter: self.map.keys() }
|
||||
}
|
||||
|
||||
@ -123,7 +131,7 @@ impl<T> BTreeSet<T> {
|
||||
/// let v: Vec<uint> = set.into_iter().collect();
|
||||
/// assert_eq!(v, vec![1u,2,3,4]);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn into_iter(self) -> IntoIter<T> {
|
||||
fn first<A, B>((a, _): (A, B)) -> A { a }
|
||||
let first: fn((T, ())) -> T = first; // coerce to fn pointer
|
||||
@ -151,7 +159,7 @@ impl<T: Ord> BTreeSet<T> {
|
||||
/// let diff: Vec<uint> = a.difference(&b).cloned().collect();
|
||||
/// assert_eq!(diff, vec![1u]);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn difference<'a>(&'a self, other: &'a BTreeSet<T>) -> Difference<'a, T> {
|
||||
Difference{a: self.iter().peekable(), b: other.iter().peekable()}
|
||||
}
|
||||
@ -174,7 +182,7 @@ impl<T: Ord> BTreeSet<T> {
|
||||
/// let sym_diff: Vec<uint> = a.symmetric_difference(&b).cloned().collect();
|
||||
/// assert_eq!(sym_diff, vec![1u,3]);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn symmetric_difference<'a>(&'a self, other: &'a BTreeSet<T>)
|
||||
-> SymmetricDifference<'a, T> {
|
||||
SymmetricDifference{a: self.iter().peekable(), b: other.iter().peekable()}
|
||||
@ -198,7 +206,7 @@ impl<T: Ord> BTreeSet<T> {
|
||||
/// let intersection: Vec<uint> = a.intersection(&b).cloned().collect();
|
||||
/// assert_eq!(intersection, vec![2u]);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn intersection<'a>(&'a self, other: &'a BTreeSet<T>)
|
||||
-> Intersection<'a, T> {
|
||||
Intersection{a: self.iter().peekable(), b: other.iter().peekable()}
|
||||
@ -220,7 +228,7 @@ impl<T: Ord> BTreeSet<T> {
|
||||
/// let union: Vec<uint> = a.union(&b).cloned().collect();
|
||||
/// assert_eq!(union, vec![1u,2]);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn union<'a>(&'a self, other: &'a BTreeSet<T>) -> Union<'a, T> {
|
||||
Union{a: self.iter().peekable(), b: other.iter().peekable()}
|
||||
}
|
||||
@ -237,7 +245,7 @@ impl<T: Ord> BTreeSet<T> {
|
||||
/// v.insert(1i);
|
||||
/// assert_eq!(v.len(), 1);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn len(&self) -> uint { self.map.len() }
|
||||
|
||||
/// Returns true if the set contains no elements
|
||||
@ -252,7 +260,7 @@ impl<T: Ord> BTreeSet<T> {
|
||||
/// v.insert(1i);
|
||||
/// assert!(!v.is_empty());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_empty(&self) -> bool { self.len() == 0 }
|
||||
|
||||
/// Clears the set, removing all values.
|
||||
@ -267,7 +275,7 @@ impl<T: Ord> BTreeSet<T> {
|
||||
/// v.clear();
|
||||
/// assert!(v.is_empty());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn clear(&mut self) {
|
||||
self.map.clear()
|
||||
}
|
||||
@ -287,7 +295,7 @@ impl<T: Ord> BTreeSet<T> {
|
||||
/// assert_eq!(set.contains(&1), true);
|
||||
/// assert_eq!(set.contains(&4), false);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn contains<Sized? Q>(&self, value: &Q) -> bool where Q: BorrowFrom<T> + Ord {
|
||||
self.map.contains_key(value)
|
||||
}
|
||||
@ -309,7 +317,7 @@ impl<T: Ord> BTreeSet<T> {
|
||||
/// b.insert(1);
|
||||
/// assert_eq!(a.is_disjoint(&b), false);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_disjoint(&self, other: &BTreeSet<T>) -> bool {
|
||||
self.intersection(other).next().is_none()
|
||||
}
|
||||
@ -330,7 +338,7 @@ impl<T: Ord> BTreeSet<T> {
|
||||
/// set.insert(4);
|
||||
/// assert_eq!(set.is_subset(&sup), false);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_subset(&self, other: &BTreeSet<T>) -> bool {
|
||||
// Stolen from TreeMap
|
||||
let mut x = self.iter();
|
||||
@ -375,7 +383,7 @@ impl<T: Ord> BTreeSet<T> {
|
||||
/// set.insert(2);
|
||||
/// assert_eq!(set.is_superset(&sub), true);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_superset(&self, other: &BTreeSet<T>) -> bool {
|
||||
other.is_subset(self)
|
||||
}
|
||||
@ -394,7 +402,7 @@ impl<T: Ord> BTreeSet<T> {
|
||||
/// assert_eq!(set.insert(2i), false);
|
||||
/// assert_eq!(set.len(), 1);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn insert(&mut self, value: T) -> bool {
|
||||
self.map.insert(value, ()).is_none()
|
||||
}
|
||||
@ -417,12 +425,13 @@ impl<T: Ord> BTreeSet<T> {
|
||||
/// assert_eq!(set.remove(&2), true);
|
||||
/// assert_eq!(set.remove(&2), false);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn remove<Sized? Q>(&mut self, value: &Q) -> bool where Q: BorrowFrom<T> + Ord {
|
||||
self.map.remove(value).is_some()
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<T: Ord> FromIterator<T> for BTreeSet<T> {
|
||||
fn from_iter<Iter: Iterator<T>>(iter: Iter) -> BTreeSet<T> {
|
||||
let mut set = BTreeSet::new();
|
||||
@ -431,6 +440,7 @@ impl<T: Ord> FromIterator<T> for BTreeSet<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<T: Ord> Extend<T> for BTreeSet<T> {
|
||||
#[inline]
|
||||
fn extend<Iter: Iterator<T>>(&mut self, mut iter: Iter) {
|
||||
@ -448,7 +458,7 @@ impl<T: Ord> Default for BTreeSet<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
impl<'a, 'b, T: Ord + Clone> Sub<&'b BTreeSet<T>, BTreeSet<T>> for &'a BTreeSet<T> {
|
||||
/// Returns the difference of `self` and `rhs` as a new `BTreeSet<T>`.
|
||||
///
|
||||
@ -469,7 +479,7 @@ impl<'a, 'b, T: Ord + Clone> Sub<&'b BTreeSet<T>, BTreeSet<T>> for &'a BTreeSet<
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
impl<'a, 'b, T: Ord + Clone> BitXor<&'b BTreeSet<T>, BTreeSet<T>> for &'a BTreeSet<T> {
|
||||
/// Returns the symmetric difference of `self` and `rhs` as a new `BTreeSet<T>`.
|
||||
///
|
||||
@ -490,7 +500,7 @@ impl<'a, 'b, T: Ord + Clone> BitXor<&'b BTreeSet<T>, BTreeSet<T>> for &'a BTreeS
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
impl<'a, 'b, T: Ord + Clone> BitAnd<&'b BTreeSet<T>, BTreeSet<T>> for &'a BTreeSet<T> {
|
||||
/// Returns the intersection of `self` and `rhs` as a new `BTreeSet<T>`.
|
||||
///
|
||||
@ -511,7 +521,7 @@ impl<'a, 'b, T: Ord + Clone> BitAnd<&'b BTreeSet<T>, BTreeSet<T>> for &'a BTreeS
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
impl<'a, 'b, T: Ord + Clone> BitOr<&'b BTreeSet<T>, BTreeSet<T>> for &'a BTreeSet<T> {
|
||||
/// Returns the union of `self` and `rhs` as a new `BTreeSet<T>`.
|
||||
///
|
||||
@ -532,6 +542,7 @@ impl<'a, 'b, T: Ord + Clone> BitOr<&'b BTreeSet<T>, BTreeSet<T>> for &'a BTreeSe
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<T: Show> Show for BTreeSet<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
try!(write!(f, "{{"));
|
||||
@ -545,23 +556,28 @@ impl<T: Show> Show for BTreeSet<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T> Iterator<&'a T> for Iter<'a, T> {
|
||||
fn next(&mut self) -> Option<&'a T> { self.iter.next() }
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
|
||||
}
|
||||
#[stable]
|
||||
impl<'a, T> DoubleEndedIterator<&'a T> for Iter<'a, T> {
|
||||
fn next_back(&mut self) -> Option<&'a T> { self.iter.next_back() }
|
||||
}
|
||||
#[stable]
|
||||
impl<'a, T> ExactSizeIterator<&'a T> for Iter<'a, T> {}
|
||||
|
||||
|
||||
#[stable]
|
||||
impl<T> Iterator<T> for IntoIter<T> {
|
||||
fn next(&mut self) -> Option<T> { self.iter.next() }
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
|
||||
}
|
||||
#[stable]
|
||||
impl<T> DoubleEndedIterator<T> for IntoIter<T> {
|
||||
fn next_back(&mut self) -> Option<T> { self.iter.next_back() }
|
||||
}
|
||||
#[stable]
|
||||
impl<T> ExactSizeIterator<T> for IntoIter<T> {}
|
||||
|
||||
/// Compare `x` and `y`, but return `short` if x is None and `long` if y is None
|
||||
@ -574,6 +590,7 @@ fn cmp_opt<T: Ord>(x: Option<&T>, y: Option<&T>,
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T: Ord> Iterator<&'a T> for Difference<'a, T> {
|
||||
fn next(&mut self) -> Option<&'a T> {
|
||||
loop {
|
||||
@ -586,6 +603,7 @@ impl<'a, T: Ord> Iterator<&'a T> for Difference<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T: Ord> Iterator<&'a T> for SymmetricDifference<'a, T> {
|
||||
fn next(&mut self) -> Option<&'a T> {
|
||||
loop {
|
||||
@ -598,6 +616,7 @@ impl<'a, T: Ord> Iterator<&'a T> for SymmetricDifference<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T: Ord> Iterator<&'a T> for Intersection<'a, T> {
|
||||
fn next(&mut self) -> Option<&'a T> {
|
||||
loop {
|
||||
@ -616,6 +635,7 @@ impl<'a, T: Ord> Iterator<&'a T> for Intersection<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T: Ord> Iterator<&'a T> for Union<'a, T> {
|
||||
fn next(&mut self) -> Option<&'a T> {
|
||||
loop {
|
||||
|
@ -30,6 +30,7 @@ use core::ptr;
|
||||
use std::hash::{Writer, Hash};
|
||||
|
||||
/// A doubly-linked list.
|
||||
#[stable]
|
||||
pub struct DList<T> {
|
||||
length: uint,
|
||||
list_head: Link<T>,
|
||||
@ -53,20 +54,27 @@ struct Node<T> {
|
||||
}
|
||||
|
||||
/// An iterator over references to the items of a `DList`.
|
||||
#[stable]
|
||||
pub struct Iter<'a, T:'a> {
|
||||
head: &'a Link<T>,
|
||||
tail: Rawlink<Node<T>>,
|
||||
nelem: uint,
|
||||
}
|
||||
|
||||
// FIXME #11820: the &'a Option<> of the Link stops clone working.
|
||||
// FIXME #19839: deriving is too aggressive on the bounds (T doesn't need to be Clone).
|
||||
#[stable]
|
||||
impl<'a, T> Clone for Iter<'a, T> {
|
||||
fn clone(&self) -> Iter<'a, T> { *self }
|
||||
fn clone(&self) -> Iter<'a, T> {
|
||||
Iter {
|
||||
head: self.head.clone(),
|
||||
tail: self.tail,
|
||||
nelem: self.nelem,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a,T> Copy for Iter<'a,T> {}
|
||||
|
||||
/// An iterator over mutable references to the items of a `DList`.
|
||||
#[stable]
|
||||
pub struct IterMut<'a, T:'a> {
|
||||
list: &'a mut DList<T>,
|
||||
head: Rawlink<Node<T>>,
|
||||
@ -76,6 +84,7 @@ pub struct IterMut<'a, T:'a> {
|
||||
|
||||
/// An iterator over mutable references to the items of a `DList`.
|
||||
#[deriving(Clone)]
|
||||
#[stable]
|
||||
pub struct IntoIter<T> {
|
||||
list: DList<T>
|
||||
}
|
||||
@ -204,59 +213,21 @@ impl<T> Default for DList<T> {
|
||||
impl<T> DList<T> {
|
||||
/// Creates an empty `DList`.
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn new() -> DList<T> {
|
||||
DList{list_head: None, list_tail: Rawlink::none(), length: 0}
|
||||
}
|
||||
|
||||
/// Moves the last element to the front of the list.
|
||||
///
|
||||
/// If the list is empty, does nothing.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use std::collections::DList;
|
||||
///
|
||||
/// let mut dl = DList::new();
|
||||
/// dl.push_back(1i);
|
||||
/// dl.push_back(2);
|
||||
/// dl.push_back(3);
|
||||
///
|
||||
/// dl.rotate_forward();
|
||||
///
|
||||
/// for e in dl.iter() {
|
||||
/// println!("{}", e); // prints 3, then 1, then 2
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
/// Deprecated: Not clearly useful enough; use split and append when available.
|
||||
#[deprecated = "Not clearly useful enough; use split and append when available"]
|
||||
pub fn rotate_forward(&mut self) {
|
||||
self.pop_back_node().map(|tail| {
|
||||
self.push_front_node(tail)
|
||||
});
|
||||
}
|
||||
|
||||
/// Moves the first element to the back of the list.
|
||||
///
|
||||
/// If the list is empty, does nothing.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use std::collections::DList;
|
||||
///
|
||||
/// let mut dl = DList::new();
|
||||
/// dl.push_back(1i);
|
||||
/// dl.push_back(2);
|
||||
/// dl.push_back(3);
|
||||
///
|
||||
/// dl.rotate_backward();
|
||||
///
|
||||
/// for e in dl.iter() {
|
||||
/// println!("{}", e); // prints 2, then 3, then 1
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
/// Deprecated: Not clearly useful enough; use split and append when available.
|
||||
#[deprecated = "Not clearly useful enough; use split and append when available"]
|
||||
pub fn rotate_backward(&mut self) {
|
||||
self.pop_front_node().map(|head| {
|
||||
self.push_back_node(head)
|
||||
@ -285,6 +256,7 @@ impl<T> DList<T> {
|
||||
/// println!("{}", e); // prints 1, then 2, then 3, then 4
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable = "append should be by-mutable-reference"]
|
||||
pub fn append(&mut self, mut other: DList<T>) {
|
||||
match self.list_tail.resolve() {
|
||||
None => *self = other,
|
||||
@ -304,57 +276,15 @@ impl<T> DList<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds all elements from `other` to the beginning of the list.
|
||||
///
|
||||
/// This operation should compute in O(1) time.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use std::collections::DList;
|
||||
///
|
||||
/// let mut a = DList::new();
|
||||
/// let mut b = DList::new();
|
||||
/// a.push_back(1i);
|
||||
/// a.push_back(2);
|
||||
/// b.push_back(3i);
|
||||
/// b.push_back(4);
|
||||
///
|
||||
/// a.prepend(b);
|
||||
///
|
||||
/// for e in a.iter() {
|
||||
/// println!("{}", e); // prints 3, then 4, then 1, then 2
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
/// Deprecated: Use append and a swap instead.
|
||||
#[deprecated = "Use append and a swap instead"]
|
||||
pub fn prepend(&mut self, mut other: DList<T>) {
|
||||
mem::swap(self, &mut other);
|
||||
self.append(other);
|
||||
}
|
||||
|
||||
/// Inserts `elt` before the first `x` in the list where `f(x, elt)` is
|
||||
/// true, or at the end.
|
||||
///
|
||||
/// This operation should compute in O(N) time.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use std::collections::DList;
|
||||
///
|
||||
/// let mut a: DList<int> = DList::new();
|
||||
/// a.push_back(2i);
|
||||
/// a.push_back(4);
|
||||
/// a.push_back(7);
|
||||
/// a.push_back(8);
|
||||
///
|
||||
/// // insert 11 before the first odd number in the list
|
||||
/// a.insert_when(11, |&e, _| e % 2 == 1);
|
||||
///
|
||||
/// for e in a.iter() {
|
||||
/// println!("{}", e); // prints 2, then 4, then 11, then 7, then 8
|
||||
/// }
|
||||
/// ```
|
||||
/// Deprecated: Use custom methods on IterMut.
|
||||
#[deprecated = "Use custom methods on IterMut"]
|
||||
pub fn insert_when<F>(&mut self, elt: T, mut f: F) where F: FnMut(&T, &T) -> bool {
|
||||
let mut it = self.iter_mut();
|
||||
loop {
|
||||
@ -367,12 +297,8 @@ impl<T> DList<T> {
|
||||
it.insert_next(elt);
|
||||
}
|
||||
|
||||
/// Merges `other` into this `DList`, using the function `f`.
|
||||
///
|
||||
/// Iterates both `DList`s with `a` from self and `b` from `other`, and
|
||||
/// put `a` in the result if `f(a, b)` is true, and otherwise `b`.
|
||||
///
|
||||
/// This operation should compute in O(max(N, M)) time.
|
||||
/// Deprecated: Use custom methods on IterMut.
|
||||
#[deprecated = "Use custom methods on IterMut"]
|
||||
pub fn merge<F>(&mut self, mut other: DList<T>, mut f: F) where F: FnMut(&T, &T) -> bool {
|
||||
{
|
||||
let mut it = self.iter_mut();
|
||||
@ -395,15 +321,15 @@ impl<T> DList<T> {
|
||||
|
||||
/// Provides a forward iterator.
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
pub fn iter<'a>(&'a self) -> Iter<'a, T> {
|
||||
#[stable]
|
||||
pub fn iter(&self) -> Iter<T> {
|
||||
Iter{nelem: self.len(), head: &self.list_head, tail: self.list_tail}
|
||||
}
|
||||
|
||||
/// Provides a forward iterator with mutable references.
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, T> {
|
||||
#[stable]
|
||||
pub fn iter_mut(&mut self) -> IterMut<T> {
|
||||
let head_raw = match self.list_head {
|
||||
Some(ref mut h) => Rawlink::some(&mut **h),
|
||||
None => Rawlink::none(),
|
||||
@ -418,7 +344,7 @@ impl<T> DList<T> {
|
||||
|
||||
/// Consumes the list into an iterator yielding elements by value.
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn into_iter(self) -> IntoIter<T> {
|
||||
IntoIter{list: self}
|
||||
}
|
||||
@ -427,7 +353,7 @@ impl<T> DList<T> {
|
||||
///
|
||||
/// This operation should compute in O(1) time.
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.list_head.is_none()
|
||||
}
|
||||
@ -436,7 +362,7 @@ impl<T> DList<T> {
|
||||
///
|
||||
/// This operation should compute in O(1) time.
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn len(&self) -> uint {
|
||||
self.length
|
||||
}
|
||||
@ -445,7 +371,7 @@ impl<T> DList<T> {
|
||||
///
|
||||
/// This operation should compute in O(n) time.
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn clear(&mut self) {
|
||||
*self = DList::new()
|
||||
}
|
||||
@ -485,7 +411,7 @@ impl<T> DList<T> {
|
||||
/// Adds an element first in the list.
|
||||
///
|
||||
/// This operation should compute in O(1) time.
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn push_front(&mut self, elt: T) {
|
||||
self.push_front_node(box Node::new(elt))
|
||||
}
|
||||
@ -494,7 +420,7 @@ impl<T> DList<T> {
|
||||
/// empty.
|
||||
///
|
||||
/// This operation should compute in O(1) time.
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn pop_front(&mut self) -> Option<T> {
|
||||
self.pop_front_node().map(|box Node{value, ..}| value)
|
||||
}
|
||||
@ -517,7 +443,7 @@ impl<T> DList<T> {
|
||||
/// d.push_back(3);
|
||||
/// assert_eq!(3, *d.back().unwrap());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn push_back(&mut self, elt: T) {
|
||||
self.push_back_node(box Node::new(elt))
|
||||
}
|
||||
@ -542,23 +468,23 @@ impl<T> DList<T> {
|
||||
/// d.push_back(3);
|
||||
/// assert_eq!(d.pop_back(), Some(3));
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn pop_back(&mut self) -> Option<T> {
|
||||
self.pop_back_node().map(|box Node{value, ..}| value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Ord> DList<T> {
|
||||
/// Inserts `elt` sorted in ascending order.
|
||||
///
|
||||
/// This operation should compute in O(N) time.
|
||||
#[inline]
|
||||
/// Deprecated: Why are you maintaining a sorted DList?
|
||||
#[deprecated = "Why are you maintaining a sorted DList?"]
|
||||
#[allow(deprecated)]
|
||||
pub fn insert_ordered(&mut self, elt: T) {
|
||||
self.insert_when(elt, |a, b| a >= b)
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe_destructor]
|
||||
#[stable]
|
||||
impl<T> Drop for DList<T> {
|
||||
fn drop(&mut self) {
|
||||
// Dissolve the dlist in backwards direction
|
||||
@ -580,7 +506,7 @@ impl<T> Drop for DList<T> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[stable]
|
||||
impl<'a, A> Iterator<&'a A> for Iter<'a, A> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<&'a A> {
|
||||
@ -600,6 +526,7 @@ impl<'a, A> Iterator<&'a A> for Iter<'a, A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, A> DoubleEndedIterator<&'a A> for Iter<'a, A> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<&'a A> {
|
||||
@ -614,8 +541,10 @@ impl<'a, A> DoubleEndedIterator<&'a A> for Iter<'a, A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, A> ExactSizeIterator<&'a A> for Iter<'a, A> {}
|
||||
|
||||
#[stable]
|
||||
impl<'a, A> Iterator<&'a mut A> for IterMut<'a, A> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<&'a mut A> {
|
||||
@ -638,6 +567,7 @@ impl<'a, A> Iterator<&'a mut A> for IterMut<'a, A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, A> DoubleEndedIterator<&'a mut A> for IterMut<'a, A> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<&'a mut A> {
|
||||
@ -652,6 +582,7 @@ impl<'a, A> DoubleEndedIterator<&'a mut A> for IterMut<'a, A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, A> ExactSizeIterator<&'a mut A> for IterMut<'a, A> {}
|
||||
|
||||
/// Allows mutating a `DList` while iterating.
|
||||
@ -713,6 +644,7 @@ impl<'a, A> IterMut<'a, A> {
|
||||
/// }
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "this is probably better handled by a cursor type -- we'll see"]
|
||||
pub fn insert_next(&mut self, elt: A) {
|
||||
self.insert_next_node(box Node::new(elt))
|
||||
}
|
||||
@ -733,6 +665,7 @@ impl<'a, A> IterMut<'a, A> {
|
||||
/// assert_eq!(it.next().unwrap(), &2);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "this is probably better handled by a cursor type -- we'll see"]
|
||||
pub fn peek_next(&mut self) -> Option<&mut A> {
|
||||
if self.nelem == 0 {
|
||||
return None
|
||||
@ -741,6 +674,7 @@ impl<'a, A> IterMut<'a, A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<A> Iterator<A> for IntoIter<A> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<A> { self.list.pop_front() }
|
||||
@ -751,11 +685,13 @@ impl<A> Iterator<A> for IntoIter<A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<A> DoubleEndedIterator<A> for IntoIter<A> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<A> { self.list.pop_back() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<A> FromIterator<A> for DList<A> {
|
||||
fn from_iter<T: Iterator<A>>(iterator: T) -> DList<A> {
|
||||
let mut ret = DList::new();
|
||||
@ -764,6 +700,7 @@ impl<A> FromIterator<A> for DList<A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<A> Extend<A> for DList<A> {
|
||||
fn extend<T: Iterator<A>>(&mut self, mut iterator: T) {
|
||||
for elt in iterator { self.push_back(elt); }
|
||||
@ -808,6 +745,7 @@ impl<A: Clone> Clone for DList<A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<A: fmt::Show> fmt::Show for DList<A> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
try!(write!(f, "["));
|
||||
@ -821,6 +759,7 @@ impl<A: fmt::Show> fmt::Show for DList<A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<S: Writer, A: Hash<S>> Hash<S> for DList<A> {
|
||||
fn hash(&self, state: &mut S) {
|
||||
self.len().hash(state);
|
||||
|
@ -64,11 +64,12 @@ pub mod vec;
|
||||
pub mod vec_map;
|
||||
|
||||
pub mod bitv {
|
||||
pub use bit::{Bitv, Bits, from_fn, from_bytes};
|
||||
pub use bit::{Bitv, Iter, from_fn, from_bytes};
|
||||
}
|
||||
|
||||
pub mod bitv_set {
|
||||
pub use bit::{BitvSet, BitPositions, TwoBitPositions};
|
||||
pub use bit::{BitvSet, Union, Intersection, Difference, SymmetricDifference};
|
||||
pub use bit::SetIter as Iter;
|
||||
}
|
||||
|
||||
pub mod btree_map {
|
||||
|
@ -35,6 +35,7 @@ static MINIMUM_CAPACITY: uint = 2u;
|
||||
// be scrapped anyway. Defer to rewrite?
|
||||
|
||||
/// `RingBuf` is a circular buffer, which can be used as a double-ended queue efficiently.
|
||||
#[stable]
|
||||
pub struct RingBuf<T> {
|
||||
// tail and head are pointers into the buffer. Tail always points
|
||||
// to the first element that could be read, Head always points
|
||||
@ -62,6 +63,7 @@ impl<T: Clone> Clone for RingBuf<T> {
|
||||
}
|
||||
|
||||
#[unsafe_destructor]
|
||||
#[stable]
|
||||
impl<T> Drop for RingBuf<T> {
|
||||
fn drop(&mut self) {
|
||||
self.clear();
|
||||
@ -77,7 +79,6 @@ impl<T> Drop for RingBuf<T> {
|
||||
|
||||
#[stable]
|
||||
impl<T> Default for RingBuf<T> {
|
||||
#[stable]
|
||||
#[inline]
|
||||
fn default() -> RingBuf<T> { RingBuf::new() }
|
||||
}
|
||||
@ -85,13 +86,13 @@ impl<T> Default for RingBuf<T> {
|
||||
impl<T> RingBuf<T> {
|
||||
/// Turn ptr into a slice
|
||||
#[inline]
|
||||
unsafe fn buffer_as_slice<'a>(&'a self) -> &'a [T] {
|
||||
unsafe fn buffer_as_slice(&self) -> &[T] {
|
||||
mem::transmute(RawSlice { data: self.ptr as *const T, len: self.cap })
|
||||
}
|
||||
|
||||
/// Turn ptr into a mut slice
|
||||
#[inline]
|
||||
unsafe fn buffer_as_mut_slice<'a>(&'a mut self) -> &'a mut [T] {
|
||||
unsafe fn buffer_as_mut_slice(&mut self) -> &mut [T] {
|
||||
mem::transmute(RawSlice { data: self.ptr as *const T, len: self.cap })
|
||||
}
|
||||
|
||||
@ -131,13 +132,13 @@ impl<T> RingBuf<T> {
|
||||
|
||||
impl<T> RingBuf<T> {
|
||||
/// Creates an empty `RingBuf`.
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn new() -> RingBuf<T> {
|
||||
RingBuf::with_capacity(INITIAL_CAPACITY)
|
||||
}
|
||||
|
||||
/// Creates an empty `RingBuf` with space for at least `n` elements.
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn with_capacity(n: uint) -> RingBuf<T> {
|
||||
// +1 since the ringbuffer always leaves one space empty
|
||||
let cap = cmp::max(n + 1, MINIMUM_CAPACITY).next_power_of_two();
|
||||
@ -175,7 +176,7 @@ impl<T> RingBuf<T> {
|
||||
/// buf.push_back(5);
|
||||
/// assert_eq!(buf.get(1).unwrap(), &4);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn get(&self, i: uint) -> Option<&T> {
|
||||
if i < self.len() {
|
||||
let idx = self.wrap_index(self.tail + i);
|
||||
@ -205,7 +206,7 @@ impl<T> RingBuf<T> {
|
||||
///
|
||||
/// assert_eq!(buf[1], 7);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn get_mut(&mut self, i: uint) -> Option<&mut T> {
|
||||
if i < self.len() {
|
||||
let idx = self.wrap_index(self.tail + i);
|
||||
@ -257,7 +258,7 @@ impl<T> RingBuf<T> {
|
||||
/// assert!(buf.capacity() >= 10);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn capacity(&self) -> uint { self.cap - 1 }
|
||||
|
||||
/// Reserves the minimum capacity for exactly `additional` more elements to be inserted in the
|
||||
@ -280,7 +281,7 @@ impl<T> RingBuf<T> {
|
||||
/// buf.reserve_exact(10);
|
||||
/// assert!(buf.capacity() >= 11);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn reserve_exact(&mut self, additional: uint) {
|
||||
self.reserve(additional);
|
||||
}
|
||||
@ -301,7 +302,7 @@ impl<T> RingBuf<T> {
|
||||
/// buf.reserve(10);
|
||||
/// assert!(buf.capacity() >= 11);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn reserve(&mut self, additional: uint) {
|
||||
let new_len = self.len() + additional;
|
||||
assert!(new_len + 1 > self.len(), "capacity overflow");
|
||||
@ -382,7 +383,7 @@ impl<T> RingBuf<T> {
|
||||
/// let b: &[_] = &[&5, &3, &4];
|
||||
/// assert_eq!(buf.iter().collect::<Vec<&int>>().as_slice(), b);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn iter(&self) -> Iter<T> {
|
||||
Iter {
|
||||
tail: self.tail,
|
||||
@ -408,7 +409,7 @@ impl<T> RingBuf<T> {
|
||||
/// let b: &[_] = &[&mut 3, &mut 1, &mut 2];
|
||||
/// assert_eq!(buf.iter_mut().collect::<Vec<&mut int>>()[], b);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, T> {
|
||||
IterMut {
|
||||
tail: self.tail,
|
||||
@ -420,7 +421,7 @@ impl<T> RingBuf<T> {
|
||||
}
|
||||
|
||||
/// Consumes the list into an iterator yielding elements by value.
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn into_iter(self) -> IntoIter<T> {
|
||||
IntoIter {
|
||||
inner: self,
|
||||
@ -481,7 +482,7 @@ impl<T> RingBuf<T> {
|
||||
/// v.push_back(1i);
|
||||
/// assert_eq!(v.len(), 1);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn len(&self) -> uint { count(self.tail, self.head, self.cap) }
|
||||
|
||||
/// Returns true if the buffer contains no elements
|
||||
@ -496,7 +497,7 @@ impl<T> RingBuf<T> {
|
||||
/// v.push_front(1i);
|
||||
/// assert!(!v.is_empty());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_empty(&self) -> bool { self.len() == 0 }
|
||||
|
||||
/// Creates a draining iterator that clears the `RingBuf` and iterates over
|
||||
@ -514,7 +515,7 @@ impl<T> RingBuf<T> {
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
pub fn drain<'a>(&'a mut self) -> Drain<'a, T> {
|
||||
pub fn drain(&mut self) -> Drain<T> {
|
||||
Drain {
|
||||
inner: self,
|
||||
}
|
||||
@ -532,7 +533,7 @@ impl<T> RingBuf<T> {
|
||||
/// v.clear();
|
||||
/// assert!(v.is_empty());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
#[inline]
|
||||
pub fn clear(&mut self) {
|
||||
self.drain();
|
||||
@ -643,7 +644,7 @@ impl<T> RingBuf<T> {
|
||||
/// assert_eq!(d.pop_front(), Some(2i));
|
||||
/// assert_eq!(d.pop_front(), None);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn pop_front(&mut self) -> Option<T> {
|
||||
if self.is_empty() {
|
||||
None
|
||||
@ -666,7 +667,7 @@ impl<T> RingBuf<T> {
|
||||
/// d.push_front(2i);
|
||||
/// assert_eq!(d.front(), Some(&2i));
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn push_front(&mut self, t: T) {
|
||||
if self.is_full() {
|
||||
self.reserve(1);
|
||||
@ -696,7 +697,7 @@ impl<T> RingBuf<T> {
|
||||
/// buf.push_back(3);
|
||||
/// assert_eq!(3, *buf.back().unwrap());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn push_back(&mut self, t: T) {
|
||||
if self.is_full() {
|
||||
self.reserve(1);
|
||||
@ -728,7 +729,7 @@ impl<T> RingBuf<T> {
|
||||
/// buf.push_back(3);
|
||||
/// assert_eq!(buf.pop_back(), Some(3));
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn pop_back(&mut self) -> Option<T> {
|
||||
if self.is_empty() {
|
||||
None
|
||||
@ -966,7 +967,7 @@ impl<T> RingBuf<T> {
|
||||
/// buf.remove(2);
|
||||
/// assert_eq!(Some(&15), buf.get(2));
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification; waiting on panic semantics"]
|
||||
#[stable]
|
||||
pub fn remove(&mut self, i: uint) -> Option<T> {
|
||||
if self.is_empty() || self.len() <= i {
|
||||
return None;
|
||||
@ -1129,6 +1130,7 @@ fn count(tail: uint, head: uint, size: uint) -> uint {
|
||||
}
|
||||
|
||||
/// `RingBuf` iterator.
|
||||
#[stable]
|
||||
pub struct Iter<'a, T:'a> {
|
||||
ring: &'a [T],
|
||||
tail: uint,
|
||||
@ -1146,6 +1148,7 @@ impl<'a, T> Clone for Iter<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T> Iterator<&'a T> for Iter<'a, T> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<&'a T> {
|
||||
@ -1164,6 +1167,7 @@ impl<'a, T> Iterator<&'a T> for Iter<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T> DoubleEndedIterator<&'a T> for Iter<'a, T> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<&'a T> {
|
||||
@ -1175,8 +1179,10 @@ impl<'a, T> DoubleEndedIterator<&'a T> for Iter<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T> ExactSizeIterator<&'a T> for Iter<'a, T> {}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T> RandomAccessIterator<&'a T> for Iter<'a, T> {
|
||||
#[inline]
|
||||
fn indexable(&self) -> uint {
|
||||
@ -1199,6 +1205,7 @@ impl<'a, T> RandomAccessIterator<&'a T> for Iter<'a, T> {
|
||||
// with returning the mutable reference. I couldn't find a way to
|
||||
// make the lifetime checker happy so, but there should be a way.
|
||||
/// `RingBuf` mutable iterator.
|
||||
#[stable]
|
||||
pub struct IterMut<'a, T:'a> {
|
||||
ptr: *mut T,
|
||||
tail: uint,
|
||||
@ -1207,6 +1214,7 @@ pub struct IterMut<'a, T:'a> {
|
||||
marker: marker::ContravariantLifetime<'a>,
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T> Iterator<&'a mut T> for IterMut<'a, T> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<&'a mut T> {
|
||||
@ -1228,6 +1236,7 @@ impl<'a, T> Iterator<&'a mut T> for IterMut<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T> DoubleEndedIterator<&'a mut T> for IterMut<'a, T> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<&'a mut T> {
|
||||
@ -1242,13 +1251,16 @@ impl<'a, T> DoubleEndedIterator<&'a mut T> for IterMut<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T> ExactSizeIterator<&'a mut T> for IterMut<'a, T> {}
|
||||
|
||||
// A by-value RingBuf iterator
|
||||
/// A by-value RingBuf iterator
|
||||
#[stable]
|
||||
pub struct IntoIter<T> {
|
||||
inner: RingBuf<T>,
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<T> Iterator<T> for IntoIter<T> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<T> {
|
||||
@ -1262,6 +1274,7 @@ impl<T> Iterator<T> for IntoIter<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<T> DoubleEndedIterator<T> for IntoIter<T> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<T> {
|
||||
@ -1269,14 +1282,17 @@ impl<T> DoubleEndedIterator<T> for IntoIter<T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<T> ExactSizeIterator<T> for IntoIter<T> {}
|
||||
|
||||
/// A draining RingBuf iterator
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
pub struct Drain<'a, T: 'a> {
|
||||
inner: &'a mut RingBuf<T>,
|
||||
}
|
||||
|
||||
#[unsafe_destructor]
|
||||
#[stable]
|
||||
impl<'a, T: 'a> Drop for Drain<'a, T> {
|
||||
fn drop(&mut self) {
|
||||
for _ in *self {}
|
||||
@ -1285,6 +1301,7 @@ impl<'a, T: 'a> Drop for Drain<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T: 'a> Iterator<T> for Drain<'a, T> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<T> {
|
||||
@ -1298,6 +1315,7 @@ impl<'a, T: 'a> Iterator<T> for Drain<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T: 'a> DoubleEndedIterator<T> for Drain<'a, T> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<T> {
|
||||
@ -1305,6 +1323,7 @@ impl<'a, T: 'a> DoubleEndedIterator<T> for Drain<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T: 'a> ExactSizeIterator<T> for Drain<'a, T> {}
|
||||
|
||||
#[stable]
|
||||
@ -1333,6 +1352,7 @@ impl<A: Ord> Ord for RingBuf<A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<S: Writer, A: Hash<S>> Hash<S> for RingBuf<A> {
|
||||
fn hash(&self, state: &mut S) {
|
||||
self.len().hash(state);
|
||||
@ -1342,6 +1362,7 @@ impl<S: Writer, A: Hash<S>> Hash<S> for RingBuf<A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<A> Index<uint, A> for RingBuf<A> {
|
||||
#[inline]
|
||||
fn index<'a>(&'a self, i: &uint) -> &'a A {
|
||||
@ -1349,6 +1370,7 @@ impl<A> Index<uint, A> for RingBuf<A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<A> IndexMut<uint, A> for RingBuf<A> {
|
||||
#[inline]
|
||||
fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut A {
|
||||
@ -1356,6 +1378,7 @@ impl<A> IndexMut<uint, A> for RingBuf<A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<A> FromIterator<A> for RingBuf<A> {
|
||||
fn from_iter<T: Iterator<A>>(iterator: T) -> RingBuf<A> {
|
||||
let (lower, _) = iterator.size_hint();
|
||||
@ -1365,6 +1388,7 @@ impl<A> FromIterator<A> for RingBuf<A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<A> Extend<A> for RingBuf<A> {
|
||||
fn extend<T: Iterator<A>>(&mut self, mut iterator: T) {
|
||||
for elt in iterator {
|
||||
@ -1373,6 +1397,7 @@ impl<A> Extend<A> for RingBuf<A> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<T: fmt::Show> fmt::Show for RingBuf<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
try!(write!(f, "["));
|
||||
|
@ -105,7 +105,7 @@ impl<V> VecMap<V> {
|
||||
/// use std::collections::VecMap;
|
||||
/// let mut map: VecMap<&str> = VecMap::new();
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn new() -> VecMap<V> { VecMap { v: vec![] } }
|
||||
|
||||
/// Creates an empty `VecMap` with space for at least `capacity`
|
||||
@ -117,7 +117,7 @@ impl<V> VecMap<V> {
|
||||
/// use std::collections::VecMap;
|
||||
/// let mut map: VecMap<&str> = VecMap::with_capacity(10);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn with_capacity(capacity: uint) -> VecMap<V> {
|
||||
VecMap { v: Vec::with_capacity(capacity) }
|
||||
}
|
||||
@ -133,7 +133,7 @@ impl<V> VecMap<V> {
|
||||
/// assert!(map.capacity() >= 10);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn capacity(&self) -> uint {
|
||||
self.v.capacity()
|
||||
}
|
||||
@ -152,7 +152,7 @@ impl<V> VecMap<V> {
|
||||
/// map.reserve_len(10);
|
||||
/// assert!(map.capacity() >= 10);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn reserve_len(&mut self, len: uint) {
|
||||
let cur_len = self.v.len();
|
||||
if len >= cur_len {
|
||||
@ -176,7 +176,7 @@ impl<V> VecMap<V> {
|
||||
/// map.reserve_len_exact(10);
|
||||
/// assert!(map.capacity() >= 10);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn reserve_len_exact(&mut self, len: uint) {
|
||||
let cur_len = self.v.len();
|
||||
if len >= cur_len {
|
||||
@ -186,7 +186,7 @@ impl<V> VecMap<V> {
|
||||
|
||||
/// Returns an iterator visiting all keys in ascending order by the keys.
|
||||
/// The iterator's element type is `uint`.
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn keys<'r>(&'r self) -> Keys<'r, V> {
|
||||
fn first<A, B>((a, _): (A, B)) -> A { a }
|
||||
let first: fn((uint, &'r V)) -> uint = first; // coerce to fn pointer
|
||||
@ -196,7 +196,7 @@ impl<V> VecMap<V> {
|
||||
|
||||
/// Returns an iterator visiting all values in ascending order by the keys.
|
||||
/// The iterator's element type is `&'r V`.
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn values<'r>(&'r self) -> Values<'r, V> {
|
||||
fn second<A, B>((_, b): (A, B)) -> B { b }
|
||||
let second: fn((uint, &'r V)) -> &'r V = second; // coerce to fn pointer
|
||||
@ -222,7 +222,7 @@ impl<V> VecMap<V> {
|
||||
/// println!("{}: {}", key, value);
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn iter<'r>(&'r self) -> Iter<'r, V> {
|
||||
Iter {
|
||||
front: 0,
|
||||
@ -253,7 +253,7 @@ impl<V> VecMap<V> {
|
||||
/// assert_eq!(value, &"x");
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn iter_mut<'r>(&'r mut self) -> IterMut<'r, V> {
|
||||
IterMut {
|
||||
front: 0,
|
||||
@ -281,7 +281,7 @@ impl<V> VecMap<V> {
|
||||
///
|
||||
/// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn into_iter(&mut self) -> IntoIter<V> {
|
||||
fn filter<A>((i, v): (uint, Option<A>)) -> Option<(uint, A)> {
|
||||
v.map(|v| (i, v))
|
||||
@ -304,7 +304,7 @@ impl<V> VecMap<V> {
|
||||
/// a.insert(1, "a");
|
||||
/// assert_eq!(a.len(), 1);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn len(&self) -> uint {
|
||||
self.v.iter().filter(|elt| elt.is_some()).count()
|
||||
}
|
||||
@ -321,7 +321,7 @@ impl<V> VecMap<V> {
|
||||
/// a.insert(1, "a");
|
||||
/// assert!(!a.is_empty());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.v.iter().all(|elt| elt.is_none())
|
||||
}
|
||||
@ -338,7 +338,7 @@ impl<V> VecMap<V> {
|
||||
/// a.clear();
|
||||
/// assert!(a.is_empty());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn clear(&mut self) { self.v.clear() }
|
||||
|
||||
/// Deprecated: Renamed to `get`.
|
||||
@ -359,7 +359,7 @@ impl<V> VecMap<V> {
|
||||
/// assert_eq!(map.get(&1), Some(&"a"));
|
||||
/// assert_eq!(map.get(&2), None);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn get(&self, key: &uint) -> Option<&V> {
|
||||
if *key < self.v.len() {
|
||||
match self.v[*key] {
|
||||
@ -384,7 +384,7 @@ impl<V> VecMap<V> {
|
||||
/// assert_eq!(map.contains_key(&2), false);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn contains_key(&self, key: &uint) -> bool {
|
||||
self.get(key).is_some()
|
||||
}
|
||||
@ -410,7 +410,7 @@ impl<V> VecMap<V> {
|
||||
/// }
|
||||
/// assert_eq!(map[1], "b");
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn get_mut(&mut self, key: &uint) -> Option<&mut V> {
|
||||
if *key < self.v.len() {
|
||||
match *(&mut self.v[*key]) {
|
||||
@ -444,7 +444,7 @@ impl<V> VecMap<V> {
|
||||
/// assert_eq!(map.insert(37, "c"), Some("b"));
|
||||
/// assert_eq!(map[37], "c");
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn insert(&mut self, key: uint, value: V) -> Option<V> {
|
||||
let len = self.v.len();
|
||||
if len <= key {
|
||||
@ -472,7 +472,7 @@ impl<V> VecMap<V> {
|
||||
/// assert_eq!(map.remove(&1), Some("a"));
|
||||
/// assert_eq!(map.remove(&1), None);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn remove(&mut self, key: &uint) -> Option<V> {
|
||||
if *key >= self.v.len() {
|
||||
return None;
|
||||
@ -482,50 +482,15 @@ impl<V> VecMap<V> {
|
||||
}
|
||||
|
||||
impl<V:Clone> VecMap<V> {
|
||||
/// Updates a value in the map. If the key already exists in the map,
|
||||
/// modifies the value with `ff` taking `oldval, newval`.
|
||||
/// Otherwise, sets the value to `newval`.
|
||||
/// Returns `true` if the key did not already exist in the map.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::VecMap;
|
||||
///
|
||||
/// let mut map = VecMap::new();
|
||||
///
|
||||
/// // Key does not exist, will do a simple insert
|
||||
/// assert!(map.update(1, vec![1i, 2], |mut old, new| { old.extend(new.into_iter()); old }));
|
||||
/// assert_eq!(map[1], vec![1i, 2]);
|
||||
///
|
||||
/// // Key exists, update the value
|
||||
/// assert!(!map.update(1, vec![3i, 4], |mut old, new| { old.extend(new.into_iter()); old }));
|
||||
/// assert_eq!(map[1], vec![1i, 2, 3, 4]);
|
||||
/// ```
|
||||
/// Deprecated: Use the entry API when available; shouldn't matter anyway, access is cheap.
|
||||
#[deprecated = "Use the entry API when available; shouldn't matter anyway, access is cheap"]
|
||||
#[allow(deprecated)]
|
||||
pub fn update<F>(&mut self, key: uint, newval: V, ff: F) -> bool where F: FnOnce(V, V) -> V {
|
||||
self.update_with_key(key, newval, move |_k, v, v1| ff(v,v1))
|
||||
}
|
||||
|
||||
/// Updates a value in the map. If the key already exists in the map,
|
||||
/// modifies the value with `ff` taking `key, oldval, newval`.
|
||||
/// Otherwise, sets the value to `newval`.
|
||||
/// Returns `true` if the key did not already exist in the map.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use std::collections::VecMap;
|
||||
///
|
||||
/// let mut map = VecMap::new();
|
||||
///
|
||||
/// // Key does not exist, will do a simple insert
|
||||
/// assert!(map.update_with_key(7, 10, |key, old, new| (old + new) % key));
|
||||
/// assert_eq!(map[7], 10);
|
||||
///
|
||||
/// // Key exists, update the value
|
||||
/// assert!(!map.update_with_key(7, 20, |key, old, new| (old + new) % key));
|
||||
/// assert_eq!(map[7], 2);
|
||||
/// ```
|
||||
/// Deprecated: Use the entry API when available; shouldn't matter anyway, access is cheap.
|
||||
#[deprecated = "Use the entry API when available; shouldn't matter anyway, access is cheap"]
|
||||
pub fn update_with_key<F>(&mut self, key: uint, val: V, ff: F) -> bool where
|
||||
F: FnOnce(uint, V, V) -> V
|
||||
{
|
||||
@ -537,7 +502,6 @@ impl<V:Clone> VecMap<V> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[stable]
|
||||
impl<V: PartialEq> PartialEq for VecMap<V> {
|
||||
fn eq(&self, other: &VecMap<V>) -> bool {
|
||||
@ -564,6 +528,7 @@ impl<V: Ord> Ord for VecMap<V> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<V: fmt::Show> fmt::Show for VecMap<V> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
try!(write!(f, "{{"));
|
||||
@ -577,6 +542,7 @@ impl<V: fmt::Show> fmt::Show for VecMap<V> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<V> FromIterator<(uint, V)> for VecMap<V> {
|
||||
fn from_iter<Iter: Iterator<(uint, V)>>(iter: Iter) -> VecMap<V> {
|
||||
let mut map = VecMap::new();
|
||||
@ -585,6 +551,7 @@ impl<V> FromIterator<(uint, V)> for VecMap<V> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<V> Extend<(uint, V)> for VecMap<V> {
|
||||
fn extend<Iter: Iterator<(uint, V)>>(&mut self, mut iter: Iter) {
|
||||
for (k, v) in iter {
|
||||
@ -593,6 +560,7 @@ impl<V> Extend<(uint, V)> for VecMap<V> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<V> Index<uint, V> for VecMap<V> {
|
||||
#[inline]
|
||||
fn index<'a>(&'a self, i: &uint) -> &'a V {
|
||||
@ -600,6 +568,7 @@ impl<V> Index<uint, V> for VecMap<V> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<V> IndexMut<uint, V> for VecMap<V> {
|
||||
#[inline]
|
||||
fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut V {
|
||||
@ -609,6 +578,7 @@ impl<V> IndexMut<uint, V> for VecMap<V> {
|
||||
|
||||
macro_rules! iterator {
|
||||
(impl $name:ident -> $elem:ty, $($getter:ident),+) => {
|
||||
#[stable]
|
||||
impl<'a, V> Iterator<$elem> for $name<'a, V> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<$elem> {
|
||||
@ -641,6 +611,7 @@ macro_rules! iterator {
|
||||
|
||||
macro_rules! double_ended_iterator {
|
||||
(impl $name:ident -> $elem:ty, $($getter:ident),+) => {
|
||||
#[stable]
|
||||
impl<'a, V> DoubleEndedIterator<$elem> for $name<'a, V> {
|
||||
#[inline]
|
||||
fn next_back(&mut self) -> Option<$elem> {
|
||||
@ -666,6 +637,7 @@ macro_rules! double_ended_iterator {
|
||||
}
|
||||
|
||||
/// An iterator over the key-value pairs of a map.
|
||||
#[stable]
|
||||
pub struct Iter<'a, V:'a> {
|
||||
front: uint,
|
||||
back: uint,
|
||||
@ -688,6 +660,7 @@ double_ended_iterator! { impl Iter -> (uint, &'a V), as_ref }
|
||||
|
||||
/// An iterator over the key-value pairs of a map, with the
|
||||
/// values being mutable.
|
||||
#[stable]
|
||||
pub struct IterMut<'a, V:'a> {
|
||||
front: uint,
|
||||
back: uint,
|
||||
@ -698,6 +671,7 @@ iterator! { impl IterMut -> (uint, &'a mut V), as_mut }
|
||||
double_ended_iterator! { impl IterMut -> (uint, &'a mut V), as_mut }
|
||||
|
||||
/// An iterator over the keys of a map.
|
||||
#[stable]
|
||||
pub struct Keys<'a, V: 'a> {
|
||||
iter: Map<(uint, &'a V), uint, Iter<'a, V>, fn((uint, &'a V)) -> uint>
|
||||
}
|
||||
@ -712,6 +686,7 @@ impl<'a, V> Clone for Keys<'a, V> {
|
||||
}
|
||||
|
||||
/// An iterator over the values of a map.
|
||||
#[stable]
|
||||
pub struct Values<'a, V: 'a> {
|
||||
iter: Map<(uint, &'a V), &'a V, Iter<'a, V>, fn((uint, &'a V)) -> &'a V>
|
||||
}
|
||||
@ -726,6 +701,7 @@ impl<'a, V> Clone for Values<'a, V> {
|
||||
}
|
||||
|
||||
/// A consuming iterator over the key-value pairs of a map.
|
||||
#[stable]
|
||||
pub struct IntoIter<V> {
|
||||
iter: FilterMap<
|
||||
(uint, Option<V>),
|
||||
@ -734,28 +710,32 @@ pub struct IntoIter<V> {
|
||||
fn((uint, Option<V>)) -> Option<(uint, V)>>
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, V> Iterator<uint> for Keys<'a, V> {
|
||||
fn next(&mut self) -> Option<uint> { self.iter.next() }
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
|
||||
}
|
||||
#[stable]
|
||||
impl<'a, V> DoubleEndedIterator<uint> for Keys<'a, V> {
|
||||
fn next_back(&mut self) -> Option<uint> { self.iter.next_back() }
|
||||
}
|
||||
|
||||
|
||||
#[stable]
|
||||
impl<'a, V> Iterator<&'a V> for Values<'a, V> {
|
||||
fn next(&mut self) -> Option<(&'a V)> { self.iter.next() }
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
|
||||
}
|
||||
#[stable]
|
||||
impl<'a, V> DoubleEndedIterator<&'a V> for Values<'a, V> {
|
||||
fn next_back(&mut self) -> Option<(&'a V)> { self.iter.next_back() }
|
||||
}
|
||||
|
||||
|
||||
#[stable]
|
||||
impl<V> Iterator<(uint, V)> for IntoIter<V> {
|
||||
fn next(&mut self) -> Option<(uint, V)> { self.iter.next() }
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
|
||||
}
|
||||
#[stable]
|
||||
impl<V> DoubleEndedIterator<(uint, V)> for IntoIter<V> {
|
||||
fn next_back(&mut self) -> Option<(uint, V)> { self.iter.next_back() }
|
||||
}
|
||||
|
@ -296,6 +296,7 @@ fn test_resize_policy() {
|
||||
/// }
|
||||
/// ```
|
||||
#[deriving(Clone)]
|
||||
#[stable]
|
||||
pub struct HashMap<K, V, H = RandomSipHasher> {
|
||||
// All hashes are keyed on these values, to prevent hash collision attacks.
|
||||
hasher: H,
|
||||
@ -508,7 +509,7 @@ impl<K: Hash + Eq, V> HashMap<K, V, RandomSipHasher> {
|
||||
/// let mut map: HashMap<&str, int> = HashMap::new();
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn new() -> HashMap<K, V, RandomSipHasher> {
|
||||
let hasher = RandomSipHasher::new();
|
||||
HashMap::with_hasher(hasher)
|
||||
@ -523,7 +524,7 @@ impl<K: Hash + Eq, V> HashMap<K, V, RandomSipHasher> {
|
||||
/// let mut map: HashMap<&str, int> = HashMap::with_capacity(10);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn with_capacity(capacity: uint) -> HashMap<K, V, RandomSipHasher> {
|
||||
let hasher = RandomSipHasher::new();
|
||||
HashMap::with_capacity_and_hasher(capacity, hasher)
|
||||
@ -546,6 +547,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// map.insert(1i, 2u);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "hasher stuff is unclear"]
|
||||
pub fn with_hasher(hasher: H) -> HashMap<K, V, H> {
|
||||
HashMap {
|
||||
hasher: hasher,
|
||||
@ -573,6 +575,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// map.insert(1i, 2u);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "hasher stuff is unclear"]
|
||||
pub fn with_capacity_and_hasher(capacity: uint, hasher: H) -> HashMap<K, V, H> {
|
||||
let resize_policy = DefaultResizePolicy::new();
|
||||
let min_cap = max(INITIAL_CAPACITY, resize_policy.min_capacity(capacity));
|
||||
@ -595,7 +598,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// assert!(map.capacity() >= 100);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn capacity(&self) -> uint {
|
||||
self.resize_policy.usable_capacity(self.table.capacity())
|
||||
}
|
||||
@ -615,7 +618,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// let mut map: HashMap<&str, int> = HashMap::new();
|
||||
/// map.reserve(10);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn reserve(&mut self, additional: uint) {
|
||||
let new_size = self.len().checked_add(additional).expect("capacity overflow");
|
||||
let min_cap = self.resize_policy.min_capacity(new_size);
|
||||
@ -727,7 +730,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// map.shrink_to_fit();
|
||||
/// assert!(map.capacity() >= 2);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn shrink_to_fit(&mut self) {
|
||||
let min_capacity = self.resize_policy.min_capacity(self.len());
|
||||
let min_capacity = max(min_capacity.next_power_of_two(), INITIAL_CAPACITY);
|
||||
@ -845,7 +848,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// println!("{}", key);
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
|
||||
fn first<A, B>((a, _): (A, B)) -> A { a }
|
||||
let first: fn((&'a K,&'a V)) -> &'a K = first; // coerce to fn ptr
|
||||
@ -870,7 +873,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// println!("{}", key);
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn values<'a>(&'a self) -> Values<'a, K, V> {
|
||||
fn second<A, B>((_, b): (A, B)) -> B { b }
|
||||
let second: fn((&'a K,&'a V)) -> &'a V = second; // coerce to fn ptr
|
||||
@ -895,7 +898,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// println!("key: {} val: {}", key, val);
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn iter(&self) -> Iter<K, V> {
|
||||
Iter { inner: self.table.iter() }
|
||||
}
|
||||
@ -923,7 +926,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// println!("key: {} val: {}", key, val);
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn iter_mut(&mut self) -> IterMut<K, V> {
|
||||
IterMut { inner: self.table.iter_mut() }
|
||||
}
|
||||
@ -945,7 +948,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// // Not possible with .iter()
|
||||
/// let vec: Vec<(&str, int)> = map.into_iter().collect();
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn into_iter(self) -> IntoIter<K, V> {
|
||||
fn last_two<A, B, C>((_, b, c): (A, B, C)) -> (B, C) { (b, c) }
|
||||
let last_two: fn((SafeHash, K, V)) -> (K, V) = last_two;
|
||||
@ -976,7 +979,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// a.insert(1u, "a");
|
||||
/// assert_eq!(a.len(), 1);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn len(&self) -> uint { self.table.size() }
|
||||
|
||||
/// Return true if the map contains no elements.
|
||||
@ -992,7 +995,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// assert!(!a.is_empty());
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_empty(&self) -> bool { self.len() == 0 }
|
||||
|
||||
/// Clears the map, returning all key-value pairs as an iterator. Keeps the
|
||||
@ -1038,7 +1041,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// a.clear();
|
||||
/// assert!(a.is_empty());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
#[inline]
|
||||
pub fn clear(&mut self) {
|
||||
self.drain();
|
||||
@ -1066,7 +1069,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// assert_eq!(map.get(&1), Some(&"a"));
|
||||
/// assert_eq!(map.get(&2), None);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn get<Sized? Q>(&self, k: &Q) -> Option<&V>
|
||||
where Q: Hash<S> + Eq + BorrowFrom<K>
|
||||
{
|
||||
@ -1089,7 +1092,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// assert_eq!(map.contains_key(&1), true);
|
||||
/// assert_eq!(map.contains_key(&2), false);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn contains_key<Sized? Q>(&self, k: &Q) -> bool
|
||||
where Q: Hash<S> + Eq + BorrowFrom<K>
|
||||
{
|
||||
@ -1121,7 +1124,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// }
|
||||
/// assert_eq!(map[1], "b");
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn get_mut<Sized? Q>(&mut self, k: &Q) -> Option<&mut V>
|
||||
where Q: Hash<S> + Eq + BorrowFrom<K>
|
||||
{
|
||||
@ -1150,7 +1153,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// assert_eq!(map.insert(37, "c"), Some("b"));
|
||||
/// assert_eq!(map[37], "c");
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
|
||||
let hash = self.make_hash(&k);
|
||||
self.reserve(1);
|
||||
@ -1185,7 +1188,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
|
||||
/// assert_eq!(map.remove(&1), Some("a"));
|
||||
/// assert_eq!(map.remove(&1), None);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn remove<Sized? Q>(&mut self, k: &Q) -> Option<V>
|
||||
where Q: Hash<S> + Eq + BorrowFrom<K>
|
||||
{
|
||||
@ -1275,6 +1278,7 @@ impl<K: Eq + Hash<S>, V: PartialEq, S, H: Hasher<S>> PartialEq for HashMap<K, V,
|
||||
#[stable]
|
||||
impl<K: Eq + Hash<S>, V: Eq, S, H: Hasher<S>> Eq for HashMap<K, V, H> {}
|
||||
|
||||
#[stable]
|
||||
impl<K: Eq + Hash<S> + Show, V: Show, S, H: Hasher<S>> Show for HashMap<K, V, H> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
try!(write!(f, "{{"));
|
||||
@ -1296,6 +1300,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S> + Default> Default for HashMap<K, V, H>
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<K: Hash<S> + Eq, Sized? Q, V, S, H: Hasher<S>> Index<Q, V> for HashMap<K, V, H>
|
||||
where Q: BorrowFrom<K> + Hash<S> + Eq
|
||||
{
|
||||
@ -1305,6 +1310,7 @@ impl<K: Hash<S> + Eq, Sized? Q, V, S, H: Hasher<S>> Index<Q, V> for HashMap<K, V
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<K: Hash<S> + Eq, Sized? Q, V, S, H: Hasher<S>> IndexMut<Q, V> for HashMap<K, V, H>
|
||||
where Q: BorrowFrom<K> + Hash<S> + Eq
|
||||
{
|
||||
@ -1315,6 +1321,7 @@ impl<K: Hash<S> + Eq, Sized? Q, V, S, H: Hasher<S>> IndexMut<Q, V> for HashMap<K
|
||||
}
|
||||
|
||||
/// HashMap iterator
|
||||
#[stable]
|
||||
pub struct Iter<'a, K: 'a, V: 'a> {
|
||||
inner: table::Iter<'a, K, V>
|
||||
}
|
||||
@ -1329,11 +1336,13 @@ impl<'a, K, V> Clone for Iter<'a, K, V> {
|
||||
}
|
||||
|
||||
/// HashMap mutable values iterator
|
||||
#[stable]
|
||||
pub struct IterMut<'a, K: 'a, V: 'a> {
|
||||
inner: table::IterMut<'a, K, V>
|
||||
}
|
||||
|
||||
/// HashMap move iterator
|
||||
#[stable]
|
||||
pub struct IntoIter<K, V> {
|
||||
inner: iter::Map<
|
||||
(SafeHash, K, V),
|
||||
@ -1344,6 +1353,7 @@ pub struct IntoIter<K, V> {
|
||||
}
|
||||
|
||||
/// HashMap keys iterator
|
||||
#[stable]
|
||||
pub struct Keys<'a, K: 'a, V: 'a> {
|
||||
inner: Map<(&'a K, &'a V), &'a K, Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>
|
||||
}
|
||||
@ -1358,6 +1368,7 @@ impl<'a, K, V> Clone for Keys<'a, K, V> {
|
||||
}
|
||||
|
||||
/// HashMap values iterator
|
||||
#[stable]
|
||||
pub struct Values<'a, K: 'a, V: 'a> {
|
||||
inner: Map<(&'a K, &'a V), &'a V, Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>
|
||||
}
|
||||
@ -1372,6 +1383,7 @@ impl<'a, K, V> Clone for Values<'a, K, V> {
|
||||
}
|
||||
|
||||
/// HashMap drain iterator
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
pub struct Drain<'a, K: 'a, V: 'a> {
|
||||
inner: iter::Map<
|
||||
(SafeHash, K, V),
|
||||
@ -1410,31 +1422,37 @@ enum VacantEntryState<K, V, M> {
|
||||
NoElem(EmptyBucket<K, V, M>),
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, K, V> Iterator<(&'a K, &'a V)> for Iter<'a, K, V> {
|
||||
#[inline] fn next(&mut self) -> Option<(&'a K, &'a V)> { self.inner.next() }
|
||||
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, K, V> Iterator<(&'a K, &'a mut V)> for IterMut<'a, K, V> {
|
||||
#[inline] fn next(&mut self) -> Option<(&'a K, &'a mut V)> { self.inner.next() }
|
||||
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<K, V> Iterator<(K, V)> for IntoIter<K, V> {
|
||||
#[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next() }
|
||||
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, K, V> Iterator<&'a K> for Keys<'a, K, V> {
|
||||
#[inline] fn next(&mut self) -> Option<(&'a K)> { self.inner.next() }
|
||||
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, K, V> Iterator<&'a V> for Values<'a, K, V> {
|
||||
#[inline] fn next(&mut self) -> Option<(&'a V)> { self.inner.next() }
|
||||
#[inline] fn size_hint(&self) -> (uint, Option<uint>) { self.inner.size_hint() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, K: 'a, V: 'a> Iterator<(K, V)> for Drain<'a, K, V> {
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<(K, V)> {
|
||||
@ -1491,6 +1509,7 @@ impl<'a, K, V> VacantEntry<'a, K, V> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<K: Eq + Hash<S>, V, S, H: Hasher<S> + Default> FromIterator<(K, V)> for HashMap<K, V, H> {
|
||||
fn from_iter<T: Iterator<(K, V)>>(iter: T) -> HashMap<K, V, H> {
|
||||
let lower = iter.size_hint().0;
|
||||
@ -1500,6 +1519,7 @@ impl<K: Eq + Hash<S>, V, S, H: Hasher<S> + Default> FromIterator<(K, V)> for Has
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<K: Eq + Hash<S>, V, S, H: Hasher<S> + Default> Extend<(K, V)> for HashMap<K, V, H> {
|
||||
fn extend<T: Iterator<(K, V)>>(&mut self, mut iter: T) {
|
||||
for (k, v) in iter {
|
||||
|
@ -90,6 +90,7 @@ use super::map::{mod, HashMap, Keys, INITIAL_CAPACITY};
|
||||
/// }
|
||||
/// ```
|
||||
#[deriving(Clone)]
|
||||
#[stable]
|
||||
pub struct HashSet<T, H = RandomSipHasher> {
|
||||
map: HashMap<T, (), H>
|
||||
}
|
||||
@ -104,7 +105,7 @@ impl<T: Hash + Eq> HashSet<T, RandomSipHasher> {
|
||||
/// let mut set: HashSet<int> = HashSet::new();
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn new() -> HashSet<T, RandomSipHasher> {
|
||||
HashSet::with_capacity(INITIAL_CAPACITY)
|
||||
}
|
||||
@ -119,7 +120,7 @@ impl<T: Hash + Eq> HashSet<T, RandomSipHasher> {
|
||||
/// let mut set: HashSet<int> = HashSet::with_capacity(10);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn with_capacity(capacity: uint) -> HashSet<T, RandomSipHasher> {
|
||||
HashSet { map: HashMap::with_capacity(capacity) }
|
||||
}
|
||||
@ -142,6 +143,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// set.insert(2u);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "hasher stuff is unclear"]
|
||||
pub fn with_hasher(hasher: H) -> HashSet<T, H> {
|
||||
HashSet::with_capacity_and_hasher(INITIAL_CAPACITY, hasher)
|
||||
}
|
||||
@ -165,6 +167,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// set.insert(1i);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "hasher stuff is unclear"]
|
||||
pub fn with_capacity_and_hasher(capacity: uint, hasher: H) -> HashSet<T, H> {
|
||||
HashSet { map: HashMap::with_capacity_and_hasher(capacity, hasher) }
|
||||
}
|
||||
@ -179,7 +182,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// assert!(set.capacity() >= 100);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn capacity(&self) -> uint {
|
||||
self.map.capacity()
|
||||
}
|
||||
@ -199,7 +202,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// let mut set: HashSet<int> = HashSet::new();
|
||||
/// set.reserve(10);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn reserve(&mut self, additional: uint) {
|
||||
self.map.reserve(additional)
|
||||
}
|
||||
@ -220,7 +223,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// set.shrink_to_fit();
|
||||
/// assert!(set.capacity() >= 2);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn shrink_to_fit(&mut self) {
|
||||
self.map.shrink_to_fit()
|
||||
}
|
||||
@ -248,8 +251,8 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// println!("{}", x);
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
pub fn iter<'a>(&'a self) -> Iter<'a, T> {
|
||||
#[stable]
|
||||
pub fn iter(&self) -> Iter<T> {
|
||||
Iter { iter: self.map.keys() }
|
||||
}
|
||||
|
||||
@ -273,7 +276,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// println!("{}", x);
|
||||
/// }
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn into_iter(self) -> IntoIter<T> {
|
||||
fn first<A, B>((a, _): (A, B)) -> A { a }
|
||||
let first: fn((T, ())) -> T = first;
|
||||
@ -303,7 +306,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// let diff: HashSet<int> = b.difference(&a).map(|&x| x).collect();
|
||||
/// assert_eq!(diff, [4i].iter().map(|&x| x).collect());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn difference<'a>(&'a self, other: &'a HashSet<T, H>) -> Difference<'a, T, H> {
|
||||
Difference {
|
||||
iter: self.iter(),
|
||||
@ -331,7 +334,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// assert_eq!(diff1, diff2);
|
||||
/// assert_eq!(diff1, [1i, 4].iter().map(|&x| x).collect());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn symmetric_difference<'a>(&'a self, other: &'a HashSet<T, H>)
|
||||
-> SymmetricDifference<'a, T, H> {
|
||||
SymmetricDifference { iter: self.difference(other).chain(other.difference(self)) }
|
||||
@ -354,7 +357,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// let diff: HashSet<int> = a.intersection(&b).map(|&x| x).collect();
|
||||
/// assert_eq!(diff, [2i, 3].iter().map(|&x| x).collect());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn intersection<'a>(&'a self, other: &'a HashSet<T, H>) -> Intersection<'a, T, H> {
|
||||
Intersection {
|
||||
iter: self.iter(),
|
||||
@ -379,7 +382,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// let diff: HashSet<int> = a.union(&b).map(|&x| x).collect();
|
||||
/// assert_eq!(diff, [1i, 2, 3, 4].iter().map(|&x| x).collect());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn union<'a>(&'a self, other: &'a HashSet<T, H>) -> Union<'a, T, H> {
|
||||
Union { iter: self.iter().chain(other.difference(self)) }
|
||||
}
|
||||
@ -396,7 +399,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// v.insert(1u);
|
||||
/// assert_eq!(v.len(), 1);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn len(&self) -> uint { self.map.len() }
|
||||
|
||||
/// Returns true if the set contains no elements
|
||||
@ -411,7 +414,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// v.insert(1u);
|
||||
/// assert!(!v.is_empty());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_empty(&self) -> bool { self.map.len() == 0 }
|
||||
|
||||
/// Clears the set, returning all elements in an iterator.
|
||||
@ -436,7 +439,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// v.clear();
|
||||
/// assert!(v.is_empty());
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn clear(&mut self) { self.map.clear() }
|
||||
|
||||
/// Returns `true` if the set contains a value.
|
||||
@ -454,7 +457,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// assert_eq!(set.contains(&1), true);
|
||||
/// assert_eq!(set.contains(&4), false);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn contains<Sized? Q>(&self, value: &Q) -> bool
|
||||
where Q: BorrowFrom<T> + Hash<S> + Eq
|
||||
{
|
||||
@ -478,7 +481,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// b.insert(1);
|
||||
/// assert_eq!(a.is_disjoint(&b), false);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_disjoint(&self, other: &HashSet<T, H>) -> bool {
|
||||
self.iter().all(|v| !other.contains(v))
|
||||
}
|
||||
@ -499,7 +502,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// set.insert(4);
|
||||
/// assert_eq!(set.is_subset(&sup), false);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_subset(&self, other: &HashSet<T, H>) -> bool {
|
||||
self.iter().all(|v| other.contains(v))
|
||||
}
|
||||
@ -524,7 +527,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// assert_eq!(set.is_superset(&sub), true);
|
||||
/// ```
|
||||
#[inline]
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn is_superset(&self, other: &HashSet<T, H>) -> bool {
|
||||
other.is_subset(self)
|
||||
}
|
||||
@ -543,7 +546,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// assert_eq!(set.insert(2), false);
|
||||
/// assert_eq!(set.len(), 1);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()).is_none() }
|
||||
|
||||
/// Removes a value from the set. Returns `true` if the value was
|
||||
@ -564,7 +567,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
|
||||
/// assert_eq!(set.remove(&2), true);
|
||||
/// assert_eq!(set.remove(&2), false);
|
||||
/// ```
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
pub fn remove<Sized? Q>(&mut self, value: &Q) -> bool
|
||||
where Q: BorrowFrom<T> + Hash<S> + Eq
|
||||
{
|
||||
@ -584,6 +587,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S>> PartialEq for HashSet<T, H> {
|
||||
#[stable]
|
||||
impl<T: Eq + Hash<S>, S, H: Hasher<S>> Eq for HashSet<T, H> {}
|
||||
|
||||
#[stable]
|
||||
impl<T: Eq + Hash<S> + fmt::Show, S, H: Hasher<S>> fmt::Show for HashSet<T, H> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
try!(write!(f, "{{"));
|
||||
@ -597,6 +601,7 @@ impl<T: Eq + Hash<S> + fmt::Show, S, H: Hasher<S>> fmt::Show for HashSet<T, H> {
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> FromIterator<T> for HashSet<T, H> {
|
||||
fn from_iter<I: Iterator<T>>(iter: I) -> HashSet<T, H> {
|
||||
let lower = iter.size_hint().0;
|
||||
@ -606,6 +611,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> FromIterator<T> for HashSet<T,
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> Extend<T> for HashSet<T, H> {
|
||||
fn extend<I: Iterator<T>>(&mut self, mut iter: I) {
|
||||
for k in iter {
|
||||
@ -622,7 +628,7 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> Default for HashSet<T, H> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
impl<'a, 'b, T: Eq + Hash<S> + Clone, S, H: Hasher<S> + Default>
|
||||
BitOr<&'b HashSet<T, H>, HashSet<T, H>> for &'a HashSet<T, H> {
|
||||
/// Returns the union of `self` and `rhs` as a new `HashSet<T, H>`.
|
||||
@ -650,7 +656,7 @@ BitOr<&'b HashSet<T, H>, HashSet<T, H>> for &'a HashSet<T, H> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
impl<'a, 'b, T: Eq + Hash<S> + Clone, S, H: Hasher<S> + Default>
|
||||
BitAnd<&'b HashSet<T, H>, HashSet<T, H>> for &'a HashSet<T, H> {
|
||||
/// Returns the intersection of `self` and `rhs` as a new `HashSet<T, H>`.
|
||||
@ -678,7 +684,7 @@ BitAnd<&'b HashSet<T, H>, HashSet<T, H>> for &'a HashSet<T, H> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
impl<'a, 'b, T: Eq + Hash<S> + Clone, S, H: Hasher<S> + Default>
|
||||
BitXor<&'b HashSet<T, H>, HashSet<T, H>> for &'a HashSet<T, H> {
|
||||
/// Returns the symmetric difference of `self` and `rhs` as a new `HashSet<T, H>`.
|
||||
@ -706,7 +712,7 @@ BitXor<&'b HashSet<T, H>, HashSet<T, H>> for &'a HashSet<T, H> {
|
||||
}
|
||||
}
|
||||
|
||||
#[unstable = "matches collection reform specification, waiting for dust to settle"]
|
||||
#[stable]
|
||||
impl<'a, 'b, T: Eq + Hash<S> + Clone, S, H: Hasher<S> + Default>
|
||||
Sub<&'b HashSet<T, H>, HashSet<T, H>> for &'a HashSet<T, H> {
|
||||
/// Returns the difference of `self` and `rhs` as a new `HashSet<T, H>`.
|
||||
@ -735,21 +741,25 @@ Sub<&'b HashSet<T, H>, HashSet<T, H>> for &'a HashSet<T, H> {
|
||||
}
|
||||
|
||||
/// HashSet iterator
|
||||
#[stable]
|
||||
pub struct Iter<'a, K: 'a> {
|
||||
iter: Keys<'a, K, ()>
|
||||
}
|
||||
|
||||
/// HashSet move iterator
|
||||
#[stable]
|
||||
pub struct IntoIter<K> {
|
||||
iter: Map<(K, ()), K, map::IntoIter<K, ()>, fn((K, ())) -> K>
|
||||
}
|
||||
|
||||
/// HashSet drain iterator
|
||||
#[stable]
|
||||
pub struct Drain<'a, K: 'a> {
|
||||
iter: Map<(K, ()), K, map::Drain<'a, K, ()>, fn((K, ())) -> K>,
|
||||
}
|
||||
|
||||
/// Intersection iterator
|
||||
#[stable]
|
||||
pub struct Intersection<'a, T: 'a, H: 'a> {
|
||||
// iterator of the first set
|
||||
iter: Iter<'a, T>,
|
||||
@ -758,6 +768,7 @@ pub struct Intersection<'a, T: 'a, H: 'a> {
|
||||
}
|
||||
|
||||
/// Difference iterator
|
||||
#[stable]
|
||||
pub struct Difference<'a, T: 'a, H: 'a> {
|
||||
// iterator of the first set
|
||||
iter: Iter<'a, T>,
|
||||
@ -766,30 +777,36 @@ pub struct Difference<'a, T: 'a, H: 'a> {
|
||||
}
|
||||
|
||||
/// Symmetric difference iterator.
|
||||
#[stable]
|
||||
pub struct SymmetricDifference<'a, T: 'a, H: 'a> {
|
||||
iter: Chain<Difference<'a, T, H>, Difference<'a, T, H>>
|
||||
}
|
||||
|
||||
/// Set union iterator.
|
||||
#[stable]
|
||||
pub struct Union<'a, T: 'a, H: 'a> {
|
||||
iter: Chain<Iter<'a, T>, Difference<'a, T, H>>
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, K> Iterator<&'a K> for Iter<'a, K> {
|
||||
fn next(&mut self) -> Option<&'a K> { self.iter.next() }
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<K> Iterator<K> for IntoIter<K> {
|
||||
fn next(&mut self) -> Option<K> { self.iter.next() }
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, K: 'a> Iterator<K> for Drain<'a, K> {
|
||||
fn next(&mut self) -> Option<K> { self.iter.next() }
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T, S, H> Iterator<&'a T> for Intersection<'a, T, H>
|
||||
where T: Eq + Hash<S>, H: Hasher<S>
|
||||
{
|
||||
@ -810,6 +827,7 @@ impl<'a, T, S, H> Iterator<&'a T> for Intersection<'a, T, H>
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T, S, H> Iterator<&'a T> for Difference<'a, T, H>
|
||||
where T: Eq + Hash<S>, H: Hasher<S>
|
||||
{
|
||||
@ -830,6 +848,7 @@ impl<'a, T, S, H> Iterator<&'a T> for Difference<'a, T, H>
|
||||
}
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T, S, H> Iterator<&'a T> for SymmetricDifference<'a, T, H>
|
||||
where T: Eq + Hash<S>, H: Hasher<S>
|
||||
{
|
||||
@ -837,6 +856,7 @@ impl<'a, T, S, H> Iterator<&'a T> for SymmetricDifference<'a, T, H>
|
||||
fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
|
||||
}
|
||||
|
||||
#[stable]
|
||||
impl<'a, T, S, H> Iterator<&'a T> for Union<'a, T, H>
|
||||
where T: Eq + Hash<S>, H: Hasher<S>
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user