2 * Sparse matrix storage with linked lists for rows and cols.
3 * I did not need floats, so this is all integer.
11 typedef struct _matrix_elem_t {
15 typedef struct _sp_matrix_t sp_matrix_t;
18 * Allocate a new matrix and init internal data for a matrix of size
19 * row_init X col_init. Matrix cannot grow beyond these init values.
20 * All elements are initially (implicit) set to 0.
22 sp_matrix_t *new_matrix(int rows, int cols);
25 * Free space used by matrix m
27 void del_matrix(sp_matrix_t *m);
30 * Sets m[row, col] to val
32 void matrix_set(sp_matrix_t *m, int row, int col, int val);
35 * Returns the value stored in m[row, col].
37 int matrix_get(const sp_matrix_t *m, int row, int col);
40 * Returns the number of rows in this matrix; the height; the first dimension
42 int matrix_get_rowcount(const sp_matrix_t *m);
45 * Returns the number of cols in this matrix; the width; the second dimension
47 int matrix_get_rowcount(const sp_matrix_t *m);
50 * Start iteratation over a row. Elements are returned from left to right.
51 * @return NULL if row is empty, else the first element.
53 matrix_elem_t *matrix_row_first(sp_matrix_t *m, int row);
56 * Start iteratation over a column. Elements are returned from top to bottom.
57 * @return NULL if column is empty, else the first element.
59 matrix_elem_t *matrix_col_first(sp_matrix_t *m, int row);
62 * @return the next element in iteration order or NULL if iteration is done.
64 matrix_elem_t *matrix_next(sp_matrix_t *m);
69 * curr The variable to assign all elements to during iteration
71 #define matrix_foreach_in_row(m,r,curr) \
72 for (curr = matrix_row_first(m, r); curr; curr = matrix_next(m))
77 * curr The variable to assign all elements to during iteration
79 #define matrix_foreach_in_col(m,c,curr) \
80 for (curr = matrix_col_first(m, c); curr; curr = matrix_next(m))
83 * Dumps the matrix factor*m to the stream @p out.
84 * Remark: I dont need spaces between the elements. So feel free to add
85 * char *seperator to the arguments.
87 void matrix_dump(sp_matrix_t *m, FILE *out, int factor);
90 * Perform a self test with a sqare matrix of dimensions d.
92 void matrix_self_test(int d);