Skip to content
Snippets Groups Projects
Unverified Commit 03ee379e authored by Jonathan Kunstwald's avatar Jonathan Kunstwald
Browse files

Fix modify-during-iteration bug

parent 452416cf
Branches
No related tags found
No related merge requests found
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
#define ASR_COUNTOF(_arr_) (sizeof(_arr_) / sizeof(_arr_[0])) #define ASR_COUNTOF(_arr_) (sizeof(_arr_) / sizeof(_arr_[0]))
#define ASR_USE_SMALL_FILE 1 #define ASR_USE_SMALL_FILE 0
#define ASR_SKIP_SLOW_NAIVE_TASK3 1 #define ASR_SKIP_SLOW_NAIVE_TASK3 1
...@@ -247,14 +247,26 @@ struct ngram_statistics ...@@ -247,14 +247,26 @@ struct ngram_statistics
// zero occurence prefixed n-grams are a little more involved // zero occurence prefixed n-grams are a little more involved
// amount of v-prefixed n-grams theoretically existing (for a given v) // amount of v-prefixed n-grams theoretically existing (for a given v)
this->max_num_prefixed_ngrams = std::pow(vocab_size, n - 1); this->max_num_prefixed_ngrams = std::pow(vocab_size, n - 1);
// two loops- just to not modify the map while iterating over it
// this could be theoretically avoided by over-reserving the underlying vector but this is a little cleaner
for (auto const& cc_prefix_node : prefixed_count_counts._nodes)
{
prefixed_count_key key;
key.prefix_v_hash = cc_prefix_node.key.prefix_v_hash;
key.occurence = 0;
// just hit the value once, default to max_num_prefixed_ngrams
(void)prefixed_count_counts.get_value(key, max_num_prefixed_ngrams);
}
for (auto const& cc_prefix_node : prefixed_count_counts._nodes) for (auto const& cc_prefix_node : prefixed_count_counts._nodes)
{ {
prefixed_count_key key; prefixed_count_key key;
key.prefix_v_hash = cc_prefix_node.key.prefix_v_hash; key.prefix_v_hash = cc_prefix_node.key.prefix_v_hash;
key.occurence = 0; key.occurence = 0;
// the trick here is to provide the theoretical maximum as a default, instead of 0
// subtract the amount of v-prefixed n-grams that have some (nonzero) occurence from the theoretical max // subtract the amount of v-prefixed n-grams that have some (nonzero) occurence from the theoretical max
prefixed_count_counts.get_value(key, max_num_prefixed_ngrams) -= cc_prefix_node.val; prefixed_count_counts.get_value_no_miss(key) -= cc_prefix_node.val;
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment