bzr branch
http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
1 |
#include "Matrix.h" |
2 |
||
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
3 |
struct SMatrix { |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
4 |
spointer * matrix; /* An array of pointers... */ |
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
5 |
SMatrixRowInformation * row_information; |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
6 |
|
7 |
size_t height; |
|
8 |
size_t width; |
|
9 |
size_t last_tuple; |
|
10 |
};
|
|
11 |
||
12 |
SMatrix * |
|
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
13 |
s_matrix_new (size_t width, size_t height, |
14 |
SMatrixRowInformation * row_information) { |
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
15 |
SMatrix * self = malloc (sizeof (SMatrix)); |
16 |
self->matrix = calloc (height * width, sizeof (spointer *)); |
|
17 |
|
|
18 |
self->height = height; |
|
19 |
self->width = width; |
|
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
20 |
self->row_information = row_information; |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
21 |
|
22 |
self->last_tuple = 0; |
|
23 |
|
|
24 |
return self; |
|
25 |
}
|
|
26 |
||
27 |
void
|
|
28 |
s_matrix_free (SMatrix * self, bool free_data) { |
|
29 |
if (free_data) { |
|
30 |
for (int i = 0; i<= self->height * self->width; i++) { |
|
31 |
free (self->matrix[i]); |
|
32 |
} |
|
33 |
} |
|
34 |
free (self->matrix); |
|
35 |
free (self); |
|
36 |
}
|
|
37 |
||
38 |
||
39 |
SMatrix * |
|
|
67
by Gustav Hartvigsson
* Readded s_wstring_to_string function* |
40 |
s_matrix_realloc (SMatrix * self, size_t width, |
41 |
SMatrixRowInformation * new_row_information) { |
|
42 |
SMatrix * new_self = s_matrix_new (width, self->height, new_row_information); |
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
43 |
for (size_t i = 0; i <= self->height; i++) { |
44 |
for (size_t j = 0; j <= self->width; j++) { |
|
45 |
if (j > new_self->width) { |
|
46 |
s_matrix_set (new_self, i, j, s_matrix_get (self, i, j)); |
|
47 |
} else { |
|
48 |
//Nothing |
|
49 |
} |
|
50 |
} |
|
51 |
} |
|
52 |
|
|
53 |
return new_self; |
|
54 |
}
|
|
55 |
||
56 |
spointer
|
|
57 |
s_matrix_get (SMatrix * self, size_t x, size_t y) { |
|
58 |
spointer ret = NULL; |
|
59 |
if ((x < self->height) && (x < self->last_tuple)){ |
|
60 |
ret = self->matrix[self->width * x + y]; |
|
61 |
} else { |
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
62 |
s_warn_print ("The tuple requested does not exist.\n"); |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
63 |
} |
64 |
return ret; |
|
65 |
}
|
|
66 |
||
67 |
||
68 |
void
|
|
69 |
s_matrix_set (SMatrix * self, size_t x, size_t y, spointer data) { |
|
70 |
|
|
71 |
}
|
|
72 |
||
73 |
spointer * |
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
74 |
s_matrix_get_tuple (SMatrix * self, size_t x) { |
|
62
by Gustav Hartvigsson
* General documentation clean up. |
75 |
spointer * tuple = NULL; |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
76 |
if ((x < self->height) && (x < self->last_tuple)){ |
77 |
tuple = calloc (self->width, sizeof (spointer)); |
|
78 |
for (int i = 0; i <= self->width; i++) { |
|
79 |
tuple[i] = s_matrix_get (self, x, i); |
|
80 |
} |
|
81 |
} else { |
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
82 |
s_warn_print ("The tuple requested does not exist.\n"); |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
83 |
print_backtrace (); |
84 |
} |
|
85 |
return tuple; |
|
86 |
}
|
|
87 |
||
88 |
void
|
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
89 |
s_matrix_append (SMatrix * self, spointer tuple) { |
90 |
if (!tuple){ |
|
91 |
s_err_print ("Tuple is NULL, this is not allowed. Returning.\n"); |
|
92 |
return; |
|
93 |
} |
|
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
94 |
|
95 |
}
|
|
96 |
||
97 |
||
98 |
void
|
|
99 |
s_matrix_foreach (SMatrix * self, ForEachFunc callback, spointer data) { |
|
100 |
for (size_t i = 0; i <= self->height; i++) { |
|
|
63
by Gustav Hartvigsson
* Working on SMatrix to finish it off. |
101 |
callback (self, s_matrix_get_tuple (self, i), data); |
|
60
by Gustav Hartvigsson
* Added preliminary Matrix type and functions. |
102 |
} |
103 |
}
|