Added heap and list resize function and refactored some tests and added some more

This commit is contained in:
Leonardo committed 2019-10-08 13:57:58 -03:00
1 parent 9f8671f75f
commit b9b507bf07
15 files changed
+841 -752

No files matched your search

+4 -20
View File
@@ -346,26 +346,10 @@ static const char *cmc_string_fmt_deque = "%s at %p { buffer:%p, capacity:%" PRI
if (PFX##_capacity(_deque_) == capacity) \
return true; \
\
size_t new_capacity = 0; \
if (capacity < PFX##_count(_deque_)) \
return false; \
\
if (capacity < PFX##_capacity(_deque_)) \
{ \
/* Shrink */ \
if (capacity < PFX##_count(_deque_)) \
return false; \
\
new_capacity = capacity; \
} \
else \
{ \
/* Grow */ \
if (PFX##_capacity(_deque_) * 2 < capacity) \
new_capacity = capacity; \
else \
new_capacity = PFX##_capacity(_deque_) * 2; \
} \
\
V *new_buffer = malloc(sizeof(V) * new_capacity); \
V *new_buffer = malloc(sizeof(V) * capacity); \
\
if (!new_buffer) \
return false; \
@@ -380,7 +364,7 @@ static const char *cmc_string_fmt_deque = "%s at %p { buffer:%p, capacity:%" PRI
free(_deque_->buffer); \
\
_deque_->buffer = new_buffer; \
_deque_->capacity = new_capacity; \
_deque_->capacity = capacity; \
_deque_->front = 0; \
_deque_->back = _deque_->count; \
\
+18 -26
View File
@@ -112,7 +112,6 @@ typedef enum cmc_heap_order_e
bool PFX##_remove(SNAME *_heap_, V *result); \
/* Element Access */ \
V PFX##_peek(SNAME *_heap_); \
V *PFX##_peek_ref(SNAME *_heap_); \
/* Collection State */ \
bool PFX##_contains(SNAME *_heap_, V element); \
bool PFX##_empty(SNAME *_heap_); \
@@ -120,6 +119,7 @@ typedef enum cmc_heap_order_e
size_t PFX##_count(SNAME *_heap_); \
size_t PFX##_capacity(SNAME *_heap_); \
/* Collection Utility */ \
bool PFX##_resize(SNAME *_heap_, size_t capacity); \
SNAME *PFX##_copy_of(SNAME *_heap_, V (*copy_func)(V)); \
bool PFX##_equals(SNAME *_heap1_, SNAME *_heap2_); \
cmc_string PFX##_to_string(SNAME *_heap_); \
@@ -159,7 +159,6 @@ typedef enum cmc_heap_order_e
#define CMC_GENERATE_HEAP_SOURCE(PFX, SNAME, V) \
\
/* Implementation Detail Functions */ \
static bool PFX##_impl_grow(SNAME *_heap_); \
static bool PFX##_impl_float_up(SNAME *_heap_, size_t index); \
static bool PFX##_impl_float_down(SNAME *_heap_, size_t index); \
static SNAME##_iter PFX##_impl_it_start(SNAME *_heap_); \
@@ -232,7 +231,7 @@ typedef enum cmc_heap_order_e
{ \
if (PFX##_full(_heap_)) \
{ \
if (!PFX##_impl_grow(_heap_)) \
if (!PFX##_resize(_heap_, PFX##_count(_heap_) * 2)) \
return false; \
} \
\
@@ -275,14 +274,6 @@ typedef enum cmc_heap_order_e
return _heap_->buffer[0]; \
} \
\
V *PFX##_peek_ref(SNAME *_heap_) \
{ \
if (PFX##_empty(_heap_)) \
return NULL; \
\
return &(_heap_->buffer[0]); \
} \
\
bool PFX##_contains(SNAME *_heap_, V element) \
{ \
for (size_t i = 0; i < _heap_->count; i++) \
@@ -314,6 +305,22 @@ typedef enum cmc_heap_order_e
return _heap_->capacity; \
} \
\
bool PFX##_resize(SNAME *_heap_, size_t capacity) \
{ \
if (capacity < PFX##_count(_heap_)) \
return false; \
\
V *new_buffer = realloc(_heap_->buffer, sizeof(V) * capacity); \
\
if (!new_buffer) \
return false; \
\
_heap_->buffer = new_buffer; \
_heap_->capacity = capacity; \
\
return true; \
} \
\
SNAME *PFX##_copy_of(SNAME *_heap_, V (*copy_func)(V)) \
{ \
SNAME *result = PFX##_new(_heap_->capacity, _heap_->HO, _heap_->cmp); \
@@ -526,21 +533,6 @@ typedef enum cmc_heap_order_e
return iter->cursor; \
} \
\
static bool PFX##_impl_grow(SNAME *_heap_) \
{ \
size_t new_capacity = _heap_->capacity * 2; \
\
V *new_buffer = realloc(_heap_->buffer, sizeof(V) * new_capacity); \
\
if (!new_buffer) \
return false; \
\
_heap_->buffer = new_buffer; \
_heap_->capacity = new_capacity; \
\
return true; \
} \
\
static bool PFX##_impl_float_up(SNAME *_heap_, size_t index) \
{ \
/* Current index */ \
+617 -624
View File
File diff suppressed because it is too large. Load diff