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 { |
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
66 |
char * name; /**< Name of the row. */ |
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.
|
|
72 |
* This functons should be used.*/
|
|
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 |
/**
|
77 |
*
|
|
78 |
*/
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
79 |
SMatrix * |
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
80 |
s_matrix_new (size_t width, size_t height, SMatrixRowInformation * row_information); |
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
81 |
|
82 |
void
|
|
72
by Gustav Hartvigsson
* Added our own types for stability and shit and giggles. |
83 |
s_matrix_free (SMatrix * self, sboolean free_data); |
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
84 |
|
85 |
/**
|
|
86 |
* Reallocate a SMatrix from one tuple width to an other.
|
|
87 |
*
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
88 |
* @warning The new tuple size must be larger then the last, or it will be
|
89 |
* turnicated, but not freed.
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
90 |
*
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
91 |
* @note You need to free the old SMatrix yourself
|
92 |
* <em>(but not the data stored)</em>, this is to avoid memory leaks.
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
93 |
*/
|
94 |
SMatrix * |
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
95 |
s_matrix_realloc (SMatrix * self, size_t width, |
96 |
SMatrixRowInformation * new_row_information); |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
97 |
|
98 |
/**
|
|
99 |
* Get element y in tuple x.
|
|
100 |
*
|
|
62
by Gustav Hartvigsson
* General documentation clean up. |
101 |
* Equivalent to matrix[x][y] would be in static 2d arrays.
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
102 |
*/
|
103 |
spointer
|
|
104 |
s_matrix_get (SMatrix * self, size_t x, size_t y); |
|
105 |
||
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
106 |
/**
|
107 |
* Returns the number representing the last tuple.
|
|
108 |
* IE: it the last tuple has the x position of 10, this will return 10.
|
|
109 |
*
|
|
110 |
* @param self The SMatrix to get the x coordinate of the last tuple.
|
|
111 |
*
|
|
112 |
* @return The x coordinate of the last tuple.
|
|
113 |
*
|
|
114 |
* @note The value way be wrong.
|
|
115 |
*/
|
|
116 |
size_t
|
|
117 |
s_matrix_get_last_tuple_n (SMatrix self); |
|
118 |
||
119 |
/**
|
|
120 |
* @brief Set data at a point [x,y] in the array.
|
|
121 |
*
|
|
122 |
* @param self The SMatrix to operate on.
|
|
123 |
* @param x The x coordinates of where to put the data.
|
|
124 |
* (What tuple to put it in).
|
|
125 |
* @param y The y coordinates to put the data in.
|
|
126 |
* (What position in the tuple to put it in).
|
|
127 |
* @param data The data to be placed in the SMatrix.
|
|
128 |
*/
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
129 |
void
|
130 |
s_matrix_set (SMatrix * self, size_t x, size_t y, spointer data); |
|
131 |
||
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
132 |
/**
|
133 |
* Gets tuple x in SMatrix self.
|
|
134 |
*
|
|
135 |
* @param self the matrix to perform the operation on.
|
|
136 |
* @param x the tuple to get.
|
|
137 |
*
|
|
138 |
* @return a pointer to an array congaing painters to the data in the tuple.
|
|
139 |
*
|
|
140 |
* @see s_matrix_get_tuple_as_dynamic_array()
|
|
141 |
*/
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
142 |
spointer * |
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
143 |
s_matrix_get_tuple (SMatrix * self, size_t x); |
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
144 |
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
145 |
/**
|
146 |
* Append a tuple to a SMatrix.
|
|
147 |
*
|
|
148 |
* @param self The SMatrix to perform the operation on.
|
|
149 |
* @param tuple The <em>allocated</em> data tuple to be copied into the SMatrix.
|
|
150 |
*
|
|
151 |
* @note This function will consume (free) allocated data for the tuple.
|
|
152 |
*
|
|
153 |
* @note The function assumes that the tuple has the same width as specified
|
|
154 |
* during construction or reallocation. Any dangling pointers will
|
|
155 |
* <em>not</em> be freed.
|
|
156 |
*/
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
157 |
void
|
72
by Gustav Hartvigsson
* Added our own types for stability and shit and giggles. |
158 |
s_matrix_append (SMatrix * self, spointer * tuple); |
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
159 |
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
160 |
/**
|
161 |
* Get tuple x as a SDynamicArray.
|
|
162 |
*
|
|
163 |
* @param self the SMatrix to perform the operation on.
|
|
164 |
* @param x the tuple to get.
|
|
165 |
*
|
|
166 |
* @return An SDynamicArray containing pointers to the data in the tuple.
|
|
167 |
*
|
|
168 |
* @see s_matrix_get_tuple
|
|
169 |
*/
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
170 |
SDynamicArray * |
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
171 |
s_matrix_get_tuple_as_dynamic_array (SMatrix * self, size_t x); |
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
172 |
|
173 |
/**
|
|
62
by Gustav Hartvigsson
* General documentation clean up. |
174 |
* This iterates over each tuple. giving the tuple as the item in the
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
175 |
* callback function.
|
176 |
*/
|
|
177 |
void
|
|
178 |
s_matrix_foreach (SMatrix * self, ForEachFunc callback, spointer data); |
|
179 |
||
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
180 |
|
181 |
/**
|
|
182 |
* @TODO
|
|
183 |
* @warning NOT IMPLIED
|
|
184 |
*
|
|
185 |
* Get the matrix as JSON.
|
|
186 |
* @param self The SMatrix to get the JSON from.
|
|
187 |
*
|
|
188 |
* @return a null-terminated JSON string representing the matrix.
|
|
189 |
*
|
|
190 |
* @note This only works on primative types. complex types (IE: structs and
|
|
191 |
* such) can not be gotten any information from.
|
|
192 |
*
|
|
193 |
*The outputted JSON will have the format:
|
|
194 |
@code{.js}
|
|
195 |
{
|
|
196 |
{ // Tuple
|
|
197 |
{ // Item
|
|
198 |
name: "Name"
|
|
199 |
type: "STRING",
|
|
200 |
data: "John Smith"
|
|
201 |
},
|
|
202 |
{ // Item
|
|
203 |
name: "Age",
|
|
204 |
type: "UINT",
|
|
205 |
data: 32,
|
|
206 |
},
|
|
207 |
{ // Item
|
|
208 |
name: "Height",
|
|
209 |
type: "UINT",
|
|
210 |
data: 189
|
|
211 |
}
|
|
212 |
},
|
|
213 |
{ //tuple
|
|
214 |
// ...
|
|
215 |
}
|
|
216 |
}
|
|
217 |
@endcode
|
|
218 |
*/
|
|
219 |
char * |
|
220 |
s_matrix_serialize_json (SMatrix * self); |
|
221 |
||
222 |
/**
|
|
223 |
* @TODO
|
|
224 |
* @warning NOT IMPLIED
|
|
225 |
*
|
|
226 |
* Deselialize JSON into an SMatrix.
|
|
227 |
*
|
|
228 |
* @param self The SMatrix to write to.
|
|
229 |
* @param data the JSON data to be deselialised.
|
|
230 |
*
|
|
231 |
* @note The SMatrix must be empty, but initialised.
|
|
232 |
*
|
|
233 |
* @note As with s_matrix_serialize_json(), only primative types work. Complex
|
|
234 |
* types will not work.
|
|
235 |
*/
|
|
236 |
void
|
|
237 |
s_matrix_deserialize_json (SMatrix * self ,const char * data); |
|
238 |
||
62
by Gustav Hartvigsson
* General documentation clean up. |
239 |
/** @} */
|
240 |
||
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
241 |
#endif /*__GAME_MATRIX__ */ |