Impl. hash_map iter, clang-format
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 52s

This commit is contained in:
Stephen Seo 2024-09-01 15:11:57 +09:00
parent 2e46790ece
commit aeb8eff350
19 changed files with 137 additions and 57 deletions

View file

@ -36,6 +36,12 @@ typedef struct SDArchiverHashMapKeyData {
unsigned int key_size; unsigned int key_size;
} SDArchiverHashMapKeyData; } SDArchiverHashMapKeyData;
typedef struct SDArchiverInternalIterContext {
int (*iter_check_fn)(void *, unsigned int, void *, void *);
int ret;
void *user_data;
} SDArchiverInternalIterContext;
void simple_archiver_hash_map_internal_cleanup_data(void *data) { void simple_archiver_hash_map_internal_cleanup_data(void *data) {
SDArchiverHashMapData *hash_map_data = data; SDArchiverHashMapData *hash_map_data = data;
if (hash_map_data->value) { if (hash_map_data->value) {
@ -248,3 +254,32 @@ int simple_archiver_hash_map_remove(SDArchiverHashMap *hash_map, void *key,
return 2; return 2;
} }
} }
int simple_archiver_internal_hash_map_bucket_iter_fn(void *data, void *ud) {
SDArchiverHashMapData *hash_map_data = data;
SDArchiverInternalIterContext *ctx = ud;
ctx->ret = ctx->iter_check_fn(hash_map_data->key, hash_map_data->key_size,
hash_map_data->value, ctx->user_data);
if (ctx->ret != 0) {
return 1;
} else {
return 0;
}
}
int simple_archiver_hash_map_iter(const SDArchiverHashMap *hash_map,
int (*iter_check_fn)(void *, unsigned int,
void *, void *),
void *user_data) {
SDArchiverInternalIterContext ctx;
ctx.iter_check_fn = iter_check_fn;
ctx.ret = 0;
ctx.user_data = user_data;
for (unsigned int idx = 0; idx < hash_map->buckets_size; ++idx) {
simple_archiver_list_get(hash_map->buckets[idx],
simple_archiver_internal_hash_map_bucket_iter_fn,
&ctx);
}
return ctx.ret;
}

View file

@ -53,4 +53,15 @@ void *simple_archiver_hash_map_get(SDArchiverHashMap *hash_map, void *key,
int simple_archiver_hash_map_remove(SDArchiverHashMap *hash_map, void *key, int simple_archiver_hash_map_remove(SDArchiverHashMap *hash_map, void *key,
unsigned int key_size); unsigned int key_size);
/// Iterates through the hash map with the "iter_check_fn", which is passed the
/// key, key-size, value, and user_data. This function will call "iter_check_fn"
/// on every entry in the given hash_map. If "iter_check_fn" returns non-zero,
/// iteration will halt and this function will return the same value. If
/// "iter_check_fn" returns zero for every call, then this function will return
/// zero after having iterated through every key-value pair.
int simple_archiver_hash_map_iter(const SDArchiverHashMap *hash_map,
int (*iter_check_fn)(void *, unsigned int,
void *, void *),
void *user_data);
#endif #endif

View file

@ -63,6 +63,19 @@ int get_three_fn(void *data, __attribute__((unused)) void *ud) {
int more_fn(long long a, long long b) { return a > b ? 1 : 0; } int more_fn(long long a, long long b) { return a > b ? 1 : 0; }
int hash_map_iter_check_fn(__attribute__((unused)) void *key,
__attribute__((unused)) unsigned int key_size,
void *value, void *ud) {
char *found_buf = ud;
size_t real_value = (size_t)value;
if (real_value < 5) {
found_buf[real_value] += 1;
return 0;
} else {
return 1;
}
}
int main(void) { int main(void) {
// Test LinkedList. // Test LinkedList.
{ {
@ -166,6 +179,27 @@ int main(void) {
sizeof(unsigned int), NULL, NULL); sizeof(unsigned int), NULL, NULL);
} }
simple_archiver_hash_map_free(&hash_map); simple_archiver_hash_map_free(&hash_map);
// Hash map iter test.
hash_map = simple_archiver_hash_map_init();
for (size_t idx = 0; idx < 5; ++idx) {
simple_archiver_hash_map_insert(&hash_map, (void *)idx, &idx,
sizeof(size_t), no_free_fn, no_free_fn);
}
char found[5] = {0, 0, 0, 0, 0};
CHECK_TRUE(simple_archiver_hash_map_iter(hash_map, hash_map_iter_check_fn,
found) == 0);
CHECK_TRUE(found[0] == 1);
CHECK_TRUE(found[1] == 1);
CHECK_TRUE(found[2] == 1);
CHECK_TRUE(found[3] == 1);
CHECK_TRUE(found[4] == 1);
simple_archiver_hash_map_free(&hash_map);
} }
// Test PriorityHeap. // Test PriorityHeap.