Replace remove-erase idiom by C++20 erase()/erase_if()

- makes the code less verbose while being as efficient
This commit is contained in:
Robert Schulze 2022-04-16 21:43:14 +02:00
parent e04f80d4a7
commit 1e1df8e101
No known key found for this signature in database
GPG Key ID: 15C08E29800B665D
11 changed files with 33 additions and 53 deletions

View File

@ -296,11 +296,7 @@ PoolWithFailoverBase<TNestedPool>::getMany(
"All connection tries failed. Log: \n\n" + fail_messages + "\n",
DB::ErrorCodes::ALL_CONNECTION_TRIES_FAILED);
try_results.erase(
std::remove_if(
try_results.begin(), try_results.end(),
[](const TryResult & r) { return r.entry.isNull() || !r.is_usable; }),
try_results.end());
std::erase_if(try_results, [](const TryResult & r) { return r.entry.isNull() || !r.is_usable; });
/// Sort so that preferred items are near the beginning.
std::stable_sort(

View File

@ -59,10 +59,8 @@ namespace Format
{
size_t i = 0;
bool should_delete = true;
str.erase(
std::remove_if(
str.begin(),
str.end(),
std::erase_if(
str,
[&i, &should_delete, &str](char)
{
bool is_double_brace = (str[i] == '{' && str[i + 1] == '{') || (str[i] == '}' && str[i + 1] == '}');
@ -74,8 +72,7 @@ namespace Format
}
should_delete = true;
return false;
}),
str.end());
});
};
index_positions.emplace_back();

View File

@ -152,10 +152,7 @@ ASTPtr DatabaseMySQL::getCreateTableQueryImpl(const String & table_name, Context
storage_engine_arguments->children.insert(storage_engine_arguments->children.begin() + 2, mysql_table_name);
/// Unset settings
storage_children.erase(
std::remove_if(storage_children.begin(), storage_children.end(),
[&](const ASTPtr & element) { return element.get() == ast_storage->settings; }),
storage_children.end());
std::erase_if(storage_children, [&](const ASTPtr & element) { return element.get() == ast_storage->settings; });
ast_storage->settings = nullptr;
}
auto create_table_query = DB::getCreateQueryFromStorage(storage, table_storage_define, true,

View File

@ -219,10 +219,10 @@ private:
auto current_box = Box(Point(current_min_x, current_min_y), Point(current_max_x, current_max_y));
Polygon tmp_poly;
bg::convert(current_box, tmp_poly);
possible_ids.erase(std::remove_if(possible_ids.begin(), possible_ids.end(), [&](const auto id)
std::erase_if(possible_ids, [&](const auto id)
{
return !bg::intersects(current_box, polygons[id]);
}), possible_ids.end());
});
int covered = 0;
#ifndef __clang_analyzer__ /// Triggers a warning in boost geometry.
auto it = std::find_if(possible_ids.begin(), possible_ids.end(), [&](const auto id)

View File

@ -435,8 +435,7 @@ void ActionsDAG::removeUnusedActions(bool allow_remove_inputs, bool allow_consta
}
nodes.remove_if([&](const Node & node) { return visited_nodes.count(&node) == 0; });
auto it = std::remove_if(inputs.begin(), inputs.end(), [&](const Node * node) { return visited_nodes.count(node) == 0; });
inputs.erase(it, inputs.end());
std::erase_if(inputs, [&](const Node * node) { return visited_nodes.count(node) == 0; });
}
static ColumnWithTypeAndName executeActionForHeader(const ActionsDAG::Node * node, ColumnsWithTypeAndName arguments)

View File

