2013-01-31 17:56:12 -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.
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* A simple map based on a vector for small integer keys. Space requirements
|
|
|
|
* are O(highest integer key).
|
|
|
|
*/
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
#[allow(missing_doc)];
|
|
|
|
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::cmp;
|
|
|
|
use std::container::{Container, Mutable, Map, Set};
|
|
|
|
use std::uint;
|
|
|
|
use std::util::replace;
|
|
|
|
use std::vec;
|
2013-01-31 17:56:12 -06:00
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
#[allow(missing_doc)]
|
2013-01-31 18:07:08 -06:00
|
|
|
pub struct SmallIntMap<T> {
|
|
|
|
priv v: ~[Option<T>],
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<V> Container for SmallIntMap<V> {
|
2013-01-31 17:56:12 -06:00
|
|
|
/// Return the number of elements in the map
|
2013-06-15 01:20:06 -05:00
|
|
|
fn len(&self) -> uint {
|
2013-01-31 18:07:08 -06:00
|
|
|
let mut sz = 0;
|
2013-06-15 01:20:06 -05:00
|
|
|
for uint::range(0, self.v.len()) |i| {
|
2013-03-16 13:11:31 -05:00
|
|
|
match self.v[i] {
|
|
|
|
Some(_) => sz += 1,
|
|
|
|
None => {}
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sz
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if the map contains no elements
|
2013-06-15 01:20:06 -05:00
|
|
|
fn is_empty(&self) -> bool { self.len() == 0 }
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<V> Mutable for SmallIntMap<V> {
|
2013-01-31 18:07:08 -06:00
|
|
|
/// Clear the map, removing all key-value pairs.
|
|
|
|
fn clear(&mut self) { self.v.clear() }
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl<V> Map<uint, V> for SmallIntMap<V> {
|
2013-01-31 18:07:08 -06:00
|
|
|
/// Return true if the map contains a value for the specified key
|
2013-03-21 23:34:30 -05:00
|
|
|
fn contains_key(&self, key: &uint) -> bool {
|
2013-01-31 18:07:08 -06:00
|
|
|
self.find(key).is_some()
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
2013-01-31 18:07:08 -06:00
|
|
|
|
2013-03-24 19:35:23 -05:00
|
|
|
/// Return a reference to the value corresponding to the key
|
2013-04-10 15:14:06 -05:00
|
|
|
fn find<'a>(&'a self, key: &uint) -> Option<&'a V> {
|
|
|
|
if *key < self.v.len() {
|
|
|
|
match self.v[*key] {
|
|
|
|
Some(ref value) => Some(value),
|
|
|
|
None => None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-24 19:40:17 -05:00
|
|
|
/// Return a mutable reference to the value corresponding to the key
|
2013-04-10 15:14:06 -05:00
|
|
|
fn find_mut<'a>(&'a mut self, key: &uint) -> Option<&'a mut V> {
|
|
|
|
if *key < self.v.len() {
|
|
|
|
match self.v[*key] {
|
|
|
|
Some(ref mut value) => Some(value),
|
|
|
|
None => None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-31 18:07:08 -06:00
|
|
|
/// Insert a key-value pair into the map. An existing value for a
|
|
|
|
/// key is replaced by the new value. Return true if the key did
|
|
|
|
/// not already exist in the map.
|
|
|
|
fn insert(&mut self, key: uint, value: V) -> bool {
|
|
|
|
let exists = self.contains_key(&key);
|
|
|
|
let len = self.v.len();
|
|
|
|
if len <= key {
|
|
|
|
vec::grow_fn(&mut self.v, key - len + 1, |_| None);
|
|
|
|
}
|
|
|
|
self.v[key] = Some(value);
|
|
|
|
!exists
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
2013-01-31 18:07:08 -06:00
|
|
|
/// Remove a key-value pair from the map. Return true if the key
|
|
|
|
/// was present in the map, otherwise false.
|
|
|
|
fn remove(&mut self, key: &uint) -> bool {
|
2013-05-04 08:54:58 -05:00
|
|
|
self.pop(key).is_some()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Insert a key-value pair from the map. If the key already had a value
|
|
|
|
/// present in the map, that value is returned. Otherwise None is returned.
|
|
|
|
fn swap(&mut self, key: uint, value: V) -> Option<V> {
|
|
|
|
match self.find_mut(&key) {
|
|
|
|
Some(loc) => { return Some(replace(loc, value)); }
|
|
|
|
None => ()
|
|
|
|
}
|
|
|
|
self.insert(key, value);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Removes a key from the map, returning the value at the key if the key
|
|
|
|
/// was previously in the map.
|
|
|
|
fn pop(&mut self, key: &uint) -> Option<V> {
|
2013-01-31 18:07:08 -06:00
|
|
|
if *key >= self.v.len() {
|
2013-05-04 08:54:58 -05:00
|
|
|
return None;
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
2013-05-04 08:54:58 -05:00
|
|
|
replace(&mut self.v[*key], None)
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
2013-01-31 18:07:08 -06:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl<V> SmallIntMap<V> {
|
2013-01-31 18:07:08 -06:00
|
|
|
/// Create an empty SmallIntMap
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn new() -> SmallIntMap<V> { SmallIntMap{v: ~[]} }
|
2013-01-31 18:07:08 -06:00
|
|
|
|
2013-06-24 16:45:00 -05:00
|
|
|
/// Visit all key-value pairs in order
|
|
|
|
pub fn each<'a>(&'a self, it: &fn(&uint, &'a V) -> bool) -> bool {
|
|
|
|
for uint::range(0, self.v.len()) |i| {
|
|
|
|
match self.v[i] {
|
|
|
|
Some(ref elt) => if !it(&i, elt) { return false; },
|
|
|
|
None => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Visit all keys in order
|
|
|
|
pub fn each_key(&self, blk: &fn(key: &uint) -> bool) -> bool {
|
|
|
|
self.each(|k, _| blk(k))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Visit all values in order
|
|
|
|
pub fn each_value<'a>(&'a self, blk: &fn(value: &'a V) -> bool) -> bool {
|
|
|
|
self.each(|_, v| blk(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Iterate over the map and mutate the contained values
|
|
|
|
pub fn mutate_values(&mut self, it: &fn(&uint, &mut V) -> bool) -> bool {
|
|
|
|
for uint::range(0, self.v.len()) |i| {
|
|
|
|
match self.v[i] {
|
|
|
|
Some(ref mut elt) => if !it(&i, elt) { return false; },
|
|
|
|
None => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-06 10:22:36 -05:00
|
|
|
/// Visit all key-value pairs in reverse order
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn each_reverse<'a>(&'a self, it: &fn(uint, &'a V) -> bool) -> bool {
|
2013-05-02 17:33:27 -05:00
|
|
|
for uint::range_rev(self.v.len(), 0) |i| {
|
|
|
|
match self.v[i - 1] {
|
|
|
|
Some(ref elt) => if !it(i - 1, elt) { return false; },
|
|
|
|
None => ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn get<'a>(&'a self, key: &uint) -> &'a V {
|
2013-04-10 15:14:06 -05:00
|
|
|
self.find(key).expect("key not present")
|
|
|
|
}
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl<V:Copy> SmallIntMap<V> {
|
|
|
|
pub fn update_with_key(&mut self, key: uint, val: V,
|
|
|
|
ff: &fn(uint, V, V) -> V) -> bool {
|
2013-02-09 00:21:45 -06:00
|
|
|
let new_val = match self.find(&key) {
|
|
|
|
None => val,
|
2013-06-15 19:26:59 -05:00
|
|
|
Some(orig) => ff(key, copy *orig, val)
|
2013-02-09 00:21:45 -06:00
|
|
|
};
|
|
|
|
self.insert(key, new_val)
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
2013-01-31 18:07:08 -06:00
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn update(&mut self, key: uint, newval: V, ff: &fn(V, V) -> V)
|
|
|
|
-> bool {
|
2013-01-31 18:07:08 -06:00
|
|
|
self.update_with_key(key, newval, |_k, v, v1| ff(v,v1))
|
|
|
|
}
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
2013-05-28 22:11:41 -05:00
|
|
|
/// A set implemented on top of the SmallIntMap type. This set is always a set
|
|
|
|
/// of integers, and the space requirements are on the order of the highest
|
|
|
|
/// valued integer in the set.
|
2013-05-22 07:01:21 -05:00
|
|
|
pub struct SmallIntSet {
|
|
|
|
priv map: SmallIntMap<()>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Container for SmallIntSet {
|
|
|
|
/// Return the number of elements in the map
|
2013-06-15 01:20:06 -05:00
|
|
|
fn len(&self) -> uint {
|
2013-05-22 07:01:21 -05:00
|
|
|
self.map.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if the map contains no elements
|
2013-06-15 01:20:06 -05:00
|
|
|
fn is_empty(&self) -> bool { self.len() == 0 }
|
2013-05-22 07:01:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Mutable for SmallIntSet {
|
|
|
|
/// Clear the map, removing all key-value pairs.
|
|
|
|
fn clear(&mut self) { self.map.clear() }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Set<uint> for SmallIntSet {
|
|
|
|
/// Return true if the set contains a value
|
|
|
|
fn contains(&self, value: &uint) -> bool { self.map.contains_key(value) }
|
|
|
|
|
|
|
|
/// Add a value to the set. Return true if the value was not already
|
|
|
|
/// present in the set.
|
|
|
|
fn insert(&mut self, value: uint) -> bool { self.map.insert(value, ()) }
|
|
|
|
|
|
|
|
/// Remove a value from the set. Return true if the value was
|
|
|
|
/// present in the set.
|
|
|
|
fn remove(&mut self, value: &uint) -> bool { self.map.remove(value) }
|
|
|
|
|
|
|
|
/// Return true if the set has no elements in common with `other`.
|
|
|
|
/// This is equivalent to checking for an empty uintersection.
|
|
|
|
fn is_disjoint(&self, other: &SmallIntSet) -> bool {
|
2013-06-23 16:57:39 -05:00
|
|
|
for self.each |v| { if other.contains(v) { return false } }
|
|
|
|
true
|
2013-05-22 07:01:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if the set is a subset of another
|
|
|
|
fn is_subset(&self, other: &SmallIntSet) -> bool {
|
2013-06-23 16:57:39 -05:00
|
|
|
for self.each |v| { if !other.contains(v) { return false } }
|
|
|
|
true
|
2013-05-22 07:01:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return true if the set is a superset of another
|
|
|
|
fn is_superset(&self, other: &SmallIntSet) -> bool {
|
|
|
|
other.is_subset(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Visit the values representing the difference
|
|
|
|
fn difference(&self, other: &SmallIntSet, f: &fn(&uint) -> bool) -> bool {
|
|
|
|
self.each(|v| other.contains(v) || f(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Visit the values representing the symmetric difference
|
|
|
|
fn symmetric_difference(&self,
|
|
|
|
other: &SmallIntSet,
|
|
|
|
f: &fn(&uint) -> bool) -> bool {
|
2013-05-27 22:35:06 -05:00
|
|
|
let len = cmp::max(self.map.v.len() ,other.map.v.len());
|
|
|
|
|
|
|
|
for uint::range(0, len) |i| {
|
|
|
|
if self.contains(&i) ^ other.contains(&i) {
|
|
|
|
if !f(&i) { return false; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2013-05-22 07:01:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Visit the values representing the uintersection
|
|
|
|
fn intersection(&self, other: &SmallIntSet, f: &fn(&uint) -> bool) -> bool {
|
|
|
|
self.each(|v| !other.contains(v) || f(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Visit the values representing the union
|
|
|
|
fn union(&self, other: &SmallIntSet, f: &fn(&uint) -> bool) -> bool {
|
2013-05-27 22:35:06 -05:00
|
|
|
let len = cmp::max(self.map.v.len() ,other.map.v.len());
|
|
|
|
|
|
|
|
for uint::range(0, len) |i| {
|
|
|
|
if self.contains(&i) || other.contains(&i) {
|
|
|
|
if !f(&i) { return false; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2013-05-22 07:01:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl SmallIntSet {
|
2013-05-22 07:01:21 -05:00
|
|
|
/// Create an empty SmallIntSet
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn new() -> SmallIntSet { SmallIntSet{map: SmallIntMap::new()} }
|
2013-06-23 16:57:39 -05:00
|
|
|
|
|
|
|
/// Visit all values in order
|
|
|
|
pub fn each(&self, f: &fn(&uint) -> bool) -> bool { self.map.each_key(f) }
|
2013-05-22 07:01:21 -05:00
|
|
|
}
|
|
|
|
|
2013-01-31 17:56:12 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2013-05-21 19:24:31 -05:00
|
|
|
|
2013-01-31 18:07:08 -06:00
|
|
|
use super::SmallIntMap;
|
2013-03-24 19:35:23 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_find_mut() {
|
|
|
|
let mut m = SmallIntMap::new();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(m.insert(1, 12));
|
|
|
|
assert!(m.insert(2, 8));
|
|
|
|
assert!(m.insert(5, 14));
|
2013-03-24 19:35:23 -05:00
|
|
|
let new = 100;
|
|
|
|
match m.find_mut(&5) {
|
|
|
|
None => fail!(), Some(x) => *x = new
|
|
|
|
}
|
|
|
|
assert_eq!(m.find(&5), Some(&new));
|
|
|
|
}
|
2013-01-31 17:56:12 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_len() {
|
2013-01-31 18:07:08 -06:00
|
|
|
let mut map = SmallIntMap::new();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(map.len(), 0);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(map.is_empty());
|
|
|
|
assert!(map.insert(5, 20));
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(map.len(), 1);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!map.is_empty());
|
|
|
|
assert!(map.insert(11, 12));
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(map.len(), 2);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!map.is_empty());
|
|
|
|
assert!(map.insert(14, 22));
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(map.len(), 3);
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(!map.is_empty());
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_clear() {
|
2013-01-31 18:07:08 -06:00
|
|
|
let mut map = SmallIntMap::new();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(map.insert(5, 20));
|
|
|
|
assert!(map.insert(11, 12));
|
|
|
|
assert!(map.insert(14, 22));
|
2013-01-31 17:56:12 -06:00
|
|
|
map.clear();
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(map.is_empty());
|
|
|
|
assert!(map.find(&5).is_none());
|
|
|
|
assert!(map.find(&11).is_none());
|
|
|
|
assert!(map.find(&14).is_none());
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_insert_with_key() {
|
2013-01-31 18:07:08 -06:00
|
|
|
let mut map = SmallIntMap::new();
|
2013-01-31 17:56:12 -06:00
|
|
|
|
|
|
|
// 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
|
|
|
|
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);
|
|
|
|
|
|
|
|
// check the total counts
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(map.find(&3).get(), &10);
|
|
|
|
assert_eq!(map.find(&5).get(), &3);
|
|
|
|
assert_eq!(map.find(&9).get(), &1);
|
2013-01-31 17:56:12 -06:00
|
|
|
|
|
|
|
// sadly, no sevens were counted
|
2013-03-28 20:39:09 -05:00
|
|
|
assert!(map.find(&7).is_none());
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
2013-05-04 08:54:58 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_swap() {
|
|
|
|
let mut m = SmallIntMap::new();
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(m.swap(1, 2), None);
|
|
|
|
assert_eq!(m.swap(1, 3), Some(2));
|
|
|
|
assert_eq!(m.swap(1, 4), Some(3));
|
2013-05-04 08:54:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pop() {
|
|
|
|
let mut m = SmallIntMap::new();
|
|
|
|
m.insert(1, 2);
|
2013-05-18 21:02:45 -05:00
|
|
|
assert_eq!(m.pop(&1), Some(2));
|
|
|
|
assert_eq!(m.pop(&1), None);
|
2013-05-04 08:54:58 -05:00
|
|
|
}
|
2013-01-31 17:56:12 -06:00
|
|
|
}
|
2013-05-22 07:01:21 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test_set {
|
2013-05-23 10:33:30 -05:00
|
|
|
|
2013-05-22 07:01:21 -05:00
|
|
|
use super::SmallIntSet;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_disjoint() {
|
|
|
|
let mut xs = SmallIntSet::new();
|
|
|
|
let mut ys = SmallIntSet::new();
|
|
|
|
assert!(xs.is_disjoint(&ys));
|
|
|
|
assert!(ys.is_disjoint(&xs));
|
|
|
|
assert!(xs.insert(5));
|
|
|
|
assert!(ys.insert(11));
|
|
|
|
assert!(xs.is_disjoint(&ys));
|
|
|
|
assert!(ys.is_disjoint(&xs));
|
|
|
|
assert!(xs.insert(7));
|
|
|
|
assert!(xs.insert(19));
|
|
|
|
assert!(xs.insert(4));
|
|
|
|
assert!(ys.insert(2));
|
|
|
|
assert!(xs.is_disjoint(&ys));
|
|
|
|
assert!(ys.is_disjoint(&xs));
|
|
|
|
assert!(ys.insert(7));
|
|
|
|
assert!(!xs.is_disjoint(&ys));
|
|
|
|
assert!(!ys.is_disjoint(&xs));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_subset_and_superset() {
|
|
|
|
let mut a = SmallIntSet::new();
|
|
|
|
assert!(a.insert(0));
|
|
|
|
assert!(a.insert(5));
|
|
|
|
assert!(a.insert(11));
|
|
|
|
assert!(a.insert(7));
|
|
|
|
|
|
|
|
let mut b = SmallIntSet::new();
|
|
|
|
assert!(b.insert(0));
|
|
|
|
assert!(b.insert(7));
|
|
|
|
assert!(b.insert(19));
|
|
|
|
assert!(b.insert(250));
|
|
|
|
assert!(b.insert(11));
|
|
|
|
assert!(b.insert(200));
|
|
|
|
|
|
|
|
assert!(!a.is_subset(&b));
|
|
|
|
assert!(!a.is_superset(&b));
|
|
|
|
assert!(!b.is_subset(&a));
|
|
|
|
assert!(!b.is_superset(&a));
|
|
|
|
|
|
|
|
assert!(b.insert(5));
|
|
|
|
|
|
|
|
assert!(a.is_subset(&b));
|
|
|
|
assert!(!a.is_superset(&b));
|
|
|
|
assert!(!b.is_subset(&a));
|
|
|
|
assert!(b.is_superset(&a));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_intersection() {
|
|
|
|
let mut a = SmallIntSet::new();
|
|
|
|
let mut b = SmallIntSet::new();
|
|
|
|
|
|
|
|
assert!(a.insert(11));
|
|
|
|
assert!(a.insert(1));
|
|
|
|
assert!(a.insert(3));
|
|
|
|
assert!(a.insert(77));
|
|
|
|
assert!(a.insert(103));
|
|
|
|
assert!(a.insert(5));
|
|
|
|
|
|
|
|
assert!(b.insert(2));
|
|
|
|
assert!(b.insert(11));
|
|
|
|
assert!(b.insert(77));
|
|
|
|
assert!(b.insert(5));
|
|
|
|
assert!(b.insert(3));
|
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
let expected = [3, 5, 11, 77];
|
|
|
|
for a.intersection(&b) |x| {
|
2013-06-28 11:08:32 -05:00
|
|
|
assert!(expected.contains(x));
|
2013-05-22 07:01:21 -05:00
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
assert_eq!(i, expected.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_difference() {
|
|
|
|
let mut a = SmallIntSet::new();
|
|
|
|
let mut b = SmallIntSet::new();
|
|
|
|
|
|
|
|
assert!(a.insert(1));
|
|
|
|
assert!(a.insert(3));
|
|
|
|
assert!(a.insert(5));
|
|
|
|
assert!(a.insert(9));
|
|
|
|
assert!(a.insert(11));
|
|
|
|
|
|
|
|
assert!(b.insert(3));
|
|
|
|
assert!(b.insert(9));
|
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
let expected = [1, 5, 11];
|
|
|
|
for a.difference(&b) |x| {
|
2013-06-28 11:08:32 -05:00
|
|
|
assert!(expected.contains(x));
|
2013-05-22 07:01:21 -05:00
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
assert_eq!(i, expected.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_symmetric_difference() {
|
|
|
|
let mut a = SmallIntSet::new();
|
|
|
|
let mut b = SmallIntSet::new();
|
|
|
|
|
|
|
|
assert!(a.insert(1));
|
|
|
|
assert!(a.insert(3));
|
|
|
|
assert!(a.insert(5));
|
|
|
|
assert!(a.insert(9));
|
|
|
|
assert!(a.insert(11));
|
|
|
|
|
|
|
|
assert!(b.insert(3));
|
|
|
|
assert!(b.insert(9));
|
|
|
|
assert!(b.insert(14));
|
|
|
|
assert!(b.insert(22));
|
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
let expected = [1, 5, 11, 14, 22];
|
|
|
|
for a.symmetric_difference(&b) |x| {
|
2013-06-28 11:08:32 -05:00
|
|
|
assert!(expected.contains(x));
|
2013-05-22 07:01:21 -05:00
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
assert_eq!(i, expected.len());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_union() {
|
|
|
|
let mut a = SmallIntSet::new();
|
|
|
|
let mut b = SmallIntSet::new();
|
|
|
|
|
|
|
|
assert!(a.insert(1));
|
|
|
|
assert!(a.insert(3));
|
|
|
|
assert!(a.insert(5));
|
|
|
|
assert!(a.insert(9));
|
|
|
|
assert!(a.insert(11));
|
|
|
|
assert!(a.insert(16));
|
|
|
|
assert!(a.insert(19));
|
|
|
|
assert!(a.insert(24));
|
|
|
|
|
|
|
|
assert!(b.insert(1));
|
|
|
|
assert!(b.insert(5));
|
|
|
|
assert!(b.insert(9));
|
|
|
|
assert!(b.insert(13));
|
|
|
|
assert!(b.insert(19));
|
|
|
|
|
|
|
|
let mut i = 0;
|
|
|
|
let expected = [1, 3, 5, 9, 11, 13, 16, 19, 24];
|
|
|
|
for a.union(&b) |x| {
|
2013-06-28 11:08:32 -05:00
|
|
|
assert!(expected.contains(x));
|
2013-05-22 07:01:21 -05:00
|
|
|
i += 1
|
|
|
|
}
|
|
|
|
assert_eq!(i, expected.len());
|
|
|
|
}
|
|
|
|
}
|