| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- //
- // System.Collections.Specialized.BitVector32.cs
- //
- // Author:
- // Miguel de Icaza ([email protected])
- // Lawrence Pit ([email protected])
- //
- // (C) Ximian, Inc. http://www.ximian.com
- //
- using System;
- using System.Text;
- namespace System.Collections.Specialized {
-
- public struct BitVector32 {
- int value;
- public struct Section {
- private short mask;
- private 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 false;
-
- Section section = (Section) o;
- return this.mask == section.mask &&
- this.offset == section.offset;
- }
-
- public override int GetHashCode ()
- {
- return (((Int16) mask).GetHashCode () << 16) +
- ((Int16) offset).GetHashCode ();
- }
-
- public override string ToString ()
- {
- return "Section{0x" + Convert.ToString(mask, 16) +
- ", 0x" + Convert.ToString(offset, 16) + "}";
- }
- }
-
- // Constructors
-
- public BitVector32 (BitVector32 source)
- {
- value = source.value;
- }
- public BitVector32 (int init)
- {
- value = init;
- }
-
- // Properties
-
- public int Data {
- get { return value; }
- }
-
- [MonoTODO]
- public int this [BitVector32.Section section] {
- get { return ((this.value >> section.Offset) & section.Mask); }
- set {
- throw new NotImplementedException ();
- }
- }
-
- public bool this [int bit] {
- get { return (value & bit) == bit; }
- set {
- if (value)
- this.value |= bit;
- else
- this.value &= ~bit;
- }
- }
-
- // Methods
-
- public static int CreateMask ()
- {
- return 1;
- }
- public static int CreateMask (int prev)
- {
- if (prev == 0)
- return 1;
- if (prev == Int32.MinValue)
- throw new InvalidOperationException ("all bits set");
- return prev << 1;
- }
- public static Section CreateSection (short maxValue)
- {
- return CreateSection (maxValue, new Section (0, 0));
- }
-
- public static Section CreateSection (short maxValue, BitVector32.Section previous)
- {
- if (maxValue < 1)
- throw new ArgumentException ("maxValue");
-
- int newmask = (int) maxValue;
- int mask = 0x8000;
- while ((newmask & mask) == 0)
- mask >>= 1;
- while (mask > 0) {
- newmask |= mask;
- mask >>= 1;
- }
- short count = 0;
- int prev = previous.Mask;
- mask = 0x8000;
- while (mask > 0) {
- if ((prev & mask) != 0)
- count++;
- mask >>= 1;
- }
- return new Section ((short) newmask, (short) (previous.Offset + count));
- }
-
- public override bool Equals (object o)
- {
- if (!(o is BitVector32))
- return false;
- return value == ((BitVector32) o).value;
- }
- public override int GetHashCode ()
- {
- return value.GetHashCode ();
- }
-
- public override string ToString ()
- {
- return ToString (this);
- }
-
- public static string ToString (BitVector32 value)
- {
- long val = (long) value.value;
- StringBuilder b = new StringBuilder ();
- b.Append ("BitVector32{");
- long mask = (long) 0x80000000;
- while (mask > 0) {
- b.Append (((val & mask) == 0) ? '0' : '1');
- mask >>= 1;
- }
- b.Append ('}');
- return b.ToString ();
- }
- }
- }
|