Beispiel #1
0
        /// <summary>
        /// Create a minimum perfect hash function for the provided key set
        /// </summary>
        /// <param name="keySource">Key source</param>
        /// <param name="c">Load factor (.5 &gt; c &gt; .99)</param>
        /// <returns>Created Minimum Perfect Hash function</returns>
        public static MinPerfectHash Create(IKeySource keySource, double c)
        {
            var buckets = new Buckets(keySource, c);
            var dispTable = new uint[buckets.NBuckets];
            uint hashSeed;

            var iteration = 100;
            for (; ; iteration--)
            {
                uint maxBucketSize;
                if (!buckets.MappingPhase(out hashSeed, out maxBucketSize))
                {
                    throw new Exception("Mapping failure. Duplicate keys?");
                }

                var sortedLists = buckets.OrderingPhase(maxBucketSize);
                var searchingSuccess = buckets.SearchingPhase(maxBucketSize, sortedLists, dispTable);

                if (searchingSuccess) break;

                if (iteration <= 0)
                {
                    throw new Exception("Too many iteration");
                }
            }

            var cs = new CompressedSeq();
            cs.Generate(dispTable, (uint)dispTable.Length);

            var ret = new MinPerfectHash { _hashSeed = hashSeed, _cs = cs, _nbuckets = buckets.NBuckets, _n = buckets.N };

            return ret;
        }
Beispiel #2
0
        /// <summary>
        /// Create a minimum perfect hash function for the provided key set
        /// </summary>
        /// <param name="keySource">Key source</param>
        /// <param name="c">Load factor (.5 &gt; c &gt; .99)</param>
        /// <returns>Created Minimum Perfect Hash function</returns>
        public static MinPerfectHash Create(IKeySource keySource, double c)
        {
            var  buckets   = new Buckets(keySource, c);
            var  dispTable = new uint[buckets.NBuckets];
            uint hashSeed;

            var iteration = 100;

            for (; ; iteration--)
            {
                uint maxBucketSize;
                if (!buckets.MappingPhase(out hashSeed, out maxBucketSize))
                {
                    throw new Exception("Mapping failure. Duplicate keys?");
                }

                var sortedLists      = buckets.OrderingPhase(maxBucketSize);
                var searchingSuccess = buckets.SearchingPhase(maxBucketSize, sortedLists, dispTable);

                if (searchingSuccess)
                {
                    break;
                }

                if (iteration <= 0)
                {
                    throw new Exception("Too many iteration");
                }
            }

            var cs = new CompressedSeq();

            cs.Generate(dispTable, (uint)dispTable.Length);

            var ret = new MinPerfectHash {
                _hashSeed = hashSeed, _cs = cs, _nbuckets = buckets.NBuckets, _n = buckets.N
            };

            return(ret);
        }