cu
vector.h File Reference
#include <stdlib.h>
#include <stdbool.h>

Go to the source code of this file.

Data Structures

struct  vector_t
 vector dynamically growing array More...
 

Macros

#define VECTOR_INIT(N)
 Initialize a vector_t on the stack. More...
 
#define VECTOR_DEL(V, FN)   vector_clear(&V, FN); free(V.data);
 Delete a on stack allocated vector_t. More...
 

Functions

vector_tvector_new ()
 Creates a new empty vector of the size 8. More...
 
vector_tvector_with_cap (size_t n)
 Creates a empty vector of the size n. More...
 
void vector_insert (vector_t *v, size_t index, void *data)
 Insert an element at given position, should the index be greater than the current vector size then the data is pushed to the end. The vector avoids any fragmentation. More...
 
void vector_push (vector_t *v, void *data)
 Appends a element to the vector. More...
 
size_t vector_len (const vector_t *v)
 
bool vector_is_empty (const vector_t *v)
 
void vector_pop (vector_t *v, void(*df)(void *))
 Remove the last element from the vector. More...
 
void vector_remove (vector_t *v, size_t index, void(*df)(void *))
 Delete a element from the vector. More...
 
void * vector_get (const vector_t *v, size_t index)
 Retrieve the data at given index. More...
 
void vector_foreach (const vector_t *v, void(*f)(void *))
 Apply the funtion f to every element in the vector. More...
 
void vector_del (vector_t *v)
 Delete the vector and free allocated memory. The elements of the vector will not be freed. More...
 
void vector_clear (vector_t *v, void(*df)(void *))
 Remove all elements from the vector, in case that df is NULL every entry is set to 0. More...
 
void vector_clear_del (vector_t *v, void(*df)(void *))
 Remove all elements and free the vector_t. More...
 
bool vector_contains (const vector_t *v, const void *key, int(*pred)(const void *, const void *))
 Check whether a element is in the vector. for an int created on the stack: return memcmp(a, *(void **)b, sizeof(int32_t)) More...
 
void * vector_find (const vector_t *v, const void *key, bool(*pred)(const void *, const void *))
 Find an element in given vector. More...
 
void vector_sort (vector_t *v, int(*pred)(const void *, const void *))
 Sort the vector with quicksort. More...
 

Macro Definition Documentation

#define VECTOR_DEL (   V,
  FN 
)    vector_clear(&V, FN); free(V.data);

Delete a on stack allocated vector_t.

Parameters
Vvector_t to delete
FNfunction to delete the content of the vector_t
#define VECTOR_INIT (   N)
Value:
{.data = calloc(N, sizeof(void *)), .memsize = N, \
.count = 0 }
#define calloc(NM, S)
wrapper for calloc
Definition: util.h:31

Initialize a vector_t on the stack.

Parameters
Nsize of the vector

Function Documentation

void vector_clear ( vector_t v,
void(*)(void *)  df 
)

Remove all elements from the vector, in case that df is NULL every entry is set to 0.

int a = 42;
vector_push(v, &a);
vector_clear(v, NULL);
// v= []
Parameters
vvector to clear
dfdeleting function for heap allocated data
void vector_clear_del ( vector_t v,
void(*)(void *)  df 
)

Remove all elements and free the vector_t.

int a = 42;
vector_push(v, &a);
// v = NULL
Parameters
vvector_t to delete
dffunction with which the data should be deleted
bool vector_contains ( const vector_t v,
const void *  key,
int(*)(const void *, const void *)  pred 
)

Check whether a element is in the vector. for an int created on the stack: return memcmp(a, *(void **)b, sizeof(int32_t))

Parameters
vvector to search
keyitem to find
predpredicate function
Returns
returns true on succuss and false on failure
void vector_del ( vector_t v)

Delete the vector and free allocated memory. The elements of the vector will not be freed.

Parameters
vvector to delete
void* vector_find ( const vector_t v,
const void *  key,
bool(*)(const void *, const void *)  pred 
)

Find an element in given vector.

Parameters
vvector to search
keykey property
predpredicate function
Returns
returns the item found, NULL is returned in case of an error
void vector_foreach ( const vector_t v,
void(*)(void *)  f 
)

Apply the funtion f to every element in the vector.

int a = 42;
vector_push(&v, &a);
vector_foreach(&v, addTwo);
// v = [44]
Parameters
listvector to operate on
ffunction to apply
void* vector_get ( const vector_t v,
size_t  index 
)

Retrieve the data at given index.

int a = 42;
vector_push(&v, &a);
int a = vector_get(&v, 0);
print("%d", a); // 42
Parameters
listfetch the data of this arrayList
iindex to fetch
Returns
returns the pointer to the data
void vector_insert ( vector_t v,
size_t  index,
void *  data 
)

Insert an element at given position, should the index be greater than the current vector size then the data is pushed to the end. The vector avoids any fragmentation.

for(size_t i=0; i<10, i++){
vector_insert(v, 0, &i);
}
Parameters
vvector
indexposition to insert
datadata to insert
bool vector_is_empty ( const vector_t v)

Check whether the vector is empty

Parameters
vvector
Returns
Returns true if the vector is not empty, false otherwise
size_t vector_len ( const vector_t v)

Get the size of the vector_t

Parameters
vvector
Returns
Current size of the vector
vector_t* vector_new ( )

Creates a new empty vector of the size 8.

Returns
a new vector
void vector_pop ( vector_t v,
void(*)(void *)  df 
)

Remove the last element from the vector.

int a = 42;
int b = 24;
vector_push(&v, &a);
vector_push(&v, &b);
vector_pop(&v, NULL);
// v = [42]
Parameters
vvector to remove the element from
dffunction to free the data, if NULL is passed it is ignored
void vector_push ( vector_t v,
void *  data 
)

Appends a element to the vector.

int a = 42;
int b = 24;
vector_push(&v, &a);
vector_push(&v, &b);
// v = [42, 24]
Parameters
varrayList to which the data should be appended
Datato add
void vector_remove ( vector_t v,
size_t  index,
void(*)(void *)  df 
)

Delete a element from the vector.

int a = 42;
int b = 24;
vector_push(&v, &a);
vector_remove(&v, 0, NULL);
// v = [24]
Parameters
vvector
indexof the element to remove
dfdeleting function for the given element. df is ignored if NULL
void vector_sort ( vector_t v,
int(*)(const void *, const void *)  pred 
)

Sort the vector with quicksort.

Parameters
vvector to sort
predpredicate function
vector_t* vector_with_cap ( size_t  n)

Creates a empty vector of the size n.

Parameters
nsize of the empty array
returnsa new vector
Returns
a new vector