From 8112ea6f42ef215ae390a9f2e8ffc268c6aa7c80 Mon Sep 17 00:00:00 2001 From: Leonardo Vencovsky Date: Sat, 23 May 2020 07:46:23 -0300 Subject: [PATCH] Bug fixes to hashmultiset --- src/cmc/hashmultiset.h | 11 ++++++++++- tests/main/src/hashmultiset.c | 8 +++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/cmc/hashmultiset.h b/src/cmc/hashmultiset.h index af34c17..9a92106 100644 --- a/src/cmc/hashmultiset.h +++ b/src/cmc/hashmultiset.h @@ -1431,6 +1431,7 @@ static const char *cmc_string_fmt_hashmultiset = "struct %s<%s> " size_t curr_mul = 1; \ \ struct SNAME##_entry *target = &(_set_->buffer[pos]); \ + struct SNAME##_entry *to_return = NULL; \ \ if (target->state == CMC_ES_EMPTY || target->state == CMC_ES_DELETED) \ { \ @@ -1438,6 +1439,8 @@ static const char *cmc_string_fmt_hashmultiset = "struct %s<%s> " target->multiplicity = curr_mul; \ target->dist = pos - original_pos; \ target->state = CMC_ES_FILLED; \ + \ + to_return = target; \ } \ else \ { \ @@ -1454,6 +1457,9 @@ static const char *cmc_string_fmt_hashmultiset = "struct %s<%s> " target->dist = pos - original_pos; \ target->state = CMC_ES_FILLED; \ \ + if (!to_return) \ + to_return = target; \ + \ break; \ } \ else if (target->dist < pos - original_pos) \ @@ -1470,13 +1476,16 @@ static const char *cmc_string_fmt_hashmultiset = "struct %s<%s> " value = tmp; \ original_pos = pos - tmp_dist; \ curr_mul = tmp_mul; \ + \ + if (!to_return) \ + to_return = target; \ } \ } \ } \ \ _set_->count++; \ \ - return target; \ + return to_return; \ } \ \ static struct SNAME##_entry *PFX##_impl_get_entry(struct SNAME *_set_, \ diff --git a/tests/main/src/hashmultiset.c b/tests/main/src/hashmultiset.c index 7533b8c..f51a4d0 100644 --- a/tests/main/src/hashmultiset.c +++ b/tests/main/src/hashmultiset.c @@ -994,12 +994,14 @@ hms_impl_insert_and_return(struct hashmultiset *_set_, size_t value, size_t pos = original_pos; size_t curr_mul = 1; struct hashmultiset_entry *target = &(_set_->buffer[pos]); + struct hashmultiset_entry *to_return = ((void *)0); if (target->state == CMC_ES_EMPTY || target->state == CMC_ES_DELETED) { target->value = value; target->multiplicity = curr_mul; target->dist = pos - original_pos; target->state = CMC_ES_FILLED; + to_return = target; } else { @@ -1014,6 +1016,8 @@ hms_impl_insert_and_return(struct hashmultiset *_set_, size_t value, target->multiplicity = curr_mul; target->dist = pos - original_pos; target->state = CMC_ES_FILLED; + if (!to_return) + to_return = target; break; } else if (target->dist < pos - original_pos) @@ -1027,11 +1031,13 @@ hms_impl_insert_and_return(struct hashmultiset *_set_, size_t value, value = tmp; original_pos = pos - tmp_dist; curr_mul = tmp_mul; + if (!to_return) + to_return = target; } } } _set_->count++; - return target; + return to_return; } static struct hashmultiset_entry *hms_impl_get_entry(struct hashmultiset *_set_, size_t value)