/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 *
100 by Gustav Hartvigsson
* Fixed README.
13
s_matrix_new (size_t width,
14
              size_t height,
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
15
              SMatrixRowInformation * row_information) {
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
16
  SMatrix * self = s_malloc (sizeof (SMatrix));
17
  self->matrix = s_calloc (height * width, sizeof (spointer *));
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
18
  
19
  self->height = height;
20
  self->width = width;
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
21
  self->row_information = row_information;
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
22
  
23
  self->last_tuple = 0;
24
  
25
  return self;
26
}
27
28
void
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
29
s_matrix_free (SMatrix * self, sboolean free_data) {
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
30
  if (free_data) {
31
    for (int i = 0; i<= self->height * self->width; i++) {
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
32
      s_free (self->matrix[i]);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
33
    }
34
  }
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
35
  s_free (self->matrix);
36
  s_free (self);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
37
}
38
39
40
SMatrix *
67 by Gustav Hartvigsson
* Readded s_wstring_to_string function*
41
s_matrix_realloc (SMatrix * self, size_t width,
42
                  SMatrixRowInformation * new_row_information) {
43
  SMatrix * new_self = s_matrix_new (width, self->height, new_row_information);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
44
  for (size_t i = 0; i <= self->height; i++) {
45
    for (size_t j = 0; j <= self->width; j++) {
71 by Gustav Hartvigsson
* Implemented s_matrix_set and s_matrix_append
46
      if (j >= new_self->width) {
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
47
        s_matrix_set (new_self, i, j, s_matrix_get (self, i, j));
48
      } else {
49
        //Nothing
50
      }
51
    }
52
  }
53
  
54
  return new_self;
55
}
56
57
spointer
58
s_matrix_get (SMatrix * self, size_t x, size_t y) {
59
  spointer ret = NULL;
60
  if ((x < self->height) && (x < self->last_tuple)){
71 by Gustav Hartvigsson
* Implemented s_matrix_set and s_matrix_append
61
    ret = self->matrix[(self->width * x) + y];
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
62
  } else {
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
63
    s_warn_print ("The tuple requested does not exist.\n");
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
64
  }
65
  return ret;
66
}
67
68
69
void
70
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
71
  if (x >= self->height) {
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
72
    spointer new_matrix = s_realloc (self->matrix,
71 by Gustav Hartvigsson
* Implemented s_matrix_set and s_matrix_append
73
                                   sizeof (spointer) *
74
                                   round_up (x + self->width, self->width));
75
    if (new_matrix) {
76
      self->matrix = new_matrix;
77
    } else {
78
      s_err_print ("Could not reallocate memory. Out of memory?\n");
79
      print_backtrace ();
80
      return;
81
    }
82
  }
83
  if (x > self->last_tuple) {
84
    self->last_tuple = x;
85
  }
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
86
  self->matrix[(self->width * x) + y] = data;
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
87
}
88
89
spointer *
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
90
s_matrix_get_tuple (SMatrix * self, size_t x) {
62 by Gustav Hartvigsson
* General documentation clean up.
91
  spointer * tuple = NULL;
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
92
  if ((x < self->height) && (x < self->last_tuple)){
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
93
    tuple = s_calloc (self->width, sizeof (spointer));
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
94
    for (int i = 0; i <= self->width; i++) {
95
      tuple[i] = s_matrix_get (self, x, i);
96
    }
97
  } else {
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
98
    s_warn_print ("The tuple requested does not exist.\n");
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
99
  }
100
  return tuple;
101
}
102
103
void
72 by Gustav Hartvigsson
* Added our own types for stability and shit and giggles.
104
s_matrix_append (SMatrix * self, spointer * tuple) {
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
105
  if (!tuple){
106
    s_err_print ("Tuple is NULL, this is not allowed. Returning.\n");
71 by Gustav Hartvigsson
* Implemented s_matrix_set and s_matrix_append
107
    print_backtrace ();
63 by Gustav Hartvigsson
* Working on SMatrix to finish it off.
108
    return;
109
  }
71 by Gustav Hartvigsson
* Implemented s_matrix_set and s_matrix_append
110
  /* Get pos of last last_tuple + 1 */
111
  size_t new_x_pos = self->last_tuple + 1;
112
  /* add data to the tuple.*/
113
  for (size_t i = 0; i <= self->width; i++) {
114
    s_matrix_set (self, new_x_pos, i, tuple[i]);
115
  }
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
116
  s_free (tuple);
71 by Gustav Hartvigsson
* Implemented s_matrix_set and s_matrix_append
117
  self->last_tuple = new_x_pos;
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
118
}
119
120
121
void
100 by Gustav Hartvigsson
* Fixed README.
122
s_matrix_for_each (SMatrix * self, ForEachFunc callback, spointer data) {
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
123
  for (size_t i = 0; i <= self->height; i++) {
100 by Gustav Hartvigsson
* Fixed README.
124
    spointer * tuple = s_matrix_get_tuple (self, i);
125
    callback (self, tuple, data);
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
126
    s_free (tuple);
60 by Gustav Hartvigsson
* Added preliminary Matrix type and functions.
127
  }
128
}