#ifndef UCX_TREE_H
#define UCX_TREE_H
#include "common.h"
#include "iterator.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct cx_tree_iterator_s {
CX_ITERATOR_BASE;
bool skip;
bool visit_on_exit;
bool exiting;
ptrdiff_t loc_children;
ptrdiff_t loc_next;
size_t counter;
void *node;
void *node_next;
void **stack;
size_t stack_capacity;
union {
size_t stack_size;
size_t depth;
};
} CxTreeIterator;
struct cx_tree_visitor_queue_s {
void *node;
size_t depth;
struct cx_tree_visitor_queue_s *next;
};
typedef struct cx_tree_visitor_s {
CX_ITERATOR_BASE;
bool skip;
ptrdiff_t loc_children;
ptrdiff_t loc_next;
size_t counter;
void *node;
size_t depth;
struct cx_tree_visitor_queue_s *queue_next;
struct cx_tree_visitor_queue_s *queue_last;
} CxTreeVisitor;
__attribute__((__nonnull__))
static inline
void cxTreeIteratorDispose(CxTreeIterator *iter) {
free(iter->stack);
iter->stack =
NULL;
}
__attribute__((__nonnull__))
static inline
void cxTreeVisitorDispose(CxTreeVisitor *visitor) {
struct cx_tree_visitor_queue_s *q = visitor->queue_next;
while (q !=
NULL) {
struct cx_tree_visitor_queue_s *next = q->next;
free(q);
q = next;
}
}
#define cxTreeIteratorContinue(iterator) (iterator).skip = true;
continue
#define cxTreeVisitorContinue(visitor) cxTreeIteratorContinue(visitor)
__attribute__((__nonnull__))
void cx_tree_link(
void * restrict parent,
void * restrict node,
ptrdiff_t loc_parent,
ptrdiff_t loc_children,
ptrdiff_t loc_prev,
ptrdiff_t loc_next
);
__attribute__((__nonnull__))
void cx_tree_unlink(
void *node,
ptrdiff_t loc_parent,
ptrdiff_t loc_children,
ptrdiff_t loc_prev,
ptrdiff_t loc_next
);
typedef int (*cx_tree_search_func)(
void const *node,
void const* data);
__attribute__((__nonnull__))
int cx_tree_search(
void const *root,
void const *data,
cx_tree_search_func sfunc,
void **result,
ptrdiff_t loc_children,
ptrdiff_t loc_next
);
__attribute__((__nonnull__))
CxTreeIterator cx_tree_iterator(
void *root,
bool visit_on_exit,
ptrdiff_t loc_children,
ptrdiff_t loc_next
);
__attribute__((__nonnull__))
CxTreeVisitor cx_tree_visitor(
void *root,
ptrdiff_t loc_children,
ptrdiff_t loc_next
);
#ifdef __cplusplus
}
#endif
#endif