Beispiel #1
0
 public static void Main(string[] args)
 {
     var ewahBitmap1 = new EwahCompressedBitArray();
     var ewahBitmap2 = new EwahCompressedBitArray();
     ewahBitmap1.Set(0);
     ewahBitmap1.Set(2);
     ewahBitmap1.Set(64);
     ewahBitmap1.Set(1 << 30);
     Console.WriteLine("Running demo program:");
     Console.WriteLine("bitmap 1:");
     foreach (int k in ewahBitmap1)
         Console.WriteLine(k);
     ewahBitmap2.Set(1);
     ewahBitmap2.Set(3);
     ewahBitmap2.Set(64);
     ewahBitmap2.Set(1 << 30);
     Console.WriteLine("bitmap 2:");
     foreach (int k in ewahBitmap2)
         Console.WriteLine(k);
     Console.WriteLine();
     Console.WriteLine("bitmap 1 OR bitmap 2:");
     EwahCompressedBitArray orbitmap = ewahBitmap1.Or(ewahBitmap2);
     foreach (int k in orbitmap)
         Console.WriteLine(k);
     Console.WriteLine("memory usage: " + orbitmap.SizeInBytes + " bytes");
     Console.WriteLine();
     Console.WriteLine("bitmap 1 AND bitmap 2:");
     EwahCompressedBitArray andbitmap = ewahBitmap1.And(ewahBitmap2);
     foreach (int k in andbitmap)
         Console.WriteLine(k);
     Console.WriteLine("memory usage: " + andbitmap.SizeInBytes + " bytes");
     Console.WriteLine("bitmap 1 XOR bitmap 2:");
     EwahCompressedBitArray xorbitmap = ewahBitmap1.Xor(ewahBitmap2);
     foreach (int k in xorbitmap)
         Console.WriteLine(k);
     Console.WriteLine("memory usage: " + andbitmap.SizeInBytes + " bytes");
     Console.WriteLine("End of demo.");
     Console.WriteLine("");
     var tr = new EwahCompressedBitArrayTest();
     tr.TestNot();
     tr.TestCardinality();
     tr.TestEwahCompressedBitArray();
     tr.TestExternalization();
     tr.TestLargeEwahCompressedBitArray();
     tr.TestMassiveAnd();
     tr.TestMassiveAndNot();
     tr.TestMassiveOr();
     tr.TestMassiveXOR();
     tr.HabermaasTest();
     tr.VanSchaikTest();
 }
        public void TestMassiveXOR()
        {
            Console.WriteLine("testing massive xor (can take a couple of minutes)");
            int N    = 16;
            var ewah = new EwahCompressedBitArray[N];
            var bset = new BitArray[N];

            for (int k = 0; k < ewah.Length; ++k)
            {
                ewah[k] = new EwahCompressedBitArray();
            }
            for (int k = 0; k < bset.Length; ++k)
            {
                bset[k] = new BitArray(30000);
            }
            for (int k = 0; k < 30000; ++k)
            {
                ewah[(k + 2 * k * k) % ewah.Length].Set(k);
                bset[(k + 2 * k * k) % ewah.Length].Set(k, true);
            }
            EwahCompressedBitArray answer = ewah[0];
            BitArray BitArrayanswer       = bset[0];

            for (int k = 1; k < ewah.Length; ++k)
            {
                answer = answer.Xor(ewah[k]);
                BitArrayanswer.Xor(bset[k]);
                assertEqualsPositions(BitArrayanswer, answer);
            }
            int k2 = 0;

            foreach (int j in answer)
            {
                if (k2 != j)
                {
                    Console.WriteLine(answer.ToDebugString());
                }
                Assert.AreEqual(k2, j);
                k2 += 1;
            }
            Console.WriteLine("testing massive xor:ok");
        }
        /**
         * a non-deterministic test proposed by Marc Polizzi.
         *
         * @param maxlength the maximum uncompressed size of the bitmap
         */

        public static void PolizziTest(int maxlength)
        {
            Console.WriteLine("Polizzi test with max length = " + maxlength);
            for (int k = 0; k < 10000; k += 77)
            {
                var rnd          = new Random();
                var ewahBitmap1  = new EwahCompressedBitArray();
                var clrBitArray1 = new BitArray(10000);
                var ewahBitmap2  = new EwahCompressedBitArray();
                var clrBitArray2 = new BitArray(10000);
                int len          = rnd.Next(maxlength);
                for (int pos = 0; pos < len; pos++)
                {
                    // random *** number of bits set ***
                    if (rnd.Next(7) == 0)
                    {
                        // random *** increasing *** values
                        ewahBitmap1.Set(pos);
                        clrBitArray1.Set(pos, true);
                    }
                    if (rnd.Next(11) == 0)
                    {
                        // random *** increasing *** values
                        ewahBitmap2.Set(pos);
                        clrBitArray2.Set(pos, true);
                    }
                }
                assertEquals(clrBitArray1, ewahBitmap1);
                assertEquals(clrBitArray2, ewahBitmap2);
                // XOR
                {
                    EwahCompressedBitArray xorEwahBitmap = ewahBitmap1.Xor(ewahBitmap2);
                    var xorclrBitArray = (BitArray)clrBitArray1.Clone();
                    xorclrBitArray.Xor(clrBitArray2);
                    assertEquals(xorclrBitArray, xorEwahBitmap);
                }
                // AND
                {
                    EwahCompressedBitArray andEwahBitmap = ewahBitmap1.And(ewahBitmap2);
                    var andclrBitArray = (BitArray)clrBitArray1.Clone();
                    andclrBitArray.And(clrBitArray2);
                    assertEquals(andclrBitArray, andEwahBitmap);
                }
                // AND
                {
                    EwahCompressedBitArray andEwahBitmap = ewahBitmap2.And(ewahBitmap1);
                    var andclrBitArray = (BitArray)clrBitArray1.Clone();
                    andclrBitArray.And(clrBitArray2);
                    assertEquals(andclrBitArray, andEwahBitmap);
                }
                // AND NOT
                {
                    EwahCompressedBitArray andNotEwahBitmap = ewahBitmap1
                                                              .AndNot(ewahBitmap2);
                    var andNotclrBitArray = (BitArray)clrBitArray1.Clone();
                    andNotclrBitArray.AndNot(clrBitArray2);
                    assertEquals(andNotclrBitArray, andNotEwahBitmap);
                }
                // AND NOT
                {
                    EwahCompressedBitArray andNotEwahBitmap = ewahBitmap2
                                                              .AndNot(ewahBitmap1);
                    var andNotclrBitArray = (BitArray)clrBitArray2.Clone();
                    andNotclrBitArray.AndNot(clrBitArray1);
                    assertEquals(andNotclrBitArray, andNotEwahBitmap);
                }
                // OR
                {
                    EwahCompressedBitArray orEwahBitmap = ewahBitmap1.Or(ewahBitmap2);
                    var orclrBitArray = (BitArray)clrBitArray1.Clone();
                    orclrBitArray.Or(clrBitArray2);
                    assertEquals(orclrBitArray, orEwahBitmap);
                }
                // OR
                {
                    EwahCompressedBitArray orEwahBitmap = ewahBitmap2.Or(ewahBitmap1);
                    var orclrBitArray = (BitArray)clrBitArray1.Clone();
                    orclrBitArray.Or(clrBitArray2);
                    assertEquals(orclrBitArray, orEwahBitmap);
                }
            }
        }