-
-
Save minimum-necessary-change/31133a7c4af0175c8be22cf874a875e0 to your computer and use it in GitHub Desktop.
Revisions
-
kylef revised this gist
Apr 1, 2010 . 1 changed file with 36 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -15,8 +15,11 @@ dict_t **dict_alloc(void) { return malloc(sizeof(dict_t)); } void dict_dealloc(dict_t *dict) { dict_t *ptr; for (ptr = dict; ptr != NULL; ptr = ptr->next) { free(ptr); } } void *dict_getItem(dict_t *dict, char *key) { @@ -64,6 +67,28 @@ void dict_addItem(dict_t **dict, char *key, void *value) { *dict = d; } int dict_size(dict_t *dict) { int size = 0; dict_t *ptr; for (ptr = dict; ptr != NULL; ptr = ptr->next) { size++; } return size; } dict_t **dict_invert(dict_t *dict) { dict_t **invert = dict_alloc(); dict_t *ptr; for (ptr = dict; ptr != NULL; ptr = ptr->next) { dict_addItem(invert, ptr->value, ptr->key); } return invert; } #ifdef TEST int main(int argc, char **argv) { @@ -75,27 +100,32 @@ int main(int argc, char **argv) { dict_addItem(dict, "bar", "foo"); /* and print their values */ printf("%s %s (size: %i)\n", (char *)dict_getItem(*dict, "foo"), (char *)dict_getItem(*dict, "bar"), dict_size(*dict)); /* lets delete them */ dict_delItem(dict, "foo"); dict_delItem(dict, "bar"); /* see, their gone, there NULL */ printf("%s %s (size: %i)\n", (char *)dict_getItem(*dict, "foo"), (char *)dict_getItem(*dict, "bar"), dict_size(*dict)); /* add them again to proof it works */ dict_addItem(dict, "foo", "bar"); dict_addItem(dict, "bar", "foo"); dict_addItem(dict, "bar", "pan"); /* see, here */ printf("%s %s (size: %i)\n", (char *)dict_getItem(*dict, "foo"), (char *)dict_getItem(*dict, "bar"), dict_size(*dict)); /* invert the dict */ dict_t **invert = dict_invert(*dict); printf("%s %s (size: %i)\n", (char *)dict_getItem(*invert, "bar"), (char *)dict_getItem(*invert, "pan"), dict_size(*invert)); dict_dealloc(*invert); dict_delItem(dict, "foo"); dict_delItem(dict, "bar"); dict_dealloc(*dict); return 0; } -
kylef revised this gist
Apr 1, 2010 . 1 changed file with 20 additions and 20 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -11,15 +11,15 @@ typedef struct dict_t_struct { struct dict_t_struct *next; } dict_t; dict_t **dict_alloc(void) { return malloc(sizeof(dict_t)); } void dict_dealloc(dict_t **dict) { free(dict); } void *dict_getItem(dict_t *dict, char *key) { dict_t *ptr; for (ptr = dict; ptr != NULL; ptr = ptr->next) { if (strcmp(ptr->key, key) == 0) { @@ -30,7 +30,7 @@ void *getItem(dict_t *dict, char *key) { return NULL; } void dict_delItem(dict_t **dict, char *key) { dict_t *ptr, *prev; for (ptr = *dict, prev = NULL; ptr != NULL; prev = ptr, ptr = ptr->next) { if (strcmp(ptr->key, key) == 0) { @@ -54,8 +54,8 @@ void delItem(dict_t **dict, char *key) { } } void dict_addItem(dict_t **dict, char *key, void *value) { dict_delItem(dict, key); /* If we already have a item with this key, delete it. */ dict_t *d = malloc(sizeof(struct dict_t_struct)); d->key = malloc(strlen(key)+1); strcpy(d->key, key); @@ -68,34 +68,34 @@ void addItem(dict_t **dict, char *key, void *value) { int main(int argc, char **argv) { /* Create a dict */ dict_t **dict = dict_alloc(); /* lets add foo, and bar to the dict */ dict_addItem(dict, "foo", "bar"); dict_addItem(dict, "bar", "foo"); /* and print their values */ printf("%s %s\n", (char *)dict_getItem(*dict, "foo"), (char *)dict_getItem(*dict, "bar")); /* lets delete them */ dict_delItem(dict, "foo"); dict_delItem(dict, "bar"); /* see, their gone, there NULL */ printf("%s %s\n", (char *)dict_getItem(*dict, "foo"), (char *)dict_getItem(*dict, "bar")); /* add them again to proof it works */ dict_addItem(dict, "foo", "bar"); dict_addItem(dict, "bar", "foo"); dict_addItem(dict, "bar", "pan"); /* see, here */ printf("%s %s\n", (char *)dict_getItem(*dict, "foo"), (char *)dict_getItem(*dict, "bar")); dict_delItem(dict, "foo"); dict_delItem(dict, "bar"); dict_dealloc(dict); return 0; } -
kylef revised this gist
Feb 1, 2010 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -3,6 +3,8 @@ #include <stdlib.h> #include <string.h> #define TEST TRUE /* Comment this line out to compile without a main function (used when including into another application). */ typedef struct dict_t_struct { char *key; void *value; @@ -62,6 +64,8 @@ void addItem(dict_t **dict, char *key, void *value) { *dict = d; } #ifdef TEST int main(int argc, char **argv) { /* Create a dict */ dict_t **dict = dictAlloc(); @@ -95,3 +99,5 @@ int main(int argc, char **argv) { return 0; } #endif -
kylef revised this gist
Feb 1, 2010 . 1 changed file with 11 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -9,6 +9,14 @@ typedef struct dict_t_struct { struct dict_t_struct *next; } dict_t; dict_t **dictAlloc(void) { return malloc(sizeof(dict_t)); } void dictDealloc(dict_t **dict) { free(dict); } void *getItem(dict_t *dict, char *key) { dict_t *ptr; for (ptr = dict; ptr != NULL; ptr = ptr->next) { @@ -56,7 +64,7 @@ void addItem(dict_t **dict, char *key, void *value) { int main(int argc, char **argv) { /* Create a dict */ dict_t **dict = dictAlloc(); /* lets add foo, and bar to the dict */ addItem(dict, "foo", "bar"); @@ -83,5 +91,7 @@ int main(int argc, char **argv) { delItem(dict, "foo"); delItem(dict, "bar"); dictDealloc(dict); return 0; } -
kylef revised this gist
Feb 1, 2010 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -63,22 +63,22 @@ int main(int argc, char **argv) { addItem(dict, "bar", "foo"); /* and print their values */ printf("%s %s\n", (char *)getItem(*dict, "foo"), (char *)getItem(*dict, "bar")); /* lets delete them */ delItem(dict, "foo"); delItem(dict, "bar"); /* see, their gone, there NULL */ printf("%s %s\n", (char *)getItem(*dict, "foo"), (char *)getItem(*dict, "bar")); /* add them again to proof it works */ addItem(dict, "foo", "bar"); addItem(dict, "bar", "foo"); addItem(dict, "bar", "pan"); /* see, here */ printf("%s %s\n", (char *)getItem(*dict, "foo"), (char *)getItem(*dict, "bar")); delItem(dict, "foo"); delItem(dict, "bar"); -
kylef created this gist
Mar 27, 2009 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,87 @@ /* A key/value dict system in C */ #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct dict_t_struct { char *key; void *value; struct dict_t_struct *next; } dict_t; void *getItem(dict_t *dict, char *key) { dict_t *ptr; for (ptr = dict; ptr != NULL; ptr = ptr->next) { if (strcmp(ptr->key, key) == 0) { return ptr->value; } } return NULL; } void delItem(dict_t **dict, char *key) { dict_t *ptr, *prev; for (ptr = *dict, prev = NULL; ptr != NULL; prev = ptr, ptr = ptr->next) { if (strcmp(ptr->key, key) == 0) { if (ptr->next != NULL) { if (prev == NULL) { *dict = ptr->next; } else { prev->next = ptr->next; } } else if (prev != NULL) { prev->next = NULL; } else { *dict = NULL; } free(ptr->key); free(ptr); return; } } } void addItem(dict_t **dict, char *key, void *value) { delItem(dict, key); /* If we already have a item with this key, delete it. */ dict_t *d = malloc(sizeof(struct dict_t_struct)); d->key = malloc(strlen(key)+1); strcpy(d->key, key); d->value = value; d->next = *dict; *dict = d; } int main(int argc, char **argv) { /* Create a dict */ dict_t **dict = malloc(sizeof(dict_t)); /* lets add foo, and bar to the dict */ addItem(dict, "foo", "bar"); addItem(dict, "bar", "foo"); /* and print their values */ printf("%s %s\n", getItem(*dict, "foo"), getItem(*dict, "bar")); /* lets delete them */ delItem(dict, "foo"); delItem(dict, "bar"); /* see, their gone, there NULL */ printf("%s %s\n", getItem(*dict, "foo"), getItem(*dict, "bar")); /* add them again to proof it works */ addItem(dict, "foo", "bar"); addItem(dict, "bar", "foo"); addItem(dict, "bar", "pan"); /* see, here */ printf("%s %s\n", getItem(*dict, "foo"), getItem(*dict, "bar")); delItem(dict, "foo"); delItem(dict, "bar"); return 0; }