@ -221,7 +221,7 @@ DDLTaskPtr DDLWorker::initAndCheckTask(const String & entry_name, String & out_r
static void filterAndSortQueueNodes(Strings & all_nodes)
{
all_nodes.erase(std::remove_if(all_nodes.begin(), all_nodes.end(), [] (const String & s) { return !startsWith(s, "query-"); }), all_nodes.end());
std::erase_if(all_nodes, [] (const String & s) { return !startsWith(s, "query-"); });
::sort(all_nodes.begin(), all_nodes.end());
}

View File

@ -274,9 +274,9 @@ std::string buildTaggedRegex(std::string regexp_str)
std::vector<std::string> tags;
splitInto<';'>(tags, regexp_str);
/* remove empthy elements */
/* remove empty elements */
using namespace std::string_literals;
tags.erase(std::remove(tags.begin(), tags.end(), ""s), tags.end());
std::erase(tags, ""s);
if (tags[0].find('=') == tags[0].npos)
{
if (tags.size() == 1) /* only name */

View File

@ -3640,10 +3640,10 @@ void MergeTreeData::movePartitionToDisk(const ASTPtr & partition, const String &
parts = getVisibleDataPartsVectorInPartition(local_context, partition_id);
auto disk = getStoragePolicy()->getDiskByName(name);
parts.erase(std::remove_if(parts.begin(), parts.end(), [&](auto part_ptr)
std::erase_if(parts, [&](auto part_ptr)
{
return part_ptr->volume->getDisk()->getName() == disk->getName();
}), parts.end());
});
if (parts.empty())
{
@ -3688,7 +3688,7 @@ void MergeTreeData::movePartitionToVolume(const ASTPtr & partition, const String
if (parts.empty())
throw Exception("Nothing to move (сheck that the partition exists).", ErrorCodes::NO_SUCH_DATA_PART);
parts.erase(std::remove_if(parts.begin(), parts.end(), [&](auto part_ptr)
std::erase_if(parts, [&](auto part_ptr)
{
for (const auto & disk : volume->getDisks())
{
@ -3698,7 +3698,7 @@ void MergeTreeData::movePartitionToVolume(const ASTPtr & partition, const String
}
}
return false;
}), parts.end());
});
if (parts.empty())
{
@ -4184,8 +4184,7 @@ void MergeTreeData::filterVisibleDataParts(DataPartsVector & maybe_visible_parts
return !part->version.isVisible(snapshot_version, current_tid);
};
auto new_end_it = std::remove_if(maybe_visible_parts.begin(), maybe_visible_parts.end(), need_remove_pred);
maybe_visible_parts.erase(new_end_it, maybe_visible_parts.end());
std::erase_if(maybe_visible_parts, need_remove_pred);
[[maybe_unused]] size_t visible_size = maybe_visible_parts.size();
@ -6454,15 +6453,11 @@ ReservationPtr MergeTreeData::balancedReservation(
}
// Remove irrelevant parts.
covered_parts.erase(
std::remove_if(
covered_parts.begin(),
covered_parts.end(),
std::erase_if(covered_parts,
[min_bytes_to_rebalance_partition_over_jbod](const auto & part)
{
return !(part->isStoredOnDisk() && part->getBytesOnDisk() >= min_bytes_to_rebalance_partition_over_jbod);
}),
covered_parts.end());
});
// Include current submerging big parts which are not yet in `currently_submerging_big_parts`
for (const auto & part : covered_parts)

View File

@ -176,11 +176,9 @@ SelectPartsDecision MergeTreeDataMergerMutator::selectPartsToMerge(
return active_parts_set.getContainingPart(part->info) != part->name;
};
auto new_end_it = std::remove_if(active_parts.begin(), active_parts.end(), remove_pred);
active_parts.erase(new_end_it, active_parts.end());
std::erase_if(active_parts, remove_pred);
new_end_it = std::remove_if(outdated_parts.begin(), outdated_parts.end(), remove_pred);
outdated_parts.erase(new_end_it, outdated_parts.end());
std::erase_if(outdated_parts, remove_pred);
std::merge(active_parts.begin(), active_parts.end(),
outdated_parts.begin(), outdated_parts.end(),

View File

@ -611,9 +611,7 @@ int32_t ReplicatedMergeTreeQueue::pullLogsToQueue(zkutil::ZooKeeperPtr zookeeper
/// Multiple log entries that must be copied to the queue.
log_entries.erase(
std::remove_if(log_entries.begin(), log_entries.end(), [&min_log_entry](const String & entry) { return entry < min_log_entry; }),
log_entries.end());
std::erase_if(log_entries, [&min_log_entry](const String & entry) { return entry < min_log_entry; });
if (!log_entries.empty())
{

View File

@ -279,10 +279,10 @@ void StorageSystemZooKeeper::fillData(MutableColumns & res_columns, ContextPtr c
if (!prefix.empty())
{
// Remove nodes that do not match specified prefix
nodes.erase(std::remove_if(nodes.begin(), nodes.end(), [&prefix, &path_part] (const String & node)
std::erase_if(nodes, [&prefix, &path_part] (const String & node)
{
return (path_part + '/' + node).substr(0, prefix.size()) != prefix;
}), nodes.end());
});
}
std::vector<std::future<Coordination::GetResponse>> futures;