protected virtual bool ContainsKey(TKey key, int hash, bool validate)
 {
     if (validate && ValidateConfiguration)
     {
         IsValidConfiguration(Configuration.IdHash(key), hash);
     }
     return(_configuration
            .Probe(BlockSize, _hashFunctionCount, hash)
            .All(position => _data.Get((int)position)));
 }
 public void Test_Set_Get()
 {
     uint [] bitCounts = { 0, 1, 3, 100, 1000 };
     for (int run = 0; run < bitCounts.Length; ++run)
     {
         uint         bitCount = bitCounts[run];
         FastBitArray fba      = new FastBitArray(bitCount);
         // Fill with a pattern 1111...
         for (uint i = 0; i < bitCount; ++i)
         {
             fba.Set(i, true);
         }
         // Verify
         for (uint i = 0; i < bitCount; ++i)
         {
             Assert.AreEqual(true, fba.Get(i));
         }
         // Fill with a pattern 00000...
         for (uint i = 0; i < bitCount; ++i)
         {
             fba.Set(i, false);
         }
         // Verify
         for (uint i = 0; i < bitCount; ++i)
         {
             Assert.AreEqual(false, fba.Get(i));
         }
         // Fill with a pattern 010101...
         for (uint i = 0; i < bitCount; ++i)
         {
             fba.Set(i, i % 2 == 0);
         }
         // Verify
         for (uint i = 0; i < bitCount; ++i)
         {
             Assert.AreEqual(i % 2 == 0, fba.Get(i));
         }
         // Fill with a pattern 101010...
         for (uint i = 0; i < bitCount; ++i)
         {
             fba.Set(i, i % 2 == 1);
         }
         // Verify
         for (uint i = 0; i < bitCount; ++i)
         {
             Assert.AreEqual(i % 2 == 1, fba.Get(i));
         }
     }
 }
Exemple #3
0
        /// <summary>
        /// Compute similarity between <paramref name="minHashValues1"/> and <paramref name="minHashValues2"/>
        /// </summary>
        /// <param name="minHashValues1">The values</param>
        /// <param name="minHashValues2">The values to compare against</param>
        /// <param name="numHashFunctions">The number of hash functions to use</param>
        /// <param name="bitSize">The number of bits for a single cell</param>
        /// <returns></returns>
        private static double?ComputeSimilarityFromSignatures(
            FastBitArray minHashValues1,
            FastBitArray minHashValues2,
            int numHashFunctions,
            byte bitSize)
        {
            if (minHashValues1 == null ||
                minHashValues2 == null ||
                numHashFunctions <= 0 ||
                bitSize == 0)
            {
                return(0.0D);
            }
            if (minHashValues1.Length != minHashValues2.Length)
            {
                return(null);
            }
            uint identicalMinHashes = 0;
            var  bitRange           = Enumerable.Range(0, bitSize).ToArray();
            var  minHash1Length     = minHashValues1.Length / bitSize;
            var  idx1 = 0;

            for (var i = 0; i < minHash1Length; i++)
            {
                if (bitRange
                    .All(b => minHashValues1.Get(idx1 + b) == minHashValues2.Get(idx1 + b)))
                {
                    identicalMinHashes++;
                }
                idx1 += bitSize;
            }
            return(identicalMinHashes / (1.0D * minHashValues2.Length / bitSize));
        }
        public void Benchmark_Read()
        {
            uint         BIT_COUNT = 100000000;
            int          REP_COUNT = 10;
            FastBitArray ba        = new FastBitArray(BIT_COUNT);

            // Fill with a pattern 0101...
            for (uint i = 0; i < BIT_COUNT; ++i)
            {
                ba.Set(i, (i % 2) == 0);
            }

            bool checksum = false;

            var startTime = DateTime.Now;

            for (int r = 0; r < REP_COUNT; ++r)
            {
                for (uint i = 0; i < BIT_COUNT; ++i)
                {
                    checksum ^= ba.Get(i);
                }
            }
            var    runTime       = (DateTime.Now - startTime).TotalSeconds;
            double totalBitCount = (double)REP_COUNT * BIT_COUNT;

            Console.WriteLine("Reading bits: count: {0:#,#}, time: {1:0.000} s, {2:#,#} b/s, cs: {0}",
                              totalBitCount, runTime, totalBitCount / runTime, checksum);
        }
        private static bool GetFolded(FastBitArray bitArray, int position, uint foldFactor)
        {
            if (foldFactor == 1)
            {
                return(bitArray[position]);
            }
            var foldedSize = bitArray.Length / foldFactor;

            for (var i = 0; i < foldFactor; i++)
            {
                if (bitArray.Get(position + i * (int)foldedSize))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemple #6
0
        /// <summary>
        /// Get all the primes at most equal to <param name="to"></param>
        /// </summary>
        /// <param name="to"></param>
        /// <returns>All primes up to <paramref name="to"/>.</returns>
        internal static IEnumerable <long> GetPrimes(long to)
        {
            var cached = _primeCache;

            if (cached != null && cached.Item1 >= to)
            {
                return(cached.Item2.Where(p => p <= to).ToArray());
            }
            var sieveContainer = new FastBitArray((int)to + 1, true);
            int marker         = 2;    //start

            sieveContainer[0] = false; //0 is not prime
            sieveContainer[1] = false; //1 is not prime
            while (marker * marker <= sieveContainer.Length)
            {
                var factor = marker;
                while ((factor += marker) <= to)
                {
                    sieveContainer[factor] = false;
                }
                while (!sieveContainer.Get(++marker))
                {
                }
            }
            var primes = new List <long>();

            for (var i = 0; i < sieveContainer.Length; i++)
            {
                if (sieveContainer[i])
                {
                    primes.Add(i);
                }
            }
            _primeCache = new Tuple <long, IEnumerable <long> >(to, primes);
            return(primes);
        }