-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvector.h
174 lines (145 loc) · 4.24 KB
/
vector.h
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* vector.h -- flexible arrays */
/**
* \file vector.h
*
* Vector is an abstracted array which can be resized by both length and
* element width.
*
* Elements are of a fixed size, stored contiguously and are addressed by
* index.
*
* \warning If the vector is altered then pointers into the vector may be
* invalidated (should the block move when reallocated).
*/
#ifndef DATASTRUCT_VECTOR_H
#define DATASTRUCT_VECTOR_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stddef.h>
#include "base/result.h"
/**
* A vector.
*/
typedef struct vector vector_t;
/* ----------------------------------------------------------------------- */
/**
* Create a new vector.
*
* \param[in] width Byte width of each element.
*
* \return New vector, or NULL if out of memory.
*/
vector_t *vector_create(size_t width);
/**
* Destroy an existing vector.
*
* \param[in] doomed Vector to destroy.
*/
void vector_destroy(vector_t *doomed);
/* ----------------------------------------------------------------------- */
/**
* Clear the specified vector.
*
* \param[in] vector Vector to change.
*/
void vector_clear(vector_t *vector);
/* ----------------------------------------------------------------------- */
/**
* Return the number of elements stored in the specified vector.
*
* \param[in] vector Vector to query.
*
* \return Number of elements stored.
*/
size_t vector_length(const vector_t *vector);
/**
* Change the number of elements stored in the specified vector.
*
* Truncates the vector if smaller than the present size.
*
* \param[in] vector Vector to change.
* \param[in] length New length.
*
* \return result_OK or result_OOM.
*/
result_t vector_set_length(vector_t *vector, unsigned int length);
/* ----------------------------------------------------------------------- */
/**
* Reserve space for at least the specified number of elements in the
* specified vector.
*
* \param[in] vector Vector to change.
* \param[in] need Required length.
*
* \return result_OK or result_OOM.
*/
result_t vector_ensure(vector_t *vector, unsigned int need);
/* ----------------------------------------------------------------------- */
/**
* Return the byte width of element stored in the specified vector.
*
* \param[in] vector Vector to query.
*
* \return Byte width of stored elements.
*/
size_t vector_width(const vector_t *vector);
/**
* Change the byte width of element stored in the specified vector.
*
* If the element width is reduced then any extra bytes are lost. If
* increased, then zeroes are inserted.
*
* \param[in] vector Vector to change.
* \param[in] width New element width.
*
* \return result_OK, result_OOM or result_BAD_ARG.
*/
result_t vector_set_width(vector_t *vector, size_t width);
/* ----------------------------------------------------------------------- */
/**
* Retrieve a pointer to an element in the vector by index.
*
* \param[in] vector Vector to query.
* \param[in] index Index of element wanted.
*
* \return Pointer to element.
*/
void *vector_get(const vector_t *vector, unsigned int index);
/* ----------------------------------------------------------------------- */
/**
* Assign an element of the vector by index.
*
* \warning Will fail silently if index is out of bounds.
*
* \param[in] vector Vector to change.
* \param[in] index Index of element to assign.
* \param[in] value Value to assign.
*/
void vector_set(vector_t *vector, unsigned int index, const void *value);
/* ----------------------------------------------------------------------- */
/**
* Insert an element at the end of the vector, allocating memory if required.
*
* \param[in] vector Vector to change.
* \param[in] value Value to insert.
*
* \return result_OK or result_OOM.
*/
result_t vector_insert(vector_t *vector, const void *value);
/**
* Insert many elements at the end of the vector, allocating memory if required.
*
* \param[in] vector Vector to change.
* \param[in] values Array of values to insert.
* \param[in] nvalues Number of values to insert.
*
* \return result_OK or result_OOM.
*/
result_t vector_insert_many(vector_t *vector, const void *values, int nvalues);
/* ----------------------------------------------------------------------- */
#ifdef __cplusplus
}
#endif
#endif /* DATASTRUCT_VECTOR_H */