• 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 *aom_iterator_get(Iterator *iterator);
116 #define ITERATOR_GET_AS(type, iterator) *((type *)aom_iterator_get((iterator)))
117 
118 int aom_iterator_erase(Vector *vector, Iterator *iterator);
119 
120 void aom_iterator_increment(Iterator *iterator);
121 void aom_iterator_decrement(Iterator *iterator);
122 
123 void *aom_iterator_next(Iterator *iterator);
124 void *aom_iterator_previous(Iterator *iterator);
125 
126 bool aom_iterator_equals(Iterator *first, Iterator *second);
127 bool aom_iterator_is_before(Iterator *first, Iterator *second);
128 bool aom_iterator_is_after(Iterator *first, Iterator *second);
129 
130 size_t aom_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        !aom_iterator_equals(&(iterator_name), &end);                     \
136        aom_iterator_increment(&(iterator_name)))
137 
138 #endif /* VECTOR_H */
139