/simpletypesystem/trunk

To get this branch, use:
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
 */
20
/**
21
 * An SMatrix is an opaque data structure representing a simple rectangular
22
 * matrix.
23
 *
24
 * An SMatrix Stores pointers to data, and not the data itself.
25
 * So when using functions like s_matrix_realloc, it only copies the pointers
26
 * over from the old SMatrix to the new.
27
 */
28
typedef struct SMatrix SMatrix;
29
30
/**
31
 * convenience structure to store type into and name of a tuple.
32
 * Example:
33
 @code{.c}
34
typedef enum {
35
  MY_TUPLE_ENUM_NAME,
36
  MY_TUPLE_ENUM_AGE,
37
  MY_TUPLE_ENUM_HEIGHT,
38
  MY_TUPLE_ENUM_END_OF_TUPLE
39
} MyRowInfoEnum;
40
41
const SMatrixRowInformation my_row_info[] = {
42
  {"Name", S_TYPE_STRING},
43
  {"Age", S_TYPE_UINT},
44
  {"Height", S_TYPE_UINT}
45
  {NULL, NULL}
46
};
47
 @endcode
48
 * The information can then be used like this:
49
 @code{.c}
50
for (int i = 0, i <= s_matrix_get_last_tuple_n (my_matrix), i++) {
51
  spointer tmp_tuple = s_matrix_get_tuple (my_matrix, i);
52
  for (int j = 0, j <= MY_TUPLE_ENUM_END_OF_TUPLE, j++) {
53
    s_print ("%s: %s", my_row_info[j], tmp_tuple[j]);
54
  }
55
  s_print ("\n");
56
  free (tmp_tuple);
57
}
58
 @endcode
59
 *
60
 */
61
typedef struct SMatrixRowInformation {
62
  char *    name; /**< Name of the row. */
63
  SType     type; /**< Type of the row. */
64
} SMatrixRowInformation;
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
65
66
SMatrix *
67
s_matrix_new (size_t width, size_t height);
68
69
void
70
s_matrix_free (SMatrix * self, bool free_data);
71
72
/**
73
 * Reallocate a SMatrix from one tuple width to an other.
74
 *
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
75
 * @warning The new tuple size must be larger then the last, or it will be
76
 * turnicated, but not freed.
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
77
 *
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
78
 * @note You need to free the old SMatrix yourself
79
 * <em>(but not the data stored)</em>, this is to avoid memory leaks.
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
80
 */
81
SMatrix *
82
s_matrix_realloc (SMatrix * self, size_t width);
83
84
/**
85
 * Get element y in tuple x.
86
 *
62 by Gustav Hartvigsson
* General documentation clean up.
87
 * Equivalent to matrix[x][y] would be in static 2d arrays.
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
88
 */
89
spointer
90
s_matrix_get (SMatrix * self, size_t x, size_t y);
91
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
92
/**
93
 * Returns the number representing the last tuple.
94
 * IE: it the last tuple has the x position of 10, this will return 10.
95
 *
96
 * @param self The SMatrix to get the x coordinate of the last tuple.
97
 *
98
 * @return The x coordinate of the last tuple.
99
 *
100
 * @note The value way be wrong.
101
 */
102
size_t
103
s_matrix_get_last_tuple_n (SMatrix self);
104
105
/**
106
 * @brief Set data at a point [x,y] in the array.
107
 *
108
 * @param self The SMatrix to operate on.
109
 * @param x The x coordinates of where to put the data.
110
 *          (What tuple to put it in).
111
 * @param y The y coordinates to put the data in.
112
 *          (What position in the tuple to put it in).
113
 * @param data The data to be placed in the SMatrix.
114
 */
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
115
void
116
s_matrix_set (SMatrix * self, size_t x, size_t y, spointer data);
117
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
118
/**
119
 * Gets tuple x in SMatrix self.
120
 * 
121
 * @param self the matrix to perform the operation on.
122
 * @param x the tuple to get.
123
 *
124
 * @return a pointer to an array congaing painters to the data in the tuple.
125
 *
126
 * @see s_matrix_get_tuple_as_dynamic_array()
127
 */
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
128
spointer *
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
129
s_matrix_get_tuple (SMatrix * self, size_t x);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
130
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
131
/**
132
 * Append a tuple to a SMatrix.
133
 *
134
 * @param self The SMatrix to perform the operation on.
135
 * @param tuple The <em>allocated</em> data tuple to be copied into the SMatrix.
136
 *
137
 * @note This function will consume (free) allocated data for the tuple.
138
 *
139
 * @note The function assumes that the tuple has the same width as specified
140
 *       during construction or reallocation. Any dangling pointers will
141
 *       <em>not</em> be freed.
142
 */
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
143
void
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
144
s_matrix_append (SMatrix * self, spointer tuple);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
145
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
146
/**
147
 * Get tuple x as a SDynamicArray.
148
 *
149
 * @param self the SMatrix to perform the operation on.
150
 * @param x the tuple to get.
151
 *
152
 * @return An SDynamicArray containing pointers to the data in the tuple.
153
 *
154
 * @see s_matrix_get_tuple
155
 */
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
156
SDynamicArray *
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
157
s_matrix_get_tuple_as_dynamic_array (SMatrix * self, size_t x);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
158
159
/**
62 by Gustav Hartvigsson
* General documentation clean up.
160
 * This iterates over each tuple. giving the tuple as the item in the
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
161
 * callback function.
162
 */
163
void
164
s_matrix_foreach (SMatrix * self, ForEachFunc callback, spointer data);
165
62 by Gustav Hartvigsson
* General documentation clean up.
166
/** @} */
167
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
168
#endif /*__GAME_MATRIX__ */