diff --git a/Stdlib/HashTable.h b/Stdlib/HashTable.h index 6413f64..8320d53 100755 --- a/Stdlib/HashTable.h +++ b/Stdlib/HashTable.h @@ -15,205 +15,207 @@ #include #include -namespace Stdlib { - namespace HashTable { - typedef struct { - const char *key; - void *value; - } entry; +namespace Stdlib +{ + namespace HashTable + { + typedef struct { + const char *key; + void *value; + } entry; - struct ht { - bool is_fixed = false; - entry *entries; - size_t max; - size_t size; - }; + struct ht { + bool is_fixed = false; + entry *entries; + size_t max; + size_t size; + }; - typedef struct { - const char *key; - void *value; + typedef struct { + const char *key; + void *value; - ht *table; - size_t index; - } it; + ht *table; + size_t index; + } it; - inline - unsigned long long hash_key(const char *key) - { - unsigned long long hash = 14695981039346656037UL; - for (const char *p = key; *p; ++p) { - hash ^= (unsigned long long)(unsigned char)(*p); - hash *= 1099511628211UL; - } - - return hash; + inline + unsigned long long hash_key(const char *key) + { + unsigned long long hash = 14695981039346656037UL; + for (const char *p = key; *p; ++p) { + hash ^= (unsigned long long)(unsigned char)(*p); + hash *= 1099511628211UL; } - ht *create_table(int max = 0, bool is_fixed = false) - { - ht *table = (ht *) malloc(sizeof(ht)); - if (table == NULL) { - return NULL; - } - - table->size = 0; - table->max = max == 0 ? 16 : max; - table->is_fixed = is_fixed; - - table->entries = (entry *) calloc(table->max, sizeof(entry)); - if (table->entries == NULL) { - free(table); - return NULL; - } - - return table; - } - - void *get_entry(ht *table, const char *key) - { - unsigned long long hash = hash_key(key); - size_t index = (size_t)(hash & (unsigned long long)(table->max - 1)); - - while (table->entries[index].key != NULL) { - if (strcmp(key, table->entries[index].key) == 0) { - return table->entries[index].value; - } - - ++index; - if (index >= table->max) { - index = 0; - } - } + return hash; + } + ht *create_table(int max = 0, bool is_fixed = false) + { + ht *table = (ht *) malloc(sizeof(ht)); + if (table == NULL) { return NULL; } - const char *_set_entry(entry *entries, size_t max, const char *key, void *value, size_t *size) - { - unsigned long long hash = hash_key(key); - size_t index = (size_t)(hash & (unsigned long long)(max - 1)); + table->size = 0; + table->max = max == 0 ? 16 : max; + table->is_fixed = is_fixed; - while (entries[index].key != NULL) { - if (strcmp(key, entries[index].key) == 0) { - entries[index].value = value; - - return entries[index].key; - } - - ++index; - if (index >= max) { - index = 0; - } - } - - if (size != NULL) { - #ifdef _WIN32 - key = _strdup(key); - #else - key = strdup(key); - #endif - - if (key == NULL) { - return NULL; - } - - ++(*size); - } - - entries[index].key = (char *) key; - entries[index].value = value; - - return key; + table->entries = (entry *) calloc(table->max, sizeof(entry)); + if (table->entries == NULL) { + free(table); + return NULL; } - bool expand_table(ht *table) - { - size_t new_max = table->max * 2; - if (new_max < table->max) { - return false; + return table; + } + + void *get_entry(ht *table, const char *key) + { + unsigned long long hash = hash_key(key); + size_t index = (size_t)(hash & (unsigned long long)(table->max - 1)); + + while (table->entries[index].key != NULL) { + if (strcmp(key, table->entries[index].key) == 0) { + return table->entries[index].value; } - entry *new_entries = (entry *) calloc(new_max, sizeof(entry)); - if (new_entries == NULL) { - return false; + ++index; + if (index >= table->max) { + index = 0; } - - for (size_t i = 0; i < table->max; ++i) { - entry tmp = table->entries[i]; - if (tmp.key != NULL) { - _set_entry(new_entries, new_max, tmp.key, tmp.value, NULL); - } - } - - free(table->entries); - table->entries = new_entries; - table->max = new_max; - - return true; } - const char *set_entry(ht *table, const char *key, void *value) - { - if (value == NULL) { + return NULL; + } + + const char *_set_entry(entry *entries, size_t max, const char *key, void *value, size_t *size) + { + unsigned long long hash = hash_key(key); + size_t index = (size_t)(hash & (unsigned long long)(max - 1)); + + while (entries[index].key != NULL) { + if (strcmp(key, entries[index].key) == 0) { + entries[index].value = value; + + return entries[index].key; + } + + ++index; + if (index >= max) { + index = 0; + } + } + + if (size != NULL) { + #ifdef _WIN32 + key = _strdup(key); + #else + key = strdup(key); + #endif + + if (key == NULL) { return NULL; } - if (table->is_fixed && table->size == table->max) { - return NULL; - } - - if (!table->is_fixed && table->size >= table->max / 2) { - if (!expand_table(table)) { - return NULL; - } - } - - return _set_entry(table->entries, table->max, key, value, &table->size); + ++(*size); } - it table_iterator(ht *table) - { - it it; - it.table = table; - it.index = 0; + entries[index].key = (char *) key; + entries[index].value = value; - return it; - } - - bool next(it *it) - { - ht *table = it->table; - while (it->index < table->max) { - size_t i = it->index; - ++(it->index); - - if (table->entries[i].key != NULL) { - entry tmp = table->entries[i]; - it->key = tmp.key; - it->value = tmp.value; - - return true; - } - } + return key; + } + bool expand_table(ht *table) + { + size_t new_max = table->max * 2; + if (new_max < table->max) { return false; } - void free_table(ht *table) - { - if (!table || !table->entries) { - return; - } - - for (size_t i = 0; i < table->max; ++i) { - if (table->entries[i].key) { - free((void *) table->entries[i].key); - } - } - - free(table->entries); + entry *new_entries = (entry *) calloc(new_max, sizeof(entry)); + if (new_entries == NULL) { + return false; } - }; + + for (size_t i = 0; i < table->max; ++i) { + entry tmp = table->entries[i]; + if (tmp.key != NULL) { + _set_entry(new_entries, new_max, tmp.key, tmp.value, NULL); + } + } + + free(table->entries); + table->entries = new_entries; + table->max = new_max; + + return true; + } + + const char *set_entry(ht *table, const char *key, void *value) + { + if (value == NULL) { + return NULL; + } + + if (table->is_fixed && table->size == table->max) { + return NULL; + } + + if (!table->is_fixed && table->size >= table->max / 2) { + if (!expand_table(table)) { + return NULL; + } + } + + return _set_entry(table->entries, table->max, key, value, &table->size); + } + + it table_iterator(ht *table) + { + it it; + it.table = table; + it.index = 0; + + return it; + } + + bool next(it *it) + { + ht *table = it->table; + while (it->index < table->max) { + size_t i = it->index; + ++(it->index); + + if (table->entries[i].key != NULL) { + entry tmp = table->entries[i]; + it->key = tmp.key; + it->value = tmp.value; + + return true; + } + } + + return false; + } + + void free_table(ht *table) + { + if (!table || !table->entries) { + return; + } + + for (size_t i = 0; i < table->max; ++i) { + if (table->entries[i].key) { + free((void *) table->entries[i].key); + } + } + + free(table->entries); + } + } } #endif \ No newline at end of file diff --git a/Utils/ArraySort.h b/Utils/ArraySort.h index 0c35bfd..1696d3c 100755 --- a/Utils/ArraySort.h +++ b/Utils/ArraySort.h @@ -13,8 +13,10 @@ #include #include -namespace Utils { - namespace ArraySort { +namespace Utils +{ + namespace ArraySort + { inline void reverse_int(int *arr, int size) { diff --git a/Utils/StringUtils.h b/Utils/StringUtils.h index df70cec..a51893a 100755 --- a/Utils/StringUtils.h +++ b/Utils/StringUtils.h @@ -22,7 +22,7 @@ namespace Utils { inline char *search_replace(const char *haystack, const char *needle, const char *replace) { - const char* haystackT = haystack; + const char *haystackT = haystack; size_t i; size_t match = 0; @@ -83,10 +83,10 @@ namespace Utils { int size; } text_diff; - text_diff computeLCSDiff (char **from, int fromSize, char **to, int toSize) + text_diff computeLCSDiff(char **from, int fromSize, char **to, int toSize) { char **diffValues = (char **) malloc(fromSize * toSize * sizeof(char *)); - int *diffMasks = (int *) calloc(fromSize * toSize, sizeof(int)); + int *diffMasks = (int *) calloc(fromSize * toSize, sizeof(int)); int *dm = (int *) calloc((fromSize + 1) * (toSize + 1), sizeof(int)); @@ -134,7 +134,7 @@ namespace Utils { #else strcpy(diffValues[diffIndex], to[j - 1]); #endif - + diffMasks[diffIndex] = 1; --j; @@ -147,7 +147,7 @@ namespace Utils { diffValues[diffIndex] = (char *) malloc((strlen(from[i - 1]) + 1) * sizeof(char)); if (!diffValues[diffIndex]) { fprintf(stderr, "CRITICAL: malloc failed"); - + continue; } @@ -156,7 +156,7 @@ namespace Utils { #else strcpy(diffValues[diffIndex], from[i - 1]); #endif - + diffMasks[diffIndex] = -1; --i; @@ -168,7 +168,7 @@ namespace Utils { diffValues[diffIndex] = (char *) malloc((strlen(from[i - 1]) + 1) * sizeof(char)); if (!diffValues[diffIndex]) { fprintf(stderr, "CRITICAL: malloc failed"); - + continue; } @@ -210,7 +210,7 @@ namespace Utils { ArraySort::reverse_char(diffValues, diffIndex); ArraySort::reverse_int(diffMasks, diffIndex); - return text_diff { diffValues, diffMasks, diffIndex }; + return text_diff{diffValues, diffMasks, diffIndex}; } } }