00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00031 #if !defined(_TIFA_MATRIX_H_)
00032
00036 #define _TIFA_MATRIX_H_
00037
00038 #ifdef __cplusplus
00039 extern "C" {
00040 #endif
00041
00042 #include "tifa_config.h"
00043
00044 #include <inttypes.h>
00045
00046 #include "bitstring_t.h"
00047
00053 #define NO_SUCH_ROW UINT32_MAX
00054
00055
00056
00057
00058
00059
00060
00071 struct struct_binary_matrix_t {
00075 uint32_t nrows_alloced;
00086 uint32_t ncols_alloced;
00090 uint32_t nrows;
00094 uint32_t ncols;
00099 TIFA_BITSTRING_T** data;
00100 };
00105 typedef struct struct_binary_matrix_t binary_matrix_t;
00106
00130 binary_matrix_t* alloc_binary_matrix(uint32_t nrows, uint32_t ncols);
00131
00141 binary_matrix_t* clone_binary_matrix(const binary_matrix_t * const matrix);
00142
00150 void reset_binary_matrix(binary_matrix_t* const matrix);
00151
00162 void free_binary_matrix(binary_matrix_t* const matrix);
00163
00171 void print_binary_matrix(const binary_matrix_t* const matrix);
00172
00186 inline static uint8_t get_matrix_bit(uint32_t row, uint32_t col,
00187 const binary_matrix_t* const matrix) {
00188 #if BITSTRING_T_SIZE_IS_POW_OF_TWO
00189 uint32_t col_offset = col & (BITSTRING_T_BITSIZE - 1);
00190 col_offset = BITSTRING_T_BITSIZE - 1 - col_offset;
00191 col = col >> POW_TWO_BITSTRING_T_SIZE;
00192 #else
00193 uint32_t col_offset = col % BITSTRING_T_BITSIZE;
00194 col_offset = BITSTRING_T_BITSIZE - 1 - col_offset;
00195 col /= BITSTRING_T_BITSIZE;
00196 #endif
00197 if (0 == ((((TIFA_BITSTRING_T)1)<<col_offset) & matrix->data[row][col])) {
00198 return 0;
00199 } else {
00200 return 1;
00201 }
00202 }
00214 inline static void set_matrix_bit_to_one(uint32_t row, uint32_t col,
00215 binary_matrix_t* const matrix) {
00216 #if BITSTRING_T_SIZE_IS_POW_OF_TWO
00217 uint32_t col_offset = col & (BITSTRING_T_BITSIZE - 1);
00218 col_offset = BITSTRING_T_BITSIZE - 1 - col_offset;
00219 col = col >> POW_TWO_BITSTRING_T_SIZE;
00220 #else
00221 uint32_t col_offset = col % BITSTRING_T_BITSIZE;
00222 col_offset = BITSTRING_T_BITSIZE - 1 - col_offset;
00223 col /= BITSTRING_T_BITSIZE;
00224 #endif
00225 matrix->data[row][col] |= ((TIFA_BITSTRING_T)1<<col_offset);
00226 }
00227
00240 inline static void set_matrix_bit_to_zero(uint32_t row, uint32_t col,
00241 binary_matrix_t* const matrix) {
00242 #if BITSTRING_T_SIZE_IS_POW_OF_TWO
00243 uint32_t col_offset = col & (BITSTRING_T_BITSIZE - 1);
00244 col_offset = BITSTRING_T_BITSIZE - 1 - col_offset;
00245 col = col >> POW_TWO_BITSTRING_T_SIZE;
00246 #else
00247 uint32_t col_offset = col % BITSTRING_T_BITSIZE;
00248 col_offset = BITSTRING_T_BITSIZE - 1 - col_offset;
00249 col /= BITSTRING_T_BITSIZE;
00250 #endif
00251 matrix->data[row][col] &= !(((TIFA_BITSTRING_T)1)<<col_offset);
00252 }
00253
00265 inline static void flip_matrix_bit(uint32_t row, uint32_t col,
00266 binary_matrix_t* const matrix){
00267 #if BITSTRING_T_SIZE_IS_POW_OF_TWO
00268 uint32_t col_offset = col & (BITSTRING_T_BITSIZE - 1);
00269 col_offset = BITSTRING_T_BITSIZE - 1 - col_offset;
00270 col = col >> POW_TWO_BITSTRING_T_SIZE;
00271 #else
00272 uint32_t col_offset = col % BITSTRING_T_BITSIZE;
00273 col_offset = BITSTRING_T_BITSIZE - 1 - col_offset;
00274 col /= BITSTRING_T_BITSIZE;
00275 #endif
00276 matrix->data[row][col] ^= (((TIFA_BITSTRING_T)1)<<col_offset);
00277 }
00316 inline static uint32_t first_row_with_one_on_col(uint32_t col,
00317 const binary_matrix_t* const matrix) {
00318 #if BITSTRING_T_SIZE_IS_POW_OF_TWO
00319 uint32_t col_offset = col & (BITSTRING_T_BITSIZE - 1);
00320 col_offset = BITSTRING_T_BITSIZE - 1 - col_offset;
00321 col = col >> POW_TWO_BITSTRING_T_SIZE;
00322 #else
00323 uint32_t col_offset = col % BITSTRING_T_BITSIZE;
00324 col_offset = BITSTRING_T_BITSIZE - 1 - col_offset;
00325 col /= BITSTRING_T_BITSIZE;
00326 #endif
00327 for (uint32_t irow = 0; irow < matrix->nrows; irow++) {
00328 if ((((TIFA_BITSTRING_T)1)<<col_offset) & matrix->data[irow][col]) {
00329 return irow;
00330 }
00331 }
00332 return NO_SUCH_ROW;
00333 }
00334
00335
00336
00337
00338
00339
00340
00351 struct struct_byte_matrix_t {
00355 uint32_t nrows_alloced;
00359 uint32_t ncols_alloced;
00363 uint32_t nrows;
00367 uint32_t ncols;
00372 unsigned char** data;
00373 };
00374
00379 typedef struct struct_byte_matrix_t byte_matrix_t;
00380
00400 byte_matrix_t* alloc_byte_matrix(uint32_t nrows, uint32_t ncols);
00401
00411 byte_matrix_t* clone_byte_matrix(const byte_matrix_t * const matrix);
00412
00420 void reset_byte_matrix(byte_matrix_t* const matrix);
00421
00432 void free_byte_matrix(byte_matrix_t* const matrix);
00433
00441 void print_byte_matrix(const byte_matrix_t* const matrix);
00442
00443 #ifdef __cplusplus
00444 }
00445 #endif
00446
00447 #endif