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, Hasher};
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 + Hasher, 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 of 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 of 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 of 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 of 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 of
268 /// the keys, 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 /// let vec: Vec<(uint, &str)> = map.into_iter().collect();
283 /// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
286 pub fn into_iter(self) -> IntoIter<V> {
287 fn filter<A>((i, v): (uint, Option<A>)) -> Option<(uint, A)> {
290 let filter: fn((uint, Option<V>)) -> Option<(uint, V)> = filter; // coerce to fn ptr
292 IntoIter { iter: self.v.into_iter().enumerate().filter_map(filter) }
295 /// Returns an iterator visiting all key-value pairs in ascending order of
296 /// the keys, emptying (but not consuming) the original `VecMap`.
297 /// The iterator's element type is `(uint, &'r V)`. Keeps the allocated memory for reuse.
302 /// use std::collections::VecMap;
304 /// let mut map = VecMap::new();
305 /// map.insert(1, "a");
306 /// map.insert(3, "c");
307 /// map.insert(2, "b");
309 /// let vec: Vec<(uint, &str)> = map.drain().collect();
311 /// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
313 #[unstable = "matches collection reform specification, waiting for dust to settle"]
314 pub fn drain<'a>(&'a mut self) -> Drain<'a, V> {
315 fn filter<A>((i, v): (uint, Option<A>)) -> Option<(uint, A)> {
318 let filter: fn((uint, Option<V>)) -> Option<(uint, V)> = filter; // coerce to fn ptr
320 Drain { iter: self.v.drain().enumerate().filter_map(filter) }
323 /// Return the number of elements in the map.
328 /// use std::collections::VecMap;
330 /// let mut a = VecMap::new();
331 /// assert_eq!(a.len(), 0);
332 /// a.insert(1, "a");
333 /// assert_eq!(a.len(), 1);
336 pub fn len(&self) -> uint {
337 self.v.iter().filter(|elt| elt.is_some()).count()
340 /// Return true if the map contains no elements.
345 /// use std::collections::VecMap;
347 /// let mut a = VecMap::new();
348 /// assert!(a.is_empty());
349 /// a.insert(1, "a");
350 /// assert!(!a.is_empty());
353 pub fn is_empty(&self) -> bool {
354 self.v.iter().all(|elt| elt.is_none())
357 /// Clears the map, removing all key-value pairs.
362 /// use std::collections::VecMap;
364 /// let mut a = VecMap::new();
365 /// a.insert(1, "a");
367 /// assert!(a.is_empty());
370 pub fn clear(&mut self) { self.v.clear() }
372 /// Returns a reference to the value corresponding to the key.
377 /// use std::collections::VecMap;
379 /// let mut map = VecMap::new();
380 /// map.insert(1, "a");
381 /// assert_eq!(map.get(&1), Some(&"a"));
382 /// assert_eq!(map.get(&2), None);
385 pub fn get(&self, key: &uint) -> Option<&V> {
386 if *key < self.v.len() {
388 Some(ref value) => Some(value),
396 /// Returns true if the map contains a value for the specified key.
401 /// use std::collections::VecMap;
403 /// let mut map = VecMap::new();
404 /// map.insert(1, "a");
405 /// assert_eq!(map.contains_key(&1), true);
406 /// assert_eq!(map.contains_key(&2), false);
410 pub fn contains_key(&self, key: &uint) -> bool {
411 self.get(key).is_some()
414 /// Returns a mutable reference to the value corresponding to the key.
419 /// use std::collections::VecMap;
421 /// let mut map = VecMap::new();
422 /// map.insert(1, "a");
423 /// match map.get_mut(&1) {
424 /// Some(x) => *x = "b",
427 /// assert_eq!(map[1], "b");
430 pub fn get_mut(&mut self, key: &uint) -> Option<&mut V> {
431 if *key < self.v.len() {
432 match *(&mut self.v[*key]) {
433 Some(ref mut value) => Some(value),
441 /// Inserts a key-value pair from the map. If the key already had a value
442 /// present in the map, that value is returned. Otherwise, `None` is returned.
447 /// use std::collections::VecMap;
449 /// let mut map = VecMap::new();
450 /// assert_eq!(map.insert(37, "a"), None);
451 /// assert_eq!(map.is_empty(), false);
453 /// map.insert(37, "b");
454 /// assert_eq!(map.insert(37, "c"), Some("b"));
455 /// assert_eq!(map[37], "c");
458 pub fn insert(&mut self, key: uint, value: V) -> Option<V> {
459 let len = self.v.len();
461 self.v.extend(range(0, key - len + 1).map(|_| None));
463 replace(&mut self.v[key], Some(value))
466 /// Removes a key from the map, returning the value at the key if the key
467 /// was previously in the map.
472 /// use std::collections::VecMap;
474 /// let mut map = VecMap::new();
475 /// map.insert(1, "a");
476 /// assert_eq!(map.remove(&1), Some("a"));
477 /// assert_eq!(map.remove(&1), None);
480 pub fn remove(&mut self, key: &uint) -> Option<V> {
481 if *key >= self.v.len() {
484 let result = &mut self.v[*key];
490 impl<V: PartialEq> PartialEq for VecMap<V> {
491 fn eq(&self, other: &VecMap<V>) -> bool {
492 iter::order::eq(self.iter(), other.iter())
497 impl<V: Eq> Eq for VecMap<V> {}
500 impl<V: PartialOrd> PartialOrd for VecMap<V> {
502 fn partial_cmp(&self, other: &VecMap<V>) -> Option<Ordering> {
503 iter::order::partial_cmp(self.iter(), other.iter())
508 impl<V: Ord> Ord for VecMap<V> {
510 fn cmp(&self, other: &VecMap<V>) -> Ordering {
511 iter::order::cmp(self.iter(), other.iter())
516 impl<V: fmt::Show> fmt::Show for VecMap<V> {
517 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
518 try!(write!(f, "VecMap {{"));
520 for (i, (k, v)) in self.iter().enumerate() {
521 if i != 0 { try!(write!(f, ", ")); }
522 try!(write!(f, "{}: {:?}", k, *v));
530 impl<V> FromIterator<(uint, V)> for VecMap<V> {
531 fn from_iter<Iter: Iterator<Item=(uint, V)>>(iter: Iter) -> VecMap<V> {
532 let mut map = VecMap::new();
539 impl<V> Extend<(uint, V)> for VecMap<V> {
540 fn extend<Iter: Iterator<Item=(uint, V)>>(&mut self, mut iter: Iter) {
547 impl<V> Index<uint> for VecMap<V> {
551 fn index<'a>(&'a self, i: &uint) -> &'a V {
552 self.get(i).expect("key not present")
557 impl<V> IndexMut<uint> for VecMap<V> {
561 fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut V {
562 self.get_mut(i).expect("key not present")
566 macro_rules! iterator {
567 (impl $name:ident -> $elem:ty, $($getter:ident),+) => {
569 impl<'a, V> Iterator for $name<'a, V> {
573 fn next(&mut self) -> Option<$elem> {
574 while self.front < self.back {
575 match self.iter.next() {
577 match elem$(. $getter ())+ {
579 let index = self.front;
581 return Some((index, x));
594 fn size_hint(&self) -> (uint, Option<uint>) {
595 (0, Some(self.back - self.front))
601 macro_rules! double_ended_iterator {
602 (impl $name:ident -> $elem:ty, $($getter:ident),+) => {
604 impl<'a, V> DoubleEndedIterator for $name<'a, V> {
606 fn next_back(&mut self) -> Option<$elem> {
607 while self.front < self.back {
608 match self.iter.next_back() {
610 match elem$(. $getter ())+ {
613 return Some((self.back, x));
628 /// An iterator over the key-value pairs of a map.
630 pub struct Iter<'a, V:'a> {
633 iter: slice::Iter<'a, Option<V>>
636 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
637 impl<'a, V> Clone for Iter<'a, V> {
638 fn clone(&self) -> Iter<'a, V> {
642 iter: self.iter.clone()
647 iterator! { impl Iter -> (uint, &'a V), as_ref }
648 double_ended_iterator! { impl Iter -> (uint, &'a V), as_ref }
650 /// An iterator over the key-value pairs of a map, with the
651 /// values being mutable.
653 pub struct IterMut<'a, V:'a> {
656 iter: slice::IterMut<'a, Option<V>>
659 iterator! { impl IterMut -> (uint, &'a mut V), as_mut }
660 double_ended_iterator! { impl IterMut -> (uint, &'a mut V), as_mut }
662 /// An iterator over the keys of a map.
664 pub struct Keys<'a, V: 'a> {
665 iter: Map<(uint, &'a V), uint, Iter<'a, V>, fn((uint, &'a V)) -> uint>
668 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
669 impl<'a, V> Clone for Keys<'a, V> {
670 fn clone(&self) -> Keys<'a, V> {
672 iter: self.iter.clone()
677 /// An iterator over the values of a map.
679 pub struct Values<'a, V: 'a> {
680 iter: Map<(uint, &'a V), &'a V, Iter<'a, V>, fn((uint, &'a V)) -> &'a V>
683 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
684 impl<'a, V> Clone for Values<'a, V> {
685 fn clone(&self) -> Values<'a, V> {
687 iter: self.iter.clone()
692 /// A consuming iterator over the key-value pairs of a map.
694 pub struct IntoIter<V> {
698 Enumerate<vec::IntoIter<Option<V>>>,
699 fn((uint, Option<V>)) -> Option<(uint, V)>>
703 pub struct Drain<'a, V> {
707 Enumerate<vec::Drain<'a, Option<V>>>,
708 fn((uint, Option<V>)) -> Option<(uint, V)>>
712 impl<'a, V> Iterator for Drain<'a, V> {
713 type Item = (uint, V);
715 fn next(&mut self) -> Option<(uint, V)> { self.iter.next() }
716 fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
720 impl<'a, V> DoubleEndedIterator for Drain<'a, V> {
721 fn next_back(&mut self) -> Option<(uint, V)> { self.iter.next_back() }
725 impl<'a, V> Iterator for Keys<'a, V> {
728 fn next(&mut self) -> Option<uint> { self.iter.next() }
729 fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
732 impl<'a, V> DoubleEndedIterator for Keys<'a, V> {
733 fn next_back(&mut self) -> Option<uint> { self.iter.next_back() }
737 impl<'a, V> Iterator for Values<'a, V> {
740 fn next(&mut self) -> Option<(&'a V)> { self.iter.next() }
741 fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
744 impl<'a, V> DoubleEndedIterator for Values<'a, V> {
745 fn next_back(&mut self) -> Option<(&'a V)> { self.iter.next_back() }
749 impl<V> Iterator for IntoIter<V> {
750 type Item = (uint, V);
752 fn next(&mut self) -> Option<(uint, V)> { self.iter.next() }
753 fn size_hint(&self) -> (uint, Option<uint>) { self.iter.size_hint() }
756 impl<V> DoubleEndedIterator for IntoIter<V> {
757 fn next_back(&mut self) -> Option<(uint, V)> { self.iter.next_back() }
763 use core::hash::{hash, SipHasher};
769 let mut m = VecMap::new();
770 assert!(m.insert(1, 12i).is_none());
771 assert!(m.insert(2, 8).is_none());
772 assert!(m.insert(5, 14).is_none());
774 match m.get_mut(&5) {
775 None => panic!(), Some(x) => *x = new
777 assert_eq!(m.get(&5), Some(&new));
782 let mut map = VecMap::new();
783 assert_eq!(map.len(), 0);
784 assert!(map.is_empty());
785 assert!(map.insert(5, 20i).is_none());
786 assert_eq!(map.len(), 1);
787 assert!(!map.is_empty());
788 assert!(map.insert(11, 12).is_none());
789 assert_eq!(map.len(), 2);
790 assert!(!map.is_empty());
791 assert!(map.insert(14, 22).is_none());
792 assert_eq!(map.len(), 3);
793 assert!(!map.is_empty());
798 let mut map = VecMap::new();
799 assert!(map.insert(5, 20i).is_none());
800 assert!(map.insert(11, 12).is_none());
801 assert!(map.insert(14, 22).is_none());
803 assert!(map.is_empty());
804 assert!(map.get(&5).is_none());
805 assert!(map.get(&11).is_none());
806 assert!(map.get(&14).is_none());
811 let mut m = VecMap::new();
812 assert_eq!(m.insert(1, 2i), None);
813 assert_eq!(m.insert(1, 3i), Some(2));
814 assert_eq!(m.insert(1, 4i), Some(3));
819 let mut m = VecMap::new();
821 assert_eq!(m.remove(&1), Some(2));
822 assert_eq!(m.remove(&1), None);
827 let mut map = VecMap::new();
831 let keys = map.keys().collect::<Vec<uint>>();
832 assert_eq!(keys.len(), 3);
833 assert!(keys.contains(&1));
834 assert!(keys.contains(&2));
835 assert!(keys.contains(&3));
840 let mut map = VecMap::new();
844 let values = map.values().map(|&v| v).collect::<Vec<char>>();
845 assert_eq!(values.len(), 3);
846 assert!(values.contains(&'a'));
847 assert!(values.contains(&'b'));
848 assert!(values.contains(&'c'));
853 let mut m = VecMap::new();
855 assert!(m.insert(0, 1i).is_none());
856 assert!(m.insert(1, 2).is_none());
857 assert!(m.insert(3, 5).is_none());
858 assert!(m.insert(6, 10).is_none());
859 assert!(m.insert(10, 11).is_none());
861 let mut it = m.iter();
862 assert_eq!(it.size_hint(), (0, Some(11)));
863 assert_eq!(it.next().unwrap(), (0, &1));
864 assert_eq!(it.size_hint(), (0, Some(10)));
865 assert_eq!(it.next().unwrap(), (1, &2));
866 assert_eq!(it.size_hint(), (0, Some(9)));
867 assert_eq!(it.next().unwrap(), (3, &5));
868 assert_eq!(it.size_hint(), (0, Some(7)));
869 assert_eq!(it.next().unwrap(), (6, &10));
870 assert_eq!(it.size_hint(), (0, Some(4)));
871 assert_eq!(it.next().unwrap(), (10, &11));
872 assert_eq!(it.size_hint(), (0, Some(0)));
873 assert!(it.next().is_none());
877 fn test_iterator_size_hints() {
878 let mut m = VecMap::new();
880 assert!(m.insert(0, 1i).is_none());
881 assert!(m.insert(1, 2).is_none());
882 assert!(m.insert(3, 5).is_none());
883 assert!(m.insert(6, 10).is_none());
884 assert!(m.insert(10, 11).is_none());
886 assert_eq!(m.iter().size_hint(), (0, Some(11)));
887 assert_eq!(m.iter().rev().size_hint(), (0, Some(11)));
888 assert_eq!(m.iter_mut().size_hint(), (0, Some(11)));
889 assert_eq!(m.iter_mut().rev().size_hint(), (0, Some(11)));
893 fn test_mut_iterator() {
894 let mut m = VecMap::new();
896 assert!(m.insert(0, 1i).is_none());
897 assert!(m.insert(1, 2).is_none());
898 assert!(m.insert(3, 5).is_none());
899 assert!(m.insert(6, 10).is_none());
900 assert!(m.insert(10, 11).is_none());
902 for (k, v) in m.iter_mut() {
906 let mut it = m.iter();
907 assert_eq!(it.next().unwrap(), (0, &1));
908 assert_eq!(it.next().unwrap(), (1, &3));
909 assert_eq!(it.next().unwrap(), (3, &8));
910 assert_eq!(it.next().unwrap(), (6, &16));
911 assert_eq!(it.next().unwrap(), (10, &21));
912 assert!(it.next().is_none());
916 fn test_rev_iterator() {
917 let mut m = VecMap::new();
919 assert!(m.insert(0, 1i).is_none());
920 assert!(m.insert(1, 2).is_none());
921 assert!(m.insert(3, 5).is_none());
922 assert!(m.insert(6, 10).is_none());
923 assert!(m.insert(10, 11).is_none());
925 let mut it = m.iter().rev();
926 assert_eq!(it.next().unwrap(), (10, &11));
927 assert_eq!(it.next().unwrap(), (6, &10));
928 assert_eq!(it.next().unwrap(), (3, &5));
929 assert_eq!(it.next().unwrap(), (1, &2));
930 assert_eq!(it.next().unwrap(), (0, &1));
931 assert!(it.next().is_none());
935 fn test_mut_rev_iterator() {
936 let mut m = VecMap::new();
938 assert!(m.insert(0, 1i).is_none());
939 assert!(m.insert(1, 2).is_none());
940 assert!(m.insert(3, 5).is_none());
941 assert!(m.insert(6, 10).is_none());
942 assert!(m.insert(10, 11).is_none());
944 for (k, v) in m.iter_mut().rev() {
948 let mut it = m.iter();
949 assert_eq!(it.next().unwrap(), (0, &1));
950 assert_eq!(it.next().unwrap(), (1, &3));
951 assert_eq!(it.next().unwrap(), (3, &8));
952 assert_eq!(it.next().unwrap(), (6, &16));
953 assert_eq!(it.next().unwrap(), (10, &21));
954 assert!(it.next().is_none());
958 fn test_move_iter() {
959 let mut m = VecMap::new();
961 let mut called = false;
962 for (k, v) in m.into_iter() {
966 assert_eq!(v, box 2i);
974 let mut map = VecMap::new();
975 let empty = VecMap::<int>::new();
980 let map_str = format!("{:?}", map);
981 assert!(map_str == "VecMap {1: 2i, 3: 4i}" || map_str == "{3: 4i, 1: 2i}");
982 assert_eq!(format!("{:?}", empty), "VecMap {}");
987 let mut a = VecMap::new();
993 assert!(a.clone() == a);
998 let mut a = VecMap::new();
999 let mut b = VecMap::new();
1002 assert!(a.insert(0, 5i).is_none());
1004 assert!(b.insert(0, 4i).is_none());
1006 assert!(a.insert(5, 19).is_none());
1008 assert!(!b.insert(0, 5).is_none());
1010 assert!(b.insert(5, 19).is_none());
1014 b = VecMap::with_capacity(1);
1020 let mut a = VecMap::new();
1021 let mut b = VecMap::new();
1023 assert!(!(a < b) && !(b < a));
1024 assert!(b.insert(2u, 5i).is_none());
1026 assert!(a.insert(2, 7).is_none());
1027 assert!(!(a < b) && b < a);
1028 assert!(b.insert(1, 0).is_none());
1030 assert!(a.insert(0, 6).is_none());
1032 assert!(a.insert(6, 2).is_none());
1033 assert!(a < b && !(b < a));
1038 let mut a = VecMap::new();
1039 let mut b = VecMap::new();
1041 assert!(a <= b && a >= b);
1042 assert!(a.insert(1u, 1i).is_none());
1043 assert!(a > b && a >= b);
1044 assert!(b < a && b <= a);
1045 assert!(b.insert(2, 2).is_none());
1046 assert!(b > a && b >= a);
1047 assert!(a < b && a <= b);
1052 let mut x = VecMap::new();
1053 let mut y = VecMap::new();
1055 assert!(hash::<_, SipHasher>(&x) == hash::<_, SipHasher>(&y));
1064 assert!(hash::<_, SipHasher>(&x) == hash::<_, SipHasher>(&y));
1066 x.insert(1000, 'd');
1069 assert!(hash::<_, SipHasher>(&x) == hash::<_, SipHasher>(&y));
1073 fn test_from_iter() {
1074 let xs: Vec<(uint, char)> = vec![(1u, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')];
1076 let map: VecMap<char> = xs.iter().map(|&x| x).collect();
1078 for &(k, v) in xs.iter() {
1079 assert_eq!(map.get(&k), Some(&v));
1085 let mut map: VecMap<int> = VecMap::new();
1091 assert_eq!(map[3], 4);
1096 fn test_index_nonexistent() {
1097 let mut map: VecMap<int> = VecMap::new();
1111 use bench::{insert_rand_n, insert_seq_n, find_rand_n, find_seq_n};
1114 pub fn insert_rand_100(b: &mut Bencher) {
1115 let mut m : VecMap<uint> = VecMap::new();
1116 insert_rand_n(100, &mut m, b,
1117 |m, i| { m.insert(i, 1); },
1118 |m, i| { m.remove(&i); });
1122 pub fn insert_rand_10_000(b: &mut Bencher) {
1123 let mut m : VecMap<uint> = VecMap::new();
1124 insert_rand_n(10_000, &mut m, b,
1125 |m, i| { m.insert(i, 1); },
1126 |m, i| { m.remove(&i); });
1131 pub fn insert_seq_100(b: &mut Bencher) {
1132 let mut m : VecMap<uint> = VecMap::new();
1133 insert_seq_n(100, &mut m, b,
1134 |m, i| { m.insert(i, 1); },
1135 |m, i| { m.remove(&i); });
1139 pub fn insert_seq_10_000(b: &mut Bencher) {
1140 let mut m : VecMap<uint> = VecMap::new();
1141 insert_seq_n(10_000, &mut m, b,
1142 |m, i| { m.insert(i, 1); },
1143 |m, i| { m.remove(&i); });
1148 pub fn find_rand_100(b: &mut Bencher) {
1149 let mut m : VecMap<uint> = VecMap::new();
1150 find_rand_n(100, &mut m, b,
1151 |m, i| { m.insert(i, 1); },
1152 |m, i| { m.get(&i); });
1156 pub fn find_rand_10_000(b: &mut Bencher) {
1157 let mut m : VecMap<uint> = VecMap::new();
1158 find_rand_n(10_000, &mut m, b,
1159 |m, i| { m.insert(i, 1); },
1160 |m, i| { m.get(&i); });
1165 pub fn find_seq_100(b: &mut Bencher) {
1166 let mut m : VecMap<uint> = VecMap::new();
1167 find_seq_n(100, &mut m, b,
1168 |m, i| { m.insert(i, 1); },
1169 |m, i| { m.get(&i); });
1173 pub fn find_seq_10_000(b: &mut Bencher) {
1174 let mut m : VecMap<uint> = VecMap::new();
1175 find_seq_n(10_000, &mut m, b,
1176 |m, i| { m.insert(i, 1); },
1177 |m, i| { m.get(&i); });