mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-13 06:16:55 -04:00
refactor: prefer std::ranges::
algorithm variants
This commit is contained in:
parent
ca530f6cd1
commit
ab8f67619f
@ -223,7 +223,7 @@ std::vector<std::string> checksum::available_algorithms() {
|
||||
available.insert(available.end(), supported_algorithms.begin(),
|
||||
supported_algorithms.end());
|
||||
available.insert(available.end(), available_evp.begin(), available_evp.end());
|
||||
std::sort(available.begin(), available.end());
|
||||
std::ranges::sort(available);
|
||||
return available;
|
||||
}
|
||||
|
||||
|
@ -85,7 +85,7 @@ class glob_matcher_ final : public glob_matcher::impl {
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
@ -54,9 +54,8 @@ std::set<std::string>
|
||||
feature_set::get_unsupported(std::set<std::string> wanted_features) {
|
||||
auto const supported_features = get_supported();
|
||||
std::set<std::string> missing;
|
||||
std::set_difference(wanted_features.begin(), wanted_features.end(),
|
||||
supported_features.begin(), supported_features.end(),
|
||||
std::inserter(missing, missing.end()));
|
||||
std::ranges::set_difference(wanted_features, supported_features,
|
||||
std::inserter(missing, missing.end()));
|
||||
return missing;
|
||||
}
|
||||
|
||||
|
@ -272,7 +272,7 @@ string_table::pack_generic(std::span<T const> input,
|
||||
output.buffer()->swap(buffer);
|
||||
output.symtab() = std::move(symtab);
|
||||
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 {
|
||||
// store uncompressed
|
||||
output.buffer()->reserve(total_input_size);
|
||||
|
@ -87,7 +87,7 @@ void library_dependencies::add_library(std::string const& name_version_string) {
|
||||
if (tmp.starts_with("lib")) {
|
||||
tmp.erase(0, 3);
|
||||
}
|
||||
std::replace(tmp.begin(), tmp.end(), ' ', '-');
|
||||
std::ranges::replace(tmp, ' ', '-');
|
||||
deps_.insert(tmp);
|
||||
}
|
||||
|
||||
|
@ -238,8 +238,8 @@ void stream_logger::write(level_type level, std::string_view output,
|
||||
|
||||
if (output.find('\r') != std::string::npos) {
|
||||
tmp.reserve(output.size());
|
||||
std::copy_if(output.begin(), output.end(), std::back_inserter(tmp),
|
||||
[](char c) { return c != '\r'; });
|
||||
std::ranges::copy_if(output, std::back_inserter(tmp),
|
||||
[](char c) { return c != '\r'; });
|
||||
split_to(tmp, '\n', lines);
|
||||
} else {
|
||||
split_to(output, '\n', lines);
|
||||
@ -261,7 +261,7 @@ void stream_logger::write(level_type level, std::string_view output,
|
||||
<< newline;
|
||||
|
||||
if (clear_ctx) {
|
||||
std::fill(t.begin(), t.end(), '.');
|
||||
std::ranges::fill(t, '.');
|
||||
context.assign(context_len, ' ');
|
||||
clear_ctx = false;
|
||||
}
|
||||
|
@ -74,9 +74,9 @@ size_t option_map::get_size(const std::string& key, size_t default_value) {
|
||||
void option_map::report() {
|
||||
if (!opt_.empty()) {
|
||||
std::vector<std::string> invalid;
|
||||
std::transform(opt_.begin(), opt_.end(), std::back_inserter(invalid),
|
||||
[](const auto& p) { return p.first; });
|
||||
std::sort(invalid.begin(), invalid.end());
|
||||
std::ranges::transform(opt_, std::back_inserter(invalid),
|
||||
[](const auto& p) { return p.first; });
|
||||
std::ranges::sort(invalid);
|
||||
DWARFS_THROW(runtime_error,
|
||||
fmt::format("invalid option(s) for choice {}: {}", choice_,
|
||||
fmt::join(invalid, ", ")));
|
||||
|
@ -229,7 +229,7 @@ class performance_monitor_impl final : public performance_monitor {
|
||||
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) ||
|
||||
(std::get<0>(a) == std::get<0>(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(),
|
||||
[](auto const& a, auto const& b) { return a.ts < b.ts; });
|
||||
std::ranges::sort(events,
|
||||
[](auto const& a, auto const& b) { return a.ts < b.ts; });
|
||||
|
||||
bool first = true;
|
||||
auto const pid = ::getpid();
|
||||
|
@ -100,9 +100,8 @@ fsinfo_features fsinfo_features::parse(std::string_view features) {
|
||||
fsinfo_features result;
|
||||
|
||||
for (auto const& f : split_view<std::string_view>(features, ',')) {
|
||||
auto const it =
|
||||
std::find_if(fsinfo_feature_names.begin(), fsinfo_feature_names.end(),
|
||||
[&f](auto const& p) { return f == p.second; });
|
||||
auto const it = std::ranges::find_if(
|
||||
fsinfo_feature_names, [&f](auto const& p) { return f == p.second; });
|
||||
|
||||
if (it == fsinfo_feature_names.end()) {
|
||||
DWARFS_THROW(runtime_error, fmt::format("invalid feature: \"{}\"", f));
|
||||
|
@ -108,12 +108,11 @@ class lru_sequential_access_detector : public sequential_access_detector {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
auto minmax = std::minmax_element(
|
||||
lru_.begin(), lru_.end(),
|
||||
[](auto const& a, auto const& b) { return a.first < b.first; });
|
||||
auto minmax = std::ranges::minmax_element(
|
||||
lru_, [](auto const& a, auto const& b) { return a.first < b.first; });
|
||||
|
||||
auto min = minmax.first->first;
|
||||
auto max = minmax.second->first;
|
||||
auto min = minmax.min->first;
|
||||
auto max = minmax.max->first;
|
||||
|
||||
is_sequential_ = max - min + 1 == seq_blocks_;
|
||||
|
||||
@ -185,8 +184,7 @@ class block_request_set {
|
||||
|
||||
void merge(block_request_set& other) {
|
||||
queue_.reserve(queue_.size() + other.queue_.size());
|
||||
std::move(other.queue_.begin(), other.queue_.end(),
|
||||
std::back_inserter(queue_));
|
||||
std::ranges::move(other.queue_, std::back_inserter(queue_));
|
||||
other.queue_.clear();
|
||||
std::make_heap(queue_.begin(), queue_.end());
|
||||
range_end_ = std::max(range_end_, other.range_end_);
|
||||
|
@ -508,7 +508,7 @@ void check_compact_strings(
|
||||
fmt::format("invalid first compact {0} index: {1}", what,
|
||||
idx.front()));
|
||||
}
|
||||
if (!std::is_sorted(idx.begin(), idx.end())) {
|
||||
if (!std::ranges::is_sorted(idx)) {
|
||||
DWARFS_THROW(runtime_error,
|
||||
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();
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
offsets[r] = std::distance(
|
||||
inodes.begin(),
|
||||
std::partition_point(inodes.begin(), inodes.end(), pred));
|
||||
offsets[r] = std::distance(inodes.begin(),
|
||||
std::ranges::partition_point(inodes, pred));
|
||||
} else {
|
||||
auto pred = [r, modes = meta.modes(), inodes = meta.inodes()](auto ent) {
|
||||
return mode_rank(modes[inodes[ent].mode_index()]) < r;
|
||||
};
|
||||
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");
|
||||
}
|
||||
|
||||
offsets[r] = std::distance(
|
||||
entries.begin(),
|
||||
std::partition_point(entries.begin(), entries.end(), pred));
|
||||
offsets[r] = std::distance(entries.begin(),
|
||||
std::ranges::partition_point(entries, pred));
|
||||
}
|
||||
}
|
||||
|
||||
@ -1049,7 +1047,7 @@ std::string dir_entry_view_impl::unix_path() const {
|
||||
static_cast<char>(std::filesystem::path::preferred_separator);
|
||||
auto p = path();
|
||||
if constexpr (preferred != '/') {
|
||||
std::replace(p.begin(), p.end(), preferred, '/');
|
||||
std::ranges::replace(p, preferred, '/');
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
@ -310,7 +310,7 @@ void analyze_frozen(std::ostream& os,
|
||||
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);
|
||||
});
|
||||
|
||||
@ -816,7 +816,7 @@ class metadata_ final : public metadata_v2::impl {
|
||||
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;
|
||||
|
||||
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());
|
||||
|
||||
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
|
||||
// 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::iota(entries.begin(), entries.end(), 0);
|
||||
boost::sort::flat_stable_sort(
|
||||
|
@ -145,7 +145,7 @@ unsigned get_unused_lsb_count<uint16_t>(std::span<uint8_t const> imagedata) {
|
||||
size_t size = imagedata.size_bytes() / kAlignment;
|
||||
|
||||
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 k = 0; k < b512.size(); ++k) {
|
||||
|
@ -60,7 +60,7 @@ void check_unsupported_metadata_requirements(nlohmann::json& req) {
|
||||
for (auto const& [k, v] : req.items()) {
|
||||
keys.emplace_back(k);
|
||||
}
|
||||
std::sort(keys.begin(), keys.end());
|
||||
std::ranges::sort(keys);
|
||||
throw std::runtime_error(fmt::format(
|
||||
"unsupported metadata requirements: {}", fmt::join(keys, ", ")));
|
||||
}
|
||||
|
@ -129,7 +129,7 @@ std::string entry::unix_dpath() const {
|
||||
if (auto parent = parent_.lock()) {
|
||||
p = parent->unix_dpath() + p;
|
||||
} 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() {
|
||||
std::sort(entries_.begin(), entries_.end(),
|
||||
[](entry_ptr const& a, entry_ptr const& b) {
|
||||
return a->name() < b->name();
|
||||
});
|
||||
std::ranges::sort(entries_, [](entry_ptr const& a, entry_ptr const& b) {
|
||||
return a->name() < b->name();
|
||||
});
|
||||
}
|
||||
|
||||
void dir::scan(os_access const&, progress&) {}
|
||||
@ -427,8 +426,8 @@ std::shared_ptr<entry> dir::find(fs::path const& path) {
|
||||
return it->second;
|
||||
}
|
||||
} else {
|
||||
auto it = std::find_if(entries_.begin(), entries_.end(),
|
||||
[name](auto& e) { return e->name() == name; });
|
||||
auto it = std::ranges::find_if(
|
||||
entries_, [name](auto& e) { return e->name() == name; });
|
||||
if (it != entries_.end()) {
|
||||
return *it;
|
||||
}
|
||||
|
@ -95,8 +95,8 @@ class inode_ : public inode {
|
||||
bool has_category(fragment_category cat) const override {
|
||||
DWARFS_CHECK(!fragments_.empty(),
|
||||
"has_category() called with no fragments");
|
||||
return std::any_of(fragments_.begin(), fragments_.end(),
|
||||
[cat](auto const& f) { return f.category() == cat; });
|
||||
return std::ranges::any_of(
|
||||
fragments_, [cat](auto const& f) { return f.category() == cat; });
|
||||
}
|
||||
|
||||
std::optional<uint32_t>
|
||||
@ -610,7 +610,7 @@ class inode_manager_ final : public inode_manager::impl {
|
||||
return catmgr.deterministic_less(a, b);
|
||||
});
|
||||
} else {
|
||||
std::sort(rv.categories.begin(), rv.categories.end());
|
||||
std::ranges::sort(rv.categories);
|
||||
}
|
||||
|
||||
return rv;
|
||||
|
@ -19,6 +19,8 @@
|
||||
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <dwarfs/writer/internal/nilsimsa.h>
|
||||
|
||||
namespace dwarfs::writer::internal {
|
||||
@ -88,7 +90,7 @@ class nilsimsa::impl {
|
||||
|
||||
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++) {
|
||||
if (acc_[i] > threshold) {
|
||||
|
@ -55,7 +55,7 @@ auto progress::get_active_contexts() const
|
||||
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();
|
||||
});
|
||||
|
||||
|
@ -19,6 +19,7 @@
|
||||
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <regex>
|
||||
#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,
|
||||
// and invalid characters in filenames. So on Windows, it's a lossless
|
||||
// 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('/')) {
|
||||
|
@ -234,7 +234,7 @@ class save_shared_files_visitor : public visitor_base {
|
||||
|
||||
void pack_shared_files() {
|
||||
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");
|
||||
std::vector<uint32_t> compressed;
|
||||
compressed.reserve(shared_files_.back() + 1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user