This commit is contained in:
2026-05-12 15:44:24 -05:00
parent dc4d588675
commit 74d860976a
4 changed files with 88 additions and 10 deletions

View File

@@ -1,23 +1,85 @@
#include "stack.c"
#include <stdlib.h>
#include <string.h>
#include "khash.h"
typedef struct {
} HeapValue;
KHASH_MAP_INIT_INT(heap, HeapValue)
typedef int HEAP_KEY;
typedef enum {
TH_STRING, TH_ARRAY, TH_TABLE,
} TYC_HEAP_TYPE;
typedef struct {
khash_t(heap) *items;
TYC_HEAP_TYPE type;
union {
char* str;
// TODO - array and table
} value;
} HeapValue;
KHASH_MAP_INIT_INT(HEAP, HeapValue)
typedef struct {
khash_t(HEAP) *items;
} Heap;
static void heap_init(Heap* h)
{
h->items = kh_init(heap);
h->items = kh_init(HEAP);
}
static void heap_finalize(Heap* h)
{
kh_destroy(heap, h->items);
for (khiter_t k = kh_begin(h->items); k != kh_end(h->items); ++k) {
HeapValue value = kh_value(h->items, k);
switch (value.type) {
case TH_STRING:
free(value.value.str);
break;
case TH_ARRAY:
abort(); // not implemented yet
break;
case TH_TABLE:
abort(); // not implemented yet
break;
}
}
kh_destroy(HEAP, h->items);
}
static HEAP_KEY heap_add_string(Heap* h, const char* value)
{
int ret;
khiter_t k;
HEAP_KEY key;
do {
key = rand();
k = kh_get(HEAP, h->items, key);
} while (k != kh_end(h->items));
k = kh_put(HEAP, h->items, key, &ret);
kh_value(h->items, k) = (HeapValue) {
.type = TH_STRING,
.value = { .str = strdup(value) }
};
return key;
}
#include <stdio.h>
static TYC_RESULT heap_get_string(Heap* h, HEAP_KEY key, const char** value)
{
khiter_t k = kh_get(HEAP, h->items, key);
bool is_missing = (k == kh_end(h->items));
if (is_missing)
return T_ERR_HEAP_KEY_NOT_FOUND;
*value = kh_value(h->items, k).value.str;
return T_OK;
}
static size_t heap_size(Heap* h)
{
return kh_size(h->items);
}

View File

@@ -8,6 +8,7 @@ typedef enum {
typedef enum {
T_OK = 0,
T_ERR_STACK_UNDERFLOW = -1, T_ERR_STACK_FP_UNDERFLOW = -2, T_ERR_STACK_ACCESS_OUT_OF_RANGE = -3,
T_ERR_HEAP_KEY_NOT_FOUND = -10,
} TYC_RESULT;
#endif //TYCHE_TYCHE_H