Skip to content

Commit aa5e70f

Browse files
authored
Merge pull request #1 from bamless/hashmap-rework
- Breaking changes on hashmap implementation: Reworked the hashmap implementation to make possible the usage of `ext_hmap_get` and `ext_hmap_get_default` family of functions as expressions. Now it is possible to do things like: ```c Entry* e = hmap_get_cstr(&hmap, "key"); if(e != NULL) { ... } ``` - Added `Ext_Array` and `Ext_HashMap`/`Ext_Entry` macros to define required struct layout for both dynamic arrays and hashmap in-line. This makes it possible to skip declaring the full struct by hand when we do not need to reference it by name (i.e. create variables to it, pass it to functions, etc.): ```c Array(int) int_array = {0}; HashMap(char*, int) int_map = {0}; ``` Declaring full struct layout by hand is still supported. - Breaking change: removed deprecated functions and macros: 1. `ext_strdup_alloc` and `ext_memdup_alloc` 2. `EXT_DEFER_LOOP`
2 parents 93238a3 + c3765a9 commit aa5e70f

4 files changed

Lines changed: 570 additions & 243 deletions

File tree

examples/01_dynamic_array.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#define EXTLIB_IMPL
1212
#include "../extlib.h"
1313

14+
// Could also create this typedef with `Array` utility macro:
15+
// typedef Array(StringSlice) Lines;
1416
typedef struct {
1517
StringSlice *items;
1618
size_t size, capacity;
@@ -22,6 +24,8 @@ static int qsort_cmp(const void *a, const void *b) {
2224
}
2325

2426
int main(void) {
27+
// Could also define this dynamic array inline with:
28+
// Array(StringSlice) lines = {0};
2529
Lines lines = {0};
2630
StringBuffer file = {0};
2731

examples/02_hashmap.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ int main(int argc, char **argv) {
121121
StringSlice word = ss_split_once_ws(&file_slice);
122122
if(word.size == 0) continue;
123123

124-
WordFreq *e;
125-
hmap_get_default_ss(&words_freq, word, 0, &e);
124+
WordFreq *e = hmap_get_default_ss(&words_freq, word, 0);
126125
ASSERT(e != NULL, "default entry shouldn't be NULL");
127126

128127
e->value++;

0 commit comments

Comments
 (0)