2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/*!
|
|
|
|
* A simple map based on a vector for small integer keys. Space requirements
|
|
|
|
* are O(highest integer key).
|
|
|
|
*/
|
2012-10-04 21:58:31 -05:00
|
|
|
#[forbid(deprecated_mode)];
|
2012-09-02 20:05:19 -05:00
|
|
|
|
2013-01-31 15:43:49 -06:00
|
|
|
use core::container::{Container, Mutable, Map, Set};
|
2012-12-23 16:41:37 -06:00
|
|
|
use core::dvec::DVec;
|
|
|
|
use core::ops;
|
2012-09-04 13:23:53 -05:00
|
|
|
use core::option::{Some, None};
|
2012-12-27 20:24:18 -06:00
|
|
|
use core::option;
|
2013-01-08 21:37:25 -06:00
|
|
|
use core::prelude::*;
|
2011-06-03 18:14:29 -05:00
|
|
|
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (#2347): Should not be @; there's a bug somewhere in rustc that
|
|
|
|
// requires this to be.
|
2013-01-22 09:01:17 -06:00
|
|
|
struct SmallIntMap_<T> {
|
2013-01-22 10:44:24 -06:00
|
|
|
v: DVec<Option<T>>,
|
|
|
|
}
|
2012-07-11 17:00:40 -05:00
|
|
|
|
2013-01-22 09:01:17 -06:00
|
|
|
pub enum SmallIntMap<T> {
|
2012-09-04 18:04:10 -05:00
|
|
|
SmallIntMap_(@SmallIntMap_<T>)
|
2012-07-11 17:00:40 -05:00
|
|
|
}
|
2011-06-03 18:14:29 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Create a smallintmap
|
2012-10-01 16:01:42 -05:00
|
|
|
pub fn mk<T: Copy>() -> SmallIntMap<T> {
|
2012-08-27 16:22:25 -05:00
|
|
|
let v = DVec();
|
2013-01-22 10:44:24 -06:00
|
|
|
SmallIntMap_(@SmallIntMap_ { v: v } )
|
2011-06-03 18:14:29 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Add a value to the map. If the map already contains a value for
|
|
|
|
* the specified key then the original value is replaced.
|
|
|
|
*/
|
2012-06-14 13:38:45 -05:00
|
|
|
#[inline(always)]
|
2012-10-03 14:21:48 -05:00
|
|
|
pub fn insert<T: Copy>(self: SmallIntMap<T>, key: uint, val: T) {
|
2012-08-22 19:24:52 -05:00
|
|
|
//io::println(fmt!("%?", key));
|
2012-09-28 00:20:47 -05:00
|
|
|
self.v.grow_set_elt(key, &None, Some(val));
|
2011-06-03 18:14:29 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Get the value for the specified key. If the key does not exist
|
|
|
|
* in the map then returns none
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub pure fn find<T: Copy>(self: SmallIntMap<T>, key: uint) -> Option<T> {
|
2012-08-01 19:30:05 -05:00
|
|
|
if key < self.v.len() { return self.v.get_elt(key); }
|
2012-08-20 14:23:37 -05:00
|
|
|
return None::<T>;
|
2011-06-03 18:14:29 -05:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Get the value for the specified key
|
|
|
|
*
|
|
|
|
* # Failure
|
|
|
|
*
|
|
|
|
* If the key does not exist in the map
|
|
|
|
*/
|
2012-10-01 16:01:42 -05:00
|
|
|
pub pure fn get<T: Copy>(self: SmallIntMap<T>, key: uint) -> T {
|
2012-08-06 14:34:08 -05:00
|
|
|
match find(self, key) {
|
2012-08-20 14:23:37 -05:00
|
|
|
None => {
|
2012-08-22 19:24:52 -05:00
|
|
|
error!("smallintmap::get(): key not present");
|
2013-02-11 21:26:38 -06:00
|
|
|
fail!();
|
2012-08-03 21:59:04 -05:00
|
|
|
}
|
2013-02-15 01:30:30 -06:00
|
|
|
Some(v) => return v
|
2011-06-03 18:14:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Returns true if the map contains a value for the specified key
|
2012-11-17 10:41:47 -06:00
|
|
|
pub pure fn contains_key<T: Copy>(self: SmallIntMap<T>, key: uint) -> bool {
|
2012-09-21 21:37:57 -05:00
|
|
|
return !find(self, key).is_none();
|
2011-07-27 07:19:39 -05:00
|
|
|
}
|
2011-06-19 20:02:37 -05:00
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<V> Container for SmallIntMap<V> {
|
2013-01-31 15:43:49 -06:00
|
|
|
/// Return the number of elements in the map
|
|
|
|
pure fn len(&self) -> uint {
|
2012-03-14 13:03:56 -05:00
|
|
|
let mut sz = 0u;
|
2012-06-30 18:19:07 -05:00
|
|
|
for self.v.each |item| {
|
2012-09-19 18:55:01 -05:00
|
|
|
match *item {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(_) => sz += 1u,
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
|
|
|
}
|
2012-01-09 09:24:53 -06:00
|
|
|
}
|
|
|
|
sz
|
|
|
|
}
|
2013-01-31 15:43:49 -06:00
|
|
|
|
|
|
|
/// Return true if the map contains no elements
|
|
|
|
pure fn is_empty(&self) -> bool { self.len() == 0 }
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<V> Mutable for SmallIntMap<V> {
|
2013-01-31 16:28:37 -06:00
|
|
|
fn clear(&mut self) { self.v.set(~[]) }
|
|
|
|
}
|
|
|
|
|
2013-01-31 15:43:49 -06:00
|
|
|
/// Implements the map::map interface for smallintmap
|
2013-01-31 15:48:14 -06:00
|
|
|
impl<V: Copy> SmallIntMap<V> {
|
2012-06-14 13:38:45 -05:00
|
|
|
#[inline(always)]
|
2012-10-03 14:21:48 -05:00
|
|
|
fn insert(key: uint, value: V) -> bool {
|
2012-01-09 09:24:53 -06:00
|
|
|
let exists = contains_key(self, key);
|
|
|
|
insert(self, key, value);
|
2012-08-01 19:30:05 -05:00
|
|
|
return !exists;
|
2012-01-09 09:24:53 -06:00
|
|
|
}
|
2012-10-03 14:21:48 -05:00
|
|
|
fn remove(key: uint) -> bool {
|
2012-08-02 17:42:56 -05:00
|
|
|
if key >= self.v.len() {
|
2012-08-21 17:55:17 -05:00
|
|
|
return false;
|
2012-08-02 17:42:56 -05:00
|
|
|
}
|
2012-05-12 21:09:09 -05:00
|
|
|
let old = self.v.get_elt(key);
|
2012-08-20 14:23:37 -05:00
|
|
|
self.v.set_elt(key, None);
|
2012-08-21 17:55:17 -05:00
|
|
|
old.is_some()
|
2012-01-09 09:24:53 -06:00
|
|
|
}
|
2012-11-17 10:41:47 -06:00
|
|
|
pure fn contains_key(key: uint) -> bool {
|
2012-01-09 09:24:53 -06:00
|
|
|
contains_key(self, key)
|
|
|
|
}
|
2012-11-17 10:41:47 -06:00
|
|
|
pure fn contains_key_ref(key: &uint) -> bool {
|
2012-08-02 17:42:56 -05:00
|
|
|
contains_key(self, *key)
|
|
|
|
}
|
2012-11-17 10:41:47 -06:00
|
|
|
pure fn get(key: uint) -> V { get(self, key) }
|
2012-10-03 14:21:48 -05:00
|
|
|
pure fn find(key: uint) -> Option<V> { find(self, key) }
|
2012-09-18 23:41:37 -05:00
|
|
|
|
2012-11-25 14:45:18 -06:00
|
|
|
fn update_with_key(key: uint, val: V, ff: fn(uint, V, V) -> V) -> bool {
|
2012-11-11 03:01:37 -06:00
|
|
|
match self.find(key) {
|
|
|
|
None => return self.insert(key, val),
|
|
|
|
Some(copy orig) => return self.insert(key, ff(key, orig, val)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-25 14:45:18 -06:00
|
|
|
fn update(key: uint, newval: V, ff: fn(V, V) -> V) -> bool {
|
|
|
|
return self.update_with_key(key, newval, |_k, v, v1| ff(v,v1));
|
2012-11-21 01:33:31 -06:00
|
|
|
}
|
|
|
|
|
2012-10-04 21:58:31 -05:00
|
|
|
pure fn each(it: fn(key: uint, value: V) -> bool) {
|
2012-09-18 23:41:37 -05:00
|
|
|
self.each_ref(|k, v| it(*k, *v))
|
2012-01-09 09:24:53 -06:00
|
|
|
}
|
2012-10-03 14:21:48 -05:00
|
|
|
pure fn each_key(it: fn(key: uint) -> bool) {
|
2012-09-18 23:41:37 -05:00
|
|
|
self.each_ref(|k, _v| it(*k))
|
2012-08-02 17:42:56 -05:00
|
|
|
}
|
2012-10-03 14:21:48 -05:00
|
|
|
pure fn each_value(it: fn(value: V) -> bool) {
|
2012-09-18 23:41:37 -05:00
|
|
|
self.each_ref(|_k, v| it(*v))
|
2012-08-02 17:42:56 -05:00
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
pure fn each_ref(it: fn(key: &uint, value: &V) -> bool) {
|
2012-04-06 13:01:43 -05:00
|
|
|
let mut idx = 0u, l = self.v.len();
|
|
|
|
while idx < l {
|
2012-08-06 14:34:08 -05:00
|
|
|
match self.v.get_elt(idx) {
|
2012-09-28 02:22:18 -05:00
|
|
|
Some(ref elt) => if !it(&idx, elt) { break },
|
2012-08-20 14:23:37 -05:00
|
|
|
None => ()
|
2012-08-02 17:42:56 -05:00
|
|
|
}
|
2012-01-09 09:24:53 -06:00
|
|
|
idx += 1u;
|
|
|
|
}
|
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
pure fn each_key_ref(blk: fn(key: &uint) -> bool) {
|
2012-08-02 17:42:56 -05:00
|
|
|
self.each_ref(|k, _v| blk(k))
|
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
pure fn each_value_ref(blk: fn(value: &V) -> bool) {
|
2012-08-02 17:42:56 -05:00
|
|
|
self.each_ref(|_k, v| blk(v))
|
2012-01-09 09:24:53 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<V: Copy> ops::Index<uint, V> for SmallIntMap<V> {
|
2012-11-28 15:51:50 -06:00
|
|
|
pure fn index(&self, key: uint) -> V {
|
|
|
|
unsafe {
|
|
|
|
get(*self, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-27 16:51:19 -05:00
|
|
|
|
2012-11-21 01:33:31 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2013-01-31 17:18:07 -06:00
|
|
|
use super::{mk, SmallIntMap};
|
2013-01-08 21:37:25 -06:00
|
|
|
|
2012-12-27 20:24:18 -06:00
|
|
|
use core::option::None;
|
2012-11-21 01:33:31 -06:00
|
|
|
|
2013-01-31 15:43:49 -06:00
|
|
|
#[test]
|
|
|
|
fn test_len() {
|
|
|
|
let mut map = mk();
|
|
|
|
assert map.len() == 0;
|
|
|
|
assert map.is_empty();
|
|
|
|
map.insert(5, 20);
|
|
|
|
assert map.len() == 1;
|
|
|
|
assert !map.is_empty();
|
|
|
|
map.insert(11, 12);
|
|
|
|
assert map.len() == 2;
|
|
|
|
assert !map.is_empty();
|
|
|
|
map.insert(14, 22);
|
|
|
|
assert map.len() == 3;
|
|
|
|
assert !map.is_empty();
|
|
|
|
}
|
|
|
|
|
2013-01-31 16:28:37 -06:00
|
|
|
#[test]
|
|
|
|
fn test_clear() {
|
|
|
|
let mut map = mk();
|
|
|
|
map.insert(5, 20);
|
|
|
|
map.insert(11, 12);
|
|
|
|
map.insert(14, 22);
|
|
|
|
map.clear();
|
|
|
|
assert map.is_empty();
|
|
|
|
assert map.find(5).is_none();
|
|
|
|
assert map.find(11).is_none();
|
|
|
|
assert map.find(14).is_none();
|
|
|
|
}
|
|
|
|
|
2012-11-21 01:33:31 -06:00
|
|
|
#[test]
|
|
|
|
fn test_insert_with_key() {
|
|
|
|
let map: SmallIntMap<uint> = mk();
|
|
|
|
|
|
|
|
// given a new key, initialize it with this new count, given
|
|
|
|
// given an existing key, add more to its count
|
|
|
|
fn addMoreToCount(_k: uint, v0: uint, v1: uint) -> uint {
|
|
|
|
v0 + v1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn addMoreToCount_simple(v0: uint, v1: uint) -> uint {
|
|
|
|
v0 + v1
|
|
|
|
}
|
|
|
|
|
|
|
|
// count integers
|
2012-11-25 14:45:18 -06:00
|
|
|
map.update(3, 1, addMoreToCount_simple);
|
|
|
|
map.update_with_key(9, 1, addMoreToCount);
|
|
|
|
map.update(3, 7, addMoreToCount_simple);
|
|
|
|
map.update_with_key(5, 3, addMoreToCount);
|
|
|
|
map.update_with_key(3, 2, addMoreToCount);
|
2012-11-21 01:33:31 -06:00
|
|
|
|
|
|
|
// check the total counts
|
2013-01-31 16:22:55 -06:00
|
|
|
assert map.find(3).get() == 10;
|
|
|
|
assert map.find(5).get() == 3;
|
|
|
|
assert map.find(9).get() == 1;
|
2012-11-21 01:33:31 -06:00
|
|
|
|
|
|
|
// sadly, no sevens were counted
|
|
|
|
assert None == map.find(7);
|
|
|
|
}
|
|
|
|
}
|