Improve hash function for names.

PiperOrigin-RevId: 619173787
Change-Id: I7889ea71ebe7bdbe682042842c521d6631521a35
This commit is contained in:
Yuval Tassa
2024-03-26 06:49:58 -07:00
committed by Copybara-Service
parent 709719911e
commit fa8893b443
3 changed files with 5 additions and 5 deletions
+3 -3
View File
@@ -1050,11 +1050,11 @@ static int _getnumadr(const mjModel* m, mjtObj type, int** padr, int* mapadr) {
} }
// get string hash, see http://www.cse.yorku.ca/~oz/hash.html // get string hash, see http://www.cse.yorku.ca/~oz/hash.html
uint64_t mj_hashdjb2(const char* s, uint64_t n) { uint64_t mj_hashString(const char* s, uint64_t n) {
uint64_t h = 5381; uint64_t h = 5381;
int c; int c;
while ((c = *s++)) { while ((c = *s++)) {
h = ((h << 5) + h) + c; h = ((h << 5) + h) ^ c;
} }
return h % n; return h % n;
} }
@@ -1070,7 +1070,7 @@ int mj_name2id(const mjModel* m, int type, const char* name) {
// search // search
if (num) { // look up at hash address if (num) { // look up at hash address
uint64_t hash = mj_hashdjb2(name, num); uint64_t hash = mj_hashString(name, num);
uint64_t i = hash; uint64_t i = hash;
do { do {
+1 -1
View File
@@ -114,7 +114,7 @@ MJAPI void mj_angmomMat(const mjModel* m, mjData* d, mjtNum* mat, int body);
//-------------------------- name functions -------------------------------------------------------- //-------------------------- name functions --------------------------------------------------------
// get string hash, see http://www.cse.yorku.ca/~oz/hash.html // get string hash, see http://www.cse.yorku.ca/~oz/hash.html
uint64_t mj_hashdjb2(const char* s, uint64_t n); uint64_t mj_hashString(const char* s, uint64_t n);
// get id of object with the specified mjtObj type and name, returns -1 if id not found // get id of object with the specified mjtObj type and name, returns -1 if id not found
MJAPI int mj_name2id(const mjModel* m, int type, const char* name); MJAPI int mj_name2id(const mjModel* m, int type, const char* name);
+1 -1
View File
@@ -1492,7 +1492,7 @@ static int namelist(vector<T*>& list, int adr, int* name_adr, char* names, int*
continue; continue;
} }
uint64_t j = mj_hashdjb2(list[i]->name.c_str(), map_size); uint64_t j = mj_hashString(list[i]->name.c_str(), map_size);
// find first empty slot using linear probing // find first empty slot using linear probing
for (; map[j]!=-1; j=(j+1) % map_size) {} for (; map[j]!=-1; j=(j+1) % map_size) {}