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-10-12 21:02:46 -05:00
|
|
|
//! A structure for holding a set of enum variants
|
|
|
|
//!
|
|
|
|
//! This module defines a container which uses an efficient bit mask
|
|
|
|
//! representation to hold C-like enum variants.
|
|
|
|
|
2014-01-14 18:32:04 -06:00
|
|
|
use std::num::Bitwise;
|
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
#[deriving(Clone, PartialEq, TotalEq, Hash, Show)]
|
2013-07-25 23:53:29 -05:00
|
|
|
/// A specialized Set implementation to use enum types.
|
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
|
2014-03-27 17:10:04 -05:00
|
|
|
bits: uint
|
2013-05-07 16:30:21 -05:00
|
|
|
}
|
|
|
|
|
2013-08-16 00:41:28 -05:00
|
|
|
/// An interface for casting C-like enum to uint and back.
|
2013-05-07 16:30:21 -05:00
|
|
|
pub trait CLike {
|
2013-07-25 23:53:29 -05:00
|
|
|
/// Converts C-like enum to uint.
|
2013-08-09 03:25:24 -05:00
|
|
|
fn to_uint(&self) -> uint;
|
2013-07-25 23:53:29 -05:00
|
|
|
/// Converts uint to C-like enum.
|
2013-08-09 03:25:24 -05:00
|
|
|
fn from_uint(uint) -> Self;
|
2013-05-07 16:30:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn bit<E:CLike>(e: E) -> uint {
|
|
|
|
1 << e.to_uint()
|
|
|
|
}
|
|
|
|
|
2013-05-31 17:17:22 -05:00
|
|
|
impl<E:CLike> EnumSet<E> {
|
2013-07-25 23:53:29 -05:00
|
|
|
/// Returns an empty EnumSet.
|
2013-05-31 17:17:22 -05:00
|
|
|
pub fn empty() -> EnumSet<E> {
|
2013-05-07 16:30:21 -05:00
|
|
|
EnumSet {bits: 0}
|
|
|
|
}
|
|
|
|
|
2013-07-25 23:53:29 -05:00
|
|
|
/// Returns true if an EnumSet is empty.
|
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-07-25 23:53:29 -05:00
|
|
|
/// Returns true if an EnumSet contains any enum of a given EnumSet
|
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-07-25 23:53:29 -05:00
|
|
|
/// Returns an intersection of both EnumSets.
|
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-07-25 23:53:29 -05:00
|
|
|
/// Returns true if a given EnumSet is included in an EnumSet.
|
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-07-25 23:53:29 -05:00
|
|
|
/// Returns a union of both EnumSets.
|
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-07-25 23:53:29 -05:00
|
|
|
/// Add an enum to an EnumSet
|
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-07-25 23:53:29 -05:00
|
|
|
/// Returns true if an EnumSet contains a given enum
|
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-07-25 23:53:29 -05:00
|
|
|
/// Returns an iterator over an EnumSet
|
2014-01-14 21:32:24 -06:00
|
|
|
pub fn iter(&self) -> Items<E> {
|
|
|
|
Items::new(self.bits)
|
2013-06-30 13:12:04 -05:00
|
|
|
}
|
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-07-25 23:53:29 -05:00
|
|
|
/// An iterator over an EnumSet
|
2014-01-14 21:32:24 -06:00
|
|
|
pub struct Items<E> {
|
2014-03-27 17:10:04 -05:00
|
|
|
index: uint,
|
|
|
|
bits: uint,
|
2013-06-30 13:12:04 -05:00
|
|
|
}
|
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
impl<E:CLike> Items<E> {
|
|
|
|
fn new(bits: uint) -> Items<E> {
|
|
|
|
Items { index: 0, bits: bits }
|
2013-06-30 13:12:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 21:32:24 -06:00
|
|
|
impl<E:CLike> Iterator<E> for Items<E> {
|
2013-06-30 13:12:04 -05:00
|
|
|
fn next(&mut self) -> Option<E> {
|
2014-01-19 02:21:14 -06:00
|
|
|
if self.bits == 0 {
|
2013-06-30 13:12:04 -05:00
|
|
|
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>) {
|
2014-02-16 17:12:10 -06:00
|
|
|
let exact = self.bits.count_ones();
|
2013-07-02 20:40:46 -05:00
|
|
|
(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
|
|
|
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
use std::mem;
|
2013-05-22 21:59:22 -05:00
|
|
|
|
2014-02-20 00:10:41 -06:00
|
|
|
use enum_set::{EnumSet, CLike};
|
2013-05-08 14:10:26 -05:00
|
|
|
|
2014-05-29 19:45:07 -05:00
|
|
|
#[deriving(PartialEq, Show)]
|
2013-09-25 11:41:10 -05:00
|
|
|
#[repr(uint)]
|
2013-05-08 14:10:26 -05:00
|
|
|
enum Foo {
|
|
|
|
A, B, C
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CLike for Foo {
|
2013-08-09 03:25:24 -05:00
|
|
|
fn to_uint(&self) -> uint {
|
2013-05-08 14:10:26 -05:00
|
|
|
*self as uint
|
|
|
|
}
|
|
|
|
|
2013-08-09 03:25:24 -05:00
|
|
|
fn from_uint(v: uint) -> Foo {
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
unsafe { mem::transmute(v) }
|
2013-05-08 14:10:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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-07-25 23:53:29 -05:00
|
|
|
// iter
|
2013-06-30 13:12:04 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iterator() {
|
|
|
|
let mut e1: EnumSet<Foo> = EnumSet::empty();
|
|
|
|
|
2014-04-05 00:45:42 -05:00
|
|
|
let elems: Vec<Foo> = e1.iter().collect();
|
|
|
|
assert!(elems.is_empty())
|
2013-06-30 13:12:04 -05:00
|
|
|
|
|
|
|
e1.add(A);
|
2014-04-05 00:45:42 -05:00
|
|
|
let elems = e1.iter().collect();
|
|
|
|
assert_eq!(vec![A], elems)
|
2013-06-30 13:12:04 -05:00
|
|
|
|
|
|
|
e1.add(C);
|
2014-04-05 00:45:42 -05:00
|
|
|
let elems = e1.iter().collect();
|
|
|
|
assert_eq!(vec![A,C], elems)
|
2013-06-30 13:12:04 -05:00
|
|
|
|
|
|
|
e1.add(C);
|
2014-04-05 00:45:42 -05:00
|
|
|
let elems = e1.iter().collect();
|
|
|
|
assert_eq!(vec![A,C], elems)
|
2013-06-30 13:12:04 -05:00
|
|
|
|
|
|
|
e1.add(B);
|
2014-04-05 00:45:42 -05:00
|
|
|
let elems = e1.iter().collect();
|
|
|
|
assert_eq!(vec![A,B,C], elems)
|
2013-06-30 13:12:04 -05:00
|
|
|
}
|
|
|
|
|
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;
|
2014-04-05 00:45:42 -05:00
|
|
|
let elems = e_union.iter().collect();
|
|
|
|
assert_eq!(vec![A,B,C], elems)
|
2013-05-08 14:10:26 -05:00
|
|
|
|
|
|
|
let e_intersection = e1 & e2;
|
2014-04-05 00:45:42 -05:00
|
|
|
let elems = e_intersection.iter().collect();
|
|
|
|
assert_eq!(vec![C], elems)
|
2013-05-08 14:10:26 -05:00
|
|
|
|
|
|
|
let e_subtract = e1 - e2;
|
2014-04-05 00:45:42 -05:00
|
|
|
let elems = e_subtract.iter().collect();
|
|
|
|
assert_eq!(vec![A], elems)
|
2013-05-08 14:10:26 -05:00
|
|
|
}
|
|
|
|
}
|