~ubuntu-branches/ubuntu/saucy/monodevelop/saucy

« back to all changes in this revision

Viewing changes to external/mono-tools/gendarme/framework/Gendarme.Framework/Bitmask.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2012-05-27 18:08:20 UTC
  • mfrom: (1.8.5) (1.5.8 sid)
  • Revision ID: package-import@ubuntu.com-20120527180820-f1ub6lhg0s50wci1
Tags: 3.0.2+dfsg-3
* [fcecfe7] Fix monodevelop-core-addins.pc.in to point to actual 
  installed location of assemblies.
* [26e1a07] DebSrc 3.0 does not support Quilt's -p parameter, so 
  manually adjust the path in the patch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// Gendarme.Framework.Bitmask<T> class
 
3
//
 
4
// Authors:
 
5
//      Sebastien Pouliot <sebastien@ximian.com>
 
6
//
 
7
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
 
8
//
 
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:
 
15
//
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
//
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using System.Text;
 
29
 
 
30
namespace Gendarme.Framework {
 
31
 
 
32
        /// <summary>
 
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').
 
35
        /// </summary>
 
36
        /// <typeparam name="T">Type on which the bitmask is based.</typeparam>
 
37
        public class Bitmask<T> : IEquatable<Bitmask<T>> where T : struct, IConvertible {
 
38
 
 
39
                ulong mask;
 
40
 
 
41
                /// <summary>
 
42
                /// Construct a bitmask will all bits clear.
 
43
                /// </summary>
 
44
                public Bitmask ()
 
45
                {
 
46
                }
 
47
 
 
48
                /// <summary>
 
49
                /// Construct a bitmask will all bits set (true) or clear (false)
 
50
                /// </summary>
 
51
                /// <param name="initialValues"></param>
 
52
                public Bitmask (bool initialValues)
 
53
                {
 
54
                        if (initialValues)
 
55
                                mask = UInt64.MaxValue;
 
56
                }
 
57
 
 
58
 
 
59
                // note: the GetHashCode works because both enums and integral
 
60
                // types values are identical to their hash code
 
61
 
 
62
                /// <summary>
 
63
                /// Clear the bit represent by the parameter.
 
64
                /// </summary>
 
65
                /// <param name="bit">Value to clear in the bitmask</param>
 
66
                public void Clear (T bit)
 
67
                {
 
68
                        unchecked {
 
69
                                mask &= ~((ulong) 1 << bit.ToInt32 (null));
 
70
                        }
 
71
                }
 
72
 
 
73
                /// <summary>
 
74
                /// Clear all bits
 
75
                /// </summary>
 
76
                public void ClearAll ()
 
77
                {
 
78
                        mask = 0;
 
79
                }
 
80
 
 
81
                public int Count ()
 
82
                {
 
83
                        if (mask == 0)
 
84
                                return 0;
 
85
 
 
86
                        int count = 0;
 
87
                        for (int i = 0; i < 64; i++) {
 
88
                                if (((mask >> i) & 1) == 1)
 
89
                                        count++;
 
90
                        }
 
91
                        return count;
 
92
                }
 
93
 
 
94
                /// <summary>
 
95
                /// Get the bit represented by the parameter
 
96
                /// </summary>
 
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)
 
100
                {
 
101
                        return (((mask >> bit.ToInt32 (null)) & 1) == 1);
 
102
                }
 
103
 
 
104
                /// <summary>
 
105
                /// Set the bit represented by the parameter
 
106
                /// </summary>
 
107
                /// <param name="bit">Value to set in the bitmask</param>
 
108
                public void Set (T bit)
 
109
                {
 
110
                        mask |= ((ulong) 1 << bit.ToInt32 (null));
 
111
                }
 
112
 
 
113
                /// <summary>
 
114
                /// Set all bits
 
115
                /// </summary>
 
116
                public void SetAll ()
 
117
                {
 
118
                        mask = UInt64.MaxValue;
 
119
                }
 
120
 
 
121
                private void SetRange (int start, int end)
 
122
                {
 
123
                        while (start < end) {
 
124
                                mask |= ((ulong) 1 << start++);
 
125
                        }
 
126
                }
 
127
 
 
128
                /// <summary>
 
129
                /// Set all bits, from 'value' down to 0.
 
130
                /// </summary>
 
131
                /// <param name="bit"></param>
 
132
                public void SetDown (T bit)
 
133
                {
 
134
                        SetRange (0, bit.ToInt32 (null) + 1);
 
135
                }
 
136
 
 
137
                /// <summary>
 
138
                /// Set all bits, from 'value' up to the maximum.
 
139
                /// </summary>
 
140
                /// <param name="bit"></param>
 
141
                public void SetUp (T bit)
 
142
                {
 
143
                        SetRange (bit.ToInt32 (null), 64);
 
144
                }
 
145
 
 
146
                /// <summary>
 
147
                /// Does this bitmask intersects with the specified bitmask.
 
148
                /// </summary>
 
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)
 
152
                {
 
153
                        if (bitmask == null)
 
154
                                return (mask == 0);
 
155
                        return ((mask & bitmask.mask) != 0);
 
156
                }
 
157
 
 
158
                /// <summary>
 
159
                /// Is this bitmask a subset of the specified bitmask.
 
160
                /// </summary>
 
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)
 
164
                {
 
165
                        if (bitmask == null)
 
166
                                return false;
 
167
                        return ((mask & bitmask.mask) == mask);
 
168
                }
 
169
 
 
170
                public override bool Equals (object obj)
 
171
                {
 
172
                        Bitmask<T> other = (obj as Bitmask<T>);
 
173
                        return Equals (other);
 
174
                }
 
175
 
 
176
                public bool Equals (Bitmask<T> other)
 
177
                {
 
178
                        if (other == null)
 
179
                                return false;
 
180
                        return (mask == other.mask);
 
181
                }
 
182
 
 
183
                public override int GetHashCode ()
 
184
                {
 
185
                        return unchecked ((int) (mask ^ (mask >> 32)));
 
186
                }
 
187
 
 
188
                public override string ToString ()
 
189
                {
 
190
                        if (mask == 0)
 
191
                                return "0";
 
192
 
 
193
                        StringBuilder sb = new StringBuilder ();
 
194
                        bool value = false;
 
195
                        for (int i = 63; i >= 0; i--) {
 
196
                                if (((mask >> i) & 1) == 1) {
 
197
                                        sb.Append ("1");
 
198
                                        value = true;
 
199
                                } else if (value) {
 
200
                                        sb.Append ("0");
 
201
                                }
 
202
                        }
 
203
                        return sb.ToString ();
 
204
                }
 
205
        }
 
206
}