Last active
November 1, 2022 09:30
-
-
Save LimHyungTae/a07a4d7ad8d3c5cf70a1c96ce18959b1 to your computer and use it in GitHub Desktop.
Test of voxel map using hash table (`unordered_map`)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/****************************************************************************** | |
Online C++ Compiler. | |
Code, Compile, Run and Debug C++ program online. | |
Write your code in this editor and press "Run" button to compile and execute it. | |
*******************************************************************************/ | |
#include <iostream> | |
#include <unordered_map> | |
#define HASH_P 116101 | |
#define MAX_N 10000000000 | |
using namespace std; | |
int main() | |
{ | |
int64_t a = 10; | |
int64_t b = 20; | |
int64_t c = 33; | |
cout << std::hash<int64_t>()(a) << ", "; | |
cout << std::hash<int64_t>()(b) << ", "; | |
cout << std::hash<int64_t>()(c) << endl; | |
cout << (std::hash<int64_t>()(a) << 1) << ", "; | |
cout << (std::hash<int64_t>()(b) << 1) << ", "; | |
cout << (std::hash<int64_t>()(c) << 1) << endl; | |
cout << ( std::hash<int64_t>()(a) ^ (std::hash<int64_t>()(a) << 1) ) << ", "; | |
cout << ( std::hash<int64_t>()(b) ^ (std::hash<int64_t>()(b) << 1) ) << ", "; | |
cout << ( std::hash<int64_t>()(c) ^ (std::hash<int64_t>()(c) << 1) ) << endl; | |
cout << ( ( std::hash<int64_t>()(a) ^ (std::hash<int64_t>()(a) << 1) ) >> 1) << ", "; | |
cout << ( ( std::hash<int64_t>()(b) ^ (std::hash<int64_t>()(b) << 1) ) >> 1) << ", "; | |
cout << ( ( std::hash<int64_t>()(c) ^ (std::hash<int64_t>()(c) << 1) ) >> 1) << endl; | |
unordered_map<int64_t, int64_t> m; | |
int num_already_in = 0; | |
int num_new = 0; | |
int64_t count = 0; | |
for (int64_t sx = -10; sx < 10; ++ sx) { | |
for (int64_t sy = -10; sy < 10; ++ sy) { | |
for (int64_t sz = -10; sz < 10; ++ sz) { | |
// From livox_camera_calib | |
// size_t key_of_m = ((hash<int64_t>()(sx) ^ (hash<int64_t>()(sy) << 1)) >> 1) ^ (hash<int64_t>()(sz) << 1); | |
// From voxel map. [NOTE] Below one is better!!!!1 | |
int64_t key_of_m = ((((sz) * HASH_P) % MAX_N + (sy)) * HASH_P) % MAX_N + (sx); | |
// cout << key_of_m << ", "; | |
if (m.find(key_of_m) == m.end()) { | |
m[key_of_m] = count; | |
++num_new; | |
} else { | |
cout <<" It's alread in! "<< endl; | |
cout << sx << ", " << sy << ", " << sz <<endl; | |
++num_already_in; | |
} | |
++count; | |
} | |
} | |
} | |
cout << endl; | |
cout << num_already_in << " vs " << num_new << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We have to use
((((sz) * HASH_P) % MAX_N + (sy)) * HASH_P) % MAX_N + (sx)
!!!!