aboutsummaryrefslogtreecommitdiff
path: root/collections/vector/vector.adoc
blob: 1dce41bc735f3393822a628e3c7e9814df4f5a64 (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
Vector
======
Tucker Evans
v0.3, 2020-07-02

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);
----