]> git.lizzy.rs Git - rust.git/blob - src/libcollections/lib.rs
185ca6e361b3aca3671a980e843480bf13b91cfc
[rust.git] / src / libcollections / lib.rs
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.
4 //
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.
10
11 /*!
12  * Collection types.
13  */
14
15 #![crate_id = "collections#0.11.0-pre"]
16 #![crate_type = "rlib"]
17 #![license = "MIT/ASL2"]
18 #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
19        html_favicon_url = "http://www.rust-lang.org/favicon.ico",
20        html_root_url = "http://doc.rust-lang.org/",
21        html_playground_url = "http://play.rust-lang.org/")]
22
23 #![feature(macro_rules, managed_boxes, default_type_params, phase, globs)]
24 #![no_std]
25
26 extern crate alloc;
27
28 #[cfg(stage0)]
29 #[phase(syntax, link)]
30 extern crate core;
31
32 #[cfg(not(stage0))]
33 #[phase(plugin, link)]
34 extern crate core;
35
36 #[cfg(test)] extern crate native;
37 #[cfg(test)] extern crate test;
38 #[cfg(test)] extern crate debug;
39
40 #[cfg(test, stage0)] #[phase(syntax, link)] extern crate std;
41 #[cfg(test, stage0)] #[phase(syntax, link)] extern crate log;
42 #[cfg(test, not(stage0))] #[phase(plugin, link)] extern crate std;
43 #[cfg(test, not(stage0))] #[phase(plugin, link)] extern crate log;
44
45 use core::prelude::*;
46
47 pub use core::collections::Collection;
48 pub use bitv::{Bitv, BitvSet};
49 pub use btree::BTree;
50 pub use dlist::DList;
51 pub use enum_set::EnumSet;
52 pub use priority_queue::PriorityQueue;
53 pub use ringbuf::RingBuf;
54 pub use smallintmap::SmallIntMap;
55 pub use string::String;
56 pub use treemap::{TreeMap, TreeSet};
57 pub use trie::{TrieMap, TrieSet};
58 pub use vec::Vec;
59
60 mod macros;
61
62 pub mod bitv;
63 pub mod btree;
64 pub mod dlist;
65 pub mod enum_set;
66 pub mod priority_queue;
67 pub mod ringbuf;
68 pub mod smallintmap;
69 pub mod treemap;
70 pub mod trie;
71 pub mod slice;
72 pub mod str;
73 pub mod string;
74 pub mod vec;
75 pub mod hash;
76
77 // Internal unicode fiddly bits for the str module
78 mod unicode;
79
80 mod deque;
81
82 /// A trait to represent mutable containers
83 pub trait Mutable: Collection {
84     /// Clear the container, removing all values.
85     fn clear(&mut self);
86 }
87
88 /// A map is a key-value store where values may be looked up by their keys. This
89 /// trait provides basic operations to operate on these stores.
90 pub trait Map<K, V>: Collection {
91     /// Return a reference to the value corresponding to the key
92     fn find<'a>(&'a self, key: &K) -> Option<&'a V>;
93
94     /// Return true if the map contains a value for the specified key
95     #[inline]
96     fn contains_key(&self, key: &K) -> bool {
97         self.find(key).is_some()
98     }
99 }
100
101 /// This trait provides basic operations to modify the contents of a map.
102 pub trait MutableMap<K, V>: Map<K, V> + Mutable {
103     /// Insert a key-value pair into the map. An existing value for a
104     /// key is replaced by the new value. Return true if the key did
105     /// not already exist in the map.
106     #[inline]
107     fn insert(&mut self, key: K, value: V) -> bool {
108         self.swap(key, value).is_none()
109     }
110
111     /// Remove a key-value pair from the map. Return true if the key
112     /// was present in the map, otherwise false.
113     #[inline]
114     fn remove(&mut self, key: &K) -> bool {
115         self.pop(key).is_some()
116     }
117
118     /// Insert a key-value pair from the map. If the key already had a value
119     /// present in the map, that value is returned. Otherwise None is returned.
120     fn swap(&mut self, k: K, v: V) -> Option<V>;
121
122     /// Removes a key from the map, returning the value at the key if the key
123     /// was previously in the map.
124     fn pop(&mut self, k: &K) -> Option<V>;
125
126     /// Return a mutable reference to the value corresponding to the key
127     fn find_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V>;
128 }
129
130 /// A set is a group of objects which are each distinct from one another. This
131 /// trait represents actions which can be performed on sets to iterate over
132 /// them.
133 pub trait Set<T>: Collection {
134     /// Return true if the set contains a value
135     fn contains(&self, value: &T) -> bool;
136
137     /// Return true if the set has no elements in common with `other`.
138     /// This is equivalent to checking for an empty intersection.
139     fn is_disjoint(&self, other: &Self) -> bool;
140
141     /// Return true if the set is a subset of another
142     fn is_subset(&self, other: &Self) -> bool;
143
144     /// Return true if the set is a superset of another
145     fn is_superset(&self, other: &Self) -> bool {
146         other.is_subset(self)
147     }
148
149     // FIXME #8154: Add difference, sym. difference, intersection and union iterators
150 }
151
152 /// This trait represents actions which can be performed on sets to mutate
153 /// them.
154 pub trait MutableSet<T>: Set<T> + Mutable {
155     /// Add a value to the set. Return true if the value was not already
156     /// present in the set.
157     fn insert(&mut self, value: T) -> bool;
158
159     /// Remove a value from the set. Return true if the value was
160     /// present in the set.
161     fn remove(&mut self, value: &T) -> bool;
162 }
163
164 /// A double-ended sequence that allows querying, insertion and deletion at both
165 /// ends.
166 pub trait Deque<T> : Mutable {
167     /// Provide a reference to the front element, or None if the sequence is
168     /// empty
169     fn front<'a>(&'a self) -> Option<&'a T>;
170
171     /// Provide a mutable reference to the front element, or None if the
172     /// sequence is empty
173     fn front_mut<'a>(&'a mut self) -> Option<&'a mut T>;
174
175     /// Provide a reference to the back element, or None if the sequence is
176     /// empty
177     fn back<'a>(&'a self) -> Option<&'a T>;
178
179     /// Provide a mutable reference to the back element, or None if the sequence
180     /// is empty
181     fn back_mut<'a>(&'a mut self) -> Option<&'a mut T>;
182
183     /// Insert an element first in the sequence
184     fn push_front(&mut self, elt: T);
185
186     /// Insert an element last in the sequence
187     fn push_back(&mut self, elt: T);
188
189     /// Remove the last element and return it, or None if the sequence is empty
190     fn pop_back(&mut self) -> Option<T>;
191
192     /// Remove the first element and return it, or None if the sequence is empty
193     fn pop_front(&mut self) -> Option<T>;
194 }
195
196 // FIXME(#14344) this shouldn't be necessary
197 #[doc(hidden)]
198 pub fn fixme_14344_be_sure_to_link_to_collections() {}
199
200 #[cfg(not(test))]
201 mod std {
202     pub use core::fmt;      // necessary for fail!()
203     pub use core::option;   // necessary for fail!()
204     pub use core::clone;    // deriving(Clone)
205     pub use core::cmp;      // deriving(Eq, Ord, etc.)
206     pub use hash;           // deriving(Hash)
207 }