bzr branch
http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
1 |
/*
|
5.2.7
by Gustav Hartvigsson
* Switched licence to a more permisive one. |
2 |
Copyright (c) 2013-2014 Gustav Hartvigsson
|
3 |
||
4 |
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5 |
of this software and associated documentation files (the "Software"), to deal
|
|
6 |
in the Software without restriction, including without limitation the rights
|
|
7 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8 |
copies of the Software, and to permit persons to whom the Software is
|
|
9 |
furnished to do so, subject to the following conditions:
|
|
10 |
||
11 |
The above copyright notice and this permission notice shall be included in
|
|
12 |
all copies or substantial portions of the Software.
|
|
13 |
||
14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20 |
THE SOFTWARE.
|
|
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
21 |
*/
|
22 |
||
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
23 |
#ifndef __H_MAP__
|
24 |
#define __H_MAP__
|
|
25 |
||
44
by Gustav Hartvigsson
* Started to structuce the dectumentation a little better. |
26 |
#include "Func.h" |
27 |
#include "defs.h" |
|
28 |
#include "utils.h" |
|
53
by Gustav Hartvigsson
* Finnished up s_map_add () ??? |
29 |
#include "hash.h" |
30 |
#include "DynamicArray.h" |
|
44
by Gustav Hartvigsson
* Started to structuce the dectumentation a little better. |
31 |
|
110
by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub. |
32 |
S_BEGIN_DECLS
|
44
by Gustav Hartvigsson
* Started to structuce the dectumentation a little better. |
33 |
|
69
by Gustav Hartvigsson
* Finished of SMap... Sort of... |
34 |
#define S_MAP_DEFAULT_NUMBER_OF_BUCKETS 257
|
52
by Gustav Hartvigsson
* Reorderd CMakeLists.txt list of files |
35 |
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
36 |
/**
|
44
by Gustav Hartvigsson
* Started to structuce the dectumentation a little better. |
37 |
* @file
|
38 |
* @defgroup SMap SMap
|
|
39 |
* @addtogroup SMap
|
|
40 |
* @{
|
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
41 |
* An SMap is a data structure that holds many mappings of objects to objects:
|
42 |
* say, a string to an other string. This can be likened to the Dict structure
|
|
43 |
* in python, but not fully.
|
|
44 |
*
|
|
45 |
* An SMap is made up of SMapItems, each MapItem holds two pointers to data.
|
|
46 |
* The first pointer is the key, the secold is the value.
|
|
47 |
*
|
|
48 |
* please note that SMaps can be slow and are unordered.
|
|
49 |
*/
|
|
50 |
||
22
by Gustav Hartvigsson
* Made code compile |
51 |
|
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
52 |
/** @brief
|
53 |
* SMapItem holds the mapping of a key to a value.
|
|
54 |
*/
|
|
44
by Gustav Hartvigsson
* Started to structuce the dectumentation a little better. |
55 |
typedef struct SMapItem SMapItem; |
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
56 |
|
57 |
/** @brief
|
|
58 |
* An SMap is a map many SMapItems. Mapping between a key and a value.
|
|
59 |
*
|
|
46
by Gustav Hartvigsson
* Renamed DynamicArray to SDynamicArray. |
60 |
* An SMap is not dependent on SObject, because it should be more generic
|
61 |
* and have the ability to be used more easily in other SObject based classes
|
|
62 |
* without causing circular dependencies.
|
|
63 |
*
|
|
64 |
* @sa SDynamicArray
|
|
65 |
* @sa SLinkedList
|
|
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
66 |
*/
|
44
by Gustav Hartvigsson
* Started to structuce the dectumentation a little better. |
67 |
typedef struct SMap SMap; |
68 |
||
69
by Gustav Hartvigsson
* Finished of SMap... Sort of... |
69 |
#define SMAP(k) ((SMap *) k)
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
70 |
|
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
71 |
/**
|
72 |
* Data structure representing an SMapItem
|
|
73 |
*/
|
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
74 |
struct
|
75 |
SMapItem { |
|
11
by Gustav Hartvigsson
* Finnished up a the inheritance documentation. |
76 |
void * key; /**< The Key */ |
77 |
void * value; /**< The Value */ |
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
78 |
};
|
79 |
||
69
by Gustav Hartvigsson
* Finished of SMap... Sort of... |
80 |
#define SMAPITEM(k) ((SMapItem *) k)
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
81 |
|
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
82 |
/* -------------------------------
|
83 |
* The SMapItem functions.
|
|
84 |
* -------------------------------
|
|
85 |
*/
|
|
86 |
||
87 |
/** @breif
|
|
88 |
* create a new SMapItem.
|
|
89 |
*
|
|
90 |
* @param key The key to be added to the item.
|
|
91 |
* @param value The value to be added to the item.
|
|
92 |
*/
|
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
93 |
SMapItem * |
94 |
s_map_item_new (void * key, void * value); |
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
95 |
|
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
96 |
/** @breif
|
97 |
* Frees a SMapItem.
|
|
98 |
*
|
|
99 |
* @param self the item to be freed.
|
|
18
by Gustav Hartvigsson
* Made the includation graphs look sane. |
100 |
* @param free_key The function to be used to free the key.
|
101 |
* @param free_value The function to be used to free the value.
|
|
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
102 |
*/
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
103 |
void
|
69
by Gustav Hartvigsson
* Finished of SMap... Sort of... |
104 |
s_map_item_free (SMapItem * self, |
105 |
FreeFunc free_key, |
|
106 |
FreeFunc free_value); |
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
107 |
|
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
108 |
/* -------------------------------
|
109 |
* The SMap functions.
|
|
110 |
* -------------------------------
|
|
111 |
*/
|
|
112 |
||
113 |
/** @brief
|
|
114 |
* s_map_new creates a new SMap object, it takes a CompFunc as an argument.
|
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
115 |
*
|
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
116 |
* @param comp_func tells the SMap object if the key already exists when
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
117 |
* adding key/value pares or when searching after a key when retrieving a value.
|
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
118 |
*
|
14
by Gustav Hartvigsson
* Think I am 70% done with SMap now... |
119 |
* The @c CompFunc returns true if the first and second parameters are equal,
|
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
120 |
* otherwise false.
|
14
by Gustav Hartvigsson
* Think I am 70% done with SMap now... |
121 |
*
|
16
by Gustav Hartvigsson
* Made sure the code compiled |
122 |
* @todo
|
14
by Gustav Hartvigsson
* Think I am 70% done with SMap now... |
123 |
* Check if free_key and/or free_value is null and set them to something
|
124 |
* appropriate.
|
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
125 |
*/
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
126 |
SMap * |
127 |
s_map_new (CompFunc comp_func, |
|
53
by Gustav Hartvigsson
* Finnished up s_map_add () ??? |
128 |
HashFunc key_hash_func, |
69
by Gustav Hartvigsson
* Finished of SMap... Sort of... |
129 |
FreeFunc free_key, |
130 |
FreeFunc free_value); |
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
131 |
|
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
132 |
/** @breif
|
133 |
* This function frees an instance of an SMap.
|
|
134 |
*
|
|
135 |
* @param self the object to free.
|
|
136 |
*/
|
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
137 |
void
|
79
by Gustav Hartvigsson
* Clean up of SMap's for each code. |
138 |
s_map_free (SMap * self, sboolean free_data); |
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
139 |
|
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
140 |
/** @breif
|
141 |
* This function adds a key/value pair to an SMap.
|
|
142 |
*
|
|
143 |
* @param self the SMap to add the key/value pair to.
|
|
15
by Gustav Hartvigsson
* Added some notes... bah |
144 |
* @param key the key that is used to
|
145 |
*
|
|
16
by Gustav Hartvigsson
* Made sure the code compiled |
146 |
* @todo
|
18
by Gustav Hartvigsson
* Made the includation graphs look sane. |
147 |
* make it return false on failure, or some other nastiness.
|
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
148 |
*/
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
149 |
void
|
150 |
s_map_add (SMap * self, spointer key, spointer value); |
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
151 |
|
5.2.4
by Gustav Hartvigsson
Finished documenting the SMap and SMapItem interfaces. |
152 |
/** @breif
|
153 |
* Get a value using using a key.
|
|
154 |
*
|
|
155 |
* @param self the SMap that you want to retrieve a value from.
|
|
156 |
* @param key the key that you use to retrieve the value from the SMap from
|
|
157 |
* with.
|
|
158 |
*/
|
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
159 |
spointer
|
160 |
s_map_get (SMap * self, spointer key); |
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
161 |
|
15
by Gustav Hartvigsson
* Added some notes... bah |
162 |
/**
|
70
by Gustav Hartvigsson
* removed s_dynamic_array_foreach_with_return. |
163 |
* This function removes an item from
|
15
by Gustav Hartvigsson
* Added some notes... bah |
164 |
*/
|
61
by Gustav Hartvigsson
* Made the code more easy to read. |
165 |
void
|
166 |
s_map_remove (SMap * self, spointer key); |
|
167 |
||
70
by Gustav Hartvigsson
* removed s_dynamic_array_foreach_with_return. |
168 |
/**
|
169 |
* Do a for each on each key/value pair.
|
|
170 |
*
|
|
171 |
* @note This function <em>must not</em> change the key. Changing of the
|
|
172 |
* value is permited.
|
|
173 |
*
|
|
174 |
* The foreach should have the following signature:
|
|
175 |
@code{.c}
|
|
176 |
void
|
|
177 |
my_foreach_func (SMap * map, SMapItem * item, spointer user_data);
|
|
178 |
@endcode
|
|
179 |
* The <tt>user_data</tt> is passed to the function, and the <tt>item</tt>
|
|
180 |
* is what you operate on inside the function. <tt>map</tt> can be ignored.
|
|
181 |
*/
|
|
182 |
void
|
|
79
by Gustav Hartvigsson
* Clean up of SMap's for each code. |
183 |
s_map_for_each (SMap * self, ForEachFunc foreach_func, spointer user_data); |
70
by Gustav Hartvigsson
* removed s_dynamic_array_foreach_with_return. |
184 |
|
185 |
/**
|
|
186 |
* @TODO
|
|
187 |
* @warning NOT IMPLIED
|
|
188 |
*
|
|
189 |
* Get the map as JSON.
|
|
190 |
* @param self The SMap to get the JSON from.
|
|
191 |
* @param to_json_key Functon to
|
|
192 |
*
|
|
193 |
* @return a null-terminated JSON string representing the matrix.
|
|
194 |
*
|
|
195 |
* The outputted JSON will have the format:
|
|
196 |
@code{.js}
|
|
197 |
{
|
|
198 |
"Cats": "Meew",
|
|
199 |
"Dogs": "Bark",
|
|
200 |
"Cows": "Moo"
|
|
201 |
}
|
|
202 |
@endcode
|
|
203 |
*/
|
|
204 |
char * |
|
205 |
s_map_serialize_json (SMap * self, |
|
206 |
FuncPointer to_json_key, |
|
207 |
FuncPointer to_json_value); |
|
208 |
||
209 |
/**
|
|
210 |
* @TODO
|
|
211 |
* @warning NOT IMPLIED
|
|
212 |
*
|
|
213 |
* Deselialize JSON into an SMap.
|
|
214 |
*
|
|
215 |
* This will append the key/value pair to the map, with the caviat that
|
|
216 |
*
|
|
217 |
* @param self The SMap to write to.
|
|
218 |
* @param data the JSON data to be deselialised.
|
|
219 |
*/
|
|
220 |
void
|
|
221 |
s_map_deserialize_json (SMap * self ,const char * data, |
|
222 |
FuncPointer from_json_key, |
|
223 |
FuncPointer from_json_value); |
|
224 |
||
44
by Gustav Hartvigsson
* Started to structuce the dectumentation a little better. |
225 |
/** @} */
|
226 |
||
110
by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub. |
227 |
S_END_DECLS
|
22
by Gustav Hartvigsson
* Made code compile |
228 |
|
5.2.1
by Gustav Hartvigsson
Started work on the Map (SMap) data structure. |
229 |
#endif
|