Skip to content

Commit e55f64f

Browse files
committed
auto merge of #14709 : alexcrichton/rust/collections, r=brson
This is mostly just a cosmetic change, continuing the work from #14333.
2 parents a6a9e09 + da07039 commit e55f64f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+249
-233
lines changed

src/libcollections/bitv.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use core::slice;
2020
use core::uint;
2121
use std::hash;
2222

23+
use {Collection, Mutable, Set, MutableSet};
2324
use vec::Vec;
2425

2526
#[deriving(Clone)]
@@ -857,7 +858,7 @@ impl<S: hash::Writer> hash::Hash<S> for BitvSet {
857858
}
858859
}
859860

860-
impl Container for BitvSet {
861+
impl Collection for BitvSet {
861862
#[inline]
862863
fn len(&self) -> uint { self.size }
863864
}
@@ -1008,6 +1009,7 @@ mod tests {
10081009
use std::rand::Rng;
10091010
use test::Bencher;
10101011

1012+
use {Set, Mutable, MutableSet};
10111013
use bitv::{Bitv, SmallBitv, BigBitv, BitvSet, from_bools, from_fn,
10121014
from_bytes};
10131015
use bitv;

src/libcollections/btree.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use alloc::owned::Box;
2424
use core::fmt;
2525
use core::fmt::Show;
2626

27+
use Collection;
2728
use vec::Vec;
2829

2930
#[allow(missing_doc)]

src/libcollections/deque.rs

+2-29
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,13 @@
1010

1111
//! Container traits for collections
1212
13-
use core::prelude::*;
14-
15-
/// A double-ended sequence that allows querying, insertion and deletion at both ends.
16-
pub trait Deque<T> : Mutable {
17-
/// Provide a reference to the front element, or None if the sequence is empty
18-
fn front<'a>(&'a self) -> Option<&'a T>;
19-
20-
/// Provide a mutable reference to the front element, or None if the sequence is empty
21-
fn front_mut<'a>(&'a mut self) -> Option<&'a mut T>;
22-
23-
/// Provide a reference to the back element, or None if the sequence is empty
24-
fn back<'a>(&'a self) -> Option<&'a T>;
25-
26-
/// Provide a mutable reference to the back element, or None if the sequence is empty
27-
fn back_mut<'a>(&'a mut self) -> Option<&'a mut T>;
28-
29-
/// Insert an element first in the sequence
30-
fn push_front(&mut self, elt: T);
31-
32-
/// Insert an element last in the sequence
33-
fn push_back(&mut self, elt: T);
34-
35-
/// Remove the last element and return it, or None if the sequence is empty
36-
fn pop_back(&mut self) -> Option<T>;
37-
38-
/// Remove the first element and return it, or None if the sequence is empty
39-
fn pop_front(&mut self) -> Option<T>;
40-
}
41-
4213
#[cfg(test)]
4314
pub mod bench {
4415
use std::prelude::*;
4516
use std::rand;
4617
use std::rand::Rng;
4718
use test::Bencher;
19+
use MutableMap;
4820

4921
pub fn insert_rand_n<M:MutableMap<uint,uint>>(n: uint,
5022
map: &mut M,
@@ -121,3 +93,4 @@ pub mod bench {
12193
})
12294
}
12395
}
96+

src/libcollections/dlist.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! The DList allows pushing and popping elements at either end.
1414
//!
1515
//! DList implements the trait Deque. It should be imported with `use
16-
//! collections::deque::Deque`.
16+
//! collections::Deque`.
1717
1818
// DList is constructed like a singly-linked list over the field `next`.
1919
// including the last link being None; each Node owns its `next` field.
@@ -29,7 +29,7 @@ use core::iter;
2929
use core::mem;
3030
use core::ptr;
3131

32-
use deque::Deque;
32+
use {Collection, Mutable, Deque};
3333

3434
/// A doubly-linked list.
3535
pub struct DList<T> {
@@ -125,7 +125,7 @@ fn link_with_prev<T>(mut next: Box<Node<T>>, prev: Rawlink<Node<T>>)
125125
Some(next)
126126
}
127127

