aboutsummaryrefslogtreecommitdiffstats
path: root/README
diff options
context:
space:
mode:
authorFabrice <fabrice@schaub-dev.xyz>2026-03-22 21:31:59 +0100
committerFabrice <fabrice@schaub-dev.xyz>2026-03-22 21:31:59 +0100
commit403d8dc92750679eb96968f5fc4465e96401ef14 (patch)
treeafae902778194e390e7bb994597f6ccf78199240 /README
parent9b43c34b9b51d953bd4363df576a6f93e9596700 (diff)
adding proper example
Diffstat (limited to 'README')
-rw-r--r--README92
1 files changed, 65 insertions, 27 deletions
diff --git a/README b/README
index a00fff5..3d4e465 100644
--- a/README
+++ b/README
@@ -7,36 +7,74 @@ is not yet production tested but fully working.
* Example
```
-#include "cheesemap.h"
-#include <stdint.h>
+#include <stdio.h>
#include <string.h>
-#include <stddef.h>
-
-struct user_info {
- const char* country;
- int age;
- int zip;
-};
-
-uint64_t hash(const uint8_t* key) {
- const char* name = (const char*)key;
- uint64_t count;
- while(*name) {
- count++;
- name++;
- }
-
- return count;
+#include "cheesemap.h"
+
+// Convenience macro for array length
+#define countof(arr) (sizeof(arr) / sizeof(*(arr)))
+
+// Simple hash function for string keys
+uint64_t hash_string(const uint8_t* key, uint8_t* user) {
+ (void)user;
+ const char* str = *(const char**)key;
+ uint64_t hash = 5381;
+ int c;
+ while ((c = *str++))
+ hash = ((hash << 5) + hash) + c; // hash * 33 + c
+ return hash;
}
-bool compare(const uint8_t* key1, const uint8_t* key2) {
- const char* name1 = (const char*)key1;
- const char* name2 = (const char*)key2;
- return strcmp(name1, name2);
+// Compare function for string keys
+bool compare_string(const uint8_t* key1, const uint8_t* key2, uint8_t* user) {
+ (void)user;
+ return strcmp(*(const char**)key1, *(const char**)key2) == 0;
}
-int main() {
- struct cheesemap map;
- cm_new(&map, sizeof(const char*), _Alignof(const char*), sizeof(struct user_info), _Alignof(struct user_info), NULL
-}
+int main(void) {
+ // Create a map: string -> int (word frequency counter)
+ struct cheesemap map;
+ cm_new_(&map, const char*, int, NULL, hash_string, compare_string);
+
+ // Count word frequencies
+ const char* words[] = {"hello", "world", "hello", "cheesemap", "world", "hello"};
+ for (size_t i = 0; i < countof(words); i++) {
+ int* count;
+ if (cm_lookup_(&map, words[i], &count)) {
+ (*count)++; // Word exists, increment
+ } else {
+ int initial = 1;
+ cm_insert_(&map, words[i], initial);
+ }
+ }
+
+ // Iterate and print all word counts
+ printf("Word frequencies:\n");
+ struct cheesemap_iter iter;
+ cm_iter_init(&iter, &map);
+ const char** word;
+ int* count;
+ while (cm_iter_next(&iter, &map, (uint8_t**)&word, (uint8_t**)&count)) {
+ printf(" %s: %d\n", *word, *count);
+ }
+
+ // Lookup a specific word
+ const char* search = "hello";
+ if (cm_lookup_(&map, search, &count)) {
+ printf("\n'%s' appears %d times\n", search, *count);
+ }
+
+ // Remove a word
+ const char* remove = "world";
+ cm_remove_(&map, remove, NULL);
+ printf("Removed '%s'\n", remove);
+
+ // Verify removal
+ if (!cm_lookup_(&map, remove, &count)) {
+ printf("'%s' no longer in map\n", remove);
+ }
+
+ cm_drop(&map);
+ return 0;
+}
```