refactor: prefer std::ranges:: algorithm variants

This commit is contained in:
Marcus Holland-Moritz 2025-03-16 17:09:03 +01:00
parent ca530f6cd1
commit ab8f67619f
20 changed files with 52 additions and 57 deletions

View File

@ -223,7 +223,7 @@ std::vector<std::string> checksum::available_algorithms() {
available.insert(available.end(), supported_algorithms.begin(), available.insert(available.end(), supported_algorithms.begin(),
supported_algorithms.end()); supported_algorithms.end());
available.insert(available.end(), available_evp.begin(), available_evp.end()); available.insert(available.end(), available_evp.begin(), available_evp.end());
std::sort(available.begin(), available.end()); std::ranges::sort(available);
return available; return available;
} }

View File

@ -85,7 +85,7 @@ class glob_matcher_ final : public glob_matcher::impl {
} }
bool match(std::string_view sv) const override { bool match(std::string_view sv) const override {
return std::any_of(m_.begin(), m_.end(), [&sv](auto const& re) { return std::ranges::any_of(m_, [&sv](auto const& re) {
return std::regex_match(sv.begin(), sv.end(), re); return std::regex_match(sv.begin(), sv.end(), re);
}); });
} }

View File

@ -54,9 +54,8 @@ std::set<std::string>
feature_set::get_unsupported(std::set<std::string> wanted_features) { feature_set::get_unsupported(std::set<std::string> wanted_features) {
auto const supported_features = get_supported(); auto const supported_features = get_supported();
std::set<std::string> missing; std::set<std::string> missing;
std::set_difference(wanted_features.begin(), wanted_features.end(), std::ranges::set_difference(wanted_features, supported_features,
supported_features.begin(), supported_features.end(), std::inserter(missing, missing.end()));
std::inserter(missing, missing.end()));
return missing; return missing;
} }

View File

@ -272,7 +272,7 @@ string_table::pack_generic(std::span<T const> input,
output.buffer()->swap(buffer); output.buffer()->swap(buffer);
output.symtab() = std::move(symtab); output.symtab() = std::move(symtab);
output.index()->resize(size); output.index()->resize(size);
std::copy(out_len_vec.begin(), out_len_vec.end(), output.index()->begin()); std::ranges::copy(out_len_vec, output.index()->begin());
} else { } else {
// store uncompressed // store uncompressed
output.buffer()->reserve(total_input_size); output.buffer()->reserve(total_input_size);

View File

@ -87,7 +87,7 @@ void library_dependencies::add_library(std::string const& name_version_string) {
if (tmp.starts_with("lib")) { if (tmp.starts_with("lib")) {
tmp.erase(0, 3); tmp.erase(0, 3);
} }
std::replace(tmp.begin(), tmp.end(), ' ', '-'); std::ranges::replace(tmp, ' ', '-');
deps_.insert(tmp); deps_.insert(tmp);
} }

View File

