aboutsummaryrefslogtreecommitdiff
path: root/collections/map/map.adoc
blob: 740df3c5bb4230afca49c6f860c11397cf357189 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
Map
===
Tucker Evans
v0.6, 2020-07-06

A basic map implemented in an AVL tree.

NOTE: Keys are passed as void pointers and their data is not copied (this
should be handled by the user), they should not be freed without clearing the
map.

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
----
The following types are defined in the header file:
[[map]]
+map+
~~~~~
This structure holds all internal information regarding a map.
All functions (except constructors) expect a pointer to this struct as their
first parameter.

[[cmp_func]]
+cmp_func+
~~~~~~~~~~~
This is a pointer to a function that to compare keys from pointers. This
typedef is provided to cast comparison functions as a map expects the
comparison function to take void* as its parameters.

Functions
---------
[[map_new]]
+map_new(cmp_func cmp)+
~~~~~~~~~~~~~~~~~~~~~~~
Constructs an empty map.
+cmp+ should be a function that takes two pointers (+lhs+, +rhs+)to your value
type and returns (int) a negative value if +lhs+ is less than  +rhs+, zero if
+lhs+ is equal to +rhs+, and positive value if +lhs+ is greater than +rhs+.

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

map *dict = map_new((cmp_func) strcmp);
----

[[map_size]]
+map_size(map *self)+
~~~~~~~~~~~~~~~~~~~~~
Returns the number of key, value pairs stored in map +self+.

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

map *dict = map_new((cmp_func) strcmp);

assert(map_size(dict) == 0);
map_insert(dict, "ONE", NULL);
assert(map_size(dict) == 1);
----

[[map_insert]]
+int map_insert(map *self, void *key, void *value)+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Inserts a new item +value+ for +key+ into +self+
This returns an int indicating a successful insertion; providing a NULL +self+
or a key that is already in +self+ will return -1 otherwise 0 is returned.

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

map *dict = map_new((cmp_func) strcmp);

if (map_insert(dict, "ONE", NULL) < 0)
	printf("Failed to insert {\"ONE\": NULL}\n");
else
	assert(map_size(dict) == 1);
----
[[map_remove]]
+void* map_remove(map *self, void *key)+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Removes (and returns) the element at position +key+. The key and value are not
freed.

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

map *dict = map_new((cmp_func) strcmp);

map_insert(dict, "ONE", strdup("VALUE"));
map_insert(dict, "TWO", strdup("VALUE"));
map_insert(dict, "THREE", strdup("VALUE"));
map_insert(dict, "FOUR", strdup("VALUE"));
map_insert(dict, "FIVE", strdup("VALUE"));
map_insert(dict, "SIX", strdup("VALUE"));

char* tmp = map_remove(dict, "FOUR");
assert(strcmp(tmp, "FOUR") == 0);
free(tmp);
----

[[map_swap]]
+void map_swap(map *self, void *i, void *j)+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Swaps values at keys +i+ and +j+, does nothing if +i+ or +j+ are invalid keys.

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

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

map *dict = map_new((cmp_func) strcmp);

map_insert(dict, str1, strdup(str2));
map_insert(dict, str2, strdup(str3));

map_swap(dict, str1, str2);

assert(strcmp(str1, map_index(dict, str1)) == 0);
assert(strcmp(str2, map_index(dict, str2)) == 0);
----

[[map_set_val]]
+void* map_set_val(map *self, void *key, void *val)+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This replaces the value stored at +key+ (or equivalent) with +val+, the value
that is overwritten is return so the user can free it.

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

map *dict = map_new((cmp_func) strcmp);

map_insert(dict, strdup("ONE"), strdup("VALUE"));
free(map_set_val(dict, "ONE", "NEW_VALUE"));
----

[[map_check_key_ptr]]
+int map_check_key_ptr(map *self, void *key)+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This is used to check the key pointer for equivalent keys.
It is provided to ease memory management, in combination with
<<map_set_key,+map_set_key()+>>.

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

char *eq_key2

map *dict = map_new((cmp_func) strcmp);

map_insert(dict, strdup("ONE"), NULL);

eq_key2 = strdup("ONE");

/*Want to free key1 for some reason*/
if (!map_check_key_ptr(dict, eq_key2)) {
	free(map_set_key(dict, eq_key2));
}
----

[[map_set_key]]
+void* map_set_key(map *self, void *key)+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This replaces an equivalent key with the one passed in, the key that is
overwritten is return so the user can free it.

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

map *dict = map_new((cmp_func) strcmp);

map_insert(dict, strdup("ONE"), NULL);

eq_key2 = strdup("ONE");

/*Want to free key1 for some reason*/
if (!map_check_key_ptr(dict, eq_key2)) {
	free(map_set_key(dict, eq_key2));
}
----

[[map_index]]
+void* map_index(map *self, void *key)+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Returns the value associated with +key+ (or equivalent).

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

map *dict = map_new((cmp_func) strcmp, sizeof(char*));

map_insert(dict, strdup("ONE"), "VALUE");
assert(strcmp(map_index(dict, "ONE"), "VALUE") == 0);
----

[[map_clear]]
+void map_clear(map *self)+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Free all elements within dict +self+, and sets dict to empty (size 0).

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

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

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

map *dict = map_new();
map_insert(dict, str_dup(str1), NULL);
map_insert(dict, str_dup(str2), NULL);

map_clear(dict);
assert(map_size(dict) == 0);
map_free(dict);
----

[[map_free]]
+void map_free(map *self)+
~~~~~~~~~~~~~~~~~~~~~~~~~~
Frees all internal memory and +self+.

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

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

map *dict = map_new();
map_free(dict);
----