1 // Copyright 2012 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.
12 * A simple map based on a vector for small integer keys. Space requirements
13 * are O(highest integer key).
16 #[allow(missing_doc)];
18 use std::iterator::{Iterator, IteratorUtil, Enumerate, FilterMap, Invert};
20 use std::util::replace;
21 use std::vec::{VecIterator, VecMutIterator};
25 pub struct SmallIntMap<T> {
29 impl<V> Container for SmallIntMap<V> {
30 /// Return the number of elements in the map
31 fn len(&self) -> uint {
33 for i in range(0u, self.v.len()) {
43 impl<V> Mutable for SmallIntMap<V> {
44 /// Clear the map, removing all key-value pairs.
45 fn clear(&mut self) { self.v.clear() }
48 impl<V> Map<uint, V> for SmallIntMap<V> {
49 /// Return a reference to the value corresponding to the key
50 fn find<'a>(&'a self, key: &uint) -> Option<&'a V> {
51 if *key < self.v.len() {
53 Some(ref value) => Some(value),
62 impl<V> MutableMap<uint, V> for SmallIntMap<V> {
63 /// Return a mutable reference to the value corresponding to the key
64 fn find_mut<'a>(&'a mut self, key: &uint) -> Option<&'a mut V> {
65 if *key < self.v.len() {
67 Some(ref mut value) => Some(value),
75 /// Insert a key-value pair into the map. An existing value for a
76 /// key is replaced by the new value. Return true if the key did
77 /// not already exist in the map.
78 fn insert(&mut self, key: uint, value: V) -> bool {
79 let exists = self.contains_key(&key);
80 let len = self.v.len();
82 self.v.grow_fn(key - len + 1, |_| None);
84 self.v[key] = Some(value);
88 /// Remove a key-value pair from the map. Return true if the key
89 /// was present in the map, otherwise false.
90 fn remove(&mut self, key: &uint) -> bool {
91 self.pop(key).is_some()
94 /// Insert a key-value pair from the map. If the key already had a value
95 /// present in the map, that value is returned. Otherwise None is returned.
96 fn swap(&mut self, key: uint, value: V) -> Option<V> {
97 match self.find_mut(&key) {
98 Some(loc) => { return Some(replace(loc, value)); }
101 self.insert(key, value);
105 /// Removes a key from the map, returning the value at the key if the key
106 /// was previously in the map.
107 fn pop(&mut self, key: &uint) -> Option<V> {
108 if *key >= self.v.len() {
115 impl<V> SmallIntMap<V> {
116 /// Create an empty SmallIntMap
117 pub fn new() -> SmallIntMap<V> { SmallIntMap{v: ~[]} }
119 /// Visit all key-value pairs in order
120 pub fn each<'a>(&'a self, it: &fn(&uint, &'a V) -> bool) -> bool {
121 for i in range(0u, self.v.len()) {
123 Some(ref elt) => if !it(&i, elt) { return false; },
130 /// Visit all keys in order
131 pub fn each_key(&self, blk: &fn(key: &uint) -> bool) -> bool {
132 self.each(|k, _| blk(k))
135 /// Visit all values in order
136 pub fn each_value<'a>(&'a self, blk: &fn(value: &'a V) -> bool) -> bool {
137 self.each(|_, v| blk(v))
140 /// Iterate over the map and mutate the contained values
141 pub fn mutate_values(&mut self, it: &fn(&uint, &mut V) -> bool) -> bool {
142 for i in range(0, self.v.len()) {
144 Some(ref mut elt) => if !it(&i, elt) { return false; },
151 /// Visit all key-value pairs in reverse order
152 pub fn each_reverse<'a>(&'a self, it: &fn(uint, &'a V) -> bool) -> bool {
153 do uint::range_rev(self.v.len(), 0) |i| {
155 Some(ref elt) => it(i, elt),
161 pub fn get<'a>(&'a self, key: &uint) -> &'a V {
162 self.find(key).expect("key not present")
165 /// An iterator visiting all key-value pairs in ascending order by the keys.
166 /// Iterator element type is (uint, &'r V)
167 pub fn iter<'r>(&'r self) -> SmallIntMapIterator<'r, V> {
168 SmallIntMapIterator {
175 /// An iterator visiting all key-value pairs in ascending order by the keys,
176 /// with mutable references to the values
177 /// Iterator element type is (uint, &'r mut V)
178 pub fn mut_iter<'r>(&'r mut self) -> SmallIntMapMutIterator<'r, V> {
179 SmallIntMapMutIterator {
182 iter: self.v.mut_iter()
186 /// An iterator visiting all key-value pairs in descending order by the keys.
187 /// Iterator element type is (uint, &'r V)
188 pub fn rev_iter<'r>(&'r self) -> SmallIntMapRevIterator<'r, V> {
192 /// An iterator visiting all key-value pairs in descending order by the keys,
193 /// with mutable references to the values
194 /// Iterator element type is (uint, &'r mut V)
195 pub fn mut_rev_iter<'r>(&'r mut self) -> SmallIntMapMutRevIterator <'r, V> {
196 self.mut_iter().invert()
199 /// Empties the hash map, moving all values into the specified closure
200 pub fn consume(&mut self)
201 -> FilterMap<(uint, Option<V>), (uint, V),
202 Enumerate<vec::ConsumeIterator<Option<V>>>>
204 let values = replace(&mut self.v, ~[]);
205 values.consume_iter().enumerate().filter_map(|(i, v)| {
206 v.map_move(|v| (i, v))
211 impl<V:Clone> SmallIntMap<V> {
212 pub fn update_with_key(&mut self, key: uint, val: V,
213 ff: &fn(uint, V, V) -> V) -> bool {
214 let new_val = match self.find(&key) {
216 Some(orig) => ff(key, (*orig).clone(), val)
218 self.insert(key, new_val)
221 pub fn update(&mut self, key: uint, newval: V, ff: &fn(V, V) -> V)
223 self.update_with_key(key, newval, |_k, v, v1| ff(v,v1))
228 macro_rules! iterator {
229 (impl $name:ident -> $elem:ty, $getter:ident) => {
230 impl<'self, T> Iterator<$elem> for $name<'self, T> {
232 fn next(&mut self) -> Option<$elem> {
233 while self.front < self.back {
234 match self.iter.next() {
237 let index = self.front;
239 return Some((index, elem. $getter ()));
250 fn size_hint(&self) -> (uint, Option<uint>) {
251 (0, Some(self.back - self.front))
257 macro_rules! double_ended_iterator {
258 (impl $name:ident -> $elem:ty, $getter:ident) => {
259 impl<'self, T> DoubleEndedIterator<$elem> for $name<'self, T> {
261 fn next_back(&mut self) -> Option<$elem> {
262 while self.front < self.back {
263 match self.iter.next_back() {
267 return Some((self.back, elem. $getter ()));
280 pub struct SmallIntMapIterator<'self, T> {
283 priv iter: VecIterator<'self, Option<T>>
286 iterator!(impl SmallIntMapIterator -> (uint, &'self T), get_ref)
287 double_ended_iterator!(impl SmallIntMapIterator -> (uint, &'self T), get_ref)
288 pub type SmallIntMapRevIterator<'self, T> = Invert<SmallIntMapIterator<'self, T>>;
290 pub struct SmallIntMapMutIterator<'self, T> {
293 priv iter: VecMutIterator<'self, Option<T>>
296 iterator!(impl SmallIntMapMutIterator -> (uint, &'self mut T), get_mut_ref)
297 double_ended_iterator!(impl SmallIntMapMutIterator -> (uint, &'self mut T), get_mut_ref)
298 pub type SmallIntMapMutRevIterator<'self, T> = Invert<SmallIntMapMutIterator<'self, T>>;
303 use super::SmallIntMap;
307 let mut m = SmallIntMap::new();
308 assert!(m.insert(1, 12));
309 assert!(m.insert(2, 8));
310 assert!(m.insert(5, 14));
312 match m.find_mut(&5) {
313 None => fail!(), Some(x) => *x = new
315 assert_eq!(m.find(&5), Some(&new));
320 let mut map = SmallIntMap::new();
321 assert_eq!(map.len(), 0);
322 assert!(map.is_empty());
323 assert!(map.insert(5, 20));
324 assert_eq!(map.len(), 1);
325 assert!(!map.is_empty());
326 assert!(map.insert(11, 12));
327 assert_eq!(map.len(), 2);
328 assert!(!map.is_empty());
329 assert!(map.insert(14, 22));
330 assert_eq!(map.len(), 3);
331 assert!(!map.is_empty());
336 let mut map = SmallIntMap::new();
337 assert!(map.insert(5, 20));
338 assert!(map.insert(11, 12));
339 assert!(map.insert(14, 22));
341 assert!(map.is_empty());
342 assert!(map.find(&5).is_none());
343 assert!(map.find(&11).is_none());
344 assert!(map.find(&14).is_none());
348 fn test_insert_with_key() {
349 let mut map = SmallIntMap::new();
351 // given a new key, initialize it with this new count, given
352 // given an existing key, add more to its count
353 fn addMoreToCount(_k: uint, v0: uint, v1: uint) -> uint {
357 fn addMoreToCount_simple(v0: uint, v1: uint) -> uint {
362 map.update(3, 1, addMoreToCount_simple);
363 map.update_with_key(9, 1, addMoreToCount);
364 map.update(3, 7, addMoreToCount_simple);
365 map.update_with_key(5, 3, addMoreToCount);
366 map.update_with_key(3, 2, addMoreToCount);
368 // check the total counts
369 assert_eq!(map.find(&3).unwrap(), &10);
370 assert_eq!(map.find(&5).unwrap(), &3);
371 assert_eq!(map.find(&9).unwrap(), &1);
373 // sadly, no sevens were counted
374 assert!(map.find(&7).is_none());
379 let mut m = SmallIntMap::new();
380 assert_eq!(m.swap(1, 2), None);
381 assert_eq!(m.swap(1, 3), Some(2));
382 assert_eq!(m.swap(1, 4), Some(3));
387 let mut m = SmallIntMap::new();
389 assert_eq!(m.pop(&1), Some(2));
390 assert_eq!(m.pop(&1), None);
395 let mut m = SmallIntMap::new();
397 assert!(m.insert(0, 1));
398 assert!(m.insert(1, 2));
399 assert!(m.insert(3, 5));
400 assert!(m.insert(6, 10));
401 assert!(m.insert(10, 11));
403 let mut it = m.iter();
404 assert_eq!(it.size_hint(), (0, Some(11)));
405 assert_eq!(it.next().unwrap(), (0, &1));
406 assert_eq!(it.size_hint(), (0, Some(10)));
407 assert_eq!(it.next().unwrap(), (1, &2));
408 assert_eq!(it.size_hint(), (0, Some(9)));
409 assert_eq!(it.next().unwrap(), (3, &5));
410 assert_eq!(it.size_hint(), (0, Some(7)));
411 assert_eq!(it.next().unwrap(), (6, &10));
412 assert_eq!(it.size_hint(), (0, Some(4)));
413 assert_eq!(it.next().unwrap(), (10, &11));
414 assert_eq!(it.size_hint(), (0, Some(0)));
415 assert!(it.next().is_none());
419 fn test_iterator_size_hints() {
420 let mut m = SmallIntMap::new();
422 assert!(m.insert(0, 1));
423 assert!(m.insert(1, 2));
424 assert!(m.insert(3, 5));
425 assert!(m.insert(6, 10));
426 assert!(m.insert(10, 11));
428 assert_eq!(m.iter().size_hint(), (0, Some(11)));
429 assert_eq!(m.rev_iter().size_hint(), (0, Some(11)));
430 assert_eq!(m.mut_iter().size_hint(), (0, Some(11)));
431 assert_eq!(m.mut_rev_iter().size_hint(), (0, Some(11)));
435 fn test_mut_iterator() {
436 let mut m = SmallIntMap::new();
438 assert!(m.insert(0, 1));
439 assert!(m.insert(1, 2));
440 assert!(m.insert(3, 5));
441 assert!(m.insert(6, 10));
442 assert!(m.insert(10, 11));
444 for (k, v) in m.mut_iter() {
448 let mut it = m.iter();
449 assert_eq!(it.next().unwrap(), (0, &1));
450 assert_eq!(it.next().unwrap(), (1, &3));
451 assert_eq!(it.next().unwrap(), (3, &8));
452 assert_eq!(it.next().unwrap(), (6, &16));
453 assert_eq!(it.next().unwrap(), (10, &21));
454 assert!(it.next().is_none());
458 fn test_rev_iterator() {
459 let mut m = SmallIntMap::new();
461 assert!(m.insert(0, 1));
462 assert!(m.insert(1, 2));
463 assert!(m.insert(3, 5));
464 assert!(m.insert(6, 10));
465 assert!(m.insert(10, 11));
467 let mut it = m.rev_iter();
468 assert_eq!(it.next().unwrap(), (10, &11));
469 assert_eq!(it.next().unwrap(), (6, &10));
470 assert_eq!(it.next().unwrap(), (3, &5));
471 assert_eq!(it.next().unwrap(), (1, &2));
472 assert_eq!(it.next().unwrap(), (0, &1));
473 assert!(it.next().is_none());
477 fn test_mut_rev_iterator() {
478 let mut m = SmallIntMap::new();
480 assert!(m.insert(0, 1));
481 assert!(m.insert(1, 2));
482 assert!(m.insert(3, 5));
483 assert!(m.insert(6, 10));
484 assert!(m.insert(10, 11));
486 for (k, v) in m.mut_rev_iter() {
490 let mut it = m.iter();
491 assert_eq!(it.next().unwrap(), (0, &1));
492 assert_eq!(it.next().unwrap(), (1, &3));
493 assert_eq!(it.next().unwrap(), (3, &8));
494 assert_eq!(it.next().unwrap(), (6, &16));
495 assert_eq!(it.next().unwrap(), (10, &21));
496 assert!(it.next().is_none());
501 let mut m = SmallIntMap::new();
503 let mut called = false;
504 for (k, v) in m.consume() {
519 use test::BenchHarness;
520 use container::bench::*;
524 pub fn insert_rand_100(bh: &mut BenchHarness) {
525 let mut m : SmallIntMap<uint> = SmallIntMap::new();
526 insert_rand_n(100, &mut m, bh);
530 pub fn insert_rand_10_000(bh: &mut BenchHarness) {
531 let mut m : SmallIntMap<uint> = SmallIntMap::new();
532 insert_rand_n(10_000, &mut m, bh);
537 pub fn insert_seq_100(bh: &mut BenchHarness) {
538 let mut m : SmallIntMap<uint> = SmallIntMap::new();
539 insert_seq_n(100, &mut m, bh);
543 pub fn insert_seq_10_000(bh: &mut BenchHarness) {
544 let mut m : SmallIntMap<uint> = SmallIntMap::new();
545 insert_seq_n(10_000, &mut m, bh);
550 pub fn find_rand_100(bh: &mut BenchHarness) {
551 let mut m : SmallIntMap<uint> = SmallIntMap::new();
552 find_rand_n(100, &mut m, bh);
556 pub fn find_rand_10_000(bh: &mut BenchHarness) {
557 let mut m : SmallIntMap<uint> = SmallIntMap::new();
558 find_rand_n(10_000, &mut m, bh);
563 pub fn find_seq_100(bh: &mut BenchHarness) {
564 let mut m : SmallIntMap<uint> = SmallIntMap::new();
565 find_seq_n(100, &mut m, bh);
569 pub fn find_seq_10_000(bh: &mut BenchHarness) {
570 let mut m : SmallIntMap<uint> = SmallIntMap::new();
571 find_seq_n(10_000, &mut m, bh);