Added / started some infrastructure / boilerplate code.

This commit is contained in:
2024-10-30 00:28:11 -05:00
parent 8923a2006d
commit 2c85077031
12 changed files with 273 additions and 27 deletions
+57
View File
@@ -0,0 +1,57 @@
#include "../includes/dictionary.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEFAULT_DICT_SIZE 64
struct _dictionary {
int Capacity;
int Size;
KeyValPair** Pairs;
};
struct _dictionary* DictionaryCreate(void) {
struct _dictionary* dict = calloc(1, sizeof(Dictionary));
dict->Pairs = calloc(sizeof(KeyValPair*), DEFAULT_DICT_SIZE);
dict->Capacity = DEFAULT_DICT_SIZE;
return dict;
}
KeyValPair* KeyValPairCreate(const char* key, void* value) {
KeyValPair* pair = calloc(1, sizeof(KeyValPair));
pair->Key = key;
pair->Value = value;
return pair;
}
int DictionaryAdd(struct _dictionary* dict, const char* key, void* value) {
if (!dict) return 0;
if (dict->Size = dict->Capacity) {
dict->Pairs = realloc(dict->Pairs, sizeof(KeyValPair*) * dict->Capacity * 2);
}
dict->Pairs[dict->Size] = KeyValPairCreate(key, value);
dict->Size++;
return 1;
}
void* DictionaryGetValue(const struct _dictionary* dict, const char* key) {
if (!dict) return NULL;
for(int i = 0; i < dict->Size; i++) {
KeyValPair* pair = dict->Pairs[i];
if (strcmp(pair->Key, key) == 0) {
return pair->Value;
}
}
return NULL;
}