@ -238,8 +238,8 @@ void stream_logger::write(level_type level, std::string_view output,
if (output.find('\r') != std::string::npos) { if (output.find('\r') != std::string::npos) {
tmp.reserve(output.size()); tmp.reserve(output.size());
std::copy_if(output.begin(), output.end(), std::back_inserter(tmp), std::ranges::copy_if(output, std::back_inserter(tmp),
[](char c) { return c != '\r'; }); [](char c) { return c != '\r'; });
split_to(tmp, '\n', lines); split_to(tmp, '\n', lines);
} else { } else {
split_to(output, '\n', lines); split_to(output, '\n', lines);
@ -261,7 +261,7 @@ void stream_logger::write(level_type level, std::string_view output,
<< newline; << newline;
if (clear_ctx) { if (clear_ctx) {
std::fill(t.begin(), t.end(), '.'); std::ranges::fill(t, '.');
context.assign(context_len, ' '); context.assign(context_len, ' ');
clear_ctx = false; clear_ctx = false;
} }

View File

@ -74,9 +74,9 @@ size_t option_map::get_size(const std::string& key, size_t default_value) {
void option_map::report() { void option_map::report() {
if (!opt_.empty()) { if (!opt_.empty()) {
std::vector<std::string> invalid; std::vector<std::string> invalid;
std::transform(opt_.begin(), opt_.end(), std::back_inserter(invalid), std::ranges::transform(opt_, std::back_inserter(invalid),
[](const auto& p) { return p.first; }); [](const auto& p) { return p.first; });
std::sort(invalid.begin(), invalid.end()); std::ranges::sort(invalid);
DWARFS_THROW(runtime_error, DWARFS_THROW(runtime_error,
fmt::format("invalid option(s) for choice {}: {}", choice_, fmt::format("invalid option(s) for choice {}: {}", choice_,
fmt::join(invalid, ", "))); fmt::join(invalid, ", ")));

View File

@ -229,7 +229,7 @@ class performance_monitor_impl final : public performance_monitor {
ts.emplace_back(t.get_namespace(), t.total_latency(), i); ts.emplace_back(t.get_namespace(), t.total_latency(), i);
} }
std::sort(ts.begin(), ts.end(), [](auto const& a, auto const& b) { std::ranges::sort(ts, [](auto const& a, auto const& b) {
return std::get<0>(a) < std::get<0>(b) || return std::get<0>(a) < std::get<0>(b) ||
(std::get<0>(a) == std::get<0>(b) && (std::get<0>(a) == std::get<0>(b) &&
std::get<1>(a) > std::get<1>(b)); std::get<1>(a) > std::get<1>(b));
@ -302,8 +302,8 @@ class performance_monitor_impl final : public performance_monitor {
} }
} }
std::sort(events.begin(), events.end(), std::ranges::sort(events,
[](auto const& a, auto const& b) { return a.ts < b.ts; }); [](auto const& a, auto const& b) { return a.ts < b.ts; });
bool first = true; bool first = true;
auto const pid = ::getpid(); auto const pid = ::getpid();

View File

@ -100,9 +100,8 @@ fsinfo_features fsinfo_features::parse(std::string_view features) {
fsinfo_features result; fsinfo_features result;
for (auto const& f : split_view<std::string_view>(features, ',')) { for (auto const& f : split_view<std::string_view>(features, ',')) {
auto const it = auto const it = std::ranges::find_if(
std::find_if(fsinfo_feature_names.begin(), fsinfo_feature_names.end(), fsinfo_feature_names, [&f](auto const& p) { return f == p.second; });
[&f](auto const& p) { return f == p.second; });
if (it == fsinfo_feature_names.end()) { if (it == fsinfo_feature_names.end()) {
DWARFS_THROW(runtime_error, fmt::format("invalid feature: \"{}\"", f)); DWARFS_THROW(runtime_error, fmt::format("invalid feature: \"{}\"", f));

View File

@ -108,12 +108,11 @@ class lru_sequential_access_detector : public sequential_access_detector {
return std::nullopt; return std::nullopt;
} }
auto minmax = std::minmax_element( auto minmax = std::ranges::minmax_element(
lru_.begin(), lru_.end(), lru_, [](auto const& a, auto const& b) { return a.first < b.first; });
[](auto const& a, auto const& b) { return a.first < b.first; });
auto min = minmax.first->first; auto min = minmax.min->first;
auto max = minmax.second->first; auto max = minmax.max->first;
is_sequential_ = max - min + 1 == seq_blocks_; is_sequential_ = max - min + 1 == seq_blocks_;
@ -185,8 +184,7 @@ class block_request_set {
void merge(block_request_set& other) { void merge(block_request_set& other) {
queue_.reserve(queue_.size() + other.queue_.size()); queue_.reserve(queue_.size() + other.queue_.size());
std::move(other.queue_.begin(), other.queue_.end(), std::ranges::move(other.queue_, std::back_inserter(queue_));
std::back_inserter(queue_));
other.queue_.clear(); other.queue_.clear();
std::make_heap(queue_.begin(), queue_.end()); std::make_heap(queue_.begin(), queue_.end());
range_end_ = std::max(range_end_, other.range_end_); range_end_ = std::max(range_end_, other.range_end_);

View File

@ -508,7 +508,7 @@ void check_compact_strings(
fmt::format("invalid first compact {0} index: {1}", what, fmt::format("invalid first compact {0} index: {1}", what,
idx.front())); idx.front()));
} }
if (!std::is_sorted(idx.begin(), idx.end())) { if (!std::ranges::is_sorted(idx)) {
DWARFS_THROW(runtime_error, DWARFS_THROW(runtime_error,
fmt::format("compact {0} index not sorted", what)); fmt::format("compact {0} index not sorted", what));
} }
@ -663,26 +663,24 @@ std::array<size_t, 6> check_partitioning(global_metadata::Meta const& meta) {
}; };
auto inodes = meta.inodes(); auto inodes = meta.inodes();
if (!std::is_partitioned(inodes.begin(), inodes.end(), pred)) { if (!std::ranges::is_partitioned(inodes, pred)) {
DWARFS_THROW(runtime_error, "inode table is not partitioned"); DWARFS_THROW(runtime_error, "inode table is not partitioned");
} }
offsets[r] = std::distance( offsets[r] = std::distance(inodes.begin(),
inodes.begin(), std::ranges::partition_point(inodes, pred));
std::partition_point(inodes.begin(), inodes.end(), pred));
} else { } else {
auto pred = [r, modes = meta.modes(), inodes = meta.inodes()](auto ent) { auto pred = [r, modes = meta.modes(), inodes = meta.inodes()](auto ent) {
return mode_rank(modes[inodes[ent].mode_index()]) < r; return mode_rank(modes[inodes[ent].mode_index()]) < r;
}; };
auto entries = meta.entry_table_v2_2(); auto entries = meta.entry_table_v2_2();
if (!std::is_partitioned(entries.begin(), entries.end(), pred)) { if (!std::ranges::is_partitioned(entries, pred)) {
DWARFS_THROW(runtime_error, "entry_table_v2_2 is not partitioned"); DWARFS_THROW(runtime_error, "entry_table_v2_2 is not partitioned");
} }
offsets[r] = std::distance( offsets[r] = std::distance(entries.begin(),
entries.begin(), std::ranges::partition_point(entries, pred));
std::partition_point(entries.begin(), entries.end(), pred));
} }
} }
@ -1049,7 +1047,7 @@ std::string dir_entry_view_impl::unix_path() const {
static_cast<char>(std::filesystem::path::preferred_separator); static_cast<char>(std::filesystem::path::preferred_separator);
auto p = path(); auto p = path();
if constexpr (preferred != '/') { if constexpr (preferred != '/') {
std::replace(p.begin(), p.end(), preferred, '/'); std::ranges::replace(p, preferred, '/');
} }
return p; return p;
} }

View File

@ -310,7 +310,7 @@ void analyze_frozen(std::ostream& os,
l->reg_file_size_cacheField.layout.valueField.layout.lookupField); l->reg_file_size_cacheField.layout.valueField.layout.lookupField);
} }
std::sort(usage.begin(), usage.end(), [](auto const& a, auto const& b) { std::ranges::sort(usage, [](auto const& a, auto const& b) {
return a.first > b.first || (a.first == b.first && a.second < b.second); return a.first > b.first || (a.first == b.first && a.second < b.second);
}); });
@ -816,7 +816,7 @@ class metadata_ final : public metadata_v2::impl {
host_preferred = '/'; host_preferred = '/';
} }
if (meta_preferred != host_preferred) { if (meta_preferred != host_preferred) {
std::replace(rv.begin(), rv.end(), meta_preferred, host_preferred); std::ranges::replace(rv, meta_preferred, host_preferred);
} }
} }
@ -915,7 +915,7 @@ class metadata_ final : public metadata_v2::impl {
{ {
auto tt = LOG_TIMED_TRACE; auto tt = LOG_TIMED_TRACE;
uint32_t max = *std::max_element(nlinks.begin(), nlinks.end()); uint32_t max = *std::ranges::max_element(nlinks);
packed_nlinks.reset(std::bit_width(max), nlinks.size()); packed_nlinks.reset(std::bit_width(max), nlinks.size());
for (size_t i = 0; i < nlinks.size(); ++i) { for (size_t i = 0; i < nlinks.size(); ++i) {
@ -979,7 +979,7 @@ class metadata_ final : public metadata_v2::impl {
// It's faster to check here if the folded names are sorted than to // It's faster to check here if the folded names are sorted than to
// check later if the indices in `entries` are sorted. // check later if the indices in `entries` are sorted.
if (!std::is_sorted(names.begin(), names.end())) { if (!std::ranges::is_sorted(names)) {
std::vector<uint32_t> entries(range.size()); std::vector<uint32_t> entries(range.size());
std::iota(entries.begin(), entries.end(), 0); std::iota(entries.begin(), entries.end(), 0);
boost::sort::flat_stable_sort( boost::sort::flat_stable_sort(

View File

@ -145,7 +145,7 @@ unsigned get_unused_lsb_count<uint16_t>(std::span<uint8_t const> imagedata) {
size_t size = imagedata.size_bytes() / kAlignment; size_t size = imagedata.size_bytes() / kAlignment;
alignas(kAlignment) std::array<uint64_t, kAlignment / sizeof(uint64_t)> b512; alignas(kAlignment) std::array<uint64_t, kAlignment / sizeof(uint64_t)> b512;
std::fill(b512.begin(), b512.end(), 0); std::ranges::fill(b512, 0);
for (size_t i = 0; i < size; ++i) { for (size_t i = 0; i < size; ++i) {
for (size_t k = 0; k < b512.size(); ++k) { for (size_t k = 0; k < b512.size(); ++k) {

View File

@ -60,7 +60,7 @@ void check_unsupported_metadata_requirements(nlohmann::json& req) {
for (auto const& [k, v] : req.items()) { for (auto const& [k, v] : req.items()) {
keys.emplace_back(k); keys.emplace_back(k);
} }
std::sort(keys.begin(), keys.end()); std::ranges::sort(keys);
throw std::runtime_error(fmt::format( throw std::runtime_error(fmt::format(
"unsupported metadata requirements: {}", fmt::join(keys, ", "))); "unsupported metadata requirements: {}", fmt::join(keys, ", ")));
} }

View File

@ -129,7 +129,7 @@ std::string entry::unix_dpath() const {
if (auto parent = parent_.lock()) { if (auto parent = parent_.lock()) {
p = parent->unix_dpath() + p; p = parent->unix_dpath() + p;
} else if constexpr (kLocalPathSeparator != '/') { } else if constexpr (kLocalPathSeparator != '/') {
std::replace(p.begin(), p.end(), kLocalPathSeparator, '/'); std::ranges::replace(p, kLocalPathSeparator, '/');
} }
} }
@ -352,10 +352,9 @@ void dir::accept(entry_visitor& v, bool preorder) {
} }
void dir::sort() { void dir::sort() {
std::sort(entries_.begin(), entries_.end(), std::ranges::sort(entries_, [](entry_ptr const& a, entry_ptr const& b) {
[](entry_ptr const& a, entry_ptr const& b) { return a->name() < b->name();
return a->name() < b->name(); });
});
} }
void dir::scan(os_access const&, progress&) {} void dir::scan(os_access const&, progress&) {}
@ -427,8 +426,8 @@ std::shared_ptr<entry> dir::find(fs::path const& path) {
return it->second; return it->second;
} }
} else { } else {
auto it = std::find_if(entries_.begin(), entries_.end(), auto it = std::ranges::find_if(
[name](auto& e) { return e->name() == name; }); entries_, [name](auto& e) { return e->name() == name; });
if (it != entries_.end()) { if (it != entries_.end()) {
return *it; return *it;
} }

View File

@ -95,8 +95,8 @@ class inode_ : public inode {
bool has_category(fragment_category cat) const override { bool has_category(fragment_category cat) const override {
DWARFS_CHECK(!fragments_.empty(), DWARFS_CHECK(!fragments_.empty(),
"has_category() called with no fragments"); "has_category() called with no fragments");
return std::any_of(fragments_.begin(), fragments_.end(), return std::ranges::any_of(
[cat](auto const& f) { return f.category() == cat; }); fragments_, [cat](auto const& f) { return f.category() == cat; });
} }
std::optional<uint32_t> std::optional<uint32_t>
@ -610,7 +610,7 @@ class inode_manager_ final : public inode_manager::impl {
return catmgr.deterministic_less(a, b); return catmgr.deterministic_less(a, b);
}); });
} else { } else {
std::sort(rv.categories.begin(), rv.categories.end()); std::ranges::sort(rv.categories);
} }
return rv; return rv;

View File

@ -19,6 +19,8 @@
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>. * along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <algorithm>
#include <dwarfs/writer/internal/nilsimsa.h> #include <dwarfs/writer/internal/nilsimsa.h>
namespace dwarfs::writer::internal { namespace dwarfs::writer::internal {
@ -88,7 +90,7 @@ class nilsimsa::impl {
size_t threshold = total / acc_.size(); size_t threshold = total / acc_.size();
std::fill(hash.begin(), hash.end(), 0); std::ranges::fill(hash, 0);
for (size_t i = 0; i < acc_.size(); i++) { for (size_t i = 0; i < acc_.size(); i++) {
if (acc_[i] > threshold) { if (acc_[i] > threshold) {

View File

@ -55,7 +55,7 @@ auto progress::get_active_contexts() const
contexts_.end()); contexts_.end());
} }
std::stable_sort(rv.begin(), rv.end(), [](const auto& a, const auto& b) { std::ranges::stable_sort(rv, [](const auto& a, const auto& b) {
return a->get_priority() > b->get_priority(); return a->get_priority() > b->get_priority();
}); });

View File

@ -19,6 +19,7 @@
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>. * along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <algorithm>
#include <cassert> #include <cassert>
#include <regex> #include <regex>
#include <unordered_set> #include <unordered_set>
@ -185,8 +186,7 @@ void rule_based_entry_filter_<LoggerPolicy>::set_root_path(
// Both '/' and '\\' are, surprisingly, valid path separators on Windows, // Both '/' and '\\' are, surprisingly, valid path separators on Windows,
// and invalid characters in filenames. So on Windows, it's a lossless // and invalid characters in filenames. So on Windows, it's a lossless
// transformation to replace all '\\' with '/'. // transformation to replace all '\\' with '/'.
std::replace(root_path_.begin(), root_path_.end(), kLocalPathSeparator, std::ranges::replace(root_path_, kLocalPathSeparator, '/');
'/');
} }
if (root_path_.ends_with('/')) { if (root_path_.ends_with('/')) {

View File

@ -234,7 +234,7 @@ class save_shared_files_visitor : public visitor_base {
void pack_shared_files() { void pack_shared_files() {
if (!shared_files_.empty()) { if (!shared_files_.empty()) {
DWARFS_CHECK(std::is_sorted(shared_files_.begin(), shared_files_.end()), DWARFS_CHECK(std::ranges::is_sorted(shared_files_),
"shared files vector not sorted"); "shared files vector not sorted");
std::vector<uint32_t> compressed; std::vector<uint32_t> compressed;
compressed.reserve(shared_files_.back() + 1); compressed.reserve(shared_files_.back() + 1);