From a495fb6ce887f7c45e976ef5188ffc1d0bd5f981 Mon Sep 17 00:00:00 2001 From: lisk77 Date: Tue, 23 Sep 2025 00:29:16 +0200 Subject: [PATCH] feat(utilities): added a FlatMap for string key value pairs --- include/utilities.h | 13 ++++++++- src/base_file_buffer.c | 10 +++---- src/utilities.c | 60 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 76 insertions(+), 7 deletions(-) diff --git a/include/utilities.h b/include/utilities.h index a6b1209..56f6b0d 100644 --- a/include/utilities.h +++ b/include/utilities.h @@ -22,13 +22,24 @@ typedef enum { PT_ERROR } PathType; -typedef List StringBuffer; +typedef struct { + char* key; + char* value; +} KeyValuePair; +typedef List StringBuffer; +typedef List FlatMap; StringBuffer* string_buffer_new(); int string_buffer_push(StringBuffer*, char*); void string_buffer_sort(StringBuffer*); char* string_buffer_search(StringBuffer*, char*); +FlatMap* flat_map_new(); +int flat_map_put(FlatMap*, const char*, const char*); +char* flat_map_get(FlatMap*, const char*); +void flat_map_free(FlatMap*); +void flat_map_sort(FlatMap*); +KeyValuePair* flat_map_search(FlatMap*, const char*); char* find_root(char*); void walk(char*, char*, char*, FileInfoBuffer*, int, char*); char* get_repo_path(char*, char*); diff --git a/src/base_file_buffer.c b/src/base_file_buffer.c index ada2e98..f30b5dd 100644 --- a/src/base_file_buffer.c +++ b/src/base_file_buffer.c @@ -248,9 +248,9 @@ int write_base_file_list(BaseFileBuffer* buffer, char* info_file) { for (size_t idx = 0; idx < buffer->len; idx++) { BaseFileInfo* info = (BaseFileInfo*)buffer->items + idx; - buffer_size += 3 + - strlen(info->name) + - snprintf(NULL, 0, "%zu", info->base_num) + + buffer_size += 3 + + strlen(info->name) + + snprintf(NULL, 0, "%zu", info->base_num) + snprintf(NULL, 0, "%zu", info->diff_num); } @@ -262,7 +262,7 @@ int write_base_file_list(BaseFileBuffer* buffer, char* info_file) { for (size_t idx = 0; idx < buffer->len; idx++) { size_t remaining = sizeof(tmp) - offset; BaseFileInfo* info = (BaseFileInfo*)buffer->items + idx; - offset += snprintf(tmp + offset, remaining, "%s %zu %zu ", + offset += snprintf(tmp + offset, remaining, "%s %zu %zu ", info->name, info->base_num, info->diff_num); } @@ -276,7 +276,7 @@ int write_base_file_list(BaseFileBuffer* buffer, char* info_file) { fclose(fp); return 0; } - + if (compress(compressed, &compressedLen, (const Bytef*)tmp, originalLen) != Z_OK) { perror("ERROR: compression failed in snapshot_tree!"); free(compressed); diff --git a/src/utilities.c b/src/utilities.c index 9bfe709..8f52b4c 100644 --- a/src/utilities.c +++ b/src/utilities.c @@ -32,6 +32,64 @@ char* string_buffer_search(StringBuffer* buffer, char* path) { return (char*)list_binary_search(buffer, path, compare_strings); } +FlatMap* flat_map_new() { + return list_new(sizeof(KeyValuePair)); +} + +int flat_map_put(FlatMap* map, const char* key, const char* value) { + if (!map || !key || !value) return 0; + + KeyValuePair kv = {strdup(key), strdup(value)}; + int res = list_push(map, &kv); + flat_map_sort(map); + return res; +} + +char* flat_map_get(FlatMap* map, const char* key) { + if (!map || !key) return NULL; + + KeyValuePair* kv = flat_map_search(map, key); + return kv ? kv->value : NULL; +} + +static int compare_kv(const void* a, const void* b) { + const KeyValuePair* kv1 = (const KeyValuePair*)a; + const KeyValuePair* kv2 = (const KeyValuePair*)b; + return strcmp(kv1->key, kv2->key); +} + +void flat_map_sort(FlatMap* map) { + if (!map || map->len <= 1) { + return; + } + + qsort(map->items, map->len, sizeof(KeyValuePair), compare_kv); +} + +static int compare_kv_with_key(const void* key_ptr, const void* element_ptr) { + const char* key = (const char*)key_ptr; + const KeyValuePair* kv = (const KeyValuePair*)element_ptr; + return strcmp(key, kv->key); +} + +KeyValuePair* flat_map_search(FlatMap* map, const char* key) { + if (!map || !key) return NULL; + return (KeyValuePair*)list_binary_search(map, key, compare_kv_with_key); +} + +void flat_map_free(FlatMap* map) { + if (!map) return; + + for (size_t i = 0; i < map->len; i++) { + KeyValuePair* kv = &((KeyValuePair*)map->items)[i]; + free(kv->key); + free(kv->value); + } + + free(map->items); + free(map); +} + static int is_dot_or_dotdot(const char* s) { return (s[0] == '.' && (s[1] == '\0' || (s[1] == '.' && s[2] == '\0'))); } @@ -406,4 +464,4 @@ int create_default_config_file(char* config_path) { fclose(fp); return 0; -} \ No newline at end of file +}