project structure changed for clion

This commit is contained in:
2022-05-08 13:59:28 +03:00
parent 703bd4bef4
commit c6c70c6fcc
46 changed files with 92 additions and 29 deletions

100
src/Hashtable/Hashtable.c Normal file
View File

@@ -0,0 +1,100 @@
#include "Hashtable.h"
// amount of rows
static const uint16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521};
#define HT_HEIN_MIN 0
#define HT_HEIN_MAX 6
#define ARR_BC 2
#define ARR_BL 8
Hashtable* Hashtable_create(){
Hashtable* ht=malloc(sizeof(Hashtable));
ht->hein=HT_HEIN_MIN;
ht->rows=malloc(HT_HEIGHTS[HT_HEIN_MIN]*sizeof(Autoarr(KVPair)*));
for(uint16 i=0;i<HT_HEIGHTS[HT_HEIN_MIN];i++)
ht->rows[i]=Autoarr_create(KVPair,ARR_BC,ARR_BL);
return ht;
}
void Hashtable_free(Hashtable* ht){
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++)
Autoarr_free_KVPair(ht->rows[i]);
free(ht->rows);
free(ht);
}
uint16 Hashtable_height(Hashtable* ht) { return HT_HEIGHTS[ht->hein]; }
void Hashtable_expand(Hashtable* ht){
if(ht->hein>=HT_HEIN_MAX) throw(ERR_MAXLENGTH);
Autoarr(KVPair)** newrows=malloc(HT_HEIGHTS[++ht->hein]*sizeof(Autoarr(KVPair)*));
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++)
newrows[i]=Autoarr_create(KVPair,ARR_BC,ARR_BL);
for(uint16 i=0;i<HT_HEIGHTS[ht->hein-1];i++){
Autoarr(KVPair)* ar=ht->rows[i];
uint32 arlen=Autoarr_length(ar);
for(uint16 k=0;k<arlen;k++){
KVPair p=Autoarr_get(ar,k);
uint16 newrown=ihash(p.key)%HT_HEIGHTS[ht->hein];
Autoarr(KVPair)* newar=newrows[newrown];
Autoarr_add(newar,p);
}
// it is a feature, not a bug
// no need to free kvpair keys and values, they just moved to new autoarrs
Autoarr_free(ar);
}
free(ht->rows);
ht->rows=newrows;
}
Autoarr(KVPair)* getrow(Hashtable* ht, char* key, bool can_expand){
uint32 hash=ihash(key);
Autoarr(KVPair)* ar=ht->rows[hash%HT_HEIGHTS[ht->hein]];
if(can_expand && Autoarr_length(ar)==Autoarr_max_length(ar))
optime("expand",1,(Hashtable_expand(ht)));
ar=ht->rows[hash%HT_HEIGHTS[ht->hein]];
return ar;
}
void Hashtable_add(Hashtable* ht, char* key, Unitype u){
KVPair p={ .key=key, .value=u };
Autoarr_add(getrow(ht,key,true),p);
}
// returns null or pointer to value in hashtable
Unitype* Hashtable_getptr(Hashtable* ht, char* key){
Autoarr(KVPair)* ar=getrow(ht,key,false);
uint32 arlen=Autoarr_length(ar);
for(uint32 i=0;i<arlen;i++){
KVPair* p=Autoarr_getptr(ar,i);
if(cptr_compare(key,p->key)) return &p->value;
}
return NULL;
}
Unitype Hashtable_get(Hashtable* ht, char* key){
Autoarr(KVPair)* ar=getrow(ht,key,false);
uint32 arlen=Autoarr_length(ar);
for(uint32 i=0;i<arlen;i++){
KVPair p=Autoarr_get(ar,i);
if(cptr_compare(key,p.key)) return p.value;
}
return UniNull;
}
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){
Unitype u=Hashtable_get(ht,key);
*output=u;
return u.type!=Null;
}
void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){
Unitype* val=Hashtable_getptr(ht, key);
if(val) *val=u;
else Hashtable_add(ht, key, u);
}

45
src/Hashtable/Hashtable.h Normal file
View File

@@ -0,0 +1,45 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../base/base.h"
#include "hash.h"
#include "KeyValuePair.h"
typedef struct Hashtable{
uint8 hein; // height=HT_HEIGHTS[hein]
Autoarr(KVPair)** rows; // Autoarr[height]
} Hashtable;
Hashtable* Hashtable_create();
void Hashtable_free(Hashtable* ht);
// amount of rows
uint16 Hashtable_height(Hashtable* ht);
// don't add pairs with the same keys,
// or something weird will happen
// if not sure, use Hashtable_addOrSet()
void Hashtable_add(Hashtable* ht, char* key, Unitype u);
void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u);
// returns null or pointer to value in hashtable
Unitype* Hashtable_getptr(Hashtable* ht, char* key);
Unitype Hashtable_get(Hashtable* ht, char* key);
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output);
#define Hashtable_foreach(HT, EL, codeblock)({\
uint16 hmax=Hashtable_height(HT);\
for(uint16 h=0; h<hmax; h++){\
Autoarr(KVPair)* AR=HT->rows[h];\
Autoarr_foreach(AR, EL, codeblock);\
}\
})
#if __cplusplus
}
#endif

View File

@@ -0,0 +1,22 @@
#include "KeyValuePair.h"
define_Autoarr(KVPair)
// proper way to clear a KVP
void KVPair_free(KVPair p){
free(p.key);
Unitype_free(p.value);
}
// func for KVP array clearing
void Autoarr_free_KVPair(Autoarr_KVPair* ar){
Autoarr_foreach(ar,k,KVPair_free(k));
Autoarr_free(ar);
}
void printkvp(KVPair p){
printf("{\"%s\", ",p.key);
printuni(p.value);
printf("}");
}

View File

@@ -0,0 +1,27 @@
#pragma once
#if __cplusplus
extern "C" {
#endif
#include "../base/base.h"
#include "../Autoarr/Autoarr.h"
typedef struct KVPair{
char* key;
Unitype value;
} KVPair;
declare_Autoarr(KVPair)
// proper way to clear a KVP
void KVPair_free(KVPair p);
// func to clear KVP array
void Autoarr_free_KVPair(Autoarr_KVPair* ar);
void printkvp(KVPair p);
#if __cplusplus
}
#endif

15
src/Hashtable/hash.c Normal file
View File

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

16
src/Hashtable/hash.h Normal file
View File

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