REFACTOR: Translated to internal file for setup towards singular Vec
This commit is contained in:
-691
@@ -1,691 +0,0 @@
|
||||
#include "char.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
int
|
||||
is_valid(const Vec8_t* vec)
|
||||
{
|
||||
if (vec == nullptr)
|
||||
{
|
||||
LOGERROR(ERR_VECTOR_NULL);
|
||||
return ERR_VECTOR_NULL;
|
||||
}
|
||||
else if (vec->arr == nullptr)
|
||||
{
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return ERR_BUF_NULL;
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
Vec8_t
|
||||
create(const char* buf, const size_t len)
|
||||
{
|
||||
Vec8_t dst = { NULL, 0, CAPACITY, VEC_MAX };
|
||||
if (len >= dst.max_size)
|
||||
{
|
||||
LOGERROR(ERR_MAX_PASSED);
|
||||
LOG("If you need more memory to start, create a blank and "
|
||||
"reserve.");
|
||||
return dst;
|
||||
}
|
||||
if (dst.capacity < len && len < dst.max_size)
|
||||
{
|
||||
while (dst.capacity < len && dst.capacity * 2 < dst.max_size)
|
||||
dst.capacity *= 2;
|
||||
}
|
||||
if (dst.capacity * 2 > dst.max_size && len < dst.max_size)
|
||||
{
|
||||
dst.capacity = len;
|
||||
}
|
||||
if (len > 0)
|
||||
{
|
||||
char* dst_buf = calloc(dst.capacity, sizeof(char));
|
||||
if (!dst_buf)
|
||||
{
|
||||
LOGERROR(ERR_ALLOC_FAIL);
|
||||
return dst;
|
||||
}
|
||||
dst.arr = dst_buf;
|
||||
}
|
||||
if (buf)
|
||||
{
|
||||
for (size_t i = 0; i < len; i++)
|
||||
{
|
||||
dst.arr[i] = buf[i];
|
||||
dst.size++;
|
||||
}
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
Vec8_t
|
||||
push_back(Vec8_t* ptr, const char val)
|
||||
{
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL)
|
||||
{
|
||||
return create(nullptr, 0);
|
||||
}
|
||||
Vec8_t dst = { NULL, ptr->size, ptr->capacity, ptr->max_size };
|
||||
|
||||
if (is_valid(ptr) == ERR_BUF_NULL)
|
||||
{
|
||||
LOG("If you think this is wrong, reserve memory first");
|
||||
return dst;
|
||||
}
|
||||
if (ptr->size + 1 > ptr->max_size)
|
||||
{
|
||||
LOGERROR(ERR_MAX_PASSED);
|
||||
dst = *ptr;
|
||||
return dst;
|
||||
}
|
||||
|
||||
char* dst_buf;
|
||||
if (ptr->size + 1 > ptr->capacity)
|
||||
{
|
||||
if (ptr->capacity * 2 > ptr->max_size)
|
||||
{
|
||||
LOGERROR(ERR_MAX_PASSED);
|
||||
dst = *ptr;
|
||||
return dst;
|
||||
}
|
||||
dst.capacity = ptr->capacity * 2;
|
||||
dst_buf = realloc(ptr->arr, dst.capacity * sizeof(char));
|
||||
if (!dst_buf)
|
||||
{
|
||||
LOGERROR(ERR_ALLOC_FAIL);
|
||||
dst = *ptr;
|
||||
return dst;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dst_buf = ptr->arr;
|
||||
}
|
||||
dst.arr = dst_buf;
|
||||
dst.arr[dst.size] = val;
|
||||
dst.size++;
|
||||
return dst;
|
||||
}
|
||||
|
||||
Vec8_t
|
||||
pop_back(Vec8_t* ptr)
|
||||
{
|
||||
Vec8_t err = create(nullptr, 0);
|
||||
if (is_valid(ptr) != SUCCESS)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
if (ptr->size < 1)
|
||||
{
|
||||
LOGERROR(ERR_SIZE_ZERO);
|
||||
return err;
|
||||
}
|
||||
Vec8_t dst = { NULL, ptr->size - 1, ptr->capacity, ptr->max_size };
|
||||
dst.arr = ptr->arr;
|
||||
dst.arr[dst.size] = 0;
|
||||
return dst;
|
||||
}
|
||||
|
||||
Vec8_t
|
||||
reserve(const Vec8_t* ptr, const size_t capacity)
|
||||
{
|
||||
Vec8_t ret = create(nullptr, 0);
|
||||
if (capacity == 0)
|
||||
{
|
||||
LOGERROR(ERR_SIZE_ZERO);
|
||||
return ret;
|
||||
}
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
ret.arr = ptr->arr;
|
||||
if (is_valid(&ret) == ERR_BUF_NULL)
|
||||
{
|
||||
ret.arr = calloc(capacity, sizeof(char));
|
||||
if (!ret.arr)
|
||||
{
|
||||
LOGERROR(ERR_ALLOC_FAIL);
|
||||
ret = *ptr;
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
else if (is_valid(ptr) == SUCCESS)
|
||||
{
|
||||
ret.arr = realloc(ret.arr, capacity * sizeof(char));
|
||||
if (!ret.arr)
|
||||
{
|
||||
LOGERROR(ERR_ALLOC_FAIL);
|
||||
ret = *ptr;
|
||||
return ret;
|
||||
}
|
||||
ret.size = ptr->size;
|
||||
if (ret.size > capacity)
|
||||
{
|
||||
for (size_t i = capacity; i < ret.size; i++)
|
||||
{
|
||||
ret.arr[i] = 0;
|
||||
}
|
||||
ret.size = capacity;
|
||||
}
|
||||
}
|
||||
ret.capacity = capacity;
|
||||
ret.max_size = capacity;
|
||||
return ret;
|
||||
}
|
||||
|
||||
Vec8_t
|
||||
shrink_to_fit(Vec8_t* ptr)
|
||||
{
|
||||
if (is_valid(ptr) != SUCCESS) return create(nullptr, 0);
|
||||
if (ptr->size == 0)
|
||||
{
|
||||
LOGERROR(ERR_SIZE_ZERO);
|
||||
return create(nullptr, 0);
|
||||
}
|
||||
return reserve(ptr, ptr->size);
|
||||
}
|
||||
|
||||
int
|
||||
swap(Vec8_t* ptr1, Vec8_t* ptr2)
|
||||
{
|
||||
Vec8_t tmp = create(nullptr, 0);
|
||||
if (is_valid(ptr1) == ERR_VECTOR_NULL ||
|
||||
is_valid(ptr2) == ERR_VECTOR_NULL)
|
||||
{
|
||||
return ERR_VECTOR_NULL;
|
||||
}
|
||||
if (is_valid(ptr1) == ERR_BUF_NULL || is_valid(ptr2) == ERR_BUF_NULL)
|
||||
{
|
||||
return ERR_BUF_NULL;
|
||||
}
|
||||
tmp.arr = ptr1->arr;
|
||||
ptr1->arr = ptr2->arr;
|
||||
ptr2->arr = tmp.arr;
|
||||
|
||||
tmp.capacity = ptr1->capacity;
|
||||
tmp.size = ptr1->size;
|
||||
tmp.max_size = ptr1->max_size;
|
||||
ptr1->capacity = ptr2->capacity;
|
||||
ptr1->size = ptr2->size;
|
||||
ptr1->max_size = ptr2->max_size;
|
||||
|
||||
ptr2->capacity = tmp.capacity;
|
||||
ptr2->size = tmp.size;
|
||||
ptr2->max_size = tmp.max_size;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
char*
|
||||
data(const Vec8_t* ptr)
|
||||
{
|
||||
if (is_valid(ptr) == ERR_BUF_NULL || is_valid(ptr) == ERR_VECTOR_NULL)
|
||||
return nullptr;
|
||||
return &ptr->arr[0];
|
||||
}
|
||||
|
||||
size_t
|
||||
capacity(const Vec8_t* ptr)
|
||||
{
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL)
|
||||
{
|
||||
return ERR_VECTOR_NULL;
|
||||
}
|
||||
if (is_valid(ptr) == ERR_BUF_NULL)
|
||||
{
|
||||
return ERR_BUF_NULL;
|
||||
}
|
||||
return ptr->capacity;
|
||||
}
|
||||
|
||||
size_t
|
||||
max_size(const Vec8_t* ptr)
|
||||
{
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL)
|
||||
{
|
||||
return ERR_VECTOR_NULL;
|
||||
}
|
||||
if (is_valid(ptr) == ERR_BUF_NULL)
|
||||
{
|
||||
return ERR_BUF_NULL;
|
||||
}
|
||||
return ptr->max_size;
|
||||
}
|
||||
|
||||
void
|
||||
delete(const Vec8_t* ptr)
|
||||
{
|
||||
if (is_valid(ptr) == SUCCESS)
|
||||
{
|
||||
free((void*)ptr->arr);
|
||||
}
|
||||
}
|
||||
|
||||
Vec8_t
|
||||
clear(const Vec8_t* ptr)
|
||||
{
|
||||
Vec8_t dst = create(nullptr, 0);
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL || is_valid(ptr) == ERR_BUF_NULL)
|
||||
{
|
||||
return dst;
|
||||
}
|
||||
dst = *ptr;
|
||||
if (dst.size > 0)
|
||||
{
|
||||
for (size_t i = 0; i < dst.size; i++)
|
||||
{
|
||||
dst.arr[i] = 0;
|
||||
}
|
||||
dst.size = 0;
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
char
|
||||
at(const Vec8_t* ptr, const size_t idx)
|
||||
{
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL)
|
||||
{
|
||||
return ERR_VECTOR_NULL;
|
||||
}
|
||||
if (is_valid(ptr) == ERR_BUF_NULL)
|
||||
{
|
||||
return ERR_BUF_NULL;
|
||||
}
|
||||
if (idx >= ptr->size)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"At Index: [%zu] is out of bounds for size: [%zu]", idx,
|
||||
ptr->size);
|
||||
return ERR_OUT_OF_BOUNDS;
|
||||
}
|
||||
return ptr->arr[idx];
|
||||
}
|
||||
|
||||
void
|
||||
print_vec(const Vec8_t* ptr)
|
||||
{
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL || is_valid(ptr) == ERR_BUF_NULL)
|
||||
return;
|
||||
for (size_t i = 0; i < ptr->size; i++) printf("%c ", ptr->arr[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int
|
||||
begin(const Vec8_t* ptr)
|
||||
{
|
||||
if (is_valid(ptr) == SUCCESS) return 0;
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
int
|
||||
end(const Vec8_t* ptr)
|
||||
{
|
||||
if (is_valid(ptr) == SUCCESS)
|
||||
{
|
||||
if (ptr->size != 0) return (int)(ptr->size - 1);
|
||||
else return FAILURE;
|
||||
}
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
char
|
||||
front(const Vec8_t* ptr)
|
||||
{
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL) return FAILURE;
|
||||
if (ptr->size == 0) return FAILURE;
|
||||
return at(ptr, 0);
|
||||
}
|
||||
|
||||
char
|
||||
back(const Vec8_t* ptr)
|
||||
{
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL) return FAILURE;
|
||||
if (ptr->size == 0) return FAILURE;
|
||||
return at(ptr, ptr->size - 1);
|
||||
}
|
||||
|
||||
int
|
||||
empty(const Vec8_t* ptr)
|
||||
{
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL || is_valid(ptr) == ERR_BUF_NULL)
|
||||
return FAILURE;
|
||||
if (ptr->size == 0) return SUCCESS;
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) Vec8_t
|
||||
erase(const Vec8_t* ptr, const size_t iter)
|
||||
{
|
||||
Vec8_t dst = create(nullptr, 0);
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL) return dst;
|
||||
if (is_valid(ptr) == ERR_BUF_NULL) return dst;
|
||||
if (iter >= ptr->size) return dst;
|
||||
dst = *ptr;
|
||||
memmove(&dst.arr[iter], &dst.arr[iter + 1],
|
||||
(dst.size - iter - 1) * sizeof(char));
|
||||
dst.arr[dst.size - 1] = 0;
|
||||
dst.size--;
|
||||
return dst;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) Vec8_t
|
||||
erase(const Vec8_t* ptr, const size_t iter_start, const size_t iter_end)
|
||||
{
|
||||
Vec8_t dst = create(nullptr, 0);
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL) return dst;
|
||||
if (is_valid(ptr) == ERR_BUF_NULL) return dst;
|
||||
if (iter_end > ptr->size) return dst;
|
||||
if (iter_start >= iter_end) return dst;
|
||||
dst = *ptr;
|
||||
size_t diff = iter_end - iter_start;
|
||||
memmove(&dst.arr[iter_start], &dst.arr[iter_end],
|
||||
(dst.size - iter_end) * sizeof(char));
|
||||
for (size_t i = dst.size - diff; i < dst.size; i++)
|
||||
{
|
||||
dst.arr[i] = 0;
|
||||
}
|
||||
dst.size -= diff;
|
||||
return dst;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) Vec8_t
|
||||
resize(const Vec8_t* ptr, const size_t size)
|
||||
{
|
||||
Vec8_t dst = create(nullptr, 0);
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL || is_valid(ptr) == ERR_BUF_NULL)
|
||||
return dst;
|
||||
dst = *ptr;
|
||||
if (ptr->size == size)
|
||||
{
|
||||
LOG("ERR :- Ptr size and size are the same");
|
||||
dst = *ptr;
|
||||
return dst;
|
||||
}
|
||||
|
||||
else if (size > ptr->max_size)
|
||||
{
|
||||
LOGERROR(ERR_MAX_PASSED);
|
||||
dst = *ptr;
|
||||
return dst;
|
||||
}
|
||||
|
||||
else if (ptr->size == 0)
|
||||
{
|
||||
size_t max = dst.max_size;
|
||||
dst = reserve(&dst, size);
|
||||
if (dst.capacity != size)
|
||||
{
|
||||
LOGERROR(ERR_ALLOC_FAIL);
|
||||
dst = *ptr;
|
||||
return dst;
|
||||
}
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
dst.arr[i] = '\0';
|
||||
}
|
||||
dst.max_size = max;
|
||||
dst.size = size;
|
||||
return dst;
|
||||
}
|
||||
else if (size > ptr->capacity)
|
||||
{
|
||||
size_t tmpcp = ptr->capacity;
|
||||
size_t tmpmx = ptr->max_size;
|
||||
while (size > tmpcp)
|
||||
{
|
||||
tmpcp *= 2;
|
||||
}
|
||||
if (tmpcp > ptr->max_size)
|
||||
{
|
||||
tmpcp = size;
|
||||
}
|
||||
dst = reserve(ptr, tmpcp);
|
||||
if (dst.capacity != tmpcp)
|
||||
{
|
||||
LOGERROR(ERR_ALLOC_FAIL);
|
||||
dst = *ptr;
|
||||
return dst;
|
||||
}
|
||||
dst.max_size = tmpmx;
|
||||
}
|
||||
if (size < ptr->size)
|
||||
{
|
||||
for (size_t i = size; i < dst.size; i++)
|
||||
{
|
||||
dst.arr[i] = '\0';
|
||||
}
|
||||
}
|
||||
if (dst.size < size && size <= dst.capacity)
|
||||
{
|
||||
for (size_t i = dst.size; i < size; i++)
|
||||
{
|
||||
dst.arr[i] = '\0';
|
||||
}
|
||||
}
|
||||
dst.size = size;
|
||||
return dst;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) Vec8_t
|
||||
resize(const Vec8_t* ptr, const size_t size, const char val)
|
||||
{
|
||||
Vec8_t dst = create(nullptr, 0);
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL || is_valid(ptr) == ERR_BUF_NULL)
|
||||
return dst;
|
||||
dst = *ptr;
|
||||
if (ptr->size == size)
|
||||
{
|
||||
LOG("ERR :- Ptr size and size are the same");
|
||||
dst = *ptr;
|
||||
return dst;
|
||||
}
|
||||
|
||||
else if (size > ptr->max_size)
|
||||
{
|
||||
LOGERROR(ERR_MAX_PASSED);
|
||||
dst = *ptr;
|
||||
return dst;
|
||||
}
|
||||
|
||||
else if (ptr->size == 0)
|
||||
{
|
||||
size_t max = dst.max_size;
|
||||
dst = reserve(&dst, size);
|
||||
if (dst.capacity != size)
|
||||
{
|
||||
LOGERROR(ERR_ALLOC_FAIL);
|
||||
dst = *ptr;
|
||||
return dst;
|
||||
}
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
dst.arr[i] = val;
|
||||
}
|
||||
dst.max_size = max;
|
||||
dst.size = size;
|
||||
return dst;
|
||||
}
|
||||
else if (size > ptr->capacity)
|
||||
{
|
||||
size_t tmpcp = ptr->capacity;
|
||||
size_t tmpmx = ptr->max_size;
|
||||
while (size > tmpcp)
|
||||
{
|
||||
tmpcp *= 2;
|
||||
}
|
||||
if (tmpcp > ptr->max_size)
|
||||
{
|
||||
tmpcp = size;
|
||||
}
|
||||
dst = reserve(ptr, tmpcp);
|
||||
if (dst.capacity != tmpcp)
|
||||
{
|
||||
LOGERROR(ERR_ALLOC_FAIL);
|
||||
dst = *ptr;
|
||||
return dst;
|
||||
}
|
||||
dst.max_size = tmpmx;
|
||||
}
|
||||
if (size < ptr->size)
|
||||
{
|
||||
for (size_t i = size; i < dst.size; i++)
|
||||
{
|
||||
dst.arr[i] = val;
|
||||
}
|
||||
}
|
||||
if (dst.size < size && size <= dst.capacity)
|
||||
{
|
||||
for (size_t i = dst.size; i < size; i++)
|
||||
{
|
||||
dst.arr[i] = val;
|
||||
}
|
||||
}
|
||||
dst.size = size;
|
||||
return dst;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) Vec8_t
|
||||
assign(const Vec8_t* ptr, const Vec8_t* iterator, const size_t iter_start,
|
||||
const size_t iter_end)
|
||||
{
|
||||
Vec8_t dst = create(nullptr, 0);
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL || is_valid(ptr) == ERR_BUF_NULL)
|
||||
return dst;
|
||||
if (is_valid(iterator) == ERR_VECTOR_NULL ||
|
||||
is_valid(iterator) == ERR_BUF_NULL)
|
||||
return dst;
|
||||
|
||||
if (ptr->arr == iterator->arr)
|
||||
{
|
||||
LOGERROR(ERR_DOUBLE_BUF);
|
||||
dst = *ptr;
|
||||
return dst;
|
||||
}
|
||||
if (iterator->size < iter_end || iter_end <= iter_start)
|
||||
{
|
||||
LOGERROR(ERR_OUT_OF_BOUNDS);
|
||||
return dst;
|
||||
}
|
||||
dst = *ptr;
|
||||
dst = erase(ptr, begin(ptr), end(ptr) + 1);
|
||||
dst = reserve(&dst, (iter_end - iter_start));
|
||||
for (size_t i = iter_start; i < iter_end; i++)
|
||||
{
|
||||
dst = push_back(&dst, at(iterator, i));
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) Vec8_t
|
||||
assign(const Vec8_t* ptr, const size_t amount, const char val)
|
||||
{
|
||||
Vec8_t dst = create(nullptr, 0);
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL || is_valid(ptr) == ERR_BUF_NULL)
|
||||
return create(nullptr, 0);
|
||||
dst = *ptr;
|
||||
if (amount == 0)
|
||||
{
|
||||
LOGERROR(ERR_SIZE_ZERO);
|
||||
return dst;
|
||||
}
|
||||
dst = erase(&dst, begin(&dst), end(&dst) + 1);
|
||||
dst = reserve(&dst, amount);
|
||||
for (size_t i = 0; i < amount; i++)
|
||||
{
|
||||
dst = push_back(&dst, val);
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) Vec8_t
|
||||
insert(const Vec8_t* ptr, const size_t iter, const char val)
|
||||
{
|
||||
Vec8_t dst = create(nullptr, 0);
|
||||
if (is_valid(ptr) == ERR_VECTOR_NULL || is_valid(ptr) == ERR_BUF_NULL)
|
||||
{
|
||||
return dst;
|
||||
}
|
||||
if (ptr->max_size < ptr->size + 1)
|
||||
{
|
||||
LOGERROR(ERR_MAX_PASSED);
|
||||
return dst;
|
||||
}
|
||||
if (iter > ptr->size)
|
||||
{
|
||||
LOGERROR(ERR_OUT_OF_BOUNDS);
|
||||
return dst;
|
||||
}
|
||||
dst = *ptr;
|
||||
dst = push_back(&dst, 0);
|
||||
for (size_t i = dst.size - 1; i > iter; i--)
|
||||
{
|
||||
dst.arr[i] = dst.arr[i - 1];
|
||||
}
|
||||
dst.arr[iter] = val;
|
||||
|
||||
return dst;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) Vec8_t
|
||||
insert(const Vec8_t* ptr, const size_t iter, const size_t amount,
|
||||
const char val)
|
||||
{
|
||||
Vec8_t dst = create(nullptr, 0);
|
||||
if (is_valid(ptr) != SUCCESS)
|
||||
{
|
||||
LOGERROR(ERR_NULL);
|
||||
return dst;
|
||||
}
|
||||
if (iter > ptr->size)
|
||||
{
|
||||
LOGERROR(ERR_OUT_OF_BOUNDS);
|
||||
return dst;
|
||||
}
|
||||
if (amount <= 0)
|
||||
{
|
||||
LOGERROR(ERR_SIZE_ZERO);
|
||||
return dst;
|
||||
}
|
||||
dst = *ptr;
|
||||
for (size_t i = iter; i < iter + amount; i++)
|
||||
{
|
||||
dst = insert(&dst, i, val);
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) Vec8_t
|
||||
insert(const Vec8_t* dst_vec, const Vec8_t* src_vec, const size_t iter,
|
||||
const size_t iter_start, const size_t iter_end)
|
||||
{
|
||||
Vec8_t dst = create(nullptr, 0);
|
||||
if (is_valid(dst_vec) != SUCCESS || is_valid(src_vec) != SUCCESS)
|
||||
{
|
||||
LOGERROR(ERR_NULL);
|
||||
return dst;
|
||||
}
|
||||
if (iter_end > src_vec->size || iter > dst_vec->size)
|
||||
{
|
||||
LOGERROR(ERR_OUT_OF_BOUNDS);
|
||||
return dst;
|
||||
}
|
||||
if (dst_vec->arr == src_vec->arr)
|
||||
{
|
||||
LOGERROR(ERR_DOUBLE_BUF);
|
||||
return dst;
|
||||
}
|
||||
dst = *dst_vec;
|
||||
for (size_t i = iter_start; i < iter_end; i++)
|
||||
{
|
||||
dst = insert(&dst, iter + (i - iter_start), src_vec->arr[i]);
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
@@ -0,0 +1,725 @@
|
||||
#include "char.h"
|
||||
|
||||
#include <limits.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "utils/utils.h"
|
||||
|
||||
size_t
|
||||
_vec_char_is_valid(const _VecCharInternal* ptr)
|
||||
{
|
||||
if (ptr == nullptr)
|
||||
{
|
||||
LOGERROR(ERR_VECTOR_NULL);
|
||||
return ERR_VECTOR_NULL;
|
||||
}
|
||||
else if (ptr->_internal_arr == nullptr)
|
||||
{
|
||||
LOGERROR(ERR_BUF_NULL);
|
||||
return ERR_BUF_NULL;
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
_VecCharInternal
|
||||
_vec_char_create(const char* src, const size_t size)
|
||||
{
|
||||
_VecCharInternal self = {
|
||||
._internal_arr = NULL,
|
||||
._internal_size = 0,
|
||||
._internal_capacity = CAPACITY,
|
||||
._internal_max_size = VEC_MAX,
|
||||
};
|
||||
if (size >= self._internal_max_size)
|
||||
{
|
||||
LOGERROR(ERR_MAX_PASSED);
|
||||
LOG("If you need more memory to start, create a blank and "
|
||||
"selferve.");
|
||||
return self;
|
||||
}
|
||||
if (self._internal_capacity < size && size < self._internal_max_size)
|
||||
{
|
||||
while (self._internal_capacity < size &&
|
||||
self._internal_capacity * 2 < self._internal_max_size)
|
||||
self._internal_capacity *= 2;
|
||||
}
|
||||
if (self._internal_capacity * 2 > self._internal_max_size &&
|
||||
size < self._internal_max_size)
|
||||
{
|
||||
self._internal_capacity = size;
|
||||
}
|
||||
if (size > 0)
|
||||
{
|
||||
char* dst_buf = calloc(self._internal_capacity, sizeof(char));
|
||||
if (!dst_buf)
|
||||
{
|
||||
LOGERROR(ERR_ALLOC_FAIL);
|
||||
return self;
|
||||
}
|
||||
self._internal_arr = dst_buf;
|
||||
}
|
||||
if (src)
|
||||
{
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
self._internal_arr[i] = src[i];
|
||||
self._internal_size++;
|
||||
}
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
_VecCharInternal
|
||||
_vec_char_push_back(_VecCharInternal* ptr, const char val)
|
||||
{
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL)
|
||||
{
|
||||
return _vec_char_create(nullptr, 0);
|
||||
}
|
||||
_VecCharInternal self = { NULL, ptr->_internal_size,
|
||||
ptr->_internal_capacity,
|
||||
ptr->_internal_max_size };
|
||||
|
||||
if (_vec_char_is_valid(ptr) == ERR_BUF_NULL)
|
||||
{
|
||||
LOG("If you think this is wrong, reserve memory first");
|
||||
return self;
|
||||
}
|
||||
if (ptr->_internal_size + 1 > ptr->_internal_max_size)
|
||||
{
|
||||
LOGERROR(ERR_MAX_PASSED);
|
||||
self = *ptr;
|
||||
return self;
|
||||
}
|
||||
|
||||
char* dst_buf;
|
||||
if (ptr->_internal_size + 1 > ptr->_internal_capacity)
|
||||
{
|
||||
if (ptr->_internal_capacity * 2 > ptr->_internal_max_size)
|
||||
{
|
||||
LOGERROR(ERR_MAX_PASSED);
|
||||
self = *ptr;
|
||||
return self;
|
||||
}
|
||||
self._internal_capacity = ptr->_internal_capacity * 2;
|
||||
dst_buf = realloc(ptr->_internal_arr,
|
||||
self._internal_capacity * sizeof(char));
|
||||
if (!dst_buf)
|
||||
{
|
||||
LOGERROR(ERR_ALLOC_FAIL);
|
||||
self = *ptr;
|
||||
return self;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dst_buf = ptr->_internal_arr;
|
||||
}
|
||||
self._internal_arr = dst_buf;
|
||||
self._internal_arr[self._internal_size] = val;
|
||||
self._internal_size++;
|
||||
return self;
|
||||
}
|
||||
|
||||
_VecCharInternal
|
||||
_vec_char_pop_back(_VecCharInternal* ptr)
|
||||
{
|
||||
_VecCharInternal err = _vec_char_create(nullptr, 0);
|
||||
if (_vec_char_is_valid(ptr) != SUCCESS)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
if (ptr->_internal_size < 1)
|
||||
{
|
||||
LOGERROR(ERR_SIZE_ZERO);
|
||||
return err;
|
||||
}
|
||||
_VecCharInternal self = { NULL, ptr->_internal_size - 1,
|
||||
ptr->_internal_capacity,
|
||||
ptr->_internal_max_size };
|
||||
self._internal_arr = ptr->_internal_arr;
|
||||
self._internal_arr[self._internal_size] = 0;
|
||||
return self;
|
||||
}
|
||||
|
||||
_VecCharInternal
|
||||
_vec_char_reserve(const _VecCharInternal* ptr, const size_t capacity)
|
||||
{
|
||||
_VecCharInternal self = _vec_char_create(nullptr, 0);
|
||||
if (capacity == 0)
|
||||
{
|
||||
LOGERROR(ERR_SIZE_ZERO);
|
||||
return self;
|
||||
}
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL)
|
||||
{
|
||||
return self;
|
||||
}
|
||||
self._internal_arr = ptr->_internal_arr;
|
||||
if (_vec_char_is_valid(&self) == ERR_BUF_NULL)
|
||||
{
|
||||
self._internal_arr = calloc(capacity, sizeof(char));
|
||||
if (!self._internal_arr)
|
||||
{
|
||||
LOGERROR(ERR_ALLOC_FAIL);
|
||||
self = *ptr;
|
||||
return self;
|
||||
}
|
||||
}
|
||||
else if (_vec_char_is_valid(ptr) == SUCCESS)
|
||||
{
|
||||
self._internal_arr =
|
||||
realloc(self._internal_arr, capacity * sizeof(char));
|
||||
if (!self._internal_arr)
|
||||
{
|
||||
LOGERROR(ERR_ALLOC_FAIL);
|
||||
self = *ptr;
|
||||
return self;
|
||||
}
|
||||
self._internal_size = ptr->_internal_size;
|
||||
if (self._internal_size > capacity)
|
||||
{
|
||||
for (size_t i = capacity; i < self._internal_size; i++)
|
||||
{
|
||||
self._internal_arr[i] = 0;
|
||||
}
|
||||
self._internal_size = capacity;
|
||||
}
|
||||
}
|
||||
self._internal_capacity = capacity;
|
||||
self._internal_max_size = capacity;
|
||||
return self;
|
||||
}
|
||||
|
||||
_VecCharInternal
|
||||
_vec_char_shrink_to_fit(_VecCharInternal* ptr)
|
||||
{
|
||||
if (_vec_char_is_valid(ptr) != SUCCESS)
|
||||
return _vec_char_create(nullptr, 0);
|
||||
if (ptr->_internal_size == 0)
|
||||
{
|
||||
LOGERROR(ERR_SIZE_ZERO);
|
||||
return _vec_char_create(nullptr, 0);
|
||||
}
|
||||
return _vec_char_reserve(ptr, ptr->_internal_size);
|
||||
}
|
||||
|
||||
size_t
|
||||
_vec_char_swap(_VecCharInternal* ptr1, _VecCharInternal* ptr2)
|
||||
{
|
||||
_VecCharInternal self = _vec_char_create(nullptr, 0);
|
||||
if (_vec_char_is_valid(ptr1) == ERR_VECTOR_NULL ||
|
||||
_vec_char_is_valid(ptr2) == ERR_VECTOR_NULL)
|
||||
{
|
||||
return ERR_VECTOR_NULL;
|
||||
}
|
||||
if (_vec_char_is_valid(ptr1) == ERR_BUF_NULL ||
|
||||
_vec_char_is_valid(ptr2) == ERR_BUF_NULL)
|
||||
{
|
||||
return ERR_BUF_NULL;
|
||||
}
|
||||
self._internal_arr = ptr1->_internal_arr;
|
||||
ptr1->_internal_arr = ptr2->_internal_arr;
|
||||
ptr2->_internal_arr = self._internal_arr;
|
||||
|
||||
self._internal_capacity = ptr1->_internal_capacity;
|
||||
self._internal_size = ptr1->_internal_size;
|
||||
self._internal_max_size = ptr1->_internal_max_size;
|
||||
ptr1->_internal_capacity = ptr2->_internal_capacity;
|
||||
ptr1->_internal_size = ptr2->_internal_size;
|
||||
ptr1->_internal_max_size = ptr2->_internal_max_size;
|
||||
|
||||
ptr2->_internal_capacity = self._internal_capacity;
|
||||
ptr2->_internal_size = self._internal_size;
|
||||
ptr2->_internal_max_size = self._internal_max_size;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
char*
|
||||
_vec_char_data(const _VecCharInternal* ptr)
|
||||
{
|
||||
if (_vec_char_is_valid(ptr) == ERR_BUF_NULL ||
|
||||
_vec_char_is_valid(ptr) == ERR_VECTOR_NULL)
|
||||
return nullptr;
|
||||
return &ptr->_internal_arr[0];
|
||||
}
|
||||
|
||||
size_t
|
||||
_vec_char_capacity(const _VecCharInternal* ptr)
|
||||
{
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL)
|
||||
{
|
||||
return ERR_VECTOR_NULL;
|
||||
}
|
||||
if (_vec_char_is_valid(ptr) == ERR_BUF_NULL)
|
||||
{
|
||||
return ERR_BUF_NULL;
|
||||
}
|
||||
return ptr->_internal_capacity;
|
||||
}
|
||||
|
||||
size_t
|
||||
_vec_char_max_size(const _VecCharInternal* ptr)
|
||||
{
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL)
|
||||
{
|
||||
return ERR_VECTOR_NULL;
|
||||
}
|
||||
if (_vec_char_is_valid(ptr) == ERR_BUF_NULL)
|
||||
{
|
||||
return ERR_BUF_NULL;
|
||||
}
|
||||
return ptr->_internal_max_size;
|
||||
}
|
||||
|
||||
void
|
||||
_vec_char_delete(const _VecCharInternal* ptr)
|
||||
{
|
||||
if (_vec_char_is_valid(ptr) == SUCCESS)
|
||||
{
|
||||
free((void*)ptr->_internal_arr);
|
||||
}
|
||||
}
|
||||
|
||||
_VecCharInternal
|
||||
_vec_char_clear(const _VecCharInternal* ptr)
|
||||
{
|
||||
_VecCharInternal self = _vec_char_create(nullptr, 0);
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL ||
|
||||
_vec_char_is_valid(ptr) == ERR_BUF_NULL)
|
||||
{
|
||||
return self;
|
||||
}
|
||||
self = *ptr;
|
||||
if (self._internal_size > 0)
|
||||
{
|
||||
for (size_t i = 0; i < self._internal_size; i++)
|
||||
{
|
||||
self._internal_arr[i] = 0;
|
||||
}
|
||||
self._internal_size = 0;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
char
|
||||
_vec_char_at(const _VecCharInternal* ptr, const size_t idx)
|
||||
{
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL)
|
||||
{
|
||||
return ERR_VECTOR_NULL;
|
||||
}
|
||||
if (_vec_char_is_valid(ptr) == ERR_BUF_NULL)
|
||||
{
|
||||
return ERR_BUF_NULL;
|
||||
}
|
||||
if (idx >= ptr->_internal_size)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"At Index: [%zu] is out of bounds for size: [%zu]", idx,
|
||||
ptr->_internal_size);
|
||||
return ERR_OUT_OF_BOUNDS;
|
||||
}
|
||||
return ptr->_internal_arr[idx];
|
||||
}
|
||||
|
||||
void
|
||||
_vec_char_print_vec(const _VecCharInternal* ptr)
|
||||
{
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL ||
|
||||
_vec_char_is_valid(ptr) == ERR_BUF_NULL)
|
||||
return;
|
||||
for (size_t i = 0; i < ptr->_internal_size; i++)
|
||||
printf("%c ", ptr->_internal_arr[i]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
size_t
|
||||
_vec_char_begin(const _VecCharInternal* ptr)
|
||||
{
|
||||
if (_vec_char_is_valid(ptr) == SUCCESS) return 0;
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
size_t
|
||||
_vec_char_end(const _VecCharInternal* ptr)
|
||||
{
|
||||
if (_vec_char_is_valid(ptr) == SUCCESS)
|
||||
{
|
||||
if (ptr->_internal_size != 0)
|
||||
return (int)(ptr->_internal_size - 1);
|
||||
else return FAILURE;
|
||||
}
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
char
|
||||
_vec_char_front(const _VecCharInternal* ptr)
|
||||
{
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) return FAILURE;
|
||||
if (ptr->_internal_size == 0) return FAILURE;
|
||||
return _vec_char_at(ptr, 0);
|
||||
}
|
||||
|
||||
char
|
||||
_vec_char_back(const _VecCharInternal* ptr)
|
||||
{
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) return FAILURE;
|
||||
if (ptr->_internal_size == 0) return FAILURE;
|
||||
return _vec_char_at(ptr, ptr->_internal_size - 1);
|
||||
}
|
||||
|
||||
size_t
|
||||
_vec_char_empty(const _VecCharInternal* ptr)
|
||||
{
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL ||
|
||||
_vec_char_is_valid(ptr) == ERR_BUF_NULL)
|
||||
return FAILURE;
|
||||
if (ptr->_internal_size == 0) return SUCCESS;
|
||||
return FAILURE;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_erase(const _VecCharInternal* ptr, const size_t iter)
|
||||
{
|
||||
_VecCharInternal self = _vec_char_create(nullptr, 0);
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) return self;
|
||||
if (_vec_char_is_valid(ptr) == ERR_BUF_NULL) return self;
|
||||
if (iter >= ptr->_internal_size) return self;
|
||||
self = *ptr;
|
||||
memmove(&self._internal_arr[iter], &self._internal_arr[iter + 1],
|
||||
(self._internal_size - iter - 1) * sizeof(char));
|
||||
self._internal_arr[self._internal_size - 1] = 0;
|
||||
self._internal_size--;
|
||||
return self;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_erase(const _VecCharInternal* ptr, const size_t iter_start,
|
||||
const size_t iter_end)
|
||||
{
|
||||
_VecCharInternal self = _vec_char_create(nullptr, 0);
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL) return self;
|
||||
if (_vec_char_is_valid(ptr) == ERR_BUF_NULL) return self;
|
||||
if (iter_end > ptr->_internal_size) return self;
|
||||
if (iter_start >= iter_end) return self;
|
||||
self = *ptr;
|
||||
size_t diff = iter_end - iter_start;
|
||||
memmove(&self._internal_arr[iter_start], &self._internal_arr[iter_end],
|
||||
(self._internal_size - iter_end) * sizeof(char));
|
||||
for (size_t i = self._internal_size - diff; i < self._internal_size;
|
||||
i++)
|
||||
{
|
||||
self._internal_arr[i] = 0;
|
||||
}
|
||||
self._internal_size -= diff;
|
||||
return self;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_resize(const _VecCharInternal* ptr, const size_t size)
|
||||
{
|
||||
_VecCharInternal self = _vec_char_create(nullptr, 0);
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL ||
|
||||
_vec_char_is_valid(ptr) == ERR_BUF_NULL)
|
||||
return self;
|
||||
self = *ptr;
|
||||
if (ptr->_internal_size == size)
|
||||
{
|
||||
LOG("ERR :- Ptr size and size are the same");
|
||||
self = *ptr;
|
||||
return self;
|
||||
}
|
||||
|
||||
else if (size > ptr->_internal_max_size)
|
||||
{
|
||||
LOGERROR(ERR_MAX_PASSED);
|
||||
self = *ptr;
|
||||
return self;
|
||||
}
|
||||
|
||||
else if (ptr->_internal_size == 0)
|
||||
{
|
||||
size_t max = self._internal_max_size;
|
||||
self = _vec_char_reserve(&self, size);
|
||||
if (self._internal_capacity != size)
|
||||
{
|
||||
LOGERROR(ERR_ALLOC_FAIL);
|
||||
self = *ptr;
|
||||
return self;
|
||||
}
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
self._internal_arr[i] = '\0';
|
||||
}
|
||||
self._internal_max_size = max;
|
||||
self._internal_size = size;
|
||||
return self;
|
||||
}
|
||||
else if (size > ptr->_internal_capacity)
|
||||
{
|
||||
size_t tmpcp = ptr->_internal_capacity;
|
||||
size_t tmpmx = ptr->_internal_max_size;
|
||||
while (size > tmpcp)
|
||||
{
|
||||
tmpcp *= 2;
|
||||
}
|
||||
if (tmpcp > ptr->_internal_max_size)
|
||||
{
|
||||
tmpcp = size;
|
||||
}
|
||||
self = _vec_char_reserve(ptr, tmpcp);
|
||||
if (self._internal_capacity != tmpcp)
|
||||
{
|
||||
LOGERROR(ERR_ALLOC_FAIL);
|
||||
self = *ptr;
|
||||
return self;
|
||||
}
|
||||
self._internal_max_size = tmpmx;
|
||||
}
|
||||
if (size < ptr->_internal_size)
|
||||
{
|
||||
for (size_t i = size; i < self._internal_size; i++)
|
||||
{
|
||||
self._internal_arr[i] = '\0';
|
||||
}
|
||||
}
|
||||
if (self._internal_size < size && size <= self._internal_capacity)
|
||||
{
|
||||
for (size_t i = self._internal_size; i < size; i++)
|
||||
{
|
||||
self._internal_arr[i] = '\0';
|
||||
}
|
||||
}
|
||||
self._internal_size = size;
|
||||
return self;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_resize(const _VecCharInternal* ptr, const size_t size, const char val)
|
||||
{
|
||||
_VecCharInternal self = _vec_char_create(nullptr, 0);
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL ||
|
||||
_vec_char_is_valid(ptr) == ERR_BUF_NULL)
|
||||
return self;
|
||||
self = *ptr;
|
||||
if (ptr->_internal_size == size)
|
||||
{
|
||||
LOG("ERR :- Ptr size and size are the same");
|
||||
self = *ptr;
|
||||
return self;
|
||||
}
|
||||
|
||||
else if (size > ptr->_internal_max_size)
|
||||
{
|
||||
LOGERROR(ERR_MAX_PASSED);
|
||||
self = *ptr;
|
||||
return self;
|
||||
}
|
||||
|
||||
else if (ptr->_internal_size == 0)
|
||||
{
|
||||
size_t max = self._internal_max_size;
|
||||
self = _vec_char_reserve(&self, size);
|
||||
if (self._internal_capacity != size)
|
||||
{
|
||||
LOGERROR(ERR_ALLOC_FAIL);
|
||||
self = *ptr;
|
||||
return self;
|
||||
}
|
||||
for (size_t i = 0; i < size; i++)
|
||||
{
|
||||
self._internal_arr[i] = val;
|
||||
}
|
||||
self._internal_max_size = max;
|
||||
self._internal_size = size;
|
||||
return self;
|
||||
}
|
||||
else if (size > ptr->_internal_capacity)
|
||||
{
|
||||
size_t tmpcp = ptr->_internal_capacity;
|
||||
size_t tmpmx = ptr->_internal_max_size;
|
||||
while (size > tmpcp)
|
||||
{
|
||||
tmpcp *= 2;
|
||||
}
|
||||
if (tmpcp > ptr->_internal_max_size)
|
||||
{
|
||||
tmpcp = size;
|
||||
}
|
||||
self = _vec_char_reserve(ptr, tmpcp);
|
||||
if (self._internal_capacity != tmpcp)
|
||||
{
|
||||
LOGERROR(ERR_ALLOC_FAIL);
|
||||
self = *ptr;
|
||||
return self;
|
||||
}
|
||||
self._internal_max_size = tmpmx;
|
||||
}
|
||||
if (size < ptr->_internal_size)
|
||||
{
|
||||
for (size_t i = size; i < self._internal_size; i++)
|
||||
{
|
||||
self._internal_arr[i] = val;
|
||||
}
|
||||
}
|
||||
if (self._internal_size < size && size <= self._internal_capacity)
|
||||
{
|
||||
for (size_t i = self._internal_size; i < size; i++)
|
||||
{
|
||||
self._internal_arr[i] = val;
|
||||
}
|
||||
}
|
||||
self._internal_size = size;
|
||||
return self;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_assign(const _VecCharInternal* ptr, const _VecCharInternal* iterator,
|
||||
const size_t iter_start, const size_t iter_end)
|
||||
{
|
||||
_VecCharInternal self = _vec_char_create(nullptr, 0);
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL ||
|
||||
_vec_char_is_valid(ptr) == ERR_BUF_NULL)
|
||||
return self;
|
||||
if (_vec_char_is_valid(iterator) == ERR_VECTOR_NULL ||
|
||||
_vec_char_is_valid(iterator) == ERR_BUF_NULL)
|
||||
return self;
|
||||
|
||||
if (ptr->_internal_arr == iterator->_internal_arr)
|
||||
{
|
||||
LOGERROR(ERR_DOUBLE_BUF);
|
||||
self = *ptr;
|
||||
return self;
|
||||
}
|
||||
if (iterator->_internal_size < iter_end || iter_end <= iter_start)
|
||||
{
|
||||
LOGERROR(ERR_OUT_OF_BOUNDS);
|
||||
return self;
|
||||
}
|
||||
self = *ptr;
|
||||
self =
|
||||
_vec_char_erase(ptr, _vec_char_begin(ptr), _vec_char_end(ptr) + 1);
|
||||
self = _vec_char_reserve(&self, (iter_end - iter_start));
|
||||
for (size_t i = iter_start; i < iter_end; i++)
|
||||
{
|
||||
self = _vec_char_push_back(&self, _vec_char_at(iterator, i));
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_assign(const _VecCharInternal* ptr, const size_t amount,
|
||||
const char val)
|
||||
{
|
||||
_VecCharInternal self = _vec_char_create(nullptr, 0);
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL ||
|
||||
_vec_char_is_valid(ptr) == ERR_BUF_NULL)
|
||||
return _vec_char_create(nullptr, 0);
|
||||
self = *ptr;
|
||||
if (amount == 0)
|
||||
{
|
||||
LOGERROR(ERR_SIZE_ZERO);
|
||||
return self;
|
||||
}
|
||||
self = _vec_char_erase(&self, _vec_char_begin(&self),
|
||||
_vec_char_end(&self) + 1);
|
||||
self = _vec_char_reserve(&self, amount);
|
||||
for (size_t i = 0; i < amount; i++)
|
||||
{
|
||||
self = _vec_char_push_back(&self, val);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_insert(const _VecCharInternal* ptr, const size_t iter, const char val)
|
||||
{
|
||||
_VecCharInternal self = _vec_char_create(nullptr, 0);
|
||||
if (_vec_char_is_valid(ptr) == ERR_VECTOR_NULL ||
|
||||
_vec_char_is_valid(ptr) == ERR_BUF_NULL)
|
||||
{
|
||||
return self;
|
||||
}
|
||||
if (ptr->_internal_max_size < ptr->_internal_size + 1)
|
||||
{
|
||||
LOGERROR(ERR_MAX_PASSED);
|
||||
return self;
|
||||
}
|
||||
if (iter > ptr->_internal_size)
|
||||
{
|
||||
LOGERROR(ERR_OUT_OF_BOUNDS);
|
||||
return self;
|
||||
}
|
||||
self = *ptr;
|
||||
self = _vec_char_push_back(&self, 0);
|
||||
for (size_t i = self._internal_size - 1; i > iter; i--)
|
||||
{
|
||||
self._internal_arr[i] = self._internal_arr[i - 1];
|
||||
}
|
||||
self._internal_arr[iter] = val;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_insert(const _VecCharInternal* ptr, const size_t iter,
|
||||
const size_t amount, const char val)
|
||||
{
|
||||
_VecCharInternal self = _vec_char_create(nullptr, 0);
|
||||
if (_vec_char_is_valid(ptr) != SUCCESS)
|
||||
{
|
||||
LOGERROR(ERR_NULL);
|
||||
return self;
|
||||
}
|
||||
if (iter > ptr->_internal_size)
|
||||
{
|
||||
LOGERROR(ERR_OUT_OF_BOUNDS);
|
||||
return self;
|
||||
}
|
||||
if (amount <= 0)
|
||||
{
|
||||
LOGERROR(ERR_SIZE_ZERO);
|
||||
return self;
|
||||
}
|
||||
self = *ptr;
|
||||
for (size_t i = iter; i < iter + amount; i++)
|
||||
{
|
||||
self = _vec_char_insert(&self, i, val);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_insert(const _VecCharInternal* ptr, const _VecCharInternal* iterator,
|
||||
const size_t iter, const size_t iter_start,
|
||||
const size_t iter_end)
|
||||
{
|
||||
_VecCharInternal self = _vec_char_create(nullptr, 0);
|
||||
if (_vec_char_is_valid(ptr) != SUCCESS ||
|
||||
_vec_char_is_valid(iterator) != SUCCESS)
|
||||
{
|
||||
LOGERROR(ERR_NULL);
|
||||
return self;
|
||||
}
|
||||
if (iter_end > iterator->_internal_size || iter > ptr->_internal_size)
|
||||
{
|
||||
LOGERROR(ERR_OUT_OF_BOUNDS);
|
||||
return self;
|
||||
}
|
||||
if (ptr->_internal_arr == iterator->_internal_arr)
|
||||
{
|
||||
LOGERROR(ERR_DOUBLE_BUF);
|
||||
return self;
|
||||
}
|
||||
self = *ptr;
|
||||
for (size_t i = iter_start; i < iter_end; i++)
|
||||
{
|
||||
self = _vec_char_insert(&self, iter + (i - iter_start),
|
||||
iterator->_internal_arr[i]);
|
||||
}
|
||||
return self;
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
#ifndef CHAR_H
|
||||
#define CHAR_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <utils/utils.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char* _internal_arr;
|
||||
size_t _internal_size;
|
||||
size_t _internal_capacity;
|
||||
size_t _internal_max_size;
|
||||
} _VecCharInternal;
|
||||
|
||||
size_t
|
||||
_vec_char_is_valid(const _VecCharInternal* ptr);
|
||||
|
||||
_VecCharInternal
|
||||
_vec_char_create(const char* src, const size_t size);
|
||||
|
||||
_VecCharInternal
|
||||
_vec_char_push_back(_VecCharInternal* vec, const char val);
|
||||
|
||||
_VecCharInternal
|
||||
_vec_char_pop_back(_VecCharInternal* ptr);
|
||||
|
||||
_VecCharInternal
|
||||
_vec_char_reserve(const _VecCharInternal* ptr, const size_t max_size);
|
||||
|
||||
_VecCharInternal
|
||||
_vec_char_shrink_to_fit(_VecCharInternal* ptr);
|
||||
|
||||
size_t
|
||||
_vec_char_swap(_VecCharInternal* ptr, _VecCharInternal* ptr_other);
|
||||
|
||||
char*
|
||||
_vec_char_data(const _VecCharInternal* ptr);
|
||||
|
||||
size_t
|
||||
_vec_char_capacity(const _VecCharInternal* ptr);
|
||||
|
||||
size_t
|
||||
_vec_char_max_size(const _VecCharInternal* ptr);
|
||||
|
||||
void
|
||||
_vec_char_delete(const _VecCharInternal* ptr);
|
||||
|
||||
_VecCharInternal
|
||||
_vec_char_clear(const _VecCharInternal* ptr);
|
||||
|
||||
char
|
||||
_vec_char_at(const _VecCharInternal* ptr, const size_t idx);
|
||||
|
||||
void
|
||||
_vec_char_print_vec(const _VecCharInternal* ptr);
|
||||
|
||||
size_t
|
||||
_vec_char_begin(const _VecCharInternal* ptr);
|
||||
|
||||
size_t
|
||||
_vec_char_end(const _VecCharInternal* ptr);
|
||||
|
||||
char
|
||||
_vec_char_front(const _VecCharInternal* ptr);
|
||||
|
||||
char
|
||||
_vec_char_back(const _VecCharInternal* ptr);
|
||||
|
||||
size_t
|
||||
_vec_char_empty(const _VecCharInternal* ptr);
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_erase(const _VecCharInternal* ptr, const size_t iter);
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_erase(const _VecCharInternal* ptr, const size_t iter_start,
|
||||
const size_t iter_end);
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_resize(const _VecCharInternal* ptr, const size_t size);
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_resize(const _VecCharInternal* ptr, const size_t size,
|
||||
const char val);
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_assign(const _VecCharInternal* ptr, const _VecCharInternal* iterator,
|
||||
const size_t iter_start, const size_t iter_end);
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_assign(const _VecCharInternal* ptr, const size_t amount,
|
||||
const char val);
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_insert(const _VecCharInternal* ptr, const size_t iter,
|
||||
const char val);
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_insert(const _VecCharInternal* ptr, const size_t iter,
|
||||
const size_t amount, const char val);
|
||||
|
||||
__attribute__((overloadable)) _VecCharInternal
|
||||
_vec_char_insert(const _VecCharInternal* ptr, const _VecCharInternal* iterator,
|
||||
const size_t iter, const size_t iter_start,
|
||||
const size_t iter_end);
|
||||
|
||||
#endif
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <utils.h>
|
||||
#include <utils/utils.h>
|
||||
|
||||
const char* error_messages[] = {
|
||||
[ERR_ALLOC_FAIL] = "Calloc :- Failed to allocate",
|
||||
@@ -0,0 +1,31 @@
|
||||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define CAPACITY 1024
|
||||
#define SUCCESS 0
|
||||
|
||||
#define FAILURE 1
|
||||
#define ERR_ALLOC_FAIL 20
|
||||
#define ERR_BUF_NULL 21
|
||||
#define ERR_SIZE_ZERO 22
|
||||
#define ERR_VECTOR_NULL 23
|
||||
#define ERR_MAX_PASSED 24
|
||||
#define ERR_OUT_OF_BOUNDS 25
|
||||
#define ERR_NULL 26
|
||||
#define ERR_DOUBLE_BUF 27
|
||||
|
||||
extern const char* error_messages[];
|
||||
|
||||
#define LOG(msg) printf("LOG: %s\n", msg) /**< Print log message*/
|
||||
#define LOGERROR(error_code) \
|
||||
printf("ERROR: %s\n", \
|
||||
error_messages[error_code]) /**< Print log error message*/
|
||||
|
||||
#define find_len(arr) sizeof(arr) / sizeof(arr[0])
|
||||
|
||||
static const size_t VEC_MAX = SIZE_MAX / sizeof(int);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,3 @@
|
||||
#include <libvec.h>
|
||||
|
||||
#include "char/char.h"
|
||||
+9
-18
@@ -12,31 +12,22 @@
|
||||
* String 32 Bits with suffix of 's'
|
||||
*/
|
||||
|
||||
#include <libvec.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "char.h"
|
||||
#include "utils.h"
|
||||
|
||||
int
|
||||
main()
|
||||
{
|
||||
char ar1[] = { '1', '2', '3', '4' };
|
||||
char ar2[] = { 'a', 'b', 'c', 'd' };
|
||||
// Vec8_t v1;
|
||||
Vec8_t v1 = create(ar2, find_len(ar2));
|
||||
// char ar1[] = { '1', '2', '3', '4' };
|
||||
// char ar2[] = { 'a', 'b', 'c', 'd' };
|
||||
// Vec8_t v1 = create(ar2, find_len(ar2));
|
||||
// v1 = reserve(&v1, 9);
|
||||
// print_vec(&v1);
|
||||
// v1 = insert(&v1, 1, 'x');
|
||||
// v1 = resize(&v1, 12);
|
||||
// print_vec(&v1);
|
||||
// printf("Hello\n");
|
||||
// v1 = assign(&v1, &v2, 0, 1);
|
||||
v1 = reserve(&v1, 9);
|
||||
print_vec(&v1);
|
||||
v1 = insert(&v1, 1, 'x');
|
||||
v1 = resize(&v1, 12);
|
||||
// v1 = insert(&v1, 1, 5, 'a');
|
||||
// printf("%c", at(&v1, 1));
|
||||
// printf("RESERVED SIZE: %zu", v1.max_size);
|
||||
print_vec(&v1);
|
||||
// v2 = resize(&v2, 3);
|
||||
|
||||
// print_vec(&v2);
|
||||
@@ -58,7 +49,7 @@ main()
|
||||
// v1 = push_back(&v1, 'a');
|
||||
// print_vec(&v1);
|
||||
// printf("SIZE: %zu\n", v1.size);
|
||||
delete(&v1);
|
||||
// delete(&v1);
|
||||
// delete(&v2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user