Added _copy_of functions to all collections

This commit is contained in:
Leonardo committed 2019-08-28 16:34:15 -03:00
1 parent e68b8a8e0e
commit 1f50add1bb
13 files changed
+2888 -2490

No files matched your search

+28
View File
@@ -121,6 +121,7 @@
FMOD size_t PFX##_count(SNAME *_deque_); \
FMOD size_t PFX##_capacity(SNAME *_deque_); \
/* Collection Utility */ \
FMOD SNAME *PFX##_copy_of(SNAME *_deque_, V (*copy_func)(V)); \
FMOD cmc_string PFX##_to_string(SNAME *_deque_); \
\
/* Iterator Functions */ \
@@ -363,6 +364,33 @@
return _deque_->capacity; \
} \
\
FMOD SNAME *PFX##_copy_of(SNAME *_deque_, V (*copy_func)(V)) \
{ \
SNAME *result = PFX##_new(_deque_->capacity); \
\
if (!result) \
return NULL; \
\
if (copy_func) \
{ \
for (size_t i = _deque_->front, j = 0; j < _deque_->count; i = (i + 1) % _deque_->capacity, j++) \
{ \
result->buffer[j] = copy_func(_deque_->buffer[i]); \
} \
} \
else \
{ \
for (size_t i = _deque_->front, j = 0; j < _deque_->count; i = (i + 1) % _deque_->capacity, j++) \
{ \
result->buffer[j] = _deque_->buffer[i]; \
} \
} \
\
result->count = _deque_->count; \
\
return result; \
} \
\
FMOD cmc_string PFX##_to_string(SNAME *_deque_) \
{ \
cmc_string str; \
+46
View File
@@ -178,6 +178,7 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
FMOD size_t PFX##_count(SNAME *_map_); \
FMOD size_t PFX##_capacity(SNAME *_map_); \
/* Collection Utility */ \
FMOD SNAME *PFX##_copy_of(SNAME *_map_, K (*key_copy_func)(K), V (*value_copy_func)(V)); \
FMOD cmc_string PFX##_to_string(SNAME *_map_); \
\
/* Iterator Functions */ \
@@ -525,6 +526,51 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
return _map_->capacity; \
} \
\
FMOD SNAME *PFX##_copy_of(SNAME *_map_, K (*key_copy_func)(K), V (*value_copy_func)(V)) \
{ \
SNAME *result = PFX##_new(_map_->capacity, _map_->load, _map_->cmp, _map_->hash); \
\
if (!result) \
return NULL; \
\
if (key_copy_func || value_copy_func) \
{ \
for (size_t i = 0; i < _map_->capacity; i++) \
{ \
SNAME##_entry *scan = &(_map_->buffer[i]); \
\
if (scan->state != CMC_ES_EMPTY) \
{ \
SNAME##_entry *target = &(result->buffer[i]); \
\
if (scan->state == CMC_ES_DELETED) \
target->state = CMC_ES_DELETED; \
else \
{ \
target->state = scan->state; \
target->dist = scan->dist; \
\
if (key_copy_func) \
target->key = key_copy_func(scan->key); \
else \
target->key = scan->key; \
\
if (value_copy_func) \
target->value = value_copy_func(scan->value); \
else \
target->value = scan->value; \
} \
} \
} \
} \
else \
memcpy(result->buffer, _map_->buffer, sizeof(SNAME##_entry) * _map_->capacity); \
\
result->count = _map_->count; \
\
return result; \
} \
\
FMOD cmc_string PFX##_to_string(SNAME *_map_) \
{ \
cmc_string str; \
+38
View File
@@ -169,6 +169,7 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
FMOD size_t PFX##_count(SNAME *_set_); \
FMOD size_t PFX##_capacity(SNAME *_set_); \
/* Collection Utility */ \
FMOD SNAME *PFX##_copy_of(SNAME *_set_, V (*copy_func)(V)); \
FMOD cmc_string PFX##_to_string(SNAME *_set_); \
\
/* Set Operations */ \
@@ -448,6 +449,43 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
return _set_->capacity; \
} \
\
FMOD SNAME *PFX##_copy_of(SNAME *_set_, V (*copy_func)(V)) \
{ \
SNAME *result = PFX##_new(_set_->capacity, _set_->load, _set_->cmp, _set_->hash); \
\
if (!result) \
return NULL; \
\
if (copy_func) \
{ \
for (size_t i = 0; i < _set_->capacity; i++) \
{ \
SNAME##_entry *scan = &(_set_->buffer[i]); \
\
if (scan->state != CMC_ES_EMPTY) \
{ \
SNAME##_entry *target = &(result->buffer[i]); \
\
if (scan->state == CMC_ES_DELETED) \
target->state = CMC_ES_DELETED; \
else \
{ \
target->state = scan->state; \
target->dist = scan->dist; \
\
target->value = copy_func(scan->value); \
} \
} \
} \
} \
else \
memcpy(result->buffer, _set_->buffer, sizeof(SNAME##_entry) * _set_->capacity); \
\
result->count = _set_->count; \
\
return result; \
} \
\
FMOD cmc_string PFX##_to_string(SNAME *_set_) \
{ \
cmc_string str; \
+21
View File
@@ -119,6 +119,7 @@ typedef enum cmc_heap_order_e
FMOD size_t PFX##_count(SNAME *_heap_); \
FMOD size_t PFX##_capacity(SNAME *_heap_); \
/* Collection Utility */ \
FMOD SNAME *PFX##_copy_of(SNAME *_heap_, V (*copy_func)(V)); \
FMOD cmc_string PFX##_to_string(SNAME *_heap_); \
\
/* Iterator Functions */ \
@@ -324,6 +325,26 @@ typedef enum cmc_heap_order_e
return _heap_->capacity; \
} \
\
FMOD SNAME *PFX##_copy_of(SNAME *_heap_, V (*copy_func)(V)) \
{ \
SNAME *result = PFX##_new(_heap_->capacity, _heap_->HO, _heap_->cmp); \
\
if (!result) \
return NULL; \
\
if (copy_func) \
{ \
for (size_t i = 0; i < _heap_->count; i++) \
result->buffer[i] = copy_func(_heap_->buffer[i]); \
} \
else \
memcpy(result->buffer, _heap_->buffer, sizeof(V) * _heap_->count); \
\
result->count = _heap_->count; \
\
return result; \
} \
\
FMOD cmc_string PFX##_to_string(SNAME *_heap_) \
{ \
cmc_string str; \
+41
View File
@@ -126,6 +126,7 @@
FMOD bool PFX##_empty(SNAME *_list_); \
FMOD size_t PFX##_count(SNAME *_list_); \
/* Collection Utility */ \
FMOD SNAME *PFX##_copy_of(SNAME *_list_, V (*copy_func)(V)); \
FMOD cmc_string PFX##_to_string(SNAME *_list_); \
\
/* Node Related Functions */ \
@@ -467,6 +468,46 @@
return _list_->count; \
} \
\
FMOD SNAME *PFX##_copy_of(SNAME *_list_, V (*copy_func)(V)) \
{ \
SNAME *result = PFX##_new(); \
\
if (!result) \
return NULL; \
\
SNAME##_node *scan = _list_->head; \
\
while (scan != NULL) \
{ \
/* This allocation should never fail since it might not be */ \
/* possible to recover from it. That is why it isn't checked */ \
SNAME##_node *new_node; \
\
if (copy_func) \
new_node = PFX##_new_node(copy_func(scan->data)); \
else \
new_node = PFX##_new_node(scan->data); \
\
if (!result->head) \
{ \
result->head = new_node; \
result->tail = new_node; \
} \
else \
{ \
new_node->prev = result->tail; \
result->tail->next = new_node; \
result->tail = new_node; \
} \
\
scan = scan->next; \
} \
\
result->count = _list_->count; \
\
return result; \
} \
\
FMOD cmc_string PFX##_to_string(SNAME *_list_) \
{ \
cmc_string str; \
+21
View File
@@ -135,6 +135,7 @@
FMOD bool PFX##_fits(SNAME *_list_, size_t size); \
FMOD size_t PFX##_capacity(SNAME *_list_); \
/* Collection Utility */ \
FMOD SNAME *PFX##_copy_of(SNAME *_list_, V (*copy_func)(V)); \
FMOD cmc_string PFX##_to_string(SNAME *_list_); \
\
/* Iterator Functions */ \
@@ -564,6 +565,26 @@
return _list_->capacity; \
} \
\
FMOD SNAME *PFX##_copy_of(SNAME *_list_, V (*copy_func)(V)) \
{ \
SNAME *result = PFX##_new(_list_->capacity); \
\
if (!result) \
return NULL; \
\
if (copy_func) \
{ \
for (size_t i = 0; i < _list_->count; i++) \
result->buffer[i] = copy_func(_list_->buffer[i]); \
} \
else \
memcpy(result->buffer, _list_->buffer, sizeof(V) * _list_->count); \
\
result->count = _list_->count; \
\
return result; \
} \
\
FMOD cmc_string PFX##_to_string(SNAME *_list_) \
{ \
cmc_string str; \
+28
View File
@@ -128,6 +128,7 @@
FMOD size_t PFX##_count(SNAME *_queue_); \
FMOD size_t PFX##_capacity(SNAME *_queue_); \
/* Collection Utility */ \
FMOD SNAME *PFX##_copy_of(SNAME *_queue_, V (*copy_func)(V)); \
FMOD cmc_string PFX##_to_string(SNAME *_queue_); \
\
/* Iterator Functions */ \
@@ -313,6 +314,33 @@
return _queue_->capacity; \
} \
\
FMOD SNAME *PFX##_copy_of(SNAME *_queue_, V (*copy_func)(V)) \
{ \
SNAME *result = PFX##_new(_queue_->capacity); \
\
if (!result) \
return NULL; \
\
if (copy_func) \
{ \
for (size_t i = _queue_->front, j = 0; j < _queue_->count; i = (i + 1) % _queue_->capacity, j++) \
{ \
result->buffer[j] = copy_func(_queue_->buffer[i]); \
} \
} \
else \
{ \
for (size_t i = _queue_->front, j = 0; j < _queue_->count; i = (i + 1) % _queue_->capacity, j++) \
{ \
result->buffer[j] = _queue_->buffer[i]; \
} \
} \
\
result->count = _queue_->count; \
\
return result; \
} \
\
FMOD cmc_string PFX##_to_string(SNAME *_queue_) \
{ \
cmc_string str; \
+21
View File
@@ -104,6 +104,7 @@
FMOD size_t PFX##_count(SNAME *_stack_); \
FMOD size_t PFX##_capacity(SNAME *_stack_); \
/* Collection Utility */ \
FMOD SNAME *PFX##_copy_of(SNAME *_stack_, V (*copy_func)(V)); \
FMOD cmc_string PFX##_to_string(SNAME *_stack_); \
\
/* Iterator Functions */ \
@@ -275,6 +276,26 @@
return _stack_->capacity; \
} \
\
FMOD SNAME *PFX##_copy_of(SNAME *_stack_, V (*copy_func)(V)) \
{ \
SNAME *result = PFX##_new(_stack_->capacity); \
\
if (!result) \
return NULL; \
\
if (copy_func) \
{ \
for (size_t i = 0; i < _stack_->count; i++) \
result->buffer[i] = copy_func(_stack_->buffer[i]); \
} \
else \
memcpy(result->buffer, _stack_->buffer, sizeof(V) * _stack_->count); \
\
result->count = _stack_->count; \
\
return result; \
} \
\
FMOD cmc_string PFX##_to_string(SNAME *_stack_) \
{ \
cmc_string str; \
+924 -894
View File
File diff suppressed because it is too large. Load diff
+918 -893
View File
File diff suppressed because it is too large. Load diff
+732 -703
View File
File diff suppressed because it is too large. Load diff
+30
View File
@@ -200,6 +200,7 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
FMOD size_t PFX##_count(SNAME *_map_); \
FMOD size_t PFX##_capacity(SNAME *_map_); \
/* Collection Utility */ \
FMOD SNAME *PFX##_copy_of(SNAME *_map_, K (*key_copy_func)(K), V (*value_copy_func)(V)); \
FMOD cmc_string PFX##_to_string(SNAME *_map_); \
\
/* Iterator Functions */ \
@@ -709,6 +710,35 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
return _map_->capacity; \
} \
\
FMOD SNAME *PFX##_copy_of(SNAME *_map_, K (*key_copy_func)(K), V (*value_copy_func)(V)) \
{ \
SNAME *result = PFX##_new(_map_->capacity, _map_->load, _map_->cmp, _map_->hash); \
\
if (!result) \
return NULL; \
\
SNAME##_iter iter; \
PFX##_iter_init(&iter, _map_); \
\
if (!PFX##_empty(_map_)) \
{ \
for (PFX##_iter_to_start(&iter); !PFX##_iter_end(&iter); PFX##_iter_next(&iter)) \
{ \
K key = PFX##_iter_key(&iter); \
V value = PFX##_iter_value(&iter); \
\
if (key_copy_func) \
key = key_copy_func(key); \
if (value_copy_func) \
value = value_copy_func(value); \
\
PFX##_insert(result, key, value); \
} \
} \
\
return result; \
} \
\
FMOD cmc_string PFX##_to_string(SNAME *_map_) \
{ \
cmc_string str; \
+40
View File
@@ -183,6 +183,7 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
FMOD size_t PFX##_cardinality(SNAME *_set_); \
FMOD size_t PFX##_capacity(SNAME *_set_); \
/* Collection Utility */ \
FMOD SNAME *PFX##_copy_of(SNAME *_set_, V (*copy_func)(V)); \
FMOD cmc_string PFX##_to_string(SNAME *_set_); \
\
/* Set Operations */ \
@@ -513,6 +514,45 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
return _set_->capacity; \
} \
\
FMOD SNAME *PFX##_copy_of(SNAME *_set_, V (*copy_func)(V)) \
{ \
SNAME *result = PFX##_new(_set_->capacity, _set_->load, _set_->cmp, _set_->hash); \
\
if (!result) \
return NULL; \
\
if (copy_func) \
{ \
for (size_t i = 0; i < _set_->capacity; i++) \
{ \
SNAME##_entry *scan = &(_set_->buffer[i]); \
\
if (scan->state != CMC_ES_EMPTY) \
{ \
SNAME##_entry *target = &(result->buffer[i]); \
\
if (scan->state == CMC_ES_DELETED) \
target->state = CMC_ES_DELETED; \
else \
{ \
target->state = scan->state; \
target->dist = scan->dist; \
target->multiplicity = scan->multiplicity; \
\
target->value = copy_func(scan->value); \
} \
} \
} \
} \
else \
memcpy(result->buffer, _set_->buffer, sizeof(SNAME##_entry) * _set_->capacity); \
\
result->count = _set_->count; \
result->cardinality = _set_->cardinality; \
\
return result; \
} \
\
FMOD cmc_string PFX##_to_string(SNAME *_set_) \
{ \
cmc_string str; \