forked from TheAlgorithms/C-Sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitArray.cs
919 lines (816 loc) · 28 KB
/
BitArray.cs
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
// Original Author: Christian Bender
// Class: BitArray
//
// implements IComparable, ICloneable, IEnumerator, IEnumerable
//
// This class implements a bit-array and provides some
// useful functions/operations to deal with this type of
// data structure. You see a overview about the functionality, below.
//
//
// Overview
//
// Constructor (N : int)
// The constructor receives a length (N) of the to create bit-field.
//
// Constructor (sequence : string)
// setups the array with the input sequence.
// assumes: the sequence may only be allowed contains onese or zeros.
//
// Constructor (bits : bool[] )
// setups the bit-field with the input array.
//
// Compile(sequence : string)
// compiles a string sequence of 0's and 1's in the inner structure.
// assumes: the sequence may only be allowed contains onese or zeros.
//
// Compile (number : int)
// compiles a positive integer number in the inner data structure.
//
// Compile (number : long)
// compiles a positive long integer number in the inner data structure.
//
// ToString ()
// returns a string representation of the inner structure.
// The returned string is a sequence of 0's and 1's.
//
// Length : int
// Is a property that returns the length of the bit-field.
//
// Indexer : bool
// indexer for selecting the individual bits of the bit array.
//
// NumberOfOneBits() : int
// returns the number of One-bits.
//
// NumberOfZeroBits() : int
// returns the number of Zero-Bits.
//
// EvenParity() : bool
// returns true if parity is even, otherwise false.
//
// OddParity() : bool
// returns true if parity is odd, otherwise false.
//
// ToInt64() : long
// returns a long integer representation of the bit-array.
// assumes: the bit-array length must been smaller or equal to 64 bit.
//
// ToInt32() : int
// returns a integer representation of the bit-array.
// assumes: the bit-array length must been smaller or equal to 32 bit.
//
// ResetField() : void
// sets all bits on false.
//
// SetAll(flag : bool) : void
// sets all bits on the value of the flag.
//
// GetHashCode() : int
// returns hash-code (ToInt32())
//
// Equals (other : Object) : bool
// returns true if there inputs are equal otherwise false.
// assumes: the input bit-arrays must have same length.
//
// CompareTo (other : Object) : int (interface IComparable)
// output: 0 - if the bit-arrays a equal.
// -1 - if this bit-array is smaller.
// 1 - if this bit-array is greater.
// assumes: bit-array lentgh must been smaller or equal to 64 bit
//
// Clone () : object
// returns a copy of this bit-array
//
// Current : object
// returns the current selected bit.
//
// MoveNext() : bool
// purpose: increases the position of the enumerator
// returns true if 'position' successful increased otherwise false.
//
// Reset() : void
// resets the position of the enumerator.
//
// GetEnumerator() : IEnumerator
// returns a enumerator for this BitArray-object.
//
// Operations:
//
// & bitwise AND
// | bitwise OR
// ~ bitwise NOT
// >> bitwise shift right
// >> bitwise shift left
// ^ bitwise XOR
//
// Each operation (above) returns a new BitArray-object.
//
// == equal operator. : bool
// returns true if there inputs are equal otherwise false.
// assumes: the input bit-arrays must have same length.
//
// != not-equal operator : bool
// returns true if there inputs aren't equal otherwise false.
// assumes: the input bit-arrays must have same length.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DataStructures
{
/// <summary>
/// This class implements a bit-array and provides some
/// useful functions/operations to deal with this type of
/// data structure.
/// </summary>
public sealed class BitArray : ICloneable, IEnumerator<bool>, IEnumerable<bool>
{
private readonly bool[] field; // the actual bit-field
private int position = -1; // position for enumerator
/// <summary>
/// Initializes a new instance of the <see cref="BitArray"/> class.
/// setups the array with false-values.
/// </summary>
/// <param name="n">length of the array.</param>
public BitArray(int n)
{
if (n < 1)
{
field = new bool[0];
}
field = new bool[n];
// fills up the field with zero-bits.
for (var i = 0; i < n; i++)
{
field[i] = false;
}
}
/// <summary>
/// Initializes a new instance of the <see cref="BitArray"/> class.
/// Setups the array with the input sequence.
///
/// purpose: Setups the array with the input sequence.
/// assumes: sequence must been greater or equal to 1.
/// the sequence may only be allowed contains onese or zeros.
/// </summary>
/// <param name="sequence">A string sequence of 0's and 1's.</param>
public BitArray(string sequence)
{
// precondition I
if (sequence.Length > 0)
{
// precondition II
if (Match(sequence))
{
field = new bool[sequence.Length];
Compile(sequence);
}
else
{
// error case II
throw new Exception("BitArray: the sequence may only " +
"be allowed contains onese or zeros.");
}
}
else
{
// error case I
throw new Exception("BitArray: sequence must been greater or equal as 1");
}
}
/// <summary>
/// Initializes a new instance of the <see cref="BitArray"/> class.
/// Setups the bit-array with the input array.
/// </summary>
/// <param name="bits">A boolean array of bits.</param>
public BitArray(bool[] bits) => field = bits;
/// <summary>
/// Gets a value indicating whether the current bit of the array is set.
/// </summary>
public bool Current
{
get
{
try
{
return field[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
/// <summary>
/// Gets a value indicating whether the current bit of the array is set.
/// </summary>
object IEnumerator.Current
{
get
{
try
{
return field[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
/// <summary>
/// Gets the length of the current bit array.
/// </summary>
private int Length => field.Length;
/// <summary>
/// Gets element given an offset.
/// </summary>
/// <param name="offset">Position.</param>
/// <returns>Element on array.</returns>
public bool this[int offset]
{
get => field[offset];
private set => field[offset] = value;
}
/// <summary>
/// Returns a bit-array that represents the bit by bit AND (&).
/// Assumes arrays have the same length.
/// </summary>
/// <param name="one">First bit-array.</param>
/// <param name="two">Second bit-array.</param>
/// <returns>bit-array.</returns>
public static BitArray operator &(BitArray one, BitArray two)
{
var sequence1 = one.ToString();
var sequence2 = two.ToString();
var result = string.Empty;
var tmp = string.Empty;
// for scaling of same length.
if (one.Length != two.Length)
{
int difference;
if (one.Length > two.Length)
{
// one is greater
difference = one.Length - two.Length;
// fills up with 0's
for (var i = 0; i < difference; i++)
{
tmp += '0';
}
tmp += two.ToString();
sequence2 = tmp;
}
else
{
// two is greater
difference = two.Length - one.Length;
// fills up with 0's
for (var i = 0; i < difference; i++)
{
tmp += '0';
}
tmp += one.ToString();
sequence1 = tmp;
}
} // end scaling
var len = one.Length > two.Length ? one.Length : two.Length;
var ans = new BitArray(len);
for (var i = 0; i < one.Length; i++)
{
result += sequence1[i].Equals('1') && sequence2[i].Equals('1') ? '1' : '0';
}
result = result.Trim();
ans.Compile(result);
return ans;
}
/// <summary>
/// Returns a bit-array that represents the bit by bit OR.
/// Assumes arrays have the same length.
/// </summary>
/// <param name="one">First bit-array.</param>
/// <param name="two">Second bit-array.</param>
/// <returns>bit-array that represents the bit by bit OR.</returns>
public static BitArray operator |(BitArray one, BitArray two)
{
var sequence1 = one.ToString();
var sequence2 = two.ToString();
var result = string.Empty;
var tmp = string.Empty;
// for scaling of same length.
if (one.Length != two.Length)
{
int difference;
if (one.Length > two.Length)
{
// one is greater
difference = one.Length - two.Length;
// fills up with 0's
for (var i = 0; i < difference; i++)
{
tmp += '0';
}
tmp += two.ToString();
sequence2 = tmp;
}
else
{
// two is greater
difference = two.Length - one.Length;
// fills up with 0's
for (var i = 0; i < difference; i++)
{
tmp += '0';
}
tmp += one.ToString();
sequence1 = tmp;
}
} // end scaling
var len = one.Length > two.Length ? one.Length : two.Length;
var ans = new BitArray(len);
for (var i = 0; i < len; i++)
{
result += sequence1[i].Equals('0') && sequence2[i].Equals('0') ? '0' : '1';
}
result = result.Trim();
ans.Compile(result);
return ans;
}
/// <summary>
/// Returns a bit-array that represents the operator ~ (NOT).
/// Assumes arrays have the same length.
/// </summary>
/// <param name="one">Bit-array.</param>
/// <returns>bitwise not.</returns>
public static BitArray operator ~(BitArray one)
{
var ans = new BitArray(one.Length);
var sequence = one.ToString();
var result = string.Empty;
foreach (var ch in sequence)
{
if (ch == '1')
{
result += '0';
}
else
{
result += '1';
}
}
result = result.Trim();
ans.Compile(result);
return ans;
}
/// <summary>
/// Returns a bit-array that represents bitwise shift left (>>).
/// Assumes arrays have the same length.
/// </summary>
/// <param name="other">Bit-array.</param>
/// <param name="n">Number of bits.</param>
/// <returns>Bitwise shifted BitArray.</returns>
public static BitArray operator <<(BitArray other, int n)
{
var ans = new BitArray(other.Length + n);
// actual shifting process
for (var i = 0; i < other.Length; i++)
{
ans[i] = other[i];
}
return ans;
}
/// <summary>
/// Returns a bit-array that represents the bit by bit XOR.
/// Assumes arrays have the same length.
/// </summary>
/// <param name="one">First bit-array.</param>
/// <param name="two">Second bit-array.</param>
/// <returns>bit-array.</returns>
public static BitArray operator ^(BitArray one, BitArray two)
{
var sequence1 = one.ToString();
var sequence2 = two.ToString();
var tmp = string.Empty;
// for scaling of same length.
if (one.Length != two.Length)
{
int difference;
if (one.Length > two.Length)
{
// one is greater
difference = one.Length - two.Length;
// fills up with 0's
for (var i = 0; i < difference; i++)
{
tmp += '0';
}
tmp += two.ToString();
sequence2 = tmp;
}
else
{
// two is greater
difference = two.Length - one.Length;
// fills up with 0's
for (var i = 0; i < difference; i++)
{
tmp += '0';
}
tmp += one.ToString();
sequence1 = tmp;
}
} // end scaling
var len = one.Length > two.Length ? one.Length : two.Length;
var ans = new BitArray(len);
var sb = new StringBuilder();
for (var i = 0; i < len; i++)
{
_ = sb.Append(sequence1[i] == sequence2[i] ? '0' : '1');
}
var result = sb.ToString().Trim();
ans.Compile(result);
return ans;
}
/// <summary>
/// Returns a bit-array that represents bitwise shift right (>>).
/// Assumes arrays have the same length.
/// </summary>
/// <param name="other">Bit-array.</param>
/// <param name="n">Number of bits.</param>
/// <returns>Bitwise shifted BitArray.</returns>
public static BitArray operator >>(BitArray other, int n)
{
var ans = new BitArray(other.Length - n);
// actual shifting process.
for (var i = 0; i < other.Length - n; i++)
{
ans[i] = other[i];
}
return ans;
}
/// <summary>
/// Checks if both arrays are == (equal).
/// The input assumes arrays have the same length.
/// </summary>
/// <param name="one">First bit-array.</param>
/// <param name="two">Second bit-array.</param>
/// <returns>Returns True if there inputs are equal; False otherwise.</returns>
public static bool operator ==(BitArray one, BitArray two)
{
if (ReferenceEquals(one, two))
{
return true;
}
if (one is null || two is null)
{
return false;
}
if (one.Length != two.Length)
{
return false;
}
var status = true;
for (var i = 0; i < one.Length; i++)
{
if (one[i] != two[i])
{
status = false;
break;
}
}
return status;
}
/// <summary>
/// Checks if both arrays are != (not-equal).
/// The input assumes arrays have the same length.
/// </summary>
/// <param name="one">First bit-array.</param>
/// <param name="two">Second bit-array.</param>
/// <returns>Returns True if there inputs aren't equal; False otherwise.</returns>
public static bool operator !=(BitArray one, BitArray two) => !(one == two);
/// <summary>
/// Returns a copy of the current bit-array.
/// </summary>
/// <returns>Bit-array clone.</returns>
public object Clone()
{
var theClone = new BitArray(Length);
for (var i = 0; i < Length; i++)
{
theClone[i] = field[i];
}
return theClone;
}
/// <summary>
/// Gets a enumerator for this BitArray-Object.
/// </summary>
/// <returns>Returns a enumerator for this BitArray-Object.</returns>
public IEnumerator<bool> GetEnumerator() => this;
/// <summary>
/// Gets a enumerator for this BitArray-Object.
/// </summary>
/// <returns>Returns a enumerator for this BitArray-Object.</returns>
IEnumerator IEnumerable.GetEnumerator() => this;
/// <summary>
/// MoveNext (for interface IEnumerator).
/// </summary>
/// <returns>Returns True if 'position' successful increased; False otherwise.</returns>
public bool MoveNext()
{
if (position + 1 >= field.Length)
{
return false;
}
position++;
return true;
}
/// <summary>
/// Resets the position of the enumerator.
/// Reset (for interface IEnumerator).
/// </summary>
public void Reset() => position = -1;
/// <summary>
/// Compiles the binary sequence into the inner data structure.
/// The sequence must have the same length, as the bit-array.
/// The sequence may only be allowed contains onese or zeros.
/// </summary>
/// <param name="sequence">A string sequence of 0's and 1's.</param>
public void Compile(string sequence)
{
var tmp = string.Empty;
sequence = sequence.Trim();
// precondition I
if (sequence.Length <= field.Length)
{
// precondition II
if (Match(sequence))
{
// for appropriate scaling
if (sequence.Length < field.Length)
{
var difference = field.Length - sequence.Length;
for (var i = 0; i < difference; i++)
{
tmp += '0';
}
tmp += sequence;
sequence = tmp;
}
// actual compile procedure.
for (var i = 0; i < sequence.Length; i++)
{
field[i] = sequence[i] == '1';
}
}
else
{
// error case II
throw new Exception("Compile: the sequence may only " +
"be allowed contains onese or zeros.");
}
}
else
{
// error case I
throw new Exception("Compile: not equal length!");
}
}
/// <summary>
/// Compiles integer number into the inner data structure.
/// Assumes: the number must have the same bit length.
/// </summary>
/// <param name="number">A positive integer number.</param>
public void Compile(int number)
{
var tmp = string.Empty;
// precondition I
if (number > 0)
{
// converts to binary representation
var binaryNumber = Convert.ToString(number, 2);
// precondition II
if (binaryNumber.Length <= field.Length)
{
// for appropriate scaling
if (binaryNumber.Length < field.Length)
{
var difference = field.Length - binaryNumber.Length;
for (var i = 0; i < difference; i++)
{
tmp += '0';
}
tmp += binaryNumber;
binaryNumber = tmp;
}
// actual compile procedure.
for (var i = 0; i < binaryNumber.Length; i++)
{
field[i] = binaryNumber[i] == '1';
}
}
else
{
// error case II
throw new Exception("Compile: not apt length!");
}
}
else
{
// error case I
throw new Exception("Compile: only positive numbers > 0");
}
}
/// <summary>
/// Compiles integer number into the inner data structure.
/// The number must have the same bit length.
/// </summary>
/// <param name="number">A positive long integer number.</param>
public void Compile(long number)
{
var tmp = string.Empty;
// precondition I
if (number > 0)
{
// converts to binary representation
var binaryNumber = Convert.ToString(number, 2);
// precondition II
if (binaryNumber.Length <= field.Length)
{
// for appropriate scaling
if (binaryNumber.Length < field.Length)
{
var difference = field.Length - binaryNumber.Length;
for (var i = 0; i < difference; i++)
{
tmp += '0';
}
tmp += binaryNumber;
binaryNumber = tmp;
}
// actual compile procedure.
for (var i = 0; i < binaryNumber.Length; i++)
{
field[i] = binaryNumber[i] == '1';
}
}
else
{
// error case II
throw new Exception("Compile: not apt length!");
}
}
else
{
// error case I
throw new Exception("Compile: only positive numbers > 0");
}
}
/// <summary>
/// Is the opposit of the Compile(...) method.
/// </summary>
/// <returns>Returns a string representation of the inner data structure.</returns>
public override string ToString()
{
// creates return-string
return field.Aggregate(string.Empty, (current, t) => current + (t ? "1" : "0"));
}
/// <summary>
/// Gets the number of one-bits in the field.
/// </summary>
/// <returns>quantity of bits in current bit-array.</returns>
public int NumberOfOneBits()
{
// counting one-bits.
return field.Count(bit => bit);
}
/// <summary>
/// Gets the number of zero-bits in the field.
/// </summary>
/// <returns>quantity of bits.</returns>
public int NumberOfZeroBits()
{
// counting zero-bits
return field.Count(bit => !bit);
}
/// <summary>
/// To check for even parity.
/// </summary>
/// <returns>Returns True if parity is even; False otherwise.</returns>
public bool EvenParity() => NumberOfOneBits() % 2 == 0;
/// <summary>
/// To check for odd parity.
/// </summary>
/// <returns>Returns True if parity is odd; False otherwise.</returns>
public bool OddParity() => NumberOfOneBits() % 2 != 0;
/// <summary>
/// Returns a long integer representation of the bit-array.
/// Assumes the bit-array length must been smaller or equal to 64 bit.
/// </summary>
/// <returns>Long integer array.</returns>
public long ToInt64()
{
// Precondition
if (field.Length > 64)
{
throw new Exception("ToInt: field is too long.");
}
var sequence = ToString();
return Convert.ToInt64(sequence, 2);
}
/// <summary>
/// Returns a long integer representation of the bit-array.
/// Assumes the bit-array length must been smaller or equal to 32 bit.
/// </summary>
/// <returns>integer array.</returns>
public int ToInt32()
{
// Precondition
if (field.Length > 32)
{
throw new Exception("ToInt: field is too long.");
}
var sequence = ToString();
return Convert.ToInt32(sequence, 2);
}
/// <summary>
/// Sets all bits on false.
/// </summary>
public void ResetField()
{
for (var i = 0; i < field.Length; i++)
{
field[i] = false;
}
}
/// <summary>
/// Sets all bits on the value of the flag.
/// </summary>
/// <param name="flag">Bollean flag (false-true).</param>
public void SetAll(bool flag)
{
for (var i = 0; i < field.Length; i++)
{
field[i] = flag;
}
}
/// <summary>
/// Checks if bit-array are equal.
/// Assumes the input bit-arrays must have same length.
/// </summary>
/// <param name="other">Bit-array object.</param>
/// <returns>Returns true if there inputs are equal otherwise false.</returns>
public override bool Equals(object other)
{
var status = true;
var otherBitArray = (BitArray)other;
if (Length == otherBitArray?.Length)
{
for (var i = 0; i < Length; i++)
{
if (field[i] != otherBitArray[i])
{
status = false;
}
}
}
else
{
throw new Exception("== : inputs haven't same length!");
}
return status;
}
/// <summary>
/// Gets has-code of bit-array.
/// Assumes bit-array lentgh must been smaller or equal to 32.
/// </summary>
/// <returns>hash-code for this BitArray instance.</returns>
public override int GetHashCode() => ToInt32();
/// <summary>
/// Disposes object, nothing to dispose here though.
/// </summary>
public void Dispose()
{
// Done
}
/// <summary>
/// Utility method foir checking a given sequence contains only zeros and ones.
/// This method will used in Constructor (sequence : string) and Compile(sequence : string).
/// </summary>
/// <param name="sequence">String sequence.</param>
/// <returns>returns True if sequence contains only zeros and ones; False otherwise.</returns>
private static bool Match(string sequence)
{
var status = true;
foreach (var ch in sequence)
{
if (ch != '0' && ch != '1')
{
status = false;
}
}
return status;
}
}
}