Massive changes, unit tests halted

Code format changes that significantly reduce file sizes
Working towards many new features at the same time
Callbacks are going to be optional
Everything is still breaking
This commit is contained in:
Leonardo Vencovsky committed 2020-06-04 17:20:47 -03:00
1 parent 770ec1c4c7
commit aa033b0316
71 files changed
+19023 -21074

No files matched your search

+11 -19
View File
@@ -20,15 +20,12 @@
#include "unt/foreach.c"
#define cmc_run(unit, unit_fails, test_fails) \
do \
{ \
uintmax_t f = unit(); \
\
if (f != 0) \
unit_fails += 1; \
\
test_fails += f; \
\
do \
{ \
uintmax_t f = unit(); \
if (f != 0) \
unit_fails += 1; \
test_fails += f; \
} while (0)
int main(void)
@@ -86,19 +83,14 @@ int main(void)
cmc_timer_stop(timer);
printf(
" +---------------------------------------------------------------+\n");
printf(
" | SUMMARY |\n");
printf(
" +---------------------------------------------------------------+\n");
printf(" +---------------------------------------------------------------+\n");
printf(" | SUMMARY |\n");
printf(" +---------------------------------------------------------------+\n");
printf(" | Total Units Failed : %25" PRIuMAX " units |\n", units);
printf(" | Total Tests Failed : %25" PRIuMAX " tests |\n", tests);
printf(" | Total Asserts Failed : %25" PRIuMAX " asserts |\n",
cmc_assert_failed);
printf(" | Total Asserts Failed : %25" PRIuMAX " asserts |\n", cmc_assert_failed);
printf(" | Total Runtime : %25.0lf milliseconds |\n", timer.result);
printf(
" +---------------------------------------------------------------+\n");
printf(" +---------------------------------------------------------------+\n");
return tests;
}
+10 -21
View File
@@ -19,15 +19,12 @@ struct bitset_iter
_Bool end;
};
struct bitset *bs_new(size_t n_bits);
struct bitset *bs_new_custom(size_t n_bits, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
struct bitset *bs_new_custom(size_t n_bits, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
struct bitset bs_init(size_t n_bits);
struct bitset bs_init_custom(size_t n_bits, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
struct bitset bs_init_custom(size_t n_bits, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
void bs_free(struct bitset *_bitset_);
void bs_release(struct bitset _bitset_);
void bs_customize(struct bitset *_bitset_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void bs_customize(struct bitset *_bitset_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
_Bool bs_set(struct bitset *_bitset_, size_t bit_index);
_Bool bs_set_range(struct bitset *_bitset_, size_t from, size_t to);
_Bool bs_clear(struct bitset *_bitset_, size_t bit_index);
@@ -35,8 +32,7 @@ _Bool bs_clear_range(struct bitset *_bitset_, size_t from, size_t to);
_Bool bs_flip(struct bitset *_bitset_, size_t bit_index);
_Bool bs_flip_range(struct bitset *_bitset_, size_t from, size_t to);
_Bool bs_put(struct bitset *_bitset_, size_t bit_index, _Bool state);
_Bool bs_put_range(struct bitset *_bitset_, size_t from, size_t to,
_Bool state);
_Bool bs_put_range(struct bitset *_bitset_, size_t from, size_t to, _Bool state);
_Bool bs_set_all(struct bitset *_bitset_);
_Bool bs_clear_all(struct bitset *_bitset_);
_Bool bs_flip_all(struct bitset *_bitset_);
@@ -47,9 +43,7 @@ static inline size_t bs_bit_to_index(size_t idx)
static const size_t shift =
((sizeof(cmc_bitset_word) * 8) >> 6) > 0
? 6
: ((sizeof(cmc_bitset_word) * 8) >> 5) > 0
? 5
: ((sizeof(cmc_bitset_word) * 8) >> 4) > 0 ? 4 : 3;
: ((sizeof(cmc_bitset_word) * 8) >> 5) > 0 ? 5 : ((sizeof(cmc_bitset_word) * 8) >> 4) > 0 ? 4 : 3;
return idx >> shift;
}
_Bool bs_impl_resize(struct bitset *_bitset_, size_t n_bits, _Bool do_resize);
@@ -57,8 +51,7 @@ struct bitset *bs_new(size_t n_bits)
{
return bs_new_custom(n_bits, ((void *)0), ((void *)0));
}
struct bitset *bs_new_custom(size_t n_bits, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
struct bitset *bs_new_custom(size_t n_bits, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (n_bits < 1)
return ((void *)0);
@@ -84,8 +77,7 @@ struct bitset bs_init(size_t n_bits)
{
return bs_init_custom(n_bits, ((void *)0), ((void *)0));
}
struct bitset bs_init_custom(size_t n_bits, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
struct bitset bs_init_custom(size_t n_bits, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
struct bitset _bitset_ = { 0 };
if (n_bits < 1)
@@ -111,8 +103,7 @@ void bs_release(struct bitset _bitset_)
{
free(_bitset_.buffer);
}
void bs_customize(struct bitset *_bitset_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
void bs_customize(struct bitset *_bitset_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!alloc)
_bitset_->alloc = &cmc_alloc_node_default;
@@ -317,8 +308,7 @@ _Bool bs_impl_resize(struct bitset *_bitset_, size_t n_bits, _Bool do_resize)
size_t words = bs_bit_to_index(n_bits - 1) + 1;
if (!do_resize && words <= _bitset_->capacity)
return 1;
cmc_bitset_word *new_buffer =
realloc(_bitset_->buffer, words * sizeof(cmc_bitset_word));
cmc_bitset_word *new_buffer = realloc(_bitset_->buffer, words * sizeof(cmc_bitset_word));
if (!new_buffer)
{
_bitset_->flag = CMC_FLAG_ALLOC;
@@ -326,8 +316,7 @@ _Bool bs_impl_resize(struct bitset *_bitset_, size_t n_bits, _Bool do_resize)
}
_bitset_->buffer = new_buffer;
if (_bitset_->capacity < words)
memset(_bitset_->buffer + _bitset_->capacity, 0,
(words - _bitset_->capacity) * sizeof(cmc_bitset_word));
memset(_bitset_->buffer + _bitset_->capacity, 0, (words - _bitset_->capacity) * sizeof(cmc_bitset_word));
_bitset_->capacity = words;
if (_bitset_->callbacks && _bitset_->callbacks->resize)
_bitset_->callbacks->resize();
+12 -24
View File
@@ -33,14 +33,12 @@ struct deque_iter
_Bool end;
};
struct deque *d_new(size_t capacity, struct deque_fval *f_val);
struct deque *d_new_custom(size_t capacity, struct deque_fval *f_val,
struct cmc_alloc_node *alloc,
struct deque *d_new_custom(size_t capacity, struct deque_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void d_clear(struct deque *_deque_);
void d_free(struct deque *_deque_);
void d_release(struct deque _deque_);
void d_customize(struct deque *_deque_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void d_customize(struct deque *_deque_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
_Bool d_push_front(struct deque *_deque_, size_t value);
_Bool d_push_back(struct deque *_deque_, size_t value);
_Bool d_pop_front(struct deque *_deque_);
@@ -76,8 +74,7 @@ struct deque *d_new(size_t capacity, struct deque_fval *f_val)
{
return d_new_custom(capacity, f_val, ((void *)0), ((void *)0));
}
struct deque *d_new_custom(size_t capacity, struct deque_fval *f_val,
struct cmc_alloc_node *alloc,
struct deque *d_new_custom(size_t capacity, struct deque_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
{
if (capacity < 1)
@@ -146,8 +143,7 @@ void d_release(struct deque _deque_)
}
_deque_.alloc->free(_deque_.buffer);
}
void d_customize(struct deque *_deque_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
void d_customize(struct deque *_deque_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!alloc)
_deque_->alloc = &cmc_alloc_node_default;
@@ -163,8 +159,7 @@ _Bool d_push_front(struct deque *_deque_, size_t value)
if (!d_resize(_deque_, _deque_->capacity * 2))
return 0;
}
_deque_->front =
(_deque_->front == 0) ? _deque_->capacity - 1 : _deque_->front - 1;
_deque_->front = (_deque_->front == 0) ? _deque_->capacity - 1 : _deque_->front - 1;
_deque_->buffer[_deque_->front] = value;
_deque_->count++;
_deque_->flag = CMC_FLAG_OK;
@@ -180,8 +175,7 @@ _Bool d_push_back(struct deque *_deque_, size_t value)
return 0;
}
_deque_->buffer[_deque_->back] = value;
_deque_->back =
(_deque_->back == _deque_->capacity - 1) ? 0 : _deque_->back + 1;
_deque_->back = (_deque_->back == _deque_->capacity - 1) ? 0 : _deque_->back + 1;
_deque_->count++;
_deque_->flag = CMC_FLAG_OK;
if (_deque_->callbacks && _deque_->callbacks->create)
@@ -196,8 +190,7 @@ _Bool d_pop_front(struct deque *_deque_)
return 0;
}
_deque_->buffer[_deque_->front] = (size_t){ 0 };
_deque_->front =
(_deque_->front == _deque_->capacity - 1) ? 0 : _deque_->front + 1;
_deque_->front = (_deque_->front == _deque_->capacity - 1) ? 0 : _deque_->front + 1;
_deque_->count--;
_deque_->flag = CMC_FLAG_OK;
if (_deque_->callbacks && _deque_->callbacks->delete)
@@ -211,8 +204,7 @@ _Bool d_pop_back(struct deque *_deque_)
_deque_->flag = CMC_FLAG_EMPTY;
return 0;
}
_deque_->back =
(_deque_->back == 0) ? _deque_->capacity - 1 : _deque_->back - 1;
_deque_->back = (_deque_->back == 0) ? _deque_->capacity - 1 : _deque_->back - 1;
_deque_->buffer[_deque_->back] = (size_t){ 0 };
_deque_->count--;
_deque_->flag = CMC_FLAG_OK;
@@ -242,8 +234,7 @@ size_t d_back(struct deque *_deque_)
_deque_->flag = CMC_FLAG_OK;
if (_deque_->callbacks && _deque_->callbacks->read)
_deque_->callbacks->read();
return _deque_->buffer[(_deque_->back == 0) ? _deque_->capacity - 1
: _deque_->back - 1];
return _deque_->buffer[(_deque_->back == 0) ? _deque_->capacity - 1 : _deque_->back - 1];
}
_Bool d_contains(struct deque *_deque_, size_t value)
{
@@ -315,8 +306,7 @@ success:
}
struct deque *d_copy_of(struct deque *_deque_)
{
struct deque *result = d_new_custom(_deque_->capacity, _deque_->f_val,
_deque_->alloc, _deque_->callbacks);
struct deque *result = d_new_custom(_deque_->capacity, _deque_->f_val, _deque_->alloc, _deque_->callbacks);
if (!result)
{
_deque_->flag = CMC_FLAG_ERROR;
@@ -351,8 +341,7 @@ _Bool d_equals(struct deque *_deque1_, struct deque *_deque2_)
if (_deque1_->count != _deque2_->count)
return 0;
size_t i, j, k;
for (i = _deque1_->front, j = _deque2_->front, k = 0; k < _deque1_->count;
k++)
for (i = _deque1_->front, j = _deque2_->front, k = 0; k < _deque1_->count; k++)
{
if (_deque1_->f_val->cmp(_deque1_->buffer[i], _deque2_->buffer[j]) != 0)
return 0;
@@ -451,8 +440,7 @@ _Bool d_iter_prev(struct deque_iter *iter)
return 0;
}
iter->end = d_empty(iter->target);
iter->cursor =
(iter->cursor == 0) ? iter->target->capacity - 1 : iter->cursor - 1;
iter->cursor = (iter->cursor == 0) ? iter->target->capacity - 1 : iter->cursor - 1;
iter->index--;
return 1;
}
+39 -107
View File
@@ -52,25 +52,19 @@ struct hashbidimap_iter
_Bool start;
_Bool end;
};
struct hashbidimap *hbm_new(size_t capacity, double load,
struct hashbidimap_fkey *f_key,
struct hashbidimap *hbm_new(size_t capacity, double load, struct hashbidimap_fkey *f_key,
struct hashbidimap_fval *f_val);
struct hashbidimap *hbm_new_custom(size_t capacity, double load,
struct hashbidimap_fkey *f_key,
struct hashbidimap_fval *f_val,
struct cmc_alloc_node *alloc,
struct hashbidimap *hbm_new_custom(size_t capacity, double load, struct hashbidimap_fkey *f_key,
struct hashbidimap_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void hbm_clear(struct hashbidimap *_map_);
void hbm_free(struct hashbidimap *_map_);
void hbm_customize(struct hashbidimap *_map_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void hbm_customize(struct hashbidimap *_map_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
_Bool hbm_insert(struct hashbidimap *_map_, size_t key, size_t value);
_Bool hbm_update_key(struct hashbidimap *_map_, size_t val, size_t new_key);
_Bool hbm_update_val(struct hashbidimap *_map_, size_t key, size_t new_val);
_Bool hbm_remove_by_key(struct hashbidimap *_map_, size_t key, size_t *out_key,
size_t *out_val);
_Bool hbm_remove_by_val(struct hashbidimap *_map_, size_t val, size_t *out_key,
size_t *out_val);
_Bool hbm_remove_by_key(struct hashbidimap *_map_, size_t key, size_t *out_key, size_t *out_val);
_Bool hbm_remove_by_val(struct hashbidimap *_map_, size_t val, size_t *out_key, size_t *out_val);
size_t hbm_get_key(struct hashbidimap *_map_, size_t val);
size_t hbm_get_val(struct hashbidimap *_map_, size_t key);
_Bool hbm_contains_key(struct hashbidimap *_map_, size_t key);
@@ -101,23 +95,15 @@ _Bool hbm_iter_go_to(struct hashbidimap_iter *iter, size_t index);
size_t hbm_iter_key(struct hashbidimap_iter *iter);
size_t hbm_iter_value(struct hashbidimap_iter *iter);
size_t hbm_iter_index(struct hashbidimap_iter *iter);
static struct hashbidimap_entry *hbm_impl_new_entry(struct hashbidimap *_map_,
size_t key, size_t value);
static struct hashbidimap_entry **
hbm_impl_get_entry_by_key(struct hashbidimap *_map_, size_t key);
static struct hashbidimap_entry **
hbm_impl_get_entry_by_val(struct hashbidimap *_map_, size_t val);
static struct hashbidimap_entry **
hbm_impl_add_entry_to_key(struct hashbidimap *_map_,
struct hashbidimap_entry *entry);
static struct hashbidimap_entry **
hbm_impl_add_entry_to_val(struct hashbidimap *_map_,
struct hashbidimap_entry *entry);
static struct hashbidimap_entry *hbm_impl_new_entry(struct hashbidimap *_map_, size_t key, size_t value);
static struct hashbidimap_entry **hbm_impl_get_entry_by_key(struct hashbidimap *_map_, size_t key);
static struct hashbidimap_entry **hbm_impl_get_entry_by_val(struct hashbidimap *_map_, size_t val);
static struct hashbidimap_entry **hbm_impl_add_entry_to_key(struct hashbidimap *_map_, struct hashbidimap_entry *entry);
static struct hashbidimap_entry **hbm_impl_add_entry_to_val(struct hashbidimap *_map_, struct hashbidimap_entry *entry);
static size_t hbm_impl_calculate_size(size_t required);
static struct hashbidimap_iter hbm_impl_it_start(struct hashbidimap *_map_);
static struct hashbidimap_iter hbm_impl_it_end(struct hashbidimap *_map_);
struct hashbidimap *hbm_new(size_t capacity, double load,
struct hashbidimap_fkey *f_key,
struct hashbidimap *hbm_new(size_t capacity, double load, struct hashbidimap_fkey *f_key,
struct hashbidimap_fval *f_val)
{
struct cmc_alloc_node *alloc = &cmc_alloc_node_default;
@@ -131,8 +117,7 @@ struct hashbidimap *hbm_new(size_t capacity, double load,
struct hashbidimap *_map_ = alloc->malloc(sizeof(struct hashbidimap));
if (!_map_)
return ((void *)0);
_map_->buffer =
alloc->calloc(real_capacity, sizeof(struct hashbidimap_entry *[2]));
_map_->buffer = alloc->calloc(real_capacity, sizeof(struct hashbidimap_entry *[2]));
if (!_map_->buffer)
{
alloc->free(_map_->buffer);
@@ -151,10 +136,8 @@ struct hashbidimap *hbm_new(size_t capacity, double load,
_map_->it_end = hbm_impl_it_end;
return _map_;
}
struct hashbidimap *hbm_new_custom(size_t capacity, double load,
struct hashbidimap_fkey *f_key,
struct hashbidimap_fval *f_val,
struct cmc_alloc_node *alloc,
struct hashbidimap *hbm_new_custom(size_t capacity, double load, struct hashbidimap_fkey *f_key,
struct hashbidimap_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
{
if (capacity == 0 || load <= 0 || load >= 1)
@@ -169,8 +152,7 @@ struct hashbidimap *hbm_new_custom(size_t capacity, double load,
struct hashbidimap *_map_ = alloc->malloc(sizeof(struct hashbidimap));
if (!_map_)
return ((void *)0);
_map_->buffer =
alloc->calloc(real_capacity, sizeof(struct hashbidimap_entry *[2]));
_map_->buffer = alloc->calloc(real_capacity, sizeof(struct hashbidimap_entry *[2]));
if (!_map_->buffer)
{
alloc->free(_map_->buffer);
@@ -215,8 +197,7 @@ void hbm_free(struct hashbidimap *_map_)
_map_->alloc->free(_map_->buffer);
_map_->alloc->free(_map_);
}
void hbm_customize(struct hashbidimap *_map_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
void hbm_customize(struct hashbidimap *_map_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!alloc)
_map_->alloc = &cmc_alloc_node_default;
@@ -232,17 +213,14 @@ _Bool hbm_insert(struct hashbidimap *_map_, size_t key, size_t value)
if (!hbm_resize(_map_, _map_->capacity + 1))
return 0;
}
if (hbm_impl_get_entry_by_key(_map_, key) != ((void *)0) ||
hbm_impl_get_entry_by_val(_map_, value) != ((void *)0))
if (hbm_impl_get_entry_by_key(_map_, key) != ((void *)0) || hbm_impl_get_entry_by_val(_map_, value) != ((void *)0))
{
_map_->flag = CMC_FLAG_DUPLICATE;
return 0;
}
struct hashbidimap_entry *entry = hbm_impl_new_entry(_map_, key, value);
struct hashbidimap_entry **key_entry =
hbm_impl_add_entry_to_key(_map_, entry);
struct hashbidimap_entry **val_entry =
hbm_impl_add_entry_to_val(_map_, entry);
struct hashbidimap_entry **key_entry = hbm_impl_add_entry_to_key(_map_, entry);
struct hashbidimap_entry **val_entry = hbm_impl_add_entry_to_val(_map_, entry);
if (!key_entry || !val_entry)
{
if (key_entry)
@@ -347,8 +325,7 @@ success:
_map_->callbacks->update();
return 1;
}
_Bool hbm_remove_by_key(struct hashbidimap *_map_, size_t key, size_t *out_key,
size_t *out_val)
_Bool hbm_remove_by_key(struct hashbidimap *_map_, size_t key, size_t *out_key, size_t *out_val)
{
if (hbm_empty(_map_))
{
@@ -381,8 +358,7 @@ _Bool hbm_remove_by_key(struct hashbidimap *_map_, size_t key, size_t *out_key,
_map_->callbacks->delete ();
return 1;
}
_Bool hbm_remove_by_val(struct hashbidimap *_map_, size_t val, size_t *out_key,
size_t *out_val)
_Bool hbm_remove_by_val(struct hashbidimap *_map_, size_t val, size_t *out_key, size_t *out_val)
{
if (hbm_empty(_map_))
{
@@ -500,8 +476,7 @@ _Bool hbm_resize(struct hashbidimap *_map_, size_t capacity)
return 0;
}
struct hashbidimap *_new_map_ =
hbm_new_custom(capacity, _map_->load, _map_->f_key, _map_->f_val,
_map_->alloc, ((void *)0));
hbm_new_custom(capacity, _map_->load, _map_->f_key, _map_->f_val, _map_->alloc, ((void *)0));
if (!_new_map_)
{
_map_->flag = CMC_FLAG_ALLOC;
@@ -512,10 +487,8 @@ _Bool hbm_resize(struct hashbidimap *_map_, size_t capacity)
struct hashbidimap_entry *scan = _map_->buffer[i][0];
if (scan && scan != ((void *)1))
{
struct hashbidimap_entry **e1 =
hbm_impl_add_entry_to_key(_new_map_, scan);
struct hashbidimap_entry **e2 =
hbm_impl_add_entry_to_val(_new_map_, scan);
struct hashbidimap_entry **e1 = hbm_impl_add_entry_to_key(_new_map_, scan);
struct hashbidimap_entry **e2 = hbm_impl_add_entry_to_val(_new_map_, scan);
if (!e1 || !e2)
{
_new_map_->f_key = &(struct hashbidimap_fkey){ ((void *)0) };
@@ -549,9 +522,8 @@ success:
}
struct hashbidimap *hbm_copy_of(struct hashbidimap *_map_)
{
struct hashbidimap *result =
hbm_new_custom(_map_->capacity * _map_->load, _map_->load, _map_->f_key,
_map_->f_val, _map_->alloc, _map_->callbacks);
struct hashbidimap *result = hbm_new_custom(_map_->capacity * _map_->load, _map_->load, _map_->f_key, _map_->f_val,
_map_->alloc, _map_->callbacks);
if (!result)
{
_map_->flag = CMC_FLAG_ERROR;
@@ -572,8 +544,7 @@ struct hashbidimap *hbm_copy_of(struct hashbidimap *_map_)
tmp_val = _map_->f_val->cpy(scan->value);
else
tmp_val = scan->value;
struct hashbidimap_entry *entry =
hbm_impl_new_entry(result, tmp_key, tmp_val);
struct hashbidimap_entry *entry = hbm_impl_new_entry(result, tmp_key, tmp_val);
hbm_impl_add_entry_to_key(result, entry);
hbm_impl_add_entry_to_val(result, entry);
result->count++;
@@ -605,8 +576,7 @@ _Bool hbm_equals(struct hashbidimap *_map1_, struct hashbidimap *_map2_)
struct hashbidimap_entry *scan = _mapA_->buffer[i][0];
if (scan && scan != ((void *)1))
{
struct hashbidimap_entry **entry_B =
hbm_impl_get_entry_by_key(_mapB_, scan->key);
struct hashbidimap_entry **entry_B = hbm_impl_get_entry_by_key(_mapB_, scan->key);
if (!entry_B)
return 0;
if (_mapA_->f_val->cmp((*entry_B)->value, scan->value) != 0)
@@ -615,36 +585,9 @@ _Bool hbm_equals(struct hashbidimap *_map1_, struct hashbidimap *_map2_)
}
return 1;
}
struct cmc_string hbm_to_string(struct hashbidimap *_map_)
{
struct cmc_string str;
struct hashbidimap *m_ = _map_;
int n = snprintf(str.s, cmc_string_len, cmc_cmc_string_fmt_hashbidimap,
"CMC_PARAM_SNAME((hbm, hashbidimap, , size_t, size_t))",
"CMC_PARAM_K((hbm, hashbidimap, , size_t, size_t))",
"CMC_PARAM_V((hbm, hashbidimap, , size_t, size_t))", m_,
m_->buffer, m_->capacity, m_->count, m_->load, m_->flag,
m_->f_key, m_->f_val, m_->alloc, m_->callbacks);
return n >= 0 ? str : (struct cmc_string){ 0 };
}
_Bool hbm_print(struct hashbidimap *_map_, FILE *fptr)
{
for (size_t i = 0; i < _map_->capacity; i++)
{
struct hashbidimap_entry *target = _map_->buffer[i][0];
if (target && target != ((void *)1))
{
if (!_map_->f_key->str(fptr, target->key) ||
!_map_->f_val->str(fptr, target->value))
return 0;
}
}
return 1;
}
struct hashbidimap_iter *hbm_iter_new(struct hashbidimap *target)
{
struct hashbidimap_iter *iter =
target->alloc->malloc(sizeof(struct hashbidimap_iter));
struct hashbidimap_iter *iter = target->alloc->malloc(sizeof(struct hashbidimap_iter));
if (!iter)
return ((void *)0);
hbm_iter_init(iter, target);
@@ -815,11 +758,9 @@ size_t hbm_iter_index(struct hashbidimap_iter *iter)
{
return iter->index;
}
static struct hashbidimap_entry *hbm_impl_new_entry(struct hashbidimap *_map_,
size_t key, size_t value)
static struct hashbidimap_entry *hbm_impl_new_entry(struct hashbidimap *_map_, size_t key, size_t value)
{
struct hashbidimap_entry *entry =
_map_->alloc->malloc(sizeof(struct hashbidimap_entry));
struct hashbidimap_entry *entry = _map_->alloc->malloc(sizeof(struct hashbidimap_entry));
if (!entry)
return ((void *)0);
entry->key = key;
@@ -830,8 +771,7 @@ static struct hashbidimap_entry *hbm_impl_new_entry(struct hashbidimap *_map_,
entry->ref[1] = ((void *)0);
return entry;
}
static struct hashbidimap_entry **
hbm_impl_get_entry_by_key(struct hashbidimap *_map_, size_t key)
static struct hashbidimap_entry **hbm_impl_get_entry_by_key(struct hashbidimap *_map_, size_t key)
{
size_t hash = _map_->f_key->hash(key);
size_t pos = hash % _map_->capacity;
@@ -845,8 +785,7 @@ hbm_impl_get_entry_by_key(struct hashbidimap *_map_, size_t key)
}
return ((void *)0);
}
static struct hashbidimap_entry **
hbm_impl_get_entry_by_val(struct hashbidimap *_map_, size_t val)
static struct hashbidimap_entry **hbm_impl_get_entry_by_val(struct hashbidimap *_map_, size_t val)
{
size_t hash = _map_->f_val->hash(val);
size_t pos = hash % _map_->capacity;
@@ -860,16 +799,13 @@ hbm_impl_get_entry_by_val(struct hashbidimap *_map_, size_t val)
}
return ((void *)0);
}
static struct hashbidimap_entry **
hbm_impl_add_entry_to_key(struct hashbidimap *_map_,
struct hashbidimap_entry *entry)
static struct hashbidimap_entry **hbm_impl_add_entry_to_key(struct hashbidimap *_map_, struct hashbidimap_entry *entry)
{
struct hashbidimap_entry **to_return = ((void *)0);
size_t hash = _map_->f_key->hash(entry->key);
size_t original_pos = hash % _map_->capacity;
size_t pos = original_pos;
struct hashbidimap_entry **scan =
&(_map_->buffer[hash % _map_->capacity][0]);
struct hashbidimap_entry **scan = &(_map_->buffer[hash % _map_->capacity][0]);
if (*scan == ((void *)0))
{
*scan = entry;
@@ -907,16 +843,13 @@ hbm_impl_add_entry_to_key(struct hashbidimap *_map_,
}
return ((void *)0);
}
static struct hashbidimap_entry **
hbm_impl_add_entry_to_val(struct hashbidimap *_map_,
struct hashbidimap_entry *entry)
static struct hashbidimap_entry **hbm_impl_add_entry_to_val(struct hashbidimap *_map_, struct hashbidimap_entry *entry)
{
struct hashbidimap_entry **to_return = ((void *)0);
size_t hash = _map_->f_val->hash(entry->value);
size_t original_pos = hash % _map_->capacity;
size_t pos = original_pos;
struct hashbidimap_entry **scan =
&(_map_->buffer[hash % _map_->capacity][1]);
struct hashbidimap_entry **scan = &(_map_->buffer[hash % _map_->capacity][1]);
if (*scan == ((void *)0))
{
*scan = entry;
@@ -956,8 +889,7 @@ hbm_impl_add_entry_to_val(struct hashbidimap *_map_,
}
static size_t hbm_impl_calculate_size(size_t required)
{
const size_t count =
sizeof(cmc_hashtable_primes) / sizeof(cmc_hashtable_primes[0]);
const size_t count = sizeof(cmc_hashtable_primes) / sizeof(cmc_hashtable_primes[0]);
if (cmc_hashtable_primes[count - 1] < required)
return required;
size_t i = 0;
+20 -41
View File
@@ -50,21 +50,15 @@ struct hashmap_iter
_Bool start;
_Bool end;
};
struct hashmap *hm_new(size_t capacity, double load, struct hashmap_fkey *f_key,
struct hashmap_fval *f_val);
struct hashmap *hm_new_custom(size_t capacity, double load,
struct hashmap_fkey *f_key,
struct hashmap_fval *f_val,
struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
struct hashmap *hm_new(size_t capacity, double load, struct hashmap_fkey *f_key, struct hashmap_fval *f_val);
struct hashmap *hm_new_custom(size_t capacity, double load, struct hashmap_fkey *f_key, struct hashmap_fval *f_val,
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
void hm_clear(struct hashmap *_map_);
void hm_free(struct hashmap *_map_);
void hm_release(struct hashmap _map_);
void hm_customize(struct hashmap *_map_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void hm_customize(struct hashmap *_map_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
_Bool hm_insert(struct hashmap *_map_, size_t key, size_t value);
_Bool hm_update(struct hashmap *_map_, size_t key, size_t new_value,
size_t *old_value);
_Bool hm_update(struct hashmap *_map_, size_t key, size_t new_value, size_t *old_value);
_Bool hm_remove(struct hashmap *_map_, size_t key, size_t *out_value);
_Bool hm_max(struct hashmap *_map_, size_t *key, size_t *value);
_Bool hm_min(struct hashmap *_map_, size_t *key, size_t *value);
@@ -97,20 +91,14 @@ size_t hm_iter_key(struct hashmap_iter *iter);
size_t hm_iter_value(struct hashmap_iter *iter);
size_t *hm_iter_rvalue(struct hashmap_iter *iter);
size_t hm_iter_index(struct hashmap_iter *iter);
static struct hashmap_entry *hm_impl_get_entry(struct hashmap *_map_,
size_t key);
static struct hashmap_entry *hm_impl_get_entry(struct hashmap *_map_, size_t key);
static size_t hm_impl_calculate_size(size_t required);
struct hashmap *hm_new(size_t capacity, double load, struct hashmap_fkey *f_key,
struct hashmap_fval *f_val)
struct hashmap *hm_new(size_t capacity, double load, struct hashmap_fkey *f_key, struct hashmap_fval *f_val)
{
return hm_new_custom(capacity, load, f_key, f_val, ((void *)0),
((void *)0));
return hm_new_custom(capacity, load, f_key, f_val, ((void *)0), ((void *)0));
}
struct hashmap *hm_new_custom(size_t capacity, double load,
struct hashmap_fkey *f_key,
struct hashmap_fval *f_val,
struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
struct hashmap *hm_new_custom(size_t capacity, double load, struct hashmap_fkey *f_key, struct hashmap_fval *f_val,
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (capacity == 0 || load <= 0 || load >= 1)
return ((void *)0);
@@ -197,8 +185,7 @@ void hm_release(struct hashmap _map_)
}
_map_.alloc->free(_map_.buffer);
}
void hm_customize(struct hashmap *_map_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
void hm_customize(struct hashmap *_map_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!alloc)
_map_->alloc = &cmc_alloc_node_default;
@@ -236,8 +223,7 @@ _Bool hm_insert(struct hashmap *_map_, size_t key, size_t value)
{
pos++;
target = &(_map_->buffer[pos % _map_->capacity]);
if (target->state == CMC_ES_EMPTY ||
target->state == CMC_ES_DELETED)
if (target->state == CMC_ES_EMPTY || target->state == CMC_ES_DELETED)
{
target->key = key;
target->value = value;
@@ -265,8 +251,7 @@ _Bool hm_insert(struct hashmap *_map_, size_t key, size_t value)
_map_->callbacks->create();
return 1;
}
_Bool hm_update(struct hashmap *_map_, size_t key, size_t new_value,
size_t *old_value)
_Bool hm_update(struct hashmap *_map_, size_t key, size_t new_value, size_t *old_value)
{
if (hm_empty(_map_))
{
@@ -457,8 +442,7 @@ _Bool hm_resize(struct hashmap *_map_, size_t capacity)
return 0;
}
struct hashmap *_new_map_ =
hm_new_custom(capacity, _map_->load, _map_->f_key, _map_->f_val,
_map_->alloc, ((void *)0));
hm_new_custom(capacity, _map_->load, _map_->f_key, _map_->f_val, _map_->alloc, ((void *)0));
if (!_new_map_)
{
_map_->flag = CMC_FLAG_ALLOC;
@@ -493,9 +477,8 @@ success:
}
struct hashmap *hm_copy_of(struct hashmap *_map_)
{
struct hashmap *result =
hm_new_custom(_map_->capacity * _map_->load, _map_->load, _map_->f_key,
_map_->f_val, _map_->alloc, _map_->callbacks);
struct hashmap *result = hm_new_custom(_map_->capacity * _map_->load, _map_->load, _map_->f_key, _map_->f_val,
_map_->alloc, _map_->callbacks);
if (!result)
{
_map_->flag = CMC_FLAG_ERROR;
@@ -528,8 +511,7 @@ struct hashmap *hm_copy_of(struct hashmap *_map_)
}
}
else
memcpy(result->buffer, _map_->buffer,
sizeof(struct hashmap_entry) * _map_->capacity);
memcpy(result->buffer, _map_->buffer, sizeof(struct hashmap_entry) * _map_->capacity);
result->count = _map_->count;
_map_->flag = CMC_FLAG_OK;
return result;
@@ -543,8 +525,7 @@ _Bool hm_equals(struct hashmap *_map1_, struct hashmap *_map2_)
struct hashmap_iter iter = hm_iter_start(_map1_);
for (; !hm_iter_at_end(&iter); hm_iter_next(&iter))
{
struct hashmap_entry *entry =
hm_impl_get_entry(_map2_, hm_iter_key(&iter));
struct hashmap_entry *entry = hm_impl_get_entry(_map2_, hm_iter_key(&iter));
if (entry == ((void *)0))
return 0;
if (_map1_->f_val->cmp(entry->value, hm_iter_value(&iter)) != 0)
@@ -753,8 +734,7 @@ size_t hm_iter_index(struct hashmap_iter *iter)
{
return iter->index;
}
static struct hashmap_entry *hm_impl_get_entry(struct hashmap *_map_,
size_t key)
static struct hashmap_entry *hm_impl_get_entry(struct hashmap *_map_, size_t key)
{
size_t hash = _map_->f_key->hash(key);
size_t pos = hash % _map_->capacity;
@@ -770,8 +750,7 @@ static struct hashmap_entry *hm_impl_get_entry(struct hashmap *_map_,
}
static size_t hm_impl_calculate_size(size_t required)
{
const size_t count =
sizeof(cmc_hashtable_primes) / sizeof(cmc_hashtable_primes[0]);
const size_t count = sizeof(cmc_hashtable_primes) / sizeof(cmc_hashtable_primes[0]);
if (cmc_hashtable_primes[count - 1] < required)
return required;
size_t i = 0;
+30 -59
View File
@@ -51,26 +51,19 @@ struct hashmultimap_iter
_Bool start;
_Bool end;
};
struct hashmultimap *hmm_new(size_t capacity, double load,
struct hashmultimap_fkey *f_key,
struct hashmultimap *hmm_new(size_t capacity, double load, struct hashmultimap_fkey *f_key,
struct hashmultimap_fval *f_val);
struct hashmultimap *hmm_new_custom(size_t capacity, double load,
struct hashmultimap_fkey *f_key,
struct hashmultimap_fval *f_val,
struct cmc_alloc_node *alloc,
struct hashmultimap *hmm_new_custom(size_t capacity, double load, struct hashmultimap_fkey *f_key,
struct hashmultimap_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void hmm_clear(struct hashmultimap *_map_);
void hmm_free(struct hashmultimap *_map_);
void hmm_customize(struct hashmultimap *_map_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void hmm_customize(struct hashmultimap *_map_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
_Bool hmm_insert(struct hashmultimap *_map_, size_t key, size_t value);
_Bool hmm_update(struct hashmultimap *_map_, size_t key, size_t new_value,
size_t *old_value);
size_t hmm_update_all(struct hashmultimap *_map_, size_t key, size_t new_value,
size_t **old_values);
_Bool hmm_update(struct hashmultimap *_map_, size_t key, size_t new_value, size_t *old_value);
size_t hmm_update_all(struct hashmultimap *_map_, size_t key, size_t new_value, size_t **old_values);
_Bool hmm_remove(struct hashmultimap *_map_, size_t key, size_t *out_value);
size_t hmm_remove_all(struct hashmultimap *_map_, size_t key,
size_t **out_values);
size_t hmm_remove_all(struct hashmultimap *_map_, size_t key, size_t **out_values);
_Bool hmm_max(struct hashmultimap *_map_, size_t *key, size_t *value);
_Bool hmm_min(struct hashmultimap *_map_, size_t *key, size_t *value);
size_t hmm_get(struct hashmultimap *_map_, size_t key);
@@ -103,14 +96,11 @@ size_t hmm_iter_key(struct hashmultimap_iter *iter);
size_t hmm_iter_value(struct hashmultimap_iter *iter);
size_t *hmm_iter_rvalue(struct hashmultimap_iter *iter);
size_t hmm_iter_index(struct hashmultimap_iter *iter);
struct hashmultimap_entry *hmm_impl_new_entry(struct hashmultimap *_map_,
size_t key, size_t value);
struct hashmultimap_entry *hmm_impl_get_entry(struct hashmultimap *_map_,
size_t key);
struct hashmultimap_entry *hmm_impl_new_entry(struct hashmultimap *_map_, size_t key, size_t value);
struct hashmultimap_entry *hmm_impl_get_entry(struct hashmultimap *_map_, size_t key);
size_t hmm_impl_key_count(struct hashmultimap *_map_, size_t key);
size_t hmm_impl_calculate_size(size_t required);
struct hashmultimap *hmm_new(size_t capacity, double load,
struct hashmultimap_fkey *f_key,
struct hashmultimap *hmm_new(size_t capacity, double load, struct hashmultimap_fkey *f_key,
struct hashmultimap_fval *f_val)
{
struct cmc_alloc_node *alloc = &cmc_alloc_node_default;
@@ -124,8 +114,7 @@ struct hashmultimap *hmm_new(size_t capacity, double load,
struct hashmultimap *_map_ = alloc->malloc(sizeof(struct hashmultimap));
if (!_map_)
return ((void *)0);
_map_->buffer =
alloc->calloc(real_capacity, sizeof(struct hashmultimap_entry *[2]));
_map_->buffer = alloc->calloc(real_capacity, sizeof(struct hashmultimap_entry *[2]));
if (!_map_->buffer)
{
alloc->free(_map_);
@@ -141,10 +130,8 @@ struct hashmultimap *hmm_new(size_t capacity, double load,
_map_->callbacks = ((void *)0);
return _map_;
}
struct hashmultimap *hmm_new_custom(size_t capacity, double load,
struct hashmultimap_fkey *f_key,
struct hashmultimap_fval *f_val,
struct cmc_alloc_node *alloc,
struct hashmultimap *hmm_new_custom(size_t capacity, double load, struct hashmultimap_fkey *f_key,
struct hashmultimap_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
{
if (capacity == 0 || load <= 0)
@@ -159,8 +146,7 @@ struct hashmultimap *hmm_new_custom(size_t capacity, double load,
struct hashmultimap *_map_ = alloc->malloc(sizeof(struct hashmultimap));
if (!_map_)
return ((void *)0);
_map_->buffer =
alloc->calloc(real_capacity, sizeof(struct hashmultimap_entry *[2]));
_map_->buffer = alloc->calloc(real_capacity, sizeof(struct hashmultimap_entry *[2]));
if (!_map_->buffer)
{
alloc->free(_map_);
@@ -192,8 +178,7 @@ void hmm_clear(struct hashmultimap *_map_)
scan = next;
}
}
memset(_map_->buffer, 0,
sizeof(struct hashmultimap_entry *[2]) * _map_->capacity);
memset(_map_->buffer, 0, sizeof(struct hashmultimap_entry *[2]) * _map_->capacity);
_map_->count = 0;
_map_->flag = CMC_FLAG_OK;
}
@@ -232,8 +217,7 @@ void hmm_free(struct hashmultimap *_map_)
_map_->alloc->free(_map_->buffer);
_map_->alloc->free(_map_);
}
void hmm_customize(struct hashmultimap *_map_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
void hmm_customize(struct hashmultimap *_map_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!alloc)
_map_->alloc = &cmc_alloc_node_default;
@@ -269,8 +253,7 @@ _Bool hmm_insert(struct hashmultimap *_map_, size_t key, size_t value)
_map_->callbacks->create();
return 1;
}
_Bool hmm_update(struct hashmultimap *_map_, size_t key, size_t new_value,
size_t *old_value)
_Bool hmm_update(struct hashmultimap *_map_, size_t key, size_t new_value, size_t *old_value)
{
if (hmm_empty(_map_))
{
@@ -291,8 +274,7 @@ _Bool hmm_update(struct hashmultimap *_map_, size_t key, size_t new_value,
_map_->callbacks->update();
return 1;
}
size_t hmm_update_all(struct hashmultimap *_map_, size_t key, size_t new_value,
size_t **old_values)
size_t hmm_update_all(struct hashmultimap *_map_, size_t key, size_t new_value, size_t **old_values)
{
if (hmm_empty(_map_))
{
@@ -346,10 +328,8 @@ _Bool hmm_remove(struct hashmultimap *_map_, size_t key, size_t *out_value)
return 0;
}
size_t hash = _map_->f_key->hash(key);
struct hashmultimap_entry **head =
&(_map_->buffer[hash % _map_->capacity][0]);
struct hashmultimap_entry **tail =
&(_map_->buffer[hash % _map_->capacity][1]);
struct hashmultimap_entry **head = &(_map_->buffer[hash % _map_->capacity][0]);
struct hashmultimap_entry **tail = &(_map_->buffer[hash % _map_->capacity][1]);
if (*head == ((void *)0))
{
_map_->flag = CMC_FLAG_NOT_FOUND;
@@ -407,8 +387,7 @@ _Bool hmm_remove(struct hashmultimap *_map_, size_t key, size_t *out_value)
_map_->callbacks->delete ();
return 1;
}
size_t hmm_remove_all(struct hashmultimap *_map_, size_t key,
size_t **out_values)
size_t hmm_remove_all(struct hashmultimap *_map_, size_t key, size_t **out_values)
{
if (hmm_empty(_map_))
{
@@ -416,10 +395,8 @@ size_t hmm_remove_all(struct hashmultimap *_map_, size_t key,
return 0;
}
size_t hash = _map_->f_key->hash(key);
struct hashmultimap_entry **head =
&(_map_->buffer[hash % _map_->capacity][0]);
struct hashmultimap_entry **tail =
&(_map_->buffer[hash % _map_->capacity][1]);
struct hashmultimap_entry **head = &(_map_->buffer[hash % _map_->capacity][0]);
struct hashmultimap_entry **tail = &(_map_->buffer[hash % _map_->capacity][1]);
if (*head == ((void *)0))
{
_map_->flag = CMC_FLAG_NOT_FOUND;
@@ -643,8 +620,7 @@ _Bool hmm_resize(struct hashmultimap *_map_, size_t capacity)
return 0;
}
struct hashmultimap *_new_map_ =
hmm_new_custom(capacity, _map_->load, _map_->f_key, _map_->f_val,
_map_->alloc, ((void *)0));
hmm_new_custom(capacity, _map_->load, _map_->f_key, _map_->f_val, _map_->alloc, ((void *)0));
if (!_new_map_)
{
_map_->flag = CMC_FLAG_ALLOC;
@@ -679,9 +655,8 @@ success:
}
struct hashmultimap *hmm_copy_of(struct hashmultimap *_map_)
{
struct hashmultimap *result =
hmm_new_custom(_map_->capacity * _map_->load, _map_->load, _map_->f_key,
_map_->f_val, _map_->alloc, ((void *)0));
struct hashmultimap *result = hmm_new_custom(_map_->capacity * _map_->load, _map_->load, _map_->f_key, _map_->f_val,
_map_->alloc, ((void *)0));
if (!result)
{
_map_->flag = CMC_FLAG_ERROR;
@@ -935,11 +910,9 @@ size_t hmm_iter_index(struct hashmultimap_iter *iter)
{
return iter->index;
}
struct hashmultimap_entry *hmm_impl_new_entry(struct hashmultimap *_map_,
size_t key, size_t value)
struct hashmultimap_entry *hmm_impl_new_entry(struct hashmultimap *_map_, size_t key, size_t value)
{
struct hashmultimap_entry *entry =
_map_->alloc->malloc(sizeof(struct hashmultimap_entry));
struct hashmultimap_entry *entry = _map_->alloc->malloc(sizeof(struct hashmultimap_entry));
if (!entry)
return ((void *)0);
entry->key = key;
@@ -948,8 +921,7 @@ struct hashmultimap_entry *hmm_impl_new_entry(struct hashmultimap *_map_,
entry->prev = ((void *)0);
return entry;
}
struct hashmultimap_entry *hmm_impl_get_entry(struct hashmultimap *_map_,
size_t key)
struct hashmultimap_entry *hmm_impl_get_entry(struct hashmultimap *_map_, size_t key)
{
size_t hash = _map_->f_key->hash(key);
struct hashmultimap_entry *entry = _map_->buffer[hash % _map_->capacity][0];
@@ -978,8 +950,7 @@ size_t hmm_impl_key_count(struct hashmultimap *_map_, size_t key)
}
size_t hmm_impl_calculate_size(size_t required)
{
const size_t count =
sizeof(cmc_hashtable_primes) / sizeof(cmc_hashtable_primes[0]);
const size_t count = sizeof(cmc_hashtable_primes) / sizeof(cmc_hashtable_primes[0]);
if (cmc_hashtable_primes[count - 1] < required)
return required;
size_t i = 0;
+51 -104
View File
@@ -41,16 +41,12 @@ struct hashmultiset_iter
_Bool start;
_Bool end;
};
struct hashmultiset *hms_new(size_t capacity, double load,
struct hashmultiset_fval *f_val);
struct hashmultiset *hms_new_custom(size_t capacity, double load,
struct hashmultiset_fval *f_val,
struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
struct hashmultiset *hms_new(size_t capacity, double load, struct hashmultiset_fval *f_val);
struct hashmultiset *hms_new_custom(size_t capacity, double load, struct hashmultiset_fval *f_val,
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
void hms_clear(struct hashmultiset *_set_);
void hms_free(struct hashmultiset *_set_);
void hms_customize(struct hashmultiset *_set_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void hms_customize(struct hashmultiset *_set_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
_Bool hms_insert(struct hashmultiset *_set_, size_t value);
_Bool hms_insert_many(struct hashmultiset *_set_, size_t value, size_t count);
_Bool hms_update(struct hashmultiset *_set_, size_t value, size_t multiplicity);
@@ -72,24 +68,16 @@ struct hashmultiset *hms_copy_of(struct hashmultiset *_set_);
_Bool hms_equals(struct hashmultiset *_set1_, struct hashmultiset *_set2_);
struct cmc_string hms_to_string(struct hashmultiset *_set_);
_Bool hms_print(struct hashmultiset *_set_, FILE *fptr);
struct hashmultiset *hms_union(struct hashmultiset *_set1_,
struct hashmultiset *_set2_);
struct hashmultiset *hms_intersection(struct hashmultiset *_set1_,
struct hashmultiset *_set2_);
struct hashmultiset *hms_difference(struct hashmultiset *_set1_,
struct hashmultiset *_set2_);
struct hashmultiset *hms_summation(struct hashmultiset *_set1_,
struct hashmultiset *_set2_);
struct hashmultiset *hms_symmetric_difference(struct hashmultiset *_set1_,
struct hashmultiset *_set2_);
struct hashmultiset *hms_union(struct hashmultiset *_set1_, struct hashmultiset *_set2_);
struct hashmultiset *hms_intersection(struct hashmultiset *_set1_, struct hashmultiset *_set2_);
struct hashmultiset *hms_difference(struct hashmultiset *_set1_, struct hashmultiset *_set2_);
struct hashmultiset *hms_summation(struct hashmultiset *_set1_, struct hashmultiset *_set2_);
struct hashmultiset *hms_symmetric_difference(struct hashmultiset *_set1_, struct hashmultiset *_set2_);
_Bool hms_is_subset(struct hashmultiset *_set1_, struct hashmultiset *_set2_);
_Bool hms_is_superset(struct hashmultiset *_set1_, struct hashmultiset *_set2_);
_Bool hms_is_proper_subset(struct hashmultiset *_set1_,
struct hashmultiset *_set2_);
_Bool hms_is_proper_superset(struct hashmultiset *_set1_,
struct hashmultiset *_set2_);
_Bool hms_is_disjointset(struct hashmultiset *_set1_,
struct hashmultiset *_set2_);
_Bool hms_is_proper_subset(struct hashmultiset *_set1_, struct hashmultiset *_set2_);
_Bool hms_is_proper_superset(struct hashmultiset *_set1_, struct hashmultiset *_set2_);
_Bool hms_is_disjointset(struct hashmultiset *_set1_, struct hashmultiset *_set2_);
struct hashmultiset_iter hms_iter_start(struct hashmultiset *target);
struct hashmultiset_iter hms_iter_end(struct hashmultiset *target);
_Bool hms_iter_at_start(struct hashmultiset_iter *iter);
@@ -104,16 +92,11 @@ _Bool hms_iter_go_to(struct hashmultiset_iter *iter, size_t index);
size_t hms_iter_value(struct hashmultiset_iter *iter);
size_t hms_iter_multiplicity(struct hashmultiset_iter *iter);
size_t hms_iter_index(struct hashmultiset_iter *iter);
static size_t hms_impl_multiplicity_of(struct hashmultiset *_set_,
size_t value);
static struct hashmultiset_entry *
hms_impl_insert_and_return(struct hashmultiset *_set_, size_t value,
_Bool *new_node);
static struct hashmultiset_entry *hms_impl_get_entry(struct hashmultiset *_set_,
size_t value);
static size_t hms_impl_multiplicity_of(struct hashmultiset *_set_, size_t value);
static struct hashmultiset_entry *hms_impl_insert_and_return(struct hashmultiset *_set_, size_t value, _Bool *new_node);
static struct hashmultiset_entry *hms_impl_get_entry(struct hashmultiset *_set_, size_t value);
static size_t hms_impl_calculate_size(size_t required);
struct hashmultiset *hms_new(size_t capacity, double load,
struct hashmultiset_fval *f_val)
struct hashmultiset *hms_new(size_t capacity, double load, struct hashmultiset_fval *f_val)
{
struct cmc_alloc_node *alloc = &cmc_alloc_node_default;
if (capacity == 0 || load <= 0 || load >= 1)
@@ -126,8 +109,7 @@ struct hashmultiset *hms_new(size_t capacity, double load,
struct hashmultiset *_set_ = alloc->malloc(sizeof(struct hashmultiset));
if (!_set_)
return ((void *)0);
_set_->buffer =
alloc->calloc(real_capacity, sizeof(struct hashmultiset_entry));
_set_->buffer = alloc->calloc(real_capacity, sizeof(struct hashmultiset_entry));
if (!_set_->buffer)
{
alloc->free(_set_);
@@ -143,10 +125,8 @@ struct hashmultiset *hms_new(size_t capacity, double load,
_set_->callbacks = ((void *)0);
return _set_;
}
struct hashmultiset *hms_new_custom(size_t capacity, double load,
struct hashmultiset_fval *f_val,
struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
struct hashmultiset *hms_new_custom(size_t capacity, double load, struct hashmultiset_fval *f_val,
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (capacity == 0 || load <= 0 || load >= 1)
return ((void *)0);
@@ -160,8 +140,7 @@ struct hashmultiset *hms_new_custom(size_t capacity, double load,
struct hashmultiset *_set_ = alloc->malloc(sizeof(struct hashmultiset));
if (!_set_)
return ((void *)0);
_set_->buffer =
alloc->calloc(real_capacity, sizeof(struct hashmultiset_entry));
_set_->buffer = alloc->calloc(real_capacity, sizeof(struct hashmultiset_entry));
if (!_set_->buffer)
{
alloc->free(_set_);
@@ -190,8 +169,7 @@ void hms_clear(struct hashmultiset *_set_)
}
}
}
memset(_set_->buffer, 0,
sizeof(struct hashmultiset_entry) * _set_->capacity);
memset(_set_->buffer, 0, sizeof(struct hashmultiset_entry) * _set_->capacity);
_set_->count = 0;
_set_->flag = CMC_FLAG_OK;
}
@@ -211,8 +189,7 @@ void hms_free(struct hashmultiset *_set_)
_set_->alloc->free(_set_->buffer);
_set_->alloc->free(_set_);
}
void hms_customize(struct hashmultiset *_set_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
void hms_customize(struct hashmultiset *_set_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!alloc)
_set_->alloc = &cmc_alloc_node_default;
@@ -224,8 +201,7 @@ void hms_customize(struct hashmultiset *_set_, struct cmc_alloc_node *alloc,
_Bool hms_insert(struct hashmultiset *_set_, size_t value)
{
_Bool new_node;
struct hashmultiset_entry *entry =
hms_impl_insert_and_return(_set_, value, &new_node);
struct hashmultiset_entry *entry = hms_impl_insert_and_return(_set_, value, &new_node);
if (!entry)
{
_set_->flag = CMC_FLAG_ERROR;
@@ -244,8 +220,7 @@ _Bool hms_insert_many(struct hashmultiset *_set_, size_t value, size_t count)
if (count == 0)
goto success;
_Bool new_node;
struct hashmultiset_entry *entry =
hms_impl_insert_and_return(_set_, value, &new_node);
struct hashmultiset_entry *entry = hms_impl_insert_and_return(_set_, value, &new_node);
if (!entry)
{
_set_->flag = CMC_FLAG_ERROR;
@@ -278,14 +253,12 @@ _Bool hms_update(struct hashmultiset *_set_, size_t value, size_t multiplicity)
goto success;
}
_Bool new_node;
struct hashmultiset_entry *entry =
hms_impl_insert_and_return(_set_, value, &new_node);
struct hashmultiset_entry *entry = hms_impl_insert_and_return(_set_, value, &new_node);
if (!entry)
return 0;
if (new_node)
_set_->cardinality++;
_set_->cardinality =
(_set_->cardinality - entry->multiplicity) + multiplicity;
_set_->cardinality = (_set_->cardinality - entry->multiplicity) + multiplicity;
entry->multiplicity = multiplicity;
success:
_set_->flag = CMC_FLAG_OK;
@@ -461,8 +434,7 @@ _Bool hms_resize(struct hashmultiset *_set_, size_t capacity)
_set_->flag = CMC_FLAG_INVALID;
return 0;
}
struct hashmultiset *_new_set_ = hms_new_custom(
capacity, _set_->load, _set_->f_val, _set_->alloc, ((void *)0));
struct hashmultiset *_new_set_ = hms_new_custom(capacity, _set_->load, _set_->f_val, _set_->alloc, ((void *)0));
if (!_new_set_)
{
_set_->flag = CMC_FLAG_ERROR;
@@ -471,8 +443,7 @@ _Bool hms_resize(struct hashmultiset *_set_, size_t capacity)
struct hashmultiset_iter iter = hms_iter_start(_set_);
for (; !hms_iter_at_end(&iter); hms_iter_next(&iter))
{
hms_insert_many(_new_set_, hms_iter_value(&iter),
hms_iter_multiplicity(&iter));
hms_insert_many(_new_set_, hms_iter_value(&iter), hms_iter_multiplicity(&iter));
}
if (_set_->count != _new_set_->count)
{
@@ -496,8 +467,7 @@ success:
struct hashmultiset *hms_copy_of(struct hashmultiset *_set_)
{
struct hashmultiset *result =
hms_new_custom(_set_->capacity * _set_->load, _set_->load, _set_->f_val,
_set_->alloc, _set_->callbacks);
hms_new_custom(_set_->capacity * _set_->load, _set_->load, _set_->f_val, _set_->alloc, _set_->callbacks);
if (!result)
{
_set_->flag = CMC_FLAG_ERROR;
@@ -524,8 +494,7 @@ struct hashmultiset *hms_copy_of(struct hashmultiset *_set_)
}
}
else
memcpy(result->buffer, _set_->buffer,
sizeof(struct hashmultiset_entry) * _set_->capacity);
memcpy(result->buffer, _set_->buffer, sizeof(struct hashmultiset_entry) * _set_->capacity);
result->count = _set_->count;
result->cardinality = _set_->cardinality;
_set_->flag = CMC_FLAG_OK;
@@ -544,8 +513,7 @@ _Bool hms_equals(struct hashmultiset *_set1_, struct hashmultiset *_set2_)
struct hashmultiset_iter iter = hms_iter_start(_set1_);
for (; !hms_iter_at_end(&iter); hms_iter_next(&iter))
{
struct hashmultiset_entry *entry =
hms_impl_get_entry(_set2_, hms_iter_value(&iter));
struct hashmultiset_entry *entry = hms_impl_get_entry(_set2_, hms_iter_value(&iter));
if (!entry)
return 0;
if (entry->multiplicity != hms_iter_multiplicity(&iter))
@@ -553,12 +521,10 @@ _Bool hms_equals(struct hashmultiset *_set1_, struct hashmultiset *_set2_)
}
return 1;
}
struct hashmultiset *hms_union(struct hashmultiset *_set1_,
struct hashmultiset *_set2_)
struct hashmultiset *hms_union(struct hashmultiset *_set1_, struct hashmultiset *_set2_)
{
struct hashmultiset *_set_r_ =
hms_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val,
_set1_->alloc, ((void *)0));
hms_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val, _set1_->alloc, ((void *)0));
if (!_set_r_)
return ((void *)0);
struct hashmultiset_iter iter1 = hms_iter_start(_set1_);
@@ -582,16 +548,13 @@ struct hashmultiset *hms_union(struct hashmultiset *_set1_,
_set_r_->callbacks = _set1_->callbacks;
return _set_r_;
}
struct hashmultiset *hms_intersection(struct hashmultiset *_set1_,
struct hashmultiset *_set2_)
struct hashmultiset *hms_intersection(struct hashmultiset *_set1_, struct hashmultiset *_set2_)
{
struct hashmultiset *_set_r_ =
hms_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val,
_set1_->alloc, ((void *)0));
hms_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val, _set1_->alloc, ((void *)0));
if (!_set_r_)
return ((void *)0);
struct hashmultiset *_set_A_ =
_set1_->count < _set2_->count ? _set1_ : _set2_;
struct hashmultiset *_set_A_ = _set1_->count < _set2_->count ? _set1_ : _set2_;
struct hashmultiset *_set_B_ = _set_A_ == _set1_ ? _set2_ : _set1_;
struct hashmultiset_iter iter = hms_iter_start(_set_A_);
for (; !hms_iter_at_end(&iter); hms_iter_next(&iter))
@@ -605,12 +568,10 @@ struct hashmultiset *hms_intersection(struct hashmultiset *_set1_,
_set_r_->callbacks = _set1_->callbacks;
return _set_r_;
}
struct hashmultiset *hms_difference(struct hashmultiset *_set1_,
struct hashmultiset *_set2_)
struct hashmultiset *hms_difference(struct hashmultiset *_set1_, struct hashmultiset *_set2_)
{
struct hashmultiset *_set_r_ =
hms_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val,
_set1_->alloc, ((void *)0));
hms_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val, _set1_->alloc, ((void *)0));
if (!_set_r_)
return ((void *)0);
struct hashmultiset_iter iter = hms_iter_start(_set1_);
@@ -625,35 +586,29 @@ struct hashmultiset *hms_difference(struct hashmultiset *_set1_,
_set_r_->callbacks = _set1_->callbacks;
return _set_r_;
}
struct hashmultiset *hms_summation(struct hashmultiset *_set1_,
struct hashmultiset *_set2_)
struct hashmultiset *hms_summation(struct hashmultiset *_set1_, struct hashmultiset *_set2_)
{
struct hashmultiset *_set_r_ =
hms_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val,
_set1_->alloc, ((void *)0));
hms_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val, _set1_->alloc, ((void *)0));
if (!_set_r_)
return ((void *)0);
struct hashmultiset_iter iter1 = hms_iter_start(_set1_);
struct hashmultiset_iter iter2 = hms_iter_start(_set2_);
for (; !hms_iter_at_end(&iter1); hms_iter_next(&iter1))
{
hms_insert_many(_set_r_, hms_iter_value(&iter1),
hms_iter_multiplicity(&iter1));
hms_insert_many(_set_r_, hms_iter_value(&iter1), hms_iter_multiplicity(&iter1));
}
for (; !hms_iter_at_end(&iter2); hms_iter_next(&iter2))
{
hms_insert_many(_set_r_, hms_iter_value(&iter2),
hms_iter_multiplicity(&iter2));
hms_insert_many(_set_r_, hms_iter_value(&iter2), hms_iter_multiplicity(&iter2));
}
_set_r_->callbacks = _set1_->callbacks;
return _set_r_;
}
struct hashmultiset *hms_symmetric_difference(struct hashmultiset *_set1_,
struct hashmultiset *_set2_)
struct hashmultiset *hms_symmetric_difference(struct hashmultiset *_set1_, struct hashmultiset *_set2_)
{
struct hashmultiset *_set_r_ =
hms_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val,
_set1_->alloc, ((void *)0));
hms_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val, _set1_->alloc, ((void *)0));
if (!_set_r_)
return ((void *)0);
struct hashmultiset_iter iter1 = hms_iter_start(_set1_);
@@ -708,8 +663,7 @@ _Bool hms_is_superset(struct hashmultiset *_set1_, struct hashmultiset *_set2_)
{
return hms_is_subset(_set2_, _set1_);
}
_Bool hms_is_proper_subset(struct hashmultiset *_set1_,
struct hashmultiset *_set2_)
_Bool hms_is_proper_subset(struct hashmultiset *_set1_, struct hashmultiset *_set2_)
{
if (_set1_->count >= _set2_->count)
return 0;
@@ -731,13 +685,11 @@ _Bool hms_is_proper_subset(struct hashmultiset *_set1_,
}
return 1;
}
_Bool hms_is_proper_superset(struct hashmultiset *_set1_,
struct hashmultiset *_set2_)
_Bool hms_is_proper_superset(struct hashmultiset *_set1_, struct hashmultiset *_set2_)
{
return hms_is_proper_subset(_set2_, _set1_);
}
_Bool hms_is_disjointset(struct hashmultiset *_set1_,
struct hashmultiset *_set2_)
_Bool hms_is_disjointset(struct hashmultiset *_set1_, struct hashmultiset *_set2_)
{
if (hms_empty(_set1_))
return 1;
@@ -952,9 +904,7 @@ static size_t hms_impl_multiplicity_of(struct hashmultiset *_set_, size_t value)
return 0;
return entry->multiplicity;
}
static struct hashmultiset_entry *
hms_impl_insert_and_return(struct hashmultiset *_set_, size_t value,
_Bool *new_node)
static struct hashmultiset_entry *hms_impl_insert_and_return(struct hashmultiset *_set_, size_t value, _Bool *new_node)
{
*new_node = 0;
struct hashmultiset_entry *entry = hms_impl_get_entry(_set_, value);
@@ -986,8 +936,7 @@ hms_impl_insert_and_return(struct hashmultiset *_set_, size_t value,
{
pos++;
target = &(_set_->buffer[pos % _set_->capacity]);
if (target->state == CMC_ES_EMPTY ||
target->state == CMC_ES_DELETED)
if (target->state == CMC_ES_EMPTY || target->state == CMC_ES_DELETED)
{
target->value = value;
target->multiplicity = curr_mul;
@@ -1016,8 +965,7 @@ hms_impl_insert_and_return(struct hashmultiset *_set_, size_t value,
_set_->count++;
return to_return;
}
static struct hashmultiset_entry *hms_impl_get_entry(struct hashmultiset *_set_,
size_t value)
static struct hashmultiset_entry *hms_impl_get_entry(struct hashmultiset *_set_, size_t value)
{
size_t hash = _set_->f_val->hash(value);
size_t pos = hash % _set_->capacity;
@@ -1033,8 +981,7 @@ static struct hashmultiset_entry *hms_impl_get_entry(struct hashmultiset *_set_,
}
static size_t hms_impl_calculate_size(size_t required)
{
const size_t count =
sizeof(cmc_hashtable_primes) / sizeof(cmc_hashtable_primes[0]);
const size_t count = sizeof(cmc_hashtable_primes) / sizeof(cmc_hashtable_primes[0]);
if (cmc_hashtable_primes[count - 1] < required)
return required;
size_t i = 0;
+18 -42
View File
@@ -39,16 +39,12 @@ struct hashset_iter
_Bool start;
_Bool end;
};
struct hashset *hs_new(size_t capacity, double load,
struct hashset_fval *f_val);
struct hashset *hs_new_custom(size_t capacity, double load,
struct hashset_fval *f_val,
struct cmc_alloc_node *alloc,
struct hashset *hs_new(size_t capacity, double load, struct hashset_fval *f_val);
struct hashset *hs_new_custom(size_t capacity, double load, struct hashset_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void hs_clear(struct hashset *_set_);
void hs_free(struct hashset *_set_);
void hs_customize(struct hashset *_set_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void hs_customize(struct hashset *_set_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
_Bool hs_insert(struct hashset *_set_, size_t value);
_Bool hs_remove(struct hashset *_set_, size_t value);
_Bool hs_max(struct hashset *_set_, size_t *value);
@@ -68,8 +64,7 @@ _Bool hs_print(struct hashset *_set_, FILE *fptr);
struct hashset *hs_union(struct hashset *_set1_, struct hashset *_set2_);
struct hashset *hs_intersection(struct hashset *_set1_, struct hashset *_set2_);
struct hashset *hs_difference(struct hashset *_set1_, struct hashset *_set2_);
struct hashset *hs_symmetric_difference(struct hashset *_set1_,
struct hashset *_set2_);
struct hashset *hs_symmetric_difference(struct hashset *_set1_, struct hashset *_set2_);
_Bool hs_is_subset(struct hashset *_set1_, struct hashset *_set2_);
_Bool hs_is_superset(struct hashset *_set1_, struct hashset *_set2_);
_Bool hs_is_proper_subset(struct hashset *_set1_, struct hashset *_set2_);
@@ -88,8 +83,7 @@ _Bool hs_iter_rewind(struct hashset_iter *iter, size_t steps);
_Bool hs_iter_go_to(struct hashset_iter *iter, size_t index);
size_t hs_iter_value(struct hashset_iter *iter);
size_t hs_iter_index(struct hashset_iter *iter);
static struct hashset_entry *hs_impl_get_entry(struct hashset *_set_,
size_t value);
static struct hashset_entry *hs_impl_get_entry(struct hashset *_set_, size_t value);
static size_t hs_impl_calculate_size(size_t required);
static struct hashset_iter hs_impl_it_start(struct hashset *_set_);
static struct hashset_iter hs_impl_it_end(struct hashset *_set_);
@@ -121,9 +115,7 @@ struct hashset *hs_new(size_t capacity, double load, struct hashset_fval *f_val)
_set_->callbacks = ((void *)0);
return _set_;
}
struct hashset *hs_new_custom(size_t capacity, double load,
struct hashset_fval *f_val,
struct cmc_alloc_node *alloc,
struct hashset *hs_new_custom(size_t capacity, double load, struct hashset_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
{
if (capacity == 0 || load <= 0 || load >= 1)
@@ -186,8 +178,7 @@ void hs_free(struct hashset *_set_)
_set_->alloc->free(_set_->buffer);
_set_->alloc->free(_set_);
}
void hs_customize(struct hashset *_set_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
void hs_customize(struct hashset *_set_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!alloc)
_set_->alloc = &cmc_alloc_node_default;
@@ -224,8 +215,7 @@ _Bool hs_insert(struct hashset *_set_, size_t value)
{
pos++;
target = &(_set_->buffer[pos % _set_->capacity]);
if (target->state == CMC_ES_EMPTY ||
target->state == CMC_ES_DELETED)
if (target->state == CMC_ES_EMPTY || target->state == CMC_ES_DELETED)
{
target->value = value;
target->dist = pos - original_pos;
@@ -371,8 +361,7 @@ _Bool hs_resize(struct hashset *_set_, size_t capacity)
_set_->flag = CMC_FLAG_INVALID;
return 0;
}
struct hashset *_new_set_ = hs_new_custom(
capacity, _set_->load, _set_->f_val, _set_->alloc, ((void *)0));
struct hashset *_new_set_ = hs_new_custom(capacity, _set_->load, _set_->f_val, _set_->alloc, ((void *)0));
if (!_new_set_)
{
_set_->flag = CMC_FLAG_ALLOC;
@@ -406,8 +395,7 @@ success:
struct hashset *hs_copy_of(struct hashset *_set_)
{
struct hashset *result =
hs_new_custom(_set_->capacity * _set_->load, _set_->load, _set_->f_val,
_set_->alloc, _set_->callbacks);
hs_new_custom(_set_->capacity * _set_->load, _set_->load, _set_->f_val, _set_->alloc, _set_->callbacks);
if (!result)
{
_set_->flag = CMC_FLAG_ERROR;
@@ -433,8 +421,7 @@ struct hashset *hs_copy_of(struct hashset *_set_)
}
}
else
memcpy(result->buffer, _set_->buffer,
sizeof(struct hashset_entry) * _set_->capacity);
memcpy(result->buffer, _set_->buffer, sizeof(struct hashset_entry) * _set_->capacity);
result->count = _set_->count;
_set_->flag = CMC_FLAG_OK;
return result;
@@ -457,9 +444,7 @@ _Bool hs_equals(struct hashset *_set1_, struct hashset *_set2_)
}
struct hashset *hs_union(struct hashset *_set1_, struct hashset *_set2_)
{
struct hashset *_set_r_ =
hs_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val,
_set1_->alloc, ((void *)0));
struct hashset *_set_r_ = hs_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val, _set1_->alloc, ((void *)0));
if (!_set_r_)
{
_set1_->flag = CMC_FLAG_ALLOC;
@@ -481,9 +466,7 @@ struct hashset *hs_union(struct hashset *_set1_, struct hashset *_set2_)
}
struct hashset *hs_intersection(struct hashset *_set1_, struct hashset *_set2_)
{
struct hashset *_set_r_ =
hs_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val,
_set1_->alloc, ((void *)0));
struct hashset *_set_r_ = hs_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val, _set1_->alloc, ((void *)0));
if (!_set_r_)
{
_set1_->flag = CMC_FLAG_ALLOC;
@@ -504,9 +487,7 @@ struct hashset *hs_intersection(struct hashset *_set1_, struct hashset *_set2_)
}
struct hashset *hs_difference(struct hashset *_set1_, struct hashset *_set2_)
{
struct hashset *_set_r_ =
hs_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val,
_set1_->alloc, ((void *)0));
struct hashset *_set_r_ = hs_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val, _set1_->alloc, ((void *)0));
if (!_set_r_)
{
_set1_->flag = CMC_FLAG_ALLOC;
@@ -523,12 +504,9 @@ struct hashset *hs_difference(struct hashset *_set1_, struct hashset *_set2_)
_set_r_->callbacks = _set1_->callbacks;
return _set_r_;
}
struct hashset *hs_symmetric_difference(struct hashset *_set1_,
struct hashset *_set2_)
struct hashset *hs_symmetric_difference(struct hashset *_set1_, struct hashset *_set2_)
{
struct hashset *_set_r_ =
hs_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val,
_set1_->alloc, ((void *)0));
struct hashset *_set_r_ = hs_new_custom(_set1_->capacity, _set1_->load, _set1_->f_val, _set1_->alloc, ((void *)0));
if (!_set_r_)
{
_set1_->flag = CMC_FLAG_ALLOC;
@@ -803,8 +781,7 @@ size_t hs_iter_index(struct hashset_iter *iter)
{
return iter->index;
}
static struct hashset_entry *hs_impl_get_entry(struct hashset *_set_,
size_t value)
static struct hashset_entry *hs_impl_get_entry(struct hashset *_set_, size_t value)
{
size_t hash = _set_->f_val->hash(value);
size_t pos = hash % _set_->capacity;
@@ -820,8 +797,7 @@ static struct hashset_entry *hs_impl_get_entry(struct hashset *_set_,
}
static size_t hs_impl_calculate_size(size_t required)
{
const size_t count =
sizeof(cmc_hashtable_primes) / sizeof(cmc_hashtable_primes[0]);
const size_t count = sizeof(cmc_hashtable_primes) / sizeof(cmc_hashtable_primes[0]);
if (cmc_hashtable_primes[count - 1] < required)
return required;
size_t i = 0;
+12 -23
View File
@@ -30,15 +30,12 @@ struct heap_iter
_Bool start;
_Bool end;
};
struct heap *h_new(size_t capacity, enum cmc_heap_order HO,
struct heap_fval *f_val);
struct heap *h_new_custom(size_t capacity, enum cmc_heap_order HO,
struct heap_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
struct heap *h_new(size_t capacity, enum cmc_heap_order HO, struct heap_fval *f_val);
struct heap *h_new_custom(size_t capacity, enum cmc_heap_order HO, struct heap_fval *f_val,
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
void h_clear(struct heap *_heap_);
void h_free(struct heap *_heap_);
void h_customize(struct heap *_heap_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void h_customize(struct heap *_heap_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
_Bool h_insert(struct heap *_heap_, size_t value);
_Bool h_remove(struct heap *_heap_);
size_t h_peek(struct heap *_heap_);
@@ -68,8 +65,7 @@ size_t h_iter_value(struct heap_iter *iter);
size_t h_iter_index(struct heap_iter *iter);
static void h_impl_float_up(struct heap *_heap_, size_t index);
static void h_impl_float_down(struct heap *_heap_, size_t index);
struct heap *h_new(size_t capacity, enum cmc_heap_order HO,
struct heap_fval *f_val)
struct heap *h_new(size_t capacity, enum cmc_heap_order HO, struct heap_fval *f_val)
{
struct cmc_alloc_node *alloc = &cmc_alloc_node_default;
if (capacity < 1)
@@ -96,9 +92,8 @@ struct heap *h_new(size_t capacity, enum cmc_heap_order HO,
_heap_->callbacks = ((void *)0);
return _heap_;
}
struct heap *h_new_custom(size_t capacity, enum cmc_heap_order HO,
struct heap_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
struct heap *h_new_custom(size_t capacity, enum cmc_heap_order HO, struct heap_fval *f_val,
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (capacity < 1)
return ((void *)0);
@@ -151,8 +146,7 @@ void h_free(struct heap *_heap_)
_heap_->alloc->free(_heap_->buffer);
_heap_->alloc->free(_heap_);
}
void h_customize(struct heap *_heap_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
void h_customize(struct heap *_heap_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!alloc)
_heap_->alloc = &cmc_alloc_node_default;
@@ -252,8 +246,7 @@ _Bool h_resize(struct heap *_heap_, size_t capacity)
_heap_->flag = CMC_FLAG_INVALID;
return 0;
}
size_t *new_buffer =
_heap_->alloc->realloc(_heap_->buffer, sizeof(size_t) * capacity);
size_t *new_buffer = _heap_->alloc->realloc(_heap_->buffer, sizeof(size_t) * capacity);
if (!new_buffer)
{
_heap_->flag = CMC_FLAG_ALLOC;
@@ -268,9 +261,7 @@ success:
}
struct heap *h_copy_of(struct heap *_heap_)
{
struct heap *result =
h_new_custom(_heap_->capacity, _heap_->HO, _heap_->f_val, _heap_->alloc,
_heap_->callbacks);
struct heap *result = h_new_custom(_heap_->capacity, _heap_->HO, _heap_->f_val, _heap_->alloc, _heap_->callbacks);
if (!result)
{
_heap_->flag = CMC_FLAG_ERROR;
@@ -454,13 +445,11 @@ static void h_impl_float_down(struct heap *_heap_, size_t index)
size_t L = 2 * index + 1;
size_t R = 2 * index + 2;
size_t C = index;
if (L < _heap_->count &&
_heap_->f_val->cmp(_heap_->buffer[L], _heap_->buffer[C]) * mod > 0)
if (L < _heap_->count && _heap_->f_val->cmp(_heap_->buffer[L], _heap_->buffer[C]) * mod > 0)
{
C = L;
}
if (R < _heap_->count &&
_heap_->f_val->cmp(_heap_->buffer[R], _heap_->buffer[C]) * mod > 0)
if (R < _heap_->count && _heap_->f_val->cmp(_heap_->buffer[R], _heap_->buffer[C]) * mod > 0)
{
C = R;
}
+13 -28
View File
@@ -31,14 +31,11 @@ struct intervalheap_iter
_Bool end;
};
struct intervalheap *ih_new(size_t capacity, struct intervalheap_fval *f_val);
struct intervalheap *ih_new_custom(size_t capacity,
struct intervalheap_fval *f_val,
struct cmc_alloc_node *alloc,
struct intervalheap *ih_new_custom(size_t capacity, struct intervalheap_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void ih_clear(struct intervalheap *_heap_);
void ih_free(struct intervalheap *_heap_);
void ih_customize(struct intervalheap *_heap_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void ih_customize(struct intervalheap *_heap_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
_Bool ih_insert(struct intervalheap *_heap_, size_t value);
_Bool ih_remove_max(struct intervalheap *_heap_);
_Bool ih_remove_min(struct intervalheap *_heap_);
@@ -100,9 +97,7 @@ struct intervalheap *ih_new(size_t capacity, struct intervalheap_fval *f_val)
_heap_->callbacks = ((void *)0);
return _heap_;
}
struct intervalheap *ih_new_custom(size_t capacity,
struct intervalheap_fval *f_val,
struct cmc_alloc_node *alloc,
struct intervalheap *ih_new_custom(size_t capacity, struct intervalheap_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
{
if (capacity == 0 || capacity == 0xffffffffffffffffULL)
@@ -156,8 +151,7 @@ void ih_free(struct intervalheap *_heap_)
_heap_->alloc->free(_heap_->buffer);
_heap_->alloc->free(_heap_);
}
void ih_customize(struct intervalheap *_heap_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
void ih_customize(struct intervalheap *_heap_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!alloc)
_heap_->alloc = &cmc_alloc_node_default;
@@ -405,8 +399,7 @@ _Bool ih_resize(struct intervalheap *_heap_, size_t capacity)
return 0;
}
capacity += capacity % 2;
size_t(*new_buffer)[2] =
_heap_->alloc->realloc(_heap_->buffer, sizeof(size_t[2]) * capacity);
size_t(*new_buffer)[2] = _heap_->alloc->realloc(_heap_->buffer, sizeof(size_t[2]) * capacity);
if (!new_buffer)
{
_heap_->flag = CMC_FLAG_ALLOC;
@@ -414,8 +407,7 @@ _Bool ih_resize(struct intervalheap *_heap_, size_t capacity)
}
if (capacity > _heap_->capacity)
{
memset(new_buffer + _heap_->capacity, 0,
sizeof(size_t[2]) * (capacity - _heap_->capacity));
memset(new_buffer + _heap_->capacity, 0, sizeof(size_t[2]) * (capacity - _heap_->capacity));
}
_heap_->buffer = new_buffer;
_heap_->capacity = capacity;
@@ -427,16 +419,14 @@ success:
}
struct intervalheap *ih_copy_of(struct intervalheap *_heap_)
{
struct intervalheap *result =
_heap_->alloc->malloc(sizeof(struct intervalheap));
struct intervalheap *result = _heap_->alloc->malloc(sizeof(struct intervalheap));
if (!result)
{
_heap_->flag = CMC_FLAG_ALLOC;
return ((void *)0);
}
memcpy(result, _heap_, sizeof(struct intervalheap));
result->buffer =
_heap_->alloc->malloc(sizeof(size_t[2]) * _heap_->capacity);
result->buffer = _heap_->alloc->malloc(sizeof(size_t[2]) * _heap_->capacity);
if (!result->buffer)
{
_heap_->alloc->free(result);
@@ -446,12 +436,10 @@ struct intervalheap *ih_copy_of(struct intervalheap *_heap_)
if (_heap_->f_val->cpy)
{
for (size_t i = 0; i < _heap_->count; i++)
result->buffer[i / 2][i % 2] =
_heap_->f_val->cpy(_heap_->buffer[i / 2][i % 2]);
result->buffer[i / 2][i % 2] = _heap_->f_val->cpy(_heap_->buffer[i / 2][i % 2]);
}
else
memcpy(result->buffer, _heap_->buffer,
sizeof(size_t[2]) * _heap_->capacity);
memcpy(result->buffer, _heap_->buffer, sizeof(size_t[2]) * _heap_->capacity);
result->capacity = _heap_->capacity;
result->size = _heap_->size;
result->count = _heap_->count;
@@ -666,11 +654,9 @@ static void ih_impl_float_down_max(struct intervalheap *_heap_)
size_t(*L)[2] = &(_heap_->buffer[L_index]);
size_t(*R)[2] = &(_heap_->buffer[R_index]);
if (R_index == _heap_->size - 1 && _heap_->count % 2 != 0)
child = _heap_->f_val->cmp((*L)[1], (*R)[0]) > 0 ? L_index
: R_index;
child = _heap_->f_val->cmp((*L)[1], (*R)[0]) > 0 ? L_index : R_index;
else
child = _heap_->f_val->cmp((*L)[1], (*R)[1]) > 0 ? L_index
: R_index;
child = _heap_->f_val->cmp((*L)[1], (*R)[1]) > 0 ? L_index : R_index;
}
else
child = L_index;
@@ -716,8 +702,7 @@ static void ih_impl_float_down_min(struct intervalheap *_heap_)
{
size_t(*L)[2] = &(_heap_->buffer[L_index]);
size_t(*R)[2] = &(_heap_->buffer[R_index]);
child =
_heap_->f_val->cmp((*L)[0], (*R)[0]) < 0 ? L_index : R_index;
child = _heap_->f_val->cmp((*L)[0], (*R)[0]) < 0 ? L_index : R_index;
}
else
child = L_index;
+10 -20
View File
@@ -37,13 +37,11 @@ struct linkedlist_iter
_Bool end;
};
struct linkedlist *ll_new(struct linkedlist_fval *f_val);
struct linkedlist *ll_new_custom(struct linkedlist_fval *f_val,
struct cmc_alloc_node *alloc,
struct linkedlist *ll_new_custom(struct linkedlist_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void ll_clear(struct linkedlist *_list_);
void ll_free(struct linkedlist *_list_);
void ll_customize(struct linkedlist *_list_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void ll_customize(struct linkedlist *_list_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
_Bool ll_push_front(struct linkedlist *_list_, size_t value);
_Bool ll_push_at(struct linkedlist *_list_, size_t value, size_t index);
_Bool ll_push_back(struct linkedlist *_list_, size_t value);
@@ -67,10 +65,8 @@ void ll_free_node(struct linkedlist *_list_, struct linkedlist_node *_node_);
struct linkedlist_node *ll_head(struct linkedlist *_list_);
struct linkedlist_node *ll_get_node(struct linkedlist *_list_, size_t index);
struct linkedlist_node *ll_tail(struct linkedlist *_list_);
_Bool ll_add_next(struct linkedlist *_owner_, struct linkedlist_node *_node_,
size_t value);
_Bool ll_add_prev(struct linkedlist *_owner_, struct linkedlist_node *_node_,
size_t value);
_Bool ll_add_next(struct linkedlist *_owner_, struct linkedlist_node *_node_, size_t value);
_Bool ll_add_prev(struct linkedlist *_owner_, struct linkedlist_node *_node_, size_t value);
_Bool ll_del_next(struct linkedlist *_owner_, struct linkedlist_node *_node_);
_Bool ll_del_curr(struct linkedlist *_owner_, struct linkedlist_node *_node_);
_Bool ll_del_prev(struct linkedlist *_owner_, struct linkedlist_node *_node_);
@@ -108,8 +104,7 @@ struct linkedlist *ll_new(struct linkedlist_fval *f_val)
_list_->callbacks = ((void *)0);
return _list_;
}
struct linkedlist *ll_new_custom(struct linkedlist_fval *f_val,
struct cmc_alloc_node *alloc,
struct linkedlist *ll_new_custom(struct linkedlist_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
{
if (!f_val)
@@ -151,8 +146,7 @@ void ll_free(struct linkedlist *_list_)
ll_clear(_list_);
_list_->alloc->free(_list_);
}
void ll_customize(struct linkedlist *_list_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
void ll_customize(struct linkedlist *_list_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!alloc)
_list_->alloc = &cmc_alloc_node_default;
@@ -400,8 +394,7 @@ int ll_flag(struct linkedlist *_list_)
}
struct linkedlist *ll_copy_of(struct linkedlist *_list_)
{
struct linkedlist *result =
ll_new_custom(_list_->f_val, _list_->alloc, _list_->callbacks);
struct linkedlist *result = ll_new_custom(_list_->f_val, _list_->alloc, _list_->callbacks);
if (!result)
return ((void *)0);
struct linkedlist_node *scan = _list_->head;
@@ -448,8 +441,7 @@ _Bool ll_equals(struct linkedlist *_list1_, struct linkedlist *_list2_)
}
struct linkedlist_node *ll_new_node(struct linkedlist *_list_, size_t value)
{
struct linkedlist_node *_node_ =
_list_->alloc->malloc(sizeof(struct linkedlist_node));
struct linkedlist_node *_node_ = _list_->alloc->malloc(sizeof(struct linkedlist_node));
if (!_node_)
{
_list_->flag = CMC_FLAG_ALLOC;
@@ -504,8 +496,7 @@ struct linkedlist_node *ll_tail(struct linkedlist *_list_)
{
return _list_->tail;
}
_Bool ll_add_next(struct linkedlist *_owner_, struct linkedlist_node *_node_,
size_t value)
_Bool ll_add_next(struct linkedlist *_owner_, struct linkedlist_node *_node_, size_t value)
{
struct linkedlist_node *new_node = ll_new_node(_owner_, value);
if (!new_node)
@@ -521,8 +512,7 @@ _Bool ll_add_next(struct linkedlist *_owner_, struct linkedlist_node *_node_,
_owner_->flag = CMC_FLAG_OK;
return 1;
}
_Bool ll_add_prev(struct linkedlist *_owner_, struct linkedlist_node *_node_,
size_t value)
_Bool ll_add_prev(struct linkedlist *_owner_, struct linkedlist_node *_node_, size_t value)
{
struct linkedlist_node *new_node = ll_new_node(_owner_, value);
if (!new_node)
+17 -54
View File
@@ -30,13 +30,11 @@ struct list_iter
_Bool end;
};
struct list *l_new(size_t capacity, struct list_fval *f_val);
struct list *l_new_custom(size_t capacity, struct list_fval *f_val,
struct cmc_alloc_node *alloc,
struct list *l_new_custom(size_t capacity, struct list_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void l_clear(struct list *_list_);
void l_free(struct list *_list_);
void l_customize(struct list *_list_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void l_customize(struct list *_list_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
_Bool l_push_front(struct list *_list_, size_t value);
_Bool l_push_at(struct list *_list_, size_t value, size_t index);
_Bool l_push_back(struct list *_list_, size_t value);
@@ -44,8 +42,7 @@ _Bool l_pop_front(struct list *_list_);
_Bool l_pop_at(struct list *_list_, size_t index);
_Bool l_pop_back(struct list *_list_);
_Bool l_seq_push_front(struct list *_list_, size_t *values, size_t size);
_Bool l_seq_push_at(struct list *_list_, size_t *values, size_t size,
size_t index);
_Bool l_seq_push_at(struct list *_list_, size_t *values, size_t size, size_t index);
_Bool l_seq_push_back(struct list *_list_, size_t *values, size_t size);
_Bool l_seq_pop_at(struct list *_list_, size_t from, size_t to);
struct list *l_seq_sublist(struct list *_list_, size_t from, size_t to);
@@ -102,8 +99,7 @@ struct list *l_new(size_t capacity, struct list_fval *f_val)
_list_->callbacks = ((void *)0);
return _list_;
}
struct list *l_new_custom(size_t capacity, struct list_fval *f_val,
struct cmc_alloc_node *alloc,
struct list *l_new_custom(size_t capacity, struct list_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
{
if (capacity < 1)
@@ -150,8 +146,7 @@ void l_free(struct list *_list_)
_list_->alloc->free(_list_->buffer);
_list_->alloc->free(_list_);
}
void l_customize(struct list *_list_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
void l_customize(struct list *_list_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!alloc)
_list_->alloc = &cmc_alloc_node_default;
@@ -169,8 +164,7 @@ _Bool l_push_front(struct list *_list_, size_t value)
}
if (!l_empty(_list_))
{
memmove(_list_->buffer + 1, _list_->buffer,
_list_->count * sizeof(size_t));
memmove(_list_->buffer + 1, _list_->buffer, _list_->count * sizeof(size_t));
}
_list_->buffer[0] = value;
_list_->count++;
@@ -191,8 +185,7 @@ _Bool l_push_at(struct list *_list_, size_t value, size_t index)
if (!l_resize(_list_, _list_->count * 2))
return 0;
}
memmove(_list_->buffer + index + 1, _list_->buffer + index,
(_list_->count - index) * sizeof(size_t));
memmove(_list_->buffer + index + 1, _list_->buffer + index, (_list_->count - index) * sizeof(size_t));
_list_->buffer[index] = value;
_list_->count++;
_list_->flag = CMC_FLAG_OK;
@@ -220,8 +213,7 @@ _Bool l_pop_front(struct list *_list_)
_list_->flag = CMC_FLAG_EMPTY;
return 0;
}
memmove(_list_->buffer, _list_->buffer + 1,
(_list_->count - 1) * sizeof(size_t));
memmove(_list_->buffer, _list_->buffer + 1, (_list_->count - 1) * sizeof(size_t));
_list_->buffer[--_list_->count] = (size_t){ 0 };
_list_->flag = CMC_FLAG_OK;
if (_list_->callbacks && _list_->callbacks->delete)
@@ -240,8 +232,7 @@ _Bool l_pop_at(struct list *_list_, size_t index)
_list_->flag = CMC_FLAG_RANGE;
return 0;
}
memmove(_list_->buffer + index, _list_->buffer + index + 1,
(_list_->count - index - 1) * sizeof(size_t));
memmove(_list_->buffer + index, _list_->buffer + index + 1, (_list_->count - index - 1) * sizeof(size_t));
_list_->buffer[--_list_->count] = (size_t){ 0 };
_list_->flag = CMC_FLAG_OK;
if (_list_->callbacks && _list_->callbacks->delete)
@@ -270,8 +261,7 @@ _Bool l_seq_push_front(struct list *_list_, size_t *values, size_t size)
if (!l_resize(_list_, _list_->count + size))
return 0;
}
memmove(_list_->buffer + size, _list_->buffer,
_list_->count * sizeof(size_t));
memmove(_list_->buffer + size, _list_->buffer, _list_->count * sizeof(size_t));
memcpy(_list_->buffer, values, size * sizeof(size_t));
_list_->count += size;
_list_->flag = CMC_FLAG_OK;
@@ -279,8 +269,7 @@ _Bool l_seq_push_front(struct list *_list_, size_t *values, size_t size)
_list_->callbacks->create();
return 1;
}
_Bool l_seq_push_at(struct list *_list_, size_t *values, size_t size,
size_t index)
_Bool l_seq_push_at(struct list *_list_, size_t *values, size_t size, size_t index)
{
if (size == 0)
{
@@ -301,8 +290,7 @@ _Bool l_seq_push_at(struct list *_list_, size_t *values, size_t size,
if (!l_resize(_list_, _list_->count + size))
return 0;
}
memmove(_list_->buffer + index + size, _list_->buffer + index,
(_list_->count - index) * sizeof(size_t));
memmove(_list_->buffer + index + size, _list_->buffer + index, (_list_->count - index) * sizeof(size_t));
memcpy(_list_->buffer + index, values, size * sizeof(size_t));
_list_->count += size;
_list_->flag = CMC_FLAG_OK;
@@ -342,8 +330,7 @@ _Bool l_seq_pop_at(struct list *_list_, size_t from, size_t to)
return 0;
}
size_t length = (to - from + 1);
memmove(_list_->buffer + from, _list_->buffer + to + 1,
(_list_->count - to - 1) * sizeof(size_t));
memmove(_list_->buffer + from, _list_->buffer + to + 1, (_list_->count - to - 1) * sizeof(size_t));
memset(_list_->buffer + _list_->count - length, 0, length * sizeof(size_t));
_list_->count -= to - from + 1;
_list_->flag = CMC_FLAG_OK;
@@ -364,16 +351,14 @@ struct list *l_seq_sublist(struct list *_list_, size_t from, size_t to)
return ((void *)0);
}
size_t length = to - from + 1;
struct list *result =
l_new_custom(length, _list_->f_val, _list_->alloc, _list_->callbacks);
struct list *result = l_new_custom(length, _list_->f_val, _list_->alloc, _list_->callbacks);
if (!result)
{
_list_->flag = CMC_FLAG_ALLOC;
return ((void *)0);
}
memcpy(result->buffer, _list_->buffer, length * sizeof(size_t));
memmove(_list_->buffer + from, _list_->buffer + to + 1,
(_list_->count - to - 1) * sizeof(size_t));
memmove(_list_->buffer + from, _list_->buffer + to + 1, (_list_->count - to - 1) * sizeof(size_t));
memset(_list_->buffer + _list_->count - length, 0, length * sizeof(size_t));
_list_->count -= length;
result->count = length;
@@ -520,8 +505,7 @@ _Bool l_resize(struct list *_list_, size_t capacity)
_list_->flag = CMC_FLAG_INVALID;
return 0;
}
size_t *new_buffer =
_list_->alloc->realloc(_list_->buffer, sizeof(size_t) * capacity);
size_t *new_buffer = _list_->alloc->realloc(_list_->buffer, sizeof(size_t) * capacity);
if (!new_buffer)
{
_list_->flag = CMC_FLAG_ALLOC;
@@ -535,8 +519,7 @@ _Bool l_resize(struct list *_list_, size_t capacity)
}
struct list *l_copy_of(struct list *_list_)
{
struct list *result = l_new_custom(_list_->capacity, _list_->f_val,
_list_->alloc, _list_->callbacks);
struct list *result = l_new_custom(_list_->capacity, _list_->f_val, _list_->alloc, _list_->callbacks);
if (!result)
{
_list_->flag = CMC_FLAG_ALLOC;
@@ -566,26 +549,6 @@ _Bool l_equals(struct list *_list1_, struct list *_list2_)
}
return 1;
}
struct cmc_string l_to_string(struct list *_list_)
{
struct cmc_string str;
struct list *l_ = _list_;
int n = snprintf(str.s, cmc_string_len, cmc_cmc_string_fmt_list,
"CMC_PARAM_SNAME((l, list, , , size_t))",
"CMC_PARAM_V((l, list, , , size_t))", l_, l_->buffer,
l_->capacity, l_->count, l_->flag, l_->f_val, l_->alloc,
l_->callbacks);
return n >= 0 ? str : (struct cmc_string){ 0 };
}
_Bool l_print(struct list *_list_, FILE *fptr)
{
for (size_t i = 0; i < _list_->count; i++)
{
if (!_list_->f_val->str(fptr, _list_->buffer[i]))
return 0;
}
return 1;
}
struct list_iter l_iter_start(struct list *target)
{
struct list_iter iter;
+9 -39
View File
@@ -33,13 +33,11 @@ struct queue_iter
_Bool end;
};
struct queue *q_new(size_t capacity, struct queue_fval *f_val);
struct queue *q_new_custom(size_t capacity, struct queue_fval *f_val,
struct cmc_alloc_node *alloc,
struct queue *q_new_custom(size_t capacity, struct queue_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void q_clear(struct queue *_queue_);
void q_free(struct queue *_queue_);
void q_customize(struct queue *_queue_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void q_customize(struct queue *_queue_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
_Bool q_enqueue(struct queue *_queue_, size_t value);
_Bool q_dequeue(struct queue *_queue_);
size_t q_peek(struct queue *_queue_);
@@ -94,8 +92,7 @@ struct queue *q_new(size_t capacity, struct queue_fval *f_val)
_queue_->callbacks = ((void *)0);
return _queue_;
}
struct queue *q_new_custom(size_t capacity, struct queue_fval *f_val,
struct cmc_alloc_node *alloc,
struct queue *q_new_custom(size_t capacity, struct queue_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
{
if (capacity < 1)
@@ -152,8 +149,7 @@ void q_free(struct queue *_queue_)
_queue_->alloc->free(_queue_->buffer);
_queue_->alloc->free(_queue_);
}
void q_customize(struct queue *_queue_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
void q_customize(struct queue *_queue_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!alloc)
_queue_->alloc = &cmc_alloc_node_default;
@@ -170,8 +166,7 @@ _Bool q_enqueue(struct queue *_queue_, size_t value)
return 0;
}
_queue_->buffer[_queue_->back] = value;
_queue_->back =
(_queue_->back == _queue_->capacity - 1) ? 0 : _queue_->back + 1;
_queue_->back = (_queue_->back == _queue_->capacity - 1) ? 0 : _queue_->back + 1;
_queue_->count++;
_queue_->flag = CMC_FLAG_OK;
if (_queue_->callbacks && _queue_->callbacks->create)
@@ -186,8 +181,7 @@ _Bool q_dequeue(struct queue *_queue_)
return 0;
}
_queue_->buffer[_queue_->front] = (size_t){ 0 };
_queue_->front =
(_queue_->front == _queue_->capacity - 1) ? 0 : _queue_->front + 1;
_queue_->front = (_queue_->front == _queue_->capacity - 1) ? 0 : _queue_->front + 1;
_queue_->count--;
_queue_->flag = CMC_FLAG_OK;
if (_queue_->callbacks && _queue_->callbacks->delete)
@@ -276,8 +270,7 @@ success:
}
struct queue *q_copy_of(struct queue *_queue_)
{
struct queue *result = q_new_custom(_queue_->capacity, _queue_->f_val,
_queue_->alloc, _queue_->callbacks);
struct queue *result = q_new_custom(_queue_->capacity, _queue_->f_val, _queue_->alloc, _queue_->callbacks);
if (!result)
{
_queue_->flag = CMC_FLAG_ERROR;
@@ -312,8 +305,7 @@ _Bool q_equals(struct queue *_queue1_, struct queue *_queue2_)
if (_queue1_->count != _queue2_->count)
return 0;
size_t i, j, k;
for (i = _queue1_->front, j = _queue2_->front, k = 0; k < _queue1_->count;
k++)
for (i = _queue1_->front, j = _queue2_->front, k = 0; k < _queue1_->count; k++)
{
if (_queue1_->f_val->cmp(_queue1_->buffer[i], _queue2_->buffer[j]) != 0)
return 0;
@@ -322,27 +314,6 @@ _Bool q_equals(struct queue *_queue1_, struct queue *_queue2_)
}
return 1;
}
struct cmc_string q_to_string(struct queue *_queue_)
{
struct cmc_string str;
struct queue *q_ = _queue_;
int n = snprintf(str.s, cmc_string_len, cmc_cmc_string_fmt_queue,
"CMC_PARAM_SNAME((q, queue, , , size_t))",
"CMC_PARAM_V((q, queue, , , size_t))", q_, q_->buffer,
q_->capacity, q_->count, q_->front, q_->back, q_->flag,
q_->f_val, q_->alloc, q_->callbacks);
return n >= 0 ? str : (struct cmc_string){ 0 };
}
_Bool q_print(struct queue *_queue_, FILE *fptr)
{
for (size_t i = _queue_->front, j = 0; j < _queue_->count; j++)
{
if (!_queue_->f_val->str(fptr, _queue_->buffer[i]))
return 0;
i = (i + 1) % _queue_->capacity;
}
return 1;
}
struct queue_iter q_iter_start(struct queue *target)
{
struct queue_iter iter;
@@ -433,8 +404,7 @@ _Bool q_iter_prev(struct queue_iter *iter)
return 0;
}
iter->end = q_empty(iter->target);
iter->cursor =
(iter->cursor == 0) ? iter->target->capacity - 1 : iter->cursor - 1;
iter->cursor = (iter->cursor == 0) ? iter->target->capacity - 1 : iter->cursor - 1;
iter->index--;
return 1;
}
+16 -54
View File
@@ -31,13 +31,11 @@ struct sortedlist_iter
_Bool end;
};
struct sortedlist *sl_new(size_t capacity, struct sortedlist_fval *f_val);
struct sortedlist *sl_new_custom(size_t capacity, struct sortedlist_fval *f_val,
struct cmc_alloc_node *alloc,
struct sortedlist *sl_new_custom(size_t capacity, struct sortedlist_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void sl_clear(struct sortedlist *_list_);
void sl_free(struct sortedlist *_list_);
void sl_customize(struct sortedlist *_list_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void sl_customize(struct sortedlist *_list_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
_Bool sl_insert(struct sortedlist *_list_, size_t value);
_Bool sl_remove(struct sortedlist *_list_, size_t index);
size_t sl_max(struct sortedlist *_list_);
@@ -69,14 +67,10 @@ _Bool sl_iter_rewind(struct sortedlist_iter *iter, size_t steps);
_Bool sl_iter_go_to(struct sortedlist_iter *iter, size_t index);
size_t sl_iter_value(struct sortedlist_iter *iter);
size_t sl_iter_index(struct sortedlist_iter *iter);
static size_t sl_impl_binary_search_first(struct sortedlist *_list_,
size_t value);
static size_t sl_impl_binary_search_last(struct sortedlist *_list_,
size_t value);
void sl_impl_sort_quicksort(size_t *array, int (*cmp)(size_t, size_t),
size_t low, size_t high);
void sl_impl_sort_insertion(size_t *array, int (*cmp)(size_t, size_t),
size_t low, size_t high);
static size_t sl_impl_binary_search_first(struct sortedlist *_list_, size_t value);
static size_t sl_impl_binary_search_last(struct sortedlist *_list_, size_t value);
void sl_impl_sort_quicksort(size_t *array, int (*cmp)(size_t, size_t), size_t low, size_t high);
void sl_impl_sort_insertion(size_t *array, int (*cmp)(size_t, size_t), size_t low, size_t high);
struct sortedlist *sl_new(size_t capacity, struct sortedlist_fval *f_val)
{
struct cmc_alloc_node *alloc = &cmc_alloc_node_default;
@@ -102,8 +96,7 @@ struct sortedlist *sl_new(size_t capacity, struct sortedlist_fval *f_val)
_list_->callbacks = ((void *)0);
return _list_;
}
struct sortedlist *sl_new_custom(size_t capacity, struct sortedlist_fval *f_val,
struct cmc_alloc_node *alloc,
struct sortedlist *sl_new_custom(size_t capacity, struct sortedlist_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
{
if (capacity < 1)
@@ -149,8 +142,7 @@ void sl_free(struct sortedlist *_list_)
_list_->alloc->free(_list_->buffer);
_list_->alloc->free(_list_);
}
void sl_customize(struct sortedlist *_list_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
void sl_customize(struct sortedlist *_list_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!alloc)
_list_->alloc = &cmc_alloc_node_default;
@@ -185,8 +177,7 @@ _Bool sl_remove(struct sortedlist *_list_, size_t index)
_list_->flag = CMC_FLAG_RANGE;
return 0;
}
memmove(_list_->buffer + index, _list_->buffer + index + 1,
(_list_->count - index) * sizeof(size_t));
memmove(_list_->buffer + index, _list_->buffer + index + 1, (_list_->count - index) * sizeof(size_t));
_list_->buffer[--_list_->count] = (size_t){ 0 };
_list_->flag = CMC_FLAG_OK;
if (_list_->callbacks && _list_->callbacks->delete)
@@ -288,8 +279,7 @@ _Bool sl_resize(struct sortedlist *_list_, size_t capacity)
_list_->flag = CMC_FLAG_INVALID;
return 0;
}
size_t *new_buffer =
_list_->alloc->realloc(_list_->buffer, sizeof(size_t) * capacity);
size_t *new_buffer = _list_->alloc->realloc(_list_->buffer, sizeof(size_t) * capacity);
if (!new_buffer)
{
_list_->flag = CMC_FLAG_ALLOC;
@@ -308,15 +298,13 @@ void sl_sort(struct sortedlist *_list_)
_list_->flag = CMC_FLAG_OK;
if (!_list_->is_sorted && _list_->count > 1)
{
sl_impl_sort_quicksort(_list_->buffer, _list_->f_val->cmp, 0,
_list_->count - 1);
sl_impl_sort_quicksort(_list_->buffer, _list_->f_val->cmp, 0, _list_->count - 1);
_list_->is_sorted = 1;
}
}
struct sortedlist *sl_copy_of(struct sortedlist *_list_)
{
struct sortedlist *result = sl_new_custom(_list_->capacity, _list_->f_val,
_list_->alloc, _list_->callbacks);
struct sortedlist *result = sl_new_custom(_list_->capacity, _list_->f_val, _list_->alloc, _list_->callbacks);
if (!result)
{
_list_->flag = CMC_FLAG_ERROR;
@@ -346,28 +334,6 @@ _Bool sl_equals(struct sortedlist *_list1_, struct sortedlist *_list2_)
}
return 1;
}
struct cmc_string sl_to_string(struct sortedlist *_list_)
{
struct cmc_string str;
struct sortedlist *l_ = _list_;
int n =
snprintf(str.s, cmc_string_len, cmc_cmc_string_fmt_sortedlist,
"CMC_PARAM_SNAME((sl, sortedlist, , , size_t))",
"CMC_PARAM_V((sl, sortedlist, , , size_t))", l_, l_->buffer,
l_->capacity, l_->count, l_->is_sorted ? "true" : "false",
l_->flag, l_->f_val, l_->alloc, l_->callbacks);
return n >= 0 ? str : (struct cmc_string){ 0 };
}
_Bool sl_print(struct sortedlist *_list_, FILE *fptr)
{
sl_sort(_list_);
for (size_t i = 0; i < _list_->count; i++)
{
if (!_list_->f_val->str(fptr, _list_->buffer[i]))
return 0;
}
return 1;
}
struct sortedlist_iter sl_iter_start(struct sortedlist *target)
{
sl_sort(target);
@@ -496,8 +462,7 @@ size_t sl_iter_index(struct sortedlist_iter *iter)
{
return iter->cursor;
}
static size_t sl_impl_binary_search_first(struct sortedlist *_list_,
size_t value)
static size_t sl_impl_binary_search_first(struct sortedlist *_list_, size_t value)
{
if (sl_empty(_list_))
return 1;
@@ -515,8 +480,7 @@ static size_t sl_impl_binary_search_first(struct sortedlist *_list_,
return L;
return _list_->count;
}
static size_t sl_impl_binary_search_last(struct sortedlist *_list_,
size_t value)
static size_t sl_impl_binary_search_last(struct sortedlist *_list_, size_t value)
{
if (sl_empty(_list_))
return 1;
@@ -534,8 +498,7 @@ static size_t sl_impl_binary_search_last(struct sortedlist *_list_,
return L - 1;
return _list_->count;
}
void sl_impl_sort_quicksort(size_t *array, int (*cmp)(size_t, size_t),
size_t low, size_t high)
void sl_impl_sort_quicksort(size_t *array, int (*cmp)(size_t, size_t), size_t low, size_t high)
{
while (low < high)
{
@@ -574,8 +537,7 @@ void sl_impl_sort_quicksort(size_t *array, int (*cmp)(size_t, size_t),
}
}
}
void sl_impl_sort_insertion(size_t *array, int (*cmp)(size_t, size_t),
size_t low, size_t high)
void sl_impl_sort_insertion(size_t *array, int (*cmp)(size_t, size_t), size_t low, size_t high)
{
for (size_t i = low + 1; i <= high; i++)
{
+7 -34
View File
@@ -30,13 +30,11 @@ struct stack_iter
_Bool end;
};
struct stack *s_new(size_t capacity, struct stack_fval *f_val);
struct stack *s_new_custom(size_t capacity, struct stack_fval *f_val,
struct cmc_alloc_node *alloc,
struct stack *s_new_custom(size_t capacity, struct stack_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void s_clear(struct stack *_stack_);
void s_free(struct stack *_stack_);
void s_customize(struct stack *_stack_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void s_customize(struct stack *_stack_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
_Bool s_push(struct stack *_stack_, size_t value);
_Bool s_pop(struct stack *_stack_);
size_t s_top(struct stack *_stack_);
@@ -89,8 +87,7 @@ struct stack *s_new(size_t capacity, struct stack_fval *f_val)
_stack_->callbacks = ((void *)0);
return _stack_;
}
struct stack *s_new_custom(size_t capacity, struct stack_fval *f_val,
struct cmc_alloc_node *alloc,
struct stack *s_new_custom(size_t capacity, struct stack_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
{
if (capacity < 1)
@@ -137,8 +134,7 @@ void s_free(struct stack *_stack_)
_stack_->alloc->free(_stack_->buffer);
_stack_->alloc->free(_stack_);
}
void s_customize(struct stack *_stack_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
void s_customize(struct stack *_stack_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!alloc)
_stack_->alloc = &cmc_alloc_node_default;
@@ -230,8 +226,7 @@ _Bool s_resize(struct stack *_stack_, size_t capacity)
_stack_->flag = CMC_FLAG_INVALID;
return 0;
}
size_t *new_buffer =
_stack_->alloc->realloc(_stack_->buffer, sizeof(size_t) * capacity);
size_t *new_buffer = _stack_->alloc->realloc(_stack_->buffer, sizeof(size_t) * capacity);
if (!new_buffer)
{
_stack_->flag = CMC_FLAG_ALLOC;
@@ -247,8 +242,7 @@ success:
}
struct stack *s_copy_of(struct stack *_stack_)
{
struct stack *result = s_new_custom(_stack_->capacity, _stack_->f_val,
_stack_->alloc, _stack_->callbacks);
struct stack *result = s_new_custom(_stack_->capacity, _stack_->f_val, _stack_->alloc, _stack_->callbacks);
if (!result)
{
_stack_->flag = CMC_FLAG_ERROR;
@@ -260,8 +254,7 @@ struct stack *s_copy_of(struct stack *_stack_)
result->buffer[i] = _stack_->f_val->cpy(_stack_->buffer[i]);
}
else
memcpy(result->buffer, _stack_->buffer,
sizeof(size_t) * _stack_->count);
memcpy(result->buffer, _stack_->buffer, sizeof(size_t) * _stack_->count);
result->count = _stack_->count;
_stack_->flag = CMC_FLAG_OK;
return result;
@@ -279,26 +272,6 @@ _Bool s_equals(struct stack *_stack1_, struct stack *_stack2_)
}
return 1;
}
struct cmc_string s_to_string(struct stack *_stack_)
{
struct cmc_string str;
struct stack *s_ = _stack_;
int n = snprintf(str.s, cmc_string_len, cmc_cmc_string_fmt_stack,
"CMC_PARAM_SNAME((s, stack, , , size_t))",
"CMC_PARAM_V((s, stack, , , size_t))", s_, s_->buffer,
s_->capacity, s_->count, s_->flag, s_->f_val, s_->alloc,
s_->callbacks);
return n >= 0 ? str : (struct cmc_string){ 0 };
}
_Bool s_print(struct stack *_stack_, FILE *fptr)
{
for (size_t i = 0; i < _stack_->count; i++)
{
if (!_stack_->f_val->str(fptr, _stack_->buffer[i]))
return 0;
}
return 1;
}
struct stack_iter s_iter_start(struct stack *target)
{
struct stack_iter iter;
+11 -69
View File
@@ -51,17 +51,13 @@ struct treemap_iter
_Bool end;
};
struct treemap *tm_new(struct treemap_fkey *f_key, struct treemap_fval *f_val);
struct treemap *tm_new_custom(struct treemap_fkey *f_key,
struct treemap_fval *f_val,
struct cmc_alloc_node *alloc,
struct treemap *tm_new_custom(struct treemap_fkey *f_key, struct treemap_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void tm_clear(struct treemap *_map_);
void tm_free(struct treemap *_map_);
void tm_customize(struct treemap *_map_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void tm_customize(struct treemap *_map_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
_Bool tm_insert(struct treemap *_map_, size_t key, size_t value);
_Bool tm_update(struct treemap *_map_, size_t key, size_t new_value,
size_t *old_value);
_Bool tm_update(struct treemap *_map_, size_t key, size_t new_value, size_t *old_value);
_Bool tm_remove(struct treemap *_map_, size_t key, size_t *out_value);
_Bool tm_max(struct treemap *_map_, size_t *key, size_t *value);
_Bool tm_min(struct treemap *_map_, size_t *key, size_t *value);
@@ -90,8 +86,7 @@ size_t tm_iter_key(struct treemap_iter *iter);
size_t tm_iter_value(struct treemap_iter *iter);
size_t *tm_iter_rvalue(struct treemap_iter *iter);
size_t tm_iter_index(struct treemap_iter *iter);
static struct treemap_node *tm_impl_new_node(struct treemap *_map_, size_t key,
size_t value);
static struct treemap_node *tm_impl_new_node(struct treemap *_map_, size_t key, size_t value);
static struct treemap_node *tm_impl_get_node(struct treemap *_map_, size_t key);
static unsigned char tm_impl_h(struct treemap_node *node);
static unsigned char tm_impl_hupdate(struct treemap_node *node);
@@ -115,9 +110,7 @@ struct treemap *tm_new(struct treemap_fkey *f_key, struct treemap_fval *f_val)
_map_->callbacks = ((void *)0);
return _map_;
}
struct treemap *tm_new_custom(struct treemap_fkey *f_key,
struct treemap_fval *f_val,
struct cmc_alloc_node *alloc,
struct treemap *tm_new_custom(struct treemap_fkey *f_key, struct treemap_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
{
if (!f_key || !f_val)
@@ -198,8 +191,7 @@ void tm_free(struct treemap *_map_)
tm_clear(_map_);
_map_->alloc->free(_map_);
}
void tm_customize(struct treemap *_map_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
void tm_customize(struct treemap *_map_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!alloc)
_map_->alloc = &cmc_alloc_node_default;
@@ -267,8 +259,7 @@ _Bool tm_insert(struct treemap *_map_, size_t key, size_t value)
_map_->callbacks->create();
return 1;
}
_Bool tm_update(struct treemap *_map_, size_t key, size_t new_value,
size_t *old_value)
_Bool tm_update(struct treemap *_map_, size_t key, size_t new_value, size_t *old_value)
{
struct treemap_node *node = tm_impl_get_node(_map_, key);
if (!node)
@@ -491,8 +482,7 @@ int tm_flag(struct treemap *_map_)
}
struct treemap *tm_copy_of(struct treemap *_map_)
{
struct treemap *result =
tm_new_custom(_map_->f_key, _map_->f_val, _map_->alloc, ((void *)0));
struct treemap *result = tm_new_custom(_map_->f_key, _map_->f_val, _map_->alloc, ((void *)0));
if (!result)
{
_map_->flag = CMC_FLAG_ERROR;
@@ -522,8 +512,7 @@ _Bool tm_equals(struct treemap *_map1_, struct treemap *_map2_)
struct treemap_iter iter = tm_iter_start(_map1_);
for (; !tm_iter_at_end(&iter); tm_iter_next(&iter))
{
struct treemap_node *node =
tm_impl_get_node(_map2_, tm_iter_key(&iter));
struct treemap_node *node = tm_impl_get_node(_map2_, tm_iter_key(&iter));
if (node == ((void *)0))
return 0;
if (_map1_->f_val->cmp(node->value, tm_iter_value(&iter)) != 0)
@@ -531,51 +520,6 @@ _Bool tm_equals(struct treemap *_map1_, struct treemap *_map2_)
}
return 1;
}
struct cmc_string tm_to_string(struct treemap *_map_)
{
struct cmc_string str;
struct treemap *m_ = _map_;
int n = snprintf(str.s, cmc_string_len, cmc_cmc_string_fmt_treemap,
"CMC_PARAM_SNAME((tm, treemap, , size_t, size_t))",
"CMC_PARAM_K((tm, treemap, , size_t, size_t))",
"CMC_PARAM_V((tm, treemap, , size_t, size_t))", m_,
m_->root, m_->count, m_->flag, m_->f_key, m_->f_val,
m_->alloc, m_->callbacks);
return n >= 0 ? str : (struct cmc_string){ 0 };
}
_Bool tm_print(struct treemap *_map_, FILE *fptr)
{
struct treemap_node *root = _map_->root;
_Bool left_done = 0;
while (root)
{
if (!left_done)
{
while (root->left)
root = root->left;
}
if (!_map_->f_key->str(fptr, root->key) &&
!_map_->f_val->str(fptr, root->value))
return 0;
left_done = 1;
if (root->right)
{
left_done = 0;
root = root->right;
}
else if (root->parent)
{
while (root->parent && root == root->parent->right)
root = root->parent;
if (!root->parent)
break;
root = root->parent;
}
else
break;
}
return 1;
}
struct treemap_iter tm_iter_start(struct treemap *target)
{
struct treemap_iter iter;
@@ -771,11 +715,9 @@ size_t tm_iter_index(struct treemap_iter *iter)
{
return iter->index;
}
static struct treemap_node *tm_impl_new_node(struct treemap *_map_, size_t key,
size_t value)
static struct treemap_node *tm_impl_new_node(struct treemap *_map_, size_t key, size_t value)
{
struct treemap_node *node =
_map_->alloc->malloc(sizeof(struct treemap_node));
struct treemap_node *node = _map_->alloc->malloc(sizeof(struct treemap_node));
if (!node)
return ((void *)0);
node->key = key;
+16 -75
View File
@@ -40,13 +40,11 @@ struct treeset_iter
_Bool end;
};
struct treeset *ts_new(struct treeset_fval *f_val);
struct treeset *ts_new_custom(struct treeset_fval *f_val,
struct cmc_alloc_node *alloc,
struct treeset *ts_new_custom(struct treeset_fval *f_val, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void ts_clear(struct treeset *_set_);
void ts_free(struct treeset *_set_);
void ts_customize(struct treeset *_set_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks);
void ts_customize(struct treeset *_set_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks);
_Bool ts_insert(struct treeset *_set_, size_t value);
_Bool ts_remove(struct treeset *_set_, size_t value);
_Bool ts_max(struct treeset *_set_, size_t *value);
@@ -62,8 +60,7 @@ _Bool ts_print(struct treeset *_set_, FILE *fptr);
struct treeset *ts_union(struct treeset *_set1_, struct treeset *_set2_);
struct treeset *ts_intersection(struct treeset *_set1_, struct treeset *_set2_);
struct treeset *ts_difference(struct treeset *_set1_, struct treeset *_set2_);
struct treeset *ts_symmetric_difference(struct treeset *_set1_,
struct treeset *_set2_);
struct treeset *ts_symmetric_difference(struct treeset *_set1_, struct treeset *_set2_);
_Bool ts_is_subset(struct treeset *_set1_, struct treeset *_set2_);
_Bool ts_is_superset(struct treeset *_set1_, struct treeset *_set2_);
_Bool ts_is_proper_subset(struct treeset *_set1_, struct treeset *_set2_);
@@ -88,10 +85,8 @@ static inline size_t ts_impl_default_value(void)
memset(&_empty_value_, 0, sizeof(size_t));
return _empty_value_;
}
static struct treeset_node *ts_impl_new_node(struct treeset *_set_,
size_t value);
static struct treeset_node *ts_impl_get_node(struct treeset *_set_,
size_t value);
static struct treeset_node *ts_impl_new_node(struct treeset *_set_, size_t value);
static struct treeset_node *ts_impl_get_node(struct treeset *_set_, size_t value);
static unsigned char ts_impl_h(struct treeset_node *node);
static unsigned char ts_impl_hupdate(struct treeset_node *node);
static void ts_impl_rotate_right(struct treeset_node **Z);
@@ -113,9 +108,7 @@ struct treeset *ts_new(struct treeset_fval *f_val)
_set_->callbacks = ((void *)0);
return _set_;
}
struct treeset *ts_new_custom(struct treeset_fval *f_val,
struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
struct treeset *ts_new_custom(struct treeset_fval *f_val, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!f_val)
return ((void *)0);
@@ -190,8 +183,7 @@ void ts_free(struct treeset *_set_)
ts_clear(_set_);
_set_->alloc->free(_set_);
}
void ts_customize(struct treeset *_set_, struct cmc_alloc_node *alloc,
struct cmc_callbacks *callbacks)
void ts_customize(struct treeset *_set_, struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks)
{
if (!alloc)
_set_->alloc = &cmc_alloc_node_default;
@@ -422,8 +414,7 @@ int ts_flag(struct treeset *_set_)
}
struct treeset *ts_copy_of(struct treeset *_set_)
{
struct treeset *result =
ts_new_custom(_set_->f_val, _set_->alloc, _set_->callbacks);
struct treeset *result = ts_new_custom(_set_->f_val, _set_->alloc, _set_->callbacks);
if (!result)
{
_set_->flag = CMC_FLAG_ERROR;
@@ -457,52 +448,9 @@ _Bool ts_equals(struct treeset *_set1_, struct treeset *_set2_)
}
return 1;
}
struct cmc_string ts_to_string(struct treeset *_set_)
{
struct cmc_string str;
struct treeset *s_ = _set_;
int n = snprintf(str.s, cmc_string_len, cmc_cmc_string_fmt_treeset,
"CMC_PARAM_SNAME((ts, treeset, , , size_t))",
"CMC_PARAM_V((ts, treeset, , , size_t))", s_, s_->root,
s_->count, s_->flag, s_->f_val, s_->alloc, s_->callbacks);
return n >= 0 ? str : (struct cmc_string){ 0 };
}
_Bool ts_print(struct treeset *_set_, FILE *fptr)
{
struct treeset_node *root = _set_->root;
_Bool left_done = 0;
while (root)
{
if (!left_done)
{
while (root->left)
root = root->left;
}
if (!_set_->f_val->str(fptr, root->value))
return 0;
left_done = 1;
if (root->right)
{
left_done = 0;
root = root->right;
}
else if (root->parent)
{
while (root->parent && root == root->parent->right)
root = root->parent;
if (!root->parent)
break;
root = root->parent;
}
else
break;
}
return 1;
}
struct treeset *ts_union(struct treeset *_set1_, struct treeset *_set2_)
{
struct treeset *_set_r_ =
ts_new_custom(_set1_->f_val, _set1_->alloc, _set1_->callbacks);
struct treeset *_set_r_ = ts_new_custom(_set1_->f_val, _set1_->alloc, _set1_->callbacks);
if (!_set_r_)
return ((void *)0);
struct treeset_iter iter1 = ts_iter_start(_set1_);
@@ -519,8 +467,7 @@ struct treeset *ts_union(struct treeset *_set1_, struct treeset *_set2_)
}
struct treeset *ts_intersection(struct treeset *_set1_, struct treeset *_set2_)
{
struct treeset *_set_r_ =
ts_new_custom(_set1_->f_val, _set1_->alloc, _set1_->callbacks);
struct treeset *_set_r_ = ts_new_custom(_set1_->f_val, _set1_->alloc, _set1_->callbacks);
if (!_set_r_)
return ((void *)0);
struct treeset *_set_A_ = _set1_->count < _set2_->count ? _set1_ : _set2_;
@@ -536,8 +483,7 @@ struct treeset *ts_intersection(struct treeset *_set1_, struct treeset *_set2_)
}
struct treeset *ts_difference(struct treeset *_set1_, struct treeset *_set2_)
{
struct treeset *_set_r_ =
ts_new_custom(_set1_->f_val, _set1_->alloc, _set1_->callbacks);
struct treeset *_set_r_ = ts_new_custom(_set1_->f_val, _set1_->alloc, _set1_->callbacks);
if (!_set_r_)
return ((void *)0);
struct treeset_iter iter = ts_iter_start(_set1_);
@@ -549,11 +495,9 @@ struct treeset *ts_difference(struct treeset *_set1_, struct treeset *_set2_)
}
return _set_r_;
}
struct treeset *ts_symmetric_difference(struct treeset *_set1_,
struct treeset *_set2_)
struct treeset *ts_symmetric_difference(struct treeset *_set1_, struct treeset *_set2_)
{
struct treeset *_set_r_ =
ts_new_custom(_set1_->f_val, _set1_->alloc, _set1_->callbacks);
struct treeset *_set_r_ = ts_new_custom(_set1_->f_val, _set1_->alloc, _set1_->callbacks);
if (!_set_r_)
return ((void *)0);
struct treeset_iter iter1 = ts_iter_start(_set1_);
@@ -811,11 +755,9 @@ size_t ts_iter_index(struct treeset_iter *iter)
{
return iter->index;
}
static struct treeset_node *ts_impl_new_node(struct treeset *_set_,
size_t value)
static struct treeset_node *ts_impl_new_node(struct treeset *_set_, size_t value)
{
struct treeset_node *node =
_set_->alloc->malloc(sizeof(struct treeset_node));
struct treeset_node *node = _set_->alloc->malloc(sizeof(struct treeset_node));
if (!node)
return ((void *)0);
node->value = value;
@@ -825,8 +767,7 @@ static struct treeset_node *ts_impl_new_node(struct treeset *_set_,
node->height = 0;
return node;
}
static struct treeset_node *ts_impl_get_node(struct treeset *_set_,
size_t value)
static struct treeset_node *ts_impl_get_node(struct treeset *_set_, size_t value)
{
struct treeset_node *scan = _set_->root;
while (scan != ((void *)0))
+17 -34
View File
@@ -4,9 +4,8 @@
#include "../src/bitset.c"
struct cmc_alloc_node *bs_alloc_node = &(struct cmc_alloc_node){
.malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free
};
struct cmc_alloc_node *bs_alloc_node =
&(struct cmc_alloc_node){ .malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free };
CMC_CREATE_UNIT(BitSet, true, {
const size_t word_bytes = sizeof(cmc_bitset_word);
@@ -137,18 +136,14 @@ CMC_CREATE_UNIT(BitSet, true, {
bs->buffer[0] = 0;
cmc_assert(bs_set(bs, word_bits - 1));
cmc_assert_equals(CMC_BITSET_WORD_TYPE,
((cmc_bitset_word)1 << (word_bits - 1)),
bs->buffer[0]);
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ((cmc_bitset_word)1 << (word_bits - 1)), bs->buffer[0]);
cmc_assert(bs_set(bs, word_bits * 8 - 1));
cmc_assert_equals(CMC_BITSET_WORD_TYPE,
((cmc_bitset_word)1 << (word_bits - 1)),
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ((cmc_bitset_word)1 << (word_bits - 1)),
bs->buffer[bs_bit_to_index(word_bits * 8 - 1)]);
cmc_assert(bs_set(bs, word_bits * 18 - 2));
cmc_assert_equals(CMC_BITSET_WORD_TYPE,
((cmc_bitset_word)1 << (word_bits - 2)),
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ((cmc_bitset_word)1 << (word_bits - 2)),
bs->buffer[bs_bit_to_index(word_bits * 18 - 2)]);
bs_free(bs);
@@ -177,28 +172,23 @@ CMC_CREATE_UNIT(BitSet, true, {
cmc_assert_equals(uint8_t, 244, bs->buffer[0]);
cmc_assert(bs_set_range(bs, word_bits * 6 - 2, word_bits * 6 - 2));
cmc_assert_equals(CMC_BITSET_WORD_TYPE,
((cmc_bitset_word)1 << (word_bits - 2)),
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ((cmc_bitset_word)1 << (word_bits - 2)),
bs->buffer[bs_bit_to_index(word_bits * 6 - 2)]);
// Make sure we have at least word_bits * 40 slots
bs_set(bs, word_bits * 40);
cmc_assert(bs_set_range(bs, word_bits * 20, word_bits * 21 - 1));
cmc_assert_equals(CMC_BITSET_WORD_TYPE, (~(cmc_bitset_word)0),
bs->buffer[bs_bit_to_index(word_bits * 20)]);
cmc_assert_equals(CMC_BITSET_WORD_TYPE, (~(cmc_bitset_word)0), bs->buffer[bs_bit_to_index(word_bits * 20)]);
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0,
bs->buffer[bs_bit_to_index(word_bits * 20 - 1)]);
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0,
bs->buffer[bs_bit_to_index(word_bits * 21 + 1)]);
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[bs_bit_to_index(word_bits * 20 - 1)]);
cmc_assert_equals(CMC_BITSET_WORD_TYPE, 0, bs->buffer[bs_bit_to_index(word_bits * 21 + 1)]);
cmc_assert(bs_set_range(bs, 0, word_bits * 100 - 1));
cmc_assert_equals(size_t, 100, bs->capacity);
for (size_t i = 0; i < bs->capacity; i++)
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ~((cmc_bitset_word)0),
bs->buffer[i]);
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ~((cmc_bitset_word)0), bs->buffer[i]);
bs_free(bs);
});
@@ -239,8 +229,7 @@ CMC_CREATE_UNIT(BitSet, true, {
cmc_assert(bs_set_range(bs, word_bits * 20, word_bits * 21 - 1));
cmc_assert(bs_clear_range(bs, word_bits * 20, word_bits * 21 - 1));
cmc_assert_equals(uint32_t, 0,
bs->buffer[bs_bit_to_index(word_bits * 20)]);
cmc_assert_equals(uint32_t, 0, bs->buffer[bs_bit_to_index(word_bits * 20)]);
for (size_t i = 0; i < bs->capacity; i++)
bs->buffer[i] = ~((cmc_bitset_word)0);
@@ -309,8 +298,7 @@ CMC_CREATE_UNIT(BitSet, true, {
cmc_assert_equals(size_t, 100, bs->capacity);
for (size_t i = 0; i < bs->capacity; i++)
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ~((cmc_bitset_word)0),
bs->buffer[i]);
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ~((cmc_bitset_word)0), bs->buffer[i]);
bs_free(bs);
});
@@ -359,8 +347,7 @@ CMC_CREATE_UNIT(BitSet, true, {
cmc_assert_equals(size_t, 100, bs->capacity);
for (size_t i = 0; i < bs->capacity; i++)
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ~((cmc_bitset_word)0),
bs->buffer[i]);
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ~((cmc_bitset_word)0), bs->buffer[i]);
cmc_assert(bs_put_range(bs, 0, word_bits * 100 - 1, false));
for (size_t i = 0; i < bs->capacity; i++)
@@ -377,8 +364,7 @@ CMC_CREATE_UNIT(BitSet, true, {
cmc_assert(bs_set_all(bs));
for (size_t i = 0; i < bs->capacity; i++)
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ~((cmc_bitset_word)0),
bs->buffer[i]);
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ~((cmc_bitset_word)0), bs->buffer[i]);
bs_free(bs);
});
@@ -404,8 +390,7 @@ CMC_CREATE_UNIT(BitSet, true, {
cmc_assert(bs_flip_all(bs));
for (size_t i = 0; i < bs->capacity; i++)
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ~((cmc_bitset_word)0),
bs->buffer[i]);
cmc_assert_equals(CMC_BITSET_WORD_TYPE, ~((cmc_bitset_word)0), bs->buffer[i]);
cmc_assert(bs_flip_all(bs));
for (size_t i = 0; i < bs->capacity; i++)
@@ -457,12 +442,10 @@ int main(void)
{
int result = BitSet() + BitSetIter();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | BitSet Suit : %-47s |\n", result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+22 -42
View File
@@ -4,24 +4,17 @@
#include "../src/deque.c"
struct deque_fval *d_fval = &(struct deque_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct deque_fval *d_fval_counter = &(struct deque_fval){ .cmp = v_c_cmp,
.cpy = v_c_cpy,
.str = v_c_str,
.free = v_c_free,
.hash = v_c_hash,
.pri = v_c_pri };
struct cmc_alloc_node *d_alloc_node = &(struct cmc_alloc_node){
.malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free
struct deque_fval *d_fval = &(struct deque_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
struct deque_fval *d_fval_counter = &(struct deque_fval){
.cmp = v_c_cmp, .cpy = v_c_cpy, .str = v_c_str, .free = v_c_free, .hash = v_c_hash, .pri = v_c_pri
};
struct cmc_alloc_node *d_alloc_node =
&(struct cmc_alloc_node){ .malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free };
CMC_CREATE_UNIT(Deque, true, {
CMC_CREATE_TEST(PFX##_new(), {
struct deque *d = d_new(1000000, d_fval);
@@ -50,8 +43,7 @@ CMC_CREATE_UNIT(Deque, true, {
});
CMC_CREATE_TEST(PFX##_new_custom(), {
struct deque *d =
d_new_custom(1000000, d_fval, d_alloc_node, callbacks);
struct deque *d = d_new_custom(1000000, d_fval, d_alloc_node, callbacks);
cmc_assert_not_equals(ptr, NULL, d);
cmc_assert_not_equals(ptr, NULL, d->buffer);
@@ -176,8 +168,7 @@ CMC_CREATE_UNIT(Deque, true, {
cmc_assert_equals(size_t, 1000, d_count(d));
size_t sum = 0;
for (size_t i = d->front, j = 0; j < d->count;
i = (i + 1) % d->capacity, j++)
for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++)
sum += d->buffer[i];
cmc_assert_equals(size_t, 500500, sum);
@@ -219,8 +210,7 @@ CMC_CREATE_UNIT(Deque, true, {
cmc_assert_equals(size_t, 1000, d_count(d));
size_t sum = 0;
for (size_t i = d->front, j = 0; j < d->count;
i = (i + 1) % d->capacity, j++)
for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++)
sum += d->buffer[i];
cmc_assert_equals(size_t, 500500, sum);
@@ -319,8 +309,7 @@ CMC_CREATE_UNIT(Deque, true, {
size_t sum = 0;
for (size_t i = d->front, j = 0; j < d->count;
i = (i + 1) % d->capacity, j++)
for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++)
{
sum += d->buffer[i];
}
@@ -367,8 +356,7 @@ CMC_CREATE_UNIT(Deque, true, {
size_t sum = 0;
for (size_t i = d->front, j = 0; j < d->count;
i = (i + 1) % d->capacity, j++)
for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++)
{
sum += d->buffer[i];
}
@@ -417,8 +405,7 @@ CMC_CREATE_UNIT(Deque, true, {
size_t sum = 0;
for (size_t i = d->front, j = 0; j < d->count;
i = (i + 1) % d->capacity, j++)
for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++)
{
sum += d->buffer[i];
}
@@ -437,8 +424,7 @@ CMC_CREATE_UNIT(Deque, true, {
sum = 0;
for (size_t i = d->front, j = 0; j < d->count;
i = (i + 1) % d->capacity, j++)
for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++)
{
sum += d->buffer[i];
}
@@ -482,8 +468,7 @@ CMC_CREATE_UNIT(Deque, true, {
size_t sum = 0;
for (size_t i = d->front, j = 0; j < d->count;
i = (i + 1) % d->capacity, j++)
for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++)
{
sum += d->buffer[i];
}
@@ -502,8 +487,7 @@ CMC_CREATE_UNIT(Deque, true, {
sum = 0;
for (size_t i = d->front, j = 0; j < d->count;
i = (i + 1) % d->capacity, j++)
for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++)
{
sum += d->buffer[i];
}
@@ -802,8 +786,7 @@ CMC_CREATE_UNIT(Deque, true, {
size_t sum = 0;
for (size_t i = d->front, j = 0; j < d->count;
i = (i + 1) % d->capacity, j++)
for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++)
{
sum += d->buffer[i];
}
@@ -1093,8 +1076,7 @@ CMC_CREATE_UNIT(Deque, true, {
}
size_t sum = 0;
for (size_t i = d->front, j = 0; j < d->count;
i = (i + 1) % d->capacity, j++)
for (size_t i = d->front, j = 0; j < d->count; i = (i + 1) % d->capacity, j++)
sum += d->buffer[i];
cmc_assert_equals(size_t, 1200, d_count(d));
@@ -1732,12 +1714,10 @@ int main(void)
{
int result = Deque() + DequeIter();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | Deque Suit : %-48s |\n", result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+102 -117
View File
@@ -31,13 +31,12 @@ CMC_CREATE_UNIT(ForEach, true, {
// });
CMC_CREATE_TEST(CMC<Deque>, {
struct deque *d =
d_new(10000, &(struct deque_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
struct deque *d = d_new(10000, &(struct deque_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
for (size_t i = 1; i <= 10000; i++)
d_push_back(d, i);
@@ -81,20 +80,19 @@ CMC_CREATE_UNIT(ForEach, true, {
// });
CMC_CREATE_TEST(CMC<HashMap>, {
struct hashmap *hm =
hm_new(10000, 0.6,
&(struct hashmap_fkey){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp },
&(struct hashmap_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
struct hashmap *hm = hm_new(10000, 0.6,
&(struct hashmap_fkey){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp },
&(struct hashmap_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
for (size_t i = 1; i <= 10000; i++)
hm_insert(hm, i, i);
@@ -123,20 +121,19 @@ CMC_CREATE_UNIT(ForEach, true, {
});
CMC_CREATE_TEST(CMC<HashMultiMap>, {
struct hashmultimap *hmm =
hmm_new(10000, 0.6,
&(struct hashmultimap_fkey){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp },
&(struct hashmultimap_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
struct hashmultimap *hmm = hmm_new(10000, 0.6,
&(struct hashmultimap_fkey){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp },
&(struct hashmultimap_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
for (size_t i = 1; i <= 10000; i++)
hmm_insert(hmm, i, i);
@@ -165,14 +162,13 @@ CMC_CREATE_UNIT(ForEach, true, {
});
CMC_CREATE_TEST(CMC<HashMultiSet>, {
struct hashmultiset *hms =
hms_new(10000, 0.6,
&(struct hashmultiset_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
struct hashmultiset *hms = hms_new(10000, 0.6,
&(struct hashmultiset_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
for (size_t i = 1; i <= 10000; i++)
hms_insert_many(hms, i, i);
@@ -201,14 +197,13 @@ CMC_CREATE_UNIT(ForEach, true, {
});
CMC_CREATE_TEST(CMC<HashSet>, {
struct hashset *hs =
hs_new(10000, 0.6,
&(struct hashset_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
struct hashset *hs = hs_new(10000, 0.6,
&(struct hashset_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
for (size_t i = 1; i <= 10000; i++)
hs_insert(hs, i);
@@ -260,13 +255,12 @@ CMC_CREATE_UNIT(ForEach, true, {
});
CMC_CREATE_TEST(CMC<IntervalHeap>, {
struct intervalheap *ih =
ih_new(10000, &(struct intervalheap_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
struct intervalheap *ih = ih_new(10000, &(struct intervalheap_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
for (size_t i = 1; i <= 10000; i++)
ih_insert(ih, i);
@@ -289,13 +283,12 @@ CMC_CREATE_UNIT(ForEach, true, {
});
CMC_CREATE_TEST(CMC<LinkedList>, {
struct linkedlist *ll =
ll_new(&(struct linkedlist_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
struct linkedlist *ll = ll_new(&(struct linkedlist_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
for (size_t i = 1; i <= 10000; i++)
ll_push_back(ll, i);
@@ -318,13 +311,12 @@ CMC_CREATE_UNIT(ForEach, true, {
});
CMC_CREATE_TEST(CMC<List>, {
struct list *l =
l_new(10000, &(struct list_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
struct list *l = l_new(10000, &(struct list_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
for (size_t i = 1; i <= 10000; i++)
l_push_back(l, i);
@@ -347,13 +339,12 @@ CMC_CREATE_UNIT(ForEach, true, {
});
CMC_CREATE_TEST(CMC<Queue>, {
struct queue *q =
q_new(10000, &(struct queue_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
struct queue *q = q_new(10000, &(struct queue_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
for (size_t i = 1; i <= 10000; i++)
q_enqueue(q, i);
@@ -376,13 +367,12 @@ CMC_CREATE_UNIT(ForEach, true, {
});
CMC_CREATE_TEST(CMC<SortedList>, {
struct sortedlist *sl =
sl_new(10000, &(struct sortedlist_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
struct sortedlist *sl = sl_new(10000, &(struct sortedlist_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
for (size_t i = 1; i <= 10000; i++)
sl_insert(sl, i);
@@ -405,13 +395,12 @@ CMC_CREATE_UNIT(ForEach, true, {
});
CMC_CREATE_TEST(CMC<Stack>, {
struct stack *s =
s_new(10000, &(struct stack_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
struct stack *s = s_new(10000, &(struct stack_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
for (size_t i = 1; i <= 10000; i++)
s_push(s, i);
@@ -434,19 +423,18 @@ CMC_CREATE_UNIT(ForEach, true, {
});
CMC_CREATE_TEST(CMC<TreeMap>, {
struct treemap *tm =
tm_new(&(struct treemap_fkey){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp },
&(struct treemap_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
struct treemap *tm = tm_new(&(struct treemap_fkey){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp },
&(struct treemap_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
for (size_t i = 1; i <= 10000; i++)
tm_insert(tm, i, i);
@@ -475,13 +463,12 @@ CMC_CREATE_UNIT(ForEach, true, {
});
CMC_CREATE_TEST(CMC<TreeSet>, {
struct treeset *ts =
ts_new(&(struct treeset_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
struct treeset *ts = ts_new(&(struct treeset_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp });
for (size_t i = 1; i <= 10000; i++)
ts_insert(ts, i);
@@ -509,12 +496,10 @@ int main(void)
{
int result = ForEach();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | ForEach Suit : %-46s |\n", result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+37 -72
View File
@@ -4,42 +4,25 @@
#include "../src/hashbidimap.c"
struct hashbidimap_fkey *hbm_fkey =
&(struct hashbidimap_fkey){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct hashbidimap_fval *hbm_fval =
&(struct hashbidimap_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct hashbidimap_fkey *hbm_fkey_counter =
&(struct hashbidimap_fkey){ .cmp = k_c_cmp,
.cpy = k_c_cpy,
.str = k_c_str,
.free = k_c_free,
.hash = k_c_hash,
.pri = k_c_pri };
struct hashbidimap_fval *hbm_fval_counter =
&(struct hashbidimap_fval){ .cmp = v_c_cmp,
.cpy = v_c_cpy,
.str = v_c_str,
.free = v_c_free,
.hash = v_c_hash,
.pri = v_c_pri };
struct cmc_alloc_node *hbm_alloc_node = &(struct cmc_alloc_node){
.malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free
struct hashbidimap_fkey *hbm_fkey = &(struct hashbidimap_fkey){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
struct hashbidimap_fval *hbm_fval = &(struct hashbidimap_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
struct hashbidimap_fkey *hbm_fkey_counter = &(struct hashbidimap_fkey){
.cmp = k_c_cmp, .cpy = k_c_cpy, .str = k_c_str, .free = k_c_free, .hash = k_c_hash, .pri = k_c_pri
};
struct hashbidimap_fval *hbm_fval_counter = &(struct hashbidimap_fval){
.cmp = v_c_cmp, .cpy = v_c_cpy, .str = v_c_str, .free = v_c_free, .hash = v_c_hash, .pri = v_c_pri
};
struct cmc_alloc_node *hbm_alloc_node =
&(struct cmc_alloc_node){ .malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free };
CMC_CREATE_UNIT(HashBidiMap, true, {
CMC_CREATE_TEST(PFX##_new(), {
struct hashbidimap *map = hbm_new(943722, 0.6, hbm_fkey, hbm_fval);
@@ -87,8 +70,7 @@ CMC_CREATE_UNIT(HashBidiMap, true, {
});
CMC_CREATE_TEST(PFX##_new_custom(), {
struct hashbidimap *map = hbm_new_custom(
943722, 0.6, hbm_fkey, hbm_fval, hbm_alloc_node, callbacks);
struct hashbidimap *map = hbm_new_custom(943722, 0.6, hbm_fkey, hbm_fval, hbm_alloc_node, callbacks);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert_not_equals(ptr, NULL, map->buffer);
@@ -123,8 +105,7 @@ CMC_CREATE_UNIT(HashBidiMap, true, {
CMC_CREATE_TEST(PFX##_clear(), {
k_total_free = 0;
v_total_free = 0;
struct hashbidimap *map =
hbm_new(100, 0.6, hbm_fkey_counter, hbm_fval_counter);
struct hashbidimap *map = hbm_new(100, 0.6, hbm_fkey_counter, hbm_fval_counter);
cmc_assert_not_equals(ptr, NULL, map);
@@ -149,8 +130,7 @@ CMC_CREATE_UNIT(HashBidiMap, true, {
CMC_CREATE_TEST(PFX##_free(), {
k_total_free = 0;
v_total_free = 0;
struct hashbidimap *map =
hbm_new(100, 0.6, hbm_fkey_counter, hbm_fval_counter);
struct hashbidimap *map = hbm_new(100, 0.6, hbm_fkey_counter, hbm_fval_counter);
cmc_assert_not_equals(ptr, NULL, map);
@@ -178,8 +158,7 @@ CMC_CREATE_UNIT(HashBidiMap, true, {
});
CMC_CREATE_TEST(customize, {
struct hashbidimap *map =
hbm_new_custom(100, 0.6, hbm_fkey, hbm_fval, NULL, NULL);
struct hashbidimap *map = hbm_new_custom(100, 0.6, hbm_fkey, hbm_fval, NULL, NULL);
cmc_assert_not_equals(ptr, NULL, map);
@@ -313,8 +292,7 @@ CMC_CREATE_UNIT(HashBidiMap, true, {
});
CMC_CREATE_TEST(insert[ftab hash calls], {
struct hashbidimap *map =
hbm_new(100, 0.7, hbm_fkey_counter, hbm_fval_counter);
struct hashbidimap *map = hbm_new(100, 0.7, hbm_fkey_counter, hbm_fval_counter);
cmc_assert_not_equals(ptr, NULL, map);
@@ -468,13 +446,10 @@ CMC_CREATE_UNIT(HashBidiMap, true, {
});
CMC_CREATE_TEST(remove_by_key[cleanup custom], {
struct hashbidimap *map =
hbm_new_custom(10000, 0.6, hbm_fkey, hbm_fval,
&(struct cmc_alloc_node){ .malloc = malloc,
.calloc = calloc,
.realloc = realloc,
.free = free },
&(struct cmc_callbacks){ 0 });
struct hashbidimap *map = hbm_new_custom(
10000, 0.6, hbm_fkey, hbm_fval,
&(struct cmc_alloc_node){ .malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free },
&(struct cmc_callbacks){ 0 });
cmc_assert_not_equals(ptr, NULL, map);
@@ -493,21 +468,17 @@ CMC_CREATE_UNIT(HashBidiMap, true, {
struct hashbidimap_entry *k = map->buffer[i][0];
struct hashbidimap_entry *v = map->buffer[i][1];
cmc_assert((k == NULL || k == CMC_ENTRY_DELETED) &&
(v == NULL || v == CMC_ENTRY_DELETED));
cmc_assert((k == NULL || k == CMC_ENTRY_DELETED) && (v == NULL || v == CMC_ENTRY_DELETED));
}
hbm_free(map);
});
CMC_CREATE_TEST(remove_by_val[cleanup custom], {
struct hashbidimap *map =
hbm_new_custom(10000, 0.6, hbm_fkey, hbm_fval,
&(struct cmc_alloc_node){ .malloc = malloc,
.calloc = calloc,
.realloc = realloc,
.free = free },
&(struct cmc_callbacks){ 0 });
struct hashbidimap *map = hbm_new_custom(
10000, 0.6, hbm_fkey, hbm_fval,
&(struct cmc_alloc_node){ .malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free },
&(struct cmc_callbacks){ 0 });
cmc_assert_not_equals(ptr, NULL, map);
@@ -526,8 +497,7 @@ CMC_CREATE_UNIT(HashBidiMap, true, {
struct hashbidimap_entry *k = map->buffer[i][0];
struct hashbidimap_entry *v = map->buffer[i][1];
cmc_assert((k == NULL || k == CMC_ENTRY_DELETED) &&
(v == NULL || v == CMC_ENTRY_DELETED));
cmc_assert((k == NULL || k == CMC_ENTRY_DELETED) && (v == NULL || v == CMC_ENTRY_DELETED));
}
hbm_free(map);
@@ -601,8 +571,7 @@ CMC_CREATE_UNIT(HashBidiMap, true, {
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map));
// customize
hbm_customize(map, &cmc_alloc_node_default,
&(struct cmc_callbacks){ 0 });
hbm_customize(map, &cmc_alloc_node_default, &(struct cmc_callbacks){ 0 });
cmc_assert_equals(int32_t, CMC_FLAG_OK, hbm_flag(map));
// Insert
@@ -731,8 +700,7 @@ CMC_CREATE_UNIT(HashBidiMap, true, {
});
CMC_CREATE_TEST(callbacks, {
struct hashbidimap *map =
hbm_new_custom(100, 0.7, hbm_fkey, hbm_fval, NULL, callbacks);
struct hashbidimap *map = hbm_new_custom(100, 0.7, hbm_fkey, hbm_fval, NULL, callbacks);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert_equals(ptr, callbacks, map->callbacks);
@@ -835,13 +803,10 @@ int main(void)
{
int result = HashBidiMap() + HashBidiMapIter();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | HashBidiMap Suit : %-42s |\n",
result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" | HashBidiMap Suit : %-42s |\n", result == 0 ? "PASSED" : "FAILED");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+29 -55
View File
@@ -4,38 +4,25 @@
#include "../src/hashmap.c"
struct hashmap_fkey *hm_fkey = &(struct hashmap_fkey){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct hashmap_fval *hm_fval = &(struct hashmap_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct hashmap_fkey *hm_fkey_counter = &(struct hashmap_fkey){ .cmp = k_c_cmp,
.cpy = k_c_cpy,
.str = k_c_str,
.free = k_c_free,
.hash = k_c_hash,
.pri = k_c_pri };
struct hashmap_fval *hm_fval_counter = &(struct hashmap_fval){ .cmp = v_c_cmp,
.cpy = v_c_cpy,
.str = v_c_str,
.free = v_c_free,
.hash = v_c_hash,
.pri = v_c_pri };
struct cmc_alloc_node *hm_alloc_node = &(struct cmc_alloc_node){
.malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free
struct hashmap_fkey *hm_fkey = &(struct hashmap_fkey){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
struct hashmap_fval *hm_fval = &(struct hashmap_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
struct hashmap_fkey *hm_fkey_counter = &(struct hashmap_fkey){
.cmp = k_c_cmp, .cpy = k_c_cpy, .str = k_c_str, .free = k_c_free, .hash = k_c_hash, .pri = k_c_pri
};
struct hashmap_fval *hm_fval_counter = &(struct hashmap_fval){
.cmp = v_c_cmp, .cpy = v_c_cpy, .str = v_c_str, .free = v_c_free, .hash = v_c_hash, .pri = v_c_pri
};
struct cmc_alloc_node *hm_alloc_node =
&(struct cmc_alloc_node){ .malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free };
CMC_CREATE_UNIT(HashMap, true, {
CMC_CREATE_TEST(PFX##_new(), {
struct hashmap *map = hm_new(943722, 0.6, hm_fkey, hm_fval);
@@ -71,8 +58,7 @@ CMC_CREATE_UNIT(HashMap, true, {
});
CMC_CREATE_TEST(PFX##_new_custom(), {
struct hashmap *map = hm_new_custom(943722, 0.6, hm_fkey, hm_fval,
hm_alloc_node, callbacks);
struct hashmap *map = hm_new_custom(943722, 0.6, hm_fkey, hm_fval, hm_alloc_node, callbacks);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert_not_equals(ptr, NULL, map->buffer);
@@ -172,8 +158,7 @@ CMC_CREATE_UNIT(HashMap, true, {
CMC_CREATE_TEST(PFX##_clear(), {
k_total_free = 0;
v_total_free = 0;
struct hashmap *map =
hm_new(100, 0.6, hm_fkey_counter, hm_fval_counter);
struct hashmap *map = hm_new(100, 0.6, hm_fkey_counter, hm_fval_counter);
cmc_assert_not_equals(ptr, NULL, map);
@@ -205,8 +190,7 @@ CMC_CREATE_UNIT(HashMap, true, {
CMC_CREATE_TEST(PFX##_free(), {
k_total_free = 0;
v_total_free = 0;
struct hashmap *map =
hm_new(100, 0.6, hm_fkey_counter, hm_fval_counter);
struct hashmap *map = hm_new(100, 0.6, hm_fkey_counter, hm_fval_counter);
cmc_assert_not_equals(ptr, NULL, map);
@@ -360,8 +344,7 @@ CMC_CREATE_UNIT(HashMap, true, {
capacity = hm_capacity(map);
// Just to be sure, not part of the test
cmc_assert_equals(size_t, cmc_hashtable_primes[0] - 1,
hashcapminus1(0));
cmc_assert_equals(size_t, cmc_hashtable_primes[0] - 1, hashcapminus1(0));
cmc_assert_equals(size_t, capacity - 1, hashcapminus1(0));
cmc_assert(hm_insert(map, 0, 0));
@@ -636,8 +619,7 @@ CMC_CREATE_UNIT(HashMap, true, {
});
CMC_CREATE_TEST(PFX##_full(), {
struct hashmap *map =
hm_new(cmc_hashtable_primes[0], 0.99999, hm_fkey, hm_fval);
struct hashmap *map = hm_new(cmc_hashtable_primes[0], 0.99999, hm_fkey, hm_fval);
cmc_assert_not_equals(ptr, NULL, map);
@@ -676,8 +658,7 @@ CMC_CREATE_UNIT(HashMap, true, {
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert_greater_equals(size_t, (size_t)(2500 / 0.6),
hm_capacity(map));
cmc_assert_greater_equals(size_t, (size_t)(2500 / 0.6), hm_capacity(map));
hm_free(map);
@@ -811,8 +792,7 @@ CMC_CREATE_UNIT(HashMap, true, {
});
CMC_CREATE_TEST(callbacks, {
struct hashmap *map =
hm_new_custom(100, 0.6, hm_fkey, hm_fval, NULL, callbacks);
struct hashmap *map = hm_new_custom(100, 0.6, hm_fkey, hm_fval, NULL, callbacks);
cmc_assert_not_equals(ptr, NULL, map);
@@ -890,13 +870,9 @@ CMC_CREATE_UNIT(HashMap, true, {
});
});
struct hashmap_fkey *hm_fkey_numhash =
&(struct hashmap_fkey){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = numhash,
.pri = cmc_size_cmp };
struct hashmap_fkey *hm_fkey_numhash = &(struct hashmap_fkey){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = numhash, .pri = cmc_size_cmp
};
CMC_CREATE_UNIT(HashMapIter, true, {
CMC_CREATE_TEST(PFX##_iter_start(), {
@@ -1344,12 +1320,10 @@ int main(void)
{
int result = HashMap() + HashMapIter();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | HashMap Suit : %-46s |\n", result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+36 -73
View File
@@ -4,42 +4,25 @@
#include "../src/hashmultimap.c"
struct hashmultimap_fkey *hmm_fkey =
&(struct hashmultimap_fkey){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct hashmultimap_fval *hmm_fval =
&(struct hashmultimap_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct hashmultimap_fkey *hmm_fkey_counter =
&(struct hashmultimap_fkey){ .cmp = k_c_cmp,
.cpy = k_c_cpy,
.str = k_c_str,
.free = k_c_free,
.hash = k_c_hash,
.pri = k_c_pri };
struct hashmultimap_fval *hmm_fval_counter =
&(struct hashmultimap_fval){ .cmp = v_c_cmp,
.cpy = v_c_cpy,
.str = v_c_str,
.free = v_c_free,
.hash = v_c_hash,
.pri = v_c_pri };
struct cmc_alloc_node *hmm_alloc_node = &(struct cmc_alloc_node){
.malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free
struct hashmultimap_fkey *hmm_fkey = &(struct hashmultimap_fkey){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
struct hashmultimap_fval *hmm_fval = &(struct hashmultimap_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
struct hashmultimap_fkey *hmm_fkey_counter = &(struct hashmultimap_fkey){
.cmp = k_c_cmp, .cpy = k_c_cpy, .str = k_c_str, .free = k_c_free, .hash = k_c_hash, .pri = k_c_pri
};
struct hashmultimap_fval *hmm_fval_counter = &(struct hashmultimap_fval){
.cmp = v_c_cmp, .cpy = v_c_cpy, .str = v_c_str, .free = v_c_free, .hash = v_c_hash, .pri = v_c_pri
};
struct cmc_alloc_node *hmm_alloc_node =
&(struct cmc_alloc_node){ .malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free };
CMC_CREATE_UNIT(HashMultiMap, true, {
CMC_CREATE_TEST(PFX##_new(), {
struct hashmultimap *map = hmm_new(943722, 0.6, hmm_fkey, hmm_fval);
@@ -87,8 +70,7 @@ CMC_CREATE_UNIT(HashMultiMap, true, {
});
CMC_CREATE_TEST(PFX##_new_custom(), {
struct hashmultimap *map = hmm_new_custom(
943722, 0.6, hmm_fkey, hmm_fval, hmm_alloc_node, callbacks);
struct hashmultimap *map = hmm_new_custom(943722, 0.6, hmm_fkey, hmm_fval, hmm_alloc_node, callbacks);
cmc_assert_not_equals(ptr, NULL, map);
cmc_assert_not_equals(ptr, NULL, map->buffer);
@@ -123,8 +105,7 @@ CMC_CREATE_UNIT(HashMultiMap, true, {
CMC_CREATE_TEST(PFX##_clear(), {
k_total_free = 0;
v_total_free = 0;
struct hashmultimap *map =
hmm_new(100, 0.6, hmm_fkey_counter, hmm_fval_counter);
struct hashmultimap *map = hmm_new(100, 0.6, hmm_fkey_counter, hmm_fval_counter);
cmc_assert_not_equals(ptr, NULL, map);
@@ -149,8 +130,7 @@ CMC_CREATE_UNIT(HashMultiMap, true, {
CMC_CREATE_TEST(PFX##_free(), {
k_total_free = 0;
v_total_free = 0;
struct hashmultimap *map =
hmm_new(100, 0.6, hmm_fkey_counter, hmm_fval_counter);
struct hashmultimap *map = hmm_new(100, 0.6, hmm_fkey_counter, hmm_fval_counter);
cmc_assert_not_equals(ptr, NULL, map);
@@ -380,8 +360,7 @@ CMC_CREATE_UNIT(HashMultiMap, true, {
});
CMC_CREATE_TEST(callbacks, {
struct hashmultimap *map =
hmm_new_custom(100, 0.8, hmm_fkey, hmm_fval, NULL, callbacks);
struct hashmultimap *map = hmm_new_custom(100, 0.8, hmm_fkey, hmm_fval, NULL, callbacks);
cmc_assert_not_equals(ptr, NULL, map);
@@ -481,18 +460,13 @@ CMC_CREATE_UNIT(HashMultiMap, true, {
});
});
struct hashmultimap_fkey *hmm_fkey_numhash =
&(struct hashmultimap_fkey){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = numhash,
.pri = cmc_size_cmp };
struct hashmultimap_fkey *hmm_fkey_numhash = &(struct hashmultimap_fkey){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = numhash, .pri = cmc_size_cmp
};
CMC_CREATE_UNIT(HashMultiMapIter, true, {
CMC_CREATE_TEST(PFX##_iter_start(), {
struct hashmultimap *map =
hmm_new(100, 0.6, hmm_fkey_numhash, hmm_fval);
struct hashmultimap *map = hmm_new(100, 0.6, hmm_fkey_numhash, hmm_fval);
cmc_assert_not_equals(ptr, NULL, map);
@@ -526,8 +500,7 @@ CMC_CREATE_UNIT(HashMultiMapIter, true, {
});
CMC_CREATE_TEST(PFX##_iter_end(), {
struct hashmultimap *map =
hmm_new(100, 0.6, hmm_fkey_numhash, hmm_fval);
struct hashmultimap *map = hmm_new(100, 0.6, hmm_fkey_numhash, hmm_fval);
cmc_assert_not_equals(ptr, NULL, map);
@@ -607,8 +580,7 @@ CMC_CREATE_UNIT(HashMultiMapIter, true, {
});
CMC_CREATE_TEST(PFX##_iter_to_start(), {
struct hashmultimap *map =
hmm_new(100, 0.6, hmm_fkey_numhash, hmm_fval);
struct hashmultimap *map = hmm_new(100, 0.6, hmm_fkey_numhash, hmm_fval);
cmc_assert_not_equals(ptr, NULL, map);
@@ -639,8 +611,7 @@ CMC_CREATE_UNIT(HashMultiMapIter, true, {
});
CMC_CREATE_TEST(PFX##_iter_to_end(), {
struct hashmultimap *map =
hmm_new(100, 0.6, hmm_fkey_numhash, hmm_fval);
struct hashmultimap *map = hmm_new(100, 0.6, hmm_fkey_numhash, hmm_fval);
cmc_assert_not_equals(ptr, NULL, map);
@@ -681,8 +652,7 @@ CMC_CREATE_UNIT(HashMultiMapIter, true, {
hmm_insert(map, i, i);
size_t sum = 0;
for (it = hmm_iter_start(map); !hmm_iter_at_end(&it);
hmm_iter_next(&it))
for (it = hmm_iter_start(map); !hmm_iter_at_end(&it); hmm_iter_next(&it))
{
sum += hmm_iter_value(&it);
}
@@ -715,8 +685,7 @@ CMC_CREATE_UNIT(HashMultiMapIter, true, {
hmm_insert(map, i, i);
size_t sum = 0;
for (it = hmm_iter_end(map); !hmm_iter_at_start(&it);
hmm_iter_prev(&it))
for (it = hmm_iter_end(map); !hmm_iter_at_start(&it); hmm_iter_prev(&it))
{
sum += hmm_iter_value(&it);
}
@@ -737,8 +706,7 @@ CMC_CREATE_UNIT(HashMultiMapIter, true, {
});
CMC_CREATE_TEST(PFX##_iter_advance(), {
struct hashmultimap *map =
hmm_new(100, 0.6, hmm_fkey_numhash, hmm_fval);
struct hashmultimap *map = hmm_new(100, 0.6, hmm_fkey_numhash, hmm_fval);
cmc_assert_not_equals(ptr, NULL, map);
@@ -772,8 +740,7 @@ CMC_CREATE_UNIT(HashMultiMapIter, true, {
});
CMC_CREATE_TEST(PFX##_iter_rewind(), {
struct hashmultimap *map =
hmm_new(100, 0.6, hmm_fkey_numhash, hmm_fval);
struct hashmultimap *map = hmm_new(100, 0.6, hmm_fkey_numhash, hmm_fval);
cmc_assert_not_equals(ptr, NULL, map);
@@ -807,8 +774,7 @@ CMC_CREATE_UNIT(HashMultiMapIter, true, {
});
CMC_CREATE_TEST(PFX##_iter_go_to(), {
struct hashmultimap *map =
hmm_new(100, 0.6, hmm_fkey_numhash, hmm_fval);
struct hashmultimap *map = hmm_new(100, 0.6, hmm_fkey_numhash, hmm_fval);
cmc_assert_not_equals(ptr, NULL, map);
@@ -944,13 +910,10 @@ int main(void)
{
int result = HashMultiMap() + HashMultiMapIter();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | HashMultiMap Suit : %-41s |\n",
result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" | HashMultiMap Suit : %-41s |\n", result == 0 ? "PASSED" : "FAILED");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+19 -39
View File
@@ -4,26 +4,17 @@
#include "../src/hashmultiset.c"
struct hashmultiset_fval *hms_fval =
&(struct hashmultiset_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct hashmultiset_fval *hms_fval_counter =
&(struct hashmultiset_fval){ .cmp = v_c_cmp,
.cpy = v_c_cpy,
.str = v_c_str,
.free = v_c_free,
.hash = v_c_hash,
.pri = v_c_pri };
struct cmc_alloc_node *hms_alloc_node = &(struct cmc_alloc_node){
.malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free
struct hashmultiset_fval *hms_fval = &(struct hashmultiset_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
struct hashmultiset_fval *hms_fval_counter = &(struct hashmultiset_fval){
.cmp = v_c_cmp, .cpy = v_c_cpy, .str = v_c_str, .free = v_c_free, .hash = v_c_hash, .pri = v_c_pri
};
struct cmc_alloc_node *hms_alloc_node =
&(struct cmc_alloc_node){ .malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free };
CMC_CREATE_UNIT(HashMultiSet, true, {
CMC_CREATE_TEST(PFX##_new(), {
struct hashmultiset *set = hms_new(943722, 0.6, hms_fval);
@@ -55,8 +46,7 @@ CMC_CREATE_UNIT(HashMultiSet, true, {
});
CMC_CREATE_TEST(PFX##_new_custom(), {
struct hashmultiset *set =
hms_new_custom(943722, 0.6, hms_fval, hms_alloc_node, callbacks);
struct hashmultiset *set = hms_new_custom(943722, 0.6, hms_fval, hms_alloc_node, callbacks);
cmc_assert_not_equals(ptr, NULL, set);
cmc_assert_not_equals(ptr, NULL, set->buffer);
@@ -336,8 +326,7 @@ CMC_CREATE_UNIT(HashMultiSet, true, {
});
CMC_CREATE_TEST(callbacks, {
struct hashmultiset *set =
hms_new_custom(100, 0.8, hms_fval, NULL, callbacks);
struct hashmultiset *set = hms_new_custom(100, 0.8, hms_fval, NULL, callbacks);
cmc_assert_not_equals(ptr, NULL, set);
@@ -425,13 +414,9 @@ CMC_CREATE_UNIT(HashMultiSet, true, {
});
});
struct hashmultiset_fval *hms_fval_numhash =
&(struct hashmultiset_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = numhash,
.pri = cmc_size_cmp };
struct hashmultiset_fval *hms_fval_numhash = &(struct hashmultiset_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = numhash, .pri = cmc_size_cmp
};
CMC_CREATE_UNIT(HashMultiSetIter, true, {
CMC_CREATE_TEST(PFX##_iter_start(), {
@@ -621,8 +606,7 @@ CMC_CREATE_UNIT(HashMultiSetIter, true, {
hms_insert(set, i);
size_t sum = 0;
for (it = hms_iter_start(set); !hms_iter_at_end(&it);
hms_iter_next(&it))
for (it = hms_iter_start(set); !hms_iter_at_end(&it); hms_iter_next(&it))
{
sum += hms_iter_value(&it);
}
@@ -655,8 +639,7 @@ CMC_CREATE_UNIT(HashMultiSetIter, true, {
hms_insert(set, i);
size_t sum = 0;
for (it = hms_iter_end(set); !hms_iter_at_start(&it);
hms_iter_prev(&it))
for (it = hms_iter_end(set); !hms_iter_at_start(&it); hms_iter_prev(&it))
{
sum += hms_iter_value(&it);
}
@@ -864,13 +847,10 @@ int main(void)
{
int result = HashMultiSet() + HashMultiSetIter();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | HashMultiSet Suit : %-41s |\n",
result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" | HashMultiSet Suit : %-41s |\n", result == 0 ? "PASSED" : "FAILED");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+18 -35
View File
@@ -4,24 +4,17 @@
#include "../src/hashset.c"
struct hashset_fval *hs_fval = &(struct hashset_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct hashset_fval *hs_fval_counter = &(struct hashset_fval){ .cmp = v_c_cmp,
.cpy = v_c_cpy,
.str = v_c_str,
.free = v_c_free,
.hash = v_c_hash,
.pri = v_c_pri };
struct cmc_alloc_node *hs_alloc_node = &(struct cmc_alloc_node){
.malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free
struct hashset_fval *hs_fval = &(struct hashset_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
struct hashset_fval *hs_fval_counter = &(struct hashset_fval){
.cmp = v_c_cmp, .cpy = v_c_cpy, .str = v_c_str, .free = v_c_free, .hash = v_c_hash, .pri = v_c_pri
};
struct cmc_alloc_node *hs_alloc_node =
&(struct cmc_alloc_node){ .malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free };
CMC_CREATE_UNIT(HashSet, true, {
CMC_CREATE_TEST(PFX##_new(), {
struct hashset *set = hs_new(943722, 0.6, hs_fval);
@@ -53,8 +46,7 @@ CMC_CREATE_UNIT(HashSet, true, {
});
CMC_CREATE_TEST(PFX##_new_custom(), {
struct hashset *set =
hs_new_custom(943722, 0.6, hs_fval, hs_alloc_node, callbacks);
struct hashset *set = hs_new_custom(943722, 0.6, hs_fval, hs_alloc_node, callbacks);
cmc_assert_not_equals(ptr, NULL, set);
cmc_assert_not_equals(ptr, NULL, set->buffer);
@@ -165,8 +157,7 @@ CMC_CREATE_UNIT(HashSet, true, {
cmc_assert(hs_insert(set, capacity - 1));
cmc_assert(hs_insert(set, capacity));
cmc_assert_equals(size_t, capacity - 1,
set->buffer[capacity - 1].value);
cmc_assert_equals(size_t, capacity - 1, set->buffer[capacity - 1].value);
cmc_assert_equals(size_t, capacity, set->buffer[0].value);
hs_fval->hash = cmc_size_hash;
@@ -206,8 +197,7 @@ CMC_CREATE_UNIT(HashSet, true, {
size_t capacity = hs_capacity(set);
// Just to be sure, not part of the test
cmc_assert_equals(size_t, cmc_hashtable_primes[0] - 1,
hashcapminus1(0));
cmc_assert_equals(size_t, cmc_hashtable_primes[0] - 1, hashcapminus1(0));
cmc_assert_equals(size_t, capacity - 1, hashcapminus1(0));
cmc_assert(hs_insert(set, 0));
@@ -451,8 +441,7 @@ CMC_CREATE_UNIT(HashSet, true, {
cmc_assert_not_equals(ptr, NULL, set);
cmc_assert_greater_equals(size_t, (size_t)(2500 / 0.6),
hs_capacity(set));
cmc_assert_greater_equals(size_t, (size_t)(2500 / 0.6), hs_capacity(set));
hs_free(set);
});
@@ -625,13 +614,9 @@ CMC_CREATE_UNIT(HashSet, true, {
});
});
struct hashset_fval *hs_fval_numhash =
&(struct hashset_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = numhash,
.pri = cmc_size_cmp };
struct hashset_fval *hs_fval_numhash = &(struct hashset_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = numhash, .pri = cmc_size_cmp
};
CMC_CREATE_UNIT(HashSetIter, true, {
CMC_CREATE_TEST(PFX##_iter_start(), {
@@ -1042,12 +1027,10 @@ int main(void)
{
int result = HashSet() + HashSetIter();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | HashSet Suit : %-46s |\n", result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+6 -12
View File
@@ -4,12 +4,9 @@
#include "../src/heap.c"
struct heap_fval *h_fval = &(struct heap_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct heap_fval *h_fval = &(struct heap_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
CMC_CREATE_UNIT(Heap, true, {
CMC_CREATE_TEST(new, {
@@ -222,8 +219,7 @@ CMC_CREATE_UNIT(Heap, true, {
});
CMC_CREATE_TEST(callbacks, {
struct heap *h =
h_new_custom(100, CMC_MAX_HEAP, h_fval, NULL, callbacks);
struct heap *h = h_new_custom(100, CMC_MAX_HEAP, h_fval, NULL, callbacks);
cmc_assert_not_equals(ptr, NULL, h);
@@ -720,12 +716,10 @@ int main(void)
{
int result = Heap() + HeapIter();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | Heap Suit : %-49s |\n", result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+13 -26
View File
@@ -4,26 +4,17 @@
#include "../src/intervalheap.c"
struct intervalheap_fval *ih_fval =
&(struct intervalheap_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct intervalheap_fval *ih_fval_counter =
&(struct intervalheap_fval){ .cmp = v_c_cmp,
.cpy = v_c_cpy,
.str = v_c_str,
.free = v_c_free,
.hash = v_c_hash,
.pri = v_c_pri };
struct cmc_alloc_node *ih_alloc_node = &(struct cmc_alloc_node){
.malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free
struct intervalheap_fval *ih_fval = &(struct intervalheap_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
struct intervalheap_fval *ih_fval_counter = &(struct intervalheap_fval){
.cmp = v_c_cmp, .cpy = v_c_cpy, .str = v_c_str, .free = v_c_free, .hash = v_c_hash, .pri = v_c_pri
};
struct cmc_alloc_node *ih_alloc_node =
&(struct cmc_alloc_node){ .malloc = malloc, .calloc = calloc, .realloc = realloc, .free = free };
CMC_CREATE_UNIT(IntervalHeap, true, {
CMC_CREATE_TEST(PFX##_new(), {
struct intervalheap *ih = ih_new(1000000, ih_fval);
@@ -72,8 +63,7 @@ CMC_CREATE_UNIT(IntervalHeap, true, {
});
CMC_CREATE_TEST(PFX##_new_custom(), {
struct intervalheap *ih =
ih_new_custom(1000000, ih_fval, ih_alloc_node, callbacks);
struct intervalheap *ih = ih_new_custom(1000000, ih_fval, ih_alloc_node, callbacks);
cmc_assert_not_equals(ptr, NULL, ih);
cmc_assert_not_equals(ptr, NULL, ih->buffer);
@@ -1044,13 +1034,10 @@ int main(void)
{
int result = IntervalHeap() + IntervalHeapIter();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | IntervalHeap Suit : %-41s |\n",
result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" | IntervalHeap Suit : %-41s |\n", result == 0 ? "PASSED" : "FAILED");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+5 -11
View File
@@ -4,13 +4,9 @@
#include "../src/linkedlist.c"
struct linkedlist_fval *ll_fval =
&(struct linkedlist_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct linkedlist_fval *ll_fval = &(struct linkedlist_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
CMC_CREATE_UNIT(LinkedList, true, {
CMC_CREATE_TEST(new, {
@@ -804,12 +800,10 @@ int main(void)
{
int result = LinkedList() + LinkedListIter();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | LinkedList Suit : %-43s |\n", result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+5 -10
View File
@@ -4,12 +4,9 @@
#include "../src/list.c"
struct list_fval *l_fval = &(struct list_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct list_fval *l_fval = &(struct list_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
CMC_CREATE_UNIT(List, true, {
CMC_CREATE_TEST(new, {
@@ -1070,12 +1067,10 @@ int main(void)
{
int result = List() + ListIter();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | List Suit : %-49s |\n", result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+9 -18
View File
@@ -4,12 +4,9 @@
#include "../src/queue.c"
struct queue_fval *q_fval = &(struct queue_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct queue_fval *q_fval = &(struct queue_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
CMC_CREATE_UNIT(Queue, true, {
CMC_CREATE_TEST(new, {
@@ -78,8 +75,7 @@ CMC_CREATE_UNIT(Queue, true, {
}
size_t sum = 0;
for (size_t i = q->front, j = 0; j < q->count;
i = (i + 1) % q->capacity, j++)
for (size_t i = q->front, j = 0; j < q->count; i = (i + 1) % q->capacity, j++)
sum += q->buffer[i];
cmc_assert_equals(size_t, 1200, q_count(q));
@@ -112,8 +108,7 @@ CMC_CREATE_UNIT(Queue, true, {
size_t sum = 0;
for (size_t i = q->front, j = 0; j < q->count;
i = (i + 1) % q->capacity, j++)
for (size_t i = q->front, j = 0; j < q->count; i = (i + 1) % q->capacity, j++)
{
sum += q->buffer[i];
}
@@ -154,8 +149,7 @@ CMC_CREATE_UNIT(Queue, true, {
size_t sum = 0;
for (size_t i = q->front, j = 0; j < q->count;
i = (i + 1) % q->capacity, j++)
for (size_t i = q->front, j = 0; j < q->count; i = (i + 1) % q->capacity, j++)
{
sum += q->buffer[i];
}
@@ -326,8 +320,7 @@ CMC_CREATE_UNIT(Queue, true, {
size_t sum = 0;
for (size_t i = q->front, j = 0; j < q->count;
i = (i + 1) % q->capacity, j++)
for (size_t i = q->front, j = 0; j < q->count; i = (i + 1) % q->capacity, j++)
{
sum += q->buffer[i];
}
@@ -960,12 +953,10 @@ int main(void)
{
int result = Queue() + QueueIter();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | Queue Suit : %-48s |\n", result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+6 -13
View File
@@ -4,13 +4,9 @@
#include "../src/sortedlist.c"
struct sortedlist_fval *sl_fval =
&(struct sortedlist_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct sortedlist_fval *sl_fval = &(struct sortedlist_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
CMC_CREATE_UNIT(SortedList, true, {
CMC_CREATE_TEST(new, {
@@ -66,8 +62,7 @@ CMC_CREATE_UNIT(SortedList, true, {
sl_sort(sl);
cmc_assert_equals(size_t, 50, sl_count(sl));
cmc_assert_array_sorted_any(size_t, sl->buffer, cmc_size_cmp, 0,
sl->count - 1);
cmc_assert_array_sorted_any(size_t, sl->buffer, cmc_size_cmp, 0, sl->count - 1);
sl_free(sl);
});
@@ -705,12 +700,10 @@ int main(void)
{
int result = SortedList() + SortedListIter();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | SortedList Suit : %-43s |\n", result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+5 -10
View File
@@ -4,12 +4,9 @@
#include "../src/stack.c"
struct stack_fval *s_fval = &(struct stack_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct stack_fval *s_fval = &(struct stack_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
CMC_CREATE_UNIT(Stack, true, {
CMC_CREATE_TEST(new, {
@@ -838,12 +835,10 @@ int main(void)
{
int result = Stack() + StackIter();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | Stack Suit : %-48s |\n", result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+8 -16
View File
@@ -4,19 +4,13 @@
#include "../src/treemap.c"
struct treemap_fkey *tm_fkey = &(struct treemap_fkey){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct treemap_fkey *tm_fkey = &(struct treemap_fkey){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
struct treemap_fval *tm_fval = &(struct treemap_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct treemap_fval *tm_fval = &(struct treemap_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
CMC_CREATE_UNIT(TreeMap, true, {
CMC_CREATE_TEST(new, {
@@ -659,12 +653,10 @@ int main(void)
{
int result = TreeMap() + TreeMapIter();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | TreeMap Suit : %-46s |\n", result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+5 -10
View File
@@ -4,12 +4,9 @@
#include "../src/treeset.c"
struct treeset_fval *ts_fval = &(struct treeset_fval){ .cmp = cmc_size_cmp,
.cpy = NULL,
.str = cmc_size_str,
.free = NULL,
.hash = cmc_size_hash,
.pri = cmc_size_cmp };
struct treeset_fval *ts_fval = &(struct treeset_fval){
.cmp = cmc_size_cmp, .cpy = NULL, .str = cmc_size_str, .free = NULL, .hash = cmc_size_hash, .pri = cmc_size_cmp
};
CMC_CREATE_UNIT(TreeSet, true, {
CMC_CREATE_TEST(new, {
@@ -574,12 +571,10 @@ int main(void)
{
int result = TreeSet() + TreeSetIter();
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n");
printf(" | TreeSet Suit : %-46s |\n", result == 0 ? "PASSED" : "FAILED");
printf(
" +---------------------------------------------------------------+");
printf(" +---------------------------------------------------------------+");
printf("\n\n\n");
return result;
+5 -15
View File
@@ -2,15 +2,6 @@
#ifndef CMC_UNIT_TEST_UTL__
#define CMC_UNIT_TEST_UTL__
/**
* TEMPORARY
*/
struct cmc_string
{
char s[400];
};
static const size_t cmc_string_len = 400;
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
@@ -163,11 +154,10 @@ void callback_resize(void)
total_resize++;
}
struct cmc_callbacks *callbacks =
&(struct cmc_callbacks){ .create = callback_create,
.read = callback_read,
.update = callback_update,
.delete = callback_delete,
.resize = callback_resize };
struct cmc_callbacks *callbacks = &(struct cmc_callbacks){ .create = callback_create,
.read = callback_read,
.update = callback_update,
.delete = callback_delete,
.resize = callback_resize };
#endif /* CMC_UNIT_TEST_UTL__ */