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
|
/*
Copyright (c) 2013-2014 Gustav Hartvigsson
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.
*/
#pragma once
#include "Func.h"
#include "defs.h"
#include "utils.h"
#include "hash.h"
#include "DynamicArray.h"
S_BEGIN_DECLS
#define S_MAP_DEFAULT_NUMBER_OF_BUCKETS 257
/**
* @file
* @defgroup SMap SMap
* @addtogroup SMap
* @{
* An SMap is a data structure that holds many mappings of objects to objects:
* say, a string to an other string. This can be likened to the Dict structure
* in python, but not fully.
*
* An SMap is made up of SMapItems, each MapItem holds two pointers to data.
* The first pointer is the key, the secold is the value.
*
* please note that SMaps can be slow and are unordered.
*/
/** @brief
* SMapItem holds the mapping of a key to a value.
*/
typedef struct SMapItem SMapItem;
/** @brief
* An SMap is a map many SMapItems. Mapping between a key and a value.
*
* An SMap is not dependent on SObject, because it should be more generic
* and have the ability to be used more easily in other SObject based classes
* without causing circular dependencies.
*
* @sa SDynamicArray
* @sa SLinkedList
*/
typedef struct SMap SMap;
#define SMAP(k) ((SMap *) k)
/**
* Data structure representing an SMapItem
*/
struct
SMapItem {
void * key; /**< The Key */
void * value; /**< The Value */
};
#define SMAPITEM(k) ((SMapItem *) k)
/* -------------------------------
* The SMapItem functions.
* -------------------------------
*/
/** @breif
* create a new SMapItem.
*
* @param key The key to be added to the item.
* @param value The value to be added to the item.
*/
S_EXPORTED
SMapItem *
s_map_item_new (void * key, void * value);
/** @breif
* Frees a SMapItem.
*
* @param self the item to be freed.
* @param free_key The function to be used to free the key.
* @param free_value The function to be used to free the value.
*/
S_EXPORTED
void
s_map_item_free (SMapItem * self,
FreeFunc free_key,
FreeFunc free_value);
/* -------------------------------
* The SMap functions.
* -------------------------------
*/
/** @brief
* s_map_new creates a new SMap object, it takes a CompFunc as an argument.
*
* @param comp_func tells the SMap object if the key already exists when
* adding key/value pares or when searching after a key when retrieving a value.
*
* The @c CompFunc returns true if the first and second parameters are equal,
* otherwise false.
*
* @todo
* Check if free_key and/or free_value is null and set them to something
* appropriate.
*/
S_EXPORTED
SMap *
s_map_new (CompFunc comp_func,
HashFunc key_hash_func,
FreeFunc free_key,
FreeFunc free_value);
/** @breif
* This function frees an instance of an SMap.
*
* @param self the object to free.
*/
S_EXPORTED
void
s_map_free (SMap * self, sboolean free_data);
/** @breif
* This function adds a key/value pair to an SMap.
*
* @param self the SMap to add the key/value pair to.
* @param key the key that is used to
*
* @todo
* make it return false on failure, or some other nastiness.
*/
S_EXPORTED
void
s_map_add (SMap * self, spointer key, spointer value);
/** @breif
* Get a value using using a key.
*
* @param self the SMap that you want to retrieve a value from.
* @param key the key that you use to retrieve the value from the SMap from
* with.
*/
S_EXPORTED
spointer
s_map_get (SMap * self, spointer key);
S_EXPORTED
SMapItem *
s_map_get_item (SMap * self, spointer key);
/**
* This function removes an item from
*/
S_EXPORTED
void
s_map_remove (SMap * self, spointer key);
/**
* Do a for each on each key/value pair.
*
* @note This function <em>must not</em> change the key. Changing of the
* value is permited.
*
* The foreach should have the following signature:
@code{.c}
void
my_foreach_func (SMap * map, SMapItem * item, spointer user_data);
@endcode
* The <tt>user_data</tt> is passed to the function, and the <tt>item</tt>
* is what you operate on inside the function. <tt>map</tt> can be ignored.
*/
S_EXPORTED
void
s_map_for_each (SMap * self, ForEachFunc foreach_func, spointer user_data);
/**
* @TODO
* @warning NOT IMPLIED
*
* Get the map as JSON.
* @param self The SMap to get the JSON from.
* @param to_json_key Functon to
*
* @return a null-terminated JSON string representing the matrix.
*
* The outputted JSON will have the format:
@code{.js}
{
"Cats": "Meew",
"Dogs": "Bark",
"Cows": "Moo"
}
@endcode
*/
S_EXPORTED
char *
s_map_serialize_json (SMap * self,
FuncPointer to_json_key,
FuncPointer to_json_value);
/**
* @TODO
* @warning NOT IMPLIED
*
* Deselialize JSON into an SMap.
*
* This will append the key/value pair to the map, with the caviat that
*
* @param self The SMap to write to.
* @param data the JSON data to be deselialised.
*/
S_EXPORTED
void
s_map_deserialize_json (SMap * self ,const char * data,
FuncPointer from_json_key,
FuncPointer from_json_value);
/** @} */
S_END_DECLS
|