Program Listing for File llist.h

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

#pragma once

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

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

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

  dptr_t *meta;

  dptr_t *elements;

  size_t elt_size;

  int max_elts;

  uint32_t flags;
};

struct RCSW_ATTR(packed, aligned (sizeof(dptr_t))) llist_node {
  struct llist_node *next;

  struct llist_node *prev;

  dptr_t *data;
};

struct llist_space_mgmt {
  dptr_t*             datablocks;

  struct allocm_entry* db_map;

  struct llist_node*   nodes;

  struct allocm_entry* node_map;
};

struct llist {
  int (*cmpe)(const void *const e1, const void *const e2);
  void (*printe)(const void *e);
  struct llist_space_mgmt space;
  size_t current;
  int max_elts;
  size_t elt_size;
  bool_t sorted;
  struct llist_node *first;
  struct llist_node *last;
  struct ds_iterator iter;

  uint32_t flags;
};


/*******************************************************************************
 * Macros
 ******************************************************************************/
#define LLIST_FOREACH(LIST, DIR, VAR)                                   \
    struct llist_node *_node = NULL;                                    \
    struct llist_node *(VAR)   = NULL;                                  \
    for ((VAR) = _node = (LIST)->first; _node != NULL; (VAR) = _node = _node->DIR)

#define LLIST_ITER(LIST, START, DIR, VAR)                       \
    struct llist_node *_node = NULL;                              \
    struct llist_node *(VAR)     = NULL;                                \
    for ((VAR) = _node = (START); _node != NULL; (VAR) = _node = _node->DIR)

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

static inline bool_t llist_isfull(const struct llist* const list) {
  RCSW_FPC_NV(false, NULL != list);
  return (list->current == (size_t)list->max_elts);
}

static inline bool_t llist_isempty(const struct llist* const list) {
  RCSW_FPC_NV(false, NULL != list);
  return (list->current == 0);
}

static inline size_t llist_size(const struct llist* const list) {
  RCSW_FPC_NV(0, NULL != list);
  return list->current;
}

static inline size_t llist_element_space(size_t max_elts, size_t elt_size) {
  return ds_elt_space_with_meta(max_elts, elt_size);
}

static inline size_t llist_meta_space(size_t max_elts) {
  return ds_elt_space_with_meta(max_elts, sizeof(struct llist_node));
}

RCSW_API struct llist *llist_init(struct llist *list_in,
                         const struct llist_params *params) RCSW_WUR;

RCSW_API void llist_destroy(struct llist *list);

RCSW_API status_t llist_clear(struct llist *list);

RCSW_API status_t llist_remove(struct llist *list, const void *e);

RCSW_API status_t llist_delete(struct llist * list, struct llist_node * victim,
                      void *e);
RCSW_API status_t llist_append(struct llist *list, void *data);

RCSW_API status_t llist_prepend(struct llist *list, void *data);

RCSW_API void llist_print(struct llist *list);

RCSW_API void* llist_data_query(struct llist *list, const void *e);

RCSW_API struct llist_node* llist_node_query(struct llist *list,
                                    const void *e);
RCSW_API status_t llist_sort(struct llist *list, enum exec_type type);

RCSW_API struct llist* llist_copy(struct llist *list,
                         uint32_t flags,
                         void* elements,
                         void* nodes);

RCSW_API struct llist *llist_copy2(struct llist *list,
                          bool_t (*pred)(const void *e),
                          uint32_t flags,
                          void* elements,
                          void* nodes);

RCSW_API struct llist *llist_filter(struct llist *list,
                           bool_t (*pred)(const void *const e),
                           uint32_t flags,
                           void* elements,
                           void* nodes);

RCSW_API status_t llist_filter2(struct llist *list,
                                bool_t (*pred)(const void * e));

RCSW_API status_t llist_splice(struct llist *list1,
                      struct llist *list2,
                      const struct llist_node * node);

RCSW_API status_t llist_map(struct llist *list, void (*f)(void *e));

RCSW_API status_t llist_inject(struct llist * list,
                               void (*f)(void *e, void *res),
                               void *result);

RCSW_API size_t llist_heap_footprint(const struct llist * list);

END_C_DECLS