-
Notifications
You must be signed in to change notification settings - Fork 294
Fix the O(N^2) bug of passColName and passRowName #1782
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -594,14 +594,13 @@ void HighsNameHash::form(const std::vector<std::string>& name) { | |
| size_t num_name = name.size(); | ||
| this->clear(); | ||
| for (size_t index = 0; index < num_name; index++) { | ||
| const bool duplicate = !this->name2index.emplace(name[index], index).second; | ||
| auto emplace_result = this->name2index.emplace(name[index], index); | ||
| const bool duplicate = !emplace_result.second; | ||
| if (duplicate) { | ||
| // Find the original and mark it as duplicate | ||
| auto search = this->name2index.find(name[index]); | ||
| assert(search != this->name2index.end()); | ||
| auto& search = emplace_result.first; | ||
| assert(int(search->second) < int(this->name2index.size())); | ||
| this->name2index.erase(search); | ||
| this->name2index.insert({name[index], kHashIsDuplicate}); | ||
| search->second = kHashIsDuplicate; | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -618,4 +617,17 @@ bool HighsNameHash::hasDuplicate(const std::vector<std::string>& name) { | |
| return has_duplicate; | ||
| } | ||
|
|
||
| void HighsNameHash::update(int index, const std::string& old_name, | ||
| const std::string& new_name) { | ||
| this->name2index.erase(old_name); | ||
| auto emplace_result = this->name2index.emplace(new_name, index); | ||
| const bool duplicate = !emplace_result.second; | ||
| if (duplicate) { | ||
| // Find the original and mark it as duplicate | ||
| auto& search = emplace_result.first; | ||
| assert(int(search->second) < int(this->name2index.size())); | ||
| search->second = kHashIsDuplicate; | ||
|
Member
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. Ditto |
||
| } | ||
| } | ||
|
|
||
| void HighsNameHash::clear() { this->name2index.clear(); } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
By deleting
and only setting
(which has no effect since search is immediately destroyed) the property of marking duplicate names is lost. hence errors due to the same name being given to different columns are not trapped.
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.
emplacewill only insert when there is no duplication. When there is duplication,searchpoints to the duplicated key and value, and setting the value will influence the original map.This is an example: https://godbolt.org/z/YsYYrKrPb
Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks, it did cross my mind that there was more to setting
than I gave you credit for, sorry.
As I said, I'm not fluent in the use of
std::unordered_map:-)Just spotted the
&inNow I see why setting
achieves the desired result
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.
Never mind. I also learned about the meaning of return values of
emplacetoday. It is a clever choice to useemplaceto detect duplication.