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