128-
impl<T> Container for DList<T> {
128+
impl<T> Collection for DList<T> {
129129
/// O(1)
130130
#[inline]
131131
fn is_empty(&self) -> bool {
@@ -629,7 +629,7 @@ mod tests {
629629
use test::Bencher;
630630
use test;
631631

632-
use deque::Deque;
632+
use Deque;
633633
use super::{DList, Node, ListInsertion};
634634
use vec::Vec;
635635

src/libcollections/lib.rs

+116-7
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ extern crate alloc;
3232
#[cfg(test)] #[phase(syntax, link)] extern crate std;
3333
#[cfg(test)] #[phase(syntax, link)] extern crate log;
3434

35+
use core::prelude::*;
36+
37+
pub use core::collections::Collection;
3538
pub use bitv::{Bitv, BitvSet};
3639
pub use btree::BTree;
37-
pub use deque::Deque;
3840
pub use dlist::DList;
3941
pub use enum_set::EnumSet;
4042
pub use priority_queue::PriorityQueue;
@@ -47,7 +49,6 @@ mod macros;
4749

4850
pub mod bitv;
4951
pub mod btree;
50-
pub mod deque;
5152
pub mod dlist;
5253
pub mod enum_set;
5354
pub mod priority_queue;
@@ -64,12 +65,120 @@ pub mod hash;
6465
// Internal unicode fiddly bits for the str module
6566
mod unicode;
6667

67-
// FIXME(#14008) should this actually exist, or should a method be added?
68-
fn expect<T>(a: core::option::Option<T>, b: &str) -> T {
69-
match a {
70-
core::option::Some(a) => a,
71-
core::option::None => fail!("{}", b),
68+
mod deque;
69+
70+
/// A trait to represent mutable containers
71+
pub trait Mutable: Collection {
72+
/// Clear the container, removing all values.
73+
fn clear(&mut self);
74+
}
75+
76+
/// A map is a key-value store where values may be looked up by their keys. This
77+
/// trait provides basic operations to operate on these stores.
78+
pub trait Map<K, V>: Collection {
79+
/// Return a reference to the value corresponding to the key
80+
fn find<'a>(&'a self, key: &K) -> Option<&'a V>;
81+
82+
/// Return true if the map contains a value for the specified key
83+
#[inline]
84+
fn contains_key(&self, key: &K) -> bool {
85+
self.find(key).is_some()
86+
}
87+
}
88+
89+
/// This trait provides basic operations to modify the contents of a map.
90+
pub trait MutableMap<K, V>: Map<K, V> + Mutable {
91+
/// Insert a key-value pair into the map. An existing value for a
92+
/// key is replaced by the new value. Return true if the key did
93+
/// not already exist in the map.
94+
#[inline]
95+
fn insert(&mut self, key: K, value: V) -> bool {
96+
self.swap(key, value).is_none()
97+
}
98+
99+
/// Remove a key-value pair from the map. Return true if the key
100+
/// was present in the map, otherwise false.
101+
#[inline]
102+
fn remove(&mut self, key: &K) -> bool {
103+
self.pop(key).is_some()
104+
}
105+
106+
/// Insert a key-value pair from the map. If the key already had a value
107+
/// present in the map, that value is returned. Otherwise None is returned.
108+
fn swap(&mut self, k: K, v: V) -> Option<V>;
109+
110+
/// Removes a key from the map, returning the value at the key if the key
111+
/// was previously in the map.
112+
fn pop(&mut self, k: &K) -> Option<V>;
113+
114+
/// Return a mutable reference to the value corresponding to the key
115+
fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V>;
116+
}
117+
118+
/// A set is a group of objects which are each distinct from one another. This
119+
/// trait represents actions which can be performed on sets to iterate over
120+
/// them.
121+
pub trait Set<T>: Collection {
122+
/// Return true if the set contains a value
123+
fn contains(&self, value: &T) -> bool;
124+
125+
/// Return true if the set has no elements in common with `other`.
126+
/// This is equivalent to checking for an empty intersection.
127+
fn is_disjoint(&self, other: &Self) -> bool;
128+
129+
/// Return true if the set is a subset of another
130+
fn is_subset(&self, other: &Self) -> bool;
131+
132+
/// Return true if the set is a superset of another
133+
fn is_superset(&self, other: &Self) -> bool {
134+
other.is_subset(self)
72135
}
136+
137+
// FIXME #8154: Add difference, sym. difference, intersection and union iterators
138+
}
139+
140+
/// This trait represents actions which can be performed on sets to mutate
141+
/// them.
142+
pub trait MutableSet<T>: Set<T> + Mutable {
143+
/// Add a value to the set. Return true if the value was not already
144+
/// present in the set.
145+
fn insert(&mut self, value: T) -> bool;
146+
147+
/// Remove a value from the set. Return true if the value was
148+
/// present in the set.
149+
fn remove(&mut self, value: &T) -> bool;
150+
}
151+
152+
/// A double-ended sequence that allows querying, insertion and deletion at both
153+
/// ends.
154+
pub trait Deque<T> : Mutable {
155+
/// Provide a reference to the front element, or None if the sequence is
156+
/// empty
157+
fn front<'a>(&'a self) -> Option<&'a T>;
158+
159+
/// Provide a mutable reference to the front element, or None if the
160+
/// sequence is empty
161+
fn front_mut<'a>(&'a mut self) -> Option<&'a mut T>;
162+
163+
/// Provide a reference to the back element, or None if the sequence is
164+
/// empty
165+
fn back<'a>(&'a self) -> Option<&'a T>;
166+
167+
/// Provide a mutable reference to the back element, or None if the sequence
168+
/// is empty
169+
fn back_mut<'a>(&'a mut self) -> Option<&'a mut T>;
170+
171+
/// Insert an element first in the sequence
172+
fn push_front(&mut self, elt: T);
173+
174+
/// Insert an element last in the sequence
175+
fn push_back(&mut self, elt: T);
176+
177+
/// Remove the last element and return it, or None if the sequence is empty
178+
fn pop_back(&mut self) -> Option<T>;
179+
180+
/// Remove the first element and return it, or None if the sequence is empty
181+
fn pop_front(&mut self) -> Option<T>;
73182
}
74183

75184
// FIXME(#14344) this shouldn't be necessary

src/libcollections/priority_queue.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use core::prelude::*;
1717
use core::mem::{zeroed, replace, swap};
1818
use core::ptr;
1919

20+
use {Collection, Mutable};
2021
use slice;
2122
use vec::Vec;
2223

@@ -26,7 +27,7 @@ pub struct PriorityQueue<T> {
2627
data: Vec<T>,
2728
}
2829

29-
impl<T: Ord> Container for PriorityQueue<T> {
30+
impl<T: Ord> Collection for PriorityQueue<T> {
3031
/// Returns the length of the queue
3132
fn len(&self) -> uint { self.data.len() }
3233
}

src/libcollections/ringbuf.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
//! A double-ended queue implemented as a circular buffer
1212
//!
1313
//! RingBuf implements the trait Deque. It should be imported with `use
14-
//! collections::deque::Deque`.
14+
//! collections::Deque`.
1515
1616
use core::prelude::*;
1717

1818
use core::cmp;
1919
use core::fmt;
2020
use core::iter::RandomAccessIterator;
2121

22-
use deque::Deque;
22+
use {Deque, Collection, Mutable};
2323
use vec::Vec;
2424

2525
static INITIAL_CAPACITY: uint = 8u; // 2^3
@@ -33,7 +33,7 @@ pub struct RingBuf<T> {
3333
elts: Vec<Option<T>>
3434
}
3535

36-
impl<T> Container for RingBuf<T> {
36+
impl<T> Collection for RingBuf<T> {
3737
/// Return the number of elements in the RingBuf
3838
fn len(&self) -> uint { self.nelts }
3939
}
@@ -415,7 +415,7 @@ mod tests {
415415
use test::Bencher;
416416
use test;
417417

418-
use deque::Deque;
418+
use {Deque, Mutable};
419419
use super::RingBuf;
420420
use vec::Vec;
421421

src/libcollections/slice.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ use core::mem::transmute;
109109
use core::mem;
110110
use core::ptr;
111111
use core::iter::{range_step, MultiplicativeIterator};
112+
113+
use Collection;
112114
use vec::Vec;
113115

114116
pub use core::slice::{ref_slice, mut_ref_slice, Splits, Windows};
@@ -296,9 +298,9 @@ impl<'a, T: Clone> CloneableVector<T> for &'a [T] {
296298

297299
let len = self.len();
298300
let data_size = len.checked_mul(&mem::size_of::<T>());
299-
let data_size = ::expect(data_size, "overflow in to_owned()");
301+
let data_size = data_size.expect("overflow in to_owned()");
300302
let size = mem::size_of::<RawVec<()>>().checked_add(&data_size);
301-
let size = ::expect(size, "overflow in to_owned()");
303+
let size = size.expect("overflow in to_owned()");
302304

303305
unsafe {
304306
// this should pass the real required alignment
@@ -865,6 +867,7 @@ mod tests {
865867
use std::rt;
866868
use slice::*;
867869

870+
use Mutable;
868871
use vec::Vec;
869872

870873
fn square(n: uint) -> uint { n * n }

src/libcollections/smallintmap.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use core::fmt;
2121
use core::iter::{Enumerate, FilterMap};
2222
use core::mem::replace;
2323

24+
use {Collection, Mutable, Map, MutableMap};
2425
use {vec, slice};
2526
use vec::Vec;
2627

@@ -29,7 +30,7 @@ pub struct SmallIntMap<T> {
2930
v: Vec<Option<T>>,
3031
}
3132

32-
impl<V> Container for SmallIntMap<V> {
33+
impl<V> Collection for SmallIntMap<V> {
3334
/// Return the number of elements in the map
3435
fn len(&self) -> uint {
3536
self.v.iter().filter(|elt| elt.is_some()).count()
@@ -123,7 +124,7 @@ impl<V> SmallIntMap<V> {
123124
}
124125

125126
pub fn get<'a>(&'a self, key: &uint) -> &'a V {
126-
::expect(self.find(key), "key not present")
127+
self.find(key).expect("key not present")
127128
}
128129

129130
/// An iterator visiting all key-value pairs in ascending order by the keys.
@@ -264,6 +265,7 @@ double_ended_iterator!(impl MutEntries -> (uint, &'a mut T), get_mut_ref)
264265
mod test_map {
265266
use std::prelude::*;
266267

268+
use {Map, MutableMap, Mutable};
267269
use super::SmallIntMap;
268270

269271
#[test]

0 commit comments

Comments
 (0)