From: Mike D. Lowis Date: Tue, 2 Apr 2013 20:59:24 +0000 (-0400) Subject: Added header for vector X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=eb894b9429a3e6244b403ebd29ed6c5b8322412f;p=projs%2Flibcds.git Added header for vector --- diff --git a/source/vector/vec.h b/source/vector/vec.h index e69de29..18c5d60 100644 --- a/source/vector/vec.h +++ b/source/vector/vec.h @@ -0,0 +1,160 @@ +/** + @file vec.h + @brief TODO: Describe this file + $Revision$ + $HeadURL$ +*/ +#ifndef VEC_H +#define VEC_H + +#include +#include + +/** A vector implementation */ +typedef struct { + bool own_contents; + size_t capacity; + size_t size; + void** p_buffer; +} vec_t; + +/** + * @brief Creates a new vector initialized with the given elements. + * + * @param own_contents Whether the contents will be freed when the vector is freed. + * @param num_elements The number of elements to be put into the vector. + * @param ... The list of elements (length must be equal to num_elements). + * + * @return Pointer to newly created vector. + */ +vec_t* vec_new(bool own_contents, size_t num_elements, ...); + +/** + * @brief Reclaims the memory of the provided vector. + * + * @param p_vec The vector to free. + * @param free_contents Whether the contents of the vector should also be freed. + */ +void vec_free(vec_t* p_vec); + +/** + * @brief Returns the number of items in the vector. + * + * @param p_vec Pointer to the vector. + * + * @return The number of items in the vector. + */ +size_t vec_size(vec_t* p_vec); + +/** + * @brief Returns the maximum number of items that could be contained in a vector. + * + * @return The maximum size of a vector. + */ +size_t vec_max_size(void); + +/** + * @brief Returns whether the vector is empty (size == 0) or not. + * + * @param p_vec Pointer to the vector. + * + * @return Whether the vector is empty. + */ +bool vec_empty(vec_t* p_vec); + +/** + * @brief Resizes the vector to contain the specified number of elements. + * + * @param p_vec Pointer to the vector. + * @param size The target size of the vector. + * @param data The value of any newly created elements. + */ +void vec_resize(vec_t* p_vec, size_t size, void* data); + +/** + * @brief Shrinks the vector's capacity to equal it's size. + * + * @param p_vec Pointer to the vector. + */ +void vec_shrink_to_fit(vec_t* p_vec); + +/** + * @brief Returns the size of the internal array that is currently in use. + * + * @param p_vec Pointer to the vector. + * + * @return The size of the internal array. + */ +size_t vec_capacity(vec_t* p_vec); + +/** + * @brief Sets the minimum storage capacity of the vector to the given size. + * + * @param p_vec Pointer to the vector. + * @param size The size to reserve. + */ +void vec_reserve(vec_t* p_vec, size_t size); + +/** + * @brief Returns the item at the specified index. + * + * @param p_vec Pointer to the vector. + * @param index The index of the item to retrieve. + * + * @return The retrieved item. + */ +void* vec_at(vec_t* p_vec, size_t index); + +/** + * @brief Sets the value of the element at the given index. + * + * @param p_vec Pointer to the vector. + * @param index The index of the element to set. + * @param data The new data for the indexed element. + */ +void vec_set(vec_t* p_vec, size_t index, void* data); + +/** + * @brief Inserts the provided elements at the given index. + * + * @param p_vec Pointer to the vector. + * @param index Index at which the elements should be inserted. + * @param num_elements The number of elements to insert. + * @param ... The elements to insert. + */ +void vec_insert(vec_t* p_vec, size_t index, size_t num_elements, ...); + +/** + * @brief Erases elements from the vector that fall into the given range. + * + * @param p_vec Pointer to the vector. + * @param start_idx The start of the range. + * @param end_idx The end of the range. + */ +void vec_erase(vec_t* p_vec, size_t start_idx, size_t end_idx); + +/** + * @brief Pushes the provided element on to the back of the vector. + * + * @param p_vec Pointer to the vector. + * @param data The data to push. + */ +void vec_push_back(vec_t* p_vec, void* data); + +/** + * @brief Erases and returns the last element in the vector. + * + * @param p_vec Pointer to the vector. + * + * @return The element that was removed. + */ +void* vec_pop_back(vec_t* p_vec); + +/** + * @brief Erases all elements in the vector. + * + * @param p_vec Pointer to the vector. + */ +void vec_clear(vec_t* p_vec); + +#endif /* VEC_H */