-
Notifications
You must be signed in to change notification settings - Fork 4
/
BitVector32.cs
231 lines (211 loc) · 6.12 KB
/
BitVector32.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace System.Collections.Specialized
{
#if WINDOWS_PHONE || PCL
[StructLayout(LayoutKind.Sequential)]
public struct BitVector32
{
private uint data;
public BitVector32(int data)
{
this.data = (uint)data;
}
public BitVector32(BitVector32 value)
{
this.data = value.data;
}
public bool this[int bit]
{
get
{
return ((this.data & bit) == ((long)bit));
}
set
{
if (value)
{
this.data |= (uint)bit;
}
else
{
this.data &= (uint)~bit;
}
}
}
public int this[Section section]
{
get
{
return (int)((this.data & (section.Mask << (section.Offset & 0x1f))) >> (section.Offset & 0x1f));
}
set
{
value = value << section.Offset;
int num = (0xffff & section.Mask) << section.Offset;
this.data = (uint)((this.data & ~num) | (value & num));
}
}
public int Data
{
get
{
return (int)this.data;
}
}
private static short CountBitsSet(short mask)
{
short num = 0;
while ((mask & 1) != 0)
{
num = (short)(num + 1);
mask = (short)(mask >> 1);
}
return num;
}
public static int CreateMask()
{
return CreateMask(0);
}
public static int CreateMask(int previous)
{
if (previous == 0)
{
return 1;
}
if (previous == -2147483648)
{
throw new InvalidOperationException();
}
return (previous << 1);
}
private static short CreateMaskFromHighValue(short highValue)
{
short num = 0x10;
while ((highValue & 0x8000) == 0)
{
num = (short)(num - 1);
highValue = (short)(highValue << 1);
}
ushort num2 = 0;
while (num > 0)
{
num = (short)(num - 1);
num2 = (ushort)(num2 << 1);
num2 = (ushort)(num2 | 1);
}
return (short)num2;
}
public static Section CreateSection(short maxValue)
{
return CreateSectionHelper(maxValue, 0, 0);
}
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 ArgumentException();
}
short offset = (short)(priorOffset + CountBitsSet(priorMask));
if (offset >= 0x20)
{
throw new InvalidOperationException();
}
return new Section(CreateMaskFromHighValue(maxValue), offset);
}
public override bool Equals(object o)
{
return ((o is BitVector32) && (this.data == ((BitVector32)o).data));
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public static string ToString(BitVector32 value)
{
StringBuilder builder = new StringBuilder(0x2d);
builder.Append("BitVector32{");
int data = (int)value.data;
for (int i = 0; i < 0x20; i++)
{
if ((data & 0x80000000L) != 0L)
{
builder.Append("1");
}
else
{
builder.Append("0");
}
data = data << 1;
}
builder.Append("}");
return builder.ToString();
}
public override string ToString()
{
return ToString(this);
}
// Nested Types
[StructLayout(LayoutKind.Sequential)]
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 this.mask;
}
}
public short Offset
{
get
{
return this.offset;
}
}
public override bool Equals(object o)
{
return ((o is BitVector32.Section) && this.Equals((BitVector32.Section)o));
}
public bool Equals(BitVector32.Section obj)
{
return ((obj.mask == this.mask) && (obj.offset == this.offset));
}
public static bool operator ==(BitVector32.Section a, BitVector32.Section b)
{
return a.Equals(b);
}
public static bool operator !=(BitVector32.Section a, BitVector32.Section b)
{
return !(a == b);
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public static string ToString(BitVector32.Section value)
{
return ("Section{0x" + Convert.ToString(value.Mask, 0x10) + ", 0x" + Convert.ToString(value.Offset, 0x10) + "}");
}
public override string ToString()
{
return ToString(this);
}
}
}
#endif
}