Switched out my map impl for a GTree

This commit is contained in:
Scoopta 2020-01-19 22:09:04 -08:00
parent 22c32c0f2c
commit 868710726e
2 changed files with 23 additions and 65 deletions

View File

@ -23,6 +23,8 @@
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <gmodule.h>
struct map* map_init(void); struct map* map_init(void);
struct map* map_init_void(void); struct map* map_init_void(void);

View File

@ -18,77 +18,47 @@
#include <map.h> #include <map.h>
struct map { struct map {
char* key; GTree* tree;
void* value;
size_t size;
bool mman; bool mman;
struct map* head, *left, *right;
}; };
static gint compare(gconstpointer p1, gconstpointer p2, gpointer data) {
(void) data;
const char* str1 = p1;
const char* str2 = p2;
return strcmp(str1, str2);
}
struct map* map_init(void) { struct map* map_init(void) {
struct map* map = calloc(1, sizeof(struct map)); struct map* map = calloc(1, sizeof(struct map));
map->head = map; map->tree = g_tree_new_full(compare, NULL, free, free);
map->mman = true; map->mman = true;
return map; return map;
} }
struct map* map_init_void(void) { struct map* map_init_void(void) {
struct map* map = map_init(); struct map* map = calloc(1, sizeof(struct map));
map->tree = g_tree_new_full(compare, NULL, free, NULL);
map->mman = false; map->mman = false;
return map; return map;
} }
void map_free(struct map* map) { void map_free(struct map* map) {
if(map->left != NULL) { g_tree_destroy(map->tree);
map_free(map->left);
}
if(map->right != NULL) {
map_free(map->right);
}
if(map->key != NULL) {
free(map->key);
}
if(map->value != NULL && map->head->mman) {
free(map->value);
}
free(map); free(map);
} }
static void put(struct map* map, const char* key, void* value) { static void put(struct map* map, const char* key, void* value) {
if(map->key == NULL) { char* k = strdup(key);
map->key = strdup(key); char* v = value;
if(value != NULL && map->head->mman) { if(map->mman && value != NULL) {
map->value = strdup(value); v = strdup(value);
} else {
map->value = value;
}
++map->head->size;
} else if(strcmp(key, map->key) < 0) {
if(map->left == NULL) {
map->left = map_init();
map->left->head = map->head;
}
put(map->left, key, value);
} else if(strcmp(key, map->key) > 0) {
if(map->right == NULL) {
map->right = map_init();
map->right->head = map->head;
}
put(map->right, key, value);
} else {
if(map->value != NULL && map->head->mman) {
free(map->value);
}
if(value != NULL && map->head->mman) {
map->value = strdup(value);
} else {
map->value = value;
}
} }
g_tree_insert(map->tree, k, v);
} }
bool map_put(struct map* map, const char* key, char* value) { bool map_put(struct map* map, const char* key, char* value) {
if(map->head->mman) { if(map->mman) {
put(map, key, value); put(map, key, value);
return true; return true;
} else { } else {
@ -98,7 +68,7 @@ bool map_put(struct map* map, const char* key, char* value) {
} }
bool map_put_void(struct map* map, const char* key, void* value) { bool map_put_void(struct map* map, const char* key, void* value) {
if(map->head->mman) { if(map->mman) {
fprintf(stderr, "This is an managed map please use map_put\n"); fprintf(stderr, "This is an managed map please use map_put\n");
return false; return false;
} else { } else {
@ -108,21 +78,7 @@ bool map_put_void(struct map* map, const char* key, void* value) {
} }
void* map_get(struct map* map, const char* key) { void* map_get(struct map* map, const char* key) {
if(map->key == NULL) { return g_tree_lookup(map->tree, key);
return NULL;
} else if(strcmp(key, map->key) < 0) {
if(map->left == NULL) {
return NULL;
}
return map_get(map->left, key);
} else if(strcmp(key, map->key) > 0) {
if(map->right == NULL) {
return NULL;
}
return map_get(map->right, key);
} else {
return map->value;
}
} }
bool map_contains(struct map* map, const char* key) { bool map_contains(struct map* map, const char* key) {
@ -130,5 +86,5 @@ bool map_contains(struct map* map, const char* key) {
} }
size_t map_size(struct map* map) { size_t map_size(struct map* map) {
return map->size; return g_tree_nnodes(map->tree);
} }