]> git.seodisparate.com - SimpleArchiver/commitdiff
Impl. hash_map iter, clang-format
authorStephen Seo <seo.disparate@gmail.com>
Sun, 1 Sep 2024 06:11:57 +0000 (15:11 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Sun, 1 Sep 2024 06:13:26 +0000 (15:13 +0900)
19 files changed:
src/algorithms/linear_congruential_gen.c
src/algorithms/linear_congruential_gen.h
src/archiver.c
src/archiver.h
src/data_structures/hash_map.c
src/data_structures/hash_map.h
src/data_structures/linked_list.c
src/data_structures/linked_list.h
src/data_structures/priority_heap.c
src/data_structures/priority_heap.h
src/data_structures/test.c
src/helpers.c
src/helpers.h
src/main.c
src/parser.c
src/parser.h
src/parser_internal.h
src/platforms.h
src/test.c

index d16a01b7d630e4f3701761aa4dfda42557fb3ac7..e5a0705bd0bfb48835a9df5903e874cf8d6b2da1 100644 (file)
@@ -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,
index a9b63f16edaa271807c07cb6c2009fc15d11df3a..93714f38613df821f9416c0ade3149a21db7c586 100644 (file)
@@ -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,
index 0b30f3d3d1748fe6445bb56f9b1fa3a58176e990..c35b6760bf16ef96a7922fd998ef99dd6ef443c5 100644 (file)
@@ -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,
index 9299291a91468c845a2af6c0de7f4aae04856d7f..b8b035a2027200d324ea90ed4f1a73e4eb50179c 100644 (file)
@@ -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,
index 68d91f670d9847cbb2bc51110ec6ae9bd5bbf279..001c2ecf193be3ab13c4b53b93a942158b1f7a20 100644 (file)
@@ -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;
+}
index 60f15240036237e6af4412f17243a2c67d25032f..5691249e582447cdef7c233434306da95d3267eb 100644 (file)
@@ -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
index a37bd1ff9922358c65888cc7503d2e035259c30d..475ca2be28a584ea6a259d468567fd92435517f7 100644 (file)
@@ -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,
index 0c381357430f4e543d7d13628e8a3a382d9ca15c..7471b18adb2bbb98ea97fb5a1efa9fb4172cbb75 100644 (file)
@@ -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,
index ea9f41e4c7cf82c2766d27550ea3017f964d2ef8..c7b179f97e8ef1b338cbc6b18e48120e0415a6e6 100644 (file)
@@ -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,
index f0413cda43365285af531689135964c06f500dc0..8e9e3035e2256acc2da8bd56e02c91648504ed50 100644 (file)
@@ -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,
index 6dc9e006e8f9f783c189b94c5891d31eac821987..cd0ab51634054a19461c68f250db49e75fb4dc6f 100644 (file)
@@ -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.
index 4954e21810ce07eb0dc59802a8fc40c558d50290..828faf09d5bd4715d4933f1468f0e6a32aa19b42 100644 (file)
@@ -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,
index 42c18679f7365505f82a7e68fbe09a8bf1fcbb80..318137039b7a89e373b6b92d605a0f92f510202f 100644 (file)
@@ -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,
index 1234d20aa4aa34ac70d428894d3344be63127be3..fef432f0fecfe28d9e39025787a0ff67697aedfd 100644 (file)
@@ -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,
index 92862302c0959bfe1828ce70e52dc0355d63c8b0..70ec84bc1b91d8c0d78c6958d76f6c240cfe0a68 100644 (file)
@@ -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,
index 3548f24ffc7055a34c48f530d154d792cd926ed5..9d7017c5ad6170e15fa6443d795fc13f041a3322 100644 (file)
@@ -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,
index 229db229ac648fbb7754606b6dccfc34d28d5f43..4741abbd14a8af64ee3984864ce9e0123b22fcd3 100644 (file)
@@ -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,
index d8614c4cddee089fae729adc70f1a4f65a6b5c6b..f08e4ace6f7bad97f0251d9e28a7e8a911509dc3 100644 (file)
@@ -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,
index d97cd84fbf2e593a071f76d90a1ec85aa2d3116a..1dcd93c8b9f24b9c401aa199a7429a5a918475a0 100644 (file)
@@ -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,