bzr branch
http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
126.1.1
by Gustav Hartvigsson
* Using |
1 |
#pragma once
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
2 |
|
3 |
#include <string.h> |
|
4 |
#include <stdlib.h> |
|
5 |
#include <stdbool.h> |
|
6 |
#include <stdio.h> |
|
7 |
||
8 |
||
9 |
#include "defs.h" |
|
10 |
#include "DynamicArray.h" |
|
11 |
||
12 |
/**
|
|
62
by Gustav Hartvigsson
* General documentation clean up. |
13 |
* @file
|
14 |
* @defgroup SMatrix SMatrix
|
|
15 |
* @addtogroup SMatrix
|
|
16 |
* @{
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
17 |
* A SMatrix is a 2d, rectangular array.
|
18 |
*/
|
|
71
by Gustav Hartvigsson
* Implemented s_matrix_set and s_matrix_append |
19 |
|
110
by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub. |
20 |
S_BEGIN_DECLS
|
71
by Gustav Hartvigsson
* Implemented s_matrix_set and s_matrix_append |
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 |
*/
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
84 |
S_EXPORTED
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
85 |
SMatrix * |
100
by Gustav Hartvigsson
* Fixed README. |
86 |
s_matrix_new (size_t width, |
87 |
size_t height, |
|
88 |
SMatrixRowInformation * row_information); |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
89 |
|
100
by Gustav Hartvigsson
* Fixed README. |
90 |
/**
|
91 |
* Free a SMatrix.
|
|
92 |
*
|
|
93 |
* @param self The SMatrix to free.
|
|
94 |
* @param free_data Should the data also be freed with the data structure?
|
|
95 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
96 |
S_EXPORTED
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
97 |
void
|
72
by Gustav Hartvigsson
* Added our own types for stability and shit and giggles. |
98 |
s_matrix_free (SMatrix * self, sboolean free_data); |
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
99 |
|
100 |
/**
|
|
101 |
* Reallocate a SMatrix from one tuple width to an other.
|
|
102 |
*
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
103 |
* @warning The new tuple size must be larger then the last, or it will be
|
104 |
* turnicated, but not freed.
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
105 |
*
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
106 |
* @note You need to free the old SMatrix yourself
|
107 |
* <em>(but not the data stored)</em>, this is to avoid memory leaks.
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
108 |
*/
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
109 |
S_EXPORTED
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
110 |
SMatrix * |
100
by Gustav Hartvigsson
* Fixed README. |
111 |
s_matrix_realloc (SMatrix * self, |
112 |
size_t width, |
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
113 |
SMatrixRowInformation * new_row_information); |
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
114 |
|
115 |
/**
|
|
116 |
* Get element y in tuple x.
|
|
117 |
*
|
|
62
by Gustav Hartvigsson
* General documentation clean up. |
118 |
* Equivalent to matrix[x][y] would be in static 2d arrays.
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
119 |
*/
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
120 |
S_EXPORTED
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
121 |
spointer
|
100
by Gustav Hartvigsson
* Fixed README. |
122 |
s_matrix_get (SMatrix * self, |
123 |
size_t x, |
|
124 |
size_t y); |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
125 |
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
126 |
/**
|
127 |
* Returns the number representing the last tuple.
|
|
128 |
* IE: it the last tuple has the x position of 10, this will return 10.
|
|
129 |
*
|
|
130 |
* @param self The SMatrix to get the x coordinate of the last tuple.
|
|
131 |
*
|
|
132 |
* @return The x coordinate of the last tuple.
|
|
133 |
*
|
|
134 |
* @note The value way be wrong.
|
|
135 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
136 |
S_EXPORTED
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
137 |
size_t
|
138 |
s_matrix_get_last_tuple_n (SMatrix self); |
|
139 |
||
140 |
/**
|
|
141 |
* @brief Set data at a point [x,y] in the array.
|
|
142 |
*
|
|
143 |
* @param self The SMatrix to operate on.
|
|
144 |
* @param x The x coordinates of where to put the data.
|
|
145 |
* (What tuple to put it in).
|
|
146 |
* @param y The y coordinates to put the data in.
|
|
147 |
* (What position in the tuple to put it in).
|
|
148 |
* @param data The data to be placed in the SMatrix.
|
|
149 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
150 |
S_EXPORTED
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
151 |
void
|
100
by Gustav Hartvigsson
* Fixed README. |
152 |
s_matrix_set (SMatrix * self, |
153 |
size_t x, |
|
154 |
size_t y, |
|
155 |
spointer data); |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
156 |
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
157 |
/**
|
158 |
* Gets tuple x in SMatrix self.
|
|
159 |
*
|
|
160 |
* @param self the matrix to perform the operation on.
|
|
161 |
* @param x the tuple to get.
|
|
162 |
*
|
|
163 |
* @return a pointer to an array congaing painters to the data in the tuple.
|
|
164 |
*
|
|
165 |
* @see s_matrix_get_tuple_as_dynamic_array()
|
|
166 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
167 |
S_EXPORTED
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
168 |
spointer * |
100
by Gustav Hartvigsson
* Fixed README. |
169 |
s_matrix_get_tuple (SMatrix * self, |
170 |
size_t x); |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
171 |
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
172 |
/**
|
173 |
* Append a tuple to a SMatrix.
|
|
174 |
*
|
|
175 |
* @param self The SMatrix to perform the operation on.
|
|
176 |
* @param tuple The <em>allocated</em> data tuple to be copied into the SMatrix.
|
|
177 |
*
|
|
178 |
* @note This function will consume (free) allocated data for the tuple.
|
|
179 |
*
|
|
180 |
* @note The function assumes that the tuple has the same width as specified
|
|
181 |
* during construction or reallocation. Any dangling pointers will
|
|
182 |
* <em>not</em> be freed.
|
|
183 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
184 |
S_EXPORTED
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
185 |
void
|
100
by Gustav Hartvigsson
* Fixed README. |
186 |
s_matrix_append (SMatrix * self, |
187 |
spointer * tuple); |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
188 |
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
189 |
/**
|
190 |
* Get tuple x as a SDynamicArray.
|
|
191 |
*
|
|
192 |
* @param self the SMatrix to perform the operation on.
|
|
193 |
* @param x the tuple to get.
|
|
194 |
*
|
|
195 |
* @return An SDynamicArray containing pointers to the data in the tuple.
|
|
196 |
*
|
|
197 |
* @see s_matrix_get_tuple
|
|
198 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
199 |
S_EXPORTED
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
200 |
SDynamicArray * |
100
by Gustav Hartvigsson
* Fixed README. |
201 |
s_matrix_get_tuple_as_dynamic_array (SMatrix * self, |
202 |
size_t x); |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
203 |
|
204 |
/**
|
|
62
by Gustav Hartvigsson
* General documentation clean up. |
205 |
* This iterates over each tuple. giving the tuple as the item in the
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
206 |
* callback function.
|
207 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
208 |
S_EXPORTED
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
209 |
void
|
100
by Gustav Hartvigsson
* Fixed README. |
210 |
s_matrix_for_each (SMatrix * self, |
211 |
ForEachFunc callback, |
|
212 |
spointer data); |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
213 |
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
214 |
|
215 |
/**
|
|
216 |
* @TODO
|
|
217 |
* @warning NOT IMPLIED
|
|
218 |
*
|
|
219 |
* Get the matrix as JSON.
|
|
220 |
* @param self The SMatrix to get the JSON from.
|
|
221 |
*
|
|
222 |
* @return a null-terminated JSON string representing the matrix.
|
|
223 |
*
|
|
224 |
* @note This only works on primative types. complex types (IE: structs and
|
|
225 |
* such) can not be gotten any information from.
|
|
226 |
*
|
|
227 |
*The outputted JSON will have the format:
|
|
228 |
@code{.js}
|
|
229 |
{
|
|
230 |
{ // Tuple
|
|
231 |
{ // Item
|
|
232 |
name: "Name"
|
|
233 |
type: "STRING",
|
|
234 |
data: "John Smith"
|
|
235 |
},
|
|
236 |
{ // Item
|
|
237 |
name: "Age",
|
|
238 |
type: "UINT",
|
|
239 |
data: 32,
|
|
240 |
},
|
|
241 |
{ // Item
|
|
242 |
name: "Height",
|
|
243 |
type: "UINT",
|
|
244 |
data: 189
|
|
245 |
}
|
|
246 |
},
|
|
247 |
{ //tuple
|
|
248 |
// ...
|
|
249 |
}
|
|
250 |
}
|
|
251 |
@endcode
|
|
252 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
253 |
S_EXPORTED
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
254 |
char * |
255 |
s_matrix_serialize_json (SMatrix * self); |
|
256 |
||
257 |
/**
|
|
258 |
* @TODO
|
|
259 |
* @warning NOT IMPLIED
|
|
260 |
*
|
|
261 |
* Deselialize JSON into an SMatrix.
|
|
262 |
*
|
|
263 |
* @param self The SMatrix to write to.
|
|
264 |
* @param data the JSON data to be deselialised.
|
|
265 |
*
|
|
266 |
* @note The SMatrix must be empty, but initialised.
|
|
267 |
*
|
|
268 |
* @note As with s_matrix_serialize_json(), only primative types work. Complex
|
|
269 |
* types will not work.
|
|
270 |
*/
|
|
119
by Gustav Hartvigsson
* added S_EXPERTED to public functions. |
271 |
S_EXPORTED
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
272 |
void
|
100
by Gustav Hartvigsson
* Fixed README. |
273 |
s_matrix_deserialize_json (SMatrix * self, |
274 |
const schar * data); |
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
275 |
|
62
by Gustav Hartvigsson
* General documentation clean up. |
276 |
/** @} */
|
277 |
||
110
by Gustav Hartvigsson
* added S_ prifix to my macros. I should not be a scrub. |
278 |
S_END_DECLS
|