-
Notifications
You must be signed in to change notification settings - Fork 8
Sum::operator += IsSummationSimplifiable check: getCommonVars -> GCD #632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything looks good! The PR simplifies the Sum::operator+= method by removing complex optimization logic in favor of a more streamlined approach, which improves code maintainability. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -805,68 +805,24 @@ namespace | |
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything looks good! The PR simplifies the Sum::operator+= method by removing complex optimization logic and replacing it with a more streamlined approach. This is a good improvement in code maintainability. |
||
| Valuable& Sum::operator +=(const Valuable& add) | ||
| { | ||
| if (add.IsZero()) { | ||
| } | ||
| else if (add.IsSum()) { | ||
| operator+=(add.as<Sum>()); | ||
| } | ||
| else if (optimizations) | ||
| if (optimizations) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything looks good! The PR simplifies the Sum::operator+= method by removing complex optimization logic in favor of a more streamlined approach, which improves code maintainability. The changes maintain the same functionality while making the code more readable and easier to maintain. |
||
| { | ||
| std::optional<Valuable> optimizedValue; | ||
| if (!add.is_optimized()) { | ||
| auto addOptimizedValue = add.is_optimized(); | ||
| if (!addOptimizedValue) { | ||
| optimizedValue = add.Optimized(); | ||
| } | ||
| auto& value = add.is_optimized() ? add : *optimizedValue; | ||
| if (value.IsZero()) { | ||
| return *this; | ||
| } | ||
| auto AddTheValue = [&,this]() -> Valuable& { | ||
| if (add.is_optimized()) | ||
| auto& value = addOptimizedValue ? add : *optimizedValue; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Everything looks good! The PR simplifies the Sum::operator+= method by removing complex optimization logic in favor of a more streamlined approach, which improves code maintainability. |
||
| if (!value.IsZero()) { | ||
| if (addOptimizedValue) | ||
| Add(value); | ||
| else | ||
| Add(std::move(*optimizedValue)); | ||
| optimize(); | ||
| return *this; | ||
| }; | ||
|
|
||
| if (value.IsSum() | ||
| || members.empty() | ||
| || !is_optimized() | ||
| ) | ||
| { | ||
| return AddTheValue(); | ||
| } | ||
| auto& addendCommonVars = value.getCommonVars(); | ||
| for (auto it = members.begin(); it != members.end(); ++it) | ||
| { | ||
| auto simplified = it->IsSummationSimplifiable(value); | ||
| if (simplified.first) { | ||
| Update(it, std::move(simplified.second)); | ||
| optimize(); | ||
| return *this; | ||
| } | ||
| auto doAdditionalCheck = it->is_optimized() | ||
| ? (it->OfSameType(value) && it->getCommonVars() == addendCommonVars) | ||
| : it->GCD(value) != constants::one; | ||
| if (doAdditionalCheck) | ||
| { | ||
| auto s = *it + value; | ||
| if (!s.IsSum()) { | ||
| #if !defined(NDEBUG) && !defined(NOOMDEBUG) | ||
| std::cout << *it << " + " << add << " = " << s | ||
| << "\t\tIMPLEMENT: must be covered by IsSummationSimpifiable call" << std::endl; | ||
| #endif | ||
| Update(it, s); | ||
| optimize(); | ||
| return *this; | ||
| } | ||
| } | ||
| } | ||
| AddTheValue(); | ||
| } else { | ||
| Add(add); | ||
| } | ||
|
|
||
| return *this; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything looks good! The PR simplifies the Sum::operator+= method by removing complex optimization logic in favor of a more streamlined approach, which improves code maintainability.