Skip to content

fluffy-willa-42-team/vector-lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Vector-lib

norminette

This a simple vector library where the alloc are done for you. There is two mode of use :

  • Use the vector as a string (array of char) wher you will be able to use vec_fill and all it's mode.
  • Use the vector as a array of anything. You will only be able to use vec_add and is more limited but can be used for anything.

Nomenclature

In this doc and in the code we use certain terms often. THerefore, here is explonation.

  • length : The length is the number of elements within the array.
  • size : The size is the size in Bytes of a single element in the array.
  • rate (code only) : The rate is the speed at which the vector will expand.

Table of content


List of commands


Initialization

First of all, you have to include the header file.

#include "vector_lib.h"

Right after that, you have to create your vector. For that you will be able to use vec_init.

t_vec my_vector = vec_init(sizeof(char));

The only argument of vec_init is the size of the element.


Termination

And at the and of the code but early in here (so you dont forget it), to free the memory of the vector you can use vec_delete.

vec_delete(my_vector_ptr);

The only argument of vec_delete is a pointer to the vector you want to destroy.

But if you want to use your vector again you can just reset it with vec_delete_content.

vec_delete_content(my_vector_ptr);

The only argument of vec_delete_content is a pointer to the vector you want to reset.


Adding content

To add content, you have two mode of use as previously cited in the intro.

Array of any elements

For an array where the element could be anything, you have to use vec_add. As an example, here the vector has integers in it.

int my_number = 10;

vec_add(my_vector_ptr, &my_number);

The only argument of vec_add is a pointer to the content you want to add.

Array of characters

For an array of characters, you can use vec_fill.
To show the result of all the example, i will use the result of vec_print.

Vec_fill has many options, here is a list :


DEFAULT

DEFAULT is the default way to use vec_fill and works like this.

t_vec *vec_fill(t_vec *vec, DEFAULT, char *str);

vec_fill(my_vector_ptr, DEFAULT, "Hello World");
>> Hello World.....................

That simply added the argument given into the array. But if make a secund call...

>> Hello WorldHello World..........

It simply puts it glued to the result of the previous call.


SEP

SEP is an option of vec_fill and it will separate the result different calls.

t_vec *vec_fill(t_vec *vec, SEP, char *separator, char *str);

vec_fill(my_vector_ptr, SEP, "___", "Hello World");
>> Hello World___Hello World.......

But you can also put NULL instead of a string for the separator and the result will be a \0 between the result of the two calls.

vec_fill(my_vector_ptr, SEP, NULL, "Hello World");
>> Hello World.Hello World.......

MULTI

MULTI will allow you to add multiple strings at the same time.

t_vec *vec_fill(t_vec *vec, MULTI, int nb, char *str1, char *str2);

vec_fill(my_vector_ptr, MULTI, 2, "Hello", "World");
>> HelloWorld......................

MULTI_SEP

MULTI_SEP will allow you to add multiple strings at the same time and have a separator between the different strings.

t_vec *vec_fill(t_vec *vec, MULTI_SEP, char *separator, int nb, char *str1, char *str2);

vec_fill(my_vector_ptr, MULTI_SEP, "___", 2, "Hello", "World");
>> Hello___World...................

But you can also put NULL instead of a string for the separator and the result will be a \0 between the result of the two calls.

vec_fill(my_vector_ptr, MULTI_SEP, NULL, 2, "Hello", "World");
>> Hello.World...................

MULTI_ALL_SEP

MULTI_SEP is the combination of SEP and MULTI_SEP. It will allow you to put multiple string and to separate within them and between calls.

t_vec *vec_fill(t_vec *vec, MULTI_ALL_SEP, char *sep, char *multi_sep, int nb, char *str1, char *str2);

vec_fill(my_vector_ptr, MULTI_ALL_SEP, "___", "---", 2, "Hello", "World");
^ * 2
>> Hello---World___Hello---World...

FIXED_LEN

FIXED_LEN will allow to limit what the function will copy (for example if your string is not null terminated).

t_vec *vec_fill(t_vec *vec, FIXED_LEN, char *str, int len);

vec_fill(my_vector_ptr, FIXED_LEN, "Hello World", 5);
>> Hello...........................

Fusioning multiple options

You saw all the default option but you can also personalize what the function will do using bit or operator (|).

t_vec *vec_fill(t_vec *vec, SEP | FIXED_LEN, ...);
t_vec *vec_fill(t_vec *vec, MULTIPLE | FIXED_LEN, ...);
...

Get an element

To get an element you can use vec_get.

vec_get(my_vector_ptr, index);

You need to give the index in the array you want to get and will receive it as void *. You just need to cast it to the element you want to receive and get the first elem and you got it. As an advise, you should create a function to automize the process for simpler use.

int get_my_int_from_vector(t_vec *my_vector_ptr, int index)
{
    return (*((int *) vec_get(my_vector_ptr, index)));
}

Print the vector

If you are using the vector to store characters, you can use vec_print.

vec_print(my_vector_ptr);

vec_print will print in the std all printable character and will print all the non-printable as a '.'.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published