/// <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 > c > .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; }
/// <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 > c > .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); }
public Buckets(IKeySource keySource, double c) { _keySource = keySource; var loadFactor = c; _m = keySource.NbKeys; _nbuckets = _m / KeysPerBucket + 1; if (loadFactor < 0.5) { loadFactor = 0.5; } if (loadFactor >= 0.99) { loadFactor = 0.99; } _n = (uint)(_m / (loadFactor)) + 1; if (_n % 2 == 0) { _n++; } for (;;) { if (MillerRabin.CheckPrimality(_n)) { break; } _n += 2; // just odd numbers can be primes for n > 2 } _buckets = new Bucket[_nbuckets]; _items = new Item[_m]; }
public Buckets(IKeySource keySource, double c) { _keySource = keySource; var loadFactor = c; _m = keySource.NbKeys; _nbuckets = _m/KeysPerBucket + 1; if(loadFactor < 0.5 ) loadFactor = 0.5; if(loadFactor >= 0.99) loadFactor = 0.99; _n = (uint)(_m/(loadFactor)) + 1; if(_n % 2 == 0) _n++; for(;;) { if(MillerRabin.CheckPrimality(_n)) break; _n += 2; // just odd numbers can be primes for n > 2 } _buckets = new Bucket[_nbuckets]; _items = new Item[_m]; }
public void RegisterKeySource(IKeySource source) { source.OnKeyPress += this.KeyPressHandler; }