2 // System.Collections.Specialized.BitVector32.cs
5 // Miguel de Icaza (miguel@ximian.com)
6 // Lawrence Pit (loz@cable.a2000.nl)
7 // Andrew Birkett (adb@tardis.ed.ac.uk)
8 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
10 // (C) Ximian, Inc. http://www.ximian.com
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 namespace System
.Collections
.Specialized
{
39 public struct BitVector32
{
42 public struct Section
{
46 internal Section (short mask
, short offset
) {
56 get { return offset; }
59 public override bool Equals (object o
)
64 Section section
= (Section
) o
;
65 return this.mask
== section
.mask
&&
66 this.offset
== section
.offset
;
69 public override int GetHashCode ()
71 return (((Int16
) mask
).GetHashCode () << 16) +
72 ((Int16
) offset
).GetHashCode ();
75 public override string ToString ()
77 return "Section{0x" + Convert
.ToString(mask
, 16) +
78 ", 0x" + Convert
.ToString(offset
, 16) + "}";
81 public static string ToString (Section
value)
83 StringBuilder b
= new StringBuilder ();
84 b
.Append ("Section{0x");
85 b
.Append (Convert
.ToString(value.Mask
,16));
87 b
.Append (Convert
.ToString(value.Offset
,16));
96 public BitVector32 (BitVector32 source
)
101 public BitVector32 (int init
)
112 public int this [BitVector32
.Section section
] {
114 return ((bits
>> section
.Offset
) & section
.Mask
);
119 throw new ArgumentException ("Section can't hold negative values");
120 if (value > section
.Mask
)
121 throw new ArgumentException ("Value too large to fit in section");
122 bits
&= ~
(section
.Mask
<< section
.Offset
);
123 bits
|= (value << section
.Offset
);
127 public bool this [int mask
] {
129 long tmp
= (uint)bits
;
130 return (tmp
& (long)mask
) == (long)mask
;
143 public static int CreateMask ()
148 public static int CreateMask (int prev
)
152 if (prev
== Int32
.MinValue
)
153 throw new InvalidOperationException ("all bits set");
157 public static Section
CreateSection (short maxValue
)
159 return CreateSection (maxValue
, new Section (0, 0));
162 public static Section
CreateSection (short maxValue
, BitVector32
.Section previous
)
165 throw new ArgumentException ("maxValue");
167 int bit
= HighestSetBit(maxValue
) + 1;
168 int mask
= (1 << bit
) - 1;
169 int offset
= previous
.Offset
+ NumberOfSetBits (previous
.Mask
);
172 throw new ArgumentException ("Sections cannot exceed 32 bits in total");
175 return new Section ((short) mask
, (short) offset
);
178 public override bool Equals (object o
)
180 if (!(o
is BitVector32
))
183 return bits
== ((BitVector32
) o
).bits
;
186 public override int GetHashCode ()
188 return bits
.GetHashCode ();
191 public override string ToString ()
193 return ToString (this);
196 public static string ToString (BitVector32
value)
198 StringBuilder b
= new StringBuilder ();
199 b
.Append ("BitVector32{");
200 long mask
= (long) 0x80000000;
202 b
.Append (((value.bits
& mask
) == 0) ? '0' : '1');
206 return b
.ToString ();
210 private static int NumberOfSetBits (int i
)
213 for (int bit
= 0; bit
< 32; bit
++) {
221 private static int HighestSetBit (int i
)
223 for (int bit
= 31; bit
>= 0; bit
--) {
225 if ((mask
& i
) != 0) {