Skip to content

Commit

Permalink
Typedef struct DSCVector to *DSCVector and return bools from function…
Browse files Browse the repository at this point in the history
…s where applicable
  • Loading branch information
cm-jones committed Apr 23, 2024
1 parent 1123c2c commit 44ba7de
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 151 deletions.
43 changes: 24 additions & 19 deletions include/dsc_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,66 +27,69 @@
*/
#define DSC_VECTOR_INITIAL_CAPACITY 16

/* Forward declaration of the vector structure */
typedef struct DSCVector DSCVector;
// Forward declaration of the vector structure
typedef struct DSCVector *DSCVector;

/**
* @brief Creates a new empty vector.
*
* @return A pointer to the newly created vector, or NULL if memory allocation fails.
*/
DSCVector *dsc_vector_create(void);
DSCVector DSCVector_create(void);

/**
* @brief Destroys the vector and frees its memory.
*
* @param vector The vector to destroy.
* @return true if the vector was destroyed successfully, false otherwise.
*/
void dsc_vector_free(DSCVector *vector);
bool DSCVector_free(DSCVector vector);

/**
* @brief Inserts an element at the end of the vector.
*
* @param vector The vector to push the element into.
* @param value The value to push.
* @return true if the element was inserted successfully, false otherwise.
*/
void dsc_vector_push_back(DSCVector *vector, int value);
bool DSCVector_push_back(DSCVector vector, int value);

/**
* @brief Removes the last element from the vector.
*
* @param vector The vector to pop from.
* @return true if the element was popped successfully, false otherwise.
*/
void dsc_vector_pop_back(DSCVector *vector);
bool DSCVector_pop_back(DSCVector vector);

/**
* @brief Inserts an element at the specified position in the vector.
*
* @param vector The vector to insert the element into.
* @param position The position at which to insert the element.
* @param value The value to insert.
*
* @return The position of the inserted element.
* @param position The position at which to insert the element.
* @return true if the element was inserted successfully, false otherwise.
*/
unsigned int dsc_vector_insert(DSCVector *vector, unsigned int position, int value);
bool DSCVector_insert(DSCVector vector, int value, int position);

/**
* @brief Removes the element at the specified position from the vector.
*
* @param vector The vector to erase the element from.
* @param position The position of the element to erase.
* @return true if the element was removed successfully, false otherwise.
*/
void dsc_vector_erase(DSCVector *vector, unsigned int position);
bool DSCVector_erase(DSCVector vector, int position);

/**
* @brief Retrieves the value of the element at the specified index.
*
* @param vector The vector to get the element from.
* @param index The index of the element to retrieve.
*
* @return The value of the element at the specified index, or 0 if the index is out of bounds.
* @return The value of the element at the specified index, or -1 if the vector is NULL or the index is out of bounds.
*/
int dsc_vector_at(const DSCVector *vector, unsigned int index);
int DSCVector_at(const DSCVector vector, int index);

/**
* @brief Checks if the vector is empty.
Expand All @@ -95,7 +98,7 @@ int dsc_vector_at(const DSCVector *vector, unsigned int index);
*
* @return true if the vector is empty, false otherwise.
*/
bool dsc_vector_empty(const DSCVector *vector);
bool DSCVector_is_empty(const DSCVector vector);

/**
* @brief Gets the number of elements in the vector.
Expand All @@ -104,23 +107,25 @@ bool dsc_vector_empty(const DSCVector *vector);
*
* @return The number of elements in the vector, -1 if the vector is NULL.
*/
int dsc_vector_size(const DSCVector *vector);
int DSCVector_size(const DSCVector vector);

/**
* @brief Gets the maximum number of elements the vector can hold before needing to allocate more memory.
*
* @param vector The vector to get the capacity of.
*
* @return The maximum number of elements the vector can hold.
* @return The maximum number of elements the vector can hold, or -1 if the vector is NULL.
*/
unsigned int dsc_vector_capacity(const DSCVector *vector);
int DSCVector_capacity(const DSCVector vector);

/**
* @brief Reserves memory for the vector to hold at least the specified number of elements.
*
* @param vector The vector to reserve memory for.
* @param new_capacity The minimum capacity to reserve.
*
* @return true if the operation was successful, false otherwise.
*/
void dsc_vector_reserve(DSCVector *vector, unsigned int new_capacity);
bool DSCVector_reserve(DSCVector vector, int new_capacity);

