Merge pull request #4 from Timerix22/network

Network
This commit is contained in:
Timerix22 2022-05-08 13:45:05 +03:00 committed by GitHub
commit b8a5c0895e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 9 deletions

View File

@ -39,7 +39,7 @@ void Hashtable_expand(Hashtable* ht){
uint32 arlen=Autoarr_length(ar); uint32 arlen=Autoarr_length(ar);
for(uint16 k=0;k<arlen;k++){ for(uint16 k=0;k<arlen;k++){
KVPair p=Autoarr_get(ar,k); KVPair p=Autoarr_get(ar,k);
uint16 newrown=ihash(p.key)%HT_HEIGHTS[ht->hein]; uint16 newrown=hash32(p.key)%HT_HEIGHTS[ht->hein];
Autoarr(KVPair)* newar=newrows[newrown]; Autoarr(KVPair)* newar=newrows[newrown];
Autoarr_add(newar,p); Autoarr_add(newar,p);
} }
@ -53,7 +53,7 @@ void Hashtable_expand(Hashtable* ht){
} }
Autoarr(KVPair)* getrow(Hashtable* ht, char* key, bool can_expand){ Autoarr(KVPair)* getrow(Hashtable* ht, char* key, bool can_expand){
uint32 hash=ihash(key); uint32 hash=hash32(key);
Autoarr(KVPair)* ar=ht->rows[hash%HT_HEIGHTS[ht->hein]]; Autoarr(KVPair)* ar=ht->rows[hash%HT_HEIGHTS[ht->hein]];
if(can_expand && Autoarr_length(ar)==Autoarr_max_length(ar)) if(can_expand && Autoarr_length(ar)==Autoarr_max_length(ar))
optime("expand",1,(Hashtable_expand(ht))); optime("expand",1,(Hashtable_expand(ht)));

View File

@ -1,13 +1,13 @@
#include "hash.h" #include "hash.h"
uint32 ihash(char *str){ uint32 hash32(char *str){
uint32 hash=5381; uint32 hash=5381;
for (char c=*str;c;c=*(++str)) for (char c=*str;c;c=*(++str))
hash=((hash<<5)+hash)+c; hash=((hash<<5)+hash)+c;
return hash; return hash;
} }
uint64 lhash(char* str){ uint64 hash64(char* str){
uint64 hash = 0; uint64 hash = 0;
for (char c=*str;c;c=*(++str)) for (char c=*str;c;c=*(++str))
hash=c+(hash<<6)+(hash<<16)-hash; hash=c+(hash<<6)+(hash<<16)-hash;

View File

@ -7,9 +7,9 @@ extern "C" {
#include "../base/base.h" #include "../base/base.h"
// djb2 hash function from http:// www.cse.yorku.ca/~oz/hash.html // djb2 hash function from http:// www.cse.yorku.ca/~oz/hash.html
uint32 ihash(char *str); uint32 hash32(char *str);
// sdbm hash function // sdbm hash function
uint64 lhash(char* str); uint64 hash64(char* str);
#if __cplusplus #if __cplusplus
} }

View File

@ -4,9 +4,9 @@ TESTS=$(wildcard tests/*c) $(wildcard tests/**/*.c)
OUTDIR=bin OUTDIR=bin
CMP=gcc CMP=gcc
OPT_ARGS=-O2 -flto OPT_ARGS=-O2 -flto
WARN_ARGS=-Wall -Wno-discarded-qualifiers WARN_ARGS=-Wall -Wno-discarded-qualifiers -std=c17
all: clear_c clear_bin build_test build_test_dbg build_lib build_dll all: clear_c clear_bin build_test build_lib
clear_c: clear_c:
clear clear
@ -17,7 +17,7 @@ clear_bin:
mkdir $(OUTDIR) mkdir $(OUTDIR)
clang: CMP=clang clang: CMP=clang
clang: WARN_ARGS=-Wall -Wno-ignored-qualifiers -Wno-incompatible-pointer-types-discards-qualifiers clang: WARN_ARGS=-Wall -Wno-ignored-qualifiers -Wno-incompatible-pointer-types-discards-qualifiers -std=c17
clang: all clang: all
###################################### ######################################

View File

@ -31,6 +31,9 @@ extern "C" {
#else #else
#define CALL #define CALL
#endif #endif
#ifndef typeof
#define typeof __typeof__
#endif
#else #else
#pragma GCC error "unknown compiler" #pragma GCC error "unknown compiler"
#endif #endif