/* * C_lib Copyright (C) 2021 Wael Karram. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 3 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /* This header defines a double-linked-list and related functions. */ /* Use the double nodes. */ #include /* Used in allocations and deallocations. */ #include #include /* Include assert.h to allow code generation, but only run with debug flag. */ #ifdef DEBUG #ifndef NDEBUG #define NDEBUG #endif /* NDEBUG. */ #endif /* DEBUG. */ #include /* Include the void swapping. */ #include "../utils/swap_void.c" #ifndef VECTOR_H #define VECTOR_H /* Constants. */ /* Growth constant, for the power. */ #define GROWTH_CONSTANT_VECTOR_C 2 /* Inital power. */ #define INITIAL_POWER_VECTOR_C 3 /* Function errror/return code on success. */ #define SUCCESS_CODE_VECTOR_C 1 /* Function errror/return code on failure. */ #define FAILURE_CODE_VECTOR_C 0 /* Function errror/return code on failure to find index. */ #define INDEX_NOT_FOUND_CODE_VECTOR_C -1 /* Function errror/return code on invalid input. */ #define INVALID_INPUT_CODE_VECTOR_C -2 /* Function errror/return code on invalid input. */ #define INVALID_FREE_FUNCTION_POINTER -3 /* Error code returned when input fails sanity check. */ #define CODE_FREE_SUCCESS_MALFORMED_VECTOR_C -1 /* Check code for growing the vector. */ #define CODE_GROW_VECTOR_C 1 /* Check code for keeping the vector as is. */ #define CODE_KEEP_VECTOR_C 0 /* Check code for shrinking the vector. */ #define CODE_SHRINK_VECTOR_C -1 /* Error code for when write succeeds and vector grow fails. */ #define WRITE_SUCCESS_VECTOR_GROW_FAIL_VECTOR_C 3 /* Error code for when delete succeeds and vector shrink fails. */ #define DELETE_SUCCESS_VECTOR_SHRINK_FAIL_VECTOR_C 4 /* Error code to indicate allocation failure. */ #define ALLOC_FAILURE_VECTOR_C 5 /* Used to calculate the whole when checking size-changes. */ #define THRESHOLD_WHOLE_VECTOR_C 1.0 /* The threshold used to grow the vector. */ #define THRESHOLD_GROW_VECTOR_C 0.75 /* The threshold used to shrink the vector. */ #define THRESHOLD_SHRINK_VECTOR_C 0.25 /* Define the initial index for the last element. */ #define LAST_ELEMENT_EMPTY_INDEX_VECTOR_C 0 /* Boolean Logic. */ #define TRUE 1 #define FALSE 0 /* Useful typedef for void pointers to make it simpler to deal with. */ typedef void* void_ptr; /* The vector's struct. */ typedef struct vector { /* Stores the current power used to compute the length. */ int power; /* Stores the vector's length. */ size_t length; /* Stores the size of a single element. */ size_t element_size; /* Stores the index of the last used element (inclusive!). */ size_t last_element; /* Stores whethere or not the vector is empty. */ char empty; /* Stores a pointer to the array. */ void_ptr *array; } vector_t; /* Function declarations. */ /* This function initializes a new empty vector. */ static inline vector_t* initialize_vector(); /* This function appends to the vector. */ static inline int append_to_vector(vector_t* vector, void* const data); /* This function prepends to the vector. */ static inline int prepend_to_vector(vector_t* const vector, void* const data); /* This function reads from the vector at the given index. */ static inline void* read_from_vector(const vector_t* const vector, const size_t index); /* This function overwrites the value at the given index. */ static inline int write_to_vector(vector_t* const vector, const size_t index, void* const data); /* This function frees the data at the given index (using the given free function, if NULL doesn't change it!) and marks the cell there as NULL. */ static inline void delete_from_vector(vector_t* const vector, const size_t index, void (*free_function)(const void*)); /* This function serializes the vector into an array! */ static inline void_ptr* serialize_vector(const vector_t* const vector); /* This function copies a vector. */ static inline int copy_vector(vector_t *restrict source, vector_t **restrict destination); /* This function frees a vector. */ static inline void free_vector(vector_t *vector, void (*free_function)(const void*)); /* Internal functions. */ /* This function checks if a vector needs to be grown. */ static inline int check_vector_grow(const vector_t* const vector, const double threshold); /* This function checks if a vector needs to be shrunk. */ static inline int check_vector_shrink(const vector_t* const vector, const double threshold); /* This function grows the vector, doesn't change the vector on failure. */ static inline int grow_vector(vector_t* const vector); /* This function shrinks the vector, doesn't change the vector on failure. */ static inline int shrink_vector(vector_t* const vector); /* This function compacts a vector by pushing all the NULLs to the end. */ static inline void compact_vector(vector_t* const vector); /* This function creates a "gap" at the start of the vector. */ static inline void push_vector(const vector_t* const vector, const size_t gap_length); #endif /* VECTOR_H */