TreeMap no longer depends on ITER

This commit is contained in:
Leonardo Vencovsky committed 2020-07-08 23:00:24 -03:00
1 parent b6343b4857
commit 209d45be5d
5 files changed
+143 -32

No files matched your search

+5 -5
View File
@@ -648,9 +648,9 @@
\ \
/* TODO check this for possible errors where entry == NULL */ \ /* TODO check this for possible errors where entry == NULL */ \
struct CMC_DEF_ENTRY(SNAME) *entry = CMC_(PFX, _impl_insert_and_return)(_new_set_, value, NULL); \ struct CMC_DEF_ENTRY(SNAME) *entry = CMC_(PFX, _impl_insert_and_return)(_new_set_, value, NULL); \
\ \
entry->multiplicity = multiplicity;\ entry->multiplicity = multiplicity; \
/* Setting cardinality not required, _new_set_ is temporary */\ /* Setting cardinality not required, _new_set_ is temporary */ \
} \ } \
} \ } \
\ \
@@ -785,7 +785,7 @@
/* something with it. This function only guarantees that there is */ \ /* something with it. This function only guarantees that there is */ \
/* a valid entry for a given value */ \ /* a valid entry for a given value */ \
\ \
if (new_node)\ if (new_node) \
*new_node = false; \ *new_node = false; \
\ \
struct CMC_DEF_ENTRY(SNAME) *entry = CMC_(PFX, _impl_get_entry)(_set_, value); \ struct CMC_DEF_ENTRY(SNAME) *entry = CMC_(PFX, _impl_get_entry)(_set_, value); \
@@ -793,7 +793,7 @@
if (entry != NULL) \ if (entry != NULL) \
return entry; \ return entry; \
\ \
if (new_node)\ if (new_node) \
*new_node = true; \ *new_node = true; \
\ \
if (CMC_(PFX, _full)(_set_)) \ if (CMC_(PFX, _full)(_set_)) \
+3 -3
View File
@@ -625,9 +625,9 @@
{ \ { \
if (_set_a_->buffer[i].state == CMC_ES_FILLED) \ if (_set_a_->buffer[i].state == CMC_ES_FILLED) \
{ \ { \
\ \
if (!CMC_(PFX, _impl_get_entry)(_set_b_, _set_a_->buffer[i].value))\ if (!CMC_(PFX, _impl_get_entry)(_set_b_, _set_a_->buffer[i].value)) \
return false;\ return false; \
} \ } \
} \ } \
\ \
+73 -13
View File
@@ -688,25 +688,57 @@
return NULL; \ return NULL; \
} \ } \
\ \
/* TODO turn this into a normal loop */ \ struct CMC_DEF_NODE(SNAME) *root = _map_->root; \
struct CMC_DEF_ITER(SNAME) iter = CMC_(PFX, _iter_start)(_map_); \
\ \
for (; !CMC_(PFX, _iter_at_end)(&iter); CMC_(PFX, _iter_next)(&iter)) \ bool left_done = false; \
\
while (root) \
{ \ { \
K key = CMC_(PFX, _iter_key)(&iter); \ if (!left_done) \
V value = CMC_(PFX, _iter_value)(&iter); \ { \
while (root->left) \
root = root->left; \
} \
\
K key; \
V value; \
\ \
if (_map_->f_key->cpy) \ if (_map_->f_key->cpy) \
key = _map_->f_key->cpy(key); \ key = _map_->f_key->cpy(root->key); \
else \
key = root->key; \
if (_map_->f_val->cpy) \ if (_map_->f_val->cpy) \
value = _map_->f_val->cpy(value); \ value = _map_->f_val->cpy(root->value); \
else \
value = root->value; \
\ \
/* TODO check this for errors */ \ /* TODO check this for errors */ \
CMC_(PFX, _insert)(result, key, value); \ CMC_(PFX, _insert)(result, key, value); \
\
left_done = true; \
\
if (root->right) \
{ \
left_done = false; \
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; \
} \ } \
\ \
CMC_CALLBACKS_ASSIGN(result, _map_->callbacks); \
_map_->flag = CMC_FLAG_OK; \ _map_->flag = CMC_FLAG_OK; \
\
CMC_CALLBACKS_ASSIGN(result, _map_->callbacks); \
\ \
return result; \ return result; \
} \ } \
@@ -719,18 +751,46 @@
if (_map1_->count != _map2_->count) \ if (_map1_->count != _map2_->count) \
return false; \ return false; \
\ \
/* TODO turn this into a normal loop */ \ struct CMC_DEF_NODE(SNAME) *root = _map1_->root; \
struct CMC_DEF_ITER(SNAME) iter = CMC_(PFX, _iter_start)(_map1_); \
\ \
for (; !CMC_(PFX, _iter_at_end)(&iter); CMC_(PFX, _iter_next)(&iter)) \ bool left_done = false; \
\
while (root) \
{ \ { \
struct CMC_DEF_NODE(SNAME) *node = CMC_(PFX, _impl_get_node)(_map2_, CMC_(PFX, _iter_key)(&iter)); \ if (!left_done) \
{ \
while (root->left) \
root = root->left; \
} \
\
/* TODO this can be optimized by doing two in-order traversals */\
struct CMC_DEF_NODE(SNAME) *node = CMC_(PFX, _impl_get_node)(_map2_, root->key); \
\ \
if (node == NULL) \ if (node == NULL) \
return false; \ return false; \
\ \
if (_map1_->f_val->cmp(node->value, CMC_(PFX, _iter_value)(&iter)) != 0) \ if (_map1_->f_val->cmp(node->value, root->value) != 0) \
return false; \ return false; \
\
left_done = true; \
\
if (root->right) \
{ \
left_done = false; \
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 true; \ return true; \
+59 -11
View File
@@ -400,19 +400,45 @@ struct treemap *tm_copy_of(struct treemap *_map_)
_map_->flag = CMC_FLAG_ERROR; _map_->flag = CMC_FLAG_ERROR;
return ((void *)0); return ((void *)0);
} }
struct treemap_iter iter = tm_iter_start(_map_); struct treemap_node *root = _map_->root;
for (; !tm_iter_at_end(&iter); tm_iter_next(&iter)) _Bool left_done = 0;
while (root)
{ {
size_t key = tm_iter_key(&iter); if (!left_done)
size_t value = tm_iter_value(&iter); {
while (root->left)
root = root->left;
}
size_t key;
size_t value;
if (_map_->f_key->cpy) if (_map_->f_key->cpy)
key = _map_->f_key->cpy(key); key = _map_->f_key->cpy(root->key);
else
key = root->key;
if (_map_->f_val->cpy) if (_map_->f_val->cpy)
value = _map_->f_val->cpy(value); value = _map_->f_val->cpy(root->value);
else
value = root->value;
tm_insert(result, key, value); tm_insert(result, key, value);
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;
} }
(result)->callbacks = _map_->callbacks;
_map_->flag = CMC_FLAG_OK; _map_->flag = CMC_FLAG_OK;
(result)->callbacks = _map_->callbacks;
return result; return result;
} }
_Bool tm_equals(struct treemap *_map1_, struct treemap *_map2_) _Bool tm_equals(struct treemap *_map1_, struct treemap *_map2_)
@@ -421,14 +447,36 @@ _Bool tm_equals(struct treemap *_map1_, struct treemap *_map2_)
_map2_->flag = CMC_FLAG_OK; _map2_->flag = CMC_FLAG_OK;
if (_map1_->count != _map2_->count) if (_map1_->count != _map2_->count)
return 0; return 0;
struct treemap_iter iter = tm_iter_start(_map1_); struct treemap_node *root = _map1_->root;
for (; !tm_iter_at_end(&iter); tm_iter_next(&iter)) _Bool left_done = 0;
while (root)
{ {
struct treemap_node *node = tm_impl_get_node(_map2_, tm_iter_key(&iter)); if (!left_done)
{
while (root->left)
root = root->left;
}
struct treemap_node *node = tm_impl_get_node(_map2_, root->key);
if (node == ((void *)0)) if (node == ((void *)0))
return 0; return 0;
if (_map1_->f_val->cmp(node->value, tm_iter_value(&iter)) != 0) if (_map1_->f_val->cmp(node->value, root->value) != 0)
return 0; 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; return 1;
} }
+3
View File
@@ -116,6 +116,9 @@ CMC_CREATE_UNIT(CMCTreeMap, true, {
tm_get_ref(map, 1); tm_get_ref(map, 1);
cmc_assert_equals(int32_t, CMC_FLAG_EMPTY, tm_flag(map)); cmc_assert_equals(int32_t, CMC_FLAG_EMPTY, tm_flag(map));
for (size_t i = 0; i < 100; i++)
cmc_assert(tm_insert(map, i, i));
// copy_of // copy_of
map->flag = CMC_FLAG_ERROR; map->flag = CMC_FLAG_ERROR;
struct treemap *map2 = tm_copy_of(map); struct treemap *map2 = tm_copy_of(map);