2013-05-07 16:30:21 -05: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.
|
|
|
|
|
2013-06-30 13:12:04 -05:00
|
|
|
use std::iterator::Iterator;
|
2013-05-17 17:28:44 -05:00
|
|
|
|
2013-08-03 18:59:24 -05:00
|
|
|
#[deriving(Clone, Eq, IterBytes, ToStr)]
|
2013-05-07 16:30:21 -05:00
|
|
|
pub struct EnumSet<E> {
|
2013-05-10 14:57:27 -05:00
|
|
|
// We must maintain the invariant that no bits are set
|
|
|
|
// for which no variant exists
|
|
|
|
priv bits: uint
|
2013-05-07 16:30:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait CLike {
|
|
|
|
pub fn to_uint(&self) -> uint;
|
|
|
|
pub fn from_uint(uint) -> Self;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn bit<E:CLike>(e: E) -> uint {
|
|
|
|
1 << e.to_uint()
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl<E:CLike> EnumSet<E> {
|
|
|
|
pub fn empty() -> EnumSet<E> {
|
2013-05-07 16:30:21 -05:00
|
|
|
EnumSet {bits: 0}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn is_empty(&self) -> bool {
|
2013-05-07 16:30:21 -05:00
|
|
|
self.bits == 0
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn intersects(&self, e: EnumSet<E>) -> bool {
|
2013-05-07 16:30:21 -05:00
|
|
|
(self.bits & e.bits) != 0
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn intersection(&self, e: EnumSet<E>) -> EnumSet<E> {
|
2013-05-10 14:57:27 -05:00
|
|
|
EnumSet {bits: self.bits & e.bits}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn contains(&self, e: EnumSet<E>) -> bool {
|
2013-05-07 16:30:21 -05:00
|
|
|
(self.bits & e.bits) == e.bits
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn union(&self, e: EnumSet<E>) -> EnumSet<E> {
|
2013-05-10 14:57:27 -05:00
|
|
|
EnumSet {bits: self.bits | e.bits}
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn add(&mut self, e: E) {
|
2013-05-07 16:30:21 -05:00
|
|
|
self.bits |= bit(e);
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn contains_elem(&self, e: E) -> bool {
|
2013-05-07 16:30:21 -05:00
|
|
|
(self.bits & bit(e)) != 0
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn each(&self, f: &fn(E) -> bool) -> bool {
|
2013-05-10 17:20:06 -05:00
|
|
|
let mut bits = self.bits;
|
|
|
|
let mut index = 0;
|
|
|
|
while bits != 0 {
|
|
|
|
if (bits & 1) != 0 {
|
|
|
|
let e = CLike::from_uint(index);
|
|
|
|
if !f(e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
index += 1;
|
|
|
|
bits >>= 1;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2013-06-30 13:12:04 -05:00
|
|
|
|
|
|
|
pub fn iter(&self) -> EnumSetIterator<E> {
|
|
|
|
EnumSetIterator::new(self.bits)
|
|
|
|
}
|
2013-05-07 16:30:21 -05:00
|
|
|
}
|
|
|
|
|
2013-05-12 19:34:15 -05:00
|
|
|
impl<E:CLike> Sub<EnumSet<E>, EnumSet<E>> for EnumSet<E> {
|
2013-05-07 16:30:21 -05:00
|
|
|
fn sub(&self, e: &EnumSet<E>) -> EnumSet<E> {
|
|
|
|
EnumSet {bits: self.bits & !e.bits}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-12 19:34:15 -05:00
|
|
|
impl<E:CLike> BitOr<EnumSet<E>, EnumSet<E>> for EnumSet<E> {
|
2013-05-07 16:30:21 -05:00
|
|
|
fn bitor(&self, e: &EnumSet<E>) -> EnumSet<E> {
|
|
|
|
EnumSet {bits: self.bits | e.bits}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-12 19:34:15 -05:00
|
|
|
impl<E:CLike> BitAnd<EnumSet<E>, EnumSet<E>> for EnumSet<E> {
|
2013-05-07 16:30:21 -05:00
|
|
|
fn bitand(&self, e: &EnumSet<E>) -> EnumSet<E> {
|
|
|
|
EnumSet {bits: self.bits & e.bits}
|
|
|
|
}
|
|
|
|
}
|
2013-05-08 14:10:26 -05:00
|
|
|
|
2013-06-30 13:12:04 -05:00
|
|
|
pub struct EnumSetIterator<E> {
|
|
|
|
priv index: uint,
|
|
|
|
priv bits: uint,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E:CLike> EnumSetIterator<E> {
|
|
|
|
fn new(bits: uint) -> EnumSetIterator<E> {
|
|
|
|
EnumSetIterator { index: 0, bits: bits }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<E:CLike> Iterator<E> for EnumSetIterator<E> {
|
|
|
|
fn next(&mut self) -> Option<E> {
|
|
|
|
if (self.bits == 0) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (self.bits & 1) == 0 {
|
|
|
|
self.index += 1;
|
|
|
|
self.bits >>= 1;
|
|
|
|
}
|
|
|
|
let elem = CLike::from_uint(self.index);
|
|
|
|
self.index += 1;
|
|
|
|
self.bits >>= 1;
|
|
|
|
Some(elem)
|
|
|
|
}
|
|
|
|
|
2013-07-02 20:40:46 -05:00
|
|
|
fn size_hint(&self) -> (uint, Option<uint>) {
|
|
|
|
let exact = self.bits.population_count();
|
|
|
|
(exact, Some(exact))
|
2013-06-30 13:12:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-08 14:10:26 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2013-05-22 21:59:22 -05:00
|
|
|
|
2013-06-28 17:32:26 -05:00
|
|
|
use std::cast;
|
2013-05-22 21:59:22 -05:00
|
|
|
|
2013-05-08 14:10:26 -05:00
|
|
|
use util::enum_set::*;
|
|
|
|
|
|
|
|
#[deriving(Eq)]
|
|
|
|
enum Foo {
|
|
|
|
A, B, C
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CLike for Foo {
|
|
|
|
pub fn to_uint(&self) -> uint {
|
|
|
|
*self as uint
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_uint(v: uint) -> Foo {
|
|
|
|
unsafe { cast::transmute(v) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty() {
|
|
|
|
let e: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
assert!(e.is_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// intersect
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_two_empties_do_not_intersect() {
|
|
|
|
let e1: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
let e2: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
assert!(!e1.intersects(e2));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_empty_does_not_intersect_with_full() {
|
|
|
|
let e1: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
|
|
|
|
let mut e2: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
e2.add(A);
|
|
|
|
e2.add(B);
|
|
|
|
e2.add(C);
|
|
|
|
|
|
|
|
assert!(!e1.intersects(e2));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_disjoint_intersects() {
|
|
|
|
let mut e1: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
e1.add(A);
|
|
|
|
|
|
|
|
let mut e2: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
e2.add(B);
|
|
|
|
|
|
|
|
assert!(!e1.intersects(e2));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_overlapping_intersects() {
|
|
|
|
let mut e1: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
e1.add(A);
|
|
|
|
|
|
|
|
let mut e2: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
e2.add(A);
|
|
|
|
e2.add(B);
|
|
|
|
|
|
|
|
assert!(e1.intersects(e2));
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// contains and contains_elem
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_contains() {
|
|
|
|
let mut e1: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
e1.add(A);
|
|
|
|
|
|
|
|
let mut e2: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
e2.add(A);
|
|
|
|
e2.add(B);
|
|
|
|
|
|
|
|
assert!(!e1.contains(e2));
|
|
|
|
assert!(e2.contains(e1));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_contains_elem() {
|
|
|
|
let mut e1: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
e1.add(A);
|
|
|
|
assert!(e1.contains_elem(A));
|
|
|
|
assert!(!e1.contains_elem(B));
|
|
|
|
assert!(!e1.contains_elem(C));
|
|
|
|
|
|
|
|
e1.add(A);
|
|
|
|
e1.add(B);
|
|
|
|
assert!(e1.contains_elem(A));
|
|
|
|
assert!(e1.contains_elem(B));
|
|
|
|
assert!(!e1.contains_elem(C));
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2013-06-28 21:09:58 -05:00
|
|
|
// iter / each
|
2013-06-30 13:12:04 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator() {
|
|
|
|
let mut e1: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
|
|
|
|
let elems: ~[Foo] = e1.iter().collect();
|
|
|
|
assert_eq!(~[], elems)
|
|
|
|
|
|
|
|
e1.add(A);
|
|
|
|
let elems: ~[Foo] = e1.iter().collect();
|
|
|
|
assert_eq!(~[A], elems)
|
|
|
|
|
|
|
|
e1.add(C);
|
|
|
|
let elems: ~[Foo] = e1.iter().collect();
|
|
|
|
assert_eq!(~[A,C], elems)
|
|
|
|
|
|
|
|
e1.add(C);
|
|
|
|
let elems: ~[Foo] = e1.iter().collect();
|
|
|
|
assert_eq!(~[A,C], elems)
|
|
|
|
|
|
|
|
e1.add(B);
|
|
|
|
let elems: ~[Foo] = e1.iter().collect();
|
|
|
|
assert_eq!(~[A,B,C], elems)
|
|
|
|
}
|
|
|
|
|
2013-05-08 14:10:26 -05:00
|
|
|
#[test]
|
|
|
|
fn test_each() {
|
|
|
|
let mut e1: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
|
2013-06-30 13:12:04 -05:00
|
|
|
assert_eq!(~[], collect(e1))
|
2013-05-08 14:10:26 -05:00
|
|
|
|
|
|
|
e1.add(A);
|
2013-06-30 13:12:04 -05:00
|
|
|
assert_eq!(~[A], collect(e1))
|
2013-05-08 14:10:26 -05:00
|
|
|
|
|
|
|
e1.add(C);
|
2013-06-30 13:12:04 -05:00
|
|
|
assert_eq!(~[A,C], collect(e1))
|
2013-05-08 14:10:26 -05:00
|
|
|
|
|
|
|
e1.add(C);
|
2013-06-30 13:12:04 -05:00
|
|
|
assert_eq!(~[A,C], collect(e1))
|
2013-05-08 14:10:26 -05:00
|
|
|
|
|
|
|
e1.add(B);
|
2013-06-30 13:12:04 -05:00
|
|
|
assert_eq!(~[A,B,C], collect(e1))
|
2013-05-08 14:10:26 -05:00
|
|
|
}
|
|
|
|
|
2013-06-28 21:09:58 -05:00
|
|
|
fn collect(e: EnumSet<Foo>) -> ~[Foo] {
|
|
|
|
let mut elems = ~[];
|
|
|
|
e.each(|elem| {
|
|
|
|
elems.push(elem);
|
|
|
|
true
|
|
|
|
});
|
|
|
|
elems
|
|
|
|
}
|
|
|
|
|
2013-05-08 14:10:26 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// operators
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_operators() {
|
|
|
|
let mut e1: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
e1.add(A);
|
|
|
|
e1.add(C);
|
|
|
|
|
|
|
|
let mut e2: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
e2.add(B);
|
|
|
|
e2.add(C);
|
|
|
|
|
|
|
|
let e_union = e1 | e2;
|
2013-06-30 13:12:04 -05:00
|
|
|
let elems: ~[Foo] = e_union.iter().collect();
|
|
|
|
assert_eq!(~[A,B,C], elems)
|
2013-05-08 14:10:26 -05:00
|
|
|
|
|
|
|
let e_intersection = e1 & e2;
|
2013-06-30 13:12:04 -05:00
|
|
|
let elems: ~[Foo] = e_intersection.iter().collect();
|
|
|
|
assert_eq!(~[C], elems)
|
2013-05-08 14:10:26 -05:00
|
|
|
|
|
|
|
let e_subtract = e1 - e2;
|
2013-06-30 13:12:04 -05:00
|
|
|
let elems: ~[Foo] = e_subtract.iter().collect();
|
|
|
|
assert_eq!(~[A], elems)
|
2013-05-08 14:10:26 -05:00
|
|
|
}
|
|
|
|
}
|