• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 /* Copy Constructor */
63 int aom_vector_copy(Vector *destination, Vector *source);
64 
65 /* Copy Assignment */
66 int aom_vector_copy_assign(Vector *destination, Vector *source);
67 
68 /* Move Constructor */
69 int aom_vector_move(Vector *destination, Vector *source);
70 
71 /* Move Assignment */
72 int aom_vector_move_assign(Vector *destination, Vector *source);
73 
74 int aom_vector_swap(Vector *destination, Vector *source);
75 
76 /* Destructor */
77 int aom_vector_destroy(Vector *vector);
78 
79 /* Insertion */
80 int aom_vector_push_back(Vector *vector, void *element);
81 int aom_vector_push_front(Vector *vector, void *element);
82 int aom_vector_insert(Vector *vector, size_t index, void *element);
83 int aom_vector_assign(Vector *vector, size_t index, void *element);
84 
85 /* Deletion */
86 int aom_vector_pop_back(Vector *vector);
87 int aom_vector_pop_front(Vector *vector);
88 int aom_vector_erase(Vector *vector, size_t index);
89 int aom_vector_clear(Vector *vector);
90 
91 /* Lookup */
92 void *aom_vector_get(Vector *vector, size_t index);
93 const void *aom_vector_const_get(const Vector *vector, size_t index);
94 void *aom_vector_front(Vector *vector);
95 void *aom_vector_back(Vector *vector);
96 #define VECTOR_GET_AS(type, aom_vector_pointer, index) \
97   *((type *)aom_vector_get((aom_vector_pointer), (index)))
98 
99 /* Information */
100 bool aom_vector_is_initialized(const Vector *vector);
101 size_t aom_vector_byte_size(const Vector *vector);
102 size_t aom_vector_free_space(const Vector *vector);
103 bool aom_vector_is_empty(const Vector *vector);
104 
105 /* Memory management */
106 int aom_vector_resize(Vector *vector, size_t new_size);
107 int aom_vector_reserve(Vector *vector, size_t minimum_capacity);
108 int aom_vector_shrink_to_fit(Vector *vector);
109 
110 /* Iterators */
111 Iterator aom_vector_begin(Vector *vector);
112 Iterator aom_vector_end(Vector *vector);
113 Iterator aom_vector_iterator(Vector *vector, size_t index);
114 
115 void *iterator_get(Iterator *iterator);
116 #define ITERATOR_GET_AS(type, iterator) *((type *)iterator_get((iterator)))
117 
118 int iterator_erase(Vector *vector, Iterator *iterator);
119 
120 void iterator_increment(Iterator *iterator);
121 void iterator_decrement(Iterator *iterator);
122 
123 void *iterator_next(Iterator *iterator);
124 void *iterator_previous(Iterator *iterator);
125 
126 bool iterator_equals(Iterator *first, Iterator *second);
127 bool iterator_is_before(Iterator *first, Iterator *second);
128 bool iterator_is_after(Iterator *first, Iterator *second);
129 
130 size_t iterator_index(Vector *vector, Iterator *iterator);
131 
132 #define VECTOR_FOR_EACH(aom_vector_pointer, iterator_name)           \
133   for (Iterator(iterator_name) = aom_vector_begin((aom_vector_pointer)), \
134       end = aom_vector_end((aom_vector_pointer));                        \
135        !iterator_equals(&(iterator_name), &end);                 \
136        iterator_increment(&(iterator_name)))
137 
138 /***** PRIVATE *****/
139 
140 #define MAX(a, b) ((a) > (b) ? (a) : (b))
141 
142 bool _vector_should_grow(Vector *vector);
143 bool _vector_should_shrink(Vector *vector);
144 
145 size_t _vector_free_bytes(const Vector *vector);
146 void *_vector_offset(Vector *vector, size_t index);
147 const void *_vector_const_offset(const Vector *vector, size_t index);
148 
149 void _vector_assign(Vector *vector, size_t index, void *element);
150 
151 int _vector_move_right(Vector *vector, size_t index);
152 void _vector_move_left(Vector *vector, size_t index);
153 
154 int _vector_adjust_capacity(Vector *vector);
155 int _vector_reallocate(Vector *vector, size_t new_capacity);
156 
157 void _vector_swap(size_t *first, size_t *second);
158 
159 #endif /* VECTOR_H */
160