Autoarr_create and Autoarr_free signatures changed
This commit is contained in:
@@ -11,16 +11,15 @@ static const uint16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521};
|
||||
Hashtable* Hashtable_create(){
|
||||
Hashtable* ht=malloc(sizeof(Hashtable));
|
||||
ht->hein=HT_HEIN_MIN;
|
||||
ht->rows=malloc(HT_HEIGHTS[HT_HEIN_MIN]*sizeof(Autoarr(KeyValuePair)));
|
||||
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(KeyValuePair,ARR_BC,ARR_BL);
|
||||
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_KeyValuePair_clear(ht->rows+i);
|
||||
}
|
||||
for(uint16 i=0;i<HT_HEIGHTS[ht->hein];i++)
|
||||
Autoarr_free_KVPair(ht->rows[i]);
|
||||
free(ht->rows);
|
||||
free(ht);
|
||||
}
|
||||
@@ -30,34 +29,39 @@ 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(KeyValuePair)* newrows=malloc(HT_HEIGHTS[++ht->hein]*sizeof(Autoarr(KeyValuePair)));
|
||||
|
||||
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(KeyValuePair,ARR_BC,ARR_BL);
|
||||
newrows[i]=Autoarr_create(KVPair,ARR_BC,ARR_BL);
|
||||
|
||||
for(uint16 i=0;i<HT_HEIGHTS[ht->hein-1];i++){
|
||||
Autoarr(KeyValuePair)* ar=ht->rows+i;
|
||||
Autoarr(KVPair)* ar=ht->rows[i];
|
||||
uint32 arlen=Autoarr_length(ar);
|
||||
for(uint16 k=0;k<arlen;k++){
|
||||
KeyValuePair p=Autoarr_get(ar,k);
|
||||
KVPair p=Autoarr_get(ar,k);
|
||||
uint16 newrown=ihash(p.key)%HT_HEIGHTS[ht->hein];
|
||||
Autoarr(KeyValuePair)* newar=newrows+newrown;
|
||||
Autoarr(KVPair)* newar=newrows[newrown];
|
||||
Autoarr_add(newar,p);
|
||||
}
|
||||
Autoarr_clear(ar);
|
||||
// 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(KeyValuePair)* getrow(Hashtable* ht, char* key, bool can_expand){
|
||||
Autoarr(KeyValuePair)* ar=ht->rows+ihash(key)%HT_HEIGHTS[ht->hein];
|
||||
Autoarr(KVPair)* getrow(Hashtable* ht, char* key, bool can_expand){
|
||||
Autoarr(KVPair)* 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];
|
||||
ar=ht->rows[ihash(key)%HT_HEIGHTS[ht->hein]];
|
||||
return ar;
|
||||
}
|
||||
|
||||
|
||||
void Hashtable_add_pair(Hashtable* ht, KeyValuePair p){
|
||||
void Hashtable_add_pair(Hashtable* ht, KVPair p){
|
||||
Autoarr_add(getrow(ht,p.key,true),p);
|
||||
}
|
||||
void Hashtable_add(Hashtable* ht, char* key, Unitype u){
|
||||
@@ -66,25 +70,25 @@ void Hashtable_add(Hashtable* ht, char* key, Unitype u){
|
||||
|
||||
// returns null or pointer to value in hashtable
|
||||
Unitype* Hashtable_getptr(Hashtable* ht, char* key){
|
||||
Autoarr(KeyValuePair)* ar=getrow(ht,key,false);
|
||||
Autoarr(KVPair)* ar=getrow(ht,key,false);
|
||||
uint32 arlen=Autoarr_length(ar);
|
||||
for(uint32 i=0;i<arlen;i++){
|
||||
KeyValuePair* p=Autoarr_getptr(ar,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(KeyValuePair)* ar=getrow(ht,key,false);
|
||||
Autoarr(KVPair)* ar=getrow(ht,key,false);
|
||||
uint32 arlen=Autoarr_length(ar);
|
||||
for(uint32 i=0;i<arlen;i++){
|
||||
KeyValuePair p=Autoarr_get(ar,i);
|
||||
KVPair p=Autoarr_get(ar,i);
|
||||
if(cptr_compare(key,p.key)) return p.value;
|
||||
}
|
||||
return UniNull;
|
||||
}
|
||||
KeyValuePair Hashtable_get_pair(Hashtable* ht, char* key){
|
||||
KVPair Hashtable_get_pair(Hashtable* ht, char* key){
|
||||
return KVPair(key,Hashtable_get(ht,key));
|
||||
}
|
||||
bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){
|
||||
@@ -93,7 +97,7 @@ bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){
|
||||
return u.type!=Null;
|
||||
}
|
||||
|
||||
/* void Hashtable_set_pair(Hashtable* ht, KeyValuePair p){
|
||||
/* void Hashtable_set_pair(Hashtable* ht, KVPair p){
|
||||
if(Hashtable_try_get(ht,p.key, NULL)){
|
||||
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ extern "C" {
|
||||
|
||||
typedef struct Hashtable{
|
||||
uint8 hein; // height=HT_HEIGHTS[hein]
|
||||
Autoarr(KeyValuePair)* rows; // Autoarr[height]
|
||||
Autoarr(KVPair)** rows; // Autoarr[height]
|
||||
} Hashtable;
|
||||
|
||||
Hashtable* Hashtable_create();
|
||||
@@ -19,32 +19,32 @@ void Hashtable_free(Hashtable* ht);
|
||||
// amount of rows
|
||||
uint16 Hashtable_height(Hashtable* ht);
|
||||
|
||||
// adds charptr and value to new KeyValuePair
|
||||
// adds charptr and value to new KVPair
|
||||
// use cptr_copy() to create new string if needed
|
||||
#define KVPair(key,value) (KeyValuePair){key,value}
|
||||
#define KVPair(key,value) (KVPair){key,value}
|
||||
|
||||
//
|
||||
// don't add pairs with the same keys,
|
||||
// or something weird will happen
|
||||
//
|
||||
void Hashtable_add_pair(Hashtable* ht, KeyValuePair p);
|
||||
void Hashtable_add_pair(Hashtable* ht, KVPair 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);
|
||||
KVPair 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_pair(Hashtable* ht, KVPair 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(KVPair)* AR=HT->rows[h];\
|
||||
Autoarr_foreach(AR, EL, codeblock);\
|
||||
}\
|
||||
})
|
||||
|
||||
@@ -1,25 +1,21 @@
|
||||
#include "KeyValuePair.h"
|
||||
|
||||
define_Autoarr(KeyValuePair)
|
||||
define_Autoarr(KVPair)
|
||||
|
||||
|
||||
// proper way to clear a KVP
|
||||
void KeyValuePair_free(KeyValuePair p){
|
||||
void KVPair_free(KVPair 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 Autoarr_free_KVPair(Autoarr_KVPair* ar){
|
||||
Autoarr_foreach(ar,k,KVPair_free(k));
|
||||
Autoarr_free(ar);
|
||||
}
|
||||
|
||||
void printkvp(KeyValuePair p){
|
||||
void printkvp(KVPair p){
|
||||
printf("{\"%s\", ",p.key);
|
||||
printuni(p.value);
|
||||
printf("}");
|
||||
|
||||
@@ -7,20 +7,20 @@ extern "C" {
|
||||
#include "../base/base.h"
|
||||
#include "../Autoarr/Autoarr.h"
|
||||
|
||||
typedef struct KeyValuePair{
|
||||
typedef struct KVPair{
|
||||
char* key;
|
||||
Unitype value;
|
||||
} KeyValuePair;
|
||||
} KVPair;
|
||||
|
||||
declare_Autoarr(KeyValuePair)
|
||||
declare_Autoarr(KVPair)
|
||||
|
||||
// proper way to clear a KVP
|
||||
void KeyValuePair_free(KeyValuePair p);
|
||||
void KVPair_free(KVPair p);
|
||||
|
||||
// func to clear KVP array
|
||||
void Autoarr_KeyValuePair_clear(Autoarr_KeyValuePair* ar);
|
||||
void Autoarr_free_KVPair(Autoarr_KVPair* ar);
|
||||
|
||||
void printkvp(KeyValuePair p);
|
||||
void printkvp(KVPair p);
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user