From aeb8eff3504fa960a81f858d35395261b023182a Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sun, 1 Sep 2024 15:11:57 +0900 Subject: [PATCH] Impl. hash_map iter, clang-format --- src/algorithms/linear_congruential_gen.c | 6 ++-- src/algorithms/linear_congruential_gen.h | 6 ++-- src/archiver.c | 6 ++-- src/archiver.h | 6 ++-- src/data_structures/hash_map.c | 41 ++++++++++++++++++++++-- src/data_structures/hash_map.h | 17 ++++++++-- src/data_structures/linked_list.c | 6 ++-- src/data_structures/linked_list.h | 6 ++-- src/data_structures/priority_heap.c | 6 ++-- src/data_structures/priority_heap.h | 6 ++-- src/data_structures/test.c | 40 +++++++++++++++++++++-- src/helpers.c | 6 ++-- src/helpers.h | 6 ++-- src/main.c | 6 ++-- src/parser.c | 6 ++-- src/parser.h | 6 ++-- src/parser_internal.h | 6 ++-- src/platforms.h | 6 ++-- src/test.c | 6 ++-- 19 files changed, 137 insertions(+), 57 deletions(-) diff --git a/src/algorithms/linear_congruential_gen.c b/src/algorithms/linear_congruential_gen.c index d16a01b..e5a0705 100644 --- a/src/algorithms/linear_congruential_gen.c +++ b/src/algorithms/linear_congruential_gen.c @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, diff --git a/src/algorithms/linear_congruential_gen.h b/src/algorithms/linear_congruential_gen.h index a9b63f1..93714f3 100644 --- a/src/algorithms/linear_congruential_gen.h +++ b/src/algorithms/linear_congruential_gen.h @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, diff --git a/src/archiver.c b/src/archiver.c index 0b30f3d..c35b676 100644 --- a/src/archiver.c +++ b/src/archiver.c @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, diff --git a/src/archiver.h b/src/archiver.h index 9299291..b8b035a 100644 --- a/src/archiver.h +++ b/src/archiver.h @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, diff --git a/src/data_structures/hash_map.c b/src/data_structures/hash_map.c index 68d91f6..001c2ec 100644 --- a/src/data_structures/hash_map.c +++ b/src/data_structures/hash_map.c @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, @@ -36,6 +36,12 @@ typedef struct SDArchiverHashMapKeyData { unsigned int key_size; } 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) { SDArchiverHashMapData *hash_map_data = data; if (hash_map_data->value) { @@ -248,3 +254,32 @@ int simple_archiver_hash_map_remove(SDArchiverHashMap *hash_map, void *key, 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; +} diff --git a/src/data_structures/hash_map.h b/src/data_structures/hash_map.h index 60f1524..5691249 100644 --- a/src/data_structures/hash_map.h +++ b/src/data_structures/hash_map.h @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, @@ -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, 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 diff --git a/src/data_structures/linked_list.c b/src/data_structures/linked_list.c index a37bd1f..475ca2b 100644 --- a/src/data_structures/linked_list.c +++ b/src/data_structures/linked_list.c @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, diff --git a/src/data_structures/linked_list.h b/src/data_structures/linked_list.h index 0c38135..7471b18 100644 --- a/src/data_structures/linked_list.h +++ b/src/data_structures/linked_list.h @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, diff --git a/src/data_structures/priority_heap.c b/src/data_structures/priority_heap.c index ea9f41e..c7b179f 100644 --- a/src/data_structures/priority_heap.c +++ b/src/data_structures/priority_heap.c @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, diff --git a/src/data_structures/priority_heap.h b/src/data_structures/priority_heap.h index f0413cd..8e9e303 100644 --- a/src/data_structures/priority_heap.h +++ b/src/data_structures/priority_heap.h @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, diff --git a/src/data_structures/test.c b/src/data_structures/test.c index 6dc9e00..cd0ab51 100644 --- a/src/data_structures/test.c +++ b/src/data_structures/test.c @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, @@ -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 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) { // Test LinkedList. { @@ -166,6 +179,27 @@ int main(void) { sizeof(unsigned int), NULL, NULL); } 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. diff --git a/src/helpers.c b/src/helpers.c index 4954e21..828faf0 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, diff --git a/src/helpers.h b/src/helpers.h index 42c1867..3181370 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, diff --git a/src/main.c b/src/main.c index 1234d20..fef432f 100644 --- a/src/main.c +++ b/src/main.c @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, diff --git a/src/parser.c b/src/parser.c index 9286230..70ec84b 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, diff --git a/src/parser.h b/src/parser.h index 3548f24..9d7017c 100644 --- a/src/parser.h +++ b/src/parser.h @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, diff --git a/src/parser_internal.h b/src/parser_internal.h index 229db22..4741abb 100644 --- a/src/parser_internal.h +++ b/src/parser_internal.h @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, diff --git a/src/platforms.h b/src/platforms.h index d8614c4..f08e4ac 100644 --- a/src/platforms.h +++ b/src/platforms.h @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, diff --git a/src/test.c b/src/test.c index d97cd84..1dcd93c 100644 --- a/src/test.c +++ b/src/test.c @@ -1,11 +1,11 @@ // ISC License -// +// // Copyright (c) 2024 Stephen Seo -// +// // Permission to use, copy, modify, and/or distribute this software for any // purpose with or without fee is hereby granted, provided that the above // copyright notice and this permission notice appear in all copies. -// +// // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY // AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,