Fixing tests and added linkedqueue tests
This commit is contained in:
1 parent
7ecb2e90fd
commit
9eb8d5f682
15 files changed
+331
-57
No files matched your search
@@ -9,6 +9,7 @@
|
||||
#include "unt/treemap.c"
|
||||
#include "unt/treeset.c"
|
||||
|
||||
#include "unt/linkedqueue.c"
|
||||
#include "unt/multimap.c"
|
||||
|
||||
int main(void)
|
||||
@@ -25,6 +26,7 @@ int main(void)
|
||||
treeset_test();
|
||||
|
||||
/* ext */
|
||||
linkedqueue_test();
|
||||
multimap_test();
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "cmc/deque.h"
|
||||
|
||||
//DEQUE_GENERATE(d, deque, , size_t)
|
||||
|
||||
typedef struct deque_s
|
||||
{
|
||||
size_t *buffer;
|
||||
|
||||
+13
-11
@@ -1,5 +1,7 @@
|
||||
#include "cmc/hashmap.h"
|
||||
|
||||
//HASHMAP_GENERATE(hm, hashmap, , size_t, size_t)
|
||||
|
||||
typedef struct hashmap_s
|
||||
{
|
||||
struct hashmap_entry_s *buffer;
|
||||
@@ -16,7 +18,7 @@ typedef struct hashmap_entry_s
|
||||
size_t key;
|
||||
size_t value;
|
||||
size_t dist;
|
||||
enum EntryState_e state;
|
||||
enum cmc_entry_state_e state;
|
||||
} hashmap_entry, *hashmap_entry_ptr;
|
||||
typedef struct hashmap_iter_s
|
||||
{
|
||||
@@ -120,12 +122,12 @@ bool hm_insert(hashmap *_map_, size_t key, size_t value)
|
||||
hashmap_entry *target = &(_map_->buffer[pos]);
|
||||
if (hm_impl_get_entry(_map_, key) != NULL)
|
||||
return false;
|
||||
if (target->state == ES_EMPTY || target->state == ES_DELETED)
|
||||
if (target->state == CMC_ES_EMPTY || target->state == CMC_ES_DELETED)
|
||||
{
|
||||
target->key = key;
|
||||
target->value = value;
|
||||
target->dist = original_pos - pos;
|
||||
target->state = ES_FILLED;
|
||||
target->state = CMC_ES_FILLED;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -133,12 +135,12 @@ bool hm_insert(hashmap *_map_, size_t key, size_t value)
|
||||
{
|
||||
pos++;
|
||||
target = &(_map_->buffer[pos % _map_->capacity]);
|
||||
if (target->state == ES_EMPTY || target->state == ES_DELETED)
|
||||
if (target->state == CMC_ES_EMPTY || target->state == CMC_ES_DELETED)
|
||||
{
|
||||
target->key = key;
|
||||
target->value = value;
|
||||
target->dist = pos - original_pos;
|
||||
target->state = ES_FILLED;
|
||||
target->state = CMC_ES_FILLED;
|
||||
break;
|
||||
}
|
||||
else if (target->dist < original_pos - pos)
|
||||
@@ -167,7 +169,7 @@ bool hm_remove(hashmap *_map_, size_t key, size_t *value)
|
||||
result->key = hm_impl_default_key();
|
||||
result->value = hm_impl_default_value();
|
||||
result->dist = 0;
|
||||
result->state = ES_DELETED;
|
||||
result->state = CMC_ES_DELETED;
|
||||
_map_->count--;
|
||||
return true;
|
||||
}
|
||||
@@ -267,7 +269,7 @@ void hm_iter_init(hashmap_iter *iter, hashmap *target)
|
||||
{
|
||||
for (size_t i = 0; i < target->capacity; i++)
|
||||
{
|
||||
if (target->buffer[i].state == ES_FILLED)
|
||||
if (target->buffer[i].state == CMC_ES_FILLED)
|
||||
{
|
||||
iter->first = i;
|
||||
break;
|
||||
@@ -276,7 +278,7 @@ void hm_iter_init(hashmap_iter *iter, hashmap *target)
|
||||
iter->cursor = iter->first;
|
||||
for (size_t i = target->capacity; i > 0; i--)
|
||||
{
|
||||
if (target->buffer[i - 1].state == ES_FILLED)
|
||||
if (target->buffer[i - 1].state == CMC_ES_FILLED)
|
||||
{
|
||||
iter->last = i - 1;
|
||||
break;
|
||||
@@ -315,7 +317,7 @@ bool hm_iter_next(hashmap_iter *iter)
|
||||
{
|
||||
iter->cursor++;
|
||||
scan = &(iter->target->buffer[iter->cursor]);
|
||||
if (scan->state == ES_FILLED)
|
||||
if (scan->state == CMC_ES_FILLED)
|
||||
break;
|
||||
}
|
||||
iter->start = hm_empty(iter->target);
|
||||
@@ -336,7 +338,7 @@ bool hm_iter_prev(hashmap_iter *iter)
|
||||
{
|
||||
iter->cursor--;
|
||||
scan = &(iter->target->buffer[iter->cursor]);
|
||||
if (scan->state == ES_FILLED)
|
||||
if (scan->state == CMC_ES_FILLED)
|
||||
break;
|
||||
}
|
||||
iter->end = hm_empty(iter->target);
|
||||
@@ -391,7 +393,7 @@ static hashmap_entry *hm_impl_get_entry(hashmap *_map_, size_t key)
|
||||
size_t hash = _map_->hash(key);
|
||||
size_t pos = hash % _map_->capacity;
|
||||
hashmap_entry *target = &(_map_->buffer[pos]);
|
||||
while (target->state == ES_FILLED || target->state == ES_DELETED)
|
||||
while (target->state == CMC_ES_FILLED || target->state == CMC_ES_DELETED)
|
||||
{
|
||||
if (_map_->cmp(target->key, key) == 0)
|
||||
return target;
|
||||
|
||||
+17
-15
@@ -1,5 +1,7 @@
|
||||
#include "cmc/hashset.h"
|
||||
|
||||
//HASHSET_GENERATE(hs, hashset, , size_t)
|
||||
|
||||
typedef struct hashset_s
|
||||
{
|
||||
struct hashset_entry_s *buffer;
|
||||
@@ -15,7 +17,7 @@ typedef struct hashset_entry_s
|
||||
{
|
||||
size_t value;
|
||||
size_t dist;
|
||||
enum EntryState_e state;
|
||||
enum cmc_entry_state_e state;
|
||||
} hashset_entry, *hashset_entry_ptr;
|
||||
typedef struct hashset_iter_s
|
||||
{
|
||||
@@ -118,11 +120,11 @@ bool hs_insert(hashset *_set_, size_t element)
|
||||
hashset_entry *target = &(_set_->buffer[pos]);
|
||||
if (hs_impl_get_entry(_set_, element) != NULL)
|
||||
return false;
|
||||
if (target->state == ES_EMPTY || target->state == ES_DELETED)
|
||||
if (target->state == CMC_ES_EMPTY || target->state == CMC_ES_DELETED)
|
||||
{
|
||||
target->value = element;
|
||||
target->dist = pos - original_pos;
|
||||
target->state = ES_FILLED;
|
||||
target->state = CMC_ES_FILLED;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -130,11 +132,11 @@ bool hs_insert(hashset *_set_, size_t element)
|
||||
{
|
||||
pos++;
|
||||
target = &(_set_->buffer[pos % _set_->capacity]);
|
||||
if (target->state == ES_EMPTY || target->state == ES_DELETED)
|
||||
if (target->state == CMC_ES_EMPTY || target->state == CMC_ES_DELETED)
|
||||
{
|
||||
target->value = element;
|
||||
target->dist = pos - original_pos;
|
||||
target->state = ES_FILLED;
|
||||
target->state = CMC_ES_FILLED;
|
||||
break;
|
||||
}
|
||||
else if (target->dist < pos - original_pos)
|
||||
@@ -158,7 +160,7 @@ bool hs_remove(hashset *_set_, size_t element)
|
||||
return false;
|
||||
result->value = hs_impl_default_value();
|
||||
result->dist = 0;
|
||||
result->state = ES_DELETED;
|
||||
result->state = CMC_ES_DELETED;
|
||||
_set_->count--;
|
||||
return true;
|
||||
}
|
||||
@@ -214,7 +216,7 @@ hashset *hs_union(hashset *_set1_, hashset *_set2_)
|
||||
{
|
||||
hashset *_set_r_ = hs_new(_set1_->capacity, _set1_->load, _set1_->cmp, _set1_->hash);
|
||||
if (!_set_r_)
|
||||
return false;
|
||||
return NULL;
|
||||
hashset_iter iter1, iter2;
|
||||
hs_iter_init(&iter1, _set1_);
|
||||
hs_iter_init(&iter2, _set2_);
|
||||
@@ -232,7 +234,7 @@ hashset *hs_intersection(hashset *_set1_, hashset *_set2_)
|
||||
{
|
||||
hashset *_set_r_ = hs_new(_set1_->capacity, _set1_->load, _set1_->cmp, _set1_->hash);
|
||||
if (!_set_r_)
|
||||
return false;
|
||||
return NULL;
|
||||
hashset *_set_A_ = _set1_->count < _set2_->count ? _set1_ : _set2_;
|
||||
hashset *_set_B_ = _set_A_ == _set1_ ? _set2_ : _set1_;
|
||||
hashset_iter iter;
|
||||
@@ -249,7 +251,7 @@ hashset *hs_difference(hashset *_set1_, hashset *_set2_)
|
||||
{
|
||||
hashset *_set_r_ = hs_new(_set1_->capacity, _set1_->load, _set1_->cmp, _set1_->hash);
|
||||
if (!_set_r_)
|
||||
return false;
|
||||
return NULL;
|
||||
hashset_iter iter;
|
||||
hs_iter_init(&iter, _set1_);
|
||||
for (hs_iter_to_start(&iter); !hs_iter_end(&iter); hs_iter_next(&iter))
|
||||
@@ -265,7 +267,7 @@ hashset *hs_symmetric_difference(hashset *_set1_, hashset *_set2_)
|
||||
hashset_iter iter1, iter2;
|
||||
hashset *_set_r_ = hs_new(_set1_->capacity, _set1_->load, _set1_->cmp, _set1_->hash);
|
||||
if (!_set_r_)
|
||||
return false;
|
||||
return NULL;
|
||||
hs_iter_init(&iter1, _set1_);
|
||||
hs_iter_init(&iter2, _set2_);
|
||||
for (hs_iter_to_start(&iter1); !hs_iter_end(&iter1); hs_iter_next(&iter1))
|
||||
@@ -355,7 +357,7 @@ void hs_iter_init(hashset_iter *iter, hashset *target)
|
||||
{
|
||||
for (size_t i = 0; i < target->capacity; i++)
|
||||
{
|
||||
if (target->buffer[i].state == ES_FILLED)
|
||||
if (target->buffer[i].state == CMC_ES_FILLED)
|
||||
{
|
||||
iter->first = i;
|
||||
break;
|
||||
@@ -364,7 +366,7 @@ void hs_iter_init(hashset_iter *iter, hashset *target)
|
||||
iter->cursor = iter->first;
|
||||
for (size_t i = target->capacity; i > 0; i--)
|
||||
{
|
||||
if (target->buffer[i - 1].state == ES_FILLED)
|
||||
if (target->buffer[i - 1].state == CMC_ES_FILLED)
|
||||
{
|
||||
iter->last = i - 1;
|
||||
break;
|
||||
@@ -402,7 +404,7 @@ bool hs_iter_next(hashset_iter *iter)
|
||||
{
|
||||
iter->cursor++;
|
||||
scan = &(iter->target->buffer[iter->cursor]);
|
||||
if (scan->state == ES_FILLED)
|
||||
if (scan->state == CMC_ES_FILLED)
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -423,7 +425,7 @@ bool hs_iter_prev(hashset_iter *iter)
|
||||
{
|
||||
iter->cursor--;
|
||||
scan = &(iter->target->buffer[iter->cursor]);
|
||||
if (scan->state == ES_FILLED)
|
||||
if (scan->state == CMC_ES_FILLED)
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -465,7 +467,7 @@ static hashset_entry *hs_impl_get_entry(hashset *_set_, size_t element)
|
||||
size_t hash = _set_->hash(element);
|
||||
size_t pos = hash % _set_->capacity;
|
||||
hashset_entry *target = &(_set_->buffer[pos]);
|
||||
while (target->state == ES_FILLED || target->state == ES_DELETED)
|
||||
while (target->state == CMC_ES_FILLED || target->state == CMC_ES_DELETED)
|
||||
{
|
||||
if (_set_->cmp(target->value, element) == 0)
|
||||
return target;
|
||||
|
||||
+13
-22
@@ -1,11 +1,13 @@
|
||||
#include "cmc/heap.h"
|
||||
|
||||
//HEAP_GENERATE(h, heap, , size_t)
|
||||
|
||||
typedef struct heap_s
|
||||
{
|
||||
size_t *buffer;
|
||||
size_t capacity;
|
||||
size_t count;
|
||||
enum HeapOrder HO;
|
||||
enum cmc_heap_order_e HO;
|
||||
int (*cmp)(size_t, size_t);
|
||||
struct heap_iter_s (*it_start)(struct heap_s *);
|
||||
struct heap_iter_s (*it_end)(struct heap_s *);
|
||||
@@ -17,7 +19,7 @@ typedef struct heap_iter_s
|
||||
bool start;
|
||||
bool end;
|
||||
} heap_iter, *heap_iter_ptr;
|
||||
heap *h_new(size_t capacity, HeapOrder HO, int (*compare)(size_t, size_t));
|
||||
heap *h_new(size_t capacity, cmc_heap_order HO, int (*compare)(size_t, size_t));
|
||||
void h_clear(heap *_heap_);
|
||||
void h_free(heap *_heap_);
|
||||
bool h_insert(heap *_heap_, size_t element);
|
||||
@@ -49,18 +51,15 @@ static inline size_t h_impl_default_value(void)
|
||||
return _empty_value_;
|
||||
}
|
||||
static bool h_impl_grow(heap *_heap_);
|
||||
static size_t h_impl_p(size_t index);
|
||||
static size_t h_impl_l(size_t index);
|
||||
static size_t h_impl_r(size_t index);
|
||||
static bool h_impl_float_up(heap *_heap_, size_t index);
|
||||
static bool h_impl_float_down(heap *_heap_, size_t index);
|
||||
static heap_iter h_impl_it_start(heap *_heap_);
|
||||
static heap_iter h_impl_it_end(heap *_heap_);
|
||||
heap *h_new(size_t capacity, HeapOrder HO, int (*compare)(size_t, size_t))
|
||||
heap *h_new(size_t capacity, cmc_heap_order HO, int (*compare)(size_t, size_t))
|
||||
{
|
||||
if (capacity < 1)
|
||||
return NULL;
|
||||
if (HO != MinHeap && HO != MaxHeap)
|
||||
if (HO != cmc_min_heap && HO != cmc_max_heap)
|
||||
return NULL;
|
||||
heap *_heap_ = malloc(sizeof(heap));
|
||||
if (!_heap_)
|
||||
@@ -225,28 +224,20 @@ static bool h_impl_grow(heap *_heap_)
|
||||
_heap_->capacity = new_capacity;
|
||||
return true;
|
||||
}
|
||||
static size_t h_impl_p(size_t index)
|
||||
{
|
||||
if (index == 0)
|
||||
return 0;
|
||||
return (index - 1) / 2;
|
||||
}
|
||||
static size_t h_impl_l(size_t index) { return 2 * index + 1; }
|
||||
static size_t h_impl_r(size_t index) { return 2 * index + 2; }
|
||||
static bool h_impl_float_up(heap *_heap_, size_t index)
|
||||
{
|
||||
size_t C = index;
|
||||
size_t child = _heap_->buffer[C];
|
||||
size_t parent = _heap_->buffer[h_impl_p(C)];
|
||||
size_t parent = _heap_->buffer[(index - 1) / 2];
|
||||
int mod = _heap_->HO;
|
||||
while (C > 0 && _heap_->cmp(child, parent) * mod > 0)
|
||||
{
|
||||
size_t tmp = _heap_->buffer[C];
|
||||
_heap_->buffer[C] = _heap_->buffer[h_impl_p(C)];
|
||||
_heap_->buffer[h_impl_p(C)] = tmp;
|
||||
C = h_impl_p(C);
|
||||
_heap_->buffer[C] = _heap_->buffer[(index - 1) / 2];
|
||||
_heap_->buffer[(index - 1) / 2] = tmp;
|
||||
C = (index - 1) / 2;
|
||||
child = _heap_->buffer[C];
|
||||
parent = _heap_->buffer[h_impl_p(C)];
|
||||
parent = _heap_->buffer[(index - 1) / 2];
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -255,8 +246,8 @@ static bool h_impl_float_down(heap *_heap_, size_t index)
|
||||
int mod = _heap_->HO;
|
||||
while (index < _heap_->count)
|
||||
{
|
||||
size_t L = h_impl_l(index);
|
||||
size_t R = h_impl_r(index);
|
||||
size_t L = 2 * index + 1;
|
||||
size_t R = 2 * index + 2;
|
||||
size_t C = index;
|
||||
if (L < _heap_->count && _heap_->cmp(_heap_->buffer[L], _heap_->buffer[C]) * mod > 0)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "cmc/linkedlist.h"
|
||||
|
||||
//LINKEDLIST_GENERATE(ll, linkedlist, , size_t)
|
||||
|
||||
typedef struct linkedlist_s
|
||||
{
|
||||
struct linkedlist_node_s *head;
|
||||
|
||||
@@ -0,0 +1,220 @@
|
||||
#include "ext/linkedqueue.h"
|
||||
|
||||
//LINKEDQUEUE_GENERATE(lq, linkedqueue, , size_t)
|
||||
|
||||
typedef struct linkedqueue_s
|
||||
{
|
||||
struct linkedqueue_node_s *head;
|
||||
struct linkedqueue_node_s *tail;
|
||||
size_t count;
|
||||
struct linkedqueue_iter_s (*it_start)(struct linkedqueue_s *);
|
||||
} linkedqueue, *linkedqueue_ptr;
|
||||
typedef struct linkedqueue_node_s
|
||||
{
|
||||
size_t data;
|
||||
struct linkedqueue_node_s *next;
|
||||
} linkedqueue_node, *linkedqueue_node_ptr;
|
||||
typedef struct linkedqueue_iter_s
|
||||
{
|
||||
struct linkedqueue_s *target;
|
||||
struct linkedqueue_node_s *cursor;
|
||||
size_t index;
|
||||
bool start;
|
||||
bool end;
|
||||
} linkedqueue_iter, *linkedqueue_iter_ptr;
|
||||
linkedqueue *lq_new(void);
|
||||
void lq_clear(linkedqueue *_lqueue_);
|
||||
void lq_free(linkedqueue *_lqueue_);
|
||||
bool lq_enqueue(linkedqueue *_lqueue_, size_t element);
|
||||
bool lq_dequeue(linkedqueue *_lqueue_);
|
||||
bool lq_enqueue_if(linkedqueue *_lqueue_, size_t element, bool condition);
|
||||
bool lq_dequeue_if(linkedqueue *_lqueue_, bool condition);
|
||||
size_t lq_peek(linkedqueue *_lqueue_);
|
||||
bool lq_contains(linkedqueue *_lqueue_, size_t element, int (*comparator)(size_t, size_t));
|
||||
bool lq_empty(linkedqueue *_lqueue_);
|
||||
size_t lq_count(linkedqueue *_lqueue_);
|
||||
linkedqueue_iter *lq_iter_new(linkedqueue *target);
|
||||
void lq_iter_free(linkedqueue_iter *iter);
|
||||
void lq_iter_init(linkedqueue_iter *iter, linkedqueue *target);
|
||||
bool lq_iter_start(linkedqueue_iter *iter);
|
||||
bool lq_iter_end(linkedqueue_iter *iter);
|
||||
void lq_iter_to_start(linkedqueue_iter *iter);
|
||||
void lq_iter_to_end(linkedqueue_iter *iter);
|
||||
bool lq_iter_next(linkedqueue_iter *iter);
|
||||
size_t lq_iter_value(linkedqueue_iter *iter);
|
||||
size_t *lq_iter_rvalue(linkedqueue_iter *iter);
|
||||
size_t lq_iter_index(linkedqueue_iter *iter);
|
||||
static inline size_t lq_impl_default_value(void)
|
||||
{
|
||||
size_t _empty_value_;
|
||||
memset(&_empty_value_, 0, sizeof(size_t));
|
||||
return _empty_value_;
|
||||
}
|
||||
linkedqueue_node *lq_impl_new_node(size_t element);
|
||||
static linkedqueue_iter lq_impl_it_start(linkedqueue *_lqueue_);
|
||||
linkedqueue *lq_new(void)
|
||||
{
|
||||
linkedqueue *_lqueue_ = malloc(sizeof(linkedqueue));
|
||||
if (!_lqueue_)
|
||||
return NULL;
|
||||
_lqueue_->count = 0;
|
||||
_lqueue_->head = NULL;
|
||||
_lqueue_->tail = NULL;
|
||||
_lqueue_->it_start = lq_impl_it_start;
|
||||
return _lqueue_;
|
||||
}
|
||||
void lq_clear(linkedqueue *_lqueue_)
|
||||
{
|
||||
linkedqueue_node *scan = _lqueue_->head;
|
||||
while (_lqueue_->head != NULL)
|
||||
{
|
||||
_lqueue_->head = _lqueue_->head->next;
|
||||
free(scan);
|
||||
scan = _lqueue_->head;
|
||||
}
|
||||
_lqueue_->count = 0;
|
||||
_lqueue_->head = NULL;
|
||||
_lqueue_->tail = NULL;
|
||||
}
|
||||
void lq_free(linkedqueue *_lqueue_)
|
||||
{
|
||||
lq_clear(_lqueue_);
|
||||
free(_lqueue_);
|
||||
}
|
||||
bool lq_enqueue(linkedqueue *_lqueue_, size_t element)
|
||||
{
|
||||
linkedqueue_node *_node_ = lq_impl_new_node(element);
|
||||
if (!_node_)
|
||||
return false;
|
||||
if (lq_empty(_lqueue_))
|
||||
{
|
||||
_lqueue_->head = _node_;
|
||||
_lqueue_->tail = _node_;
|
||||
}
|
||||
else
|
||||
{
|
||||
_lqueue_->tail->next = _node_;
|
||||
_lqueue_->tail = _node_;
|
||||
}
|
||||
_lqueue_->count++;
|
||||
return true;
|
||||
}
|
||||
bool lq_dequeue(linkedqueue *_lqueue_)
|
||||
{
|
||||
if (lq_empty(_lqueue_))
|
||||
return false;
|
||||
linkedqueue_node *_node_ = _lqueue_->head;
|
||||
_lqueue_->head = _lqueue_->head->next;
|
||||
free(_node_);
|
||||
if (_lqueue_->head == NULL)
|
||||
_lqueue_->tail = NULL;
|
||||
_lqueue_->count--;
|
||||
return true;
|
||||
}
|
||||
bool lq_enqueue_if(linkedqueue *_lqueue_, size_t element, bool condition)
|
||||
{
|
||||
if (condition)
|
||||
return lq_enqueue(_lqueue_, element);
|
||||
return false;
|
||||
}
|
||||
bool lq_dequeue_if(linkedqueue *_lqueue_, bool condition)
|
||||
{
|
||||
if (condition)
|
||||
return lq_dequeue(_lqueue_);
|
||||
return false;
|
||||
}
|
||||
size_t lq_peek(linkedqueue *_lqueue_)
|
||||
{
|
||||
if (lq_empty(_lqueue_))
|
||||
return lq_impl_default_value();
|
||||
return _lqueue_->head->data;
|
||||
}
|
||||
bool lq_contains(linkedqueue *_lqueue_, size_t element, int (*comparator)(size_t, size_t))
|
||||
{
|
||||
linkedqueue_node *scan = _lqueue_->head;
|
||||
while (scan != NULL)
|
||||
{
|
||||
if (comparator(scan->data, element) == 0)
|
||||
return true;
|
||||
scan = scan->next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool lq_empty(linkedqueue *_lqueue_) { return _lqueue_->count == 0; }
|
||||
size_t lq_count(linkedqueue *_lqueue_) { return _lqueue_->count; }
|
||||
linkedqueue_iter *lq_iter_new(linkedqueue *target)
|
||||
{
|
||||
linkedqueue_iter *iter = malloc(sizeof(linkedqueue_iter));
|
||||
if (!iter)
|
||||
return NULL;
|
||||
lq_iter_init(iter, target);
|
||||
return iter;
|
||||
}
|
||||
void lq_iter_free(linkedqueue_iter *iter) { free(iter); }
|
||||
void lq_iter_init(linkedqueue_iter *iter, linkedqueue *target)
|
||||
{
|
||||
iter->target = target;
|
||||
iter->cursor = target->head;
|
||||
iter->index = 0;
|
||||
iter->start = true;
|
||||
iter->end = lq_empty(target);
|
||||
}
|
||||
bool lq_iter_start(linkedqueue_iter *iter) { return lq_empty(iter->target) || iter->start; }
|
||||
bool lq_iter_end(linkedqueue_iter *iter) { return lq_empty(iter->target) || iter->end; }
|
||||
void lq_iter_to_start(linkedqueue_iter *iter)
|
||||
{
|
||||
iter->cursor = iter->target->head;
|
||||
iter->index = 0;
|
||||
iter->start = true;
|
||||
iter->end = lq_empty(iter->target);
|
||||
}
|
||||
void lq_iter_to_end(linkedqueue_iter *iter)
|
||||
{
|
||||
iter->cursor = iter->target->tail;
|
||||
iter->index = iter->target->count - 1;
|
||||
iter->start = lq_empty(iter->target);
|
||||
iter->end = true;
|
||||
}
|
||||
bool lq_iter_next(linkedqueue_iter *iter)
|
||||
{
|
||||
if (iter->end)
|
||||
return false;
|
||||
iter->start = lq_empty(iter->target);
|
||||
if (iter->cursor->next == NULL)
|
||||
iter->end = true;
|
||||
else
|
||||
{
|
||||
iter->cursor = iter->cursor->next;
|
||||
iter->index++;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
size_t lq_iter_value(linkedqueue_iter *iter)
|
||||
{
|
||||
if (lq_empty(iter->target))
|
||||
return lq_impl_default_value();
|
||||
return iter->cursor->data;
|
||||
}
|
||||
size_t *lq_iter_rvalue(linkedqueue_iter *iter)
|
||||
{
|
||||
if (lq_empty(iter->target))
|
||||
return NULL;
|
||||
return &(iter->cursor->data);
|
||||
}
|
||||
size_t lq_iter_index(linkedqueue_iter *iter) { return iter->index; }
|
||||
linkedqueue_node *lq_impl_new_node(size_t element)
|
||||
{
|
||||
linkedqueue_node *_node_ = malloc(sizeof(linkedqueue_node));
|
||||
if (!_node_)
|
||||
return NULL;
|
||||
_node_->data = element;
|
||||
_node_->next = NULL;
|
||||
return _node_;
|
||||
}
|
||||
static linkedqueue_iter lq_impl_it_start(linkedqueue *_lqueue_)
|
||||
{
|
||||
linkedqueue_iter iter;
|
||||
lq_iter_init(&iter, _lqueue_);
|
||||
lq_iter_to_start(&iter);
|
||||
return iter;
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "cmc/list.h"
|
||||
|
||||
//LIST_GENERATE(l, list, , size_t)
|
||||
|
||||
typedef struct list_s
|
||||
{
|
||||
size_t *buffer;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "ext/multimap.h"
|
||||
|
||||
//MULTIMAP_GENERATE(mm, multimap, , size_t, size_t)
|
||||
|
||||
typedef struct multimap_s
|
||||
{
|
||||
struct multimap_entry_s *(*buffer)[2];
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "cmc/queue.h"
|
||||
|
||||
//QUEUE_GENERATE(q, queue, , size_t)
|
||||
|
||||
typedef struct queue_s
|
||||
{
|
||||
size_t *buffer;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "cmc/stack.h"
|
||||
|
||||
//STACK_GENERATE(s, stack, , size_t)
|
||||
|
||||
typedef struct stack_s
|
||||
{
|
||||
size_t *buffer;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "cmc/treemap.h"
|
||||
|
||||
//TREEMAP_GENERATE(tm, treemap, , size_t, size_t)
|
||||
|
||||
typedef struct treemap_s
|
||||
{
|
||||
struct treemap_node_s *root;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "cmc/treeset.h"
|
||||
|
||||
//TREESET_GENERATE(ts, treeset, , size_t)
|
||||
|
||||
typedef struct treeset_s
|
||||
{
|
||||
struct treeset_node_s *root;
|
||||
@@ -331,7 +333,7 @@ treeset *ts_union(treeset *_set1_, treeset *_set2_)
|
||||
{
|
||||
treeset *_set_r_ = ts_new(_set1_->cmp);
|
||||
if (!_set_r_)
|
||||
return false;
|
||||
return NULL;
|
||||
treeset_iter iter1, iter2;
|
||||
ts_iter_init(&iter1, _set1_);
|
||||
ts_iter_init(&iter2, _set2_);
|
||||
@@ -349,7 +351,7 @@ treeset *ts_intersection(treeset *_set1_, treeset *_set2_)
|
||||
{
|
||||
treeset *_set_r_ = ts_new(_set1_->cmp);
|
||||
if (!_set_r_)
|
||||
return false;
|
||||
return NULL;
|
||||
treeset *_set_A_ = _set1_->count < _set2_->count ? _set1_ : _set2_;
|
||||
treeset *_set_B_ = _set_A_ == _set1_ ? _set2_ : _set1_;
|
||||
treeset_iter iter;
|
||||
@@ -366,7 +368,7 @@ treeset *ts_difference(treeset *_set1_, treeset *_set2_)
|
||||
{
|
||||
treeset *_set_r_ = ts_new(_set1_->cmp);
|
||||
if (!_set_r_)
|
||||
return false;
|
||||
return NULL;
|
||||
treeset_iter iter;
|
||||
ts_iter_init(&iter, _set1_);
|
||||
for (ts_iter_to_start(&iter); !ts_iter_end(&iter); ts_iter_next(&iter))
|
||||
@@ -381,7 +383,7 @@ treeset *ts_symmetric_difference(treeset *_set1_, treeset *_set2_)
|
||||
{
|
||||
treeset *_set_r_ = ts_new(_set1_->cmp);
|
||||
if (!_set_r_)
|
||||
return false;
|
||||
return NULL;
|
||||
treeset_iter iter1, iter2;
|
||||
ts_iter_init(&iter1, _set1_);
|
||||
ts_iter_init(&iter2, _set2_);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
CMC_CREATE_UNIT(heap_test, true, {
|
||||
CMC_CREATE_TEST(new, {
|
||||
heap *h = h_new(1000000, MaxHeap, cmp);
|
||||
heap *h = h_new(1000000, cmc_max_heap, cmp);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
@@ -22,7 +22,7 @@ CMC_CREATE_UNIT(heap_test, true, {
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(new[edge_case:capacity = 0], {
|
||||
heap *h = h_new(0, MaxHeap, cmp);
|
||||
heap *h = h_new(0, cmc_max_heap, cmp);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, h);
|
||||
|
||||
@@ -30,7 +30,7 @@ CMC_CREATE_UNIT(heap_test, true, {
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(new[edge_case:capacity = UINT64_MAX], {
|
||||
heap *h = h_new(UINT64_MAX, MaxHeap, cmp);
|
||||
heap *h = h_new(UINT64_MAX, cmc_max_heap, cmp);
|
||||
|
||||
cmc_assert_equals(ptr, NULL, h);
|
||||
|
||||
@@ -38,7 +38,7 @@ CMC_CREATE_UNIT(heap_test, true, {
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(clear[count capacity], {
|
||||
heap *h = h_new(100, MaxHeap, cmp);
|
||||
heap *h = h_new(100, cmc_max_heap, cmp);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
@@ -58,7 +58,7 @@ CMC_CREATE_UNIT(heap_test, true, {
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(buffer_growth[edge_case:capacity = 1], {
|
||||
heap *h = h_new(1, MaxHeap, cmp);
|
||||
heap *h = h_new(1, cmc_max_heap, cmp);
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, h);
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "utl/assert.h"
|
||||
#include "utl/test.h"
|
||||
#include "../src/linkedqueue.c"
|
||||
|
||||
CMC_CREATE_UNIT(linkedqueue_test, true, {
|
||||
CMC_CREATE_TEST(new, {
|
||||
linkedqueue *q = lq_new();
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
bool passed = q->count == 0 && !q->head && !q->tail;
|
||||
|
||||
cmc_assert_equals(size_t, 0, lq_count(q));
|
||||
cmc_assert_equals(ptr, NULL, q->head);
|
||||
cmc_assert_equals(ptr, NULL, q->tail);
|
||||
|
||||
CMC_TEST_PASS_ELSE_FAIL(passed);
|
||||
|
||||
lq_free(q);
|
||||
});
|
||||
|
||||
CMC_CREATE_TEST(clear[count], {
|
||||
linkedqueue *q = lq_new();
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, q);
|
||||
|
||||
for (size_t i = 0; i < 50; i++)
|
||||
lq_enqueue(q, i);
|
||||
|
||||
cmc_assert_equals(size_t, 50, lq_count(q));
|
||||
|
||||
lq_clear(q);
|
||||
|
||||
cmc_assert_equals(size_t, 0, lq_count(q));
|
||||
|
||||
CMC_TEST_PASS_ELSE_FAIL(lq_count(q) == 0);
|
||||
|
||||
lq_free(q);
|
||||
});
|
||||
|
||||
});
|
||||
Reference in new issue
Block a user