(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System / System.Collections.Specialized / BitVector32.cs
blobce3672c5fb2b3da3949b91b80e73bd63be42119c
1 //
2 // System.Collections.Specialized.BitVector32.cs
3 //
4 // Author:
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)
9 //
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:
21 //
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 //
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.
34 using System;
35 using System.Text;
37 namespace System.Collections.Specialized {
39 public struct BitVector32 {
40 int bits;
42 public struct Section {
43 private short mask;
44 private short offset;
46 internal Section (short mask, short offset) {
47 this.mask = mask;
48 this.offset = offset;
51 public short Mask {
52 get { return mask; }
55 public short Offset {
56 get { return offset; }
59 public override bool Equals (object o)
61 if (! (o is Section))
62 return false;
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));
86 b.Append (", 0x");
87 b.Append (Convert.ToString(value.Offset,16));
88 b.Append ("}");
90 return b.ToString ();
94 // Constructors
96 public BitVector32 (BitVector32 source)
98 bits = source.bits;
101 public BitVector32 (int init)
103 bits = init;
106 // Properties
108 public int Data {
109 get { return bits; }
112 public int this [BitVector32.Section section] {
113 get {
114 return ((bits >> section.Offset) & section.Mask);
117 set {
118 if (value < 0)
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] {
128 get {
129 long tmp = (uint)bits;
130 return (tmp & (long)mask) == (long)mask;
133 set {
134 if (value)
135 bits |= mask;
136 else
137 bits &= ~mask;
141 // Methods
143 public static int CreateMask ()
145 return 1;
148 public static int CreateMask (int prev)
150 if (prev == 0)
151 return 1;
152 if (prev == Int32.MinValue)
153 throw new InvalidOperationException ("all bits set");
154 return prev << 1;
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)
164 if (maxValue < 1)
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);
171 if (offset > 32) {
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))
181 return false;
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;
201 while (mask > 0) {
202 b.Append (((value.bits & mask) == 0) ? '0' : '1');
203 mask >>= 1;
205 b.Append ('}');
206 return b.ToString ();
209 // Private utilities
210 private static int NumberOfSetBits (int i)
212 int count = 0;
213 for (int bit = 0; bit < 32; bit++) {
214 int mask = 1 << bit;
215 if ((i & mask) != 0)
216 count++;
218 return count;
221 private static int HighestSetBit (int i)
223 for (int bit = 31; bit >= 0; bit--) {
224 int mask = 1 << bit;
225 if ((mask & i) != 0) {
226 return bit;
230 return -1;