/// <summary> /// Check if two arrays of bits are equals /// Returns true if every bit of this first array is equal to the corresponding bit of the second, false otherwise /// </summary> public static bool Equals(BitArray a, BitArray b) { if (a.Length != b.Length) return false; var enumA = a.GetEnumerator(); var enumB = b.GetEnumerator(); while (enumA.MoveNext() && enumB.MoveNext()) { if ((bool)enumA.Current != (bool)enumB.Current) return false; } return true; }
static public int GetEnumerator(IntPtr l) { try { System.Collections.BitArray self = (System.Collections.BitArray)checkSelf(l); var ret = self.GetEnumerator(); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
/// <summary> /// Check if two arrays of bits are equals /// </summary> /// <param name="sourceData">Source data</param> /// <param name="targetData">Target data</param> /// <returns>Return whether </returns> public static bool Compare(this BitArray sourceData, BitArray targetData) { if (sourceData == null || targetData == null || sourceData.Length != targetData.Length) { return(false); } var sourceDataEnumerator = sourceData.GetEnumerator(); var targetDataEnumerator = targetData.GetEnumerator(); while (sourceDataEnumerator.MoveNext() && targetDataEnumerator.MoveNext()) { if ((bool)sourceDataEnumerator.Current != (bool)targetDataEnumerator.Current) { return(false); } } return(true); }
public void InsertMessage(string message) { Stopwatch watch = new Stopwatch(); watch.Start(); var dataToEncode = Encoding.ASCII.GetBytes(message); byte[] dataWithMeta = new byte[dataToEncode.Length + MessageLength + MessageLengthPadding]; //four bytes for message length; 2 bytes for padding var messageLengthInfo = BitConverter.GetBytes(message.Length); Array.Copy(messageLengthInfo, dataWithMeta, MessageLength); Array.Copy(dataToEncode, 0, dataWithMeta, MessageLength + MessageLengthPadding, dataToEncode.Length); BitArray bits = new BitArray(dataWithMeta); var e = bits.GetEnumerator(); var bmpPixelEnumerator = new BitmapPixelEnumerator(_img); foreach (Pixel pixel in bmpPixelEnumerator) { byte oldBlue = pixel.Blue; byte oldGreen = pixel.Green; byte oldRed = pixel.Red; byte newBlue; if (InsertData(e, oldBlue, out newBlue)) return; _copy.SetPixel(pixel.X, pixel.Y, Color.FromArgb(newBlue, oldGreen, oldRed)); byte newGreen; if (InsertData(e, oldGreen, out newGreen)) return; _copy.SetPixel(pixel.X, pixel.Y, Color.FromArgb(newBlue, newGreen, oldRed)); byte newRed; if (InsertData(e, oldRed, out newRed)) return; _copy.SetPixel(pixel.X, pixel.Y, Color.FromArgb(newBlue, newGreen, newRed)); } watch.Stop(); Debug.WriteLine(watch.Elapsed); }
public static void BitArray_GetEnumeratorTest_Negative() { int size = 10; Boolean[] bolArr1 = new Boolean[size]; for (int i = 0; i < size; i++) { if (i > 5) bolArr1[i] = true; else bolArr1[i] = false; } BitArray bitArr1 = new BitArray(bolArr1); IEnumerator ienm1 = bitArr1.GetEnumerator(); // test that initially enumerator is positioned before the first element in the collection --> Current will be undefined Assert.Throws<InvalidOperationException>(delegate { Object obj = ienm1.Current; }); //"Err_40! wrong exception thrown." // get to the end of the collection while (ienm1.MoveNext()) ; // test that after MoveNext() returns false (i.e. we are at the end) enumerator is positioned after the last element in the collection --> Current will be undefined Assert.Throws<InvalidOperationException>(delegate { Object obj = ienm1.Current; }); //"Err_41! wrong exception thrown." //[] we will change the underlying BitArray and see the effect ienm1.Reset(); ienm1.MoveNext(); bitArr1[0] = false; // we do not throw exception when getting Current Object obj2 = ienm1.Current; // test that the enumerator is not valid after modifying collection Assert.Throws<InvalidOperationException>(delegate { ienm1.MoveNext(); }); //"Err_42! wrong exception thrown." Assert.Throws<InvalidOperationException>(delegate { ienm1.Reset(); }); //"Err_43! wrong exception thrown." }
public static void BitArray_GetEnumeratorTest() { int size = 10; Boolean[] bolArr1 = new Boolean[size]; for (int i = 0; i < size; i++) { if (i > 5) bolArr1[i] = true; else bolArr1[i] = false; } BitArray bitArr1 = new BitArray(bolArr1); IEnumerator ienm1 = bitArr1.GetEnumerator(); int iCount = 0; while (ienm1.MoveNext()) { Assert.Equal((Boolean)ienm1.Current, bolArr1[iCount++]); //"Err_26! wrong value returned" } ienm1.Reset(); iCount = 0; while (ienm1.MoveNext()) { Assert.Equal((Boolean)ienm1.Current, bolArr1[iCount++]); //"Err_27! wrong value returned" } }
/// <summary> Zjistí, jestli stejnì dlouhé BitArrays mají nastavené stejné bity. </summary> /// <param name="bits1"> 1. BitArray. </param> /// <param name="bits2"> 2. BitArray. </param> /// <returns> true, když mají nastavené stejné bity. </returns> public static bool BitArraysEqual ( BitArray bits1, BitArray bits2 ) { if ( bits1.Count != bits2.Count ) throw new Exception( string.Format( "OtherCore.BitArraysEqual: different BitArray sizes {0} != {1}", bits1.Count != bits2.Count ) ); return EnumerablesAreSame( bits1.GetEnumerator(), bits2.GetEnumerator() ); // // int[] array1 = new int[ ( bits1.Count + 31 ) / 32 ]; // bits1.CopyTo( array1, 0 ); // int[] array2 = new int[ array1.Length ]; // bits2.CopyTo( array2, 0 ); // // for ( int index = array1.Length ; --index >= 0 ; ) // if ( array1[ index ] != array2[ index ] ) // return false; // // return true; }