feat(utilities): added a FlatMap for string key value pairs
This commit is contained in:
parent
bb961f0437
commit
a495fb6ce8
3 changed files with 76 additions and 7 deletions
|
|
@ -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*);
|
||||
|
|
|
|||
|
|
@ -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')));
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue