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
11 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 namespace System.Collections.Specialized {
37 public struct BitVector32 {
40 public struct Section {
44 internal Section (short mask, short offset) {
54 get { return offset; }
57 public static bool operator == (Section v1, Section v2)
59 return v1.mask == v2.mask &&
60 v1.offset == v2.offset;
63 public static bool operator != (Section v1, Section v2)
65 return v1.mask != v2.mask ||
66 v1.offset != v2.offset;
69 public bool Equals (Section obj)
71 return this.mask == obj.mask &&
72 this.offset == obj.offset;
75 public override bool Equals (object o)
80 Section section = (Section) o;
81 return this.mask == section.mask &&
82 this.offset == section.offset;
85 public override int GetHashCode ()
87 return mask << offset;
90 public override string ToString ()
92 return ToString (this);
95 public static string ToString (Section value)
97 StringBuilder b = new StringBuilder ();
98 b.Append ("Section{0x");
99 b.Append (Convert.ToString(value.Mask,16));
101 b.Append (Convert.ToString(value.Offset,16));
104 return b.ToString ();
110 public BitVector32 (BitVector32 source)
115 public BitVector32 (int init)
126 public int this [BitVector32.Section section] {
128 return ((bits >> section.Offset) & section.Mask);
133 throw new ArgumentException ("Section can't hold negative values");
134 if (value > section.Mask)
135 throw new ArgumentException ("Value too large to fit in section");
136 bits &= ~(section.Mask << section.Offset);
137 bits |= (value << section.Offset);
141 public bool this [int mask] {
144 return (bits & mask) == mask;
146 long tmp = (uint)bits;
147 return (tmp & (long)mask) == (long)mask;
161 public static int CreateMask ()
166 public static int CreateMask (int prev)
170 if (prev == Int32.MinValue)
171 throw new InvalidOperationException ("all bits set");
175 public static Section CreateSection (short maxValue)
177 return CreateSection (maxValue, new Section (0, 0));
180 public static Section CreateSection (short maxValue, BitVector32.Section previous)
183 throw new ArgumentException ("maxValue");
185 int bit = HighestSetBit(maxValue);
186 int mask = (1 << bit) - 1;
187 int offset = previous.Offset + HighestSetBit (previous.Mask);
189 if (offset + bit > 32) {
190 throw new ArgumentException ("Sections cannot exceed 32 bits in total");
193 return new Section ((short) mask, (short) offset);
196 public override bool Equals (object o)
198 return (o is BitVector32) && bits == ((BitVector32) o).bits;
201 public override int GetHashCode ()
203 return bits.GetHashCode ();
206 public override string ToString ()
208 return ToString (this);
211 public static string ToString (BitVector32 value)
213 StringBuilder b = new StringBuilder ();
214 b.Append ("BitVector32{");
215 long mask = (long) 0x80000000;
217 b.Append (((value.bits & mask) == 0) ? '0' : '1');
221 return b.ToString ();
225 private static int HighestSetBit (int i)
228 while(i >> count != 0)