Containers
This library provides various containers. Each container has utility functions to manipulate the data it holds. This is an abstraction as to not have to manually manage and reallocate memory.
vector.h File Reference
#include "_bk_defines.h"
Include dependency graph for vector.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef struct internal_vector * vector
 

Functions

vector vector_init (size_t data_size)
 
size_t vector_size (vector me)
 
size_t vector_capacity (vector me)
 
bk_bool vector_is_empty (vector me)
 
bk_err vector_reserve (vector me, size_t size)
 
bk_err vector_trim (vector me)
 
void vector_copy_to_array (void *arr, vector me)
 
void * vector_get_data (vector me)
 
bk_err vector_add_all (vector me, void *arr, size_t size)
 
bk_err vector_add_first (vector me, void *data)
 
bk_err vector_add_at (vector me, size_t index, void *data)
 
bk_err vector_add_last (vector me, void *data)
 
bk_err vector_remove_first (vector me)
 
bk_err vector_remove_at (vector me, size_t index)
 
bk_err vector_remove_last (vector me)
 
bk_err vector_set_first (vector me, void *data)
 
bk_err vector_set_at (vector me, size_t index, void *data)
 
bk_err vector_set_last (vector me, void *data)
 
bk_err vector_get_first (void *data, vector me)
 
bk_err vector_get_at (void *data, vector me, size_t index)
 
bk_err vector_get_last (void *data, vector me)
 
bk_err vector_clear (vector me)
 
vector vector_destroy (vector me)
 

Typedef Documentation

◆ vector

typedef struct internal_vector* vector

The vector data structure, which is a dynamic contiguous array.

Function Documentation

◆ vector_add_all()

bk_err vector_add_all ( vector  me,
void *const  arr,
const size_t  size 
)

Copies elements from an array to the vector. The size specifies the number of elements to copy, starting from the beginning of the array. The size must be less than or equal to the size of the array.

Parameters
methe vector to add data to
arrthe array to copy data from
sizethe number of elements to copy
Returns
BK_OK if no error
-BK_ENOMEM if out of memory
-BK_ERANGE if size has reached representable limit

◆ vector_add_at()

bk_err vector_add_at ( vector  me,
const size_t  index,
void *const  data 
)

Adds an element to the location specified. The pointer to the data being passed in should point to the data type which this vector holds. For example, if this vector holds integers, the data pointer should be a pointer to an integer. Since the data is being copied, the pointer only has to be valid when this function is called.

Parameters
methe vector to add to
indexthe location in the vector to add the data to
datathe data to add to the vector
Returns
BK_OK if no error
-BK_ENOMEM if out of memory
-BK_EINVAL if invalid argument
-BK_ERANGE if size has reached representable limit

◆ vector_add_first()

bk_err vector_add_first ( vector  me,
void *const  data 
)

Adds an element to the start of the vector. The pointer to the data being passed in should point to the data type which this vector holds. For example, if this vector holds integers, the data pointer should be a pointer to an integer. Since the data is being copied, the pointer only has to be valid when this function is called.

Parameters
methe vector to add to
datathe data to add to the vector
Returns
BK_OK if no error
-BK_ENOMEM if out of memory
-BK_ERANGE if size has reached representable limit

◆ vector_add_last()

bk_err vector_add_last ( vector  me,
void *const  data 
)

Adds an element to the end of the vector.

Parameters
methe vector to add to
datathe data to add to the vector
Returns
BK_OK if no error
-BK_ENOMEM if out of memory
-BK_ERANGE if size has reached representable limit

◆ vector_capacity()

size_t vector_capacity ( vector  me)

Gets the capacity that the internal storage of the vector is using.

Parameters
methe vector to check
Returns
the capacity that the internal storage of the vector is using

◆ vector_clear()

bk_err vector_clear ( vector  me)

Clears the elements from the vector.

Parameters
methe vector to clear
Returns
BK_OK if no error
-BK_ENOMEM if out of memory

◆ vector_copy_to_array()

void vector_copy_to_array ( void *const  arr,
vector  me 
)

Copies the vector to an array. Since it is a copy, the array may be modified without causing side effects to the vector data structure. Memory is not allocated, thus the array being used for the copy must be allocated before this function is called. The size of the vector should be queried prior to calling this function, which also serves as the size of the newly-copied array.

Parameters
arrthe initialized array to copy the vector to
methe vector to copy to the array

◆ vector_destroy()

vector vector_destroy ( vector  me)

Frees the vector memory. Performing further operations after calling this function results in undefined behavior. Freeing NULL is legal, and causes no operation to be performed.

Parameters
methe vector to free from memory
Returns
NULL

◆ vector_get_at()

bk_err vector_get_at ( void *const  data,
vector  me,
const size_t  index 
)

Copies the element at index of the vector to data. The pointer to the data being obtained should point to the data type which this vector holds. For example, if this vector holds integers, the data pointer should be a pointer to an integer. Since this data is being copied from the array to the data pointer, the pointer only has to be valid when this function is called.

