2
// Gendarme.Framework.Bitmask<T> class
5
// Sebastien Pouliot <sebastien@ximian.com>
7
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
namespace Gendarme.Framework {
33
/// Provide a bitmask, up to 64 bits, based on an enum or integral value
34
/// (sadly this can't be restricted to enums/integral types using 'where').
36
/// <typeparam name="T">Type on which the bitmask is based.</typeparam>
37
public class Bitmask<T> : IEquatable<Bitmask<T>> where T : struct, IConvertible {
42
/// Construct a bitmask will all bits clear.
49
/// Construct a bitmask will all bits set (true) or clear (false)
51
/// <param name="initialValues"></param>
52
public Bitmask (bool initialValues)
55
mask = UInt64.MaxValue;
59
// note: the GetHashCode works because both enums and integral
60
// types values are identical to their hash code
63
/// Clear the bit represent by the parameter.
65
/// <param name="bit">Value to clear in the bitmask</param>
66
public void Clear (T bit)
69
mask &= ~((ulong) 1 << bit.ToInt32 (null));
76
public void ClearAll ()
87
for (int i = 0; i < 64; i++) {
88
if (((mask >> i) & 1) == 1)
95
/// Get the bit represented by the parameter
97
/// <param name="bit">Value to get in the bitmask</param>
98
/// <returns>True if the bit is set, false otherwise.</returns>
99
public bool Get (T bit)
101
return (((mask >> bit.ToInt32 (null)) & 1) == 1);
105
/// Set the bit represented by the parameter
107
/// <param name="bit">Value to set in the bitmask</param>
108
public void Set (T bit)
110
mask |= ((ulong) 1 << bit.ToInt32 (null));
116
public void SetAll ()
118
mask = UInt64.MaxValue;
121
private void SetRange (int start, int end)
123
while (start < end) {
124
mask |= ((ulong) 1 << start++);
129
/// Set all bits, from 'value' down to 0.
131
/// <param name="bit"></param>
132
public void SetDown (T bit)
134
SetRange (0, bit.ToInt32 (null) + 1);
138
/// Set all bits, from 'value' up to the maximum.
140
/// <param name="bit"></param>
141
public void SetUp (T bit)
143
SetRange (bit.ToInt32 (null), 64);
147
/// Does this bitmask intersects with the specified bitmask.
149
/// <param name="bitmask">Bitmask to compare</param>
150
/// <returns>True if the bitmask intersects with the specified bitmask, False otherwise.</returns>
151
public bool Intersect (Bitmask<T> bitmask)
155
return ((mask & bitmask.mask) != 0);
159
/// Is this bitmask a subset of the specified bitmask.
161
/// <param name="bitmask">Bitmask to compare (potential superset)</param>
162
/// <returns>True if the bitmask is a subset of the specified bitmask, False otherwise.</returns>
163
public bool IsSubsetOf (Bitmask<T> bitmask)
167
return ((mask & bitmask.mask) == mask);
170
public override bool Equals (object obj)
172
Bitmask<T> other = (obj as Bitmask<T>);
173
return Equals (other);
176
public bool Equals (Bitmask<T> other)
180
return (mask == other.mask);
183
public override int GetHashCode ()
185
return unchecked ((int) (mask ^ (mask >> 32)));
188
public override string ToString ()
193
StringBuilder sb = new StringBuilder ();
195
for (int i = 63; i >= 0; i--) {
196
if (((mask >> i) & 1) == 1) {
203
return sb.ToString ();