Skip to content

Commit 2a79d06

Browse files
committed
Minor cleanup of new hashmap implementation
1 parent b58e0a7 commit 2a79d06

1 file changed

Lines changed: 14 additions & 18 deletions

File tree

extlib.h

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,15 +1472,13 @@ int ext_cmd_write(const char *cmd, const void *data, size_t size);
14721472
} \
14731473
ext__hmap_tmp_(hmap).key = (entry_key); \
14741474
ext__hmap_tmp_(hmap).value = (entry_val); \
1475-
size_t hash_ = hash_fn(&ext__hmap_tmp_(hmap).key, sizeof(ext__hmap_tmp_(hmap).key)); \
1476-
if(hash_ < 2) hash_ += 2; \
1477-
ext__hmap_find_((hmap)->entries, (hmap)->hashes, (hmap)->capacity, \
1478-
sizeof(*(hmap)->entries), sizeof(ext__hmap_tmp_(hmap).key), (hash_fn), \
1479-
(cmp_fn)); \
1480-
size_t idx_ = (hmap)->hashes[EXT_HMAP_TMP_SLOT]; \
1481-
if(!EXT_HMAP_IS_VALID((hmap)->hashes[idx_])) (hmap)->size++; \
1482-
(hmap)->entries[idx_] = ext__hmap_tmp_(hmap); \
1483-
(hmap)->hashes[idx_] = hash_; \
1475+
size_t hash = ext__hmap_find_((hmap)->entries, (hmap)->hashes, (hmap)->capacity, \
1476+
sizeof(*(hmap)->entries), sizeof(ext__hmap_tmp_(hmap).key), \
1477+
(hash_fn), (cmp_fn)); \
1478+
size_t idx = (hmap)->hashes[EXT_HMAP_TMP_SLOT]; \
1479+
if(!EXT_HMAP_IS_VALID((hmap)->hashes[idx])) (hmap)->size++; \
1480+
(hmap)->entries[idx] = ext__hmap_tmp_(hmap); \
1481+
(hmap)->hashes[idx] = hash; \
14841482
} while(0)
14851483

14861484
// Returns a pointer to the entry with the given key (custom hash/cmp), or NULL.
@@ -1524,8 +1522,8 @@ int ext_cmd_write(const char *cmd, const void *data, size_t size);
15241522
: 0, \
15251523
ext__hmap_tmp_(hmap).key = (entry_key), ext__hmap_tmp_(hmap).value = (entry_val), \
15261524
ext__hmap_find_default_((hmap)->entries, (hmap)->hashes, &(hmap)->size, (hmap)->capacity, \
1527-
sizeof(*(hmap)->entries), sizeof((hmap)->entries[0].key), (hash_fn), \
1528-
(cmp_fn)), \
1525+
sizeof(*(hmap)->entries), sizeof(ext__hmap_tmp_(hmap).key), \
1526+
(hash_fn), (cmp_fn)), \
15291527
(hmap)->entries + (hmap)->hashes[EXT_HMAP_TMP_SLOT])
15301528

15311529
// Returns a pointer to the existing entry with the given key, inserting a new
@@ -1551,14 +1549,12 @@ int ext_cmd_write(const char *cmd, const void *data, size_t size);
15511549
do { \
15521550
if(!(hmap)->size) break; \
15531551
ext__hmap_tmp_(hmap).key = (entry_key); \
1554-
size_t hash_ = hash_fn(&ext__hmap_tmp_(hmap).key, sizeof(ext__hmap_tmp_(hmap).key)); \
1555-
if(hash_ < 2) hash_ += 2; \
15561552
ext__hmap_find_((hmap)->entries, (hmap)->hashes, (hmap)->capacity, \
15571553
sizeof(*(hmap)->entries), sizeof(ext__hmap_tmp_(hmap).key), (hash_fn), \
15581554
(cmp_fn)); \
1559-
size_t idx_ = (hmap)->hashes[EXT_HMAP_TMP_SLOT]; \
1560-
if(EXT_HMAP_IS_VALID((hmap)->hashes[idx_])) { \
1561-
(hmap)->hashes[idx_] = EXT_HMAP_TOMB_MARK; \
1555+
size_t idx = (hmap)->hashes[EXT_HMAP_TMP_SLOT]; \
1556+
if(EXT_HMAP_IS_VALID((hmap)->hashes[idx])) { \
1557+
(hmap)->hashes[idx] = EXT_HMAP_TOMB_MARK; \
15621558
(hmap)->size--; \
15631559
} \
15641560
} while(0)
@@ -1914,7 +1910,7 @@ static inline size_t ext__hmap_find_(const void *entries, size_t *hashes, size_t
19141910
if(hash < 2) hash += 2;
19151911

19161912
size_t idx = hash & cap;
1917-
bool tomb_found = 0;
1913+
bool tomb_found = false;
19181914
size_t tomb_idx = 0;
19191915
for(;;) {
19201916
size_t bucket = hashes[idx + 1];
@@ -1923,7 +1919,7 @@ static inline size_t ext__hmap_find_(const void *entries, size_t *hashes, size_t
19231919
hashes[EXT_HMAP_TMP_SLOT] = tomb_found ? tomb_idx : idx + 1;
19241920
break;
19251921
} else if(!tomb_found) {
1926-
tomb_found = 1;
1922+
tomb_found = true;
19271923
tomb_idx = idx + 1;
19281924
}
19291925
} else if(bucket == (hash) &&

0 commit comments

Comments
 (0)