Program Listing for File adj_matrix.h

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

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <math.h>
#include "rcsw/ds/ds.h"
#include "rcsw/ds/matrix.h"
#include "rcsw/common/fpc.h"
#include "rcsw/utils/utils.h"

/*******************************************************************************
 * Structure Definitions
 ******************************************************************************/
struct adj_matrix_params {
  dptr_t *elements;

  size_t elt_size;

  uint32_t flags;

  size_t n_vertices;

  bool_t is_directed;

  bool_t is_weighted;
};

struct adj_matrix {
  bool_t        is_directed;

  bool_t        is_weighted;

  size_t        n_edges;

  size_t        elt_size;

  size_t        n_vertices;

  struct matrix matrix;

  uint32_t      flags;
};

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

static inline void* adj_matrix_access(const struct adj_matrix* const matrix,
                                       size_t u,
                                      size_t v) {
  RCSW_FPC_NV(NULL,
              NULL != matrix,
              u < matrix->n_vertices,
              v < matrix->n_vertices);
  return matrix_access(&matrix->matrix, u, v);
}

static inline size_t adj_matrix_element_space(size_t n_vertices,
                                              bool_t is_weighted) {
  return matrix_element_space(n_vertices, n_vertices,
                              is_weighted?sizeof(double):sizeof(int));
}

static inline bool_t adj_matrix_edge_query(struct adj_matrix* const matrix,
                                            size_t u,
                                           size_t v) {
  RCSW_FPC_NV(false,
              NULL != matrix,
              u < matrix->n_vertices,
              v < matrix->n_vertices);
  if (matrix->is_weighted) {
    return (!isnan(*(double*)adj_matrix_access(matrix, u, v)));
  } else {
    return !util_zchk(adj_matrix_access(matrix, u, v), matrix->elt_size);
  }
}

static inline size_t adj_matrix_n_edges(const struct adj_matrix *const matrix) {
  RCSW_FPC_NV(0, NULL != matrix);
  return matrix->n_edges;
}

static inline void adj_matrix_print(const struct adj_matrix* const matrix) {
  RCSW_FPC_V(NULL != matrix);
  matrix_print(&matrix->matrix);
}

static inline bool_t adj_matrix_isempty(const struct adj_matrix* matrix) {
  RCSW_FPC_NV(false, NULL != matrix);
  return (0 == matrix->n_edges);
}

static inline status_t adj_matrix_transpose(struct adj_matrix* const matrix) {
  return matrix_transpose(&matrix->matrix);
}

RCSW_API struct adj_matrix* adj_matrix_init(struct adj_matrix* matrix_in,
                                            const struct adj_matrix_params* params) RCSW_WUR;

RCSW_API void adj_matrix_destroy(struct adj_matrix* matrix);

RCSW_API status_t adj_matrix_edge_addd(struct adj_matrix* matrix,
                              size_t u,
                              size_t v,
                              const double *w);

RCSW_API status_t adj_matrix_edge_addu(struct adj_matrix* matrix,
                                       size_t u,
                                       size_t v);

RCSW_API status_t adj_matrix_edge_remove(struct adj_matrix* matrix,
                                size_t u,
                                size_t v);

END_C_DECLS