EnumSet

An EnumSet allows to create a bit field using the members of an enum.

It's designed similarly to the Pascal built-in sets (the "Set Of" construct). It's also related to the phobos type EnumFlag, except that it has no constraint related to the enum member values since it's based on the enum members rank.

It's efficient as function parameter since the size of an EnumSet is equal to the size of its container (so from 1 to 8 bytes). Since manipulating an EnumSet set is mostly about making bitwise operations an EnumSet is completly safe. Another notable characteristic is that an EnumSet is ordered, so if a range is implemented to allow the usage of std.algorithm.searching functions it's always more simple and efficient to use the "in" operator.

There are two ways to use an EnumSet: * using the C-like operators, "+" and "-" to add or remove members, "^" and "&" to get the difference and the intersection. * using the Pascal-like intrinsics (here some functions): include(), exclude() and the 'in' operator.

Constructors

this
this(Stuff stuff)

Initializes the set with some stuff.

Members

Functions

all
bool all()

Returns true if all the members are included.

any
bool any()

Returns true if at least one member is included.

asBitString
string asBitString()

Returns the string representation of the set as a binary representation. Note that the result is preffixed with "0b", as a binary litteral.

container
const(SetType) container()

Returns the enum count

difference
EnumSetType difference(R rhs)

Returns a set representing the difference between this set and the argument.

exclude
void exclude(E someMembers)
void exclude(E[] someMembers)

Excludes someMembers from the set. This is the primitive used for to the operator "-".

fromString
void fromString(const(char)[] str)

Defines the set with a string representation.

include
void include(E someMembers)
void include(E[] someMembers)

Includes someMembers in the set. This is the primitive used for to the operator "+".

intersection
EnumSetType intersection(R rhs)

Returns a set representing the intersection between this set and the argument.

isIncluded
bool isIncluded(E aMember)

Returns true if aMember is in the set. This is the primitive used for to the operator "in".

loadFromText
void loadFromText(const(char)[] value)

Implements the iz.rtti "text struct" traits to allow the deserialization.

none
bool none()

Returns true if the set is empty.

opAssign
void opAssign(const S rhs)
void opAssign(const E[] rhs)
void opAssign(auto ref const EnumSetType rhs)
void opAssign(auto ref R rhs)

Support for the assignment operator.

opBinary
EnumSetType opBinary(E rhs)
EnumSetType opBinary(E[] rhs)
EnumSetType opBinary(EnumSetType rhs)
EnumSetType opBinary(SetType rhs)

Support for "+" and "-" operators.

opEquals
bool opEquals(T rhs)

Support for comparison "=" and "!=" operators.

opEquals
bool opEquals(ref const EnumSetType rhs)

Support for built-in AA.

opIn_r
bool opIn_r(T rhs)

Support for the in operator.

opIndex
bool opIndex(I index)

Support for the array syntax.

opIndexAssign
void opIndexAssign(bool value, E index)

Support for the array assignment syntax.

opOpAssign
void opOpAssign(E[] rhs)
void opOpAssign(E rhs)
void opOpAssign(EnumSetType rhs)

Support for "+=" and "-=" operators.

opSlice
Range opSlice()

see range()

range
Range range()

Returns a range allowing to iterate for each member included in the set.

saveToText
const(char)[] saveToText()

Implements the iz.rtti "text struct" traits to allow the serialization.

set
void set(E member, bool value)

Sets the bit of a particular member

toHash
size_t toHash()

Support for built-in AA.

toString
string toString()

Returns the string representation of the set. The format is the same as the one used in this() and fromString(), similar to an array litteral.

Static functions

max
const(S) max()

Returns the maximal value the set can have.

memberCount
const(S) memberCount()

Returns the enum count

rankInfo
const(EnumRankInfo!E) rankInfo()

Returns a lookup table that can be used to retrieve the rank of a member.

Examples

1 enum Employee {jhon, steve, sophia, douglas, clarice, mitch}
2 alias Team = EnumSet!(Employee, Set8);
3 auto team1 = Team(Employee.jhon, Employee.sophia, Employee.douglas);
4 auto team2 = Team(Employee.jhon, Employee.clarice, Employee.mitch);
5 if (Employee.sophia in team1) writeln("Sophia works in team1");
6 Team overzealous = team1.intersection(team2);
7 if (overzealous != 0)
8 {
9    writeln(overzealous, " work(s) too much !");
10    team1 -= overzealous;
11    assert(team1 == [Employee.sophia, Employee.douglas]);
12 }
13 if (team1.memberCount != team2.memberCount)
14    writeln("teams are not well balanced !");

Meta