xxhash added

This commit is contained in:
Timerix22 2022-06-11 15:38:35 +03:00
parent a475b0d73c
commit abdc56d404
11 changed files with 6134 additions and 36 deletions

View File

@ -17,8 +17,8 @@
<option name="myMisraCPPChecks" value="clion-misra-cpp2008-*,-clion-misra-cpp2008-11-0-1" /> <option name="myMisraCPPChecks" value="clion-misra-cpp2008-*,-clion-misra-cpp2008-11-0-1" />
</inspection_tool> </inspection_tool>
<inspection_tool class="OCUnusedGlobalDeclaration" enabled="false" level="WARNING" enabled_by_default="false" /> <inspection_tool class="OCUnusedGlobalDeclaration" enabled="false" level="WARNING" enabled_by_default="false" />
<inspection_tool class="ShellCheck" enabled="true" level="ERROR" enabled_by_default="true"> <inspection_tool class="ShellCheck" enabled="false" level="ERROR" enabled_by_default="false">
<shellcheck_settings value="SC2034,SC2035,SC2061" /> <shellcheck_settings value="SC2034,SC2035,SC2059,SC2061,SC2091,SC2155" />
</inspection_tool> </inspection_tool>
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false"> <inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
<option name="processCode" value="true" /> <option name="processCode" value="true" />

View File

@ -14,8 +14,8 @@
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<MakefileProjectSettings> <MakefileProjectSettings>
<option name="buildOptions" value=" " /> <option name="buildOptions" value=" " />
<option name="buildTarget" value="build_lib" /> <option name="buildTarget" value="" />
<option name="cleanTarget" value="clear_bin" /> <option name="cleanTarget" value="" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules"> <option name="modules">
<set> <set>

View File

@ -2,9 +2,9 @@
uint32 hash_sdbm32(uint32 oldhash, void* buf, uint32 len){ uint32 hash_sdbm32(uint32 oldhash, void* buf, uint32 len){
uint8* ubuf=(uint8*)buf; uint8* ubuf=(uint8*)buf;
uint32 hash=oldhash; register uint32 hash=oldhash;
for (; len ; len--, ubuf++) for (; len ; len--, ubuf++)
hash=*ubuf+(hash<<6)+(hash<<16)-hash; hash=(hash<<6)+(hash<<16)-hash+*ubuf;
return hash; return hash;
} }
@ -79,10 +79,11 @@ uint32 hash_crc32(uint32 oldhash, void* buf, uint32 len){
uint8* ubuf=(uint8*)buf; uint8* ubuf=(uint8*)buf;
register uint32 crc=oldhash; register uint32 crc=oldhash;
for (; len; --len, ++ubuf) for (; len; --len, ++ubuf)
crc = crc_32_tab[(crc^(*ubuf)) & 0xff] ^ (crc>>8); crc=crc_32_tab[(crc^(*ubuf)) & 0xff] ^ (crc>>8);
return ~crc; return ~crc;
} }
// bool hashf_crc32c(char *name, uint32 *crc, long *charcnt) { // bool hashf_crc32c(char *name, uint32 *crc, long *charcnt) {
// register FILE *fin; // register FILE *fin;
// register uint32 oldcrc32; // register uint32 oldcrc32;

View File

@ -11,6 +11,8 @@ extern "C" {
uint32 hash_sdbm32(uint32 oldhash, void* buf, uint32 len); uint32 hash_sdbm32(uint32 oldhash, void* buf, uint32 len);
uint32 hash_crc32(uint32 oldhash, void* buf, uint32 len); uint32 hash_crc32(uint32 oldhash, void* buf, uint32 len);
uint32 hash_xxh32(uint32 oldhash, void* buf, uint32 len);
uint64 hash_xxh3_64(uint32 oldhash, void* buf, uint32 len);
#if __cplusplus #if __cplusplus
} }

View File

@ -0,0 +1,12 @@
#include "hash.h"
#define XXH_INLINE_ALL
#define XXH_STATIC_LINKING_ONLY /* access advanced declarations */
#define XXH_IMPLEMENTATION /* access definitions */
#include "./xxhash.h"
uint32 hash_xxh32(uint32 oldhash, void* buf, uint32 len){
return XXH32(buf,len,oldhash);
}
uint64 hash_xxh3_64(uint32 oldhash, void* buf, uint32 len){
return XXH3_64bits_withSeed(buf,len,oldhash);
}

