Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lp_data/HStruct.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ struct HighsNameHash {
std::unordered_map<std::string, int> name2index;
void form(const std::vector<std::string>& name);
bool hasDuplicate(const std::vector<std::string>& name);
void update(int index, const std::string& old_name,
const std::string& new_name);
void clear();
};

Expand Down
6 changes: 4 additions & 2 deletions src/lp_data/Highs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -591,8 +591,9 @@ HighsStatus Highs::passColName(const HighsInt col, const std::string& name) {
return HighsStatus::kError;
}
this->model_.lp_.col_names_.resize(num_col);
this->model_.lp_.col_hash_.update(col, this->model_.lp_.col_names_[col],
name);
this->model_.lp_.col_names_[col] = name;
this->model_.lp_.col_hash_.clear();
return HighsStatus::kOk;
}

Expand All @@ -611,8 +612,9 @@ HighsStatus Highs::passRowName(const HighsInt row, const std::string& name) {
return HighsStatus::kError;
}
this->model_.lp_.row_names_.resize(num_row);
this->model_.lp_.row_hash_.update(row, this->model_.lp_.row_names_[row],
name);
this->model_.lp_.row_names_[row] = name;
this->model_.lp_.row_hash_.clear();
return HighsStatus::kOk;
}

Expand Down
22 changes: 17 additions & 5 deletions src/lp_data/HighsLp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

@jajhall jajhall May 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By deleting

this->name2index.erase(search);
this->name2index.insert({name[index], kHashIsDuplicate});

and only setting

search->second = kHashIsDuplicate;

(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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

emplace will only insert when there is no duplication. When there is duplication, search points to the duplicated key and value, and setting the value will influence the original map.

This is an example: https://godbolt.org/z/YsYYrKrPb

Copy link
Member

@jajhall jajhall May 30, 2024

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

search->second = kHashIsDuplicate;

than I gave you credit for, sorry.

As I said, I'm not fluent in the use of std::unordered_map :-)

Just spotted the & in

auto& search = emplace_result.first;

Now I see why setting

search->second = kHashIsDuplicate;

achieves the desired result

Copy link
Contributor Author

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 emplace today. It is a clever choice to use emplace to detect duplication.

}
}
}
Expand All @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

}
}

void HighsNameHash::clear() { this->name2index.clear(); }