#endif /* __DSC_VECTOR_H__ */
#endif // __DSC_VECTOR_H__
114 changes: 63 additions & 51 deletions src/dsc_vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
#include "../include/dsc_vector.h"
#include "../include/dsc_error.h"

/* Represents a vector. */
struct DSCVector {
int *values; /* Array of values stored in the vector. */
unsigned int size; /* The number of elements in the vector. */
unsigned int capacity; /* The current capacity of the vector. */
int *values; // Array of values stored in the vector
int size; // The number of elements in the vector
int capacity; // The current capacity of the vector.
};

static bool dsc_vector_resize(DSCVector *vector, unsigned int new_capacity) {
static bool DSCVector_resize(DSCVector vector, int new_capacity) {
int *new_values = realloc(vector->values, new_capacity * sizeof(int));
if (new_values == NULL) {
dsc_set_error(DSC_ERROR_OUT_OF_MEMORY);
Expand All @@ -37,20 +36,22 @@ static bool dsc_vector_resize(DSCVector *vector, unsigned int new_capacity) {

vector->values = new_values;
vector->capacity = new_capacity;

dsc_set_error(DSC_ERROR_NONE);
return true;
}

DSCVector *dsc_vector_create(void) {
DSCVector *new_vector = malloc(sizeof *new_vector);
DSCVector DSCVector_create(void) {
DSCVector new_vector = malloc(sizeof *new_vector);
if (new_vector == NULL) {
dsc_set_error(DSC_ERROR_OUT_OF_MEMORY);
return NULL;
}

new_vector->size = 0;
new_vector->capacity = DSC_VECTOR_INITIAL_CAPACITY;
new_vector->values = malloc(new_vector->capacity * sizeof(int));

new_vector->values = malloc(new_vector->capacity * sizeof(int));
if (new_vector->values == NULL) {
free(new_vector);
dsc_set_error(DSC_ERROR_OUT_OF_MEMORY);
Expand All @@ -61,129 +62,138 @@ DSCVector *dsc_vector_create(void) {
return new_vector;
}

void dsc_vector_free(DSCVector *vector) {
bool DSCVector_free(DSCVector vector) {
if (vector == NULL) {
dsc_set_error(DSC_ERROR_INVALID_ARGUMENT);
return;
return false;
}

free(vector->values);
free(vector);

dsc_set_error(DSC_ERROR_NONE);
return true;
}

void dsc_vector_push_back(DSCVector *vector, int value) {
bool DSCVector_push_back(DSCVector vector, int value) {
if (vector == NULL) {
dsc_set_error(DSC_ERROR_INVALID_ARGUMENT);
return;
return false;
}

/* Resize the vector if the size exceeds the capacity. */
// Resize the vector if the size exceeds the capacity
if (vector->size >= vector->capacity) {
unsigned int new_capacity = vector->capacity * 1.5;
if (!dsc_vector_resize(vector, new_capacity)) {
return;
int new_capacity = vector->capacity * 1.5;
if (!DSCVector_resize(vector, new_capacity)) {
dsc_set_error(DSC_ERROR_OUT_OF_MEMORY);
return false;
}
}

vector->values[vector->size] = value;
vector->size++;

dsc_set_error(DSC_ERROR_NONE);
return true;
}

void dsc_vector_pop_back(DSCVector *vector) {
bool DSCVector_pop_back(DSCVector vector) {
if (vector == NULL) {
dsc_set_error(DSC_ERROR_INVALID_ARGUMENT);
return;
return false;
}

if (dsc_vector_empty(vector)) {
if (DSCVector_is_empty(vector)) {
dsc_set_error(DSC_ERROR_EMPTY_CONTAINER);
return;
return false;
}

vector->size--;

dsc_set_error(DSC_ERROR_NONE);
return vector->values[vector->size];
}

unsigned int dsc_vector_insert(DSCVector *vector, unsigned int position, int value) {
bool DSCVector_insert(DSCVector vector, int value, int position) {
if (vector == NULL) {
dsc_set_error(DSC_ERROR_INVALID_ARGUMENT);
return 0;
return false;
}

if (position > vector->size) {
if (position < 0 || position > vector->size) {
dsc_set_error(DSC_ERROR_OUT_OF_RANGE);
return 0;
return false;
}

/* Resize the vector if the size exceeds the capacity. */
// Resize the vector if the size exceeds the capacity
if (vector->size >= vector->capacity) {
unsigned int new_capacity = vector->capacity * 1.5;
if (!dsc_vector_resize(vector, new_capacity)) {
return 0;
int new_capacity = vector->capacity * 1.5;
if (!DSCVector_resize(vector, new_capacity)) {
dsc_set_error(DSC_ERROR_OUT_OF_MEMORY);
return false;
}
}

/* Shift elements to the right to make room for the new element. */
for (unsigned int i = vector->size; i > position; --i) {
// Shift elements to the right to make room for the new element
for (int i = vector->size; i > position; --i) {
vector->values[i] = vector->values[i - 1];
}

vector->values[position] = value;
vector->size++;

dsc_set_error(DSC_ERROR_NONE);
return position;
return true;
}

void dsc_vector_erase(DSCVector *vector, unsigned int position) {
bool DSCVector_erase(DSCVector vector, int position) {
if (vector == NULL) {
dsc_set_error(DSC_ERROR_INVALID_ARGUMENT);
return;
return false;
}

if (position >= vector->size) {
if (position < 0 || position >= vector->size) {
dsc_set_error(DSC_ERROR_OUT_OF_RANGE);
return;
return false;
}

/* Shift elements to the left to fill the gap. */
for (unsigned int i = position; i < vector->size - 1; ++i) {
// Shift elements to the left to fill the gap
for (int i = position; i < vector->size - 1; ++i) {
vector->values[i] = vector->values[i + 1];
}

vector->size--;

dsc_set_error(DSC_ERROR_NONE);
return true;
}

int dsc_vector_at(const DSCVector *vector, unsigned int index) {
int DSCVector_at(const DSCVector vector, int index) {
if (vector == NULL) {
dsc_set_error(DSC_ERROR_INVALID_ARGUMENT);
return 0;
return -1;
}

if (index >= vector->size) {
if (index < 0 || index >= vector->size) {
dsc_set_error(DSC_ERROR_OUT_OF_RANGE);
return 0;
return -1;
}

dsc_set_error(DSC_ERROR_NONE);
return vector->values[index];
}

bool dsc_vector_empty(const DSCVector *vector) {
bool DSCVector_is_empty(const DSCVector vector) {
if (vector == NULL) {
dsc_set_error(DSC_ERROR_INVALID_ARGUMENT);
return true;
return false;
}

dsc_set_error(DSC_ERROR_NONE);
return vector->size == 0;
}

int dsc_vector_size(const DSCVector *vector) {
int DSCVector_size(const DSCVector vector) {
if (vector == NULL) {
dsc_set_error(DSC_ERROR_INVALID_ARGUMENT);
return -1;
Expand All @@ -193,30 +203,32 @@ int dsc_vector_size(const DSCVector *vector) {
return vector->size;
}

unsigned int dsc_vector_capacity(const DSCVector *vector) {
int DSCVector_capacity(const DSCVector vector) {
if (vector == NULL) {
dsc_set_error(DSC_ERROR_INVALID_ARGUMENT);
return 0;
return -1;
}

dsc_set_error(DSC_ERROR_NONE);
return vector->capacity;
}

void dsc_vector_reserve(DSCVector *vector, unsigned int new_capacity) {
bool DSCVector_reserve(DSCVector vector, int new_capacity) {
if (vector == NULL) {
dsc_set_error(DSC_ERROR_INVALID_ARGUMENT);
return;
return false;
}

if (new_capacity <= vector->capacity) {
dsc_set_error(DSC_ERROR_INVALID_ARGUMENT);
return;
return false;
}

if (!dsc_vector_resize(vector, new_capacity)) {
return;
if (!DSCVector_resize(vector, new_capacity)) {
dsc_set_error(DSC_ERROR_OUT_OF_MEMORY);
return false;
}

dsc_set_error(DSC_ERROR_NONE);
return true;
}
Loading

0 comments on commit 44ba7de

Please sign in to comment.