Skip to content

Commit

Permalink
refactor: replace usages of std::find, std::find_if and std::reverse …
Browse files Browse the repository at this point in the history
…with the respective functions using C++20 ranges
  • Loading branch information
Taepper committed Jan 8, 2025
1 parent 1522497 commit d963308
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 38 deletions.
3 changes: 2 additions & 1 deletion include/silo/common/cons_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//! (no reference counting). This is meant to be used with recursive
//! algorithms to maintain a path back up.

#include <algorithm>
#include <functional>
#include <memory>
#include <optional>
Expand Down Expand Up @@ -62,7 +63,7 @@ class ConsList {
// then set Vec slots via index, but that needs Default and
// writes to memory twice, too), right?
std::vector<T> values = toVec();
std::reverse(values.begin(), values.end());
std::ranges::reverse(values);
return values;
}
};
20 changes: 7 additions & 13 deletions src/config/config_specification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,10 @@ namespace silo::config {

std::optional<ConfigAttributeSpecification> ConfigSpecification::
getAttributeSpecificationFromAmbiguousKey(const AmbiguousConfigKeyPath& key) const {
// TODO(#663)
auto maybe_result = std::find_if(
attribute_specifications.begin(),
attribute_specifications.end(),
[&](const auto& attribute_spec) {
auto maybe_result =
std::ranges::find_if(attribute_specifications, [&](const auto& attribute_spec) {
return AmbiguousConfigKeyPath::from(attribute_spec.key) == key;
}
);
});
if (maybe_result == attribute_specifications.end()) {
return std::nullopt;
}
Expand All @@ -51,12 +47,10 @@ std::optional<ConfigAttributeSpecification> ConfigSpecification::
std::optional<ConfigAttributeSpecification> ConfigSpecification::getAttributeSpecification(
const ConfigKeyPath& key
) const {
// TODO(#663)
auto maybe_result = std::find_if(
attribute_specifications.begin(),
attribute_specifications.end(),
[&](const auto& attribute_spec) { return attribute_spec.key == key; }
);
auto maybe_result =
std::ranges::find_if(attribute_specifications, [&](const auto& attribute_spec) {
return attribute_spec.key == key;
});
if (maybe_result == attribute_specifications.end()) {
return std::nullopt;
}
Expand Down
2 changes: 1 addition & 1 deletion src/config/source/environment_variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ AmbiguousConfigKeyPath EnvironmentVariables::stringToConfigKeyPath(
const ConfigValue value = attribute_spec.parseValueFromString(value_string);
config_values.emplace(attribute_spec.key, value);
} else {
if (std::find(allow_list.begin(), allow_list.end(), key_string) != allow_list.end()) {
if (std::ranges::find(allow_list, key_string) != allow_list.end()) {
SPDLOG_INFO(
"Given env variable '{}' is not a valid key for '{}' but in allow_list.",
key_string,
Expand Down
3 changes: 1 addition & 2 deletions src/silo/common/lineage_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ std::optional<std::vector<Idx>> Graph::getCycle() const {
// We need to remove leading vertices up until the cycle
SILO_ASSERT_GE(witness_lasso.value().size(), 2UL);
const Idx cycle_node = witness_lasso.value().back();
auto cycle_node_first_occurrence =
std::find(witness_lasso.value().begin(), witness_lasso.value().end(), cycle_node);
auto cycle_node_first_occurrence = std::ranges::find(witness_lasso.value(), cycle_node);
SILO_ASSERT(cycle_node_first_occurrence < witness_lasso.value().end());
witness_lasso.value().erase(witness_lasso.value().begin(), cycle_node_first_occurrence);
return witness_lasso;
Expand Down
10 changes: 4 additions & 6 deletions src/silo/config/config_repository.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ void validatePartitionBy(const DatabaseConfig& config) {

const std::string partition_by = config.schema.partition_by.value();

// TODO(#663)
const auto& partition_by_metadata = std::find_if(
config.schema.metadata.begin(),
config.schema.metadata.end(),
[&](const DatabaseMetadata& metadata) { return metadata.name == partition_by; }
);
const auto& partition_by_metadata =
std::ranges::find_if(config.schema.metadata, [&](const DatabaseMetadata& metadata) {
return metadata.name == partition_by;
});
if (partition_by_metadata == config.schema.metadata.end()) {
throw ConfigException("partitionBy '" + partition_by + "' is not in metadata");
}
Expand Down
8 changes: 3 additions & 5 deletions src/silo/config/database_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,9 @@ ColumnType DatabaseMetadata::getColumnType() const {
}

std::optional<DatabaseMetadata> DatabaseConfig::getMetadata(const std::string& name) const {
// TODO(#663)
auto element =
std::find_if(schema.metadata.begin(), schema.metadata.end(), [&name](const auto& metadata) {
return metadata.name == name;
});
auto element = std::ranges::find_if(schema.metadata, [&name](const auto& metadata) {
return metadata.name == name;
});
if (element == std::end(schema.metadata)) {
return std::nullopt;
}
Expand Down
6 changes: 2 additions & 4 deletions src/silo/preprocessing/metadata_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ void MetadataInfo::validateMetadataFile(
std::set<std::string> actual_fields;
for (size_t idx = 0; idx < result->ColumnCount(); idx++) {
actual_fields.emplace(result->ColumnName(idx));
// TODO(#663)
if (std::find_if(database_config.schema.metadata.begin(), database_config.schema.metadata.end(), [&](const auto& metadata) {
if (std::ranges::find_if(database_config.schema.metadata, [&](const auto& metadata) {
return metadata.name == result->ColumnName(idx);
}) == database_config.schema.metadata.end()) {
SPDLOG_WARN(
Expand Down Expand Up @@ -131,8 +130,7 @@ void MetadataInfo::validateNdjsonFile(
std::set<std::string> actual_fields;
for (size_t idx = 0; idx < result->ColumnCount(); idx++) {
actual_fields.emplace(result->ColumnName(idx));
// TODO(#663)
if (std::find_if(database_config.schema.metadata.begin(), database_config.schema.metadata.end(), [&](const auto& metadata) {
if (std::ranges::find_if(database_config.schema.metadata, [&](const auto& metadata) {
return metadata.name == result->ColumnName(idx);
}) == database_config.schema.metadata.end()) {
SPDLOG_WARN(
Expand Down
9 changes: 3 additions & 6 deletions src/silo/query_engine/actions/tuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,9 @@ std::vector<Tuple::ComparatorField> Tuple::getCompareFields(
tuple_field_comparators.resize(order_by_fields.size());
size_t offset = 0;
for (const auto& metadata : columns_metadata) {
// TODO(#663)
auto element = std::find_if(
order_by_fields.begin(),
order_by_fields.end(),
[&](const auto& order_by_field) { return metadata.name == order_by_field.name; }
);
auto element = std::ranges::find_if(order_by_fields, [&](const auto& order_by_field) {
return metadata.name == order_by_field.name;
});
if (element != order_by_fields.end()) {
const size_t index = std::distance(order_by_fields.begin(), element);
tuple_field_comparators[index] =
Expand Down

0 comments on commit d963308

Please sign in to comment.