moved files from DTLib repo

This commit is contained in:
2022-03-18 20:39:06 +03:00
commit 20ce758528
37 changed files with 1720 additions and 0 deletions

101
Hashtable/Hashtable.c Normal file
View File

@@ -0,0 +1,101 @@
#include "Hashtable.h"
// amount of rows
#define HT_HEIN_MIN 0
#define HT_HEIN_MAX 5
static const uint16 HT_HEIGHTS[]={61,257,1021,4099,16381,65521};
#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(KeyValuePair)));
for(uint16 i=0;i<HT_HEIGHTS[HT_HEIN_MIN];i++)
ht->rows[i]=Autoarr_create(KeyValuePair,ARR_BC,ARR_BL);
return ht;
}
void Hashtable_free(Hashtable* ht){
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++){
Autoarr_KeyValuePair_clear(ht->rows+i);
}
free(ht->rows);
free(ht);
}
uint32 Hashtable_height(Hashtable* ht){ return HT_HEIGHTS[ht->hein]; }
void Hashtable_expand(Hashtable* ht){
if(ht->hein>=HT_HEIN_MAX) throw(ERR_MAXLENGTH);
Autoarr(KeyValuePair)* newrows=malloc(HT_HEIGHTS[++ht->hein]*sizeof(Autoarr(KeyValuePair)));
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++)
newrows[i]=Autoarr_create(KeyValuePair,ARR_BC,ARR_BL);
for(uint16 i=0;i<HT_HEIGHTS[ht->hein-1];i++){
Autoarr(KeyValuePair)* ar=ht->rows+i;
uint32 arlen=Autoarr_length(ar);
for(uint16 k=0;k<arlen;k++){
KeyValuePair p=Autoarr_get(ar,k);
uint16 newrown=ihash(p.key)%HT_HEIGHTS[ht->hein];
Autoarr(KeyValuePair)* newar=newrows+newrown;
Autoarr_add(newar,p);
}
Autoarr_clear(ar);
}
free(ht->rows);
ht->rows=newrows;
}
Autoarr(KeyValuePair)* getrow(Hashtable* ht, char* key, bool can_expand){
Autoarr(KeyValuePair)* ar=ht->rows+ihash(key)%HT_HEIGHTS[ht->hein];
if(can_expand && Autoarr_length(ar)==Autoarr_max_length(ar))
optime("expand",1,(Hashtable_expand(ht)));
ar=ht->rows+ihash(key)%HT_HEIGHTS[ht->hein];
return ar;
}
void Hashtable_add_pair(Hashtable* ht, KeyValuePair p){
Autoarr_add(getrow(ht,p.key,true),p);
}
void Hashtable_add(Hashtable* ht, char* key, Unitype u){
Hashtable_add_pair(ht,KVPair(key,u));
}
//returns null or pointer to value in hashtable
Unitype* Hashtable_getptr(Hashtable* ht, char* key){
Autoarr(KeyValuePair)* ar=getrow(ht,key,false);
uint32 arlen=Autoarr_length(ar);
for(uint32 i=0;i<arlen;i++){
KeyValuePair* p=Autoarr_getptr(ar,i);
if(cptr_compare(key,p->key)) return &p->value;
}
return NULL;
}
Unitype Hashtable_get(Hashtable* ht, char* key){
Autoarr(KeyValuePair)* ar=getrow(ht,key,false);
uint32 arlen=Autoarr_length(ar);
for(uint32 i=0;i<arlen;i++){
KeyValuePair p=Autoarr_get(ar,i);
if(cptr_compare(key,p.key)) return p.value;
}
return UniNull;
}
KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key){
return KVPair(key,Hashtable_get(ht,key));
}
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){
Unitype u=Hashtable_get(ht,key);
*output=u;
return u.type!=Null;
}
/* void Hashtable_set_pair(Hashtable* ht, KeyValuePair p){
if(Hashtable_try_get(ht,p.key, NULL)){
}
}
void Hashtable_set(Hashtable* ht, char* key, Unitype u){ Hashtable_set_pair(ht,KVPair(key,u)); } */

43
Hashtable/Hashtable.h Normal file
View File

@@ -0,0 +1,43 @@
#pragma once
#include "../base/base.h"
#include "hash.h"
#include "KeyValuePair.h"
typedef struct Hashtable{
uint8 hein; //height=HT_HEIGHTS[hein]
Autoarr(KeyValuePair)* rows; // Autoarr[height]
} Hashtable;
Hashtable* Hashtable_create();
void Hashtable_free(Hashtable* ht);
//amount of rows
uint32 Hashtable_height(Hashtable* ht);
//adds charptr and value to new KeyValuePair
//use cptr_copy() to create new string if needed
#define KVPair(key,value) (KeyValuePair){key,value}
void Hashtable_add_pair(Hashtable* ht, KeyValuePair p);
void Hashtable_add(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);
KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key);
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output);
//not implemented yet
void Hashtable_set_pair(Hashtable* ht, KeyValuePair p);
void Hashtable_set(Hashtable* ht, char* key, Unitype u);
#define Hashtable_foreach(HT, EL, codeblock)({\
uint16 hmax=Hashtable_height(HT);\
for(uint16 h=0; h<hmax; h++){\
Autoarr(KeyValuePair)* AR=HT->rows+h;\
Autoarr_foreach(AR, EL, codeblock);\
}\
})

26
Hashtable/KeyValuePair.c Normal file
View File

@@ -0,0 +1,26 @@
#include "KeyValuePair.h"
define_Autoarr(KeyValuePair)
//proper way to clear a KVP
void KeyValuePair_free(KeyValuePair p){
free(p.key);
Unitype_free(p.value);
}
//func for KVP array clearing
void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar){
for(uint16 blockI=0; blockI < ar->blocks_count-1; blockI++)
for(uint16 elemI=0; elemI < ar->max_block_length; elemI++)
KeyValuePair_free(ar->values[blockI][elemI]);
for(uint16 elemI=0; elemI < ar->block_length; elemI++)
KeyValuePair_free(ar->values[ar->blocks_count-1][elemI]);
Autoarr_clear(ar);
}
void printkvp(KeyValuePair p){
printf("{\"%s\", ",p.key);
printuni(p.value);
printf("}");
}

17
Hashtable/KeyValuePair.h Normal file
View File

@@ -0,0 +1,17 @@
#include "../base/base.h"
#include "../Autoarr/Autoarr.h"
typedef struct KeyValuePair{
char* key;
Unitype value;
} KeyValuePair;
declare_Autoarr(KeyValuePair)
//proper way to clear a KVP
void KeyValuePair_free(KeyValuePair p);
//func to clear KVP array
void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar);
void printkvp(KeyValuePair p);

15
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;
}

8
Hashtable/hash.h Normal file
View File

@@ -0,0 +1,8 @@
#pragma once
#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);