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::default::Default;
21 use core::iter::{Enumerate, FilterMap};
22 use core::mem::replace;
29 // FIXME(conventions): capacity management???
31 /// A map optimized for small integer keys.
36 /// use std::collections::VecMap;
38 /// let mut months = VecMap::new();
39 /// months.insert(1, "Jan");
40 /// months.insert(2, "Feb");
41 /// months.insert(3, "Mar");
43 /// if !months.contains_key(&12) {
44 /// println!("The end is near!");
47 /// assert_eq!(months.get(&1), Some(&"Jan"));
49 /// match months.get_mut(&3) {
50 /// Some(value) => *value = "Venus",
54 /// assert_eq!(months.get(&3), Some(&"Venus"));
56 /// // Print out all months
57 /// for (key, value) in months.iter() {
58 /// println!("month {} is {}", key, value);
62 /// assert!(months.is_empty());
64 #[deriving(PartialEq, Eq)]
65 pub struct VecMap<T> {
69 impl<V> Default for VecMap<V> {
71 fn default() -> VecMap<V> { VecMap::new() }
74 impl<V:Clone> Clone for VecMap<V> {
76 fn clone(&self) -> VecMap<V> {
77 VecMap { v: self.v.clone() }
81 fn clone_from(&mut self, source: &VecMap<V>) {
82 self.v.clone_from(&source.v);
86 impl <S: hash::Writer, T: Hash<S>> Hash<S> for VecMap<T> {
87 fn hash(&self, state: &mut S) {
93 /// Creates an empty `VecMap`.
98 /// use std::collections::VecMap;
99 /// let mut map: VecMap<&str> = VecMap::new();
101 #[unstable = "matches collection reform specification, waiting for dust to settle"]
102 pub fn new() -> VecMap<V> { VecMap{v: vec!()} }
104 /// Creates an empty `VecMap` with space for at least `capacity`
105 /// elements before resizing.
110 /// use std::collections::VecMap;
111 /// let mut map: VecMap<&str> = VecMap::with_capacity(10);
113 #[unstable = "matches collection reform specification, waiting for dust to settle"]
114 pub fn with_capacity(capacity: uint) -> VecMap<V> {
115 VecMap { v: Vec::with_capacity(capacity) }
118 /// Returns the number of elements the `VecMap` can hold without
124 /// use std::collections::VecMap;
125 /// let map: VecMap<String> = VecMap::with_capacity(10);
126 /// assert!(map.capacity() >= 10);
129 #[unstable = "matches collection reform specification, waiting for dust to settle"]
130 pub fn capacity(&self) -> uint {
134 /// Returns an iterator visiting all keys in ascending order by the keys.
135 /// The iterator's element type is `uint`.
136 #[unstable = "matches collection reform specification, waiting for dust to settle"]
137 pub fn keys<'r>(&'r self) -> Keys<'r, V> {
138 self.iter().map(|(k, _v)| k)
141 /// Returns an iterator visiting all values in ascending order by the keys.
142 /// The iterator's element type is `&'r V`.
143 #[unstable = "matches collection reform specification, waiting for dust to settle"]
144 pub fn values<'r>(&'r self) -> Values<'r, V> {
145 self.iter().map(|(_k, v)| v)
148 /// Returns an iterator visiting all key-value pairs in ascending order by the keys.
149 /// The iterator's element type is `(uint, &'r V)`.
154 /// use std::collections::VecMap;
156 /// let mut map = VecMap::new();
157 /// map.insert(1, "a");
158 /// map.insert(3, "c");
159 /// map.insert(2, "b");
161 /// // Print `1: a` then `2: b` then `3: c`
162 /// for (key, value) in map.iter() {
163 /// println!("{}: {}", key, value);
166 #[unstable = "matches collection reform specification, waiting for dust to settle"]
167 pub fn iter<'r>(&'r self) -> Entries<'r, V> {
175 /// Returns an iterator visiting all key-value pairs in ascending order by the keys,
176 /// with mutable references to the values.
177 /// The iterator's element type is `(uint, &'r mut V)`.
182 /// use std::collections::VecMap;
184 /// let mut map = VecMap::new();
185 /// map.insert(1, "a");
186 /// map.insert(2, "b");
187 /// map.insert(3, "c");
189 /// for (key, value) in map.iter_mut() {
193 /// for (key, value) in map.iter() {
194 /// assert_eq!(value, &"x");
197 #[unstable = "matches collection reform specification, waiting for dust to settle"]
198 pub fn iter_mut<'r>(&'r mut self) -> MutEntries<'r, V> {
202 iter: self.v.iter_mut()
206 /// Returns an iterator visiting all key-value pairs in ascending order by
207 /// the keys, emptying (but not consuming) the original `VecMap`.
208 /// The iterator's element type is `(uint, &'r V)`.
213 /// use std::collections::VecMap;
215 /// let mut map = VecMap::new();
216 /// map.insert(1, "a");
217 /// map.insert(3, "c");
218 /// map.insert(2, "b");
220 /// // Not possible with .iter()
221 /// let vec: Vec<(uint, &str)> = map.into_iter().collect();
223 /// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]);
225 #[unstable = "matches collection reform specification, waiting for dust to settle"]
226 pub fn into_iter(&mut self)
227 -> FilterMap<(uint, Option<V>), (uint, V),
228 Enumerate<vec::MoveItems<Option<V>>>>
230 let values = replace(&mut self.v, vec!());
231 values.into_iter().enumerate().filter_map(|(i, v)| {
236 /// Return the number of elements in the map.
241 /// use std::collections::VecMap;
243 /// let mut a = VecMap::new();
244 /// assert_eq!(a.len(), 0);
245 /// a.insert(1, "a");
246 /// assert_eq!(a.len(), 1);
248 #[unstable = "matches collection reform specification, waiting for dust to settle"]
249 pub fn len(&self) -> uint {
250 self.v.iter().filter(|elt| elt.is_some()).count()
253 /// Return true if the map contains no elements.
258 /// use std::collections::VecMap;
260 /// let mut a = VecMap::new();
261 /// assert!(a.is_empty());
262 /// a.insert(1, "a");
263 /// assert!(!a.is_empty());
265 #[unstable = "matches collection reform specification, waiting for dust to settle"]
266 pub fn is_empty(&self) -> bool {
267 self.v.iter().all(|elt| elt.is_none())
270 /// Clears the map, removing all key-value pairs.
275 /// use std::collections::VecMap;
277 /// let mut a = VecMap::new();
278 /// a.insert(1, "a");
280 /// assert!(a.is_empty());
282 #[unstable = "matches collection reform specification, waiting for dust to settle"]
283 pub fn clear(&mut self) { self.v.clear() }
285 /// Deprecated: Renamed to `get`.
286 #[deprecated = "Renamed to `get`"]
287 pub fn find(&self, key: &uint) -> Option<&V> {
291 /// Returns a reference to the value corresponding to the key.
296 /// use std::collections::VecMap;
298 /// let mut map = VecMap::new();
299 /// map.insert(1, "a");
300 /// assert_eq!(map.get(&1), Some(&"a"));
301 /// assert_eq!(map.get(&2), None);
303 #[unstable = "matches collection reform specification, waiting for dust to settle"]
304 pub fn get(&self, key: &uint) -> Option<&V> {
305 if *key < self.v.len() {
307 Some(ref value) => Some(value),
315 /// Returns true if the map contains a value for the specified key.
320 /// use std::collections::VecMap;
322 /// let mut map = VecMap::new();
323 /// map.insert(1, "a");
324 /// assert_eq!(map.contains_key(&1), true);
325 /// assert_eq!(map.contains_key(&2), false);
328 #[unstable = "matches collection reform specification, waiting for dust to settle"]
329 pub fn contains_key(&self, key: &uint) -> bool {
330 self.get(key).is_some()
333 /// Deprecated: Renamed to `get_mut`.
334 #[deprecated = "Renamed to `get_mut`"]
335 pub fn find_mut(&mut self, key: &uint) -> Option<&mut V> {
339 /// Returns a mutable reference to the value corresponding to the key.
344 /// use std::collections::VecMap;
346 /// let mut map = VecMap::new();
347 /// map.insert(1, "a");
348 /// match map.get_mut(&1) {
349 /// Some(x) => *x = "b",
352 /// assert_eq!(map[1], "b");
354 #[unstable = "matches collection reform specification, waiting for dust to settle"]
355 pub fn get_mut(&mut self, key: &uint) -> Option<&mut V> {
356 if *key < self.v.len() {
357 match *(&mut self.v[*key]) {
358 Some(ref mut value) => Some(value),
366 /// Deprecated: Renamed to `insert`.
367 #[deprecated = "Renamed to `insert`"]
368 pub fn swap(&mut self, key: uint, value: V) -> Option<V> {
369 self.insert(key, value)
372 /// Inserts a key-value pair from the map. If the key already had a value
373 /// present in the map, that value is returned. Otherwise, `None` is returned.
378 /// use std::collections::VecMap;
380 /// let mut map = VecMap::new();
381 /// assert_eq!(map.insert(37, "a"), None);
382 /// assert_eq!(map.is_empty(), false);
384 /// map.insert(37, "b");
385 /// assert_eq!(map.insert(37, "c"), Some("b"));
386 /// assert_eq!(map[37], "c");
388 #[unstable = "matches collection reform specification, waiting for dust to settle"]
389 pub fn insert(&mut self, key: uint, value: V) -> Option<V> {
390 let len = self.v.len();
392 self.v.grow_fn(key - len + 1, |_| None);
394 replace(&mut self.v[key], Some(value))
397 /// Deprecated: Renamed to `remove`.
398 #[deprecated = "Renamed to `remove`"]
399 pub fn pop(&mut self, key: &uint) -> Option<V> {
403 /// Removes a key from the map, returning the value at the key if the key
404 /// was previously in the map.
409 /// use std::collections::VecMap;
411 /// let mut map = VecMap::new();
412 /// map.insert(1, "a");
413 /// assert_eq!(map.remove(&1), Some("a"));
414 /// assert_eq!(map.remove(&1), None);
416 #[unstable = "matches collection reform specification, waiting for dust to settle"]
417 pub fn remove(&mut self, key: &uint) -> Option<V> {
418 if *key >= self.v.len() {
425 impl<V:Clone> VecMap<V> {
426 /// Updates a value in the map. If the key already exists in the map,
427 /// modifies the value with `ff` taking `oldval, newval`.
428 /// Otherwise, sets the value to `newval`.
429 /// Returns `true` if the key did not already exist in the map.
434 /// use std::collections::VecMap;
436 /// let mut map = VecMap::new();
438 /// // Key does not exist, will do a simple insert
439 /// assert!(map.update(1, vec![1i, 2], |mut old, new| { old.extend(new.into_iter()); old }));
440 /// assert_eq!(map[1], vec![1i, 2]);
442 /// // Key exists, update the value
443 /// assert!(!map.update(1, vec![3i, 4], |mut old, new| { old.extend(new.into_iter()); old }));
444 /// assert_eq!(map[1], vec![1i, 2, 3, 4]);
446 pub fn update(&mut self, key: uint, newval: V, ff: |V, V| -> V) -> bool {
447 self.update_with_key(key, newval, |_k, v, v1| ff(v,v1))
450 /// Updates a value in the map. If the key already exists in the map,
451 /// modifies the value with `ff` taking `key, oldval, newval`.
452 /// Otherwise, sets the value to `newval`.
453 /// Returns `true` if the key did not already exist in the map.
458 /// use std::collections::VecMap;
460 /// let mut map = VecMap::new();
462 /// // Key does not exist, will do a simple insert
463 /// assert!(map.update_with_key(7, 10, |key, old, new| (old + new) % key));
464 /// assert_eq!(map[7], 10);
466 /// // Key exists, update the value
467 /// assert!(!map.update_with_key(7, 20, |key, old, new| (old + new) % key));
468 /// assert_eq!(map[7], 2);
470 pub fn update_with_key(&mut self,
473 ff: |uint, V, V| -> V)
475 let new_val = match self.get(&key) {
477 Some(orig) => ff(key, (*orig).clone(), val)
479 self.insert(key, new_val).is_none()
483 impl<V: PartialOrd> PartialOrd for VecMap<V> {
485 fn partial_cmp(&self, other: &VecMap<V>) -> Option<Ordering> {
486 iter::order::partial_cmp(self.iter(), other.iter())
490 impl<V: Ord> Ord for VecMap<V> {
492 fn cmp(&self, other: &VecMap<V>) -> Ordering {
493 iter::order::cmp(self.iter(), other.iter())
497 impl<V: fmt::Show> fmt::Show for VecMap<V> {
498 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
499 try!(write!(f, "{{"));
501 for (i, (k, v)) in self.iter().enumerate() {
502 if i != 0 { try!(write!(f, ", ")); }
503 try!(write!(f, "{}: {}", k, *v));
510 impl<V> FromIterator<(uint, V)> for VecMap<V> {
511 fn from_iter<Iter: Iterator<(uint, V)>>(iter: Iter) -> VecMap<V> {
512 let mut map = VecMap::new();
518 impl<V> Extend<(uint, V)> for VecMap<V> {
519 fn extend<Iter: Iterator<(uint, V)>>(&mut self, mut iter: Iter) {
526 impl<V> Index<uint, V> for VecMap<V> {
528 fn index<'a>(&'a self, i: &uint) -> &'a V {
529 self.get(i).expect("key not present")
533 impl<V> IndexMut<uint, V> for VecMap<V> {
535 fn index_mut<'a>(&'a mut self, i: &uint) -> &'a mut V {
536 self.get_mut(i).expect("key not present")
540 macro_rules! iterator {
541 (impl $name:ident -> $elem:ty, $($getter:ident),+) => {
542 impl<'a, T> Iterator<$elem> for $name<'a, T> {
544 fn next(&mut self) -> Option<$elem> {
545 while self.front < self.back {
546 match self.iter.next() {
549 let index = self.front;
551 return Some((index, elem $(. $getter ())+));
562 fn size_hint(&self) -> (uint, Option<uint>) {
563 (0, Some(self.back - self.front))
569 macro_rules! double_ended_iterator {
570 (impl $name:ident -> $elem:ty, $($getter:ident),+) => {
571 impl<'a, T> DoubleEndedIterator<$elem> for $name<'a, T> {
573 fn next_back(&mut self) -> Option<$elem> {
574 while self.front < self.back {
575 match self.iter.next_back() {
579 return Some((self.back, elem$(. $getter ())+));
592 /// Forward iterator over a map.
593 pub struct Entries<'a, T:'a> {
596 iter: slice::Items<'a, Option<T>>
599 iterator!(impl Entries -> (uint, &'a T), as_ref, unwrap)
600 double_ended_iterator!(impl Entries -> (uint, &'a T), as_ref, unwrap)
602 /// Forward iterator over the key-value pairs of a map, with the
603 /// values being mutable.
604 pub struct MutEntries<'a, T:'a> {
607 iter: slice::MutItems<'a, Option<T>>
610 iterator!(impl MutEntries -> (uint, &'a mut T), as_mut, unwrap)
611 double_ended_iterator!(impl MutEntries -> (uint, &'a mut T), as_mut, unwrap)
613 /// Forward iterator over the keys of a map
614 pub type Keys<'a, T> =
615 iter::Map<'static, (uint, &'a T), uint, Entries<'a, T>>;
617 /// Forward iterator over the values of a map
618 pub type Values<'a, T> =
619 iter::Map<'static, (uint, &'a T), &'a T, Entries<'a, T>>;
631 let mut m = VecMap::new();
632 assert!(m.insert(1, 12i).is_none());
633 assert!(m.insert(2, 8).is_none());
634 assert!(m.insert(5, 14).is_none());
636 match m.get_mut(&5) {
637 None => panic!(), Some(x) => *x = new
639 assert_eq!(m.get(&5), Some(&new));
644 let mut map = VecMap::new();
645 assert_eq!(map.len(), 0);
646 assert!(map.is_empty());
647 assert!(map.insert(5, 20i).is_none());
648 assert_eq!(map.len(), 1);
649 assert!(!map.is_empty());
650 assert!(map.insert(11, 12).is_none());
651 assert_eq!(map.len(), 2);
652 assert!(!map.is_empty());
653 assert!(map.insert(14, 22).is_none());
654 assert_eq!(map.len(), 3);
655 assert!(!map.is_empty());
660 let mut map = VecMap::new();
661 assert!(map.insert(5, 20i).is_none());
662 assert!(map.insert(11, 12).is_none());
663 assert!(map.insert(14, 22).is_none());
665 assert!(map.is_empty());
666 assert!(map.get(&5).is_none());
667 assert!(map.get(&11).is_none());
668 assert!(map.get(&14).is_none());
672 fn test_insert_with_key() {
673 let mut map = VecMap::new();
675 // given a new key, initialize it with this new count,
676 // given an existing key, add more to its count
677 fn add_more_to_count(_k: uint, v0: uint, v1: uint) -> uint {
681 fn add_more_to_count_simple(v0: uint, v1: uint) -> uint {
686 map.update(3, 1, add_more_to_count_simple);
687 map.update_with_key(9, 1, add_more_to_count);
688 map.update(3, 7, add_more_to_count_simple);
689 map.update_with_key(5, 3, add_more_to_count);
690 map.update_with_key(3, 2, add_more_to_count);
692 // check the total counts
693 assert_eq!(map.get(&3).unwrap(), &10);
694 assert_eq!(map.get(&5).unwrap(), &3);
695 assert_eq!(map.get(&9).unwrap(), &1);
697 // sadly, no sevens were counted
698 assert!(map.get(&7).is_none());
703 let mut m = VecMap::new();
704 assert_eq!(m.insert(1, 2i), None);
705 assert_eq!(m.insert(1, 3i), Some(2));
706 assert_eq!(m.insert(1, 4i), Some(3));
711 let mut m = VecMap::new();
713 assert_eq!(m.remove(&1), Some(2));
714 assert_eq!(m.remove(&1), None);
719 let mut map = VecMap::new();
723 let keys = map.keys().collect::<Vec<uint>>();
724 assert_eq!(keys.len(), 3);
725 assert!(keys.contains(&1));
726 assert!(keys.contains(&2));
727 assert!(keys.contains(&3));
732 let mut map = VecMap::new();
736 let values = map.values().map(|&v| v).collect::<Vec<char>>();
737 assert_eq!(values.len(), 3);
738 assert!(values.contains(&'a'));
739 assert!(values.contains(&'b'));
740 assert!(values.contains(&'c'));
745 let mut m = VecMap::new();
747 assert!(m.insert(0, 1i).is_none());
748 assert!(m.insert(1, 2).is_none());
749 assert!(m.insert(3, 5).is_none());
750 assert!(m.insert(6, 10).is_none());
751 assert!(m.insert(10, 11).is_none());
753 let mut it = m.iter();
754 assert_eq!(it.size_hint(), (0, Some(11)));
755 assert_eq!(it.next().unwrap(), (0, &1));
756 assert_eq!(it.size_hint(), (0, Some(10)));
757 assert_eq!(it.next().unwrap(), (1, &2));
758 assert_eq!(it.size_hint(), (0, Some(9)));
759 assert_eq!(it.next().unwrap(), (3, &5));
760 assert_eq!(it.size_hint(), (0, Some(7)));
761 assert_eq!(it.next().unwrap(), (6, &10));
762 assert_eq!(it.size_hint(), (0, Some(4)));
763 assert_eq!(it.next().unwrap(), (10, &11));
764 assert_eq!(it.size_hint(), (0, Some(0)));
765 assert!(it.next().is_none());
769 fn test_iterator_size_hints() {
770 let mut m = VecMap::new();
772 assert!(m.insert(0, 1i).is_none());
773 assert!(m.insert(1, 2).is_none());
774 assert!(m.insert(3, 5).is_none());
775 assert!(m.insert(6, 10).is_none());
776 assert!(m.insert(10, 11).is_none());
778 assert_eq!(m.iter().size_hint(), (0, Some(11)));
779 assert_eq!(m.iter().rev().size_hint(), (0, Some(11)));
780 assert_eq!(m.iter_mut().size_hint(), (0, Some(11)));
781 assert_eq!(m.iter_mut().rev().size_hint(), (0, Some(11)));
785 fn test_mut_iterator() {
786 let mut m = VecMap::new();
788 assert!(m.insert(0, 1i).is_none());
789 assert!(m.insert(1, 2).is_none());
790 assert!(m.insert(3, 5).is_none());
791 assert!(m.insert(6, 10).is_none());
792 assert!(m.insert(10, 11).is_none());
794 for (k, v) in m.iter_mut() {
798 let mut it = m.iter();
799 assert_eq!(it.next().unwrap(), (0, &1));
800 assert_eq!(it.next().unwrap(), (1, &3));
801 assert_eq!(it.next().unwrap(), (3, &8));
802 assert_eq!(it.next().unwrap(), (6, &16));
803 assert_eq!(it.next().unwrap(), (10, &21));
804 assert!(it.next().is_none());
808 fn test_rev_iterator() {
809 let mut m = VecMap::new();
811 assert!(m.insert(0, 1i).is_none());
812 assert!(m.insert(1, 2).is_none());
813 assert!(m.insert(3, 5).is_none());
814 assert!(m.insert(6, 10).is_none());
815 assert!(m.insert(10, 11).is_none());
817 let mut it = m.iter().rev();
818 assert_eq!(it.next().unwrap(), (10, &11));
819 assert_eq!(it.next().unwrap(), (6, &10));
820 assert_eq!(it.next().unwrap(), (3, &5));
821 assert_eq!(it.next().unwrap(), (1, &2));
822 assert_eq!(it.next().unwrap(), (0, &1));
823 assert!(it.next().is_none());
827 fn test_mut_rev_iterator() {
828 let mut m = VecMap::new();
830 assert!(m.insert(0, 1i).is_none());
831 assert!(m.insert(1, 2).is_none());
832 assert!(m.insert(3, 5).is_none());
833 assert!(m.insert(6, 10).is_none());
834 assert!(m.insert(10, 11).is_none());
836 for (k, v) in m.iter_mut().rev() {
840 let mut it = m.iter();
841 assert_eq!(it.next().unwrap(), (0, &1));
842 assert_eq!(it.next().unwrap(), (1, &3));
843 assert_eq!(it.next().unwrap(), (3, &8));
844 assert_eq!(it.next().unwrap(), (6, &16));
845 assert_eq!(it.next().unwrap(), (10, &21));
846 assert!(it.next().is_none());
850 fn test_move_iter() {
851 let mut m = VecMap::new();
853 let mut called = false;
854 for (k, v) in m.into_iter() {
858 assert_eq!(v, box 2i);
866 let mut map = VecMap::new();
867 let empty = VecMap::<int>::new();
872 let map_str = map.to_string();
873 assert!(map_str == "{1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}");
874 assert_eq!(format!("{}", empty), "{}");
879 let mut a = VecMap::new();
885 assert!(a.clone() == a);
890 let mut a = VecMap::new();
891 let mut b = VecMap::new();
894 assert!(a.insert(0, 5i).is_none());
896 assert!(b.insert(0, 4i).is_none());
898 assert!(a.insert(5, 19).is_none());
900 assert!(!b.insert(0, 5).is_none());
902 assert!(b.insert(5, 19).is_none());
908 let mut a = VecMap::new();
909 let mut b = VecMap::new();
911 assert!(!(a < b) && !(b < a));
912 assert!(b.insert(2u, 5i).is_none());
914 assert!(a.insert(2, 7).is_none());
915 assert!(!(a < b) && b < a);
916 assert!(b.insert(1, 0).is_none());
918 assert!(a.insert(0, 6).is_none());
920 assert!(a.insert(6, 2).is_none());
921 assert!(a < b && !(b < a));
926 let mut a = VecMap::new();
927 let mut b = VecMap::new();
929 assert!(a <= b && a >= b);
930 assert!(a.insert(1u, 1i).is_none());
931 assert!(a > b && a >= b);
932 assert!(b < a && b <= a);
933 assert!(b.insert(2, 2).is_none());
934 assert!(b > a && b >= a);
935 assert!(a < b && a <= b);
940 let mut x = VecMap::new();
941 let mut y = VecMap::new();
943 assert!(hash::hash(&x) == hash::hash(&y));
952 assert!(hash::hash(&x) == hash::hash(&y));
956 fn test_from_iter() {
957 let xs: Vec<(uint, char)> = vec![(1u, 'a'), (2, 'b'), (3, 'c'), (4, 'd'), (5, 'e')];
959 let map: VecMap<char> = xs.iter().map(|&x| x).collect();
961 for &(k, v) in xs.iter() {
962 assert_eq!(map.get(&k), Some(&v));
968 let mut map: VecMap<int> = VecMap::new();
974 assert_eq!(map[3], 4);
979 fn test_index_nonexistent() {
980 let mut map: VecMap<int> = VecMap::new();
993 use self::test::Bencher;
995 use bench::{insert_rand_n, insert_seq_n, find_rand_n, find_seq_n};
998 pub fn insert_rand_100(b: &mut Bencher) {
999 let mut m : VecMap<uint> = VecMap::new();
1000 insert_rand_n(100, &mut m, b,
1001 |m, i| { m.insert(i, 1); },
1002 |m, i| { m.remove(&i); });
1006 pub fn insert_rand_10_000(b: &mut Bencher) {
1007 let mut m : VecMap<uint> = VecMap::new();
1008 insert_rand_n(10_000, &mut m, b,
1009 |m, i| { m.insert(i, 1); },
1010 |m, i| { m.remove(&i); });
1015 pub fn insert_seq_100(b: &mut Bencher) {
1016 let mut m : VecMap<uint> = VecMap::new();
1017 insert_seq_n(100, &mut m, b,
1018 |m, i| { m.insert(i, 1); },
1019 |m, i| { m.remove(&i); });
1023 pub fn insert_seq_10_000(b: &mut Bencher) {
1024 let mut m : VecMap<uint> = VecMap::new();
1025 insert_seq_n(10_000, &mut m, b,
1026 |m, i| { m.insert(i, 1); },
1027 |m, i| { m.remove(&i); });
1032 pub fn find_rand_100(b: &mut Bencher) {
1033 let mut m : VecMap<uint> = VecMap::new();
1034 find_rand_n(100, &mut m, b,
1035 |m, i| { m.insert(i, 1); },
1036 |m, i| { m.get(&i); });
1040 pub fn find_rand_10_000(b: &mut Bencher) {
1041 let mut m : VecMap<uint> = VecMap::new();
1042 find_rand_n(10_000, &mut m, b,
1043 |m, i| { m.insert(i, 1); },
1044 |m, i| { m.get(&i); });
1049 pub fn find_seq_100(b: &mut Bencher) {
1050 let mut m : VecMap<uint> = VecMap::new();
1051 find_seq_n(100, &mut m, b,
1052 |m, i| { m.insert(i, 1); },
1053 |m, i| { m.get(&i); });
1057 pub fn find_seq_10_000(b: &mut Bencher) {
1058 let mut m : VecMap<uint> = VecMap::new();
1059 find_seq_n(10_000, &mut m, b,
1060 |m, i| { m.insert(i, 1); },
1061 |m, i| { m.get(&i); });