Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BitArray.cs « System.Collections « corlib « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1fc6b156c0d35f9ef429ba6a77f98564d5811662 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
using System;

namespace System.Collections
{
  // do we really need to specify IEnumerable since ICollection extends it?
  [Serializable]
  public sealed class BitArray : ICollection, IEnumerable, ICloneable
  {
    private Int32[] m_array;
    private int m_length;
    private int m_modCount = 0;
    
    private static void clearJunk(Int32[] arr, int numbits)
    {
      int numjunkbits = 32 - (numbits%32);
      UInt32 mask = (~0U >> numjunkbits);
      arr[arr.Length - 1] &= (int)mask;
    }

    private static int bitsToInts(int bits)
    {
      int retval = bits/32;
      if (bits % 32 != 0)
	retval++;

      return retval;
    }

    private static int bitsToBytes(int bits)
    {
      int retval = bits/8;
      if (bits % 8 != 0)
	retval++;

      return retval;
    }


    private void setBit(int bitIndex, bool value)
    {
      int index = bitIndex/32;
      int shift = bitIndex%32;

      Int32 theBit = 1 << shift;

      if(value)
	m_array[index] |= theBit;
      else
	m_array[index] &= ~theBit;
      
      m_modCount++;
    }

    private bool getBit(int bitIndex)
    {
      int index = bitIndex/32;
      int shift = bitIndex%32;

      Int32 theBit = m_array[index] & (1 << shift);

      return (theBit == 0) ? false : true;
    }

    private byte getByte(int byteIndex)
    {
      int index = byteIndex/4;
      int shift = (byteIndex%4)*8;

      Int32 theByte = m_array[index] & (0xff << shift);

      return (byte)((theByte >> shift)&0xff);
    }

    private void setByte(int byteIndex, byte value)
    {
      int index = byteIndex/4;
      int shift = (byteIndex%4)*8;

      Int32 orig = m_array[index];

      // clear the byte
      orig &= ~(0xff << shift);
      // or in the new byte
      orig |= value << shift;
      
      m_array[index] = orig;

      m_modCount++;
    }

    /* --- Constructors --- */
    public BitArray(BitArray orig)
    {
      m_length = orig.m_length;
      
      int numInts = bitsToInts(m_length);
      m_array = new Int32[numInts];
      Array.Copy(orig.m_array, m_array, numInts);
    }

    public BitArray(bool[] bits)
    {
      m_length = bits.Length;

      int numInts = bitsToInts(m_length);
      m_array = new Int32[numInts];
      for (int i=0; i < bits.Length; i++)
	setBit(i, bits[i]);
    }

    public BitArray(byte[] bytes)
    {
      m_length = bytes.Length * 8;

      m_array = new Int32[bitsToInts(m_length)];
      for (int i=0; i < bytes.Length; i++)
	setByte(i, bytes[i]);
    }

    public BitArray(int capacity)
    {
      m_length = capacity;
      m_array = new Int32[bitsToInts(m_length)];
    }
    
    public BitArray(int[] words)
    {
      int arrlen = words.Length;
      m_length = arrlen*32;
      m_array = new Int32[arrlen];
      Array.Copy(words, m_array, arrlen);
    }
    
    public BitArray(int capacity, bool value) : this(capacity)
    {
      if (value)
      {
	// FIXME:  Maybe you can create an array pre filled?
	for (int i = 0; i < m_array.Length; i++)
	  m_array[i] = ~0;
      }
    }

    private BitArray(Int32 [] array, int length)
    {
      m_array = array;
      m_length = length;
    }
    

    /* --- Public properties --- */
    public int Count
    {
      get
      {
	return m_length;
      }
    }

    public bool IsReadOnly
    {
      get
      {
	return false;
      }
    }

    public bool IsSynchronized
    {
      get
      {
	return false;
      }
    }

    public bool this[int index]
    {
      get
      {
        return Get(index);
      }
      set
      {
        Set(index, value);
      }

    }

    public int Length
    {
      get
      {
	return m_length;
      }
      set
      {
	if (value < 0)
	  throw new ArgumentOutOfRangeException();

	int newLen = value;
	if (m_length != newLen)
	{
	  int numints = bitsToInts(newLen);
	  Int32 [] newArr = new Int32[numints];
	  int copylen = (numints > m_array.Length ? m_array.Length : numints);
	  Array.Copy(m_array, newArr, copylen);
	  
	  // clear out the junk bits at the end:
	  clearJunk(newArr, newLen);

	  // set the internal state
	  m_array = newArr;
	  m_length = newLen;
	  m_modCount++;
	}
      }
    }

    public object SyncRoot
    {
      get
      {
	return this;
      }
    }

    /* --- Public methods --- */
    public BitArray And(BitArray operand)
    {
      if (operand == null)
        throw new ArgumentNullException();
      if (operand.m_length != m_length)
        throw new ArgumentException();
      
      Int32 [] newarr = new Int32[m_array.Length];
      for (int i=0; i < m_array.Length; i++)
        newarr[i] = m_array[i] & operand.m_array[i];

      return new BitArray(newarr, m_length);
    }

    public object Clone()
    {
      // FIXME: according to the doc, this should be a shallow copy.
      // But the MS implementation seems to do a deep copy.
      return new BitArray((Int32 [])m_array.Clone(), m_length);
    }

