Created
August 21, 2011 01:11
-
-
Save haileys/1159930 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <string.h> | |
#include <gc.h> | |
#include "dict.h" | |
#include "string.h" | |
kari_dict_t* new_kari_dict() | |
{ | |
kari_dict_t* d = (kari_dict_t*)GC_MALLOC(sizeof(kari_dict_t)); | |
d->branch = (kari_dict_branch_t*)GC_MALLOC(sizeof(kari_dict_branch_t)); | |
return d; | |
} | |
bool kari_dict_set(kari_dict_t* dict, char* key, void* value) | |
{ | |
kari_dict_branch_t* branch = dict->branch; | |
while(*key) { | |
if(branch->branches[(int)*key] == NULL) { | |
branch->branches[(int)*key] = (kari_dict_branch_t*)GC_MALLOC(sizeof(kari_dict_branch_t)); | |
} | |
branch = branch->branches[(int)*key]; | |
key++; | |
} | |
branch->value = value; | |
return true; | |
} | |
bool kari_dict_exists(kari_dict_t* dict, char* key) | |
{ | |
kari_dict_branch_t* e = kari_dict_find(dict, key); | |
if(e == NULL) { | |
return false; | |
} else { | |
return true; | |
} | |
} | |
void kari_dict_remove(kari_dict_t* dict, char* key) | |
{ | |
kari_dict_branch_t* b = kari_dict_find(dict, key); | |
b->value = NULL; | |
} | |
kari_dict_branch_t* kari_dict_find(kari_dict_t* dict, char* key) | |
{ | |
kari_dict_branch_t* branch = dict->branch; | |
while(*key) { | |
if(branch->branches[(int)*key] == NULL) { | |
return NULL; | |
} | |
branch = branch->branches[(int)*key]; | |
key++; | |
} | |
return branch; | |
} | |
void* kari_dict_find_value(kari_dict_t* dict, char* key) | |
{ | |
kari_dict_branch_t* e = kari_dict_find(dict, key); | |
if(e == NULL) { | |
return NULL; | |
} else { | |
return e->value; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _DICT_H | |
#define _DICT_H | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <stdbool.h> | |
typedef struct kari_dict_branch { | |
void* value; | |
struct kari_dict_branch* branches[256]; | |
} kari_dict_branch_t; | |
typedef struct kari_dict { | |
kari_dict_branch_t* branch; | |
} kari_dict_t; | |
kari_dict_t* new_kari_dict(); | |
bool kari_dict_set(kari_dict_t* dict, char* key, void* value); | |
bool kari_dict_exists(kari_dict_t* dict, char* key); | |
void kari_dict_remove(kari_dict_t* dict, char* key); | |
kari_dict_branch_t* kari_dict_find(kari_dict_t* dict, char* key); | |
void* kari_dict_find_value(kari_dict_t* dict, char* key); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment