Skip to content

Commit da07039

Browse files
committed
core: Move the collections traits to libcollections
This commit moves Mutable, Map, MutableMap, Set, and MutableSet from `core::collections` to the `collections` crate at the top-level. Additionally, this removes the `deque` module and moves the `Deque` trait to only being available at the top-level of the collections crate. All functionality continues to be reexported through `std::collections`. [breaking-change]
1 parent 50942c7 commit da07039

25 files changed

+171
-154
lines changed

src/libcollections/bitv.rs

Lines changed: 2 additions & 0 deletions
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)]
@@ -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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 29 deletions
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

Lines changed: 3 additions & 3 deletions
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> {
@@ -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

Lines changed: 116 additions & 7 deletions
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

Lines changed: 1 addition & 0 deletions
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

src/libcollections/ringbuf.rs

Lines changed: 3 additions & 3 deletions
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
@@ -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

Lines changed: 5 additions & 2 deletions
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

Lines changed: 3 additions & 1 deletion
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

@@ -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]

src/libcollections/str.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ use core::cmp;
7676
use core::iter::AdditiveIterator;
7777
use core::mem;
7878

79+
use Collection;
7980
use hash;
8081
use string::String;
8182
use vec::Vec;

src/libcollections/string.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use core::mem;
1818
use core::ptr;
1919
use core::raw::Slice;
2020

21+
use {Collection, Mutable};
2122
use hash;
2223
use str;
2324
use str::{CharRange, StrAllocating};
@@ -356,6 +357,7 @@ mod tests {
356357
use std::prelude::*;
357358
use test::Bencher;
358359

360+
use Mutable;
359361
use str::{Str, StrSlice};
360362
use super::String;
361363

src/libcollections/treemap.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use core::iter;
2222
use core::mem::{replace, swap};
2323
use core::ptr;
2424

25+
use {Collection, Mutable, Set, MutableSet, MutableMap, Map};
2526
use vec::Vec;
2627

2728
// This is implemented as an AA tree, which is a simplified variation of
@@ -1006,6 +1007,7 @@ mod test_treemap {
10061007
use std::rand::Rng;
10071008
use std::rand;
10081009

1010+
use {Map, MutableMap, Mutable};
10091011
use super::{TreeMap, TreeNode};
10101012

10111013
#[test]
@@ -1437,7 +1439,6 @@ mod test_treemap {
14371439

14381440
#[cfg(test)]
14391441
mod bench {
1440-
use std::prelude::*;
14411442
use test::Bencher;
14421443

14431444
use super::TreeMap;
@@ -1500,6 +1501,7 @@ mod bench {
15001501
mod test_set {
15011502
use std::prelude::*;
15021503

1504+
use {Set, MutableSet, Mutable, MutableMap};
15031505
use super::{TreeMap, TreeSet};
15041506

15051507
#[test]

0 commit comments

Comments
 (0)