    [MonoTODO]	 
    public void CopyTo(Array array, int index)
    {
      if (array == null)
	throw new ArgumentNullException();
      if (index < 0)
	throw new ArgumentOutOfRangeException();
      
      // FIXME: Throw ArgumentException if array is multidimensional
      if (index >= array.Length)
	throw new ArgumentException();

      // in each case, check to make sure enough space in array

      if (array is bool[])
      {
	if (index + m_length >= array.Length)
	  throw new ArgumentException();

	bool [] barray = (bool []) array;

	// Copy the bits into the array
	for (int i = 0; i < m_length; i++)
	  barray[index + i] = getBit(i);
      }
      else if (array is byte[])
      {
	int numbytes = bitsToBytes(m_length);
	if (index + numbytes >= array.Length)
	  throw new ArgumentException();

	byte [] barray = (byte []) array;
	// Copy the bytes into the array
	for (int i = 0; i < numbytes; i++)
	  barray[index + i] = getByte(i);
      }
      else if (array is int[])
      {
	int numints = bitsToInts(m_length);
	if (index + numints >= array.Length)
	  throw new ArgumentException();
	Array.Copy(m_array, 0, array, index, numints);
      }
      else
      {
	throw new InvalidCastException();
      }
    }

      
    /*
     * All this code for nothing... Apparently, The MS BitArray doesn't
     * override Equals!
     *public override bool Equals(object obj)
    {
      // If it's not a BitArray, then it can't be equal to us.
      if (!(obj is BitArray))
	return false;

      // If the references are equal, then clearly the instances are equal
      if (this == obj)
	return true;

      // If its length is different, than it can't be equal.
      BitArray b = (BitArray) obj;
      if (m_length != b.m_length)
	return false;


      // Now compare the bits.
      // This is a little tricky, because if length doesn't divide 32,
      // then we shouldn't compare the unused bits in the last element 
      // of m_array.

      // Compare all full ints.  If any differ, then we are not equal.
      int numints = m_length/32;
      for (int i = 0; i < numints; i++)
      {
	if (b.m_array[i] != m_array[i])
	  return false;
      }
      
      // Compare the left over bits (if any)
      int extrabits = m_length%32;
      if (extrabits != 0)
      {
	// our mask is the "extrabits" least significant bits set to 1.
	UInt32 comparemask = ~0U >> (32 - extrabits);

	// numints is rounded down, so it's safe to use as an index here,
	// as long as extrabits > 0.
	if ((b.m_array[numints] & comparemask) 
	    != (m_array[numints] & comparemask))
	  return false;
      }
      
      // We passed through all the above, so we are equal.
      return true;

    }
    *  End comment out of Equals()
    */

    public bool Get(int index)
    {
      if (index < 0 || index >= m_length)
        throw new ArgumentOutOfRangeException();
      return getBit(index);
    }

    public IEnumerator GetEnumerator()
    {      
      return new BitArrayEnumerator(this);
    }

    /*
     *  Since MS doesn't appear to override Equals/GetHashCode, we don't.
     *public override int GetHashCode()
    {
      // We could make this a constant time function 
      // by just picking a constant number of bits, spread out
      // evenly across the entire array.  For now, this will suffice.

      int retval = m_length;

      // Add in each array element, except for the leftover bits.
      int numints = m_length/32;
      for (int i = 0; i < numints; i++)
	retval += (int)m_array[i];

      // That's enough.  Adding in the leftover bits is tiring.

      return retval;
    }
    * End comment out of GetHashCode()
    */

    public BitArray Not()
    {
      Int32 [] newarr = new Int32[m_array.Length];
      for (int i=0; i < m_array.Length; i++)
        newarr[i] = ~m_array[i];

      return new BitArray(newarr, m_length);
    }

    public BitArray Or(BitArray operand)
    {
      if (operand == null)
        throw new ArgumentNullException();
      if (operand.m_length != m_length)
        throw new ArgumentException();
      
      Int32 [] newarr = new Int32[m_array.Length];
      for (int i=0; i < m_array.Length; i++)
        newarr[i] = m_array[i] | operand.m_array[i];

      return new BitArray(newarr, m_length);
    }

    public void Set(int index, bool value)
    {
      if (index < 0 || index >= m_length)
        throw new ArgumentOutOfRangeException();
      setBit(index, value);
    }

    public void SetAll(bool value)
    {
      if (value)
      {
	for (int i = 0; i < m_array.Length; i++)
	  m_array[i] = ~0;
	
	// clear out the junk bits that we might have set
	clearJunk(m_array, m_length);
      }
      else
	Array.Clear(m_array, 0, m_array.Length);


      m_modCount++;
    }
    
    public BitArray Xor(BitArray operand)
    {
      if (operand == null)
        throw new ArgumentNullException();
      if (operand.m_length != m_length)
        throw new ArgumentException();
      
      Int32 [] newarr = new Int32[m_array.Length];
      for (int i=0; i < m_array.Length; i++)
        newarr[i] = m_array[i] ^ operand.m_array[i];

      return new BitArray(newarr, m_length);
    }

    class BitArrayEnumerator : IEnumerator
    {
      BitArray m_bitArray;
      private bool m_current;
      private int m_index;
      private int m_max;
      private int m_modCount;
     
      public BitArrayEnumerator(BitArray ba)
      {
	m_index = -1;
	m_bitArray = ba;
	m_max = ba.m_length;
	m_modCount = ba.m_modCount;
      }
      
      public object Current
      {
	get
	{
	  if (m_index < 0 || m_index >= m_max)
	    throw new InvalidOperationException();
	  return m_current;
	}
      }
      
      public bool MoveNext()
      {
	if (m_modCount != m_bitArray.m_modCount)
	  throw new InvalidOperationException();
	
	if (m_index + 1 >= m_max)
	  return false;

	m_index++;
	m_current = m_bitArray[m_index];
	return true;
      }
      
      public void Reset()
      {
	if (m_modCount != m_bitArray.m_modCount)
	  throw new InvalidOperationException();
	m_index = -1;
      }
    }
  }
}