1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A structure for holding a set of enum variants.
13 //! This module defines a container which uses an efficient bit mask
14 //! representation to hold C-like enum variants.
19 use core
::iter
::{FromIterator}
;
20 use core
::ops
::{Sub, BitOr, BitAnd, BitXor}
;
22 // FIXME(contentions): implement union family of methods? (general design may be wrong here)
24 #[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
25 /// A specialized set implementation to use enum types.
27 /// It is a logic error for an item to be modified in such a way that the transformation of the
28 /// item to or from a `usize`, as determined by the `CLike` trait, changes while the item is in the
29 /// set. This is normally only possible through `Cell`, `RefCell`, global state, I/O, or unsafe
31 pub struct EnumSet
<E
> {
32 // We must maintain the invariant that no bits are set
33 // for which no variant exists
35 marker
: marker
::PhantomData
<E
>,
38 impl<E
> Copy
for EnumSet
<E
> {}
40 impl<E
> Clone
for EnumSet
<E
> {
41 fn clone(&self) -> EnumSet
<E
> { *self }
44 #[stable(feature = "rust1", since = "1.0.0")]
45 impl<E
:CLike
+ fmt
::Debug
> fmt
::Debug
for EnumSet
<E
> {
46 fn fmt(&self, fmt
: &mut fmt
::Formatter
) -> fmt
::Result
{
47 try
!(write
!(fmt
, "{{"));
51 try
!(write
!(fmt
, ", "));
53 try
!(write
!(fmt
, "{:?}", e
));
60 /// An interface for casting C-like enum to usize and back.
61 /// A typically implementation is as below.
69 /// impl CLike for Foo {
70 /// fn to_usize(&self) -> usize {
74 /// fn from_usize(v: usize) -> Foo {
75 /// unsafe { mem::transmute(v) }
80 /// Converts a C-like enum to a `usize`.
81 fn to_usize(&self) -> usize;
82 /// Converts a `usize` to a C-like enum.
83 fn from_usize(usize) -> Self;
86 fn bit
<E
:CLike
>(e
: &E
) -> usize {
88 let value
= e
.to_usize();
89 assert
!(value
< usize::BITS
,
90 "EnumSet only supports up to {} variants.", usize::BITS
- 1);
94 impl<E
:CLike
> EnumSet
<E
> {
95 /// Returns an empty `EnumSet`.
96 #[unstable(feature = "collections",
97 reason
= "matches collection reform specification, waiting for dust to settle")]
98 pub fn new() -> EnumSet
<E
> {
99 EnumSet {bits: 0, marker: marker::PhantomData}
102 /// Returns the number of elements in the given `EnumSet`.
103 #[unstable(feature = "collections",
104 reason
= "matches collection reform specification, waiting for dust to settle")]
105 pub fn len(&self) -> usize {
106 self.bits
.count_ones() as usize
109 /// Returns true if the `EnumSet` is empty.
110 #[unstable(feature = "collections",
111 reason
= "matches collection reform specification, waiting for dust to settle")]
112 pub fn is_empty(&self) -> bool
{
116 pub fn clear(&mut self) {
120 /// Returns `false` if the `EnumSet` contains any enum of the given `EnumSet`.
121 #[unstable(feature = "collections",
122 reason
= "matches collection reform specification, waiting for dust to settle")]
123 pub fn is_disjoint(&self, other
: &EnumSet
<E
>) -> bool
{
124 (self.bits
& other
.bits
) == 0
127 /// Returns `true` if a given `EnumSet` is included in this `EnumSet`.
128 #[unstable(feature = "collections",
129 reason
= "matches collection reform specification, waiting for dust to settle")]
130 pub fn is_superset(&self, other
: &EnumSet
<E
>) -> bool
{
131 (self.bits
& other
.bits
) == other
.bits
134 /// Returns `true` if this `EnumSet` is included in the given `EnumSet`.
135 #[unstable(feature = "collections",
136 reason
= "matches collection reform specification, waiting for dust to settle")]
137 pub fn is_subset(&self, other
: &EnumSet
<E
>) -> bool
{
138 other
.is_superset(self)
141 /// Returns the union of both `EnumSets`.
142 pub fn union(&self, e
: EnumSet
<E
>) -> EnumSet
<E
> {
143 EnumSet
{bits
: self.bits
| e
.bits
,
144 marker
: marker
::PhantomData
}
147 /// Returns the intersection of both `EnumSets`.
148 pub fn intersection(&self, e
: EnumSet
<E
>) -> EnumSet
<E
> {
149 EnumSet
{bits
: self.bits
& e
.bits
,
150 marker
: marker
::PhantomData
}
153 /// Adds an enum to the `EnumSet`, and returns `true` if it wasn't there before
154 #[unstable(feature = "collections",
155 reason
= "matches collection reform specification, waiting for dust to settle")]
156 pub fn insert(&mut self, e
: E
) -> bool
{
157 let result
= !self.contains(&e
);
158 self.bits
|= bit(&e
);
162 /// Removes an enum from the EnumSet
163 #[unstable(feature = "collections",
164 reason
= "matches collection reform specification, waiting for dust to settle")]
165 pub fn remove(&mut self, e
: &E
) -> bool
{
166 let result
= self.contains(e
);
167 self.bits
&= !bit(e
);
171 /// Returns `true` if an `EnumSet` contains a given enum.
172 #[unstable(feature = "collections",
173 reason
= "matches collection reform specification, waiting for dust to settle")]
174 pub fn contains(&self, e
: &E
) -> bool
{
175 (self.bits
& bit(e
)) != 0
178 /// Returns an iterator over an `EnumSet`.
179 #[unstable(feature = "collections",
180 reason
= "matches collection reform specification, waiting for dust to settle")]
181 pub fn iter(&self) -> Iter
<E
> {
186 impl<E
:CLike
> Sub
for EnumSet
<E
> {
187 type Output
= EnumSet
<E
>;
189 fn sub(self, e
: EnumSet
<E
>) -> EnumSet
<E
> {
190 EnumSet {bits: self.bits & !e.bits, marker: marker::PhantomData}
194 impl<E
:CLike
> BitOr
for EnumSet
<E
> {
195 type Output
= EnumSet
<E
>;
197 fn bitor(self, e
: EnumSet
<E
>) -> EnumSet
<E
> {
198 EnumSet {bits: self.bits | e.bits, marker: marker::PhantomData}
202 impl<E
:CLike
> BitAnd
for EnumSet
<E
> {
203 type Output
= EnumSet
<E
>;
205 fn bitand(self, e
: EnumSet
<E
>) -> EnumSet
<E
> {
206 EnumSet {bits: self.bits & e.bits, marker: marker::PhantomData}
210 impl<E
:CLike
> BitXor
for EnumSet
<E
> {
211 type Output
= EnumSet
<E
>;
213 fn bitxor(self, e
: EnumSet
<E
>) -> EnumSet
<E
> {
214 EnumSet {bits: self.bits ^ e.bits, marker: marker::PhantomData}
218 /// An iterator over an EnumSet
222 marker
: marker
::PhantomData
<E
>,
225 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
226 impl<E
> Clone
for Iter
<E
> {
227 fn clone(&self) -> Iter
<E
> {
231 marker
: marker
::PhantomData
,
236 impl<E
:CLike
> Iter
<E
> {
237 fn new(bits
: usize) -> Iter
<E
> {
238 Iter { index: 0, bits: bits, marker: marker::PhantomData }
242 impl<E
:CLike
> Iterator
for Iter
<E
> {
245 fn next(&mut self) -> Option
<E
> {
250 while (self.bits
& 1) == 0 {
254 let elem
= CLike
::from_usize(self.index
);
260 fn size_hint(&self) -> (usize, Option
<usize>) {
261 let exact
= self.bits
.count_ones() as usize;
266 impl<E
:CLike
> FromIterator
<E
> for EnumSet
<E
> {
267 fn from_iter
<I
: IntoIterator
<Item
=E
>>(iter
: I
) -> EnumSet
<E
> {
268 let mut ret
= EnumSet
::new();
274 #[stable(feature = "rust1", since = "1.0.0")]
275 impl<'a
, E
> IntoIterator
for &'a EnumSet
<E
> where E
: CLike
{
277 type IntoIter
= Iter
<E
>;
279 fn into_iter(self) -> Iter
<E
> {
284 impl<E
:CLike
> Extend
<E
> for EnumSet
<E
> {
285 fn extend
<I
: IntoIterator
<Item
=E
>>(&mut self, iter
: I
) {
286 for element
in iter
{
287 self.insert(element
);