Program Listing for File rbuffer.h

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

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include "rcsw/ds/ds.h"
#include "rcsw/ds/iter.h"
#include "rcsw/common/fpc.h"

/*******************************************************************************
 * Structure Definitions
 ******************************************************************************/
struct rbuffer_params {
  int (*cmpe)(const void *const e1, const void *const e2);

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

  dptr_t *elements;

  size_t elt_size;

  size_t max_elts;

  uint32_t flags;
};

struct rbuffer {
  void (*printe)(const void *const e);

  int (*cmpe)(const void *const e1, const void *const e2);

  struct ds_iterator iter;

  dptr_t *elements;

  size_t current;

  size_t max_elts;

  size_t elt_size;

  size_t start;

  uint32_t flags;
};

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

static inline bool_t rbuffer_isfull(const struct rbuffer* const rb) {
  RCSW_FPC_NV(false, NULL != rb);

  return rb->current == rb->max_elts;
}

static inline bool_t rbuffer_isempty(const struct rbuffer* const rb) {
  RCSW_FPC_NV(false, NULL != rb);
  return (rb->current == 0);
}

static inline size_t rbuffer_size(const struct rbuffer* const rb) {
  RCSW_FPC_NV(0, NULL != rb);
  return rb->current;
}

static inline size_t rbuffer_capacity(const struct rbuffer* const rb) {
  RCSW_FPC_NV(0, NULL != rb);
  return rb->max_elts;
}

static inline size_t rbuffer_element_space(size_t max_elts, size_t elt_size) {
  return ds_elt_space_simple(max_elts, elt_size);
}

RCSW_API struct rbuffer *rbuffer_init(struct rbuffer *rb_in,
                             const struct rbuffer_params * params) RCSW_WUR;

RCSW_API void rbuffer_destroy(struct rbuffer *rb);

RCSW_API status_t rbuffer_add(struct rbuffer * rb, const void * e);

RCSW_API status_t rbuffer_remove(struct rbuffer * rb, void * e);

RCSW_API void* rbuffer_data_get(const struct rbuffer * rb, size_t idx);

RCSW_API int rbuffer_index_query(struct rbuffer * rb, const void * e);

RCSW_API status_t rbuffer_serve_front(const struct rbuffer * rb, void * e);

RCSW_API void* rbuffer_front(const struct rbuffer * rb);

RCSW_API status_t rbuffer_clear(struct rbuffer * rb);

RCSW_API status_t rbuffer_map(struct rbuffer * rb, void (*f)(void *e));

RCSW_API status_t rbuffer_inject(struct rbuffer * rb,
                        void (*f)(void *elt, void *res),
                        void *result);

RCSW_API void rbuffer_print(struct rbuffer * rb);

END_C_DECLS