/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
3
struct _SMatrix {
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 {
58
    fprintf (stderr, "WARN: The tuple requested does not exist.\n");
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 *
70
s_matrix_get_tupel (SMatrix * self, size_t x) {
71
  spointer* tuple = NULL;
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 {
78
    fprintf (stderr, "WARN: The tuple requested does not exist.\n");
79
    print_backtrace ();
80
  }
81
  return tuple;
82
}
83
84
void
85
s_matrix_append (SMatrix * self, spointer data) {
86
  
87
}
88
89
90
void
91
s_matrix_foreach (SMatrix * self, ForEachFunc callback, spointer data) {
92
  for (size_t i = 0; i <= self->height; i++) {
93
    callback (self, s_matrix_get_tupel (self, i), data);
94
  }
95
}