aboutsummaryrefslogtreecommitdiff
path: root/BDInfo/BitVector32.cs
blob: 1ac94bfbeb0aeffcdd89daefe6860da5d4a10ea4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BDInfo
{
    using System.Diagnostics;
    using System.Text;
    using System;

    /// <devdoc>
    ///    <para>Provides a simple light bit vector with easy integer or Boolean access to
    ///       a 32 bit storage.</para>
    /// </devdoc>
    public struct BitVector32
    {
        private uint data;

        /// <devdoc>
        /// <para>Initializes a new instance of the BitVector32 structure with the specified internal data.</para>
        /// </devdoc>
        public BitVector32(int data)
        {
            this.data = (uint)data;
        }

        /// <devdoc>
        /// <para>Initializes a new instance of the BitVector32 structure with the information in the specified 
        ///    value.</para>
        /// </devdoc>
        public BitVector32(BitVector32 value)
        {
            this.data = value.data;
        }

        /// <devdoc>
        ///    <para>Gets or sets a value indicating whether all the specified bits are set.</para>
        /// </devdoc>
        public bool this[int bit]
        {
            get
            {
                return (data & bit) == (uint)bit;
            }
            set
            {
                if (value)
                {
                    data |= (uint)bit;
                }
                else
                {
                    data &= ~(uint)bit;
                }
            }
        }

        /// <devdoc>
        ///    <para>Gets or sets the value for the specified section.</para>
        /// </devdoc>
        public int this[Section section]
        {
            get
            {
                return (int)((data & (uint)(section.Mask << section.Offset)) >> section.Offset);
            }
            set
            {
                value <<= section.Offset;
                int offsetMask = (0xFFFF & (int)section.Mask) << section.Offset;
                data = (data & ~(uint)offsetMask) | ((uint)value & (uint)offsetMask);
            }
        }

        /// <devdoc>
        ///    returns the raw data stored in this bit vector...
        /// </devdoc>
        public int Data
        {
            get
            {
                return (int)data;
            }
        }

        private static short CountBitsSet(short mask)
        {

            // yes, I know there are better algorithms, however, we know the
            // bits are always right aligned, with no holes (i.e. always 00000111,
            // never 000100011), so this is just fine...
            //
            short value = 0;
            while ((mask & 0x1) != 0)
            {
                value++;
                mask >>= 1;
            }
            return value;
        }

        /// <devdoc>
        ///    <para> Creates the first mask in a series.</para>
        /// </devdoc>
        public static int CreateMask()
        {
            return CreateMask(0);
        }

        /// <devdoc>
        ///     Creates the next mask in a series.
        /// </devdoc>
        public static int CreateMask(int previous)
        {
            if (previous == 0)
            {
                return 1;
            }

            if (previous == unchecked((int)0x80000000))
            {
                throw new InvalidOperationException("Bit vector full");
            }

            return previous << 1;
        }

        /// <devdoc>
        ///     Given a highValue, creates the mask
        /// </devdoc>
        private static short CreateMaskFromHighValue(short highValue)
        {
            short required = 16;
            while ((highValue & 0x8000) == 0)
            {
                required--;
                highValue <<= 1;
            }

            ushort value = 0;
            while (required > 0)
            {
                required--;
                value <<= 1;
                value |= 0x1;
            }

            return unchecked((short)value);
        }

        /// <devdoc>
        ///    <para>Creates the first section in a series, with the specified maximum value.</para>
        /// </devdoc>
        public static Section CreateSection(short maxValue)
        {
            return CreateSectionHelper(maxValue, 0, 0);
        }

        /// <devdoc>
        ///    <para>Creates the next section in a series, with the specified maximum value.</para>
        /// </devdoc>
        public static Section CreateSection(short maxValue, Section previous)
        {
            return CreateSectionHelper(maxValue, previous.Mask, previous.Offset);
        }

        private static Section CreateSectionHelper(short maxValue, short priorMask, short priorOffset)
        {
            if (maxValue < 1)
            {
                throw new ArgumentOutOfRangeException("maxValue");
            }
#if DEBUG
            int maskCheck = CreateMaskFromHighValue(maxValue);
            int offsetCheck = priorOffset + CountBitsSet(priorMask);
            Debug.Assert(maskCheck <= short.MaxValue && offsetCheck < 32, "Overflow on BitVector32");
#endif
            short offset = (short)(priorOffset + CountBitsSet(priorMask));
            if (offset >= 32)
            {
                throw new InvalidOperationException("Bit vector full");
            }
            return new Section(CreateMaskFromHighValue(maxValue), offset);
        }

        public override bool Equals(object o)
        {
            if (!(o is BitVector32))
            {
                return false;
            }

            return data == ((BitVector32)o).data;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <devdoc>
        /// </devdoc>
        public static string ToString(BitVector32 value)
        {
            StringBuilder sb = new StringBuilder(/*"BitVector32{".Length*/12 + /*32 bits*/32 + /*"}".Length"*/1);
            sb.Append("BitVector32{");
            int locdata = (int)value.data;
            for (int i = 0; i < 32; i++)
            {
                if ((locdata & 0x80000000) != 0)
                {
                    sb.Append("1");
                }
                else
                {
                    sb.Append("0");
                }
                locdata <<= 1;
            }
            sb.Append("}");
            return sb.ToString();
        }

        /// <devdoc>
        /// </devdoc>
        public override string ToString()
        {
            return BitVector32.ToString(this);
        }

        /// <devdoc>
        ///    <para> 
        ///       Represents an section of the vector that can contain a integer number.</para>
        /// </devdoc>
        public struct Section
        {
            private readonly short mask;
            private readonly short offset;

            internal Section(short mask, short offset)
            {
                this.mask = mask;
                this.offset = offset;
            }

            public short Mask
            {
                get
                {
                    return mask;
                }
            }

            public short Offset
            {
                get
                {
                    return offset;
                }
            }

            public override bool Equals(object o)
            {
                if (o is Section)
                    return Equals((Section)o);
                else
                    return false;
            }

            public bool Equals(Section obj)
            {
                return obj.mask == mask && obj.offset == offset;
            }

            public static bool operator ==(Section a, Section b)
            {
                return a.Equals(b);
            }

            public static bool operator !=(Section a, Section b)
            {
                return !(a == b);
            }

            public override int GetHashCode()
            {
                return base.GetHashCode();
            }

            /// <devdoc>
            /// </devdoc>
            public static string ToString(Section value)
            {
                return "Section{0x" + Convert.ToString(value.Mask, 16) + ", 0x" + Convert.ToString(value.Offset, 16) + "}";
            }

            /// <devdoc>
            /// </devdoc>
            public override string ToString()
            {
                return Section.ToString(this);
            }

        }
    }
}