Update namedict.h

This commit is contained in:
blueloveTH 2023-02-24 16:25:01 +08:00
parent 4fa600d1cc
commit 2c290e051f

View File

@ -5,7 +5,7 @@
#include "str.h" #include "str.h"
namespace pkpy{ namespace pkpy{
const std::vector<uint32_t> kHashSeeds = {0, 2619911537, 3657312521, 3108729646, 3527288759, 3054146033, 3436461329, 3073779540, 2262929533, 3564928174, 2823402058, 4053700272, 3710518398, 2193895465, 3616465673, 2370151435, 3911946797, 2518033560, 4090755824, 2554076140, 2922670102, 2817437464, 3058134240, 4015911568, 2683197236, 3580292421, 2489282276, 2198476149, 3059557576, 3251314740, 2164089808, 3407729628, 4006319879, 3563204365, 2959032770, 3699872774, 3285955347, 2886756578, 2727979131, 3987926730, 3558848942, 3667783180, 3427603538, 2678804156, 3899695574, 3497073252, 4125590716, 3439003736, 3166960007, 2341256547, 3498025667, 2434275284, 2294495502, 2454032734, 2622845447, 2237894924, 4127773463, 2899873446, 3826047724, 2772822995, 4021041972, 3585330008, 3442671856, 4033639492, 4190375370, 3423510541, 3993284300, 3399740404, 2346010479, 2665226039, 3989420676, 2430396952, 4162553639, 3318451871, 2451157282, 3888084520, 4216786107, 3630490447, 3686500437, 4270289137, 2845436680, 3990477872, 3386727112, 2603155603, 2533548133, 2476236382, 2752268515, 2714540624, 3649552071, 2486775129, 3447438497, 2660214659, 3171847655, 2173117107, 2777204947, 3473126570, 2874563719, 3710212439, 3882999260, 3884415651, 3939886653, 2513961523, 3259070705, 4076001992, 3695924943, 2630642728, 2302962913, 3977147010, 4229898948, 3278694988, 3668138471, 4174657761, 2681204139, 2468496171, 3953941369, 4216451258, 3986080889, 3355338704, 3484226746, 3964851958, 4063196140, 3210555673, 3972895759, 2762823957}; const std::vector<uint32_t> kHashSeeds = {2654435761, 740041872, 89791836, 2530921597, 3099937610, 4149637300, 2701344377, 1871341841, 1162794509, 172427115, 1636841237, 716883023, 3294650677, 54921151, 3697702254, 632800580, 704251301, 1107400416, 3158440428, 581874317, 3196521560, 2374935651, 3196227762, 2033551959, 2028119291, 348132418, 392150876, 3839168722, 3705071505, 742931757, 2917622539, 3641634736, 3438863246, 1211314974, 1389620692, 3842835768, 165823282, 2225611914, 1862128271, 2147948325, 3759309280, 2087364973, 3453466014, 2082604761, 3627961499, 967790220, 3285133283, 2749567844, 262853493, 142639230, 3079101350, 2942333634, 1470374050, 3719337124, 2487858314, 1605159164, 2958061235, 3310454023, 3143584575, 3696188862, 3455413544, 148400163, 889426286, 1485235735};
uint32_t find_next_capacity(uint32_t n){ uint32_t find_next_capacity(uint32_t n){
uint32_t x = 2; uint32_t x = 2;
@ -13,10 +13,10 @@ namespace pkpy{
return x; return x;
} }
#define _hash(key, mask, hash_seed) ( ((key).index ^ (hash_seed)) & (mask) ) #define _hash(key, mask, hash_seed) ( ( (key).index * (hash_seed) >> 16 ) & (mask) )
uint32_t find_perfect_hash_seed(uint32_t capacity, const std::vector<StrName>& keys){ uint32_t find_perfect_hash_seed(uint32_t capacity, const std::vector<StrName>& keys){
if(keys.empty()) return 0; if(keys.empty()) return kHashSeeds[0];
std::set<uint32_t> indices; std::set<uint32_t> indices;
std::vector<std::pair<uint32_t, float>> scores; std::vector<std::pair<uint32_t, float>> scores;
for(int i=0; i<kHashSeeds.size(); i++){ for(int i=0; i<kHashSeeds.size(); i++){
@ -46,7 +46,7 @@ namespace pkpy{
NameDictNode* _a; NameDictNode* _a;
uint32_t _mask; uint32_t _mask;
NameDict(uint32_t capacity=2, float load_factor=0.67, uint32_t hash_seed=0): NameDict(uint32_t capacity=2, float load_factor=0.67, uint32_t hash_seed=kHashSeeds[0]):
_capacity(capacity), _size(0), _load_factor(load_factor), _capacity(capacity), _size(0), _load_factor(load_factor),
_hash_seed(hash_seed), _a(new NameDictNode[capacity]), _mask(capacity-1) {} _hash_seed(hash_seed), _a(new NameDictNode[capacity]), _mask(capacity-1) {}