/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "Matrix.h"

struct SMatrix {
  spointer * matrix; /* An array of pointers... */
  SMatrixRowInformation * row_information;
  
  size_t height;
  size_t width;
  size_t last_tuple;
};

SMatrix *
s_matrix_new (size_t width, size_t height,
              SMatrixRowInformation * row_information) {
  SMatrix * self = malloc (sizeof (SMatrix));
  self->matrix = calloc (height * width, sizeof (spointer *));
  
  self->height = height;
  self->width = width;
  self->row_information = row_information;
  
  self->last_tuple = 0;
  
  return self;
}

void
s_matrix_free (SMatrix * self, sboolean free_data) {
  if (free_data) {
    for (int i = 0; i<= self->height * self->width; i++) {
      free (self->matrix[i]);
    }
  }
  free (self->matrix);
  free (self);
}


SMatrix *
s_matrix_realloc (SMatrix * self, size_t width,
                  SMatrixRowInformation * new_row_information) {
  SMatrix * new_self = s_matrix_new (width, self->height, new_row_information);
  for (size_t i = 0; i <= self->height; i++) {
    for (size_t j = 0; j <= self->width; j++) {
      if (j >= new_self->width) {
        s_matrix_set (new_self, i, j, s_matrix_get (self, i, j));
      } else {
        //Nothing
      }
    }
  }
  
  return new_self;
}

spointer
s_matrix_get (SMatrix * self, size_t x, size_t y) {
  spointer ret = NULL;
  if ((x < self->height) && (x < self->last_tuple)){
    ret = self->matrix[(self->width * x) + y];
  } else {
    s_warn_print ("The tuple requested does not exist.\n");
  }
  return ret;
}


void
s_matrix_set (SMatrix * self, size_t x, size_t y, spointer data) {
  if (x >= self->height) {
    spointer new_matrix = realloc (self->matrix,
                                   sizeof (spointer) *
                                   round_up (x + self->width, self->width));
    if (new_matrix) {
      self->matrix = new_matrix;
    } else {
      s_err_print ("Could not reallocate memory. Out of memory?\n");
      print_backtrace ();
      return;
    }
  }
  if (x > self->last_tuple) {
    self->last_tuple = x;
  }
  self->matrix[(self->width * x) + y] = data;
}

spointer *
s_matrix_get_tuple (SMatrix * self, size_t x) {
  spointer * tuple = NULL;
  if ((x < self->height) && (x < self->last_tuple)){
    tuple = calloc (self->width, sizeof (spointer));
    for (int i = 0; i <= self->width; i++) {
      tuple[i] = s_matrix_get (self, x, i);
    }
  } else {
    s_warn_print ("The tuple requested does not exist.\n");
  }
  return tuple;
}

void
s_matrix_append (SMatrix * self, spointer * tuple) {
  if (!tuple){
    s_err_print ("Tuple is NULL, this is not allowed. Returning.\n");
    print_backtrace ();
    return;
  }
  /* Get pos of last last_tuple + 1 */
  size_t new_x_pos = self->last_tuple + 1;
  /* add data to the tuple.*/
  for (size_t i = 0; i <= self->width; i++) {
    s_matrix_set (self, new_x_pos, i, tuple[i]);
  }
  self->last_tuple = new_x_pos;
}


void
s_matrix_foreach (SMatrix * self, ForEachFunc callback, spointer data) {
  for (size_t i = 0; i <= self->height; i++) {
    callback (self, s_matrix_get_tuple (self, i), data);
  }
}