Beispiel #1
0
        public void TestFlip()
        {
            BetterBitArray bitArray = new BetterBitArray(100000);

            for (UInt32 i = 0; i < bitArray.bitLength; i++)
            {
                Assert.IsFalse(bitArray.Get(i));
            }

            for (UInt32 i = 0; i < bitArray.bitLength; i++)
            {
                bitArray.Flip(i);
            }

            for (UInt32 i = 0; i < bitArray.bitLength; i++)
            {
                Assert.IsTrue(bitArray.Get(i));
            }

            for (UInt32 i = 0; i < bitArray.bitLength; i++)
            {
                bitArray.Flip(i);
            }

            for (UInt32 i = 0; i < bitArray.bitLength; i++)
            {
                Assert.IsFalse(bitArray.Get(i));
            }
        }
Beispiel #2
0
        public static void PerformanceTestBitArrays(Int32 length)
        {
            long before;

            System.Collections.BitArray bitArray = new System.Collections.BitArray(length);
            BetterBitArray betterBitArray        = new BetterBitArray((UInt32)length);

            before = Stopwatch.GetTimestamp();
            for (int i = 0; i < 10000; i++)
            {
                bitArray.SetAll(false);
                //bitArray.Set(0, true);
            }
            Console.WriteLine((Stopwatch.GetTimestamp() - before).StopwatchTicksAsInt64Milliseconds());
            PrintGC("BitArray");

            before = Stopwatch.GetTimestamp();
            for (int i = 0; i < 10000; i++)
            {
                betterBitArray.SetAll(false);
                //betterBitArray.Assert(0);
            }
            Console.WriteLine((Stopwatch.GetTimestamp() - before).StopwatchTicksAsInt64Milliseconds());
            PrintGC("BetterBitArray");
        }
        //
        // On an HP Z600, It takes about 38 seconds to generate
        // the first 150 million primes (about 4 million primes per second)
        //
        public static UInt32[] GeneratePrimes(UInt32 max, out UInt32 outPrimeCount)
        {
            if (max == UInt32.MaxValue)
            {
                throw new ArgumentOutOfRangeException("max", "max cannot be UInt32.MaxValue");
            }

            UInt32[] primes = new UInt32[PrimeCount.UpperBound(max)];
            UInt32   primeCount;

            UInt32         maxSquareRoot = (UInt32)Math.Sqrt(max);
            BetterBitArray isComposite   = new BetterBitArray((max + 1) >> 1);

            primes[0]  = 2;
            primes[1]  = 3;
            primeCount = 2;

            UInt32 candidatePrime;
            UInt32 incrementAmount = 0;

            for (candidatePrime = 5; candidatePrime <= maxSquareRoot; candidatePrime += incrementAmount)
            {
                if (!isComposite.Get(candidatePrime >> 1))
                {
                    //
                    // Mark all multiples of this prime as composites
                    //
                    UInt32 candidatePrimeDoubled = 2 * candidatePrime;
                    for (UInt32 j = candidatePrime * candidatePrime; j <= max; j += candidatePrimeDoubled)
                    {
                        isComposite.Assert(j >> 1);
                    }

                    //
                    // Add to the primes
                    //
                    primes[primeCount++] = candidatePrime;
                }

                incrementAmount = (incrementAmount == 2) ? 4U : 2U;
            }

            // Add the rest of the primes
            for (; candidatePrime <= max; candidatePrime += incrementAmount)
            {
                if (!isComposite.Get(candidatePrime >> 1))
                {
                    primes[primeCount++] = candidatePrime;
                }
                incrementAmount = (incrementAmount == 2) ? 4U : 2U;
            }

            outPrimeCount = primeCount;
            return(primes);
        }
        public void XorEquals(BetterBitArray other)
        {
            if (this.bitLength != other.bitLength)
            {
                throw new ArgumentException(String.Format("Given bit array is {0} bits but this bit array is {1}", other.bitLength, this.bitLength));
            }

            UInt32 thisArrayLength = (UInt32)this.array.Length;

            for (UInt32 i = 0; i < thisArrayLength; i++)
            {
                this.array[i] ^= other.array[i];
            }
        }
        public static UInt32[] GeneratePrimes(UInt32 max, out UInt32 outPrimeCount)
        {
            if (max < 5)
            {
                throw new ArgumentOutOfRangeException("max", "max can't be too small");
            }
            if (max == UInt32.MaxValue)
            {
                throw new ArgumentOutOfRangeException("max", "max cannot be UInt32.MaxValue");
            }

            UInt32[] primes = new UInt32[PrimeCount.UpperBound(max)];
            UInt32   primeCount;

            UInt32         maxSquareRoot = (UInt32)Math.Sqrt(max);
            BetterBitArray isPrime       = new BetterBitArray(max + 1);

            // put in candidate primes
            for (UInt32 i = 1; i <= maxSquareRoot; i++)
            {
                for (UInt32 j = 1; j <= maxSquareRoot; j++)
                {
                    UInt32 n;

                    n = 4 * i + j;
                    //if (n > max) throw new InvalidOperationException();
                    if (n <= max) // Should always be true for large enough max (maybe take this out0
                    {
                        UInt32 nMod12 = n % 12;
                        if (nMod12 == 1 || nMod12 == 5)
                        {
                            isPrime.Flip(n);
                        }
                    }

                    n = 3 * i + j;
                    //if (n > max) throw new InvalidOperationException();
                    if (n <= max) // Should always be true for large enough max (maybe take this out0
                    {
                        UInt32 nMod12 = n % 12;
                        if (nMod12 == 7)
                        {
                            isPrime.Flip(n);
                        }
                    }

                    if (i > j)
                    {
                        n = 3 * i - j;
                        //if (n > max) throw new InvalidOperationException();
                        if (n <= max) // Should always be true for large enough max (maybe take this out0
                        {
                            UInt32 nMod12 = n % 12;
                            if (nMod12 == 11)
                            {
                                isPrime.Flip(n);
                            }
                        }
                    }
                }
            }

            primes[0]  = 2;
            primes[1]  = 3;
            primeCount = 2;

            UInt32 candidatePrime;

            for (candidatePrime = 5; candidatePrime <= maxSquareRoot; candidatePrime += 2)
            {
                if (isPrime.Get(candidatePrime))
                {
                    //
                    // Mark all multiples of its square as composite
                    //
                    //
                    UInt32 candidatePrimeSquared = candidatePrime * candidatePrime;
                    //UInt32 candidatePrimeSquaredThenDoubled = iSquared * 2;
                    for (UInt32 j = candidatePrimeSquared; j <= max; j += candidatePrimeSquared)
                    {
                        isPrime.Deassert(j);
                    }

                    //
                    // Add to the primes
                    //
                    primes[primeCount++] = candidatePrime;
                }
            }

            // Add the rest of the primes
            for (; candidatePrime <= max; candidatePrime += 2)
            {
                if (isPrime.Get(candidatePrime))
                {
                    primes[primeCount++] = candidatePrime;
                }
            }

            outPrimeCount = primeCount;
            return(primes);
        }
 public Enumerator(BetterBitArray bitarray)
 {
     this.bitarray = bitarray;
     this.bitIndex = UInt32.MaxValue;
 }