6075
src/HashFunctions/xxhash.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,7 @@
extern "C" { extern "C" {
#endif #endif
#include "hash.h" #include "../HashFunctions/hash.h"
#include "KeyValuePair.h" #include "KeyValuePair.h"
typedef struct Hashtable{ typedef struct Hashtable{

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "../Hashtable/hash.h" #include "../HashFunctions/hash.h"
#include "../Autoarr2/Autoarr2.hpp" #include "../Autoarr2/Autoarr2.hpp"
// amount of rows // amount of rows
@ -129,6 +129,7 @@ bool Hashtable2<TKey, TVal>::remove(TKey){
template<typename TKey, typename TVal> template<typename TKey, typename TVal>
Hashtable2<TKey, TVal>::~Hashtable2(){ Hashtable2<TKey, TVal>::~Hashtable2(){
delete[] hashes;
delete[] keys; delete[] keys;
delete[] values; delete[] values;
} }

View File

@ -12,9 +12,6 @@ extern "C" {
#include <time.h> #include <time.h>
#include <setjmp.h> #include <setjmp.h>
#define CHOOSE(B, Y, N) __builtin_choose_expr(B, Y, N)
#define IFTYPE(X, T) __builtin_types_compatible_p(typeof(X), T)
#define dbg(N) printf("\e[95m%d\n",N) #define dbg(N) printf("\e[95m%d\n",N)

View File

@ -9,7 +9,7 @@ void test_all(){
test_dtsod(); test_dtsod();
test_autoarr2(); test_autoarr2();
test_hash_functions(); test_hash_functions();
printf("\e[96m---------------------------------------\e[0m\n"); printf("\e[96m--------------------------------------\e[0m\n");
} }
int main(){ int main(){

View File

@ -1,37 +1,47 @@
#include "tests.h" #include "tests.h"
#include "../src/Hashtable/hash.h" #include "../src/HashFunctions/hash.h"
#include "../src/Autoarr/Autoarr.h" #include "../src/Autoarr/Autoarr.h"
#define AMOUNT_OF_TESTS 10000
#define SPEED_TESTS 10000
#define COLLISION_TESTS 16000
char data[]="iojihiojopijiugbjmoihftytryfdrh"; char data[]="iojihiojopijiugbjmoihftytryfdrh";
#define test_hashfunc(hasht, hashf, tests_n)\ #define test_hashfunc(hasht, hashf)({\
optime(#hashf,1,({\
printf("\e[94mfunction: \e[92m" #hashf "\n");\ printf("\e[94mfunction: \e[92m" #hashf "\n");\
printf("\e[94mhash of \"%s\": \e[92m%x\n",data, hashs(hashf,data));\ hasht h=0;\
optime("speed test", 1, ({\
for(uint32 i=0; i<SPEED_TESTS; i++)\
h=hashf(h, data, sizeof(data));\
}));\
/*printf("\e[94mhash of \"\e[90m%s\e[94m\": \e[92m%x\n",data, h);*/\
Autoarr(hasht)* hashes=Autoarr_create(hasht,512,32768);\ Autoarr(hasht)* hashes=Autoarr_create(hasht,512,32768);\
uint32 collisions=0;\ optime("collision test",1,({\
for(uint32 i=0;i<tests_n;i++){\ uint32 collisions=0;\
hasht h=hashb(hashf, (uint8*)&i, 4);\ for(uint64 i=0;i< COLLISION_TESTS;i++){\
bool col=false;\ hasht h=hashb(hashf, (uint8*)&i, sizeof(i));\
Autoarr_foreach(hashes,e,({\ bool col=false;\
if(e==h) {\ Autoarr_foreach(hashes,e,({\
col=true;\ if(e==h) {\
printf("\e[91mcollision: %lu\n", (uint64)h);\ col=true;\
break;\ break;\
}\ }\
}));\ }));\
if(col) collisions++;\ if(col) collisions++;\
else Autoarr_add(hashes,h);\ else Autoarr_add(hashes,h);\
}\ }\
printf("\e[93m%u \e[94mcollisions detected in %u hashes\n", collisions, tests_n);\ printf("\e[93m%u \e[94mcollisions detected in %u hashes\n", collisions, COLLISION_TESTS);\
})) }));\
printf("\e[96m--------------------------------------\n");\
})
void test_hash_functions(){ void test_hash_functions(){
optime("test_hash_functions",1,({ optime("test_hash_functions",1,({
printf("\e[96m--------[test_hash_functions]---------\n"); printf("\e[96m--------[test_hash_functions]---------\n");
test_hashfunc(uint32, hash_crc32, AMOUNT_OF_TESTS); test_hashfunc(uint32, hash_crc32);
test_hashfunc(uint32, hash_sdbm32, AMOUNT_OF_TESTS); test_hashfunc(uint32, hash_sdbm32);
test_hashfunc(uint32, hash_xxh32);
test_hashfunc(uint64, hash_xxh3_64);
})); }));
} }