Skip to content

Commit

Permalink
Change dsc_map_entry_t to camel case
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-jones committed Apr 23, 2024
1 parent 53cf5e6 commit e21417c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion include/dsc_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#define DSC_MAP_LOAD_FACTOR 0.75

/* Forward declaration of the map entry structure */
typedef struct dsc_map_entry_t dsc_map_entry_t;
typedef struct DSCMapEntry DSCMapEntry;

/* Forward declaration of the map structure */
typedef struct DSCMap DSCMap;
Expand Down
34 changes: 17 additions & 17 deletions src/dsc_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@
#include "../include/dsc_error.h"

/* Represents a hash map entry. */
struct dsc_map_entry_t {
struct DSCMapEntry {
int key; /* Key of the entry. */
int value; /* Value associated with the key. */
struct dsc_map_entry_t *next; /* Pointer to the next entry in the same bucket. */
struct DSCMapEntry *next; /* Pointer to the next entry in the same bucket. */
};

/* Represents a hash map. */
struct DSCMap {
struct dsc_map_entry_t **buckets; /* Array of buckets (linked lists). */
struct DSCMapEntry **buckets; /* Array of buckets (linked lists). */
unsigned int size; /* Number of entries in the map. */
unsigned int capacity; /* Number of buckets in the map. */
};

static void dsc_map_rehash(DSCMap *map, unsigned int new_capacity) {
dsc_map_entry_t **new_buckets = calloc(new_capacity, sizeof(dsc_map_entry_t *));
DSCMapEntry **new_buckets = calloc(new_capacity, sizeof(DSCMapEntry *));
if (new_buckets == NULL) {
dsc_set_error(DSC_ERROR_OUT_OF_MEMORY);
return;
}

/* Rehash all the elements into the new buckets. */
for (unsigned int i = 0; i < map->capacity; ++i) {
dsc_map_entry_t *entry = map->buckets[i];
DSCMapEntry *entry = map->buckets[i];
while (entry != NULL) {
dsc_map_entry_t *next = entry->next;
DSCMapEntry *next = entry->next;

/* Rehash the value to determine the new index. */
int index = dsc_hash(entry->key, new_capacity);
Expand All @@ -72,7 +72,7 @@ DSCMap *dsc_map_create() {
new_map->size = 0;
new_map->capacity = DSC_MAP_INITIAL_CAPACITY;

new_map->buckets = calloc(new_map->capacity, sizeof(dsc_map_entry_t *));
new_map->buckets = calloc(new_map->capacity, sizeof(DSCMapEntry *));
if (new_map->buckets == NULL) {
free(new_map);
dsc_set_error(DSC_ERROR_OUT_OF_MEMORY);
Expand All @@ -91,9 +91,9 @@ void dsc_map_free(DSCMap *map) {

/* Free all the entries in the map. */
for (unsigned int i = 0; i < map->capacity; ++i) {
dsc_map_entry_t *curr = map->buckets[i];
DSCMapEntry *curr = map->buckets[i];
while (curr != NULL) {
dsc_map_entry_t *next = curr->next;
DSCMapEntry *next = curr->next;
free(curr);
curr = next;
}
Expand All @@ -113,7 +113,7 @@ bool dsc_map_insert(DSCMap *map, int key, int value) {

/* Check if the key already exists. */
int index = dsc_hash(key, map->capacity);
dsc_map_entry_t *entry = map->buckets[index];
DSCMapEntry *entry = map->buckets[index];
while (entry != NULL) {
if (entry->key == key) {
dsc_set_error(DSC_ERROR_HASHMAP_KEY_ALREADY_EXISTS);
Expand All @@ -123,7 +123,7 @@ bool dsc_map_insert(DSCMap *map, int key, int value) {
}

/* Create a new entry. */
entry = malloc(sizeof(dsc_map_entry_t));
entry = malloc(sizeof(DSCMapEntry));
if (entry == NULL) {
dsc_set_error(DSC_ERROR_OUT_OF_MEMORY);
return false;
Expand Down Expand Up @@ -152,8 +152,8 @@ bool dsc_map_erase(DSCMap *map, int key) {
}

int index = dsc_hash(key, map->capacity);
dsc_map_entry_t *prev = NULL;
dsc_map_entry_t *entry = map->buckets[index];
DSCMapEntry *prev = NULL;
DSCMapEntry *entry = map->buckets[index];

while (entry != NULL) {
if (entry->key == key) {
Expand Down Expand Up @@ -182,7 +182,7 @@ int dsc_map_get(const DSCMap *map, int key) {
}

int index = dsc_hash(key, map->capacity);
dsc_map_entry_t *entry = map->buckets[index];
DSCMapEntry *entry = map->buckets[index];

while (entry != NULL) {
if (entry->key == key) {
Expand All @@ -203,7 +203,7 @@ bool dsc_map_contains(const DSCMap *map, int key) {
}

int index = dsc_hash(key, map->capacity);
dsc_map_entry_t *entry = map->buckets[index];
DSCMapEntry *entry = map->buckets[index];

while (entry != NULL) {
if (entry->key == key) {
Expand Down Expand Up @@ -245,9 +245,9 @@ void dsc_map_clear(DSCMap *map) {

/* Free all the entries in the map */
for (unsigned int i = 0; i < map->capacity; ++i) {
dsc_map_entry_t *entry = map->buckets[i];
DSCMapEntry *entry = map->buckets[i];
while (entry != NULL) {
dsc_map_entry_t *next = entry->next;
DSCMapEntry *next = entry->next;
free(entry);
entry = next;
}
Expand Down

0 comments on commit e21417c

Please sign in to comment.