Updating heap API and fixing some things
This commit is contained in:
1 parent
bf3535970e
commit
9f596f7974
10 files changed
+507
-114
No files matched your search
+39
-62
@@ -107,12 +107,6 @@ static const char *cmc_string_fmt_heap = "struct %s<%s> "
|
||||
\
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the heap */ \
|
||||
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
||||
\
|
||||
/* Function that returns an iterator to the end of the heap */ \
|
||||
struct SNAME##_iter (*it_end)(struct SNAME *); \
|
||||
}; \
|
||||
\
|
||||
/* Value struct function table */ \
|
||||
@@ -185,17 +179,15 @@ static const char *cmc_string_fmt_heap = "struct %s<%s> "
|
||||
bool PFX##_print(struct SNAME *_heap_, FILE *fptr); \
|
||||
\
|
||||
/* Iterator Functions */ \
|
||||
/* Iterator Allocation and Deallocation */ \
|
||||
struct SNAME##_iter *PFX##_iter_new(struct SNAME *target); \
|
||||
void PFX##_iter_free(struct SNAME##_iter *iter); \
|
||||
/* Iterator Initialization */ \
|
||||
void PFX##_iter_init(struct SNAME##_iter *iter, struct SNAME *target); \
|
||||
struct SNAME##_iter PFX##_iter_start(struct SNAME *target); \
|
||||
struct SNAME##_iter PFX##_iter_end(struct SNAME *target); \
|
||||
/* Iterator State */ \
|
||||
bool PFX##_iter_start(struct SNAME##_iter *iter); \
|
||||
bool PFX##_iter_end(struct SNAME##_iter *iter); \
|
||||
bool PFX##_iter_at_start(struct SNAME##_iter *iter); \
|
||||
bool PFX##_iter_at_end(struct SNAME##_iter *iter); \
|
||||
/* Iterator Movement */ \
|
||||
void PFX##_iter_to_start(struct SNAME##_iter *iter); \
|
||||
void PFX##_iter_to_end(struct SNAME##_iter *iter); \
|
||||
bool PFX##_iter_to_start(struct SNAME##_iter *iter); \
|
||||
bool PFX##_iter_to_end(struct SNAME##_iter *iter); \
|
||||
bool PFX##_iter_next(struct SNAME##_iter *iter); \
|
||||
bool PFX##_iter_prev(struct SNAME##_iter *iter); \
|
||||
bool PFX##_iter_advance(struct SNAME##_iter *iter, size_t steps); \
|
||||
@@ -213,8 +205,6 @@ static const char *cmc_string_fmt_heap = "struct %s<%s> "
|
||||
/* Implementation Detail Functions */ \
|
||||
static void PFX##_impl_float_up(struct SNAME *_heap_, size_t index); \
|
||||
static void PFX##_impl_float_down(struct SNAME *_heap_, size_t index); \
|
||||
static struct SNAME##_iter PFX##_impl_it_start(struct SNAME *_heap_); \
|
||||
static struct SNAME##_iter PFX##_impl_it_end(struct SNAME *_heap_); \
|
||||
\
|
||||
struct SNAME *PFX##_new(size_t capacity, enum cmc_heap_order HO, \
|
||||
struct SNAME##_fval *f_val) \
|
||||
@@ -250,8 +240,6 @@ static const char *cmc_string_fmt_heap = "struct %s<%s> "
|
||||
_heap_->f_val = f_val; \
|
||||
_heap_->alloc = alloc; \
|
||||
_heap_->callbacks = NULL; \
|
||||
_heap_->it_start = PFX##_impl_it_start; \
|
||||
_heap_->it_end = PFX##_impl_it_end; \
|
||||
\
|
||||
return _heap_; \
|
||||
} \
|
||||
@@ -292,8 +280,6 @@ static const char *cmc_string_fmt_heap = "struct %s<%s> "
|
||||
_heap_->f_val = f_val; \
|
||||
_heap_->alloc = alloc; \
|
||||
_heap_->callbacks = callbacks; \
|
||||
_heap_->it_start = PFX##_impl_it_start; \
|
||||
_heap_->it_end = PFX##_impl_it_end; \
|
||||
\
|
||||
return _heap_; \
|
||||
} \
|
||||
@@ -553,60 +539,71 @@ static const char *cmc_string_fmt_heap = "struct %s<%s> "
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
struct SNAME##_iter *PFX##_iter_new(struct SNAME *target) \
|
||||
struct SNAME##_iter PFX##_iter_start(struct SNAME *target) \
|
||||
{ \
|
||||
struct SNAME##_iter *iter = \
|
||||
target->alloc->malloc(sizeof(struct SNAME##_iter)); \
|
||||
struct SNAME##_iter iter; \
|
||||
\
|
||||
if (!iter) \
|
||||
return NULL; \
|
||||
\
|
||||
PFX##_iter_init(iter, target); \
|
||||
iter.target = target; \
|
||||
iter.cursor = 0; \
|
||||
iter.start = true; \
|
||||
iter.end = PFX##_empty(target); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_free(struct SNAME##_iter *iter) \
|
||||
struct SNAME##_iter PFX##_iter_end(struct SNAME *target) \
|
||||
{ \
|
||||
iter->target->alloc->free(iter); \
|
||||
struct SNAME##_iter iter; \
|
||||
\
|
||||
iter.target = target; \
|
||||
iter.cursor = 0; \
|
||||
iter.start = PFX##_empty(target); \
|
||||
iter.end = true; \
|
||||
\
|
||||
if (!PFX##_empty(target)) \
|
||||
{ \
|
||||
iter.cursor = target->count - 1; \
|
||||
} \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_init(struct SNAME##_iter *iter, struct SNAME *target) \
|
||||
{ \
|
||||
iter->target = target; \
|
||||
iter->cursor = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(target); \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_start(struct SNAME##_iter *iter) \
|
||||
bool PFX##_iter_at_start(struct SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->start; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_end(struct SNAME##_iter *iter) \
|
||||
bool PFX##_iter_at_end(struct SNAME##_iter *iter) \
|
||||
{ \
|
||||
return PFX##_empty(iter->target) || iter->end; \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_to_start(struct SNAME##_iter *iter) \
|
||||
bool PFX##_iter_to_start(struct SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (!PFX##_empty(iter->target)) \
|
||||
{ \
|
||||
iter->cursor = 0; \
|
||||
iter->start = true; \
|
||||
iter->end = PFX##_empty(iter->target); \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
void PFX##_iter_to_end(struct SNAME##_iter *iter) \
|
||||
bool PFX##_iter_to_end(struct SNAME##_iter *iter) \
|
||||
{ \
|
||||
if (!PFX##_empty(iter->target)) \
|
||||
{ \
|
||||
iter->cursor = iter->target->count - 1; \
|
||||
iter->start = PFX##_empty(iter->target); \
|
||||
iter->end = true; \
|
||||
\
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
return false; \
|
||||
} \
|
||||
\
|
||||
bool PFX##_iter_next(struct SNAME##_iter *iter) \
|
||||
@@ -648,7 +645,7 @@ static const char *cmc_string_fmt_heap = "struct %s<%s> "
|
||||
/* Returns true only if the iterator moved */ \
|
||||
bool PFX##_iter_advance(struct SNAME##_iter *iter, size_t steps) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor + 1 == iter->target->count) \
|
||||
@@ -787,26 +784,6 @@ static const char *cmc_string_fmt_heap = "struct %s<%s> "
|
||||
else \
|
||||
break; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
static struct SNAME##_iter PFX##_impl_it_start(struct SNAME *_heap_) \
|
||||
{ \
|
||||
struct SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _heap_); \
|
||||
PFX##_iter_to_start(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
} \
|
||||
\
|
||||
static struct SNAME##_iter PFX##_impl_it_end(struct SNAME *_heap_) \
|
||||
{ \
|
||||
struct SNAME##_iter iter; \
|
||||
\
|
||||
PFX##_iter_init(&iter, _heap_); \
|
||||
PFX##_iter_to_end(&iter); \
|
||||
\
|
||||
return iter; \
|
||||
}
|
||||
|
||||
#endif /* CMC_HEAP_H */
|
||||
@@ -877,7 +877,7 @@ static const char *cmc_string_fmt_intervalheap = "struct %s<%s> "
|
||||
/* Returns true only if the iterator moved */ \
|
||||
bool PFX##_iter_advance(struct SNAME##_iter *iter, size_t steps) \
|
||||
{ \
|
||||
if (iter->start) \
|
||||
if (iter->end) \
|
||||
return false; \
|
||||
\
|
||||
if (iter->cursor == 0) \
|
||||
|
||||
Reference in new issue
Block a user