style: clang-format
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
BasedOnStyle: Google
|
BasedOnStyle: Google
|
||||||
ColumnLimit: 160
|
|
||||||
UseTab: Always
|
UseTab: Always
|
||||||
IndentWidth: 4
|
|
||||||
TabWidth: 4
|
TabWidth: 4
|
||||||
|
IndentWidth: 4
|
||||||
|
|
||||||
|
ColumnLimit: 100
|
||||||
|
PointerAlignment: Right
|
||||||
|
AllowShortFunctionsOnASingleLine: None
|
||||||
|
|||||||
@@ -7,7 +7,9 @@
|
|||||||
/*
|
/*
|
||||||
* @brief Returns the mutex ID.
|
* @brief Returns the mutex ID.
|
||||||
*/
|
*/
|
||||||
static size_t get_mutex(hashmap_s *hashmap, size_t hash) { return hash % hashmap->num_locks; }
|
static size_t get_mutex(hashmap_s *hashmap, size_t hash) {
|
||||||
|
return hash % hashmap->num_locks;
|
||||||
|
}
|
||||||
|
|
||||||
static size_t get_bucket_index(hashmap_s *hashmap, void *key) {
|
static size_t get_bucket_index(hashmap_s *hashmap, void *key) {
|
||||||
unsigned int hash = hashmap->hash(key);
|
unsigned int hash = hashmap->hash(key);
|
||||||
@@ -53,7 +55,8 @@ static bucket_s *find_bucket(hashmap_s *hashmap, void *key, bucket_s **prev) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
hashmap_s *hm_new(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn, free_value_f *free_value_fn, size_t key_size, size_t value_size) {
|
hashmap_s *hm_new(hash_f *hash_fn, equal_f *equal_fn, free_key_f *free_key_fn,
|
||||||
|
free_value_f *free_value_fn, size_t key_size, size_t value_size) {
|
||||||
hashmap_s *hashmap = malloc(sizeof(hashmap_s));
|
hashmap_s *hashmap = malloc(sizeof(hashmap_s));
|
||||||
if (hashmap == NULL) {
|
if (hashmap == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
@@ -75,7 +75,8 @@ typedef struct hashmap {
|
|||||||
* @param[in] value_size Size in bytes of each value to be stored.
|
* @param[in] value_size Size in bytes of each value to be stored.
|
||||||
* @return A pointer to the newly initialized hash map, or NULL on failure.
|
* @return A pointer to the newly initialized hash map, or NULL on failure.
|
||||||
*/
|
*/
|
||||||
hashmap_s *hm_new(hash_f *hash, equal_f *equal, free_key_f *free_key, free_value_f *free_value, size_t key_size, size_t value_size);
|
hashmap_s *hm_new(hash_f *hash, equal_f *equal, free_key_f *free_key, free_value_f *free_value,
|
||||||
|
size_t key_size, size_t value_size);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Free all resources used by the hash map.
|
* @brief Free all resources used by the hash map.
|
||||||
@@ -101,7 +102,8 @@ void hm_free_bucket(bucket_s *bucket);
|
|||||||
* @param[in] hashmap Pointer to the hash map.
|
* @param[in] hashmap Pointer to the hash map.
|
||||||
* @param[in] key Pointer to the key to insert (will be copied, must not be NULL).
|
* @param[in] key Pointer to the key to insert (will be copied, must not be NULL).
|
||||||
* @param[in] value Pointer to the value to insert (will be copied, must not be NULL).
|
* @param[in] value Pointer to the value to insert (will be copied, must not be NULL).
|
||||||
* @return true if the operation succeeded, false on failure (NULL hashmap/key/value or memory allocation failure).
|
* @return true if the operation succeeded, false on failure (NULL hashmap/key/value or memory
|
||||||
|
* allocation failure).
|
||||||
*/
|
*/
|
||||||
bool hm_set(hashmap_s *hashmap, void *key, void *value);
|
bool hm_set(hashmap_s *hashmap, void *key, void *value);
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,9 @@ static void buffer_destructor(void *buf) {
|
|||||||
free(tb);
|
free(tb);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void buffer_key_init(void) { tss_create(&buffer_key, buffer_destructor); }
|
static void buffer_key_init(void) {
|
||||||
|
tss_create(&buffer_key, buffer_destructor);
|
||||||
|
}
|
||||||
|
|
||||||
/* Returns the next power of two of a number */
|
/* Returns the next power of two of a number */
|
||||||
static size_t next_power_two(size_t len) {
|
static size_t next_power_two(size_t len) {
|
||||||
@@ -477,7 +479,8 @@ int string_insert(string_s *string, size_t index, const char *text) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Shift bytes */
|
/* Shift bytes */
|
||||||
memmove((char *)string->data + ((index + text_len) * sizeof(char)), (char *)string->data + (index * sizeof(char)), string->size - index + 1);
|
memmove((char *)string->data + ((index + text_len) * sizeof(char)),
|
||||||
|
(char *)string->data + (index * sizeof(char)), string->size - index + 1);
|
||||||
/* Insert new text */
|
/* Insert new text */
|
||||||
memcpy((char *)string->data + (index * sizeof(char)), text, text_len);
|
memcpy((char *)string->data + (index * sizeof(char)), text, text_len);
|
||||||
string->size = new_size;
|
string->size = new_size;
|
||||||
@@ -502,7 +505,8 @@ int string_remove(string_s *string, size_t index, size_t length) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memmove((char *)string->data + (index * sizeof(char)), (char *)string->data + ((index + length) * sizeof(char)), string->size - length);
|
memmove((char *)string->data + (index * sizeof(char)),
|
||||||
|
(char *)string->data + ((index + length) * sizeof(char)), string->size - length);
|
||||||
string->size -= length;
|
string->size -= length;
|
||||||
string->data[string->size] = '\0';
|
string->data[string->size] = '\0';
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,8 @@ typedef struct string {
|
|||||||
* @brief Create a new string initialized with the given text.
|
* @brief Create a new string initialized with the given text.
|
||||||
*
|
*
|
||||||
* @param text Initial text.
|
* @param text Initial text.
|
||||||
* @param initial_capacity Initial buffer capacity (including null terminator). Pass 0 to auto-calculate.
|
* @param initial_capacity Initial buffer capacity (including null terminator). Pass 0 to
|
||||||
|
* auto-calculate.
|
||||||
* @return Pointer to the new string, or NULL on failure.
|
* @return Pointer to the new string, or NULL on failure.
|
||||||
*/
|
*/
|
||||||
string_s *string_new(const char *text, size_t initial_capacity);
|
string_s *string_new(const char *text, size_t initial_capacity);
|
||||||
|
|||||||
@@ -38,7 +38,9 @@ bool my_equal_fun(const void *key_a, const void *key_b) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* And our last two functions, the free key and value called inside mcl_hm_remove() */
|
/* And our last two functions, the free key and value called inside mcl_hm_remove() */
|
||||||
void my_free_key(void *key) { free(key); }
|
void my_free_key(void *key) {
|
||||||
|
free(key);
|
||||||
|
}
|
||||||
|
|
||||||
void my_free_value(void *value) {
|
void my_free_value(void *value) {
|
||||||
struct my_custom_type *mct = (struct my_custom_type *)value;
|
struct my_custom_type *mct = (struct my_custom_type *)value;
|
||||||
@@ -52,7 +54,8 @@ void test_hm1(void) {
|
|||||||
/* This hashmap will contain names as keys and a custom type as value */
|
/* This hashmap will contain names as keys and a custom type as value */
|
||||||
size_t key_size = sizeof(char) * MAX_STR_LEN;
|
size_t key_size = sizeof(char) * MAX_STR_LEN;
|
||||||
size_t value_size = sizeof(int) + sizeof(char) * MAX_STR_LEN;
|
size_t value_size = sizeof(int) + sizeof(char) * MAX_STR_LEN;
|
||||||
hashmap_s *map = hm_new(my_hash_func, my_equal_fun, my_free_key, my_free_value, key_size, value_size);
|
hashmap_s *map =
|
||||||
|
hm_new(my_hash_func, my_equal_fun, my_free_key, my_free_value, key_size, value_size);
|
||||||
assert(map != NULL);
|
assert(map != NULL);
|
||||||
|
|
||||||
/* Make a new value */
|
/* Make a new value */
|
||||||
|
|||||||
@@ -230,7 +230,8 @@ int vec_insert(vec_s *vec, size_t index, void *value) {
|
|||||||
|
|
||||||
/* Shift memory and copy the new value */
|
/* Shift memory and copy the new value */
|
||||||
size_t tmp_size = vec->size - index + 1;
|
size_t tmp_size = vec->size - index + 1;
|
||||||
memmove((char *)vec->data + (index * vec->elem_size), (char *)vec->data + ((index + 1) * vec->elem_size), tmp_size * vec->elem_size);
|
memmove((char *)vec->data + (index * vec->elem_size),
|
||||||
|
(char *)vec->data + ((index + 1) * vec->elem_size), tmp_size * vec->elem_size);
|
||||||
memcpy((char *)vec->data + (index * vec->elem_size), value, vec->elem_size);
|
memcpy((char *)vec->data + (index * vec->elem_size), value, vec->elem_size);
|
||||||
vec->size++;
|
vec->size++;
|
||||||
|
|
||||||
@@ -254,7 +255,8 @@ int vec_remove(vec_s *vec, size_t index) {
|
|||||||
|
|
||||||
size_t size = vec->size - index;
|
size_t size = vec->size - index;
|
||||||
/* Overwrite bytes */
|
/* Overwrite bytes */
|
||||||
memmove((char *)vec->data + (index * vec->elem_size), (char *)vec->data + ((index + 1) * vec->elem_size), size);
|
memmove((char *)vec->data + (index * vec->elem_size),
|
||||||
|
(char *)vec->data + ((index + 1) * vec->elem_size), size);
|
||||||
vec->size--;
|
vec->size--;
|
||||||
|
|
||||||
mtx_unlock(&vec->lock);
|
mtx_unlock(&vec->lock);
|
||||||
|
|||||||
Reference in New Issue
Block a user