// 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 or the MIT license // , at your // option. This file may not be copied, modified, or distributed // except according to those terms. use core; #[deriving(Eq, IterBytes)] pub struct EnumSet { bits: uint } pub trait CLike { pub fn to_uint(&self) -> uint; pub fn from_uint(uint) -> Self; } fn bit(e: E) -> uint { 1 << e.to_uint() } pub impl EnumSet { fn empty() -> EnumSet { EnumSet {bits: 0} } fn is_empty(&self) -> bool { self.bits == 0 } fn intersects(&self, e: EnumSet) -> bool { (self.bits & e.bits) != 0 } fn contains(&self, e: EnumSet) -> bool { (self.bits & e.bits) == e.bits } fn add(&mut self, e: E) { self.bits |= bit(e); } fn plus(&self, e: E) -> EnumSet { EnumSet {bits: self.bits | bit(e)} } fn contains_elem(&self, e: E) -> bool { (self.bits & bit(e)) != 0 } fn each(&self, f: &fn(E) -> bool) { 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; } } index += 1; bits >>= 1; } } } impl core::Sub, EnumSet> for EnumSet { fn sub(&self, e: &EnumSet) -> EnumSet { EnumSet {bits: self.bits & !e.bits} } } impl core::BitOr, EnumSet> for EnumSet { fn bitor(&self, e: &EnumSet) -> EnumSet { EnumSet {bits: self.bits | e.bits} } } impl core::BitAnd, EnumSet> for EnumSet { fn bitand(&self, e: &EnumSet) -> EnumSet { EnumSet {bits: self.bits & e.bits} } }