197 lines
4.0 KiB
C
197 lines
4.0 KiB
C
/*
|
|
* Andrew Haynes
|
|
* dmm.dev@icloud.com
|
|
* No LICENSE
|
|
* Vector Library For C
|
|
*/
|
|
/*
|
|
* Types Wanted:
|
|
* Char 8 Bits
|
|
* Float 32 Bits with suffix of 'f'
|
|
* Int 32 bits
|
|
* String 32 Bits with suffix of 's'
|
|
*/
|
|
|
|
#include <libvec.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "lib/utils/utils.h"
|
|
|
|
void
|
|
print_bits(unsigned int num);
|
|
|
|
typedef struct
|
|
{
|
|
char* _internal_arr;
|
|
size_t _internal_size;
|
|
size_t _internal_capacity;
|
|
size_t _internal_max_size;
|
|
} _VecCharInternal;
|
|
|
|
/* NOTE
|
|
* Value is the return value for functions like _vec_char_capacity
|
|
* Err is the unsigned bit range for all the errors tracked
|
|
* origin is a string for where the bug started and IS CHANGED ONLY WHEN NULL
|
|
* */
|
|
|
|
typedef struct
|
|
{
|
|
unsigned int err;
|
|
size_t value;
|
|
char* origin; // Where the main bug started
|
|
} _err_t;
|
|
|
|
void
|
|
_vec_update_origin(_err_t* err, char* str)
|
|
{
|
|
_err_t res;
|
|
if (err == nullptr)
|
|
{
|
|
res.err = 0;
|
|
res.value = 0;
|
|
}
|
|
if (!err->origin) err->origin = str;
|
|
}
|
|
|
|
unsigned int
|
|
_vec_char_raise_err(const _VecCharInternal* ptr, unsigned int mask)
|
|
|
|
{
|
|
unsigned int res = SUCCESS;
|
|
res |= mask;
|
|
if (ptr == nullptr) res |= ERR_VEC_NULL;
|
|
if (ptr->_internal_arr == nullptr) res |= ERR_BUF_NULL;
|
|
|
|
return res;
|
|
}
|
|
|
|
_VecCharInternal
|
|
_vec_char_create(const char* src, const size_t size)
|
|
{
|
|
unsigned int err = 0;
|
|
_VecCharInternal self = {
|
|
._internal_arr = NULL,
|
|
._internal_size = 0,
|
|
._internal_capacity = CAPACITY,
|
|
._internal_max_size = VEC_MAX,
|
|
};
|
|
if (size >= self._internal_max_size)
|
|
{
|
|
LOG("If you need more memory to start, create a blank and "
|
|
"reserve.");
|
|
return self;
|
|
}
|
|
if (self._internal_capacity < size)
|
|
{
|
|
size_t dst_cap = self._internal_capacity;
|
|
while (dst_cap < size && dst_cap * 2 < self._internal_max_size)
|
|
dst_cap *= 2;
|
|
self._internal_capacity = (dst_cap < size) ? size : dst_cap;
|
|
}
|
|
if (size > 0)
|
|
{
|
|
self._internal_arr =
|
|
calloc(self._internal_capacity, sizeof(char));
|
|
err = _vec_char_raise_err(&self, err);
|
|
if ((err & SUCCESS) == 1)
|
|
{
|
|
self._internal_arr = NULL;
|
|
vec_print_errs(err);
|
|
free(self._internal_arr);
|
|
return self;
|
|
}
|
|
}
|
|
if (!src) err |= ERR_BUF_NULL;
|
|
else
|
|
{
|
|
for (size_t i = 0; i < size; i++)
|
|
{
|
|
self._internal_arr[i] = src[i];
|
|
self._internal_size++;
|
|
}
|
|
}
|
|
vec_print_errs(err);
|
|
return self;
|
|
}
|
|
|
|
void
|
|
print_bits(unsigned int num)
|
|
{
|
|
// Determine the number of bits in an unsigned int (usually 32)
|
|
int total_bits = sizeof(num) * 8;
|
|
|
|
// Loop from the most significant bit (MSB) down to the least
|
|
// significant bit (LSB)
|
|
for (int i = total_bits - 1; i >= 0; i--)
|
|
{
|
|
// Shift a 1 to the i-th position and check if that bit is set
|
|
int bit = (num >> i) & 1;
|
|
printf("%d", bit);
|
|
|
|
// Optional: Add a space every 4 or 8 bits for readability
|
|
if (i % 8 == 0)
|
|
{
|
|
printf(" ");
|
|
}
|
|
}
|
|
printf("\n");
|
|
}
|
|
|
|
int
|
|
main()
|
|
{
|
|
char res[4] = { 'a', 'b', 'c', 'd' };
|
|
_VecCharInternal v1 = _vec_char_create(nullptr, 3);
|
|
unsigned int mark = 1 << 0;
|
|
_err_t err = { 0, 0, "Te" };
|
|
_vec_update_origin(&err, "Test");
|
|
printf("%s", err.origin);
|
|
// for (int i = 0; i < VEC_FAULT_BITS; i++)
|
|
// {
|
|
// print_bits(mark);
|
|
// mark <<= 1;
|
|
// }
|
|
// const char* str = vec_get_error_desc(ERR_ALLOC_FAIL);
|
|
// const unsigned int error = ERR_BUF_NULL;
|
|
// if (!(ERR_ALLOC_FAIL & SUCCESS))
|
|
// {
|
|
// printf("Hello\n");
|
|
// }
|
|
// printf("%s", str);
|
|
// 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);
|
|
// v2 = resize(&v2, 3);
|
|
|
|
// print_vec(&v2);
|
|
|
|
// if (swap(&v1, &v2))
|
|
// {
|
|
// print_vec(&v1);
|
|
// print_vec(&v2);
|
|
// }
|
|
// Vec8_t v1 = create(nullptr, 0);
|
|
// v1 = reserve(&v1, 10);
|
|
// v1 = pop_back(&v1);
|
|
// for (int i = 0; i < 10; i++)
|
|
// {
|
|
// v1 = push_back(&v1, 'a');
|
|
// }
|
|
// print_vec(&v1);
|
|
// printf("SIZE: %zu\n", v1.size);
|
|
// v1 = push_back(&v1, 'a');
|
|
// print_vec(&v1);
|
|
// printf("SIZE: %zu\n", v1.size);
|
|
// delete(&v1);
|
|
// delete(&v2);
|
|
return 0;
|
|
}
|