FEAT: Added assign and resize functions
API: Fixed CMAKELISTS.txt to work without the encapsulation as it will not work or be viable right now
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include "char.h"
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -10,17 +11,6 @@
|
||||
* Vector Library For C
|
||||
*/
|
||||
|
||||
/*
|
||||
* All functions needed
|
||||
* assign() Fills a vector with multiple values stored
|
||||
* insert() Inserts a number of elements into a vector
|
||||
* vector can have pop_back() Removes the last element of a vector rbegin()
|
||||
* Returns a reverse iterator pointing to the last element of a vector rend()
|
||||
* Returns a reverse iterator pointing to a position right before the first
|
||||
* element of a vector reserve() Reserves memory for a vector resize()
|
||||
* Changes the size of a vector, adding or removing elements if necessary
|
||||
* */
|
||||
|
||||
/**
|
||||
* Creates a new vector with type char
|
||||
* @param Takes in nullptr or another vector object. If an object is passed in
|
||||
@@ -286,3 +276,105 @@ pop_back(Vec8_t* vec)
|
||||
vec->size = vec->size - 1;
|
||||
return *vec;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) Vec8_t
|
||||
resize(Vec8_t* vec, size_t size)
|
||||
{
|
||||
Vec8_t nvec = create(nullptr);
|
||||
if (vec == nullptr || vec->arr == nullptr) return nvec;
|
||||
if (size <= 0 || vec->size <= 0)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"Size or Vector Size is at or below 0 and is invalid");
|
||||
return nvec;
|
||||
}
|
||||
for (size_t i = size; i < vec->size; i++)
|
||||
{
|
||||
vec->arr[i] = '\0';
|
||||
}
|
||||
return *vec;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) Vec8_t
|
||||
resize(Vec8_t* vec, size_t size, char val)
|
||||
{
|
||||
Vec8_t nvec = create(nullptr);
|
||||
if (vec == nullptr || vec->arr == nullptr) return nvec;
|
||||
if (size <= 0 || vec->size <= 0)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"Size or Vector Size is at or below 0 and is invalid");
|
||||
return nvec;
|
||||
}
|
||||
if (size == vec->size)
|
||||
{
|
||||
fprintf(stderr,
|
||||
"Size and Vector Size are equal to each other and is "
|
||||
"invalid");
|
||||
return nvec;
|
||||
}
|
||||
if (size < vec->size)
|
||||
{
|
||||
fprintf(stderr, "Size is below Vector Size is invalid");
|
||||
return nvec;
|
||||
}
|
||||
if (size >= vec->capacity)
|
||||
{
|
||||
nvec = reserve(vec, size);
|
||||
}
|
||||
size_t ret = vec->size;
|
||||
vec->size = vec->size + (size - vec->size);
|
||||
|
||||
for (size_t i = ret; i < vec->size; i++)
|
||||
{
|
||||
if (at(vec, (int)i) >= 0)
|
||||
{
|
||||
vec->arr[i] = val;
|
||||
}
|
||||
}
|
||||
return *vec;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) Vec8_t
|
||||
assign(Vec8_t* vec, const Vec8_t* iterator, size_t iter_start, size_t iter_end)
|
||||
{
|
||||
Vec8_t ret = create(nullptr);
|
||||
if (vec == nullptr || vec->arr == nullptr) return ret;
|
||||
if (iterator == nullptr || iterator->arr == nullptr) return ret;
|
||||
if (iter_end <= iter_start) return ret;
|
||||
vec = erase(vec, begin(vec), end(vec) + 1);
|
||||
size_t res = iter_end - iter_start;
|
||||
*vec = reserve(vec, res);
|
||||
for (size_t i = iter_start; i < iter_end; i++)
|
||||
{
|
||||
*vec = add_back(vec, at(iterator, (int)i));
|
||||
}
|
||||
return *vec;
|
||||
}
|
||||
|
||||
__attribute__((overloadable)) Vec8_t
|
||||
assign(Vec8_t* vec, const size_t amount, char val)
|
||||
{
|
||||
Vec8_t ret = create(nullptr);
|
||||
if (vec == nullptr || vec->arr == nullptr) return ret;
|
||||
vec = erase(vec, begin(vec), end(vec) + 1);
|
||||
*vec = reserve(vec, amount);
|
||||
for (int i = 0; i < amount; i++)
|
||||
{
|
||||
*vec = add_back(vec, val);
|
||||
}
|
||||
return *vec;
|
||||
}
|
||||
|
||||
/*
|
||||
* assign() Fills a vector with multiple values stored
|
||||
vector.assign(iterator start, iterator end);
|
||||
vector.assign(size_t amount, <type> value);
|
||||
|
||||
|
||||
* insert() Inserts a number of elements into a vector vector can have
|
||||
vector.insert(iterator position, <type> value);
|
||||
vector.insert(iterator position, size_t amount, <type> value);
|
||||
vector.insert(iterator position (vector to add to), iterator to add with
|
||||
(other vector) iterator start, iterator end);
|
||||
* */
|
||||
@@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Andrew Haynes
|
||||
* dmm.dev@icloud.com
|
||||
* No LICENSE
|
||||
* Vector Library For C
|
||||
*/
|
||||
|
||||
#ifndef CHAR_H
|
||||
#define CHAR_H
|
||||
|
||||
#define CAPACITY 1024
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char* arr;
|
||||
size_t size;
|
||||
size_t capacity;
|
||||
} Vec8_t;
|
||||
|
||||
__attribute__((overloadable)) Vec8_t
|
||||
create(const Vec8_t* input);
|
||||
|
||||
__attribute__((overloadable)) Vec8_t
|
||||
create(const _Bool init);
|
||||
|
||||
Vec8_t
|
||||
reserve(Vec8_t* ptr, size_t max_size);
|
||||
|
||||
Vec8_t
|
||||
shrink_to_fit(Vec8_t* ptr);
|
||||
|
||||
int
|
||||
swap(Vec8_t* ptr, Vec8_t* ptr_other);
|
||||
|
||||
char*
|
||||
data(const Vec8_t* ptr);
|
||||
|
||||
size_t
|
||||
max_size(const Vec8_t* ptr);
|
||||
|
||||
void delete (Vec8_t* vec);
|
||||
|
||||
Vec8_t
|
||||
clear(Vec8_t* vec);
|
||||
|
||||
char
|
||||
at(const Vec8_t* vec, int idx);
|
||||
|
||||
__attribute__((overloadable)) Vec8_t*
|
||||
erase(Vec8_t* vec, size_t iter);
|
||||
|
||||
__attribute__((overloadable)) Vec8_t*
|
||||
erase(Vec8_t* vec, size_t iter_start, size_t iter_end);
|
||||
|
||||
void
|
||||
print_vec(const Vec8_t* vec);
|
||||
|
||||
int
|
||||
begin(const Vec8_t* vec);
|
||||
|
||||
int
|
||||
end(const Vec8_t* vec);
|
||||
|
||||
char
|
||||
front(const Vec8_t* vec);
|
||||
|
||||
char
|
||||
back(const Vec8_t* vec);
|
||||
|
||||
int
|
||||
empty(const Vec8_t* vec);
|
||||
|
||||
Vec8_t
|
||||
add_back(Vec8_t* vec, char val);
|
||||
|
||||
Vec8_t
|
||||
pop_back(Vec8_t* vec);
|
||||
|
||||
#endif
|
||||
+35
-5
@@ -15,7 +15,7 @@
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "lib/char/char.h"
|
||||
#include "char.h"
|
||||
|
||||
int
|
||||
main()
|
||||
@@ -27,18 +27,48 @@ main()
|
||||
{
|
||||
vec = add_back(&vec, 'a');
|
||||
}
|
||||
print_vec(&vec);
|
||||
printf("\n");
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
other = add_back(&other, 'b');
|
||||
}
|
||||
print_vec(&other);
|
||||
|
||||
printf("Vec: ");
|
||||
print_vec(&vec);
|
||||
printf("\n");
|
||||
pop_back(&vec);
|
||||
printf("Vec: ");
|
||||
// for (int i = 0; i < vec.size; i++)
|
||||
// {
|
||||
// printf("%i ", at(&vec, i));
|
||||
// }
|
||||
print_vec(&vec);
|
||||
printf("\n");
|
||||
|
||||
// vec = resize(&vec, 12, 'a');
|
||||
|
||||
printf("Vec: ");
|
||||
vec = assign(&vec, &other, begin(&other) + 2, end(&other) + 1);
|
||||
vec = shrink_to_fit(&vec);
|
||||
print_vec(&vec);
|
||||
// for (int i = 0; i < vec.size; i++)
|
||||
// {
|
||||
// printf("%i ", at(&vec, i));
|
||||
// }
|
||||
// printf("\n");
|
||||
// print_vec(&vec);
|
||||
// pop_back(&vec);
|
||||
// printf("Other: ");
|
||||
// print_vec(&other);
|
||||
// printf("\n");
|
||||
//
|
||||
// printf("Vec: ");
|
||||
// print_vec(&vec);
|
||||
// printf("\n");
|
||||
// pop_back(&vec);
|
||||
// printf("Other: ");
|
||||
// print_vec(&other);
|
||||
// printf("\n");
|
||||
//
|
||||
// int error = swap(&vec, &other);
|
||||
// if (error)
|
||||
// {
|
||||
|
||||
Reference in New Issue
Block a user