Parameters
datathe data to copy to
methe vector to copy from
indexthe index to copy from in the vector
Returns
BK_OK if no error
-BK_EINVAL if invalid argument

◆ vector_get_data()

void* vector_get_data ( vector  me)

Gets the storage element of the vector structure. The storage element is contiguous in memory. The data pointer should be assigned to the correct array type. For example, if the vector holds integers, the data pointer should be assigned to an integer array. The size of the vector should be obtained prior to calling this function, which also serves as the size of the queried array. This pointer is not a copy, thus any modification to the data will cause the vector structure data to be modified. Operations using the vector functions may invalidate this pointer. The vector owns this memory, thus it should not be freed. This should not be used if the vector is empty.

Parameters
methe vector to get the storage element from
Returns
the storage element of the vector

◆ vector_get_first()

bk_err vector_get_first ( void *const  data,
vector  me 
)

Copies the first element of the vector to data. The pointer to the data being obtained should point to the data type which this vector holds. For example, if this vector holds integers, the data pointer should be a pointer to an integer. Since this data is being copied from the array to the data pointer, the pointer only has to be valid when this function is called.

Parameters
datathe data to copy to
methe vector to copy from
Returns
BK_OK if no error
-BK_EINVAL if invalid argument

◆ vector_get_last()

bk_err vector_get_last ( void *const  data,
vector  me 
)

Copies the last element of the vector to data. The pointer to the data being obtained should point to the data type which this vector holds. For example, if this vector holds integers, the data pointer should be a pointer to an integer. Since this data is being copied from the array to the data pointer, the pointer only has to be valid when this function is called.

Parameters
datathe data to copy to
methe vector to copy from
Returns
BK_OK if no error
-BK_EINVAL if invalid argument

◆ vector_init()

vector vector_init ( const size_t  data_size)

Initializes a vector.

Parameters
data_sizethe size of each element in the vector; must be positive
Returns
the newly-initialized vector, or NULL if it was not successfully initialized due to either invalid input arguments or memory allocation error

◆ vector_is_empty()

bk_bool vector_is_empty ( vector  me)

Determines whether or not the vector is empty.

Parameters
methe vector to check
Returns
BK_TRUE if the vector is empty, otherwise BK_FALSE

◆ vector_remove_at()

bk_err vector_remove_at ( vector  me,
const size_t  index 
)

Removes element based on its index.

Parameters
methe vector to remove from
indexthe location in the vector to remove the data from
Returns
BK_OK if no error
-BK_EINVAL if invalid argument

◆ vector_remove_first()

bk_err vector_remove_first ( vector  me)

Removes the first element from the vector.

Parameters
methe vector to remove from
Returns
BK_OK if no error
-BK_EINVAL if invalid argument

◆ vector_remove_last()

bk_err vector_remove_last ( vector  me)

Removes the last element from the vector.

Parameters
methe vector to remove from
Returns
BK_OK if no error
-BK_EINVAL if invalid argument

◆ vector_reserve()

bk_err vector_reserve ( vector  me,
size_t  size 
)

Reserves space specified. If more space than specified is already reserved, then the previous space will be kept.

Parameters
methe vector to reserve space for
sizethe space to reserve
Returns
BK_OK if no error
-BK_ENOMEM if out of memory
-BK_ERANGE if space to reserve exceeds representable limit

◆ vector_set_at()

bk_err vector_set_at ( vector  me,
const size_t  index,
void *const  data 
)

Sets the data for a specified element in the vector. The pointer to the data being passed in should point to the data type which this vector holds. For example, if this vector holds integers, the data pointer should be a pointer to an integer. Since the data is being copied, the pointer only has to be valid when this function is called.

Parameters
methe vector to set data for
indexthe location to set data at in the vector
datathe data to set at the location in the vector
Returns
BK_OK if no error
-BK_EINVAL if invalid argument

◆ vector_set_first()

bk_err vector_set_first ( vector  me,
void *const  data 
)

Sets the data for the first element in the vector. The pointer to the data being passed in should point to the data type which this vector holds. For example, if this vector holds integers, the data pointer should be a pointer to an integer. Since the data is being copied, the pointer only has to be valid when this function is called.

Parameters
methe vector to set data for
datathe data to set at the start of the vector
Returns
BK_OK if no error
-BK_EINVAL if invalid argument

◆ vector_set_last()

bk_err vector_set_last ( vector  me,
void *const  data 
)

Sets the data for the last element in the vector. The pointer to the data being passed in should point to the data type which this vector holds. For example, if this vector holds integers, the data pointer should be a pointer to an integer. Since the data is being copied, the pointer only has to be valid when this function is called.

Parameters
methe vector to set data for
datathe data to set at the end of the vector
Returns
BK_OK if no error
-BK_EINVAL if invalid argument

◆ vector_size()

size_t vector_size ( vector  me)

Gets the size being used by the vector.

Parameters
methe vector to check
Returns
the size being used by the vector

◆ vector_trim()

bk_err vector_trim ( vector  me)

Sets the size of the vector buffer to the current size being used.

Parameters
methe vector to trim
Returns
BK_OK if no error
-BK_ENOMEM if out of memory