Files
datum/src/vector.c

323 lines
8.2 KiB
C
Raw Normal View History

2025-10-25 17:21:12 +02:00
#define SET_MSG(result, msg) \
snprintf((char *)result.message, RESULT_MSG_SIZE, msg)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
2025-10-04 17:20:59 +02:00
#include "vector.h"
// Internal method to increase vector size
2025-10-25 17:21:12 +02:00
static vector_result_t vector_resize(vector_t *vector);
2025-10-04 17:20:59 +02:00
/**
* vector_new
* @size: initial number of elements
* @data_size: size of each element in bytes
*
2025-10-25 17:21:12 +02:00
* Returns a vector_result_t data type containing a new vector
2025-10-04 17:20:59 +02:00
*/
2025-10-25 17:21:12 +02:00
vector_result_t vector_new(size_t size, size_t data_size) {
vector_result_t result = {0};
2025-10-04 17:20:59 +02:00
2025-11-05 11:52:21 +01:00
if (size == 0) {
result.status = VECTOR_ERR_ALLOCATE;
SET_MSG(result, "Invalid vector size");
return result;
}
2025-10-04 17:20:59 +02:00
// Allocate a new vector
2025-10-25 17:21:12 +02:00
vector_t *vector = malloc(sizeof(vector_t));
2025-10-04 17:20:59 +02:00
if (vector == NULL) {
result.status = VECTOR_ERR_ALLOCATE;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Failed to allocate memory for vector");
2025-10-04 17:20:59 +02:00
return result;
}
// Initialize vector
vector->count = 0;
vector->capacity = size;
vector->data_size = data_size;
vector->elements = calloc(size, data_size);
if (vector->elements == NULL) {
2025-10-25 17:21:12 +02:00
free(vector);
result.status = VECTOR_ERR_ALLOCATE;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Failed to allocate memory for vector elements");
2025-10-04 17:20:59 +02:00
return result;
}
result.status = VECTOR_OK;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Vector successfully created");
2025-10-04 17:20:59 +02:00
result.value.vector = vector;
return result;
}
/**
* vector_resize
* @vector: a non-null vector
*
* Increases the size of @vector
*
2025-10-25 17:21:12 +02:00
* Returns a vector_result_t data type containing the status
2025-10-04 17:20:59 +02:00
*/
2025-10-25 17:21:12 +02:00
vector_result_t vector_resize(vector_t *vector) {
vector_result_t result = {0};
2025-10-04 17:20:59 +02:00
size_t old_capacity = vector->capacity;
vector->capacity = (old_capacity > 0 ? ((old_capacity * 3) / 2) : 1);
// Check for stack overflow errors
if (vector->capacity > SIZE_MAX / vector->data_size) {
result.status = VECTOR_ERR_OVERFLOW;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Exceeded maximum size while resizing vector");
2025-10-04 17:20:59 +02:00
return result;
}
void *new_elements = realloc(vector->elements, (vector->capacity * vector->data_size));
if (new_elements == NULL) {
result.status = VECTOR_ERR_ALLOCATE;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Failed to reallocate memory for vector");
2025-10-04 17:20:59 +02:00
return result;
}
vector->elements = new_elements;
result.status = VECTOR_OK;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Vector successfully resized");
2025-10-04 17:20:59 +02:00
return result;
}
/**
* vector_push
* @vector: a non-null vector
* @value: a generic value to add to the vector
2025-10-04 17:20:59 +02:00
*
* Adds @value at the end of @vector
*
2025-10-25 17:21:12 +02:00
* Returns a vector_result_t data type containing the status
2025-10-04 17:20:59 +02:00
*/
2025-10-25 17:21:12 +02:00
vector_result_t vector_push(vector_t *vector, void *value) {
vector_result_t result = {0};
2025-10-04 17:20:59 +02:00
if (vector == NULL || value == NULL) {
result.status = VECTOR_ERR_INVALID;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Invalid vector or value");
2025-10-04 17:20:59 +02:00
return result;
}
// Check whether vector has enough space available
if (vector->capacity == vector->count) {
result = vector_resize(vector);
if (result.status != VECTOR_OK) {
2025-10-04 17:20:59 +02:00
return result;
}
}
// Calculate destination memory address
uint8_t *destination_addr = (uint8_t*)vector->elements + (vector->count * vector->data_size);
// Append @value to the data structure according to its data type
if (vector->data_size == sizeof(int)) {
*(int*)destination_addr = *(int*)value;
} else if (vector->data_size == sizeof(long)) {
*(long*)destination_addr = *(long*)value;
} else if (vector->data_size == sizeof(double)) {
*(double*)destination_addr = *(double*)value;
} else if (vector->data_size == sizeof(float)) {
*(float*)destination_addr = *(float*)value;
} else {
memcpy(destination_addr, value, vector->data_size);
}
// Increase elements count
vector->count++;
result.status = VECTOR_OK;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Value successfully added");
2025-10-04 17:20:59 +02:00
return result;
}
/**
* vector_set
* @vector: a non-null vector
* @index: a non-negative integer representing the position to write into
* @value: a generic value to add to the vector
*
* Writes @value at @index
*
2025-10-25 17:21:12 +02:00
* Returns a vector_result_t data type
*/
2025-10-25 17:21:12 +02:00
vector_result_t vector_set(vector_t *vector, size_t index, void *value) {
vector_result_t result = {0};
if (vector == NULL || value == NULL) {
result.status = VECTOR_ERR_INVALID;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Invalid vector or value");
return result;
}
if (index >= vector->count) {
result.status = VECTOR_ERR_OVERFLOW;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Index out of bounds");
return result;
}
uint8_t *destination_addr = (uint8_t *)vector->elements + (index * vector->data_size);
// Append @value to the data structure according to its data type
if (vector->data_size == sizeof(int)) {
*(int*)destination_addr = *(int*)value;
} else if (vector->data_size == sizeof(long)) {
*(long*)destination_addr = *(long*)value;
} else if (vector->data_size == sizeof(double)) {
*(double*)destination_addr = *(double*)value;
} else if (vector->data_size == sizeof(float)) {
*(float*)destination_addr = *(float*)value;
} else {
memcpy(destination_addr, value, vector->data_size);
}
result.status = VECTOR_OK;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Value successfully set");
return result;
}
2025-10-04 17:20:59 +02:00
/**
* vector_get
* @vector: a non-null vector
* @index: a non-negative integer representing the position of an element
*
2025-10-25 17:21:12 +02:00
* Returns a vector_result_t data type containing the element at position @index if available
2025-10-04 17:20:59 +02:00
*/
2025-10-25 17:21:12 +02:00
vector_result_t vector_get(vector_t *vector, size_t index) {
vector_result_t result = {0};
2025-10-04 17:20:59 +02:00
if (vector == NULL) {
result.status = VECTOR_ERR_INVALID;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Invalid vector");
2025-10-04 17:20:59 +02:00
return result;
}
if (index >= vector->count) {
result.status = VECTOR_ERR_OVERFLOW;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Index out of bounds");
2025-10-04 17:20:59 +02:00
return result;
}
result.status = VECTOR_OK;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Value successfully retrieved");
result.value.element = (uint8_t *)vector->elements + (index * vector->data_size);
2025-10-04 17:20:59 +02:00
return result;
}
/**
* vector_pop
* @vector: a non-null vector
*
* Logically extract an element from the vector by following the LIFO policy.
* This method does NOT de-allocate memory
*
2025-10-25 17:21:12 +02:00
* Returns a vector_result_t data type
*/
2025-10-25 17:21:12 +02:00
vector_result_t vector_pop(vector_t *vector) {
vector_result_t result = {0};
if (vector == NULL) {
result.status = VECTOR_ERR_INVALID;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Invalid vector");
return result;
}
if (vector->count == 0) {
result.status = VECTOR_ERR_UNDERFLOW;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Vector is empty");
return result;
}
// Pop an element from the vector
const size_t index = (vector->count - 1);
2025-10-25 17:21:12 +02:00
vector_result_t popped_res = vector_get(vector, index);
if (popped_res.status != VECTOR_OK) {
return popped_res;
}
vector->count--;
result.status = VECTOR_OK;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Value successfully popped");
result.value.element = popped_res.value.element;
return result;
}
/**
* vector_clear
* @vector: a non-null vector
*
* Resets the vector to an empty state without de-allocating memory
*
2025-10-25 17:21:12 +02:00
* Returns a vector_result_t data type
*/
2025-10-25 17:21:12 +02:00
vector_result_t vector_clear(vector_t *vector) {
vector_result_t result = {0};
if (vector == NULL) {
result.status = VECTOR_ERR_INVALID;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Invalid vector");
return result;
}
vector->count = 0;
result.status = VECTOR_OK;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Vector successfully cleared");
return result;
}
2025-10-04 17:20:59 +02:00
/**
2025-11-01 17:25:05 +01:00
* vector_destroy
2025-10-04 17:20:59 +02:00
* @vector: a non-null vector
*
* Deletes the vector and all its elements from the memory
*
2025-10-25 17:21:12 +02:00
* Returns a vector_result_t data type
2025-10-04 17:20:59 +02:00
*/
2025-11-01 17:25:05 +01:00
vector_result_t vector_destroy(vector_t *vector) {
2025-10-25 17:21:12 +02:00
vector_result_t result = {0};
2025-10-04 17:20:59 +02:00
2025-11-01 17:25:05 +01:00
if (vector == NULL) {
result.status = VECTOR_ERR_INVALID;
SET_MSG(result, "Invalid vector");
return result;
2025-10-04 17:20:59 +02:00
}
2025-11-01 17:25:05 +01:00
free(vector->elements);
free(vector);
result.status = VECTOR_OK;
2025-10-25 17:21:12 +02:00
SET_MSG(result, "Vector successfully deleted");
2025-10-04 17:20:59 +02:00
return result;
}