1 /* 2 The MIT License(MIT) 3 Copyright(c) 2016 Peter Goldsborough 4 5 Permission is hereby granted, free of charge, to any person obtaining a copy of 6 this software and associated documentation files (the "Software"), to deal in 7 the Software without restriction, including without limitation the rights to 8 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 the Software, and to permit persons to whom the Software is furnished to do so, 10 subject to the following conditions : 11 12 The above copyright notice and this permission notice shall be included in all 13 copies or substantial portions of the Software. 14 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR 18 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 */ 22 23 #ifndef VECTOR_H 24 #define VECTOR_H 25 26 #include <stdbool.h> 27 #include <stddef.h> 28 29 /***** DEFINITIONS *****/ 30 31 #define VECTOR_MINIMUM_CAPACITY 2 32 #define VECTOR_GROWTH_FACTOR 2 33 #define VECTOR_SHRINK_THRESHOLD (1 / 4) 34 35 #define VECTOR_ERROR -1 36 #define VECTOR_SUCCESS 0 37 38 #define VECTOR_UNINITIALIZED NULL 39 #define VECTOR_INITIALIZER \ 40 { 0, 0, 0, VECTOR_UNINITIALIZED } 41 42 /***** STRUCTURES *****/ 43 44 typedef struct Vector { 45 size_t size; 46 size_t capacity; 47 size_t element_size; 48 49 void *data; 50 } Vector; 51 52 typedef struct Iterator { 53 void *pointer; 54 size_t element_size; 55 } Iterator; 56 57 /***** METHODS *****/ 58 59 /* Constructor */ 60 int aom_vector_setup(Vector *vector, size_t capacity, size_t element_size); 61 62 #if 0 63 /* Copy Constructor */ 64 int aom_vector_copy(Vector *destination, Vector *source); 65 66 /* Copy Assignment */ 67 int aom_vector_copy_assign(Vector *destination, Vector *source); 68 69 /* Move Constructor */ 70 int aom_vector_move(Vector *destination, Vector *source); 71 72 /* Move Assignment */ 73 int aom_vector_move_assign(Vector *destination, Vector *source); 74 75 int aom_vector_swap(Vector *destination, Vector *source); 76 #endif // 0 77 78 /* Destructor */ 79 int aom_vector_destroy(Vector *vector); 80 81 /* Insertion */ 82 int aom_vector_push_back(Vector *vector, void *element); 83 #if 0 84 int aom_vector_push_front(Vector *vector, void *element); 85 int aom_vector_insert(Vector *vector, size_t index, void *element); 86 int aom_vector_assign(Vector *vector, size_t index, void *element); 87 #endif // 0 88 89 #if 0 90 /* Deletion */ 91 int aom_vector_pop_back(Vector *vector); 92 int aom_vector_pop_front(Vector *vector); 93 int aom_vector_erase(Vector *vector, size_t index); 94 int aom_vector_clear(Vector *vector); 95 96 /* Lookup */ 97 void *aom_vector_get(Vector *vector, size_t index); 98 const void *aom_vector_const_get(const Vector *vector, size_t index); 99 void *aom_vector_front(Vector *vector); 100 void *aom_vector_back(Vector *vector); 101 #define VECTOR_GET_AS(type, aom_vector_pointer, index) \ 102 *((type *)aom_vector_get((aom_vector_pointer), (index))) 103 #endif // 0 104 105 /* Information */ 106 bool aom_vector_is_initialized(const Vector *vector); 107 size_t aom_vector_byte_size(const Vector *vector); 108 #if 0 109 size_t aom_vector_free_space(const Vector *vector); 110 bool aom_vector_is_empty(const Vector *vector); 111 112 /* Memory management */ 113 int aom_vector_resize(Vector *vector, size_t new_size); 114 int aom_vector_reserve(Vector *vector, size_t minimum_capacity); 115 int aom_vector_shrink_to_fit(Vector *vector); 116 #endif // 0 117 118 /* Iterators */ 119 Iterator aom_vector_begin(Vector *vector); 120 #if 0 121 Iterator aom_vector_end(Vector *vector); 122 #endif // 0 123 Iterator aom_vector_iterator(Vector *vector, size_t index); 124 125 void *aom_iterator_get(Iterator *iterator); 126 #if 0 127 #define ITERATOR_GET_AS(type, iterator) *((type *)aom_iterator_get((iterator))) 128 129 int aom_iterator_erase(Vector *vector, Iterator *iterator); 130 #endif // 0 131 132 void aom_iterator_increment(Iterator *iterator); 133 #if 0 134 void aom_iterator_decrement(Iterator *iterator); 135 136 void *aom_iterator_next(Iterator *iterator); 137 void *aom_iterator_previous(Iterator *iterator); 138 139 bool aom_iterator_equals(Iterator *first, Iterator *second); 140 bool aom_iterator_is_before(Iterator *first, Iterator *second); 141 bool aom_iterator_is_after(Iterator *first, Iterator *second); 142 143 size_t aom_iterator_index(Vector *vector, Iterator *iterator); 144 145 #define VECTOR_FOR_EACH(aom_vector_pointer, iterator_name) \ 146 for (Iterator(iterator_name) = aom_vector_begin((aom_vector_pointer)), \ 147 end = aom_vector_end((aom_vector_pointer)); \ 148 !aom_iterator_equals(&(iterator_name), &end); \ 149 aom_iterator_increment(&(iterator_name))) 150 #endif // 0 151 152 #endif /* VECTOR_H */ 153