bzr branch
http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
1 |
#ifndef __GAME_MATRIX__
|
2 |
#define __GAME_MATRIX__
|
|
3 |
||
4 |
#include <string.h> |
|
5 |
#include <stdlib.h> |
|
6 |
#include <stdbool.h> |
|
7 |
#include <stdio.h> |
|
8 |
||
9 |
||
10 |
#include "defs.h" |
|
11 |
#include "DynamicArray.h" |
|
12 |
||
13 |
/**
|
|
|
62
by Gustav Hartvigsson
* General documentation clean up. |
14 |
* @file
|
15 |
* @defgroup SMatrix SMatrix
|
|
16 |
* @addtogroup SMatrix
|
|
17 |
* @{
|
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
18 |
* A SMatrix is a 2d, rectangular array.
|
19 |
*/
|
|
|
71
by Gustav Hartvigsson
* Implemented s_matrix_set and s_matrix_append |
20 |
|
21 |
||
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
22 |
/**
|
23 |
* An SMatrix is an opaque data structure representing a simple rectangular
|
|
24 |
* matrix.
|
|
25 |
*
|
|
26 |
* An SMatrix Stores pointers to data, and not the data itself.
|
|
27 |
* So when using functions like s_matrix_realloc, it only copies the pointers
|
|
28 |
* over from the old SMatrix to the new.
|
|
29 |
*/
|
|
30 |
typedef struct SMatrix SMatrix; |
|
31 |
||
|
68
by Gustav Hartvigsson
* Hid internals of SDynamicArray. |
32 |
#define S_MATRIX(k) ((SMatrix *) (k))
|
33 |
||
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
34 |
/**
|
35 |
* convenience structure to store type into and name of a tuple.
|
|
36 |
* Example:
|
|
37 |
@code{.c}
|
|
38 |
typedef enum {
|
|
39 |
MY_TUPLE_ENUM_NAME,
|
|
40 |
MY_TUPLE_ENUM_AGE,
|
|
41 |
MY_TUPLE_ENUM_HEIGHT,
|
|
42 |
MY_TUPLE_ENUM_END_OF_TUPLE
|
|
43 |
} MyRowInfoEnum;
|
|
44 |
||
45 |
const SMatrixRowInformation my_row_info[] = {
|
|
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
46 |
{"Name", S_TYPE_STRING, NULL, NULL, NULL},
|
47 |
{"Age", S_TYPE_UINT, NULL, NULL, NULL},
|
|
48 |
{"Height", S_TYPE_UINT, NULL, NULL, NULL}
|
|
49 |
{NULL, NULL, NULL, NULL, NULL}
|
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
50 |
};
|
51 |
@endcode
|
|
52 |
* The information can then be used like this:
|
|
53 |
@code{.c}
|
|
54 |
for (int i = 0, i <= s_matrix_get_last_tuple_n (my_matrix), i++) {
|
|
55 |
spointer tmp_tuple = s_matrix_get_tuple (my_matrix, i);
|
|
56 |
for (int j = 0, j <= MY_TUPLE_ENUM_END_OF_TUPLE, j++) {
|
|
57 |
s_print ("%s: %s", my_row_info[j], tmp_tuple[j]);
|
|
58 |
}
|
|
59 |
s_print ("\n");
|
|
60 |
free (tmp_tuple);
|
|
61 |
}
|
|
62 |
@endcode
|
|
63 |
*
|
|
64 |
*/
|
|
65 |
typedef struct SMatrixRowInformation { |
|
|
100
by Gustav Hartvigsson
* Fixed README. |
66 |
schar * name; /**< Name of the row. */ |
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
67 |
FreeFunc free_func; /**< Free function to be used with complex objects.*/ |
68 |
FuncPointer to_json; /**< Used to get some sort of JSON information out of |
|
69 |
a complex object. (Can be NULL). */
|
|
70 |
FuncPointer from_json; /**< When constructing an object of this type from |
|
71 |
* some JSON representation of the complex object.
|
|
|
100
by Gustav Hartvigsson
* Fixed README. |
72 |
* This functons should be used. (Can be NULL)*/
|
|
81
by Gustav Hartvigsson
* Re arranged members of structs to prevent struct fragmentation and padding. |
73 |
SType type; /**< Type of the row. */ |
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
74 |
} SMatrixRowInformation; |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
75 |
|
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
76 |
/**
|
|
100
by Gustav Hartvigsson
* Fixed README. |
77 |
* Create a new SMatrix.
|
78 |
*
|
|
79 |
* @param width The width of the matrix. This can be seen as the number of
|
|
80 |
* columns each tuple has.
|
|
81 |
* @param height The initial height of the matrix. The initial number of rows
|
|
82 |
* the matrix has.
|
|
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
83 |
*/
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
84 |
SMatrix * |
|
100
by Gustav Hartvigsson
* Fixed README. |
85 |
s_matrix_new (size_t width, |
86 |
size_t height, |
|
87 |
SMatrixRowInformation * row_information); |
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
88 |
|
|
100
by Gustav Hartvigsson
* Fixed README. |
89 |
/**
|
90 |
* Free a SMatrix.
|
|
91 |
*
|
|
92 |
* @param self The SMatrix to free.
|
|
93 |
* @param free_data Should the data also be freed with the data structure?
|
|
94 |
*/
|
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
95 |
void
|
|
72
by Gustav Hartvigsson
* Added our own types for stability and shit and giggles. |
96 |
s_matrix_free (SMatrix * self, sboolean free_data); |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
97 |
|
98 |
/**
|
|
99 |
* Reallocate a SMatrix from one tuple width to an other.
|
|
100 |
*
|
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
101 |
* @warning The new tuple size must be larger then the last, or it will be
|
102 |
* turnicated, but not freed.
|
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
103 |
*
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
104 |
* @note You need to free the old SMatrix yourself
|
105 |
* <em>(but not the data stored)</em>, this is to avoid memory leaks.
|
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
106 |
*/
|
107 |
SMatrix * |
|
|
100
by Gustav Hartvigsson
* Fixed README. |
108 |
s_matrix_realloc (SMatrix * self, |
109 |
size_t width, |
|
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
110 |
SMatrixRowInformation * new_row_information); |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
111 |
|
112 |
/**
|
|
113 |
* Get element y in tuple x.
|
|
114 |
*
|
|
|
62
by Gustav Hartvigsson
* General documentation clean up. |
115 |
* Equivalent to matrix[x][y] would be in static 2d arrays.
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
116 |
*/
|
117 |
spointer
|
|
|
100
by Gustav Hartvigsson
* Fixed README. |
118 |
s_matrix_get (SMatrix * self, |
119 |
size_t x, |
|
120 |
size_t y); |
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
121 |
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
122 |
/**
|
123 |
* Returns the number representing the last tuple.
|
|
124 |
* IE: it the last tuple has the x position of 10, this will return 10.
|
|
125 |
*
|
|
126 |
* @param self The SMatrix to get the x coordinate of the last tuple.
|
|
127 |
*
|
|
128 |
* @return The x coordinate of the last tuple.
|
|
129 |
*
|
|
130 |
* @note The value way be wrong.
|
|
131 |
*/
|
|
132 |
size_t
|
|
133 |
s_matrix_get_last_tuple_n (SMatrix self); |
|
134 |
||
135 |
/**
|
|
136 |
* @brief Set data at a point [x,y] in the array.
|
|
137 |
*
|
|
138 |
* @param self The SMatrix to operate on.
|
|
139 |
* @param x The x coordinates of where to put the data.
|
|
140 |
* (What tuple to put it in).
|
|
141 |
* @param y The y coordinates to put the data in.
|
|
142 |
* (What position in the tuple to put it in).
|
|
143 |
* @param data The data to be placed in the SMatrix.
|
|
144 |
*/
|
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
145 |
void
|
|
100
by Gustav Hartvigsson
* Fixed README. |
146 |
s_matrix_set (SMatrix * self, |
147 |
size_t x, |
|
148 |
size_t y, |
|
149 |
spointer data); |
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
150 |
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
151 |
/**
|
152 |
* Gets tuple x in SMatrix self.
|
|
153 |
*
|
|
154 |
* @param self the matrix to perform the operation on.
|
|
155 |
* @param x the tuple to get.
|
|
156 |
*
|
|
157 |
* @return a pointer to an array congaing painters to the data in the tuple.
|
|
158 |
*
|
|
159 |
* @see s_matrix_get_tuple_as_dynamic_array()
|
|
160 |
*/
|
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
161 |
spointer * |
|
100
by Gustav Hartvigsson
* Fixed README. |
162 |
s_matrix_get_tuple (SMatrix * self, |
163 |
size_t x); |
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
164 |
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
165 |
/**
|
166 |
* Append a tuple to a SMatrix.
|
|
167 |
*
|
|
168 |
* @param self The SMatrix to perform the operation on.
|
|
169 |
* @param tuple The <em>allocated</em> data tuple to be copied into the SMatrix.
|
|
170 |
*
|
|
171 |
* @note This function will consume (free) allocated data for the tuple.
|
|
172 |
*
|
|
173 |
* @note The function assumes that the tuple has the same width as specified
|
|
174 |
* during construction or reallocation. Any dangling pointers will
|
|
175 |
* <em>not</em> be freed.
|
|
176 |
*/
|
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
177 |
void
|
|
100
by Gustav Hartvigsson
* Fixed README. |
178 |
s_matrix_append (SMatrix * self, |
179 |
spointer * tuple); |
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
180 |
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
181 |
/**
|
182 |
* Get tuple x as a SDynamicArray.
|
|
183 |
*
|
|
184 |
* @param self the SMatrix to perform the operation on.
|
|
185 |
* @param x the tuple to get.
|
|
186 |
*
|
|
187 |
* @return An SDynamicArray containing pointers to the data in the tuple.
|
|
188 |
*
|
|
189 |
* @see s_matrix_get_tuple
|
|
190 |
*/
|
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
191 |
SDynamicArray * |
|
100
by Gustav Hartvigsson
* Fixed README. |
192 |
s_matrix_get_tuple_as_dynamic_array (SMatrix * self, |
193 |
size_t x); |
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
194 |
|
195 |
/**
|
|
|
62
by Gustav Hartvigsson
* General documentation clean up. |
196 |
* This iterates over each tuple. giving the tuple as the item in the
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
197 |
* callback function.
|
198 |
*/
|
|
199 |
void
|
|
|
100
by Gustav Hartvigsson
* Fixed README. |
200 |
s_matrix_for_each (SMatrix * self, |
201 |
ForEachFunc callback, |
|
202 |
spointer data); |
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
203 |
|
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
204 |
|
205 |
/**
|
|
206 |
* @TODO
|
|
207 |
* @warning NOT IMPLIED
|
|
208 |
*
|
|
209 |
* Get the matrix as JSON.
|
|
210 |
* @param self The SMatrix to get the JSON from.
|
|
211 |
*
|
|
212 |
* @return a null-terminated JSON string representing the matrix.
|
|
213 |
*
|
|
214 |
* @note This only works on primative types. complex types (IE: structs and
|
|
215 |
* such) can not be gotten any information from.
|
|
216 |
*
|
|
217 |
*The outputted JSON will have the format:
|
|
218 |
@code{.js}
|
|
219 |
{
|
|
220 |
{ // Tuple
|
|
221 |
{ // Item
|
|
222 |
name: "Name"
|
|
223 |
type: "STRING",
|
|
224 |
data: "John Smith"
|
|
225 |
},
|
|
226 |
{ // Item
|
|
227 |
name: "Age",
|
|
228 |
type: "UINT",
|
|
229 |
data: 32,
|
|
230 |
},
|
|
231 |
{ // Item
|
|
232 |
name: "Height",
|
|
233 |
type: "UINT",
|
|
234 |
data: 189
|
|
235 |
}
|
|
236 |
},
|
|
237 |
{ //tuple
|
|
238 |
// ...
|
|
239 |
}
|
|
240 |
}
|
|
241 |
@endcode
|
|
242 |
*/
|
|
243 |
char * |
|
244 |
s_matrix_serialize_json (SMatrix * self); |
|
245 |
||
246 |
/**
|
|
247 |
* @TODO
|
|
248 |
* @warning NOT IMPLIED
|
|
249 |
*
|
|
250 |
* Deselialize JSON into an SMatrix.
|
|
251 |
*
|
|
252 |
* @param self The SMatrix to write to.
|
|
253 |
* @param data the JSON data to be deselialised.
|
|
254 |
*
|
|
255 |
* @note The SMatrix must be empty, but initialised.
|
|
256 |
*
|
|
257 |
* @note As with s_matrix_serialize_json(), only primative types work. Complex
|
|
258 |
* types will not work.
|
|
259 |
*/
|
|
260 |
void
|
|
|
100
by Gustav Hartvigsson
* Fixed README. |
261 |
s_matrix_deserialize_json (SMatrix * self, |
262 |
const schar * data); |
|
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
263 |
|
|
62
by Gustav Hartvigsson
* General documentation clean up. |
264 |
/** @} */
|
265 |
||
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
266 |
#endif /*__GAME_MATRIX__ */ |