1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-12-06 09:01:01 +01:00

Merge pull request #13809 from xokdvium/enumerate

libutil: Replace hand-rolled enumerate with std::views::{zip,iota}
This commit is contained in:
John Ericson 2025-08-21 22:58:19 -04:00 committed by GitHub
commit 58278974b6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,13 +4,13 @@
#include "nix/util/types.hh" #include "nix/util/types.hh"
#include "nix/util/error.hh" #include "nix/util/error.hh"
#include "nix/util/logging.hh" #include "nix/util/logging.hh"
#include "nix/util/strings.hh"
#include <functional> #include <functional>
#include <map> #include <map>
#include <sstream> #include <sstream>
#include <optional> #include <optional>
#include <ranges>
#include "nix/util/strings.hh"
namespace nix { namespace nix {
@ -300,53 +300,12 @@ struct MaintainCount
/** /**
* A Rust/Python-like enumerate() iterator adapter. * A Rust/Python-like enumerate() iterator adapter.
*
* Borrowed from http://reedbeta.com/blog/python-like-enumerate-in-cpp17.
*/ */
template< template<std::ranges::viewable_range R>
typename T, constexpr auto enumerate(R && range)
typename TIter = decltype(std::begin(std::declval<T>())),
typename = decltype(std::end(std::declval<T>()))>
constexpr auto enumerate(T && iterable)
{ {
struct iterator /* Not std::views::enumerate because it uses difference_type for the index. */
{ return std::views::zip(std::views::iota(size_t{0}), std::forward<R>(range));
size_t i;
TIter iter;
constexpr bool operator!=(const iterator & other) const
{
return iter != other.iter;
}
constexpr void operator++()
{
++i;
++iter;
}
constexpr auto operator*() const
{
return std::tie(i, *iter);
}
};
struct iterable_wrapper
{
T iterable;
constexpr auto begin()
{
return iterator{0, std::begin(iterable)};
}
constexpr auto end()
{
return iterator{0, std::end(iterable)};
}
};
return iterable_wrapper{std::forward<T>(iterable)};
} }
/** /**