Added sparse matrix impl. Used by copyopt_ilp
[libfirm] / ir / be / sp_matrix.h
1 /**
2  * Sparse matrix storage with linked lists for rows and cols.
3  * I did not need floats, so this is all integer.
4  * @author Daniel Grund
5  * @date 07.04.2005
6  */
7
8 #ifndef _SP_MATRIX_H
9 #define _SP_MATRIX_H
10
11 typedef struct _matrix_elem_t {
12         int row, col, val;
13 } matrix_elem_t;
14
15 typedef struct _sp_matrix_t sp_matrix_t;
16
17 /**
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.
21  */
22 sp_matrix_t *new_matrix(int rows, int cols);
23
24 /**
25  * Free space used by matrix m
26  */
27 void del_matrix(sp_matrix_t *m);
28
29 /**
30  * Sets m[row, col] to val
31  */
32 void matrix_set(sp_matrix_t *m, int row, int col, int val);
33
34 /**
35  * Returns the value stored in m[row, col].
36  */
37 int matrix_get(const sp_matrix_t *m, int row, int col);
38
39 /**
40  * Returns the number of rows in this matrix; the height; the first dimension
41  */
42 int matrix_get_rowcount(const sp_matrix_t *m);
43
44 /**
45  * Returns the number of cols in this matrix; the width; the second dimension
46  */
47 int matrix_get_rowcount(const sp_matrix_t *m);
48
49 /**
50  * Start iteratation over a row. Elements are returned from left to right.
51  * @return NULL if row is empty, else the first element.
52  */
53 matrix_elem_t *matrix_row_first(sp_matrix_t *m, int row);
54
55 /**
56  * Start iteratation over a column. Elements are returned from top to bottom.
57  * @return NULL if column is empty, else the first element.
58  */
59 matrix_elem_t *matrix_col_first(sp_matrix_t *m, int row);
60
61 /**
62  * @return the next element in iteration order or NULL if iteration is done.
63  */
64 matrix_elem_t *matrix_next(sp_matrix_t *m);
65
66 /**
67  * m    The matrix
68  * r    The row
69  * curr The variable to assign all elements to during iteration
70  */
71 #define matrix_foreach_in_row(m,r,curr) \
72                 for (curr = matrix_row_first(m, r); curr; curr = matrix_next(m))
73
74 /**
75  * m    The matrix
76  * c    The col
77  * curr The variable to assign all elements to during iteration
78  */
79 #define matrix_foreach_in_col(m,c,curr) \
80                 for (curr = matrix_col_first(m, c); curr; curr = matrix_next(m))
81
82 /**
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.
86  */
87 void matrix_dump(sp_matrix_t *m, FILE *out, int factor);
88
89 /**
90  * Perform a self test with a sqare matrix of dimensions d.
91  */
92 void matrix_self_test(int d);
93
94 #endif