Skip to content

Add features about adding/inserting multiple elements or array content#12

Open
Lasereyes5 wants to merge 1 commit intoMashpoe:masterfrom
Lasereyes5:master
Open

Add features about adding/inserting multiple elements or array content#12
Lasereyes5 wants to merge 1 commit intoMashpoe:masterfrom
Lasereyes5:master

Conversation

@Lasereyes5
Copy link
Copy Markdown

Thanks for creating this simple and useful library.
I try to use the char vector as string with this library, but it lacks some features. I think these features will also be helpful in dealing array and multiple elements, so I implement them.

I don't know if you need or want these features, so feel free to judge.

Added Symbols

These are macros and functions added:

// macros
// with typeof()
vector_add_dst_multiple(vec_addr, count)
vector_insert_dst_multiple(vec_addr, pos, count)
vector_add_multiple(vec_addr, ...)
vector_add_arr(vec_addr, arrptr, count)
vector_insert_multiple(vec_addr, pos, ...)
vector_insert_arr(vec_addr, pos, arrptr, count)
// without typeof()
vector_add_dst_multiple(vec_addr, type, count)
vector_insert_dst_multiple(vec_addr, type, pos, count)
vector_add_multiple(vec_addr, type, ...)
vector_add_arr(vec_addr, type, listptr, count)
vector_insert_multiple(vec_addr, type, pos, ...)
vector_insert_arr(vec_addr, type, pos, arrptr, count)


// functions
// public
void* _vector_add_dst_multiple(vector* vec_addr, vec_type_t type_size, vec_size_t reuire_count);
void* _vector_insert_dst_multiple(vector* vec_addr, vec_type_t type_size, vec_size_t pos, vec_size_t reuire_count);
// private
bool vector_has_multiple_space(vector_header* h,vec_size_t reuire_count);
vector_header* vector_realloc_require(vector_header* h, vec_type_t type_size, vec_size_t reuire_count);

Disadventages

The macro vector_add_multiple and vector_insert_multiple uses two C99 features called "Variadic Macros" and "Compound Literals" to accept multiple elements and create a temprory array, basicly like this:

#define vector_mkarr(type, ...) (type[]){__VA_ARGS__}
// expand to "(int[]){1,2,3}"
vector_mkarr(int, 1, 2, 3)

I haven't test it in MSVC (or C++ in MSVC) yet, so I don't know if this will causes problems.

The descriptions and names about these actions are quite long to fit the sheets in README file, may get a mess or force other lines align longer.

Adventages

Add/Insert multiple items or array content directly:

int* foo = vector_create();
int a[] = {3,4,5};
int b[] = {5,12,13,-1};

vector_add(&foo, 5);
vector_add_multiple(&foo, 5, 6, 8);
vector_add_arr(&foo, a, 3);

int insert_pos = 2;
vector_insert_multiple(&foo, insert_pos, -15, -61, -78);
vector_insert_arr(&foo, insert_pos, b, 4);

Now I can use the char vector as string more easily:

char* str_vec = vector_create();

// add string
const char s[] ="char vector as string test";
vector_add_arr(&str_vec, s, sizeof(s));
// vector_add_arr(&str_vec, s, strlen(s));
// vector_add(&str_vec, '\0');

// insert string
const char s2[] = "(inserting)"
int insert_pos = sizeof("char vector");// a trick about indexing a string
vector_insert_arr(&str_vec, insert_pos, s2, strlen(s2));

printf("string vector:\"%s\"\n", str_vec);
vector_free(str_vec);

It even supports adding/inserting structs directly, since I uses memcpy to implement this instead of for loop.
It uses like this:

// just an example
typedef struct size
{
	int x;
	int y;
} size;

size *size_vec = vector_create();
size a = {1,2}, b = {-3,7};
// parsing in the pointer of item and the count "1" to pretend it's an array.
vector_add_arr(&size_vec, &a, 1);
vector_add_multiple(&size_vec, b);
vector_add_multiple(&size_vec, (size){-65,39}, (size){12,-87});
/*
// or in regular way with index:
size *sizes = vector_add_dst_multiple(&size_vec, 2);
sizes[0].x = 1;
sizes[0].y = 2;
sizes[1].x = -3;
sizes[1].y = 7;
sizes = NULL;
*/

for (int i = 0, vecsize = vector_size(size_vec); i < vecsize; i++)
{
	printf("(%d,%d)\n", size_vec[i].x, size_vec[i].y);
}
vector_free(size_vec);

Note: I thought it would causes problems to make temporary structs in macro, but it eventually works in a tricky way.

For example, the struct arguments in vector_add_multiple(&size_vec, (size){-65,39}, (size){12,-87}); will break down into"(size){-65" "39}" "(size){12" "-87}", and join back again in __VA_ARGS__. So this won't affect any structure in arguments even with structs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant