Program Listing for File dyn_matrix.h

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

#pragma once

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

/*******************************************************************************
 * Structure Definitions
 ******************************************************************************/
struct dyn_matrix_params {
  void (*printe)(const void *e);

  size_t elt_size;

  uint32_t flags;

  size_t n_rows;
  size_t n_cols;
  dptr_t* rows;
};


struct dyn_matrix {
  struct darray* rows;

  size_t n_rows;

  size_t n_cols;

  size_t elt_size;

  uint32_t flags;

  void (*printe)(const void *const e);
};

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

static inline void* dyn_matrix_access(const struct dyn_matrix* const matrix,
                                          size_t u, size_t v) {
    RCSW_FPC_NV(NULL,
                NULL != matrix,
                u < matrix->n_rows,
              v < matrix->n_cols);
    return darray_data_get((struct darray*)darray_data_get(matrix->rows, u), v);
}

static inline size_t dyn_matrix_space(size_t n_rows,
                                      size_t n_cols,
                                      size_t elt_size) {
  return darray_element_space(n_cols, elt_size) * n_rows +
      darray_element_space(n_rows, sizeof(struct darray));
}

static inline status_t dyn_matrix_clear(struct dyn_matrix* const matrix,
                                           size_t u, size_t v) {
    RCSW_FPC_NV(ERROR,
                NULL != matrix,
                u < matrix->n_rows,
                v < matrix->n_cols);
    ds_elt_clear(dyn_matrix_access(matrix, u, v), matrix->elt_size);
    return OK;
}

static inline bool_t dyn_matrix_issquare(const struct dyn_matrix* const matrix) {
  RCSW_FPC_NV(false, NULL != matrix);

  return matrix->n_cols == matrix->n_rows;
}

RCSW_API struct dyn_matrix* dyn_matrix_init(struct dyn_matrix* matrix_in,
                                            const struct dyn_matrix_params* params) RCSW_WUR;

RCSW_API void dyn_matrix_destroy(struct dyn_matrix* matrix);

RCSW_API status_t dyn_matrix_transpose(struct dyn_matrix* matrix);

RCSW_API void dyn_matrix_print(const struct dyn_matrix* matrix);

RCSW_API status_t dyn_matrix_resize(struct dyn_matrix* matrix,
                                    size_t u,
                                    size_t v);

RCSW_API status_t dyn_matrix_set(struct dyn_matrix* matrix,
                        size_t u,
                        size_t v,
                        const void *w);

END_C_DECLS