1 // Copyright 2013-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
15 #![crate_name = "collections"]
17 #![crate_type = "rlib"]
18 #![license = "MIT/ASL2"]
19 #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
20 html_favicon_url = "http://www.rust-lang.org/favicon.ico",
21 html_root_url = "http://doc.rust-lang.org/master/",
22 html_playground_url = "http://play.rust-lang.org/")]
24 #![feature(macro_rules, managed_boxes, default_type_params, phase, globs)]
25 #![feature(unsafe_destructor)]
28 #[phase(plugin, link)] extern crate core;
32 #[cfg(test)] extern crate native;
33 #[cfg(test)] extern crate test;
34 #[cfg(test)] extern crate debug;
36 #[cfg(test)] #[phase(plugin, link)] extern crate std;
37 #[cfg(test)] #[phase(plugin, link)] extern crate log;
41 pub use core::collections::Collection;
42 pub use bitv::{Bitv, BitvSet};
45 pub use enum_set::EnumSet;
46 pub use priority_queue::PriorityQueue;
47 pub use ringbuf::RingBuf;
48 pub use smallintmap::SmallIntMap;
49 pub use string::String;
50 pub use treemap::{TreeMap, TreeSet};
51 pub use trie::{TrieMap, TrieSet};
60 pub mod priority_queue;
73 /// A trait to represent mutable containers
74 pub trait Mutable: Collection {
75 /// Clear the container, removing all values.
80 /// let mut v = vec![1i, 2, 3];
82 /// assert!(v.is_empty());
87 /// A map is a key-value store where values may be looked up by their keys. This
88 /// trait provides basic operations to operate on these stores.
89 pub trait Map<K, V>: Collection {
90 /// Return a reference to the value corresponding to the key.
95 /// use std::collections::HashMap;
97 /// let mut map = HashMap::new();
98 /// map.insert("a", 1i);
99 /// assert_eq!(map.find(&"a"), Some(&1i));
100 /// assert_eq!(map.find(&"b"), None);
102 fn find<'a>(&'a self, key: &K) -> Option<&'a V>;
104 /// Return true if the map contains a value for the specified key.
109 /// use std::collections::HashMap;
111 /// let mut map = HashMap::new();
112 /// map.insert("a", 1i);
113 /// assert_eq!(map.contains_key(&"a"), true);
114 /// assert_eq!(map.contains_key(&"b"), false);
117 fn contains_key(&self, key: &K) -> bool {
118 self.find(key).is_some()
122 /// This trait provides basic operations to modify the contents of a map.
123 pub trait MutableMap<K, V>: Map<K, V> + Mutable {
124 /// Insert a key-value pair into the map. An existing value for a
125 /// key is replaced by the new value. Return true if the key did
126 /// not already exist in the map.
131 /// use std::collections::HashMap;
133 /// let mut map = HashMap::new();
134 /// assert_eq!(map.insert("key", 2i), true);
135 /// assert_eq!(map.insert("key", 9i), false);
136 /// assert_eq!(map.get(&"key"), &9i);
139 fn insert(&mut self, key: K, value: V) -> bool {
140 self.swap(key, value).is_none()
143 /// Remove a key-value pair from the map. Return true if the key
144 /// was present in the map, otherwise false.
149 /// use std::collections::HashMap;
151 /// let mut map = HashMap::new();
152 /// assert_eq!(map.remove(&"key"), false);
153 /// map.insert("key", 2i);
154 /// assert_eq!(map.remove(&"key"), true);
157 fn remove(&mut self, key: &K) -> bool {
158 self.pop(key).is_some()
161 /// Insert a key-value pair from the map. If the key already had a value
162 /// present in the map, that value is returned. Otherwise None is returned.
167 /// use std::collections::HashMap;
169 /// let mut map = HashMap::new();
170 /// assert_eq!(map.swap("a", 37i), None);
171 /// assert_eq!(map.is_empty(), false);
173 /// map.insert("a", 1i);
174 /// assert_eq!(map.swap("a", 37i), Some(1i));
175 /// assert_eq!(map.get(&"a"), &37i);
177 fn swap(&mut self, k: K, v: V) -> Option<V>;
179 /// Removes a key from the map, returning the value at the key if the key
180 /// was previously in the map.
185 /// use std::collections::HashMap;
187 /// let mut map: HashMap<&str, int> = HashMap::new();
188 /// map.insert("a", 1i);
189 /// assert_eq!(map.pop(&"a"), Some(1i));
190 /// assert_eq!(map.pop(&"a"), None);
192 fn pop(&mut self, k: &K) -> Option<V>;
194 /// Return a mutable reference to the value corresponding to the key.
199 /// use std::collections::HashMap;
201 /// let mut map = HashMap::new();
202 /// map.insert("a", 1i);
203 /// match map.find_mut(&"a") {
204 /// Some(x) => *x = 7i,
207 /// assert_eq!(map.get(&"a"), &7i);
209 fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V>;
212 /// A set is a group of objects which are each distinct from one another. This
213 /// trait represents actions which can be performed on sets to iterate over
215 pub trait Set<T>: Collection {
216 /// Return true if the set contains a value.
221 /// use std::collections::HashSet;
223 /// let set: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
224 /// assert_eq!(set.contains(&1), true);
225 /// assert_eq!(set.contains(&4), false);
227 fn contains(&self, value: &T) -> bool;
229 /// Return true if the set has no elements in common with `other`.
230 /// This is equivalent to checking for an empty intersection.
235 /// use std::collections::HashSet;
237 /// let a: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
238 /// let mut b: HashSet<int> = HashSet::new();
240 /// assert_eq!(a.is_disjoint(&b), true);
242 /// assert_eq!(a.is_disjoint(&b), true);
244 /// assert_eq!(a.is_disjoint(&b), false);
246 fn is_disjoint(&self, other: &Self) -> bool;
248 /// Return true if the set is a subset of another.
253 /// use std::collections::HashSet;
255 /// let sup: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
256 /// let mut set: HashSet<int> = HashSet::new();
258 /// assert_eq!(set.is_subset(&sup), true);
260 /// assert_eq!(set.is_subset(&sup), true);
262 /// assert_eq!(set.is_subset(&sup), false);
264 fn is_subset(&self, other: &Self) -> bool;
266 /// Return true if the set is a superset of another.
271 /// use std::collections::HashSet;
273 /// let sub: HashSet<int> = [1i, 2].iter().map(|&x| x).collect();
274 /// let mut set: HashSet<int> = HashSet::new();
276 /// assert_eq!(set.is_superset(&sub), false);
280 /// assert_eq!(set.is_superset(&sub), false);
283 /// assert_eq!(set.is_superset(&sub), true);
285 fn is_superset(&self, other: &Self) -> bool {
286 other.is_subset(self)
289 // FIXME #8154: Add difference, sym. difference, intersection and union iterators
292 /// This trait represents actions which can be performed on sets to mutate
294 pub trait MutableSet<T>: Set<T> + Mutable {
295 /// Add a value to the set. Return true if the value was not already
296 /// present in the set.
301 /// use std::collections::HashSet;
303 /// let mut set = HashSet::new();
305 /// assert_eq!(set.insert(2i), true);
306 /// assert_eq!(set.insert(2i), false);
307 /// assert_eq!(set.len(), 1);
309 fn insert(&mut self, value: T) -> bool;
311 /// Remove a value from the set. Return true if the value was
312 /// present in the set.
317 /// use std::collections::HashSet;
319 /// let mut set = HashSet::new();
322 /// assert_eq!(set.remove(&2), true);
323 /// assert_eq!(set.remove(&2), false);
325 fn remove(&mut self, value: &T) -> bool;
328 /// A double-ended sequence that allows querying, insertion and deletion at both
333 /// With a `Deque` we can simulate a queue efficiently:
336 /// use std::collections::{RingBuf, Deque};
338 /// let mut queue = RingBuf::new();
339 /// queue.push_back(1i);
340 /// queue.push_back(2i);
341 /// queue.push_back(3i);
343 /// // Will print 1, 2, 3
344 /// while !queue.is_empty() {
345 /// let x = queue.pop_front().unwrap();
346 /// println!("{}", x);
350 /// We can also simulate a stack:
353 /// use std::collections::{RingBuf, Deque};
355 /// let mut stack = RingBuf::new();
356 /// stack.push_front(1i);
357 /// stack.push_front(2i);
358 /// stack.push_front(3i);
360 /// // Will print 3, 2, 1
361 /// while !stack.is_empty() {
362 /// let x = stack.pop_front().unwrap();
363 /// println!("{}", x);
367 /// And of course we can mix and match:
370 /// use std::collections::{DList, Deque};
372 /// let mut deque = DList::new();
374 /// // Init deque with 1, 2, 3, 4
375 /// deque.push_front(2i);
376 /// deque.push_front(1i);
377 /// deque.push_back(3i);
378 /// deque.push_back(4i);
380 /// // Will print (1, 4) and (2, 3)
381 /// while !deque.is_empty() {
382 /// let f = deque.pop_front().unwrap();
383 /// let b = deque.pop_back().unwrap();
384 /// println!("{}", (f, b));
387 pub trait Deque<T> : Mutable {
388 /// Provide a reference to the front element, or `None` if the sequence is
394 /// use std::collections::{RingBuf, Deque};
396 /// let mut d = RingBuf::new();
397 /// assert_eq!(d.front(), None);
401 /// assert_eq!(d.front(), Some(&1i));
403 fn front<'a>(&'a self) -> Option<&'a T>;
405 /// Provide a mutable reference to the front element, or `None` if the
406 /// sequence is empty.
411 /// use std::collections::{RingBuf, Deque};
413 /// let mut d = RingBuf::new();
414 /// assert_eq!(d.front_mut(), None);
418 /// match d.front_mut() {
419 /// Some(x) => *x = 9i,
422 /// assert_eq!(d.front(), Some(&9i));
424 fn front_mut<'a>(&'a mut self) -> Option<&'a mut T>;
426 /// Provide a reference to the back element, or `None` if the sequence is
432 /// use std::collections::{DList, Deque};
434 /// let mut d = DList::new();
435 /// assert_eq!(d.back(), None);
439 /// assert_eq!(d.back(), Some(&2i));
441 fn back<'a>(&'a self) -> Option<&'a T>;
443 /// Provide a mutable reference to the back element, or `None` if the sequence
449 /// use std::collections::{DList, Deque};
451 /// let mut d = DList::new();
452 /// assert_eq!(d.back(), None);
456 /// match d.back_mut() {
457 /// Some(x) => *x = 9i,
460 /// assert_eq!(d.back(), Some(&9i));
462 fn back_mut<'a>(&'a mut self) -> Option<&'a mut T>;
464 /// Insert an element first in the sequence.
469 /// use std::collections::{DList, Deque};
471 /// let mut d = DList::new();
472 /// d.push_front(1i);
473 /// d.push_front(2i);
474 /// assert_eq!(d.front(), Some(&2i));
476 fn push_front(&mut self, elt: T);
478 /// Insert an element last in the sequence.
483 /// use std::collections::{DList, Deque};
485 /// let mut d = DList::new();
488 /// assert_eq!(d.front(), Some(&1i));
490 fn push_back(&mut self, elt: T);
492 /// Remove the last element and return it, or `None` if the sequence is empty.
497 /// use std::collections::{RingBuf, Deque};
499 /// let mut d = RingBuf::new();
503 /// assert_eq!(d.pop_back(), Some(2i));
504 /// assert_eq!(d.pop_back(), Some(1i));
505 /// assert_eq!(d.pop_back(), None);
507 fn pop_back(&mut self) -> Option<T>;
509 /// Remove the first element and return it, or `None` if the sequence is empty.
514 /// use std::collections::{RingBuf, Deque};
516 /// let mut d = RingBuf::new();
520 /// assert_eq!(d.pop_front(), Some(1i));
521 /// assert_eq!(d.pop_front(), Some(2i));
522 /// assert_eq!(d.pop_front(), None);
524 fn pop_front(&mut self) -> Option<T>;
527 // FIXME(#14344) this shouldn't be necessary
529 pub fn fixme_14344_be_sure_to_link_to_collections() {}
533 pub use core::fmt; // necessary for fail!()
534 pub use core::option; // necessary for fail!()
535 pub use core::clone; // deriving(Clone)
536 pub use core::cmp; // deriving(Eq, Ord, etc.)
537 pub use hash; // deriving(Hash)