Skip to content

Commit

Permalink
feat(core): optimize add
Browse files Browse the repository at this point in the history
Fixes #671
  • Loading branch information
gjasny committed Dec 19, 2023
1 parent 3eca7c6 commit 540a5a5
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions core/src/registry.cc
Expand Up @@ -118,27 +118,21 @@ Family<T>& Registry::Add(const std::string& name, const std::string& help,

auto& families = GetFamilies<T>();

if (insert_behavior_ == InsertBehavior::Merge) {
auto same_name_and_labels =
[&name, &labels](const std::unique_ptr<Family<T>>& family) {
return std::tie(name, labels) ==
std::tie(family->GetName(), family->GetConstantLabels());
};

auto it =
std::find_if(families.begin(), families.end(), same_name_and_labels);
if (it != families.end()) {
return **it;
}
}

auto same_name = [&name](const std::unique_ptr<Family<T>>& family) {
return name == family->GetName();
};

auto it = std::find_if(families.begin(), families.end(), same_name);
if (it != families.end()) {
throw std::invalid_argument("Family name already exists");
if (insert_behavior_ == InsertBehavior::Merge) {
if ((*it)->GetConstantLabels() == labels) {
return **it;
}
throw std::invalid_argument(
"Family name already exists with different constant labels");
} else {
throw std::invalid_argument("Family name already exists");
}
}

auto family = detail::make_unique<Family<T>>(name, help, labels);
Expand Down

0 comments on commit 540a5a5

Please sign in to comment.