aboutsummaryrefslogtreecommitdiff
path: root/collections/vector/vector.adoc
blob: 49208934839e7f8721e49557768527fae689d40f (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
Vector
======
Tucker Evans
v0.7, 2020-07-03

A basic vector, that hold pointers to your data structures.

NOTE: There is currently no way to distinquish between a failed retrieval
(pop, index, back, etc.) and returning a NULL value. Keep this in mind if
you plan on storing NULL values in the vector, there are plans to fix this in
the future.

Types
-----

+vec+
~~~~~
This structure holds all internal information regarding a vector.
All functions (except constructors) expect a pointer to this struct as their
first parameter.

Functions
---------

+vec* vec_new()+
~~~~~~~~~~~~~~~~
Constructs an empty vector.

Examples
^^^^^^^^
[source,c]
----
#include "vector.h"

vec *vector = vec_new();
----

`vec* vec_with_capacity(int capacity)`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Constructs an empty vector with space for +capacity+ items.

Examples
^^^^^^^^
[source,c]
----
#include "vector.h"

vec *vector = vec_with_capacity(16);
----

+int vec_size(vec *self)+
~~~~~~~~~~~~~~~~~~~~~~~~~
Returns the number of elements in vector +self+.

Examples
^^^^^^^^
[source,c]
----
#include "vector.h"

vec *vector = vec_new();
assert(vec_size(vector) == 0);
vec_push_back(vector, NULL);
assert(vec_size(vector) == 1);
----

+void vec_push(vec *self, void *item)+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Pushes +item+ into back of +self+. This may cause a resize of the internal buffer.

Examples
^^^^^^^^
[source,c]
----
#include "vector.h"

vec *vector = vec_new();
vec_push(vector, NULL);
assert(vec_size(vector) == 1);
----

+void* vec_index(vec *self, int index)+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Returns the element at position +index+ of +self+.

Examples
^^^^^^^^
[source,c]
----
#include "vector.h"
#include <string.h>

char *str1 = "ONE";
char *str2 = "TWO";
char *str3 = "THREE";

vec *vector = vec_new();
vec_push(vector, str_dup(str1));
vec_push(vector, str_dup(str2));
vec_push(vector, str_dup(str3));

assert(str_cmp(vec_index(vector, 1), str2) == 0);
----

+void* vec_pop(vec *self)+
~~~~~~~~~~~~~~~~~~~~~~~~~~
Pops an item off of the back of the vector +self+.

Examples
^^^^^^^^
[source,c]
----
#include "vector.h"
#include <string.h>

char *str1 = "ONE";
char *str2 = "TWO";

vec *vector = vec_new();
vec_push(vector, str_dup(str1));
vec_push(vector, str_dup(str2));

assert(str_cmp(vec_pop(vector), str2) == 0);
assert(str_cmp(vec_pop(vector), str1) == 0);
----

[[vec_free]]
+void vec_free(vec *self)+
~~~~~~~~~~~~~~~~~~~~~~~~~~
Frees all internal memory and +self+.

NOTE: All item pointers are still valid after a call to
<<vec_free,+vec_free()+>>, <<vec_clear,+vec_clear()+>> should be called before
if they are no longer needed to avoid memory leaks.

Examples
^^^^^^^^
[source,c]
----
#include "vector.h"

vec *vector = vec_new();
vec_free(vector);
----

[[vec_clear]]
+void vec_clear(vec *self)+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Free all elements within vector +self+, and sets vector to empty (size 0).

NOTE: Does not free internal memory of +self+ or +self+ itself, if this is desired
<<vec_free,+vec_free()+>> should be called immediatly after this.

Examples
^^^^^^^^
[source,c]
----
#include "vector.h"
#include <string.h>

char *str1 = "ONE";
char *str2 = "TWO";

vec *vector = vec_new();
vec_push_back(vector, str_dup(str1));
vec_push_back(vector, str_dup(str2));

vec_clear(vector);
assert(vec_size(vector) == 0);
vec_free(vector);
----

[[vec_print]]
+void vec_print(vec *self, (char* to_string(void*)))+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Prints out the contents of the vector +self+ to +stdout+ (surounded by square
brackets and separated by commas ','). +to_string+ is a function that takes a
pointer to the type of elements stored in +self+ and returns a string
representation.

Examples
^^^^^^^^
[source,c]
----
#include "vector.h"
#include <string.h>

char* to_string(str)
void *str;
{
	return str;
}

int main()
{
char *str1 = "ONE";
char *str2 = "TWO";
char *str3 = "THREE";

vec *vector = vec_new();
vec_push(vector, str_dup(str1));
vec_push(vector, str_dup(str2));
vec_push(vector, str_dup(str3));

printf("VEC CONTENTS:\n\t")
vec_print(vector, to_string)
}
----

Output:
----
VEC_CONTENTS:
	[ONE,TWO,THREE]
----

[[vec_cp]]
+vec* vec_cp(vec *self)+
~~~~~~~~~~~~~~~~~~~~~~~~
Returns a copy of the vector +self+. All elements are kept in the same order.

Examples
^^^^^^^^
[source,c]
----
#include "vector.h"
#include <string.h>

char *str1 = "ONE";
char *str2 = "TWO";

vec *vector = vec_with_capacity(16);
vec_push_back(vector, str_dup(str1));
vec_push_back(vector, str_dup(str2));

vec *new = vec_cp(vector);
assert(strcmp(vec_pop_back, str2) == 0);
assert(strcmp(vec_pop_back, str1) == 0);
----