Skip to content

Commit

Permalink
Fix segfault when erasing cohorts in other windows; Inactive windows …
Browse files Browse the repository at this point in the history
…that become empty are now deleted entirely
  • Loading branch information
TinoDidriksen committed Apr 22, 2024
1 parent 9139743 commit 88caa3a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 26 deletions.
70 changes: 61 additions & 9 deletions src/GrammarApplicator_runRules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ bool GrammarApplicator::updateRuleToCohorts(Cohort& c, const uint32_t& rsit) {
if (!doesWordformsMatch(c.wordform, r->wordform)) {
return false;
}
if (current->rule_to_cohorts.size() < rsit+1) {
indexSingleWindow(*current);
}
CohortSet& cohortset = current->rule_to_cohorts[rsit];
cohortset.insert(&c);
return current->valid_rules.insert(rsit);
Expand Down Expand Up @@ -750,7 +753,9 @@ bool GrammarApplicator::runSingleRule(SingleWindow& current, const Rule& rule, R
}

for (auto& ctx : reading_contexts) {
if (!ctx.target.subreading->matched_target) continue;
if (!ctx.target.subreading->matched_target) {
continue;
}
if (!ctx.target.subreading->matched_tests && rule.type != K_IFF) {
continue;
}
Expand All @@ -765,7 +770,9 @@ bool GrammarApplicator::runSingleRule(SingleWindow& current, const Rule& rule, R
reset_cohorts();
break;
}
if (!finish_reading_loop) break;
if (!finish_reading_loop) {
break;
}
}

reset_cohorts_for_loop = false;
Expand Down Expand Up @@ -1031,6 +1038,7 @@ uint32_t GrammarApplicator::runRulesOnSingleWindow(SingleWindow& current, const
};

auto rem_cohort = [&](Cohort* cohort) {
auto& current = *cohort->parent;
for (auto iter : cohort->readings) {
iter->hit_by.push_back(rule->number);
iter->deleted = true;
Expand All @@ -1052,37 +1060,81 @@ uint32_t GrammarApplicator::runRulesOnSingleWindow(SingleWindow& current, const
while (!cohort->dep_children.empty()) {
uint32_t ch = cohort->dep_children.back();
if (cohort->dep_parent == DEP_NO_PARENT) {
attachParentChild(*current.parent->cohort_map[0], *current.parent->cohort_map[ch], true, true);
attachParentChild(*gWindow->cohort_map[0], *gWindow->cohort_map[ch], true, true);
}
else {
attachParentChild(*current.parent->cohort_map[cohort->dep_parent], *current.parent->cohort_map[ch], true, true);
attachParentChild(*gWindow->cohort_map[cohort->dep_parent], *gWindow->cohort_map[ch], true, true);
}
cohort->dep_children.erase(ch);
}
cohort->type |= CT_REMOVED;
if (!cohort->prev->enclosed.empty()) {
cohort->prev->enclosed.back()->removed.push_back(cohort);
cohort->prev->enclosed.back()->ignored_cohorts.insert(cohort->prev->enclosed.back()->ignored_cohorts.end(), cohort->ignored_cohorts.begin(), cohort->ignored_cohorts.end());
cohort->ignored_cohorts.clear();
}
else {
cohort->prev->removed.push_back(cohort);
cohort->prev->ignored_cohorts.insert(cohort->prev->ignored_cohorts.end(), cohort->ignored_cohorts.begin(), cohort->ignored_cohorts.end());
cohort->ignored_cohorts.clear();
}
cohort->ignored_cohorts.clear();
cohort->detach();
for (auto& cm : current.parent->cohort_map) {
for (auto& cm : gWindow->cohort_map) {
cm.second->dep_children.erase(cohort->dep_self);
}
current.parent->cohort_map.erase(cohort->global_number);
gWindow->cohort_map.erase(cohort->global_number);
current.cohorts.erase(current.cohorts.begin() + cohort->local_number);
foreach (iter, current.cohorts) {
(*iter)->local_number = UI32(std::distance(current.cohorts.begin(), iter));
}

if (current.cohorts.size() == 1 && &current != gWindow->current) {
// This window is now empty, so remove it entirely from consideration so rules can look past it
cohort = current.cohorts[0];

if (!cohort->enclosed.empty()) {
cohort->prev->enclosed.insert(cohort->prev->enclosed.end(), cohort->enclosed.begin(), cohort->enclosed.end());
cohort->enclosed.clear();
}
// Remove the cohort from all rules
for (auto& cs : current.rule_to_cohorts) {
cs.erase(cohort);
}
if (!cohort->prev->enclosed.empty()) {
cohort->prev->enclosed.back()->ignored_cohorts.insert(cohort->prev->enclosed.back()->ignored_cohorts.end(), cohort->ignored_cohorts.begin(), cohort->ignored_cohorts.end());
}
else {
cohort->prev->ignored_cohorts.insert(cohort->prev->ignored_cohorts.end(), cohort->ignored_cohorts.begin(), cohort->ignored_cohorts.end());
}
cohort->ignored_cohorts.clear();
cohort->detach();
for (auto& cm : gWindow->cohort_map) {
cm.second->dep_children.erase(cohort->dep_self);
}
gWindow->cohort_map.erase(cohort->global_number);

for (size_t i = 0; i < gWindow->previous.size(); ++i) {
if (gWindow->previous[i] == &current) {
delete gWindow->previous[i];
gWindow->previous.erase(gWindow->previous.begin() + i);
break;
}
}
for (size_t i = 0; i < gWindow->next.size(); ++i) {
if (gWindow->next[i] == &current) {
delete gWindow->next[i];
gWindow->next.erase(gWindow->next.begin() + i);
break;
}
}

gWindow->rebuildSingleWindowLinks();
}

gWindow->rebuildCohortLinks();
};

auto ignore_cohort = [&](Cohort* cohort) {
auto& current = *cohort->parent;
for (auto iter : cohort->readings) {
iter->hit_by.push_back(rule->number);
}
Expand All @@ -1101,7 +1153,7 @@ uint32_t GrammarApplicator::runRulesOnSingleWindow(SingleWindow& current, const
cohort->prev->ignored_cohorts.push_back(cohort);
}
cohort->detach();
current.parent->cohort_map.erase(cohort->global_number);
gWindow->cohort_map.erase(cohort->global_number);
current.cohorts.erase(current.cohorts.begin() + cohort->local_number);
};

Expand Down
26 changes: 17 additions & 9 deletions test/T_MergeCohorts/expected.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
"<ele>"
"ele" PERS M 3S NOM @SUBJ> #1->3
"<her el>"
"el her" merged #2->2 MERGECOHORTS:3
"el her" merged #2->2 MERGECOHORTS:4
"<bate>"
"bater" <fmc> <mv> V PR 3S IND VFIN @FS-STA #3->0
; "<em>"
; "em" <sam-> PRP @<PIV MERGECOHORTS:3
; "em" <sam-> PRP @<PIV MERGECOHORTS:4
"<a>"
"o" <artd> <-sam> DET F S @>N #4->3
; "<mulher>"
; "mulher" <Hattr> N F S @P< MERGECOHORTS:3
"<$.>"
"$." PU @PU #5->0
; "mulher" <Hattr> N F S @P< MERGECOHORTS:4
"<.>"
"." PU @PU #5->0
</s>

"<o>"
Expand All @@ -26,8 +26,8 @@
"um" <arti> <-sam> DET F S @>N #5->6
"<árvore>"
"árvore" <Btree> N F S @P< #6->4
"<$.>"
"$." PU @PU #7->0
"<.>"
"." PU @PU #7->0
</s>

"<em>"
Expand All @@ -42,8 +42,8 @@
"o" <artd> DET M S @>N #5->6
"<recorde>"
"recorde" <ac> N M S @<ACC #6->4
"<$.>"
"$." PU @PU #7->0
"<.>"
"." PU @PU #7->0

"<root>"
"root" #1->0
Expand All @@ -61,3 +61,11 @@
"six" #7->5
"<seven>"
"seven" #8->1
; "<?>"
; "1" CLB MERGECOHORTS:9
; "<?>"
; "12" CLB Heur/Merged MERGECOHORTS:9 MERGECOHORTS:9
; "<?>"
; "123" CLB Heur/Merged MERGECOHORTS:9 MERGECOHORTS:9
"<?>"
"1234" CLB Heur/Merged #9->9 MERGECOHORTS:9
7 changes: 5 additions & 2 deletions test/T_MergeCohorts/grammar.cg3
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
DELIMITERS = "<$.>" ;
Delimiters = "<.>" "<?>" "<!>" ;
CmdArgs += --num-windows 5 ;

MERGECOHORTS ("<$1 $2>"v "$2 $1"v merged) ("em") WITH
MergeCohorts ("<$1 $2>"v "$2 $1"v merged) ("em") With
(1* ("mul(her)"r))
(-1*A ("(el)e"r) LINK 1 (*))
;

MergeCohorts Repeat ("<$1>"v "$2$3"v CLB Heur/Merged) ("<(.*)>"r "([^<].*)"r) + _S_DELIMITERS_ With (1W (>>>) LINK 1 ("([^<].*)"r) + _S_DELIMITERS_) ;
20 changes: 14 additions & 6 deletions test/T_MergeCohorts/input.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"o" <artd> <-sam> DET F S @>N #4→5
"<mulher>"
"mulher" <Hattr> N F S @P< #5→3
"<$.>"
"$." PU @PU #6→0 <<<
"<.>"
"." PU @PU #6→0 <<<
</s>

"<o>"
Expand All @@ -24,8 +24,8 @@
"um" <arti> <-sam> DET F S @>N #75→76
"<árvore>"
"árvore" <Btree> N F S @P< #76→74
"<$.>"
"$." PU @PU #77→0 <<<
"<.>"
"." PU @PU #77→0 <<<
</s>

"<em>"
Expand All @@ -40,8 +40,8 @@
"o" <artd> DET M S @>N #45→46
"<recorde>"
"recorde" <ac> N M S @<ACC #46→44
"<$.>"
"$." PU @PU #47→0
"<.>"
"." PU @PU #47→0

"<root>"
"root" #1->0
Expand All @@ -59,3 +59,11 @@
"six" #7->5
"<seven>"
"seven" #8->1
"<?>"
"1" CLB
"<!>"
"2" CLB
"<!>"
"3" CLB
"<?>"
"4" CLB

0 comments on commit 88caa3a

Please sign in to comment.