mirror of
https://github.com/NixOS/nix.git
synced 2025-11-16 15:32:43 +01:00
libutil: Replace hand-rolled enumerate with std::views::{zip,iota}
It would have been nice to use std::views::enumerate here, but it uses a signed difference type for the value_type: > value_type = std::tuple<difference_type, ranges::range_value_t<Base>> zip + iota has the same semantics as the code used to have, so there's no behavior change here.
This commit is contained in:
parent
d7612f350f
commit
b853994e7a
1 changed files with 6 additions and 47 deletions
|
|
@ -4,13 +4,13 @@
|
|||
#include "nix/util/types.hh"
|
||||
#include "nix/util/error.hh"
|
||||
#include "nix/util/logging.hh"
|
||||
#include "nix/util/strings.hh"
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <optional>
|
||||
|
||||
#include "nix/util/strings.hh"
|
||||
#include <ranges>
|
||||
|
||||
namespace nix {
|
||||
|
||||
|
|
@ -300,53 +300,12 @@ struct MaintainCount
|
|||
|
||||
/**
|
||||
* A Rust/Python-like enumerate() iterator adapter.
|
||||
*
|
||||
* Borrowed from http://reedbeta.com/blog/python-like-enumerate-in-cpp17.
|
||||
*/
|
||||
template<
|
||||
typename T,
|
||||
typename TIter = decltype(std::begin(std::declval<T>())),
|
||||
typename = decltype(std::end(std::declval<T>()))>
|
||||
constexpr auto enumerate(T && iterable)
|
||||
template<std::ranges::viewable_range R>
|
||||
constexpr auto enumerate(R && range)
|
||||
{
|
||||
struct iterator
|
||||
{
|
||||
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)};
|
||||
/* 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));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue