mirror of
https://github.com/bkthomps/Containers.git
synced 2025-11-16 12:34:47 +00:00
Improve const correctness
This commit is contained in:
45
src/list.c
45
src/list.c
@@ -64,7 +64,7 @@ list list_init(const size_t data_size) {
|
|||||||
*
|
*
|
||||||
* @return The amount of elements.
|
* @return The amount of elements.
|
||||||
*/
|
*/
|
||||||
int list_size(list me) {
|
int list_size(list me const) {
|
||||||
return me->space;
|
return me->space;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ int list_size(list me) {
|
|||||||
*
|
*
|
||||||
* @return If the list is empty.
|
* @return If the list is empty.
|
||||||
*/
|
*/
|
||||||
bool list_is_empty(list me) {
|
bool list_is_empty(list me const) {
|
||||||
return list_size(me) == 0;
|
return list_size(me) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +85,7 @@ bool list_is_empty(list me) {
|
|||||||
* @param array The array to copy the list to.
|
* @param array The array to copy the list to.
|
||||||
* @param me The list to copy to the array.
|
* @param me The list to copy to the array.
|
||||||
*/
|
*/
|
||||||
void list_to_array(void *const array, list me) {
|
void list_to_array(void *const array, list me const) {
|
||||||
struct node *traverse = me->head;
|
struct node *traverse = me->head;
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
while (traverse != NULL) {
|
while (traverse != NULL) {
|
||||||
@@ -98,7 +98,7 @@ void list_to_array(void *const array, list me) {
|
|||||||
/*
|
/*
|
||||||
* Get the node at index starting from the head.
|
* Get the node at index starting from the head.
|
||||||
*/
|
*/
|
||||||
static struct node *get_node_from_head(list me, const int index) {
|
static struct node *get_node_from_head(list me const, const int index) {
|
||||||
struct node *traverse = me->head;
|
struct node *traverse = me->head;
|
||||||
for (int i = 0; i < index; i++) {
|
for (int i = 0; i < index; i++) {
|
||||||
traverse = traverse->next;
|
traverse = traverse->next;
|
||||||
@@ -109,7 +109,7 @@ static struct node *get_node_from_head(list me, const int index) {
|
|||||||
/*
|
/*
|
||||||
* Get the node at index starting from tail.
|
* Get the node at index starting from tail.
|
||||||
*/
|
*/
|
||||||
static struct node *get_node_from_tail(list me, const int index) {
|
static struct node *get_node_from_tail(list me const, const int index) {
|
||||||
struct node *traverse = me->tail;
|
struct node *traverse = me->tail;
|
||||||
for (int i = me->space - 1; i > index; i--) {
|
for (int i = me->space - 1; i > index; i--) {
|
||||||
traverse = traverse->prev;
|
traverse = traverse->prev;
|
||||||
@@ -120,7 +120,7 @@ static struct node *get_node_from_tail(list me, const int index) {
|
|||||||
/*
|
/*
|
||||||
* Get the node at the specified index.
|
* Get the node at the specified index.
|
||||||
*/
|
*/
|
||||||
static struct node *get_node_at(list me, const int index) {
|
static struct node *get_node_at(list me const, const int index) {
|
||||||
if (index <= me->space / 2) {
|
if (index <= me->space / 2) {
|
||||||
return get_node_from_head(me, index);
|
return get_node_from_head(me, index);
|
||||||
} else {
|
} else {
|
||||||
@@ -137,7 +137,7 @@ static struct node *get_node_at(list me, const int index) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -ENOMEM Out of memory.
|
* -ENOMEM Out of memory.
|
||||||
*/
|
*/
|
||||||
int list_add_first(list me, void *const data) {
|
int list_add_first(list me const, void *const data) {
|
||||||
struct node *const traverse = me->head;
|
struct node *const traverse = me->head;
|
||||||
struct node *const add = malloc(sizeof(struct node));
|
struct node *const add = malloc(sizeof(struct node));
|
||||||
if (add == NULL) {
|
if (add == NULL) {
|
||||||
@@ -173,7 +173,7 @@ int list_add_first(list me, void *const data) {
|
|||||||
* -ENOMEM Out of memory.
|
* -ENOMEM Out of memory.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int list_add_at(list me, const int index, void *const data) {
|
int list_add_at(list me const, const int index, void *const data) {
|
||||||
if (index < 0 || index > me->space) {
|
if (index < 0 || index > me->space) {
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
@@ -212,7 +212,7 @@ int list_add_at(list me, const int index, void *const data) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -ENOMEM Out of memory.
|
* -ENOMEM Out of memory.
|
||||||
*/
|
*/
|
||||||
int list_add_last(list me, void *const data) {
|
int list_add_last(list me const, void *const data) {
|
||||||
struct node *const traverse = me->tail;
|
struct node *const traverse = me->tail;
|
||||||
struct node *const add = malloc(sizeof(struct node));
|
struct node *const add = malloc(sizeof(struct node));
|
||||||
if (add == NULL) {
|
if (add == NULL) {
|
||||||
@@ -234,7 +234,10 @@ int list_add_last(list me, void *const data) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool isIllegalParameters(list me, const int index) {
|
/*
|
||||||
|
* Determines if the parameters are illegal.
|
||||||
|
*/
|
||||||
|
static bool isIllegalParameters(list me const, const int index) {
|
||||||
return index < 0 || index >= me->space || me->space == 0;
|
return index < 0 || index >= me->space || me->space == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -246,7 +249,7 @@ static bool isIllegalParameters(list me, const int index) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int list_remove_first(list me) {
|
int list_remove_first(list me const) {
|
||||||
return list_remove_at(me, 0);
|
return list_remove_at(me, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,7 +262,7 @@ int list_remove_first(list me) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int list_remove_at(list me, const int index) {
|
int list_remove_at(list me const, const int index) {
|
||||||
if (isIllegalParameters(me, index)) {
|
if (isIllegalParameters(me, index)) {
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
@@ -287,7 +290,7 @@ int list_remove_at(list me, const int index) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int list_remove_last(list me) {
|
int list_remove_last(list me const) {
|
||||||
return list_remove_at(me, me->space - 1);
|
return list_remove_at(me, me->space - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -300,7 +303,7 @@ int list_remove_last(list me) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int list_set_first(list me, void *const data) {
|
int list_set_first(list me const, void *const data) {
|
||||||
return list_set_at(me, 0, data);
|
return list_set_at(me, 0, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,7 +317,7 @@ int list_set_first(list me, void *const data) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int list_set_at(list me, const int index, void *const data) {
|
int list_set_at(list me const, const int index, void *const data) {
|
||||||
if (isIllegalParameters(me, index)) {
|
if (isIllegalParameters(me, index)) {
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
@@ -332,7 +335,7 @@ int list_set_at(list me, const int index, void *const data) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int list_set_last(list me, void *const data) {
|
int list_set_last(list me const, void *const data) {
|
||||||
return list_set_at(me, me->space - 1, data);
|
return list_set_at(me, me->space - 1, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -345,7 +348,7 @@ int list_set_last(list me, void *const data) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int list_get_first(void *const data, list me) {
|
int list_get_first(void *const data, list me const) {
|
||||||
return list_get_at(data, me, 0);
|
return list_get_at(data, me, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -359,7 +362,7 @@ int list_get_first(void *const data, list me) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int list_get_at(void *const data, list me, const int index) {
|
int list_get_at(void *const data, list me const, const int index) {
|
||||||
if (isIllegalParameters(me, index)) {
|
if (isIllegalParameters(me, index)) {
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
@@ -377,7 +380,7 @@ int list_get_at(void *const data, list me, const int index) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int list_get_last(void *const data, list me) {
|
int list_get_last(void *const data, list me const) {
|
||||||
return list_get_at(data, me, me->space - 1);
|
return list_get_at(data, me, me->space - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -386,7 +389,7 @@ int list_get_last(void *const data, list me) {
|
|||||||
*
|
*
|
||||||
* @param me The list to clear.
|
* @param me The list to clear.
|
||||||
*/
|
*/
|
||||||
void list_clear(list me) {
|
void list_clear(list me const) {
|
||||||
struct node *traverse = me->head;
|
struct node *traverse = me->head;
|
||||||
while (traverse != NULL) {
|
while (traverse != NULL) {
|
||||||
struct node *const temp = traverse;
|
struct node *const temp = traverse;
|
||||||
@@ -405,7 +408,7 @@ void list_clear(list me) {
|
|||||||
*
|
*
|
||||||
* @return NULL
|
* @return NULL
|
||||||
*/
|
*/
|
||||||
list list_destroy(list me) {
|
list list_destroy(list me const) {
|
||||||
list_clear(me);
|
list_clear(me);
|
||||||
free(me);
|
free(me);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
45
src/vector.c
45
src/vector.c
@@ -64,7 +64,7 @@ vector vector_init(const size_t data_size) {
|
|||||||
*
|
*
|
||||||
* @return The size being used by the vector.
|
* @return The size being used by the vector.
|
||||||
*/
|
*/
|
||||||
int vector_size(vector me) {
|
int vector_size(vector me const) {
|
||||||
return me->offset;
|
return me->offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -75,7 +75,7 @@ int vector_size(vector me) {
|
|||||||
*
|
*
|
||||||
* @return If the vector is empty.
|
* @return If the vector is empty.
|
||||||
*/
|
*/
|
||||||
bool vector_is_empty(vector me) {
|
bool vector_is_empty(vector me const) {
|
||||||
return vector_size(me) == 0;
|
return vector_size(me) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -87,7 +87,7 @@ bool vector_is_empty(vector me) {
|
|||||||
*
|
*
|
||||||
* @return True if big enough, else false.
|
* @return True if big enough, else false.
|
||||||
*/
|
*/
|
||||||
bool vector_ensure_capacity(vector me, const int capacity) {
|
bool vector_ensure_capacity(vector me const, const int capacity) {
|
||||||
return capacity <= me->space;
|
return capacity <= me->space;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,7 +102,7 @@ bool vector_ensure_capacity(vector me, const int capacity) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -ENOMEM Out of memory.
|
* -ENOMEM Out of memory.
|
||||||
*/
|
*/
|
||||||
int vector_set_space(vector me, const int size) {
|
int vector_set_space(vector me const, const int size) {
|
||||||
me->space = size;
|
me->space = size;
|
||||||
if (me->space < me->offset) {
|
if (me->space < me->offset) {
|
||||||
me->offset = me->space;
|
me->offset = me->space;
|
||||||
@@ -123,7 +123,7 @@ int vector_set_space(vector me, const int size) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -ENOMEM Out of memory.
|
* -ENOMEM Out of memory.
|
||||||
*/
|
*/
|
||||||
int vector_trim_to_size(vector me) {
|
int vector_trim_to_size(vector me const) {
|
||||||
return vector_set_space(me, me->offset);
|
return vector_set_space(me, me->offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +133,7 @@ int vector_trim_to_size(vector me) {
|
|||||||
* @param me The vector to copy from.
|
* @param me The vector to copy from.
|
||||||
* @param array The array to copy to.
|
* @param array The array to copy to.
|
||||||
*/
|
*/
|
||||||
void vector_to_array(void *array, vector me) {
|
void vector_to_array(void *const array, vector me const) {
|
||||||
memcpy(array, me->storage, me->offset * me->data_size);
|
memcpy(array, me->storage, me->offset * me->data_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -146,7 +146,7 @@ void vector_to_array(void *array, vector me) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -ENOMEM Out of memory.
|
* -ENOMEM Out of memory.
|
||||||
*/
|
*/
|
||||||
int vector_add_first(vector me, void *const data) {
|
int vector_add_first(vector me const, void *const data) {
|
||||||
return vector_add_at(me, 0, data);
|
return vector_add_at(me, 0, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -161,7 +161,7 @@ int vector_add_first(vector me, void *const data) {
|
|||||||
* -ENOMEM Out of memory.
|
* -ENOMEM Out of memory.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int vector_add_at(vector me, const int index, void *const data) {
|
int vector_add_at(vector me const, const int index, void *const data) {
|
||||||
if (index < 0 || index > me->offset) {
|
if (index < 0 || index > me->offset) {
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
@@ -193,11 +193,14 @@ int vector_add_at(vector me, const int index, void *const data) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -ENOMEM Out of memory.
|
* -ENOMEM Out of memory.
|
||||||
*/
|
*/
|
||||||
int vector_add_last(vector me, void *const data) {
|
int vector_add_last(vector me const, void *const data) {
|
||||||
return vector_add_at(me, me->offset, data);
|
return vector_add_at(me, me->offset, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool isIllegalParameters(vector me, const int index) {
|
/*
|
||||||
|
* Determines if the parameters are illegal.
|
||||||
|
*/
|
||||||
|
static bool isIllegalParameters(vector me const, const int index) {
|
||||||
return index < 0 || index >= me->offset || me->offset == 0;
|
return index < 0 || index >= me->offset || me->offset == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,7 +212,7 @@ static bool isIllegalParameters(vector me, const int index) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int vector_remove_first(vector me) {
|
int vector_remove_first(vector me const) {
|
||||||
return vector_remove_at(me, 0);
|
return vector_remove_at(me, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,7 +225,7 @@ int vector_remove_first(vector me) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int vector_remove_at(vector me, const int index) {
|
int vector_remove_at(vector me const, const int index) {
|
||||||
if (isIllegalParameters(me, index)) {
|
if (isIllegalParameters(me, index)) {
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
@@ -241,7 +244,7 @@ int vector_remove_at(vector me, const int index) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int vector_remove_last(vector me) {
|
int vector_remove_last(vector me const) {
|
||||||
if (me->offset == 0) {
|
if (me->offset == 0) {
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
@@ -257,7 +260,7 @@ int vector_remove_last(vector me) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int vector_set_first(vector me, void *const data) {
|
int vector_set_first(vector me const, void *const data) {
|
||||||
return vector_set_at(me, 0, data);
|
return vector_set_at(me, 0, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -270,7 +273,7 @@ int vector_set_first(vector me, void *const data) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int vector_set_at(vector me, const int index, void *const data) {
|
int vector_set_at(vector me const, const int index, void *const data) {
|
||||||
if (isIllegalParameters(me, index)) {
|
if (isIllegalParameters(me, index)) {
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
@@ -286,7 +289,7 @@ int vector_set_at(vector me, const int index, void *const data) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int vector_set_last(vector me, void *const data) {
|
int vector_set_last(vector me const, void *const data) {
|
||||||
return vector_set_at(me, me->offset - 1, data);
|
return vector_set_at(me, me->offset - 1, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -299,7 +302,7 @@ int vector_set_last(vector me, void *const data) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int vector_get_first(void *const data, vector me) {
|
int vector_get_first(void *const data, vector me const) {
|
||||||
return vector_get_at(data, me, 0);
|
return vector_get_at(data, me, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -313,7 +316,7 @@ int vector_get_first(void *const data, vector me) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int vector_get_at(void *const data, vector me, const int index) {
|
int vector_get_at(void *const data, vector me const, const int index) {
|
||||||
if (isIllegalParameters(me, index)) {
|
if (isIllegalParameters(me, index)) {
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
@@ -330,7 +333,7 @@ int vector_get_at(void *const data, vector me, const int index) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -EINVAL Invalid parameter.
|
* -EINVAL Invalid parameter.
|
||||||
*/
|
*/
|
||||||
int vector_get_last(void *const data, vector me) {
|
int vector_get_last(void *const data, vector me const) {
|
||||||
return vector_get_at(data, me, me->offset - 1);
|
return vector_get_at(data, me, me->offset - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -342,7 +345,7 @@ int vector_get_last(void *const data, vector me) {
|
|||||||
* @return 0 No error.
|
* @return 0 No error.
|
||||||
* -ENOMEM Out of memory.
|
* -ENOMEM Out of memory.
|
||||||
*/
|
*/
|
||||||
int vector_clear(vector me) {
|
int vector_clear(vector me const) {
|
||||||
const int ret = vector_set_space(me, START_SPACE);
|
const int ret = vector_set_space(me, START_SPACE);
|
||||||
me->offset = 0;
|
me->offset = 0;
|
||||||
return ret;
|
return ret;
|
||||||
@@ -355,7 +358,7 @@ int vector_clear(vector me) {
|
|||||||
*
|
*
|
||||||
* @return NULL
|
* @return NULL
|
||||||
*/
|
*/
|
||||||
vector vector_destroy(vector me) {
|
vector vector_destroy(vector me const) {
|
||||||
free(me->storage);
|
free(me->storage);
|
||||||
me->storage = NULL;
|
me->storage = NULL;
|
||||||
free(me);
|
free(me);
|
||||||
|
|||||||
@@ -231,7 +231,7 @@ static void test_list(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
//test_vector();
|
test_vector();
|
||||||
test_list();
|
test_list();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user