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