summaryrefslogtreecommitdiff
path: root/media/libaom/src/third_party/vector/vector.h
blob: 02743f5f16bc18c0b8475a65ea0a9b02945c4862 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
The MIT License(MIT)
Copyright(c) 2016 Peter Goldsborough

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files(the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions :

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef VECTOR_H
#define VECTOR_H

#include <stdbool.h>
#include <stddef.h>

/***** DEFINITIONS *****/

#define VECTOR_MINIMUM_CAPACITY 2
#define VECTOR_GROWTH_FACTOR 2
#define VECTOR_SHRINK_THRESHOLD (1 / 4)

#define VECTOR_ERROR -1
#define VECTOR_SUCCESS 0

#define VECTOR_UNINITIALIZED NULL
#define VECTOR_INITIALIZER \
  { 0, 0, 0, VECTOR_UNINITIALIZED }

/***** STRUCTURES *****/

typedef struct Vector {
  size_t size;
  size_t capacity;
  size_t element_size;

  void *data;
} Vector;

typedef struct Iterator {
  void *pointer;
  size_t element_size;
} Iterator;

/***** METHODS *****/

/* Constructor */
int aom_vector_setup(Vector *vector, size_t capacity, size_t element_size);

/* Copy Constructor */
int aom_vector_copy(Vector *destination, Vector *source);

/* Copy Assignment */
int aom_vector_copy_assign(Vector *destination, Vector *source);

/* Move Constructor */
int aom_vector_move(Vector *destination, Vector *source);

/* Move Assignment */
int aom_vector_move_assign(Vector *destination, Vector *source);

int aom_vector_swap(Vector *destination, Vector *source);

/* Destructor */
int aom_vector_destroy(Vector *vector);

/* Insertion */
int aom_vector_push_back(Vector *vector, void *element);
int aom_vector_push_front(Vector *vector, void *element);
int aom_vector_insert(Vector *vector, size_t index, void *element);
int aom_vector_assign(Vector *vector, size_t index, void *element);

/* Deletion */
int aom_vector_pop_back(Vector *vector);
int aom_vector_pop_front(Vector *vector);
int aom_vector_erase(Vector *vector, size_t index);
int aom_vector_clear(Vector *vector);

/* Lookup */
void *aom_vector_get(Vector *vector, size_t index);
const void *aom_vector_const_get(const Vector *vector, size_t index);
void *aom_vector_front(Vector *vector);
void *aom_vector_back(Vector *vector);
#define VECTOR_GET_AS(type, aom_vector_pointer, index) \
  *((type *)aom_vector_get((aom_vector_pointer), (index)))

/* Information */
bool aom_vector_is_initialized(const Vector *vector);
size_t aom_vector_byte_size(const Vector *vector);
size_t aom_vector_free_space(const Vector *vector);
bool aom_vector_is_empty(const Vector *vector);

/* Memory management */
int aom_vector_resize(Vector *vector, size_t new_size);
int aom_vector_reserve(Vector *vector, size_t minimum_capacity);
int aom_vector_shrink_to_fit(Vector *vector);

/* Iterators */
Iterator aom_vector_begin(Vector *vector);
Iterator aom_vector_end(Vector *vector);
Iterator aom_vector_iterator(Vector *vector, size_t index);

void *iterator_get(Iterator *iterator);
#define ITERATOR_GET_AS(type, iterator) *((type *)iterator_get((iterator)))

int iterator_erase(Vector *vector, Iterator *iterator);

void iterator_increment(Iterator *iterator);
void iterator_decrement(Iterator *iterator);

void *iterator_next(Iterator *iterator);
void *iterator_previous(Iterator *iterator);

bool iterator_equals(Iterator *first, Iterator *second);
bool iterator_is_before(Iterator *first, Iterator *second);
bool iterator_is_after(Iterator *first, Iterator *second);

size_t iterator_index(Vector *vector, Iterator *iterator);

#define VECTOR_FOR_EACH(aom_vector_pointer, iterator_name)           \
  for (Iterator(iterator_name) = aom_vector_begin((aom_vector_pointer)), \
      end = aom_vector_end((aom_vector_pointer));                        \
       !iterator_equals(&(iterator_name), &end);                 \
       iterator_increment(&(iterator_name)))

/***** PRIVATE *****/

#define MAX(a, b) ((a) > (b) ? (a) : (b))

bool _vector_should_grow(Vector *vector);
bool _vector_should_shrink(Vector *vector);

size_t _vector_free_bytes(const Vector *vector);
void *_vector_offset(Vector *vector, size_t index);
const void *_vector_const_offset(const Vector *vector, size_t index);

void _vector_assign(Vector *vector, size_t index, void *element);

int _vector_move_right(Vector *vector, size_t index);
void _vector_move_left(Vector *vector, size_t index);

int _vector_adjust_capacity(Vector *vector);
int _vector_reallocate(Vector *vector, size_t new_capacity);

void _vector_swap(size_t *first, size_t *second);

#endif /* VECTOR_H */