| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- namespace cyh.game
- {
- public class BitArray : IList<bool>
- {
- private byte[] array;
- private ulong count;
- public int Count { get { return (int)this.count; } }
- bool ICollection<bool>.IsReadOnly => false;
- public BitArray()
- {
- this.array = Array.Empty<byte>();
- this.count = 0;
- }
- public BitArray(int capacity)
- {
- this.array = new byte[capacity];
- this.count = 0;
- }
- private void resize(int minsize)
- {
- if (minsize == 0)
- {
- if (this.array.Length == 0)
- this.array = new byte[4];
- else
- Array.Resize<byte>(ref this.array, this.array.Length << 1);
- }
- else if (minsize > this.array.Length)
- {
- Array.Resize<byte>(ref this.array, minsize);
- }
- }
- public bool this[int index]
- {
- get
- {
- if (index < 0)
- throw new ArgumentOutOfRangeException();
- int offset = index & 7;
- index >>= 3;
- return (this.array[index] & (1 << offset)) != 0;
- }
- set
- {
- int offset = index & 7;
- if ((ulong)index >= this.count)
- {
- this.count = (ulong)index + 1;
- index >>= 3;
- if (index >= this.array.Length)
- this.resize(index + 1);
- }
- else
- index >>= 3;
- if (value)
- this.array[index] |= (byte)(1 << offset);
- else
- this.array[index] &= (byte)~(1 << offset);
- }
- }
- public bool this[ulong index]
- {
- get
- {
- int offset = (int)(index & 7);
- index >>= 3;
- return (this.array[index] & (1 << offset)) != 0;
- }
- set
- {
- int offset = (int)(index & 7);
- if ((ulong)index >= this.count)
- {
- this.count = (ulong)index + 1;
- index >>= 3;
- if (index >= (ulong)this.array.Length)
- this.resize((int)index + 1);
- }
- else
- index >>= 3;
- if (value)
- this.array[index] |= (byte)(1 << offset);
- else
- this.array[index] &= (byte)~(1 << offset);
- }
- }
- public void Add(bool bit)
- {
- this.count++;
- byte offset = (byte)(this.count & 7);
- int index = (int)(this.count >> 3);
- if (index >= this.array.Length)
- this.resize(0);
- if (bit)
- this.array[index] |= (byte)(1 << offset);
- else
- this.array[index] &= (byte)~(1 << offset);
- }
- public override string ToString()
- {
- System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder((int)this.count);
- for (ulong i = 0; i < this.count; i++)
- {
- stringBuilder.Append(this[i] ? '1' : '0');
- }
- return stringBuilder.ToString();
- }
- int IList<bool>.IndexOf(bool item)
- {
- throw new NotSupportedException();
- }
- void IList<bool>.Insert(int index, bool item)
- {
- throw new NotSupportedException();
- }
- void IList<bool>.RemoveAt(int index)
- {
- throw new NotSupportedException();
- }
- void ICollection<bool>.Clear()
- {
- this.array = Array.Empty<byte>();
- this.count = 0;
- }
- bool ICollection<bool>.Contains(bool item)
- {
- throw new NotSupportedException();
- }
- void ICollection<bool>.CopyTo(bool[] array, int arrayIndex)
- {
- for (ulong i = 0; i < this.count; i++)
- {
- array[arrayIndex++] = this[i];
- }
- }
- bool ICollection<bool>.Remove(bool item)
- {
- throw new NotSupportedException();
- }
- public IEnumerator<bool> GetEnumerator()
- {
- return new Enumerator(this);
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return this.GetEnumerator();
- }
- class Enumerator : IEnumerator<bool>
- {
- private BitArray Array;
- private long index = -1;
- public Enumerator(BitArray array)
- {
- this.Array = array;
- }
- public bool Current { get { return this.Array[(ulong)this.index]; } }
- object IEnumerator.Current { get { return this.Array[(ulong)this.index]; } }
- public void Dispose()
- {
- this.index = -1;
- }
- public bool MoveNext()
- {
- this.index++;
- return this.index < (long)this.Array.count;
- }
- public void Reset()
- {
- this.index = -1;
- }
- }
- public static void Set(byte[] data, int index, bool value)
- {
- if (index < 0)
- throw new ArgumentOutOfRangeException();
- int offset = index & 7;
- index >>= 3;
- if (index >= data.Length)
- throw new ArgumentOutOfRangeException();
- if (value)
- data[index] |= (byte)(1 << offset);
- else
- data[index] &= (byte)~(1 << offset);
- }
- public static bool Get(byte[] data, int index)
- {
- int offset = index & 7;
- index >>= 3;
- if (index >= data.Length)
- throw new ArgumentOutOfRangeException();
- return (data[index] & (1 << offset)) != 0;
- }
- }
- }
|