diff options
Diffstat (limited to '')
-rw-r--r-- | table.c | 42 |
1 files changed, 9 insertions, 33 deletions
@@ -53,8 +53,10 @@ static inline int compare(struct string a, struct string b) { } static inline uint64_t search(struct table tbl, struct string name, uint8_t *exists) { - if (tbl.len == 0) + if (tbl.len == 0) { + *exists = 0; return 0; + } size_t low = 0, high = tbl.len - 1; @@ -97,7 +99,7 @@ static inline uint64_t search(struct table tbl, struct string name, uint8_t *exi } int set_table_index(struct table *tbl, struct string name, void *ptr) { - uint8_t exists, err; + uint8_t exists; uint64_t index = search(*tbl, name, &exists); if (index == tbl->len) { @@ -135,42 +137,16 @@ int set_table_index(struct table *tbl, struct string name, void *ptr) { } void * get_table_index(struct table tbl, struct string name) { - if (tbl.len == 0) + uint8_t exists; + uint64_t index = search(tbl, name, &exists); + if (!exists) return 0; - size_t low = 0, high = tbl.len - 1; - - size_t mid = high/2; - - while (low != high) { - int val = compare(tbl.array[mid].name, name); - if (val == 0) { - return tbl.array[mid].ptr; - } else if (val > 0) { - low = mid + 1; - if (mid > low) - return 0; - if (low > high) - low = high; - } else { - high = mid - 1; - if (mid < high) - return 0; - if (high < low) - high = low; - } - - mid = low + ((high-low)/2); - } - - if (compare(tbl.array[mid].name, name) == 0) - return tbl.array[mid].ptr; - else - return 0; + return tbl.array[index].ptr; } void * remove_table_index(struct table *tbl, struct string name) { - uint8_t exists, err; + uint8_t exists; uint64_t index = search(*tbl, name, &exists); if (!exists) |