/*
* This file is part of the DXX-Rebirth project .
* It is copyright by its individual contributors, as recorded in the
* project's Git history. See COPYING.txt at the top level for license
* terms and a link to the Git history.
*/
#pragma once
#include
#include
#include
#include
#include "dxxsconf.h"
#include
#include
#include
enum class zip_sequence_length_selector : uint32_t;
template
using zip_sequence_selector = std::integral_constant;
namespace d_zip {
namespace detail {
/* `range_static_extent` stores the extent of a range. If the extent is not
* available as an integer constant expression (such as happens for
* `std::vector`, which is a range, but the extent of the range can only
* be determined at runtime), then `dynamic_extent` is used as a placeholder.
*/
enum class range_static_extent : std::size_t
{
dynamic_extent = std::dynamic_extent,
};
template
/* By default, the static size is unknown. */
inline constexpr range_static_extent range_static_size{range_static_extent::dynamic_extent};
template
/* If the range can be used to construct a `std::span`, then consider the
* extent of the range to be the extent of the resulting span. This allows
* measuring any range for which `std::span` has an overload, including
* C arrays, `std::array`, and `std::span`.
*
* `std::span` only accepts contiguous ranges. If there are non-contiguous
* ranges that can be measured at compile-time, a specialization to cover those
* types can be added below.
*/
requires(
requires(Range &r) {
decltype(std::span(r))::extent;
}
)
inline constexpr range_static_extent range_static_size{decltype(std::span(std::declval()))::extent};
template
static constexpr auto capture_end_iterator(range &&r)
{
if constexpr (ignore)
return std::ignore;
else
return std::ranges::end(r);
}
template
requires(
examine_end_range != zip_sequence_length_selector{} &&
sizeof...(range_sentinel_type) > 0
)
struct zip_sentinel : public std::tuple
{
using selector = zip_sequence_selector;
template
constexpr explicit zip_sentinel(rangeN &&...r) :
std::tuple{
capture_end_iterator<
std::is_same_v
>(r)...
}
{
}
};
template
struct for_index_sequence
{
/* This class is never instantiated, but a constructor is declared to
* support Class Template Argument Deduction.
*/
for_index_sequence(std::index_sequence);
/* Run-time functions. These are defined and called. */
template
static void increment_iterator(std::tuple &iterator)
{
(static_cast(++(std::get(iterator))), ...);
}
template
static void decrement_iterator(std::tuple &iterator)
{
(static_cast(--(std::get(iterator))), ...);
}
template
static auto dereference_iterator(const std::tuple &iterator);
template
static constexpr bool compare_zip_iterators(const zip_iterator &iterator, const zip_sentinel &sentinel);
};
template
template
auto for_index_sequence::dereference_iterator(const std::tuple &iterator)
{
/* std::make_tuple is not appropriate here, because the result of
* dereferencing the iterator may be a reference, and the resulting
* tuple should store the reference, not the underlying object.
* Calling std::make_tuple would decay such references into the
* underlying type.
*
* std::tie is not appropriate here, because it captures all
* arguments as `T &`, so it fails to compile if the result of
* dereferencing the iterator is not a reference.
*/
return std::tuple<
decltype(*(std::get(iterator)))...
>(*(std::get(iterator))...);
}
/* Given a zip_sequence_length_selector and an index N in a sequence, evaluate
* to true if the element at index N is examined, and otherwise false.
*
* This is a convenience variable to keep the test in one place, rather than
* duplicating the cast+mask everywhere it is needed.
*/
template
inline constexpr bool examine_zip_element{(N < 32 && (static_cast(examine_end_range) & (1u << N)))};
/* `std::integer_sequence` would work here, except
* that `range_static_extent` is an `enum class` and gcc-14 requires
* `std::is_integral_v` be true for the integer type `T`. `enum class` does
* not satisfy `std::is_integral_v`. Therefore, define a custom type to serve
* an equivalent purpose to `std::integer_sequence`.
*/
template
struct range_extent_sequence
{
static constexpr std::size_t size{sizeof...(N)};
};
/* Given a zip_sequence_length_selector and a sequence of static sizes In,
* return a new sequence of static sizes `On` where:
* - If index N is selected, `On`=`In`.
* - Otherwise, `On`=`range_static_extent::dynamic_extent`.
*
* The output sequence discards static size information for elements that will
* not be selected at runtime.
*/
template
range_extent_sequence<(examine_zip_element ? sizeN : range_static_extent::dynamic_extent) ...> get_examined_static_size(std::index_sequence, range_extent_sequence);
/* Yield the lesser of the first size or the next iteration of the template.
* Subsequent iterations of the template will fold down the remaining values
* until the base case is reached.
*/
template
consteval range_static_extent minimum_static_size(range_extent_sequence)
{
if constexpr (sizeof...(sizeN) > 0)
return std::min(size0, minimum_static_size(range_extent_sequence()));
else
/* A sequence of one element is trivially the size from that element, regardless
* of the value of size.
*/
return size0;
}
void range_index_type(...);
template <
typename... range_type,
/* If any `range_type::index_type` is not defined, fail.
* If there is no common_type among all the
* `range_type::index_type`, fail.
*/
typename index_type = typename std::common_type::type::index_type &>::type...>::type,
/* If the common_type `index_type` is not a suitable argument to all
* `range_type::operator[]()`, fail.
*/
typename = std::void_t().operator[](std::declval()))...>
>
index_type range_index_type(std::tuple *);
/* Given as inputs:
* - a selector mask
* - a tuple of iterator types
* - a std::index_sequence of the same length as the tuple
*
* Produce a type defined as zip_sentinel where On=In if the iterator is
* selected in by the mask, and On=std::ignore if the iterator is not selected.
* The resulting tuple will then collapse down, avoiding the need to save
* non-selected types In in the end iterator.
*/
template
requires(examine_end_range != zip_sequence_length_selector{} && sizeof...(range_index) == sizeof...(range_type))
auto iterator_end_type(std::tuple, std::index_sequence) -> zip_sentinel<
examine_end_range,
typename std::conditional<
examine_zip_element,
decltype(std::ranges::end(std::declval())),
decltype(std::ignore)
>::type ...
>;
template
static constexpr auto compare_zip_iterator(const zip_iterator &l, const zip_sentinel &r)
{
if constexpr (mask)
/* For each included member of the iterator, check if the member compares
* equal between the two instances. Excluded members use the else
* block.
*/
return std::get(l) == std::get(r);
else
{
/* For each excluded member of the iterator, do nothing. Excluded members
* are not checked for equality, because excluded members are omitted from
* the sentinel, instead of being set to the value of `rangeN.end()`,
* so examining such members is not possible.
*/
(void)l;
(void)r;
return std::false_type{};
}
}
template
template
constexpr bool for_index_sequence::compare_zip_iterators(const zip_iterator &iterator, const zip_sentinel &sentinel)
{
/* Note the use of `||`, which is atypical for an equality comparison. By
* design, a zip iterator should terminate when any of the component
* sequences reaches its end, so use of `||` is correct for that purpose.
*/
return (compare_zip_iterator, N>(iterator, sentinel) || ... || false);
}
}
}
/* This iterator terminates when the selected zipped range(s) terminate. The
* caller is responsible for ensuring that use of the zip_iterator does
* not increment past the end of any zipped range. This can be done by
* ensuring that the selected zipped range(s) are not longer than any ignored
* zipped range, or by ensuring that external logic stops the traversal
* before the zip_iterator increments past the end.
*
* There is no runtime check that the below loop would be
* safe, since a check external to the zip_iterator could stop before
* undefined behaviour occurs.
*
* However, if any selected range is convertible to a C array of known
* length or to a C++ std::array, then there is a compile-time check
* that the shortest selected range is not longer than any other range that is
* likewise convertible. If a range cannot be converted to an array,
* then its length is unknown and is not checked. If the first range is
* not convertible, then no ranges are checked.
for (auto i = zip_range.begin(), e = zip_range.end(); i != e; ++i)
{
if (condition())
break;
}
*/
template
class zip_iterator;
template
class zip_iterator, range_sentinel_type> : std::tuple
{
using range_iterator_tuple = std::tuple;
protected:
using for_index_sequence = decltype(d_zip::detail::for_index_sequence(std::make_index_sequence()));
public:
using range_iterator_tuple::range_iterator_tuple;
using difference_type = typename std::incrementable_traits<
typename std::remove_reference_t<
std::tuple_element_t<0, range_iterator_tuple>
>>::difference_type;
using value_type = decltype(for_index_sequence::dereference_iterator(std::declval()));
using pointer = value_type *;
using reference = value_type &;
/*
* Given a tuple of iterator types, return the most-derived iterator_category
* that all the iterator types satisfy.
*/
using iterator_category = typename std::common_type::iterator_category...>::type;
auto operator*() const
{
return for_index_sequence::dereference_iterator(static_cast(*this));
}
zip_iterator &operator++()
{
for_index_sequence::increment_iterator(static_cast(*this));
return *this;
}
/* operator++(int) is currently unused, but is required to satisfy
* the concept check on forward iterator.
*/
zip_iterator operator++(int)
{
auto result{*this};
++ *this;
return result;
}
zip_iterator &operator--()
requires(
requires(range_iterator_tuple i) {
for_index_sequence::decrement_iterator(i);
}
)
{
for_index_sequence::decrement_iterator(static_cast(*this));
return *this;
}
zip_iterator operator--(int)
requires(
requires(zip_iterator i) {
-- i;
}
)
{
auto result{*this};
-- *this;
return result;
}
auto operator-(const zip_iterator &i) const
requires(
std::derived_from &&
requires(range_iterator_tuple i) {
std::get<0>(i) - std::get<0>(i);
}
)
{
return std::get<0>(static_cast(*this)) - std::get<0>(static_cast(i));
}
constexpr bool operator==(const zip_iterator &) const = default;
constexpr bool operator==(const range_sentinel_type &i) const
{
return for_index_sequence::template compare_zip_iterators(*this, i);
}
};
template <
zip_sequence_length_selector examine_end_range,
/* Values in `range_all_static_sizes` are either a `range_static_extent`
* reporting the static size of the corresponding range, or
* `range_static_extent::dynamic_extent` if the range has no known
* static size.
*/
typename range_all_static_sizes, // range_extent_sequence
/* Value `minimum_all_static_size` is the `range_static_extent`
* reporting the static size of the shortest static range, or
* `range_static_extent::dynamic_extent` if no ranges have a known
* static size.
*/
d_zip::detail::range_static_extent minimum_all_static_size = d_zip::detail::minimum_static_size(range_all_static_sizes()),
/* Filter `range_all_static_sizes` to discard static sizes for elements
* that will not be examined at runtime.
*/
typename range_examined_static_sizes = decltype(d_zip::detail::get_examined_static_size(std::make_index_sequence(), range_all_static_sizes())),
/* Value `minimum_examined_static_size` reports the static size of the
* shortest examined static range, or `range_static_extent::dynamic_extent`
* if no examined ranges have a known static size.
*/
d_zip::detail::range_static_extent minimum_examined_static_size = d_zip::detail::minimum_static_size(range_examined_static_sizes())
>
concept zip_static_size_bounds_check = (
/* If no examined range has a static size, then
* `minimum_examined_static_size` is
* `range_static_extent::dynamic_extent` and no compile-time check is
* possible.
*
* Otherwise, at least one examined range has a static size, so check
* that it is not longer than an unexamined static range.
*/
(minimum_examined_static_size == d_zip::detail::range_static_extent::dynamic_extent || minimum_examined_static_size <= minimum_all_static_size)
);
template <
zip_sequence_length_selector examine_end_range,
typename... rangeN
>
concept zip_input_constraints = (
/* Require at least two ranges. A zip of a single range adds complexity for no reason. */
sizeof...(rangeN) > 1 &&
(std::ranges::input_range && ...) &&
/* Require borrowed ranges, because zip will return iterators that point into the ranges. */
(std::ranges::borrowed_range && ...) &&
zip_static_size_bounds_check<
examine_end_range,
d_zip::detail::range_extent_sequence...>
>
);
/* Class Template Argument Deduction cannot implicitly deduce the template type
* arguments for `zip` because none of the types are used directly as an
* argument to any constructor, but instead are derived from properties of the
* constructor arguments.
*
* Explicit deduction guides are provided below to handle constructing `zip`
* without specifying template argument types.
*/
template
struct zip : std::ranges::subrange, sentinel_type>
{
using index_type = range_index_type;
using iterator = zip_iterator;
template
requires(
zip_input_constraints
)
constexpr zip(rangeN &&... rN) :
std::ranges::subrange{iterator{std::ranges::begin(rN)...}, sentinel_type{rN...}}
{
}
template
/* Delegate to the no-selector constructor, since the selector is not
* needed by the constructor. The selector affected the type of
* `zip_sentinel_type`, which in turn affects how `m_end` is
* constructed.
*/
constexpr zip(sentinel_type::selector, rangeN &&... rN) :
zip{std::forward(rN)...}
{
}
};
/* zip() is always a view onto another range, and never owns the storage that
* its iterators reference. */
template
inline constexpr bool std::ranges::enable_borrowed_range> = true;
template
requires(
zip_input_constraints
)
zip(zip_sequence_selector, rangeN &&... rN) -> zip<
/* range_index_type = */ decltype(d_zip::detail::range_index_type(static_cast *>(nullptr))),
/* range_iterator_type = */ std::tuple()))...>,
/* range_sentinel_type = */ decltype(
d_zip::detail::iterator_end_type(
std::declval>(),
std::make_index_sequence()
)
)
>;
/* Store the default selector as a default template argument, so that the
* template-id specified by the deduction guide can use the same text in both
* the explicit-selector and implicit-selector guides.
*/
template
requires(
zip_input_constraints
)
zip(rangeN &&... rN) -> zip<
/* range_index_type = */ decltype(d_zip::detail::range_index_type(static_cast *>(nullptr))),
/* range_iterator_type = */ std::tuple()))...>,
/* range_sentinel_type = */ decltype(
d_zip::detail::iterator_end_type(
std::declval>(),
std::make_index_sequence()
)
)
>;