From 762b95db263b51d7f5e4d2238ceeb228f9cd8fad Mon Sep 17 00:00:00 2001 From: Leonardo Date: Tue, 3 Sep 2019 17:07:15 -0300 Subject: [PATCH] Some fixes and improvements to hashmap, hashset and multimap --- src/cmc/hashmap.h | 11 ++++++---- src/cmc/hashset.h | 54 +++++++++++++++++++++++++--------------------- src/ext/multimap.h | 10 ++++++++- 3 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/cmc/hashmap.h b/src/cmc/hashmap.h index 13047a6..a6f7537 100644 --- a/src/cmc/hashmap.h +++ b/src/cmc/hashmap.h @@ -330,7 +330,7 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531, { \ target->key = key; \ target->value = value; \ - target->dist = original_pos - pos; \ + target->dist = 0; \ target->state = CMC_ES_FILLED; \ } \ else \ @@ -775,7 +775,7 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531, \ static bool PFX##_impl_grow(SNAME *_map_) \ { \ - size_t new_size = PFX##_impl_calculate_size(_map_->capacity + _map_->capacity / 2); \ + size_t new_size = _map_->capacity + _map_->capacity / 2; \ \ SNAME *_new_map_ = PFX##_new(new_size, _map_->load, _map_->cmp, _map_->hash); \ \ @@ -795,14 +795,17 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531, if (_map_->count != _new_map_->count) \ { \ PFX##_free(_new_map_, NULL); \ + \ return false; \ } \ \ - SNAME##_entry *tmp = _map_->buffer; \ + SNAME##_entry *tmp_b = _map_->buffer; \ _map_->buffer = _new_map_->buffer; \ - _new_map_->buffer = tmp; \ + _new_map_->buffer = tmp_b; \ \ + size_t tmp_c = _map_->capacity; \ _map_->capacity = _new_map_->capacity; \ + _new_map_->capacity = tmp_c; \ \ PFX##_free(_new_map_, NULL); \ \ diff --git a/src/cmc/hashset.h b/src/cmc/hashset.h index f9151f9..75a990c 100644 --- a/src/cmc/hashset.h +++ b/src/cmc/hashset.h @@ -319,7 +319,7 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531, if (target->state == CMC_ES_EMPTY || target->state == CMC_ES_DELETED) \ { \ target->value = element; \ - target->dist = pos - original_pos; \ + target->dist = 0; \ target->state = CMC_ES_FILLED; \ } \ else \ @@ -806,19 +806,21 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531, SNAME##_entry *scan = &(iter->target->buffer[iter->cursor]); \ \ if (iter->cursor == iter->last) \ - iter->end = true; \ - else \ { \ - iter->index++; \ + iter->end = true; \ \ - while (1) \ - { \ - iter->cursor++; \ - scan = &(iter->target->buffer[iter->cursor]); \ + return false; \ + } \ \ - if (scan->state == CMC_ES_FILLED) \ - break; \ - } \ + iter->index++; \ + \ + while (1) \ + { \ + iter->cursor++; \ + scan = &(iter->target->buffer[iter->cursor]); \ + \ + if (scan->state == CMC_ES_FILLED) \ + break; \ } \ \ iter->start = PFX##_empty(iter->target); \ @@ -834,19 +836,21 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531, SNAME##_entry *scan = &(iter->target->buffer[iter->cursor]); \ \ if (iter->cursor == iter->first) \ - iter->start = true; \ - else \ { \ - iter->index--; \ + iter->start = true; \ \ - while (1) \ - { \ - iter->cursor--; \ - scan = &(iter->target->buffer[iter->cursor]); \ + return false; \ + } \ \ - if (scan->state == CMC_ES_FILLED) \ - break; \ - } \ + iter->index--; \ + \ + while (1) \ + { \ + iter->cursor--; \ + scan = &(iter->target->buffer[iter->cursor]); \ + \ + if (scan->state == CMC_ES_FILLED) \ + break; \ } \ \ iter->end = PFX##_empty(iter->target); \ @@ -869,7 +873,7 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531, \ static bool PFX##_impl_grow(SNAME *_set_) \ { \ - size_t new_size = PFX##_impl_calculate_size(_set_->capacity + _set_->capacity / 2); \ + size_t new_size = _set_->capacity + _set_->capacity / 2; \ \ SNAME *_new_set_ = PFX##_new(new_size, _set_->load, _set_->cmp, _set_->hash); \ \ @@ -890,11 +894,13 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531, return false; \ } \ \ - SNAME##_entry *tmp = _set_->buffer; \ + SNAME##_entry *tmp_b = _set_->buffer; \ _set_->buffer = _new_set_->buffer; \ - _new_set_->buffer = tmp; \ + _new_set_->buffer = tmp_b; \ \ + size_t tmp_c = _set_->capacity; \ _set_->capacity = _new_set_->capacity; \ + _new_set_->capacity = tmp_c; \ \ PFX##_free(_new_set_, NULL); \ \ diff --git a/src/ext/multimap.h b/src/ext/multimap.h index 7555d63..a701834 100644 --- a/src/ext/multimap.h +++ b/src/ext/multimap.h @@ -356,7 +356,8 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531, \ scan = scan->next; \ \ - deallocator(tmp->key, tmp->value); \ + if (deallocator) \ + deallocator(tmp->key, tmp->value); \ \ free(tmp); \ } \ @@ -1020,6 +1021,13 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531, return false; \ } \ \ + if (_map_->count != _new_map_->count) \ + { \ + PFX##_free(_new_map_, NULL); \ + \ + return false; \ + } \ + \ SNAME##_entry *(*tmp_b)[2] = _map_->buffer; \ _map_->buffer = _new_map_->buffer; \ _new_map_->buffer = tmp_b; \