Completed hash map implementation
This commit is contained in:
2
Makefile
2
Makefile
@@ -8,7 +8,7 @@ SRC_DIR = src
|
|||||||
OBJ_DIR = obj
|
OBJ_DIR = obj
|
||||||
|
|
||||||
TARGET = usage
|
TARGET = usage
|
||||||
LIB_OBJS = $(OBJ_DIR)/vector.o
|
LIB_OBJS = $(OBJ_DIR)/vector.o $(OBJ_DIR)/map.o
|
||||||
PROG_OBJS = $(OBJ_DIR)/usage.o
|
PROG_OBJS = $(OBJ_DIR)/usage.o
|
||||||
|
|
||||||
.PHONY: all clean
|
.PHONY: all clean
|
||||||
|
|||||||
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;
|
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_new();
|
||||||
map_result_t map_add(map_t *map, const char *key, void *value);
|
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_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
|
#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
|
* @vector: a non-null vector
|
||||||
*
|
*
|
||||||
* Deletes the vector and all its elements from the memory
|
* Deletes the vector and all its elements from the memory
|
||||||
*
|
*
|
||||||
* Returns a vector_result_t data type
|
* 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};
|
vector_result_t result = {0};
|
||||||
|
|
||||||
if (vector != NULL) {
|
if (vector == NULL) {
|
||||||
free(vector->elements);
|
result.status = VECTOR_ERR_INVALID;
|
||||||
free(vector);
|
SET_MSG(result, "Invalid vector");
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(vector->elements);
|
||||||
|
free(vector);
|
||||||
|
|
||||||
result.status = VECTOR_OK;
|
result.status = VECTOR_OK;
|
||||||
SET_MSG(result, "Vector successfully deleted");
|
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_get(vector_t *vector, size_t index);
|
||||||
vector_result_t vector_pop(vector_t *vector);
|
vector_result_t vector_pop(vector_t *vector);
|
||||||
vector_result_t vector_clear(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
|
// Inline methods
|
||||||
static inline size_t vector_size(const vector_t *vector) {
|
static inline size_t vector_size(const vector_t *vector) {
|
||||||
|
|||||||
152
usage.c
152
usage.c
@@ -1,7 +1,28 @@
|
|||||||
|
#define SEP(SIZE) { for (size_t i = 0; i < SIZE; i++) { printf("="); }; puts("\n"); }
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include "src/vector.h"
|
#include "src/vector.h"
|
||||||
|
#include "src/map.h"
|
||||||
|
|
||||||
|
static int vector_usage();
|
||||||
|
static int map_usage();
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
int st;
|
||||||
|
|
||||||
|
st = vector_usage();
|
||||||
|
if (st) { return st; }
|
||||||
|
|
||||||
|
SEP(50)
|
||||||
|
|
||||||
|
st = map_usage();
|
||||||
|
if (st) { return st; }
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int vector_usage() {
|
||||||
// Create a vector of 5 integers
|
// Create a vector of 5 integers
|
||||||
vector_result_t res = vector_new(5, sizeof(int));
|
vector_result_t res = vector_new(5, sizeof(int));
|
||||||
if (res.status != VECTOR_OK) {
|
if (res.status != VECTOR_OK) {
|
||||||
@@ -23,42 +44,149 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Print vector size and capacity
|
// Print vector size and capacity
|
||||||
printf("Vector size: %zu\n", vector_size(vector));
|
printf("Vector size (should be 5): %zu\n", vector_size(vector));
|
||||||
printf("Vector capacity: %zu\n", vector_capacity(vector));
|
printf("Vector capacity (should be >= 5): %zu\n\n", vector_capacity(vector));
|
||||||
|
|
||||||
// Print the whole vector
|
// Print the whole vector
|
||||||
for (size_t idx = 0; idx < vector_size(vector); idx++) {
|
for (size_t idx = 0; idx < vector_size(vector); idx++) {
|
||||||
vector_result_t get_res = vector_get(vector, idx);
|
vector_result_t get_res = vector_get(vector, idx);
|
||||||
if (get_res.status == VECTOR_OK) {
|
if (get_res.status != VECTOR_OK) {
|
||||||
|
printf("Cannot retrieve vec[%zu]: %s\n", idx, get_res.message);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
int val = *(int *)get_res.value.element;
|
int val = *(int *)get_res.value.element;
|
||||||
printf("vec[%zu] = %d\n", idx, val);
|
printf("vec[%zu] (should be '%zu') = %d\n", idx, idx, val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set an element at index 2
|
// Set an element at index 2
|
||||||
int new_val = 0xABABA;
|
int new_val = 0xBABE;
|
||||||
vector_result_t set_res = vector_set(vector, 2, &new_val);
|
vector_result_t set_res = vector_set(vector, 2, &new_val);
|
||||||
if (set_res.status == VECTOR_OK) {
|
if (set_res.status == VECTOR_OK) {
|
||||||
printf("vec[2] updated to %d\n", new_val);
|
printf("vec[2] (should be updated to 'BABE'): %X\n\n", new_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pop last element
|
// Pop last element
|
||||||
vector_result_t pop_res = vector_pop(vector);
|
vector_result_t pop_res = vector_pop(vector);
|
||||||
if (pop_res.status == VECTOR_OK) {
|
if (pop_res.status == VECTOR_OK) {
|
||||||
int val = *(int *)pop_res.value.element;
|
int val = *(int *)pop_res.value.element;
|
||||||
printf("Popped value: %d\n", val);
|
printf("Popped value (should be 5) : %d\n\n", val);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear vector
|
// Clear vector
|
||||||
vector_result_t clear_res = vector_clear(vector);
|
vector_result_t clear_res = vector_clear(vector);
|
||||||
if (clear_res.status == VECTOR_OK) {
|
if (clear_res.status != VECTOR_OK) {
|
||||||
printf("Vector cleared. New size is: %zu\n", vector_size(vector));
|
printf("Cannot clear vector: %s\n", clear_res.message);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
printf("Vector cleared (size should be 0): %zu\n\n", vector_size(vector));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Free vector
|
// Free vector
|
||||||
vector_result_t free_res = vector_free(vector);
|
vector_result_t del_res = vector_destroy(vector);
|
||||||
if (free_res.status != VECTOR_OK) {
|
if (del_res.status != VECTOR_OK) {
|
||||||
printf("Error while freeing the vector: %s\n", free_res.message);
|
printf("Error while destroying the vector: %s\n", del_res.message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int map_usage() {
|
||||||
|
// Create a new map
|
||||||
|
map_result_t res = map_new();
|
||||||
|
if (res.status != MAP_OK) {
|
||||||
|
printf("Error while creating map: %s\n", res.message);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
map_t *map = res.value.map;
|
||||||
|
|
||||||
|
// Add some values
|
||||||
|
const int x = 0xB00B5;
|
||||||
|
const char *y = "Hello";
|
||||||
|
|
||||||
|
map_result_t add_res = map_add(map, "x", (void*)&x);
|
||||||
|
if (add_res.status != MAP_OK) {
|
||||||
|
printf("Error while adding elements: %s\n", add_res.message);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
add_res = map_add(map, "y", (void*)y);
|
||||||
|
if (add_res.status != MAP_OK) {
|
||||||
|
printf("Error while adding elements: %s\n", add_res.message);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Print size and capacity
|
||||||
|
printf("Map size (should be 2): %zu\n", map_size(map));
|
||||||
|
printf("Map capacity (should be >=2): %zu\n\n", map_capacity(map));
|
||||||
|
|
||||||
|
// Retrieve keys
|
||||||
|
map_result_t get_res = map_get(map, "x");
|
||||||
|
if (get_res.status != MAP_OK) {
|
||||||
|
printf("Cannot retrieve map element 'x': %s\n", get_res.message);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
const int *val = (const int*)get_res.value.element;
|
||||||
|
printf("Key 'x' contains (should be 'B00B5'): %X\n", *val);
|
||||||
|
}
|
||||||
|
|
||||||
|
get_res = map_get(map, "y");
|
||||||
|
if (get_res.status != MAP_OK) {
|
||||||
|
printf("Cannot retrieve map element 'y': %s\n", get_res.message);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
const char *val = (const char*)get_res.value.element;
|
||||||
|
printf("Key 'y' contains (should be 'Hello') : %s\n\n", val);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update key
|
||||||
|
const int new_x = 0xC0FFEE;
|
||||||
|
map_result_t up_res = map_add(map, "x", (void*)&new_x);
|
||||||
|
|
||||||
|
up_res = map_get(map, "x");
|
||||||
|
if (get_res.status != MAP_OK) {
|
||||||
|
printf("Cannot retrieve map element 'x': %s\n", get_res.message);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
const int *val = (const int*)up_res.value.element;
|
||||||
|
printf("Key 'x' (should be updated to 'C0FFEE'): %X\n\n", *val);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove an element
|
||||||
|
map_result_t rm_res = map_remove(map, "y");
|
||||||
|
if (rm_res.status != MAP_OK) {
|
||||||
|
printf("Cannot remove map element 'y': %s\n", rm_res.message);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
printf("Map element 'y' removed (size should be 1): %zu\n\n", map_size(map));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Clear the map
|
||||||
|
map_result_t clear_res = map_clear(map);
|
||||||
|
if (clear_res.status != MAP_OK) {
|
||||||
|
printf("Cannot clear map: %s\n", clear_res.message);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
printf("Map cleared (size should be 0): %zu\n", map_size(map));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete the map
|
||||||
|
map_result_t del_res = map_destroy(map);
|
||||||
|
if (del_res.status != MAP_OK) {
|
||||||
|
printf("Error while destroying the map: %s\n", del_res.message);
|
||||||
|
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user