Program Listing for File csmatrix.h

Return to documentation for file (rcsw/ds/csmatrix.h)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "rcsw/ds/darray.h"
#include "rcsw/ds/llist.h"

/*******************************************************************************
 * Constant Definitions
 ******************************************************************************/
enum csmatrix_type {
    ekCSMATRIX_INT,
    ekCSMATRIX_FLOAT,
    ekCSMATRIX_DOUBLE
};

/*******************************************************************************
 * Structure Definitions
 ******************************************************************************/
struct csmatrix_params {
  size_t n_rows;

  size_t n_cols;

  size_t n_nz_elts;

  uint32_t flags;

  enum csmatrix_type type;
};

struct csmatrix {
  struct darray inner_indices;

  struct darray outer_starts;

  struct darray values;

  size_t n_rows;

  size_t n_cols;

  size_t n_nz_elts;

  size_t n_eff_cols;

  uint32_t flags;

  enum csmatrix_type type;

  struct llist *cols;


  uint8_t * nodes;

  uint8_t * elts;

  int* csizes;
};

/*******************************************************************************
 * API Functions
 ******************************************************************************/
BEGIN_C_DECLS

static inline size_t csmatrix_type_size(const struct csmatrix* const matrix) {
  RCSW_FPC_NV(0, NULL != matrix);

  switch (matrix->type) {
    case ekCSMATRIX_INT:
        return sizeof(int);
        break;
    case ekCSMATRIX_FLOAT:
        return sizeof(float);
        break;
    case ekCSMATRIX_DOUBLE:
        return sizeof(double);
        break;
    default:
      break;
    } /* switch() */
  return 0;
} /* csmatrix_type_size() */

static inline size_t csmatrix_n_rows(
    const struct csmatrix* const matrix) {
    RCSW_FPC_NV(0, NULL != matrix);

    /*
     * Last element does not correspond to a row--just there to make math
     * simple
     */
    return darray_size(&matrix->outer_starts) - 1;
} /* csmatrix_n_rows() */

static inline size_t csmatrix_n_eff_cols(const struct csmatrix* const matrix) {
    RCSW_FPC_NV(0, NULL != matrix);
    return matrix->n_eff_cols;
} /* csmatrix_n_eff_cols() */

static inline size_t csmatrix_n_cols(const struct csmatrix* const matrix) {
    RCSW_FPC_NV(0, NULL != matrix);
    return matrix->n_cols;
} /* csmatrix_n_eff_cols() */

static inline size_t csmatrix_size(
    const struct csmatrix* const matrix) {
    RCSW_FPC_NV(0, NULL != matrix);
    return (size_t)*(int*)darray_data_get(&matrix->outer_starts,
                                          darray_size(&matrix->outer_starts)-1);
} /* csmatrix_size() */

static inline int* csmatrix_row(const struct csmatrix* const matrix,
                                size_t row) {
    RCSW_FPC_NV(0, NULL != matrix);
    return darray_data_get(&matrix->inner_indices,
                           (size_t)*(int*)darray_data_get(&matrix->outer_starts, row));
} /* csmatrix_row() */


static inline size_t csmatrix_rsize(
    const struct csmatrix* const matrix, size_t row) {
    RCSW_FPC_NV(0, NULL != matrix, row < darray_size(&matrix->outer_starts));
    size_t row_start = (size_t)*(int*)darray_data_get(&matrix->outer_starts,
                                                      row);
    size_t row_end = (size_t)*(int*)darray_data_get(&matrix->outer_starts,
                                                    row+1);

    return row_end - row_start;
} /* csmatrix_rsize() */

static inline size_t csmatrix_csize(const struct csmatrix* const matrix,
                                    size_t col) {
    RCSW_FPC_NV(0, NULL != matrix);
    return (size_t)matrix->csizes[col];
} /* csmatrix_csize() */

static inline int* csmatrix_csizes(const struct csmatrix* const matrix) {
    RCSW_FPC_NV(0, NULL != matrix);
    return matrix->csizes;
} /* csmatrix_csize() */

static inline int* csmatrix_inner_indices(
    const struct csmatrix* const matrix) {
    RCSW_FPC_NV(0, NULL != matrix);
    return darray_data_get(&matrix->inner_indices, 0);
} /* csmatrix_rsize() */

static inline int* csmatrix_outer_starts(
    const struct csmatrix* const matrix) {
    RCSW_FPC_NV(0, NULL != matrix);
    return darray_data_get(&matrix->outer_starts, 0);
} /* csmatrix_rsize() */

static inline double* csmatrix_values(
    const struct csmatrix* const matrix) {
    RCSW_FPC_NV(0, NULL != matrix);
    return darray_data_get(&matrix->values, 0);
} /* csmatrix_rsize() */

/*******************************************************************************
 * Function Prototypes
 ******************************************************************************/
struct csmatrix* csmatrix_init(struct csmatrix* matrix_in,
                               const struct csmatrix_params* params);

void csmatrix_destroy(struct csmatrix* matrix);

status_t csmatrix_entry_add(struct csmatrix* matrix,
                            bool_t sequential_insertions, size_t row,
                            size_t col, const void* e);
status_t csmatrix_entry_delete(struct csmatrix* matrix,
                               size_t row, size_t col);

status_t csmatrix_entry_set(struct csmatrix* matrix,
                            size_t row,
                            size_t col,
                            const void* e);

int csmatrix_inner_index_get(const struct csmatrix* matrix,
                             size_t row, size_t col);

void* csmatrix_entry_get(const struct csmatrix* matrix,
                         size_t row, size_t col);

status_t csmatrix_cols_normalize(struct csmatrix* matrix);

status_t csmatrix_vmult(const struct csmatrix* matrix,
                        const struct darray* vector_in,
                        struct darray* vector_out);

struct csmatrix* csmatrix_transpose(struct csmatrix* matrix);

status_t csmatrix_resize(struct csmatrix* matrix, size_t n_rows,
                         size_t n_nz_elts);

status_t csmatrix_calc_clists(struct csmatrix* matrix);

void csmatrix_print(const struct csmatrix* matrix);

END_C_DECLS