1 // Copyright 2012-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.
11 //! A simple map based on a vector for small integer keys. Space requirements
12 //! are O(highest integer key).
14 #![allow(missing_docs)]
18 use core::cmp::Ordering;
19 use core::default::Default;
21 use core::hash::{Hash, Writer};
22 use core::iter::{Enumerate, FilterMap, Map, FromIterator};
24 use core::mem::replace;
25 use core::ops::{Index, IndexMut};
30 // FIXME(conventions): capacity management???
32 /// A map optimized for small integer keys.
37 /// use std::collections::VecMap;
39 /// let mut months = VecMap::new();
40 /// months.insert(1, "Jan");
41 /// months.insert(2, "Feb");
42 /// months.insert(3, "Mar");
44 /// if !months.contains_key(&12) {
45 /// println!("The end is near!");
48 /// assert_eq!(months.get(&1), Some(&"Jan"));
50 /// match months.get_mut(&3) {
51 /// Some(value) => *value = "Venus",
55 /// assert_eq!(months.get(&3), Some(&"Venus"));
57 /// // Print out all months
58 /// for (key, value) in months.iter() {
59 /// println!("month {} is {}", key, value);
63 /// assert!(months.is_empty());
65 pub struct VecMap<V> {
70 impl<V> Default for VecMap<V> {
73 fn default() -> VecMap<V> { VecMap::new() }
76 impl<V:Clone> Clone for VecMap<V> {
78 fn clone(&self) -> VecMap<V> {
79 VecMap { v: self.v.clone() }
83 fn clone_from(&mut self, source: &VecMap<V>) {
84 self.v.clone_from(&source.v);
88 impl<S: Writer, V: Hash<S>> Hash<S> for VecMap<V> {
89 fn hash(&self, state: &mut S) {
90 // In order to not traverse the `VecMap` twice, count the elements
92 let mut count: uint = 0;
93 for elt in self.iter() {
102 /// Creates an empty `VecMap`.
107 /// use std::collections::VecMap;
108 /// let mut map: VecMap<&str> = VecMap::new();
111 pub fn new() -> VecMap<V> { VecMap { v: vec![] } }
113 /// Creates an empty `VecMap` with space for at least `capacity`
114 /// elements before resizing.
119 /// use std::collections::VecMap;
120 /// let mut map: VecMap<&str> = VecMap::with_capacity(10);
123 pub fn with_capacity(capacity: uint) -> VecMap<V> {
124 VecMap { v: Vec::with_capacity(capacity) }
127 /// Returns the number of elements the `VecMap` can hold without
133 /// use std::collections::VecMap;
134 /// let map: VecMap<String> = VecMap::with_capacity(10);
135 /// assert!(map.capacity() >= 10);
139 pub fn capacity(&self) -> uint {
143 /// Reserves capacity for the given `VecMap` to contain `len` distinct keys.
144 /// In the case of `VecMap` this means reallocations will not occur as long
145 /// as all inserted keys are less than `len`.
147 /// The collection may reserve more space to avoid frequent reallocations.
152 /// use std::collections::VecMap;
153 /// let mut map: VecMap<&str> = VecMap::new();
154 /// map.reserve_len(10);
155 /// assert!(map.capacity() >= 10);
158 pub fn reserve_len(&mut self, len: uint) {
159 let cur_len = self.v.len();
161 self.v.reserve(len - cur_len);
165 /// Reserves the minimum capacity for the given `VecMap` to contain `len` distinct keys.
166 /// In the case of `VecMap` this means reallocations will not occur as long as all inserted
167 /// keys are less than `len`.
169 /// Note that the allocator may give the collection more space than it requests.
170 /// Therefore capacity cannot be relied upon to be precisely minimal. Prefer
171 /// `reserve_len` if future insertions are expected.
176 /// use std::collections::VecMap;
177 /// let mut map: VecMap<&str> = VecMap::new();
178 /// map.reserve_len_exact(10);
179 /// assert!(map.capacity() >= 10);
182 pub fn reserve_len_exact(&mut self, len: uint) {
183 let cur_len = self.v.len();
185 self.v.reserve_exact(len - cur_len);
189 /// Returns an iterator visiting all keys in ascending order by the keys.
190 /// The iterator's element type is `uint`.
192 pub fn keys<'r>(&'r self) -> Keys<'r, V> {
193 fn first<A, B>((a, _): (A, B)) -> A { a }
194 let first: fn((uint, &'r V)) -> uint = first; // coerce to fn pointer
196 Keys { iter: self.iter().map(first) }
199 /// Returns an iterator visiting all values in ascending order by the keys.
200 /// The iterator's element type is `&'r V`.
202 pub fn values<'r>(&'r self) -> Values<'r, V> {
203 fn second<A, B>((_, b): (A, B)) -> B { b }
204 let second: fn((uint, &'r V)) -> &'r V = second; // coerce to fn pointer
206 Values { iter: self.iter().map(second) }
209 /// Returns an iterator visiting all key-value pairs in ascending order by the keys.
210 /// The iterator's element type is `(uint, &'r V)`.
215 /// use std::collections::VecMap;
217 /// let mut map = VecMap::new();
218 /// map.insert(1, "a");
219 /// map.insert(3, "c");
220 /// map.insert(2, "b");
222 /// // Print `1: a` then `2: b` then `3: c`
223 /// for (key, value) in map.iter() {
224 /// println!("{}: {}", key, value);
228 pub fn iter<'r>(&'r self) -> Iter<'r, V> {
236 /// Returns an iterator visiting all key-value pairs in ascending order by the keys,
237 /// with mutable references to the values.
238 /// The iterator's element type is `(uint, &'r mut V)`.
243 /// use std::collections::VecMap;
245 /// let mut map = VecMap::new();
246 /// map.insert(1, "a");
247 /// map.insert(2, "b");
248 /// map.insert(3, "c");
250 /// for (key, value) in map.iter_mut() {
254 /// for (key, value) in map.iter() {
255 /// assert_eq!(value, &"x");
259 pub fn iter_mut<'r>(&'r mut self) -> IterMut<'r, V> {
263 iter: self.v.iter_mut()
267 /// Returns an iterator visiting all key-value pairs in ascending order by
268 /// the keys, emptying (but not consuming) the original `VecMap`.
269 /// The iterator's element type is `(uint, &'r V)`.
274 /// use std::collections::VecMap;
276 /// let mut map = VecMap::new();
277 /// map.insert(1, "a");
278 /// map.insert(3, "c");
279 /// map.insert(2, "b");
281 /// // Not possible with .iter()
282 /// let vec: Vec<(uint, &str)> = map.into_iter().collect();
284 /// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
287 pub fn into_iter(&mut self) -> IntoIter<V> {
288 fn filter<A>((i, v): (uint, Option<A>)) -> Option<(uint, A)> {
291 let filter: fn((uint, Option<V>)) -> Option<(uint, V)> = filter; // coerce to fn ptr
293 let values = replace(&mut self.v, vec!());
294 IntoIter { iter: values.into_iter().enumerate().filter_map(filter) }
297 /// Return the number of elements in the map.
302 /// use std::collections::VecMap;
304 /// let mut a = VecMap::new();
305 /// assert_eq!(a.len(), 0);
306 /// a.insert(1, "a");
307 /// assert_eq!(a.len(), 1);
310 pub fn len(&self) -> uint {
311 self.v.iter().filter(|elt| elt.is_some()).count()
314 /// Return true if the map contains no elements.
319 /// use std::collections::VecMap;
321 /// let mut a = VecMap::new();
322 /// assert!(a.is_empty());
323 /// a.insert(1, "a");
324 /// assert!(!a.is_empty());
327 pub fn is_empty(&self) -> bool {
328 self.v.iter().all(|elt| elt.is_none())
331 /// Clears the map, removing all key-value pairs.
336 /// use std::collections::VecMap;
338 /// let mut a = VecMap::new();
339 /// a.insert(1, "a");
341 /// assert!(a.is_empty());
344 pub fn clear(&mut self) { self.v.clear() }
346 /// Returns a reference to the value corresponding to the key.
351 /// use std::collections::VecMap;
353 /// let mut map = VecMap::new();
354 /// map.insert(1, "a");
355 /// assert_eq!(map.get(&1), Some(&"a"));
356 /// assert_eq!(map.get(&2), None);
359 pub fn get(&self, key: &uint) -> Option<&V> {
360 if *key < self.v.len() {
362 Some(ref value) => Some(value),
370 /// Returns true if the map contains a value for the specified key.
375 /// use std::collections::VecMap;
377 /// let mut map = VecMap::new();
378 /// map.insert(1, "a");
379 /// assert_eq!(map.contains_key(&1), true);
380 /// assert_eq!(map.contains_key(&2), false);
384 pub fn contains_key(&self, key: &uint) -> bool {
385 self.get(key).is_some()
388 /// Returns a mutable reference to the value corresponding to the key.
393 /// use std::collections::VecMap;
395 /// let mut map = VecMap::new();
396 /// map.insert(1, "a");
397 /// match map.get_mut(&1) {
398 /// Some(x) => *x = "b",
401 /// assert_eq!(map[1], "b");
404 pub fn get_mut(&mut self, key: &uint) -> Option<&mut V> {
405 if *key < self.v.len() {
406 match *(&mut self.v[*key]) {
407 Some(ref mut value) => Some(value),
415 /// Inserts a key-value pair from the map. If the key already had a value
416 /// present in the map, that value is returned. Otherwise, `None` is returned.
421 /// use std::collections::VecMap;
423 /// let mut map = VecMap::new();
424 /// assert_eq!(map.insert(37, "a"), None);
425 /// assert_eq!(map.is_empty(), false);
427 /// map.insert(37, "b");
428 /// assert_eq!(map.insert(37, "c"), Some("b"));
429 /// assert_eq!(map[37], "c");
432 pub fn insert(&mut self, key: uint, value: V) -> Option<V> {
433 let len = self.v.len();
435 self.v.extend(range(0, key - len + 1).map(|_| None));
437 replace(&mut self.v[key], Some(value))
440 /// Removes a key from the map, returning the value at the key if the key
441 /// was previously in the map.
446 /// use std::collections::VecMap;
448 /// let mut map = VecMap::new();
449 /// map.insert(1, "a");
450 /// assert_eq!(map.remove(&1), Some("a"));
451 /// assert_eq!(map.remove(&1), None);
454 pub fn remove(&mut self, key: &uint) -> Option<V> {
455 if *key >= self.v.len() {
463 impl<V: PartialEq> PartialEq for VecMap<V> {
464 fn eq(&self, other: &VecMap<V>) -> bool {
465 iter::order::eq(self.iter(), other.iter())
470 impl<V: Eq> Eq for VecMap<V> {}
473 impl<V: PartialOrd> PartialOrd for VecMap<V> {
475 fn partial_cmp(&self, other: &VecMap<V>) -> Option<Ordering> {
476 iter::order::partial_cmp(self.iter(), other.iter())
481 impl<V: Ord> Ord for VecMap<V> {
483 fn cmp(&self, other: &VecMap<V>) -> Ordering {
484 iter::order::cmp(self.iter(), other.iter())
489 impl<V: fmt::Show> fmt::Show for VecMap<V> {
490 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
491 try!(write!(f, "{{"));
493 for (i, (k, v)) in self.iter().enumerate() {
494 if i != 0 { try!(write!(f, ", ")); }
495 try!(write!(f, "{}: {}", k, *v));
503 impl<V> FromIterator<(uint, V)> for VecMap<V> {
504 fn from_iter<Iter: Iterator<Item=(uint, V)>>(iter: Iter) -> VecMap<V> {
505 let mut map = VecMap::new();
512 impl<V> Extend<(uint, V)> for VecMap<V> {
513 fn extend<Iter: Iterator<Item=(uint, V)>>(&mut self, mut iter: Iter) {
520 // NOTE(stage0): remove impl after a snapshot
523 impl<V> Index<uint, V> for VecMap<V> {
525 fn index<'a>(&'a self, i: &uint) -> &'a V {
526 self.get(i).expect("key not present")
530 #[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
531 impl<V> Index<uint> for VecMap<V> {
535 fn index<'a>(&'a self, i: &uint) -> &'a V {
536 self.get(i).expect("key not present")
540 // NOTE(stage0): remove impl after a snapshot
543 impl<V> IndexMut<uint, V> for VecMap<V> {
545 fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut V {
546 self.get_mut(i).expect("key not present")
550 #[cfg(not(stage0))] // NOTE(stage0): remove cfg after a snapshot
552 impl<V> IndexMut<uint> for VecMap<V> {
556 fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut V {
557 self.get_mut(i).expect("key not present")
561 macro_rules! iterator {
562 (impl $name:ident -> $elem:ty, $($getter:ident),+) => {
564 impl<'a, V> Iterator for $name<'a, V> {
568 fn next(&mut self) -> Option<$elem> {
569 while self.front < self.back {
570 match self.iter.next() {
572 match elem$(. $getter ())+ {
574 let index = self.front;
576 return Some((index, x));
589 fn size_hint(&self) -> (uint, Option<uint>) {
590 (0, Some(self.back - self.front))
596 macro_rules! double_ended_iterator {
597 (impl $name:ident -> $elem:ty, $($getter:ident),+) => {
599 impl<'a, V> DoubleEndedIterator for $name<'a, V> {
601 fn next_back(&mut self) -> Option<$elem> {
602 while self.front < self.back {
603 match self.iter.next_back() {
605 match elem$(. $getter ())+ {
608 return Some((self.back, x));
623 /// An iterator over the key-value pairs of a map.
625 pub struct Iter<'a, V:'a> {
628 iter: slice::Iter<'a, Option<V>>
631 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
632 impl<'a, V> Clone for Iter<'a, V> {
633 fn clone(&self) -> Iter<'a, V> {
637 iter: self.iter.clone()
642 iterator! { impl Iter -> (uint, &'a V), as_ref }
643 double_ended_iterator! { impl Iter -> (uint, &'a V), as_ref }
645 /// An iterator over the key-value pairs of a map, with the
646 /// values being mutable.
648 pub struct IterMut<'a, V:'a> {
651 iter: slice::IterMut<'a, Option<V>>
654 iterator! { impl IterMut -> (uint, &'a mut V), as_mut }
655 double_ended_iterator! { impl IterMut -> (uint, &'a mut V), as_mut }
657 /// An iterator over the keys of a map.
659 pub struct Keys<'a, V: 'a> {
660 iter: Map<(uint, &'a V), uint, Iter<'a, V>, fn((uint, &'a V)) -> uint>
663 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
664 impl<'a, V> Clone for Keys<'a, V> {
665 fn clone(&self) -> Keys<'a, V> {
667 iter: self.iter.clone()
672 /// An iterator over the values of a map.
674 pub struct Values<'a, V: 'a> {
675 iter: Map<(uint, &'a V), &'a V, Iter<'a, V>, fn((uint, &'a V)) -> &'a V>
678 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
679 impl<'a, V> Clone for Values<'a, V> {
680 fn clone(&self) -> Values<'a, V> {
682 iter: self.iter.clone()
687 /// A consuming iterator over the key-value pairs of a map.
689 pub struct IntoIter<V> {
693 Enumerate<vec::IntoIter<Option<V>>>,
694 fn((uint, Option<V>)) -> Option<(uint, V)>>
698 impl<'a, V> Iterator for Keys<'a, V> {
701 fn next(&mut self) -> Option<uint> { self.iter.next() }
702 fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
705 impl<'a, V> DoubleEndedIterator for Keys<'a, V> {
706 fn next_back(&mut self) -> Option<uint> { self.iter.next_back() }
710 impl<'a, V> Iterator for Values<'a, V> {
713 fn next(&mut self) -> Option<(&'a V)> { self.iter.next() }
714 fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
717 impl<'a, V> DoubleEndedIterator for Values<'a, V> {
718 fn next_back(&mut self) -> Option<(&'a V)> { self.iter.next_back() }
722 impl<V> Iterator for IntoIter<V> {
723 type Item = (uint, V);
725 fn next(&mut self) -> Option<(uint, V)> { self.iter.next() }
726 fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
729 impl<V> DoubleEndedIterator for IntoIter<V> {
730 fn next_back(&mut self) -> Option<(uint, V)> { self.iter.next_back() }
736 use core::hash::hash;
742 let mut m = VecMap::new();
743 assert!(m.insert(1, 12i).is_none());
744 assert!(m.insert(2, 8).is_none());
745 assert!(m.insert(5, 14).is_none());
747 match m.get_mut(&5) {
748 None => panic!(), Some(x) => *x = new
750 assert_eq!(m.get(&5), Some(&new));
755 let mut map = VecMap::new();
756 assert_eq!(map.len(), 0);
757 assert!(map.is_empty());
758 assert!(map.insert(5, 20i).is_none());
759 assert_eq!(map.len(), 1);
760 assert!(!map.is_empty());
761 assert!(map.insert(11, 12).is_none());
762 assert_eq!(map.len(), 2);
763 assert!(!map.is_empty());
764 assert!(map.insert(14, 22).is_none());
765 assert_eq!(map.len(), 3);
766 assert!(!map.is_empty());
771 let mut map = VecMap::new();
772 assert!(map.insert(5, 20i).is_none());
773 assert!(map.insert(11, 12).is_none());
774 assert!(map.insert(14, 22).is_none());
776 assert!(map.is_empty());
777 assert!(map.get(&5).is_none());
778 assert!(map.get(&11).is_none());
779 assert!(map.get(&14).is_none());
784 let mut m = VecMap::new();
785 assert_eq!(m.insert(1, 2i), None);
786 assert_eq!(m.insert(1, 3i), Some(2));
787 assert_eq!(m.insert(1, 4i), Some(3));
792 let mut m = VecMap::new();
794 assert_eq!(m.remove(&1), Some(2));
795 assert_eq!(m.remove(&1), None);
800 let mut map = VecMap::new();
804 let keys = map.keys().collect::<Vec<uint>>();
805 assert_eq!(keys.len(), 3);
806 assert!(keys.contains(&1));
807 assert!(keys.contains(&2));
808 assert!(keys.contains(&3));
813 let mut map = VecMap::new();
817 let values = map.values().map(|&v| v).collect::<Vec<char>>();
818 assert_eq!(values.len(), 3);
819 assert!(values.contains(&'a'));
820 assert!(values.contains(&'b'));
821 assert!(values.contains(&'c'));
826 let mut m = VecMap::new();
828 assert!(m.insert(0, 1i).is_none());
829 assert!(m.insert(1, 2).is_none());
830 assert!(m.insert(3, 5).is_none());
831 assert!(m.insert(6, 10).is_none());
832 assert!(m.insert(10, 11).is_none());
834 let mut it = m.iter();
835 assert_eq!(it.size_hint(), (0, Some(11)));
836 assert_eq!(it.next().unwrap(), (0, &1));
837 assert_eq!(it.size_hint(), (0, Some(10)));
838 assert_eq!(it.next().unwrap(), (1, &2));
839 assert_eq!(it.size_hint(), (0, Some(9)));
840 assert_eq!(it.next().unwrap(), (3, &5));
841 assert_eq!(it.size_hint(), (0, Some(7)));
842 assert_eq!(it.next().unwrap(), (6, &10));
843 assert_eq!(it.size_hint(), (0, Some(4)));
844 assert_eq!(it.next().unwrap(), (10, &11));
845 assert_eq!(it.size_hint(), (0, Some(0)));
846 assert!(it.next().is_none());
850 fn test_iterator_size_hints() {
851 let mut m = VecMap::new();
853 assert!(m.insert(0, 1i).is_none());
854 assert!(m.insert(1, 2).is_none());
855 assert!(m.insert(3, 5).is_none());
856 assert!(m.insert(6, 10).is_none());
857 assert!(m.insert(10, 11).is_none());
859 assert_eq!(m.iter().size_hint(), (0, Some(11)));
860 assert_eq!(m.iter().rev().size_hint(), (0, Some(11)));
861 assert_eq!(m.iter_mut().size_hint(), (0, Some(11)));
862 assert_eq!(m.iter_mut().rev().size_hint(), (0, Some(11)));
866 fn test_mut_iterator() {
867 let mut m = VecMap::new();
869 assert!(m.insert(0, 1i).is_none());
870 assert!(m.insert(1, 2).is_none());
871 assert!(m.insert(3, 5).is_none());
872 assert!(m.insert(6, 10).is_none());
873 assert!(m.insert(10, 11).is_none());
875 for (k, v) in m.iter_mut() {
879 let mut it = m.iter();
880 assert_eq!(it.next().unwrap(), (0, &1));
881 assert_eq!(it.next().unwrap(), (1, &3));
882 assert_eq!(it.next().unwrap(), (3, &8));
883 assert_eq!(it.next().unwrap(), (6, &16));
884 assert_eq!(it.next().unwrap(), (10, &21));
885 assert!(it.next().is_none());
889 fn test_rev_iterator() {
890 let mut m = VecMap::new();
892 assert!(m.insert(0, 1i).is_none());
893 assert!(m.insert(1, 2).is_none());
894 assert!(m.insert(3, 5).is_none());
895 assert!(m.insert(6, 10).is_none());
896 assert!(m.insert(10, 11).is_none());
898 let mut it = m.iter().rev();
899 assert_eq!(it.next().unwrap(), (10, &11));
900 assert_eq!(it.next().unwrap(), (6, &10));
901 assert_eq!(it.next().unwrap(), (3, &5));
902 assert_eq!(it.next().unwrap(), (1, &2));
903 assert_eq!(it.next().unwrap(), (0, &1));
904 assert!(it.next().is_none());
908 fn test_mut_rev_iterator() {
909 let mut m = VecMap::new();
911 assert!(m.insert(0, 1i).is_none());
912 assert!(m.insert(1, 2).is_none());
913 assert!(m.insert(3, 5).is_none());
914 assert!(m.insert(6, 10).is_none());
915 assert!(m.insert(10, 11).is_none());
917 for (k, v) in m.iter_mut().rev() {
921 let mut it = m.iter();
922 assert_eq!(it.next().unwrap(), (0, &1));
923 assert_eq!(it.next().unwrap(), (1, &3));
924 assert_eq!(it.next().unwrap(), (3, &8));
925 assert_eq!(it.next().unwrap(), (6, &16));
926 assert_eq!(it.next().unwrap(), (10, &21));
927 assert!(it.next().is_none());
931 fn test_move_iter() {
932 let mut m = VecMap::new();
934 let mut called = false;
935 for (k, v) in m.into_iter() {
939 assert_eq!(v, box 2i);
947 let mut map = VecMap::new();
948 let empty = VecMap::<int>::new();
953 let map_str = map.to_string();
954 assert!(map_str == "{1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}");
955 assert_eq!(format!("{}", empty), "{}");
960 let mut a = VecMap::new();
966 assert!(a.clone() == a);
971 let mut a = VecMap::new();
972 let mut b = VecMap::new();
975 assert!(a.insert(0, 5i).is_none());
977 assert!(b.insert(0, 4i).is_none());
979 assert!(a.insert(5, 19).is_none());
981 assert!(!b.insert(0, 5).is_none());
983 assert!(b.insert(5, 19).is_none());
987 b = VecMap::with_capacity(1);
993 let mut a = VecMap::new();
994 let mut b = VecMap::new();
996 assert!(!(a < b) && !(b < a));
997 assert!(b.insert(2u, 5i).is_none());
999 assert!(a.insert(2, 7).is_none());
1000 assert!(!(a < b) && b < a);
1001 assert!(b.insert(1, 0).is_none());
1003 assert!(a.insert(0, 6).is_none());
1005 assert!(a.insert(6, 2).is_none());
1006 assert!(a < b && !(b < a));
1011 let mut a = VecMap::new();
1012 let mut b = VecMap::new();
1014 assert!(a <= b && a >= b);
1015 assert!(a.insert(1u, 1i).is_none());
1016 assert!(a > b && a >= b);
1017 assert!(b < a && b <= a);
1018 assert!(b.insert(2, 2).is_none());
1019 assert!(b > a && b >= a);
1020 assert!(a < b && a <= b);
1025 let mut x = VecMap::new();
1026 let mut y = VecMap::new();
1028 assert!(hash(&x) == hash(&y));
1037 assert!(hash(&x) == hash(&y));
1039 x.insert(1000, 'd');
1042 assert!(hash(&x) == hash(&y));
1046 fn test_from_iter() {
1047 let xs: Vec<(uint, char)> = vec![(1u, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')];
1049 let map: VecMap<char> = xs.iter().map(|&x| x).collect();
1051 for &(k, v) in xs.iter() {
1052 assert_eq!(map.get(&k), Some(&v));
1058 let mut map: VecMap<int> = VecMap::new();
1064 assert_eq!(map[3], 4);
1069 fn test_index_nonexistent() {
1070 let mut map: VecMap<int> = VecMap::new();
1084 use bench::{insert_rand_n, insert_seq_n, find_rand_n, find_seq_n};
1087 pub fn insert_rand_100(b: &mut Bencher) {
1088 let mut m : VecMap<uint> = VecMap::new();
1089 insert_rand_n(100, &mut m, b,
1090 |m, i| { m.insert(i, 1); },
1091 |m, i| { m.remove(&i); });
1095 pub fn insert_rand_10_000(b: &mut Bencher) {
1096 let mut m : VecMap<uint> = VecMap::new();
1097 insert_rand_n(10_000, &mut m, b,
1098 |m, i| { m.insert(i, 1); },
1099 |m, i| { m.remove(&i); });
1104 pub fn insert_seq_100(b: &mut Bencher) {
1105 let mut m : VecMap<uint> = VecMap::new();
1106 insert_seq_n(100, &mut m, b,
1107 |m, i| { m.insert(i, 1); },
1108 |m, i| { m.remove(&i); });
1112 pub fn insert_seq_10_000(b: &mut Bencher) {
1113 let mut m : VecMap<uint> = VecMap::new();
1114 insert_seq_n(10_000, &mut m, b,
1115 |m, i| { m.insert(i, 1); },
1116 |m, i| { m.remove(&i); });
1121 pub fn find_rand_100(b: &mut Bencher) {
1122 let mut m : VecMap<uint> = VecMap::new();
1123 find_rand_n(100, &mut m, b,
1124 |m, i| { m.insert(i, 1); },
1125 |m, i| { m.get(&i); });
1129 pub fn find_rand_10_000(b: &mut Bencher) {
1130 let mut m : VecMap<uint> = VecMap::new();
1131 find_rand_n(10_000, &mut m, b,
1132 |m, i| { m.insert(i, 1); },
1133 |m, i| { m.get(&i); });
1138 pub fn find_seq_100(b: &mut Bencher) {
1139 let mut m : VecMap<uint> = VecMap::new();
1140 find_seq_n(100, &mut m, b,
1141 |m, i| { m.insert(i, 1); },
1142 |m, i| { m.get(&i); });
1146 pub fn find_seq_10_000(b: &mut Bencher) {
1147 let mut m : VecMap<uint> = VecMap::new();
1148 find_seq_n(10_000, &mut m, b,
1149 |m, i| { m.insert(i, 1); },
1150 |m, i| { m.get(&i); });