/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
#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
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
28
s_matrix_free (SMatrix * self, sboolean free_data) {
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
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++) {
71 by Gustav Hartvigsson
* Implemented s_matrix_set and s_matrix_append
45
      if (j >= new_self->width) {
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
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)){
71 by Gustav Hartvigsson
* Implemented s_matrix_set and s_matrix_append
60
    ret = self->matrix[(self->width * x) + y];
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
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) {
71 by Gustav Hartvigsson
* Implemented s_matrix_set and s_matrix_append
70
  if (x >= self->height) {
71
    spointer new_matrix = realloc (self->matrix,
72
                                   sizeof (spointer) *
73
                                   round_up (x + self->width, self->width));
74
    if (new_matrix) {
75
      self->matrix = new_matrix;
76
    } else {
77
      s_err_print ("Could not reallocate memory. Out of memory?\n");
78
      print_backtrace ();
79
      return;
80
    }
81
  }
82
  if (x > self->last_tuple) {
83
    self->last_tuple = x;
84
  }
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
85
  self->matrix[(self->width * x) + y] = data;
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
86
}
87
88
spointer *
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
89
s_matrix_get_tuple (SMatrix * self, size_t x) {
62 by Gustav Hartvigsson
* General documentation clean up.
90
  spointer * tuple = NULL;
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
91
  if ((x < self->height) && (x < self->last_tuple)){
92
    tuple = calloc (self->width, sizeof (spointer));
93
    for (int i = 0; i <= self->width; i++) {
94
      tuple[i] = s_matrix_get (self, x, i);
95
    }
96
  } else {
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
97
    s_warn_print ("The tuple requested does not exist.\n");
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
98
  }
99
  return tuple;
100
}
101
102
void
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
103
s_matrix_append (SMatrix * self, spointer * tuple) {
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
104
  if (!tuple){
105
    s_err_print ("Tuple is NULL, this is not allowed. Returning.\n");
71 by Gustav Hartvigsson
* Implemented s_matrix_set and s_matrix_append
106
    print_backtrace ();
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
107
    return;
108
  }
71 by Gustav Hartvigsson
* Implemented s_matrix_set and s_matrix_append
109
  /* Get pos of last last_tuple + 1 */
110
  size_t new_x_pos = self->last_tuple + 1;
111
  /* add data to the tuple.*/
112
  for (size_t i = 0; i <= self->width; i++) {
113
    s_matrix_set (self, new_x_pos, i, tuple[i]);
114
  }
115
  self->last_tuple = new_x_pos;
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
116
}
117
118
119
void
120
s_matrix_foreach (SMatrix * self, ForEachFunc callback, spointer data) {
121
  for (size_t i = 0; i <= self->height; i++) {
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
122
    callback (self, s_matrix_get_tuple (self, i), data);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
123
  }
124
}