namedict.cpp 782 B

1234567891011121314151617181920212223
  1. #include "pocketpy/namedict.h"
  2. namespace pkpy{
  3. uint16_t _find_perfect_hash_seed(uint16_t capacity, const std::vector<StrName>& keys){
  4. if(keys.empty()) return kHashSeeds[0];
  5. static std::set<uint16_t> indices;
  6. indices.clear();
  7. std::pair<uint16_t, float> best_score = {kHashSeeds[0], 0.0f};
  8. const int kHashSeedsSize = sizeof(kHashSeeds) / sizeof(kHashSeeds[0]);
  9. for(int i=0; i<kHashSeedsSize; i++){
  10. indices.clear();
  11. for(auto key: keys){
  12. uint16_t index = _hash(key, capacity-1, kHashSeeds[i]);
  13. indices.insert(index);
  14. }
  15. float score = indices.size() / (float)keys.size();
  16. if(score > best_score.second) best_score = {kHashSeeds[i], score};
  17. }
  18. return best_score.first;
  19. }
  20. } // namespace pkpy