2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Sparse matrix storage with linked lists for rows and cols.
11 #ifndef LPP_SP_MATRIX_H
12 #define LPP_SP_MATRIX_H
19 typedef struct matrix_elem_t {
20 int row; /* row index */
21 int col; /* column index */
22 float val; /* the actual value of the entry */
25 typedef struct sp_matrix_t sp_matrix_t;
28 * Allocate a new matrix and init internal data for a matrix of size
29 * row_init X col_init. Matrix cannot grow beyond these init values.
30 * All elements are initially (implicit) set to 0.
32 sp_matrix_t *new_matrix(int rows, int cols);
35 * Free space used by matrix m
37 void del_matrix(sp_matrix_t *m);
40 * Sets m[row, col] to val
42 void matrix_set(sp_matrix_t *m, int row, int col, double val);
45 * Sets m[row, cols[0,...,i]] to val for i in (0, ..., num_cols - 1)
46 * Following assumptions are done here:
47 * - the current row inserted is the last inserted row so far
48 * - cols[] is sorted ascending by col number
50 void matrix_set_row_bulk(sp_matrix_t *m, int row, int *cols, int num_cols, double val);
53 * Returns the value stored in m[row, col].
55 double matrix_get(const sp_matrix_t *m, int row, int col);
58 * Returns the number of (not-0-)entries.
60 int matrix_get_entries(const sp_matrix_t *m);
63 * Returns the number of rows in this matrix; the height; the first dimension
65 int matrix_get_rowcount(const sp_matrix_t *m);
68 * Returns the number of cols in this matrix; the width; the second dimension
70 int matrix_get_colcount(const sp_matrix_t *m);
73 * Start iteration over all matrix elements. Row by row, from top to bottom.
74 * @return NULL if the matrix is empty, else the first element.
76 const matrix_elem_t *matrix_first(sp_matrix_t *m);
79 * Start iteration over a row. Elements are returned from left to right.
80 * @return NULL if row is empty, else the first element.
82 const matrix_elem_t *matrix_row_first(sp_matrix_t *m, int row);
85 * Start iteration over a column. Elements are returned from top to bottom.
86 * @return NULL if column is empty, else the first element.
88 const matrix_elem_t *matrix_col_first(sp_matrix_t *m, int col);
91 * @return the next element in iteration order or NULL if iteration is done.
93 const matrix_elem_t *matrix_next(sp_matrix_t *m);
96 * @return the size for a single matrix element
98 unsigned matrix_get_elem_size(void);
102 * curr The variable to assign all elements to during iteration
103 * Save against removal of curr
105 #define matrix_foreach(m,curr) \
106 for (matrix_elem_t const *curr = matrix_first(m); curr; curr = matrix_next(m))
111 * curr The variable to assign all elements to during iteration
112 * Save against removal of curr
114 #define matrix_foreach_in_row(m,r,curr) \
115 for (matrix_elem_t const *curr = matrix_row_first(m, r); curr; curr = matrix_next(m))
120 * curr The variable to assign all elements to during iteration
121 * Save against removal of curr
123 #define matrix_foreach_in_col(m,c,curr) \
124 for (matrix_elem_t const *curr = matrix_col_first(m, c); curr; curr = matrix_next(m))
127 * Changes the matrix into an equivalent one with maximal number zero-rows.
128 * The only equivalence transformation is:
129 * Adding a constant to Qij and subtracting it from Qji
131 void matrix_optimize(sp_matrix_t *m);
134 * Dumps the matrix factor*m to the stream @p out.
135 * Remark: I dont need spaces between the elements. So feel free to add
136 * char *seperator to the arguments.
138 void matrix_dump(sp_matrix_t *m, FILE *out, int factor);
141 * Perform a self test with a square matrix of dimensions d.
143 void matrix_self_test(int d);