Completed hash map implementation
This commit is contained in:
119
src/map.c
119
src/map.c
@@ -261,3 +261,122 @@ map_result_t map_get(const map_t *map, const char *key) {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* map_remove
|
||||
* @map: a non-null map
|
||||
* @key: a string representing the index key
|
||||
*
|
||||
* Removes an element indexed by @key from @map
|
||||
*
|
||||
* Returns a map_result_t data type
|
||||
*/
|
||||
map_result_t map_remove(map_t *map, const char *key) {
|
||||
map_result_t result = {0};
|
||||
|
||||
if (map == NULL) {
|
||||
result.status = MAP_ERR_INVALID;
|
||||
SET_MSG(result, "Invalid map");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
const size_t idx = map_find_index(map, key);
|
||||
|
||||
if (map->elements[idx].state != ENTRY_OCCUPIED) {
|
||||
result.status = MAP_ERR_INVALID;
|
||||
SET_MSG(result, "Cannot delete this element");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Remove element key
|
||||
free(map->elements[idx].key);
|
||||
|
||||
// Remove element properties
|
||||
map->elements[idx].key = NULL;
|
||||
map->elements[idx].value = NULL;
|
||||
map->elements[idx].state = ENTRY_DELETED;
|
||||
|
||||
// Decrease map size and increase its tombstone count
|
||||
map->size--;
|
||||
map->tombstone_count++;
|
||||
|
||||
result.status = MAP_OK;
|
||||
SET_MSG(result, "Key successfully deleted");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* map_clear
|
||||
* @map: a non-null map
|
||||
*
|
||||
* Resets the map to an empty state
|
||||
*
|
||||
* Returns a map_result_t data type
|
||||
*/
|
||||
map_result_t map_clear(map_t *map) {
|
||||
map_result_t result = {0};
|
||||
|
||||
if (map == NULL) {
|
||||
result.status = MAP_ERR_INVALID;
|
||||
SET_MSG(result, "Invalid map");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
for (size_t idx = 0; idx < map->capacity; idx++) {
|
||||
if (map->elements[idx].state == ENTRY_OCCUPIED ||
|
||||
map->elements[idx].state == ENTRY_DELETED) {
|
||||
free(map->elements[idx].key);
|
||||
map->elements[idx].key = NULL;
|
||||
map->elements[idx].value = NULL;
|
||||
}
|
||||
|
||||
map->elements[idx].state = ENTRY_EMPTY;
|
||||
}
|
||||
|
||||
// Resets map size and tombstone count
|
||||
map->size = 0;
|
||||
map->tombstone_count = 0;
|
||||
|
||||
result.status = MAP_OK;
|
||||
SET_MSG(result, "Map successfully cleared");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* map_destroy
|
||||
* @map: a non-null map
|
||||
*
|
||||
* Deletes the map and all its elements from the memory
|
||||
*
|
||||
* Returns a map_result_t data type
|
||||
*/
|
||||
map_result_t map_destroy(map_t *map) {
|
||||
map_result_t result = {0};
|
||||
|
||||
if (map == NULL) {
|
||||
result.status = MAP_ERR_INVALID;
|
||||
SET_MSG(result, "Invalid map");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
for (size_t idx = 0; idx < map->capacity; idx++) {
|
||||
if (map->elements[idx].state == ENTRY_OCCUPIED ||
|
||||
map->elements[idx].state == ENTRY_DELETED) {
|
||||
free(map->elements[idx].key);
|
||||
}
|
||||
}
|
||||
|
||||
free(map->elements);
|
||||
free(map);
|
||||
|
||||
result.status = MAP_OK;
|
||||
SET_MSG(result, "Map successfully deleted");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
12
src/map.h
12
src/map.h
@@ -56,6 +56,18 @@ extern "C" {
|
||||
map_result_t map_new();
|
||||
map_result_t map_add(map_t *map, const char *key, void *value);
|
||||
map_result_t map_get(const map_t *map, const char *key);
|
||||
map_result_t map_remove(map_t *map, const char *key);
|
||||
map_result_t map_clear(map_t *map);
|
||||
map_result_t map_destroy(map_t *map);
|
||||
|
||||
// Inline methods
|
||||
static inline size_t map_size(const map_t *map) {
|
||||
return map ? map->size : 0;
|
||||
}
|
||||
|
||||
static inline size_t map_capacity(const map_t *map) {
|
||||
return map ? map->capacity : 0;
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
15
src/vector.c
15
src/vector.c
@@ -288,21 +288,26 @@ vector_result_t vector_clear(vector_t *vector) {
|
||||
}
|
||||
|
||||
/**
|
||||
* vector_free
|
||||
* vector_destroy
|
||||
* @vector: a non-null vector
|
||||
*
|
||||
* Deletes the vector and all its elements from the memory
|
||||
*
|
||||
* Returns a vector_result_t data type
|
||||
*/
|
||||
vector_result_t vector_free(vector_t *vector) {
|
||||
vector_result_t vector_destroy(vector_t *vector) {
|
||||
vector_result_t result = {0};
|
||||
|
||||
if (vector != NULL) {
|
||||
free(vector->elements);
|
||||
free(vector);
|
||||
if (vector == NULL) {
|
||||
result.status = VECTOR_ERR_INVALID;
|
||||
SET_MSG(result, "Invalid vector");
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
free(vector->elements);
|
||||
free(vector);
|
||||
|
||||
result.status = VECTOR_OK;
|
||||
SET_MSG(result, "Vector successfully deleted");
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ vector_result_t vector_set(vector_t *vector, size_t index, void *value);
|
||||
vector_result_t vector_get(vector_t *vector, size_t index);
|
||||
vector_result_t vector_pop(vector_t *vector);
|
||||
vector_result_t vector_clear(vector_t *vector);
|
||||
vector_result_t vector_free(vector_t *vector);
|
||||
vector_result_t vector_destroy(vector_t *vector);
|
||||
|
||||
// Inline methods
|
||||
static inline size_t vector_size(const vector_t *vector) {
|
||||
|
||||
Reference in New Issue
Block a user