10b57cec5SDimitry Andric //===- llvm/ADT/STLExtras.h - Useful STL related functions ------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 81fd87a68SDimitry Andric /// 91fd87a68SDimitry Andric /// \file 101fd87a68SDimitry Andric /// This file contains some templates that are useful if you are working with 111fd87a68SDimitry Andric /// the STL at all. 121fd87a68SDimitry Andric /// 131fd87a68SDimitry Andric /// No library is required when using these functions. 141fd87a68SDimitry Andric /// 150b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 160b57cec5SDimitry Andric 170b57cec5SDimitry Andric #ifndef LLVM_ADT_STLEXTRAS_H 180b57cec5SDimitry Andric #define LLVM_ADT_STLEXTRAS_H 190b57cec5SDimitry Andric 20*06c3fb27SDimitry Andric #include "llvm/ADT/ADL.h" 21bdd1243dSDimitry Andric #include "llvm/ADT/Hashing.h" 22fe6060f1SDimitry Andric #include "llvm/ADT/STLForwardCompat.h" 2304eeddc0SDimitry Andric #include "llvm/ADT/STLFunctionalExtras.h" 241fd87a68SDimitry Andric #include "llvm/ADT/identity.h" 250b57cec5SDimitry Andric #include "llvm/ADT/iterator.h" 260b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h" 270b57cec5SDimitry Andric #include "llvm/Config/abi-breaking.h" 280b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 290b57cec5SDimitry Andric #include <algorithm> 300b57cec5SDimitry Andric #include <cassert> 310b57cec5SDimitry Andric #include <cstddef> 320b57cec5SDimitry Andric #include <cstdint> 330b57cec5SDimitry Andric #include <cstdlib> 340b57cec5SDimitry Andric #include <functional> 350b57cec5SDimitry Andric #include <initializer_list> 360b57cec5SDimitry Andric #include <iterator> 370b57cec5SDimitry Andric #include <limits> 380b57cec5SDimitry Andric #include <memory> 39bdd1243dSDimitry Andric #include <optional> 400b57cec5SDimitry Andric #include <tuple> 410b57cec5SDimitry Andric #include <type_traits> 420b57cec5SDimitry Andric #include <utility> 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric #ifdef EXPENSIVE_CHECKS 450b57cec5SDimitry Andric #include <random> // for std::mt19937 460b57cec5SDimitry Andric #endif 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric namespace llvm { 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 510b57cec5SDimitry Andric // Extra additions to <type_traits> 520b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric template <typename T> struct make_const_ptr { 55bdd1243dSDimitry Andric using type = std::add_pointer_t<std::add_const_t<T>>; 560b57cec5SDimitry Andric }; 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric template <typename T> struct make_const_ref { 59bdd1243dSDimitry Andric using type = std::add_lvalue_reference_t<std::add_const_t<T>>; 600b57cec5SDimitry Andric }; 610b57cec5SDimitry Andric 625ffd83dbSDimitry Andric namespace detail { 635ffd83dbSDimitry Andric template <class, template <class...> class Op, class... Args> struct detector { 645ffd83dbSDimitry Andric using value_t = std::false_type; 655ffd83dbSDimitry Andric }; 665ffd83dbSDimitry Andric template <template <class...> class Op, class... Args> 67bdd1243dSDimitry Andric struct detector<std::void_t<Op<Args...>>, Op, Args...> { 685ffd83dbSDimitry Andric using value_t = std::true_type; 695ffd83dbSDimitry Andric }; 705ffd83dbSDimitry Andric } // end namespace detail 715ffd83dbSDimitry Andric 72fe6060f1SDimitry Andric /// Detects if a given trait holds for some set of arguments 'Args'. 73fe6060f1SDimitry Andric /// For example, the given trait could be used to detect if a given type 74fe6060f1SDimitry Andric /// has a copy assignment operator: 75fe6060f1SDimitry Andric /// template<class T> 76fe6060f1SDimitry Andric /// using has_copy_assign_t = decltype(std::declval<T&>() 77fe6060f1SDimitry Andric /// = std::declval<const T&>()); 78fe6060f1SDimitry Andric /// bool fooHasCopyAssign = is_detected<has_copy_assign_t, FooClass>::value; 795ffd83dbSDimitry Andric template <template <class...> class Op, class... Args> 805ffd83dbSDimitry Andric using is_detected = typename detail::detector<void, Op, Args...>::value_t; 815ffd83dbSDimitry Andric 825ffd83dbSDimitry Andric /// This class provides various trait information about a callable object. 835ffd83dbSDimitry Andric /// * To access the number of arguments: Traits::num_args 845ffd83dbSDimitry Andric /// * To access the type of an argument: Traits::arg_t<Index> 855ffd83dbSDimitry Andric /// * To access the type of the result: Traits::result_t 865ffd83dbSDimitry Andric template <typename T, bool isClass = std::is_class<T>::value> 875ffd83dbSDimitry Andric struct function_traits : public function_traits<decltype(&T::operator())> {}; 885ffd83dbSDimitry Andric 895ffd83dbSDimitry Andric /// Overload for class function types. 905ffd83dbSDimitry Andric template <typename ClassType, typename ReturnType, typename... Args> 915ffd83dbSDimitry Andric struct function_traits<ReturnType (ClassType::*)(Args...) const, false> { 925ffd83dbSDimitry Andric /// The number of arguments to this function. 935ffd83dbSDimitry Andric enum { num_args = sizeof...(Args) }; 945ffd83dbSDimitry Andric 955ffd83dbSDimitry Andric /// The result type of this function. 965ffd83dbSDimitry Andric using result_t = ReturnType; 975ffd83dbSDimitry Andric 985ffd83dbSDimitry Andric /// The type of an argument to this function. 995ffd83dbSDimitry Andric template <size_t Index> 100bdd1243dSDimitry Andric using arg_t = std::tuple_element_t<Index, std::tuple<Args...>>; 1015ffd83dbSDimitry Andric }; 1025ffd83dbSDimitry Andric /// Overload for class function types. 1035ffd83dbSDimitry Andric template <typename ClassType, typename ReturnType, typename... Args> 1045ffd83dbSDimitry Andric struct function_traits<ReturnType (ClassType::*)(Args...), false> 10581ad6265SDimitry Andric : public function_traits<ReturnType (ClassType::*)(Args...) const> {}; 1065ffd83dbSDimitry Andric /// Overload for non-class function types. 1075ffd83dbSDimitry Andric template <typename ReturnType, typename... Args> 1085ffd83dbSDimitry Andric struct function_traits<ReturnType (*)(Args...), false> { 1095ffd83dbSDimitry Andric /// The number of arguments to this function. 1105ffd83dbSDimitry Andric enum { num_args = sizeof...(Args) }; 1115ffd83dbSDimitry Andric 1125ffd83dbSDimitry Andric /// The result type of this function. 1135ffd83dbSDimitry Andric using result_t = ReturnType; 1145ffd83dbSDimitry Andric 1155ffd83dbSDimitry Andric /// The type of an argument to this function. 1165ffd83dbSDimitry Andric template <size_t i> 117bdd1243dSDimitry Andric using arg_t = std::tuple_element_t<i, std::tuple<Args...>>; 1185ffd83dbSDimitry Andric }; 11981ad6265SDimitry Andric template <typename ReturnType, typename... Args> 12081ad6265SDimitry Andric struct function_traits<ReturnType (*const)(Args...), false> 12181ad6265SDimitry Andric : public function_traits<ReturnType (*)(Args...)> {}; 1225ffd83dbSDimitry Andric /// Overload for non-class function type references. 1235ffd83dbSDimitry Andric template <typename ReturnType, typename... Args> 1245ffd83dbSDimitry Andric struct function_traits<ReturnType (&)(Args...), false> 1255ffd83dbSDimitry Andric : public function_traits<ReturnType (*)(Args...)> {}; 1265ffd83dbSDimitry Andric 1270eae32dcSDimitry Andric /// traits class for checking whether type T is one of any of the given 1280eae32dcSDimitry Andric /// types in the variadic list. 1290eae32dcSDimitry Andric template <typename T, typename... Ts> 130bdd1243dSDimitry Andric using is_one_of = std::disjunction<std::is_same<T, Ts>...>; 1310eae32dcSDimitry Andric 1320eae32dcSDimitry Andric /// traits class for checking whether type T is a base class for all 1330eae32dcSDimitry Andric /// the given types in the variadic list. 1340eae32dcSDimitry Andric template <typename T, typename... Ts> 135bdd1243dSDimitry Andric using are_base_of = std::conjunction<std::is_base_of<T, Ts>...>; 1360eae32dcSDimitry Andric 1370eae32dcSDimitry Andric namespace detail { 1380eae32dcSDimitry Andric template <typename T, typename... Us> struct TypesAreDistinct; 1390eae32dcSDimitry Andric template <typename T, typename... Us> 1400eae32dcSDimitry Andric struct TypesAreDistinct 1410eae32dcSDimitry Andric : std::integral_constant<bool, !is_one_of<T, Us...>::value && 1420eae32dcSDimitry Andric TypesAreDistinct<Us...>::value> {}; 1430eae32dcSDimitry Andric template <typename T> struct TypesAreDistinct<T> : std::true_type {}; 1440eae32dcSDimitry Andric } // namespace detail 1450eae32dcSDimitry Andric 1460eae32dcSDimitry Andric /// Determine if all types in Ts are distinct. 1470eae32dcSDimitry Andric /// 1480eae32dcSDimitry Andric /// Useful to statically assert when Ts is intended to describe a non-multi set 1490eae32dcSDimitry Andric /// of types. 1500eae32dcSDimitry Andric /// 1510eae32dcSDimitry Andric /// Expensive (currently quadratic in sizeof(Ts...)), and so should only be 1520eae32dcSDimitry Andric /// asserted once per instantiation of a type which requires it. 1530eae32dcSDimitry Andric template <typename... Ts> struct TypesAreDistinct; 1540eae32dcSDimitry Andric template <> struct TypesAreDistinct<> : std::true_type {}; 1550eae32dcSDimitry Andric template <typename... Ts> 1560eae32dcSDimitry Andric struct TypesAreDistinct 1570eae32dcSDimitry Andric : std::integral_constant<bool, detail::TypesAreDistinct<Ts...>::value> {}; 1580eae32dcSDimitry Andric 1590eae32dcSDimitry Andric /// Find the first index where a type appears in a list of types. 1600eae32dcSDimitry Andric /// 1610eae32dcSDimitry Andric /// FirstIndexOfType<T, Us...>::value is the first index of T in Us. 1620eae32dcSDimitry Andric /// 1630eae32dcSDimitry Andric /// Typically only meaningful when it is otherwise statically known that the 1640eae32dcSDimitry Andric /// type pack has no duplicate types. This should be guaranteed explicitly with 1650eae32dcSDimitry Andric /// static_assert(TypesAreDistinct<Us...>::value). 1660eae32dcSDimitry Andric /// 1670eae32dcSDimitry Andric /// It is a compile-time error to instantiate when T is not present in Us, i.e. 1680eae32dcSDimitry Andric /// if is_one_of<T, Us...>::value is false. 1690eae32dcSDimitry Andric template <typename T, typename... Us> struct FirstIndexOfType; 1700eae32dcSDimitry Andric template <typename T, typename U, typename... Us> 1710eae32dcSDimitry Andric struct FirstIndexOfType<T, U, Us...> 1720eae32dcSDimitry Andric : std::integral_constant<size_t, 1 + FirstIndexOfType<T, Us...>::value> {}; 1730eae32dcSDimitry Andric template <typename T, typename... Us> 1740eae32dcSDimitry Andric struct FirstIndexOfType<T, T, Us...> : std::integral_constant<size_t, 0> {}; 1750eae32dcSDimitry Andric 1760eae32dcSDimitry Andric /// Find the type at a given index in a list of types. 1770eae32dcSDimitry Andric /// 1780eae32dcSDimitry Andric /// TypeAtIndex<I, Ts...> is the type at index I in Ts. 1790eae32dcSDimitry Andric template <size_t I, typename... Ts> 1800eae32dcSDimitry Andric using TypeAtIndex = std::tuple_element_t<I, std::tuple<Ts...>>; 1810eae32dcSDimitry Andric 18281ad6265SDimitry Andric /// Helper which adds two underlying types of enumeration type. 18381ad6265SDimitry Andric /// Implicit conversion to a common type is accepted. 18481ad6265SDimitry Andric template <typename EnumTy1, typename EnumTy2, 18581ad6265SDimitry Andric typename UT1 = std::enable_if_t<std::is_enum<EnumTy1>::value, 18681ad6265SDimitry Andric std::underlying_type_t<EnumTy1>>, 18781ad6265SDimitry Andric typename UT2 = std::enable_if_t<std::is_enum<EnumTy2>::value, 18881ad6265SDimitry Andric std::underlying_type_t<EnumTy2>>> 18981ad6265SDimitry Andric constexpr auto addEnumValues(EnumTy1 LHS, EnumTy2 RHS) { 19081ad6265SDimitry Andric return static_cast<UT1>(LHS) + static_cast<UT2>(RHS); 19181ad6265SDimitry Andric } 19281ad6265SDimitry Andric 1930b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1940b57cec5SDimitry Andric // Extra additions to <iterator> 1950b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 1960b57cec5SDimitry Andric 197bdd1243dSDimitry Andric namespace callable_detail { 198bdd1243dSDimitry Andric 199bdd1243dSDimitry Andric /// Templated storage wrapper for a callable. 200bdd1243dSDimitry Andric /// 201bdd1243dSDimitry Andric /// This class is consistently default constructible, copy / move 202bdd1243dSDimitry Andric /// constructible / assignable. 203bdd1243dSDimitry Andric /// 204bdd1243dSDimitry Andric /// Supported callable types: 205bdd1243dSDimitry Andric /// - Function pointer 206bdd1243dSDimitry Andric /// - Function reference 207bdd1243dSDimitry Andric /// - Lambda 208bdd1243dSDimitry Andric /// - Function object 209bdd1243dSDimitry Andric template <typename T, 210bdd1243dSDimitry Andric bool = std::is_function_v<std::remove_pointer_t<remove_cvref_t<T>>>> 211bdd1243dSDimitry Andric class Callable { 212bdd1243dSDimitry Andric using value_type = std::remove_reference_t<T>; 213bdd1243dSDimitry Andric using reference = value_type &; 214bdd1243dSDimitry Andric using const_reference = value_type const &; 215bdd1243dSDimitry Andric 216bdd1243dSDimitry Andric std::optional<value_type> Obj; 217bdd1243dSDimitry Andric 218bdd1243dSDimitry Andric static_assert(!std::is_pointer_v<value_type>, 219bdd1243dSDimitry Andric "Pointers to non-functions are not callable."); 220bdd1243dSDimitry Andric 221bdd1243dSDimitry Andric public: 222bdd1243dSDimitry Andric Callable() = default; 223bdd1243dSDimitry Andric Callable(T const &O) : Obj(std::in_place, O) {} 224bdd1243dSDimitry Andric 225bdd1243dSDimitry Andric Callable(Callable const &Other) = default; 226bdd1243dSDimitry Andric Callable(Callable &&Other) = default; 227bdd1243dSDimitry Andric 228bdd1243dSDimitry Andric Callable &operator=(Callable const &Other) { 229bdd1243dSDimitry Andric Obj = std::nullopt; 230bdd1243dSDimitry Andric if (Other.Obj) 231bdd1243dSDimitry Andric Obj.emplace(*Other.Obj); 232bdd1243dSDimitry Andric return *this; 233bdd1243dSDimitry Andric } 234bdd1243dSDimitry Andric 235bdd1243dSDimitry Andric Callable &operator=(Callable &&Other) { 236bdd1243dSDimitry Andric Obj = std::nullopt; 237bdd1243dSDimitry Andric if (Other.Obj) 238bdd1243dSDimitry Andric Obj.emplace(std::move(*Other.Obj)); 239bdd1243dSDimitry Andric return *this; 240bdd1243dSDimitry Andric } 241bdd1243dSDimitry Andric 242bdd1243dSDimitry Andric template <typename... Pn, 243bdd1243dSDimitry Andric std::enable_if_t<std::is_invocable_v<T, Pn...>, int> = 0> 244bdd1243dSDimitry Andric decltype(auto) operator()(Pn &&...Params) { 245bdd1243dSDimitry Andric return (*Obj)(std::forward<Pn>(Params)...); 246bdd1243dSDimitry Andric } 247bdd1243dSDimitry Andric 248bdd1243dSDimitry Andric template <typename... Pn, 249bdd1243dSDimitry Andric std::enable_if_t<std::is_invocable_v<T const, Pn...>, int> = 0> 250bdd1243dSDimitry Andric decltype(auto) operator()(Pn &&...Params) const { 251bdd1243dSDimitry Andric return (*Obj)(std::forward<Pn>(Params)...); 252bdd1243dSDimitry Andric } 253bdd1243dSDimitry Andric 254bdd1243dSDimitry Andric bool valid() const { return Obj != std::nullopt; } 255bdd1243dSDimitry Andric bool reset() { return Obj = std::nullopt; } 256bdd1243dSDimitry Andric 257bdd1243dSDimitry Andric operator reference() { return *Obj; } 258bdd1243dSDimitry Andric operator const_reference() const { return *Obj; } 259bdd1243dSDimitry Andric }; 260bdd1243dSDimitry Andric 261bdd1243dSDimitry Andric // Function specialization. No need to waste extra space wrapping with a 262bdd1243dSDimitry Andric // std::optional. 263bdd1243dSDimitry Andric template <typename T> class Callable<T, true> { 264bdd1243dSDimitry Andric static constexpr bool IsPtr = std::is_pointer_v<remove_cvref_t<T>>; 265bdd1243dSDimitry Andric 266bdd1243dSDimitry Andric using StorageT = std::conditional_t<IsPtr, T, std::remove_reference_t<T> *>; 267bdd1243dSDimitry Andric using CastT = std::conditional_t<IsPtr, T, T &>; 268bdd1243dSDimitry Andric 269bdd1243dSDimitry Andric private: 270bdd1243dSDimitry Andric StorageT Func = nullptr; 271bdd1243dSDimitry Andric 272bdd1243dSDimitry Andric private: 273bdd1243dSDimitry Andric template <typename In> static constexpr auto convertIn(In &&I) { 274bdd1243dSDimitry Andric if constexpr (IsPtr) { 275bdd1243dSDimitry Andric // Pointer... just echo it back. 276bdd1243dSDimitry Andric return I; 277bdd1243dSDimitry Andric } else { 278bdd1243dSDimitry Andric // Must be a function reference. Return its address. 279bdd1243dSDimitry Andric return &I; 280bdd1243dSDimitry Andric } 281bdd1243dSDimitry Andric } 282bdd1243dSDimitry Andric 283bdd1243dSDimitry Andric public: 284bdd1243dSDimitry Andric Callable() = default; 285bdd1243dSDimitry Andric 286bdd1243dSDimitry Andric // Construct from a function pointer or reference. 287bdd1243dSDimitry Andric // 288bdd1243dSDimitry Andric // Disable this constructor for references to 'Callable' so we don't violate 289bdd1243dSDimitry Andric // the rule of 0. 290bdd1243dSDimitry Andric template < // clang-format off 291bdd1243dSDimitry Andric typename FnPtrOrRef, 292bdd1243dSDimitry Andric std::enable_if_t< 293bdd1243dSDimitry Andric !std::is_same_v<remove_cvref_t<FnPtrOrRef>, Callable>, int 294bdd1243dSDimitry Andric > = 0 295bdd1243dSDimitry Andric > // clang-format on 296bdd1243dSDimitry Andric Callable(FnPtrOrRef &&F) : Func(convertIn(F)) {} 297bdd1243dSDimitry Andric 298bdd1243dSDimitry Andric template <typename... Pn, 299bdd1243dSDimitry Andric std::enable_if_t<std::is_invocable_v<T, Pn...>, int> = 0> 300bdd1243dSDimitry Andric decltype(auto) operator()(Pn &&...Params) const { 301bdd1243dSDimitry Andric return Func(std::forward<Pn>(Params)...); 302bdd1243dSDimitry Andric } 303bdd1243dSDimitry Andric 304bdd1243dSDimitry Andric bool valid() const { return Func != nullptr; } 305bdd1243dSDimitry Andric void reset() { Func = nullptr; } 306bdd1243dSDimitry Andric 307bdd1243dSDimitry Andric operator T const &() const { 308bdd1243dSDimitry Andric if constexpr (IsPtr) { 309bdd1243dSDimitry Andric // T is a pointer... just echo it back. 310bdd1243dSDimitry Andric return Func; 311bdd1243dSDimitry Andric } else { 312bdd1243dSDimitry Andric static_assert(std::is_reference_v<T>, 313bdd1243dSDimitry Andric "Expected a reference to a function."); 314bdd1243dSDimitry Andric // T is a function reference... dereference the stored pointer. 315bdd1243dSDimitry Andric return *Func; 316bdd1243dSDimitry Andric } 317bdd1243dSDimitry Andric } 318bdd1243dSDimitry Andric }; 319bdd1243dSDimitry Andric 320bdd1243dSDimitry Andric } // namespace callable_detail 321bdd1243dSDimitry Andric 3225ffd83dbSDimitry Andric /// Returns true if the given container only contains a single element. 3235ffd83dbSDimitry Andric template <typename ContainerTy> bool hasSingleElement(ContainerTy &&C) { 3245ffd83dbSDimitry Andric auto B = std::begin(C), E = std::end(C); 3255ffd83dbSDimitry Andric return B != E && std::next(B) == E; 3265ffd83dbSDimitry Andric } 3275ffd83dbSDimitry Andric 328480093f4SDimitry Andric /// Return a range covering \p RangeOrContainer with the first N elements 329480093f4SDimitry Andric /// excluded. 330e8d8bef9SDimitry Andric template <typename T> auto drop_begin(T &&RangeOrContainer, size_t N = 1) { 331480093f4SDimitry Andric return make_range(std::next(adl_begin(RangeOrContainer), N), 332480093f4SDimitry Andric adl_end(RangeOrContainer)); 333480093f4SDimitry Andric } 334480093f4SDimitry Andric 33581ad6265SDimitry Andric /// Return a range covering \p RangeOrContainer with the last N elements 33681ad6265SDimitry Andric /// excluded. 33781ad6265SDimitry Andric template <typename T> auto drop_end(T &&RangeOrContainer, size_t N = 1) { 33881ad6265SDimitry Andric return make_range(adl_begin(RangeOrContainer), 33981ad6265SDimitry Andric std::prev(adl_end(RangeOrContainer), N)); 34081ad6265SDimitry Andric } 34181ad6265SDimitry Andric 3420b57cec5SDimitry Andric // mapped_iterator - This is a simple iterator adapter that causes a function to 3430b57cec5SDimitry Andric // be applied whenever operator* is invoked on the iterator. 3440b57cec5SDimitry Andric 3450b57cec5SDimitry Andric template <typename ItTy, typename FuncTy, 346349cc55cSDimitry Andric typename ReferenceTy = 3470b57cec5SDimitry Andric decltype(std::declval<FuncTy>()(*std::declval<ItTy>()))> 3480b57cec5SDimitry Andric class mapped_iterator 3490b57cec5SDimitry Andric : public iterator_adaptor_base< 3500b57cec5SDimitry Andric mapped_iterator<ItTy, FuncTy>, ItTy, 3510b57cec5SDimitry Andric typename std::iterator_traits<ItTy>::iterator_category, 352349cc55cSDimitry Andric std::remove_reference_t<ReferenceTy>, 353349cc55cSDimitry Andric typename std::iterator_traits<ItTy>::difference_type, 354349cc55cSDimitry Andric std::remove_reference_t<ReferenceTy> *, ReferenceTy> { 3550b57cec5SDimitry Andric public: 356bdd1243dSDimitry Andric mapped_iterator() = default; 3570b57cec5SDimitry Andric mapped_iterator(ItTy U, FuncTy F) 3580b57cec5SDimitry Andric : mapped_iterator::iterator_adaptor_base(std::move(U)), F(std::move(F)) {} 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric ItTy getCurrent() { return this->I; } 3610b57cec5SDimitry Andric 362349cc55cSDimitry Andric const FuncTy &getFunction() const { return F; } 363349cc55cSDimitry Andric 364349cc55cSDimitry Andric ReferenceTy operator*() const { return F(*this->I); } 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric private: 367bdd1243dSDimitry Andric callable_detail::Callable<FuncTy> F{}; 3680b57cec5SDimitry Andric }; 3690b57cec5SDimitry Andric 3700b57cec5SDimitry Andric // map_iterator - Provide a convenient way to create mapped_iterators, just like 3710b57cec5SDimitry Andric // make_pair is useful for creating pairs... 3720b57cec5SDimitry Andric template <class ItTy, class FuncTy> 3730b57cec5SDimitry Andric inline mapped_iterator<ItTy, FuncTy> map_iterator(ItTy I, FuncTy F) { 3740b57cec5SDimitry Andric return mapped_iterator<ItTy, FuncTy>(std::move(I), std::move(F)); 3750b57cec5SDimitry Andric } 3760b57cec5SDimitry Andric 3770b57cec5SDimitry Andric template <class ContainerTy, class FuncTy> 3785ffd83dbSDimitry Andric auto map_range(ContainerTy &&C, FuncTy F) { 379*06c3fb27SDimitry Andric return make_range(map_iterator(std::begin(C), F), 380*06c3fb27SDimitry Andric map_iterator(std::end(C), F)); 3810b57cec5SDimitry Andric } 3820b57cec5SDimitry Andric 383349cc55cSDimitry Andric /// A base type of mapped iterator, that is useful for building derived 384349cc55cSDimitry Andric /// iterators that do not need/want to store the map function (as in 385349cc55cSDimitry Andric /// mapped_iterator). These iterators must simply provide a `mapElement` method 386349cc55cSDimitry Andric /// that defines how to map a value of the iterator to the provided reference 387349cc55cSDimitry Andric /// type. 388349cc55cSDimitry Andric template <typename DerivedT, typename ItTy, typename ReferenceTy> 389349cc55cSDimitry Andric class mapped_iterator_base 390349cc55cSDimitry Andric : public iterator_adaptor_base< 391349cc55cSDimitry Andric DerivedT, ItTy, 392349cc55cSDimitry Andric typename std::iterator_traits<ItTy>::iterator_category, 393349cc55cSDimitry Andric std::remove_reference_t<ReferenceTy>, 394349cc55cSDimitry Andric typename std::iterator_traits<ItTy>::difference_type, 395349cc55cSDimitry Andric std::remove_reference_t<ReferenceTy> *, ReferenceTy> { 396349cc55cSDimitry Andric public: 397349cc55cSDimitry Andric using BaseT = mapped_iterator_base; 398349cc55cSDimitry Andric 399349cc55cSDimitry Andric mapped_iterator_base(ItTy U) 400349cc55cSDimitry Andric : mapped_iterator_base::iterator_adaptor_base(std::move(U)) {} 401349cc55cSDimitry Andric 402349cc55cSDimitry Andric ItTy getCurrent() { return this->I; } 403349cc55cSDimitry Andric 404349cc55cSDimitry Andric ReferenceTy operator*() const { 405349cc55cSDimitry Andric return static_cast<const DerivedT &>(*this).mapElement(*this->I); 406349cc55cSDimitry Andric } 407349cc55cSDimitry Andric }; 408349cc55cSDimitry Andric 4090b57cec5SDimitry Andric /// Helper to determine if type T has a member called rbegin(). 4100b57cec5SDimitry Andric template <typename Ty> class has_rbegin_impl { 4110b57cec5SDimitry Andric using yes = char[1]; 4120b57cec5SDimitry Andric using no = char[2]; 4130b57cec5SDimitry Andric 4140b57cec5SDimitry Andric template <typename Inner> 4150b57cec5SDimitry Andric static yes& test(Inner *I, decltype(I->rbegin()) * = nullptr); 4160b57cec5SDimitry Andric 4170b57cec5SDimitry Andric template <typename> 4180b57cec5SDimitry Andric static no& test(...); 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric public: 4210b57cec5SDimitry Andric static const bool value = sizeof(test<Ty>(nullptr)) == sizeof(yes); 4220b57cec5SDimitry Andric }; 4230b57cec5SDimitry Andric 4240b57cec5SDimitry Andric /// Metafunction to determine if T& or T has a member called rbegin(). 4250b57cec5SDimitry Andric template <typename Ty> 426bdd1243dSDimitry Andric struct has_rbegin : has_rbegin_impl<std::remove_reference_t<Ty>> {}; 4270b57cec5SDimitry Andric 4280b57cec5SDimitry Andric // Returns an iterator_range over the given container which iterates in reverse. 429bdd1243dSDimitry Andric template <typename ContainerTy> auto reverse(ContainerTy &&C) { 430bdd1243dSDimitry Andric if constexpr (has_rbegin<ContainerTy>::value) 4310b57cec5SDimitry Andric return make_range(C.rbegin(), C.rend()); 432bdd1243dSDimitry Andric else 43304eeddc0SDimitry Andric return make_range(std::make_reverse_iterator(std::end(C)), 43404eeddc0SDimitry Andric std::make_reverse_iterator(std::begin(C))); 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric /// An iterator adaptor that filters the elements of given inner iterators. 4380b57cec5SDimitry Andric /// 4390b57cec5SDimitry Andric /// The predicate parameter should be a callable object that accepts the wrapped 4400b57cec5SDimitry Andric /// iterator's reference type and returns a bool. When incrementing or 4410b57cec5SDimitry Andric /// decrementing the iterator, it will call the predicate on each element and 4420b57cec5SDimitry Andric /// skip any where it returns false. 4430b57cec5SDimitry Andric /// 4440b57cec5SDimitry Andric /// \code 4450b57cec5SDimitry Andric /// int A[] = { 1, 2, 3, 4 }; 4460b57cec5SDimitry Andric /// auto R = make_filter_range(A, [](int N) { return N % 2 == 1; }); 4470b57cec5SDimitry Andric /// // R contains { 1, 3 }. 4480b57cec5SDimitry Andric /// \endcode 4490b57cec5SDimitry Andric /// 4500b57cec5SDimitry Andric /// Note: filter_iterator_base implements support for forward iteration. 4510b57cec5SDimitry Andric /// filter_iterator_impl exists to provide support for bidirectional iteration, 4520b57cec5SDimitry Andric /// conditional on whether the wrapped iterator supports it. 4530b57cec5SDimitry Andric template <typename WrappedIteratorT, typename PredicateT, typename IterTag> 4540b57cec5SDimitry Andric class filter_iterator_base 4550b57cec5SDimitry Andric : public iterator_adaptor_base< 4560b57cec5SDimitry Andric filter_iterator_base<WrappedIteratorT, PredicateT, IterTag>, 4570b57cec5SDimitry Andric WrappedIteratorT, 458bdd1243dSDimitry Andric std::common_type_t<IterTag, 459bdd1243dSDimitry Andric typename std::iterator_traits< 460bdd1243dSDimitry Andric WrappedIteratorT>::iterator_category>> { 461349cc55cSDimitry Andric using BaseT = typename filter_iterator_base::iterator_adaptor_base; 4620b57cec5SDimitry Andric 4630b57cec5SDimitry Andric protected: 4640b57cec5SDimitry Andric WrappedIteratorT End; 4650b57cec5SDimitry Andric PredicateT Pred; 4660b57cec5SDimitry Andric 4670b57cec5SDimitry Andric void findNextValid() { 4680b57cec5SDimitry Andric while (this->I != End && !Pred(*this->I)) 4690b57cec5SDimitry Andric BaseT::operator++(); 4700b57cec5SDimitry Andric } 4710b57cec5SDimitry Andric 472bdd1243dSDimitry Andric filter_iterator_base() = default; 473bdd1243dSDimitry Andric 4740b57cec5SDimitry Andric // Construct the iterator. The begin iterator needs to know where the end 4750b57cec5SDimitry Andric // is, so that it can properly stop when it gets there. The end iterator only 4760b57cec5SDimitry Andric // needs the predicate to support bidirectional iteration. 4770b57cec5SDimitry Andric filter_iterator_base(WrappedIteratorT Begin, WrappedIteratorT End, 4780b57cec5SDimitry Andric PredicateT Pred) 4790b57cec5SDimitry Andric : BaseT(Begin), End(End), Pred(Pred) { 4800b57cec5SDimitry Andric findNextValid(); 4810b57cec5SDimitry Andric } 4820b57cec5SDimitry Andric 4830b57cec5SDimitry Andric public: 4840b57cec5SDimitry Andric using BaseT::operator++; 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric filter_iterator_base &operator++() { 4870b57cec5SDimitry Andric BaseT::operator++(); 4880b57cec5SDimitry Andric findNextValid(); 4890b57cec5SDimitry Andric return *this; 4900b57cec5SDimitry Andric } 49181ad6265SDimitry Andric 49281ad6265SDimitry Andric decltype(auto) operator*() const { 49381ad6265SDimitry Andric assert(BaseT::wrapped() != End && "Cannot dereference end iterator!"); 49481ad6265SDimitry Andric return BaseT::operator*(); 49581ad6265SDimitry Andric } 49681ad6265SDimitry Andric 49781ad6265SDimitry Andric decltype(auto) operator->() const { 49881ad6265SDimitry Andric assert(BaseT::wrapped() != End && "Cannot dereference end iterator!"); 49981ad6265SDimitry Andric return BaseT::operator->(); 50081ad6265SDimitry Andric } 5010b57cec5SDimitry Andric }; 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric /// Specialization of filter_iterator_base for forward iteration only. 5040b57cec5SDimitry Andric template <typename WrappedIteratorT, typename PredicateT, 5050b57cec5SDimitry Andric typename IterTag = std::forward_iterator_tag> 5060b57cec5SDimitry Andric class filter_iterator_impl 5070b57cec5SDimitry Andric : public filter_iterator_base<WrappedIteratorT, PredicateT, IterTag> { 5080b57cec5SDimitry Andric public: 509bdd1243dSDimitry Andric filter_iterator_impl() = default; 510bdd1243dSDimitry Andric 5110b57cec5SDimitry Andric filter_iterator_impl(WrappedIteratorT Begin, WrappedIteratorT End, 5120b57cec5SDimitry Andric PredicateT Pred) 513349cc55cSDimitry Andric : filter_iterator_impl::filter_iterator_base(Begin, End, Pred) {} 5140b57cec5SDimitry Andric }; 5150b57cec5SDimitry Andric 5160b57cec5SDimitry Andric /// Specialization of filter_iterator_base for bidirectional iteration. 5170b57cec5SDimitry Andric template <typename WrappedIteratorT, typename PredicateT> 5180b57cec5SDimitry Andric class filter_iterator_impl<WrappedIteratorT, PredicateT, 5190b57cec5SDimitry Andric std::bidirectional_iterator_tag> 5200b57cec5SDimitry Andric : public filter_iterator_base<WrappedIteratorT, PredicateT, 5210b57cec5SDimitry Andric std::bidirectional_iterator_tag> { 522349cc55cSDimitry Andric using BaseT = typename filter_iterator_impl::filter_iterator_base; 523349cc55cSDimitry Andric 5240b57cec5SDimitry Andric void findPrevValid() { 5250b57cec5SDimitry Andric while (!this->Pred(*this->I)) 5260b57cec5SDimitry Andric BaseT::operator--(); 5270b57cec5SDimitry Andric } 5280b57cec5SDimitry Andric 5290b57cec5SDimitry Andric public: 5300b57cec5SDimitry Andric using BaseT::operator--; 5310b57cec5SDimitry Andric 532bdd1243dSDimitry Andric filter_iterator_impl() = default; 533bdd1243dSDimitry Andric 5340b57cec5SDimitry Andric filter_iterator_impl(WrappedIteratorT Begin, WrappedIteratorT End, 5350b57cec5SDimitry Andric PredicateT Pred) 5360b57cec5SDimitry Andric : BaseT(Begin, End, Pred) {} 5370b57cec5SDimitry Andric 5380b57cec5SDimitry Andric filter_iterator_impl &operator--() { 5390b57cec5SDimitry Andric BaseT::operator--(); 5400b57cec5SDimitry Andric findPrevValid(); 5410b57cec5SDimitry Andric return *this; 5420b57cec5SDimitry Andric } 5430b57cec5SDimitry Andric }; 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric namespace detail { 5460b57cec5SDimitry Andric 5470b57cec5SDimitry Andric template <bool is_bidirectional> struct fwd_or_bidi_tag_impl { 5480b57cec5SDimitry Andric using type = std::forward_iterator_tag; 5490b57cec5SDimitry Andric }; 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andric template <> struct fwd_or_bidi_tag_impl<true> { 5520b57cec5SDimitry Andric using type = std::bidirectional_iterator_tag; 5530b57cec5SDimitry Andric }; 5540b57cec5SDimitry Andric 5550b57cec5SDimitry Andric /// Helper which sets its type member to forward_iterator_tag if the category 5560b57cec5SDimitry Andric /// of \p IterT does not derive from bidirectional_iterator_tag, and to 5570b57cec5SDimitry Andric /// bidirectional_iterator_tag otherwise. 5580b57cec5SDimitry Andric template <typename IterT> struct fwd_or_bidi_tag { 5590b57cec5SDimitry Andric using type = typename fwd_or_bidi_tag_impl<std::is_base_of< 5600b57cec5SDimitry Andric std::bidirectional_iterator_tag, 5610b57cec5SDimitry Andric typename std::iterator_traits<IterT>::iterator_category>::value>::type; 5620b57cec5SDimitry Andric }; 5630b57cec5SDimitry Andric 5640b57cec5SDimitry Andric } // namespace detail 5650b57cec5SDimitry Andric 5660b57cec5SDimitry Andric /// Defines filter_iterator to a suitable specialization of 5670b57cec5SDimitry Andric /// filter_iterator_impl, based on the underlying iterator's category. 5680b57cec5SDimitry Andric template <typename WrappedIteratorT, typename PredicateT> 5690b57cec5SDimitry Andric using filter_iterator = filter_iterator_impl< 5700b57cec5SDimitry Andric WrappedIteratorT, PredicateT, 5710b57cec5SDimitry Andric typename detail::fwd_or_bidi_tag<WrappedIteratorT>::type>; 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric /// Convenience function that takes a range of elements and a predicate, 5740b57cec5SDimitry Andric /// and return a new filter_iterator range. 5750b57cec5SDimitry Andric /// 5760b57cec5SDimitry Andric /// FIXME: Currently if RangeT && is a rvalue reference to a temporary, the 5770b57cec5SDimitry Andric /// lifetime of that temporary is not kept by the returned range object, and the 5780b57cec5SDimitry Andric /// temporary is going to be dropped on the floor after the make_iterator_range 5790b57cec5SDimitry Andric /// full expression that contains this function call. 5800b57cec5SDimitry Andric template <typename RangeT, typename PredicateT> 5810b57cec5SDimitry Andric iterator_range<filter_iterator<detail::IterOfRange<RangeT>, PredicateT>> 5820b57cec5SDimitry Andric make_filter_range(RangeT &&Range, PredicateT Pred) { 5830b57cec5SDimitry Andric using FilterIteratorT = 5840b57cec5SDimitry Andric filter_iterator<detail::IterOfRange<RangeT>, PredicateT>; 5850b57cec5SDimitry Andric return make_range( 5860b57cec5SDimitry Andric FilterIteratorT(std::begin(std::forward<RangeT>(Range)), 5870b57cec5SDimitry Andric std::end(std::forward<RangeT>(Range)), Pred), 5880b57cec5SDimitry Andric FilterIteratorT(std::end(std::forward<RangeT>(Range)), 5890b57cec5SDimitry Andric std::end(std::forward<RangeT>(Range)), Pred)); 5900b57cec5SDimitry Andric } 5910b57cec5SDimitry Andric 5920b57cec5SDimitry Andric /// A pseudo-iterator adaptor that is designed to implement "early increment" 5930b57cec5SDimitry Andric /// style loops. 5940b57cec5SDimitry Andric /// 5950b57cec5SDimitry Andric /// This is *not a normal iterator* and should almost never be used directly. It 5960b57cec5SDimitry Andric /// is intended primarily to be used with range based for loops and some range 5970b57cec5SDimitry Andric /// algorithms. 5980b57cec5SDimitry Andric /// 5990b57cec5SDimitry Andric /// The iterator isn't quite an `OutputIterator` or an `InputIterator` but 6000b57cec5SDimitry Andric /// somewhere between them. The constraints of these iterators are: 6010b57cec5SDimitry Andric /// 6020b57cec5SDimitry Andric /// - On construction or after being incremented, it is comparable and 6030b57cec5SDimitry Andric /// dereferencable. It is *not* incrementable. 6040b57cec5SDimitry Andric /// - After being dereferenced, it is neither comparable nor dereferencable, it 6050b57cec5SDimitry Andric /// is only incrementable. 6060b57cec5SDimitry Andric /// 6070b57cec5SDimitry Andric /// This means you can only dereference the iterator once, and you can only 6080b57cec5SDimitry Andric /// increment it once between dereferences. 6090b57cec5SDimitry Andric template <typename WrappedIteratorT> 6100b57cec5SDimitry Andric class early_inc_iterator_impl 6110b57cec5SDimitry Andric : public iterator_adaptor_base<early_inc_iterator_impl<WrappedIteratorT>, 6120b57cec5SDimitry Andric WrappedIteratorT, std::input_iterator_tag> { 613349cc55cSDimitry Andric using BaseT = typename early_inc_iterator_impl::iterator_adaptor_base; 6140b57cec5SDimitry Andric 6150b57cec5SDimitry Andric using PointerT = typename std::iterator_traits<WrappedIteratorT>::pointer; 6160b57cec5SDimitry Andric 6170b57cec5SDimitry Andric protected: 6180b57cec5SDimitry Andric #if LLVM_ENABLE_ABI_BREAKING_CHECKS 6190b57cec5SDimitry Andric bool IsEarlyIncremented = false; 6200b57cec5SDimitry Andric #endif 6210b57cec5SDimitry Andric 6220b57cec5SDimitry Andric public: 6230b57cec5SDimitry Andric early_inc_iterator_impl(WrappedIteratorT I) : BaseT(I) {} 6240b57cec5SDimitry Andric 6250b57cec5SDimitry Andric using BaseT::operator*; 626e8d8bef9SDimitry Andric decltype(*std::declval<WrappedIteratorT>()) operator*() { 6270b57cec5SDimitry Andric #if LLVM_ENABLE_ABI_BREAKING_CHECKS 6280b57cec5SDimitry Andric assert(!IsEarlyIncremented && "Cannot dereference twice!"); 6290b57cec5SDimitry Andric IsEarlyIncremented = true; 6300b57cec5SDimitry Andric #endif 6310b57cec5SDimitry Andric return *(this->I)++; 6320b57cec5SDimitry Andric } 6330b57cec5SDimitry Andric 6340b57cec5SDimitry Andric using BaseT::operator++; 6350b57cec5SDimitry Andric early_inc_iterator_impl &operator++() { 6360b57cec5SDimitry Andric #if LLVM_ENABLE_ABI_BREAKING_CHECKS 6370b57cec5SDimitry Andric assert(IsEarlyIncremented && "Cannot increment before dereferencing!"); 6380b57cec5SDimitry Andric IsEarlyIncremented = false; 6390b57cec5SDimitry Andric #endif 6400b57cec5SDimitry Andric return *this; 6410b57cec5SDimitry Andric } 6420b57cec5SDimitry Andric 643e8d8bef9SDimitry Andric friend bool operator==(const early_inc_iterator_impl &LHS, 644e8d8bef9SDimitry Andric const early_inc_iterator_impl &RHS) { 6450b57cec5SDimitry Andric #if LLVM_ENABLE_ABI_BREAKING_CHECKS 646e8d8bef9SDimitry Andric assert(!LHS.IsEarlyIncremented && "Cannot compare after dereferencing!"); 6470b57cec5SDimitry Andric #endif 648e8d8bef9SDimitry Andric return (const BaseT &)LHS == (const BaseT &)RHS; 6490b57cec5SDimitry Andric } 6500b57cec5SDimitry Andric }; 6510b57cec5SDimitry Andric 6520b57cec5SDimitry Andric /// Make a range that does early increment to allow mutation of the underlying 6530b57cec5SDimitry Andric /// range without disrupting iteration. 6540b57cec5SDimitry Andric /// 6550b57cec5SDimitry Andric /// The underlying iterator will be incremented immediately after it is 6560b57cec5SDimitry Andric /// dereferenced, allowing deletion of the current node or insertion of nodes to 6570b57cec5SDimitry Andric /// not disrupt iteration provided they do not invalidate the *next* iterator -- 6580b57cec5SDimitry Andric /// the current iterator can be invalidated. 6590b57cec5SDimitry Andric /// 6600b57cec5SDimitry Andric /// This requires a very exact pattern of use that is only really suitable to 6610b57cec5SDimitry Andric /// range based for loops and other range algorithms that explicitly guarantee 6620b57cec5SDimitry Andric /// to dereference exactly once each element, and to increment exactly once each 6630b57cec5SDimitry Andric /// element. 6640b57cec5SDimitry Andric template <typename RangeT> 6650b57cec5SDimitry Andric iterator_range<early_inc_iterator_impl<detail::IterOfRange<RangeT>>> 6660b57cec5SDimitry Andric make_early_inc_range(RangeT &&Range) { 6670b57cec5SDimitry Andric using EarlyIncIteratorT = 6680b57cec5SDimitry Andric early_inc_iterator_impl<detail::IterOfRange<RangeT>>; 6690b57cec5SDimitry Andric return make_range(EarlyIncIteratorT(std::begin(std::forward<RangeT>(Range))), 6700b57cec5SDimitry Andric EarlyIncIteratorT(std::end(std::forward<RangeT>(Range)))); 6710b57cec5SDimitry Andric } 6720b57cec5SDimitry Andric 673bdd1243dSDimitry Andric // Forward declarations required by zip_shortest/zip_equal/zip_first/zip_longest 6740b57cec5SDimitry Andric template <typename R, typename UnaryPredicate> 6750b57cec5SDimitry Andric bool all_of(R &&range, UnaryPredicate P); 676bdd1243dSDimitry Andric 6770b57cec5SDimitry Andric template <typename R, typename UnaryPredicate> 6780b57cec5SDimitry Andric bool any_of(R &&range, UnaryPredicate P); 6790b57cec5SDimitry Andric 680bdd1243dSDimitry Andric template <typename T> bool all_equal(std::initializer_list<T> Values); 681bdd1243dSDimitry Andric 682*06c3fb27SDimitry Andric template <typename R> constexpr size_t range_size(R &&Range); 683*06c3fb27SDimitry Andric 6840b57cec5SDimitry Andric namespace detail { 6850b57cec5SDimitry Andric 6860b57cec5SDimitry Andric using std::declval; 6870b57cec5SDimitry Andric 6880b57cec5SDimitry Andric // We have to alias this since inlining the actual type at the usage site 6890b57cec5SDimitry Andric // in the parameter list of iterator_facade_base<> below ICEs MSVC 2017. 6900b57cec5SDimitry Andric template<typename... Iters> struct ZipTupleType { 6910b57cec5SDimitry Andric using type = std::tuple<decltype(*declval<Iters>())...>; 6920b57cec5SDimitry Andric }; 6930b57cec5SDimitry Andric 694*06c3fb27SDimitry Andric template <typename ZipType, typename ReferenceTupleType, typename... Iters> 6950b57cec5SDimitry Andric using zip_traits = iterator_facade_base< 696bdd1243dSDimitry Andric ZipType, 697bdd1243dSDimitry Andric std::common_type_t< 698bdd1243dSDimitry Andric std::bidirectional_iterator_tag, 699bdd1243dSDimitry Andric typename std::iterator_traits<Iters>::iterator_category...>, 7000b57cec5SDimitry Andric // ^ TODO: Implement random access methods. 701*06c3fb27SDimitry Andric ReferenceTupleType, 702bdd1243dSDimitry Andric typename std::iterator_traits< 703bdd1243dSDimitry Andric std::tuple_element_t<0, std::tuple<Iters...>>>::difference_type, 7040b57cec5SDimitry Andric // ^ FIXME: This follows boost::make_zip_iterator's assumption that all 7050b57cec5SDimitry Andric // inner iterators have the same difference_type. It would fail if, for 7060b57cec5SDimitry Andric // instance, the second field's difference_type were non-numeric while the 7070b57cec5SDimitry Andric // first is. 708*06c3fb27SDimitry Andric ReferenceTupleType *, ReferenceTupleType>; 7090b57cec5SDimitry Andric 710*06c3fb27SDimitry Andric template <typename ZipType, typename ReferenceTupleType, typename... Iters> 711*06c3fb27SDimitry Andric struct zip_common : public zip_traits<ZipType, ReferenceTupleType, Iters...> { 712*06c3fb27SDimitry Andric using Base = zip_traits<ZipType, ReferenceTupleType, Iters...>; 713*06c3fb27SDimitry Andric using IndexSequence = std::index_sequence_for<Iters...>; 7140b57cec5SDimitry Andric using value_type = typename Base::value_type; 7150b57cec5SDimitry Andric 7160b57cec5SDimitry Andric std::tuple<Iters...> iterators; 7170b57cec5SDimitry Andric 7180b57cec5SDimitry Andric protected: 7198bcb0991SDimitry Andric template <size_t... Ns> value_type deref(std::index_sequence<Ns...>) const { 7200b57cec5SDimitry Andric return value_type(*std::get<Ns>(iterators)...); 7210b57cec5SDimitry Andric } 7220b57cec5SDimitry Andric 723*06c3fb27SDimitry Andric template <size_t... Ns> void tup_inc(std::index_sequence<Ns...>) { 724*06c3fb27SDimitry Andric (++std::get<Ns>(iterators), ...); 7250b57cec5SDimitry Andric } 7260b57cec5SDimitry Andric 727*06c3fb27SDimitry Andric template <size_t... Ns> void tup_dec(std::index_sequence<Ns...>) { 728*06c3fb27SDimitry Andric (--std::get<Ns>(iterators), ...); 7290b57cec5SDimitry Andric } 7300b57cec5SDimitry Andric 731349cc55cSDimitry Andric template <size_t... Ns> 732349cc55cSDimitry Andric bool test_all_equals(const zip_common &other, 733349cc55cSDimitry Andric std::index_sequence<Ns...>) const { 734bdd1243dSDimitry Andric return ((std::get<Ns>(this->iterators) == std::get<Ns>(other.iterators)) && 735bdd1243dSDimitry Andric ...); 736349cc55cSDimitry Andric } 737349cc55cSDimitry Andric 7380b57cec5SDimitry Andric public: 7390b57cec5SDimitry Andric zip_common(Iters &&... ts) : iterators(std::forward<Iters>(ts)...) {} 7400b57cec5SDimitry Andric 741*06c3fb27SDimitry Andric value_type operator*() const { return deref(IndexSequence{}); } 7420b57cec5SDimitry Andric 7430b57cec5SDimitry Andric ZipType &operator++() { 744*06c3fb27SDimitry Andric tup_inc(IndexSequence{}); 745*06c3fb27SDimitry Andric return static_cast<ZipType &>(*this); 7460b57cec5SDimitry Andric } 7470b57cec5SDimitry Andric 7480b57cec5SDimitry Andric ZipType &operator--() { 7490b57cec5SDimitry Andric static_assert(Base::IsBidirectional, 7500b57cec5SDimitry Andric "All inner iterators must be at least bidirectional."); 751*06c3fb27SDimitry Andric tup_dec(IndexSequence{}); 752*06c3fb27SDimitry Andric return static_cast<ZipType &>(*this); 7530b57cec5SDimitry Andric } 754349cc55cSDimitry Andric 755349cc55cSDimitry Andric /// Return true if all the iterator are matching `other`'s iterators. 756349cc55cSDimitry Andric bool all_equals(zip_common &other) { 757*06c3fb27SDimitry Andric return test_all_equals(other, IndexSequence{}); 758349cc55cSDimitry Andric } 7590b57cec5SDimitry Andric }; 7600b57cec5SDimitry Andric 7610b57cec5SDimitry Andric template <typename... Iters> 762*06c3fb27SDimitry Andric struct zip_first : zip_common<zip_first<Iters...>, 763*06c3fb27SDimitry Andric typename ZipTupleType<Iters...>::type, Iters...> { 764*06c3fb27SDimitry Andric using zip_common<zip_first, typename ZipTupleType<Iters...>::type, 765*06c3fb27SDimitry Andric Iters...>::zip_common; 7660b57cec5SDimitry Andric 767*06c3fb27SDimitry Andric bool operator==(const zip_first &other) const { 7680b57cec5SDimitry Andric return std::get<0>(this->iterators) == std::get<0>(other.iterators); 7690b57cec5SDimitry Andric } 7700b57cec5SDimitry Andric }; 7710b57cec5SDimitry Andric 7720b57cec5SDimitry Andric template <typename... Iters> 773*06c3fb27SDimitry Andric struct zip_shortest 774*06c3fb27SDimitry Andric : zip_common<zip_shortest<Iters...>, typename ZipTupleType<Iters...>::type, 775*06c3fb27SDimitry Andric Iters...> { 776*06c3fb27SDimitry Andric using zip_common<zip_shortest, typename ZipTupleType<Iters...>::type, 777*06c3fb27SDimitry Andric Iters...>::zip_common; 778*06c3fb27SDimitry Andric 779*06c3fb27SDimitry Andric bool operator==(const zip_shortest &other) const { 780*06c3fb27SDimitry Andric return any_iterator_equals(other, std::index_sequence_for<Iters...>{}); 781*06c3fb27SDimitry Andric } 782*06c3fb27SDimitry Andric 783*06c3fb27SDimitry Andric private: 7840b57cec5SDimitry Andric template <size_t... Ns> 785*06c3fb27SDimitry Andric bool any_iterator_equals(const zip_shortest &other, 7868bcb0991SDimitry Andric std::index_sequence<Ns...>) const { 787*06c3fb27SDimitry Andric return ((std::get<Ns>(this->iterators) == std::get<Ns>(other.iterators)) || 788bdd1243dSDimitry Andric ...); 7890b57cec5SDimitry Andric } 790*06c3fb27SDimitry Andric }; 7910b57cec5SDimitry Andric 792*06c3fb27SDimitry Andric /// Helper to obtain the iterator types for the tuple storage within `zippy`. 793*06c3fb27SDimitry Andric template <template <typename...> class ItType, typename TupleStorageType, 794*06c3fb27SDimitry Andric typename IndexSequence> 795*06c3fb27SDimitry Andric struct ZippyIteratorTuple; 7960b57cec5SDimitry Andric 797*06c3fb27SDimitry Andric /// Partial specialization for non-const tuple storage. 798*06c3fb27SDimitry Andric template <template <typename...> class ItType, typename... Args, 799*06c3fb27SDimitry Andric std::size_t... Ns> 800*06c3fb27SDimitry Andric struct ZippyIteratorTuple<ItType, std::tuple<Args...>, 801*06c3fb27SDimitry Andric std::index_sequence<Ns...>> { 802*06c3fb27SDimitry Andric using type = ItType<decltype(adl_begin( 803*06c3fb27SDimitry Andric std::get<Ns>(declval<std::tuple<Args...> &>())))...>; 804*06c3fb27SDimitry Andric }; 8050b57cec5SDimitry Andric 806*06c3fb27SDimitry Andric /// Partial specialization for const tuple storage. 807*06c3fb27SDimitry Andric template <template <typename...> class ItType, typename... Args, 808*06c3fb27SDimitry Andric std::size_t... Ns> 809*06c3fb27SDimitry Andric struct ZippyIteratorTuple<ItType, const std::tuple<Args...>, 810*06c3fb27SDimitry Andric std::index_sequence<Ns...>> { 811*06c3fb27SDimitry Andric using type = ItType<decltype(adl_begin( 812*06c3fb27SDimitry Andric std::get<Ns>(declval<const std::tuple<Args...> &>())))...>; 8130b57cec5SDimitry Andric }; 8140b57cec5SDimitry Andric 8150b57cec5SDimitry Andric template <template <typename...> class ItType, typename... Args> class zippy { 816*06c3fb27SDimitry Andric private: 817*06c3fb27SDimitry Andric std::tuple<Args...> storage; 818*06c3fb27SDimitry Andric using IndexSequence = std::index_sequence_for<Args...>; 819*06c3fb27SDimitry Andric 8200b57cec5SDimitry Andric public: 821*06c3fb27SDimitry Andric using iterator = typename ZippyIteratorTuple<ItType, decltype(storage), 822*06c3fb27SDimitry Andric IndexSequence>::type; 823*06c3fb27SDimitry Andric using const_iterator = 824*06c3fb27SDimitry Andric typename ZippyIteratorTuple<ItType, const decltype(storage), 825*06c3fb27SDimitry Andric IndexSequence>::type; 8260b57cec5SDimitry Andric using iterator_category = typename iterator::iterator_category; 8270b57cec5SDimitry Andric using value_type = typename iterator::value_type; 8280b57cec5SDimitry Andric using difference_type = typename iterator::difference_type; 8290b57cec5SDimitry Andric using pointer = typename iterator::pointer; 8300b57cec5SDimitry Andric using reference = typename iterator::reference; 831*06c3fb27SDimitry Andric using const_reference = typename const_iterator::reference; 832*06c3fb27SDimitry Andric 833*06c3fb27SDimitry Andric zippy(Args &&...args) : storage(std::forward<Args>(args)...) {} 834*06c3fb27SDimitry Andric 835*06c3fb27SDimitry Andric const_iterator begin() const { return begin_impl(IndexSequence{}); } 836*06c3fb27SDimitry Andric iterator begin() { return begin_impl(IndexSequence{}); } 837*06c3fb27SDimitry Andric const_iterator end() const { return end_impl(IndexSequence{}); } 838*06c3fb27SDimitry Andric iterator end() { return end_impl(IndexSequence{}); } 8390b57cec5SDimitry Andric 8400b57cec5SDimitry Andric private: 841*06c3fb27SDimitry Andric template <size_t... Ns> 842*06c3fb27SDimitry Andric const_iterator begin_impl(std::index_sequence<Ns...>) const { 843*06c3fb27SDimitry Andric return const_iterator(adl_begin(std::get<Ns>(storage))...); 844*06c3fb27SDimitry Andric } 845*06c3fb27SDimitry Andric template <size_t... Ns> iterator begin_impl(std::index_sequence<Ns...>) { 846*06c3fb27SDimitry Andric return iterator(adl_begin(std::get<Ns>(storage))...); 847*06c3fb27SDimitry Andric } 8480b57cec5SDimitry Andric 8498bcb0991SDimitry Andric template <size_t... Ns> 850*06c3fb27SDimitry Andric const_iterator end_impl(std::index_sequence<Ns...>) const { 851*06c3fb27SDimitry Andric return const_iterator(adl_end(std::get<Ns>(storage))...); 8520b57cec5SDimitry Andric } 853*06c3fb27SDimitry Andric template <size_t... Ns> iterator end_impl(std::index_sequence<Ns...>) { 854*06c3fb27SDimitry Andric return iterator(adl_end(std::get<Ns>(storage))...); 8550b57cec5SDimitry Andric } 8560b57cec5SDimitry Andric }; 8570b57cec5SDimitry Andric 8580b57cec5SDimitry Andric } // end namespace detail 8590b57cec5SDimitry Andric 860bdd1243dSDimitry Andric /// zip iterator for two or more iteratable types. Iteration continues until the 861bdd1243dSDimitry Andric /// end of the *shortest* iteratee is reached. 8620b57cec5SDimitry Andric template <typename T, typename U, typename... Args> 8630b57cec5SDimitry Andric detail::zippy<detail::zip_shortest, T, U, Args...> zip(T &&t, U &&u, 8640b57cec5SDimitry Andric Args &&...args) { 8650b57cec5SDimitry Andric return detail::zippy<detail::zip_shortest, T, U, Args...>( 8660b57cec5SDimitry Andric std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...); 8670b57cec5SDimitry Andric } 8680b57cec5SDimitry Andric 869bdd1243dSDimitry Andric /// zip iterator that assumes that all iteratees have the same length. 870bdd1243dSDimitry Andric /// In builds with assertions on, this assumption is checked before the 871bdd1243dSDimitry Andric /// iteration starts. 872bdd1243dSDimitry Andric template <typename T, typename U, typename... Args> 873bdd1243dSDimitry Andric detail::zippy<detail::zip_first, T, U, Args...> zip_equal(T &&t, U &&u, 874bdd1243dSDimitry Andric Args &&...args) { 875*06c3fb27SDimitry Andric assert(all_equal({range_size(t), range_size(u), range_size(args)...}) && 876bdd1243dSDimitry Andric "Iteratees do not have equal length"); 877bdd1243dSDimitry Andric return detail::zippy<detail::zip_first, T, U, Args...>( 878bdd1243dSDimitry Andric std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...); 879bdd1243dSDimitry Andric } 880bdd1243dSDimitry Andric 8810b57cec5SDimitry Andric /// zip iterator that, for the sake of efficiency, assumes the first iteratee to 882bdd1243dSDimitry Andric /// be the shortest. Iteration continues until the end of the first iteratee is 883bdd1243dSDimitry Andric /// reached. In builds with assertions on, we check that the assumption about 884bdd1243dSDimitry Andric /// the first iteratee being the shortest holds. 8850b57cec5SDimitry Andric template <typename T, typename U, typename... Args> 8860b57cec5SDimitry Andric detail::zippy<detail::zip_first, T, U, Args...> zip_first(T &&t, U &&u, 8870b57cec5SDimitry Andric Args &&...args) { 888*06c3fb27SDimitry Andric assert(range_size(t) <= std::min({range_size(u), range_size(args)...}) && 889bdd1243dSDimitry Andric "First iteratee is not the shortest"); 890bdd1243dSDimitry Andric 8910b57cec5SDimitry Andric return detail::zippy<detail::zip_first, T, U, Args...>( 8920b57cec5SDimitry Andric std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...); 8930b57cec5SDimitry Andric } 8940b57cec5SDimitry Andric 8950b57cec5SDimitry Andric namespace detail { 8960b57cec5SDimitry Andric template <typename Iter> 8975ffd83dbSDimitry Andric Iter next_or_end(const Iter &I, const Iter &End) { 8980b57cec5SDimitry Andric if (I == End) 8990b57cec5SDimitry Andric return End; 9000b57cec5SDimitry Andric return std::next(I); 9010b57cec5SDimitry Andric } 9020b57cec5SDimitry Andric 9030b57cec5SDimitry Andric template <typename Iter> 904bdd1243dSDimitry Andric auto deref_or_none(const Iter &I, const Iter &End) -> std::optional< 9055ffd83dbSDimitry Andric std::remove_const_t<std::remove_reference_t<decltype(*I)>>> { 9060b57cec5SDimitry Andric if (I == End) 907bdd1243dSDimitry Andric return std::nullopt; 9080b57cec5SDimitry Andric return *I; 9090b57cec5SDimitry Andric } 9100b57cec5SDimitry Andric 9110b57cec5SDimitry Andric template <typename Iter> struct ZipLongestItemType { 912bdd1243dSDimitry Andric using type = std::optional<std::remove_const_t< 913bdd1243dSDimitry Andric std::remove_reference_t<decltype(*std::declval<Iter>())>>>; 9140b57cec5SDimitry Andric }; 9150b57cec5SDimitry Andric 9160b57cec5SDimitry Andric template <typename... Iters> struct ZipLongestTupleType { 9170b57cec5SDimitry Andric using type = std::tuple<typename ZipLongestItemType<Iters>::type...>; 9180b57cec5SDimitry Andric }; 9190b57cec5SDimitry Andric 9200b57cec5SDimitry Andric template <typename... Iters> 9210b57cec5SDimitry Andric class zip_longest_iterator 9220b57cec5SDimitry Andric : public iterator_facade_base< 9230b57cec5SDimitry Andric zip_longest_iterator<Iters...>, 924bdd1243dSDimitry Andric std::common_type_t< 9250b57cec5SDimitry Andric std::forward_iterator_tag, 926bdd1243dSDimitry Andric typename std::iterator_traits<Iters>::iterator_category...>, 9270b57cec5SDimitry Andric typename ZipLongestTupleType<Iters...>::type, 928bdd1243dSDimitry Andric typename std::iterator_traits< 929bdd1243dSDimitry Andric std::tuple_element_t<0, std::tuple<Iters...>>>::difference_type, 9300b57cec5SDimitry Andric typename ZipLongestTupleType<Iters...>::type *, 9310b57cec5SDimitry Andric typename ZipLongestTupleType<Iters...>::type> { 9320b57cec5SDimitry Andric public: 9330b57cec5SDimitry Andric using value_type = typename ZipLongestTupleType<Iters...>::type; 9340b57cec5SDimitry Andric 9350b57cec5SDimitry Andric private: 9360b57cec5SDimitry Andric std::tuple<Iters...> iterators; 9370b57cec5SDimitry Andric std::tuple<Iters...> end_iterators; 9380b57cec5SDimitry Andric 9390b57cec5SDimitry Andric template <size_t... Ns> 9400b57cec5SDimitry Andric bool test(const zip_longest_iterator<Iters...> &other, 9418bcb0991SDimitry Andric std::index_sequence<Ns...>) const { 942bdd1243dSDimitry Andric return ((std::get<Ns>(this->iterators) != std::get<Ns>(other.iterators)) || 943bdd1243dSDimitry Andric ...); 9440b57cec5SDimitry Andric } 9450b57cec5SDimitry Andric 9468bcb0991SDimitry Andric template <size_t... Ns> value_type deref(std::index_sequence<Ns...>) const { 9470b57cec5SDimitry Andric return value_type( 9480b57cec5SDimitry Andric deref_or_none(std::get<Ns>(iterators), std::get<Ns>(end_iterators))...); 9490b57cec5SDimitry Andric } 9500b57cec5SDimitry Andric 9510b57cec5SDimitry Andric template <size_t... Ns> 9528bcb0991SDimitry Andric decltype(iterators) tup_inc(std::index_sequence<Ns...>) const { 9530b57cec5SDimitry Andric return std::tuple<Iters...>( 9540b57cec5SDimitry Andric next_or_end(std::get<Ns>(iterators), std::get<Ns>(end_iterators))...); 9550b57cec5SDimitry Andric } 9560b57cec5SDimitry Andric 9570b57cec5SDimitry Andric public: 9580b57cec5SDimitry Andric zip_longest_iterator(std::pair<Iters &&, Iters &&>... ts) 9590b57cec5SDimitry Andric : iterators(std::forward<Iters>(ts.first)...), 9600b57cec5SDimitry Andric end_iterators(std::forward<Iters>(ts.second)...) {} 9610b57cec5SDimitry Andric 9628bcb0991SDimitry Andric value_type operator*() const { 9638bcb0991SDimitry Andric return deref(std::index_sequence_for<Iters...>{}); 9648bcb0991SDimitry Andric } 9650b57cec5SDimitry Andric 9660b57cec5SDimitry Andric zip_longest_iterator<Iters...> &operator++() { 9678bcb0991SDimitry Andric iterators = tup_inc(std::index_sequence_for<Iters...>{}); 9680b57cec5SDimitry Andric return *this; 9690b57cec5SDimitry Andric } 9700b57cec5SDimitry Andric 9710b57cec5SDimitry Andric bool operator==(const zip_longest_iterator<Iters...> &other) const { 9728bcb0991SDimitry Andric return !test(other, std::index_sequence_for<Iters...>{}); 9730b57cec5SDimitry Andric } 9740b57cec5SDimitry Andric }; 9750b57cec5SDimitry Andric 9760b57cec5SDimitry Andric template <typename... Args> class zip_longest_range { 9770b57cec5SDimitry Andric public: 9780b57cec5SDimitry Andric using iterator = 9790b57cec5SDimitry Andric zip_longest_iterator<decltype(adl_begin(std::declval<Args>()))...>; 9800b57cec5SDimitry Andric using iterator_category = typename iterator::iterator_category; 9810b57cec5SDimitry Andric using value_type = typename iterator::value_type; 9820b57cec5SDimitry Andric using difference_type = typename iterator::difference_type; 9830b57cec5SDimitry Andric using pointer = typename iterator::pointer; 9840b57cec5SDimitry Andric using reference = typename iterator::reference; 9850b57cec5SDimitry Andric 9860b57cec5SDimitry Andric private: 9870b57cec5SDimitry Andric std::tuple<Args...> ts; 9880b57cec5SDimitry Andric 9898bcb0991SDimitry Andric template <size_t... Ns> 9908bcb0991SDimitry Andric iterator begin_impl(std::index_sequence<Ns...>) const { 9910b57cec5SDimitry Andric return iterator(std::make_pair(adl_begin(std::get<Ns>(ts)), 9920b57cec5SDimitry Andric adl_end(std::get<Ns>(ts)))...); 9930b57cec5SDimitry Andric } 9940b57cec5SDimitry Andric 9958bcb0991SDimitry Andric template <size_t... Ns> iterator end_impl(std::index_sequence<Ns...>) const { 9960b57cec5SDimitry Andric return iterator(std::make_pair(adl_end(std::get<Ns>(ts)), 9970b57cec5SDimitry Andric adl_end(std::get<Ns>(ts)))...); 9980b57cec5SDimitry Andric } 9990b57cec5SDimitry Andric 10000b57cec5SDimitry Andric public: 10010b57cec5SDimitry Andric zip_longest_range(Args &&... ts_) : ts(std::forward<Args>(ts_)...) {} 10020b57cec5SDimitry Andric 10038bcb0991SDimitry Andric iterator begin() const { 10048bcb0991SDimitry Andric return begin_impl(std::index_sequence_for<Args...>{}); 10058bcb0991SDimitry Andric } 10068bcb0991SDimitry Andric iterator end() const { return end_impl(std::index_sequence_for<Args...>{}); } 10070b57cec5SDimitry Andric }; 10080b57cec5SDimitry Andric } // namespace detail 10090b57cec5SDimitry Andric 10100b57cec5SDimitry Andric /// Iterate over two or more iterators at the same time. Iteration continues 1011bdd1243dSDimitry Andric /// until all iterators reach the end. The std::optional only contains a value 10120b57cec5SDimitry Andric /// if the iterator has not reached the end. 10130b57cec5SDimitry Andric template <typename T, typename U, typename... Args> 10140b57cec5SDimitry Andric detail::zip_longest_range<T, U, Args...> zip_longest(T &&t, U &&u, 10150b57cec5SDimitry Andric Args &&... args) { 10160b57cec5SDimitry Andric return detail::zip_longest_range<T, U, Args...>( 10170b57cec5SDimitry Andric std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...); 10180b57cec5SDimitry Andric } 10190b57cec5SDimitry Andric 10200b57cec5SDimitry Andric /// Iterator wrapper that concatenates sequences together. 10210b57cec5SDimitry Andric /// 10220b57cec5SDimitry Andric /// This can concatenate different iterators, even with different types, into 10230b57cec5SDimitry Andric /// a single iterator provided the value types of all the concatenated 10240b57cec5SDimitry Andric /// iterators expose `reference` and `pointer` types that can be converted to 10250b57cec5SDimitry Andric /// `ValueT &` and `ValueT *` respectively. It doesn't support more 10260b57cec5SDimitry Andric /// interesting/customized pointer or reference types. 10270b57cec5SDimitry Andric /// 10280b57cec5SDimitry Andric /// Currently this only supports forward or higher iterator categories as 10290b57cec5SDimitry Andric /// inputs and always exposes a forward iterator interface. 10300b57cec5SDimitry Andric template <typename ValueT, typename... IterTs> 10310b57cec5SDimitry Andric class concat_iterator 10320b57cec5SDimitry Andric : public iterator_facade_base<concat_iterator<ValueT, IterTs...>, 10330b57cec5SDimitry Andric std::forward_iterator_tag, ValueT> { 10340b57cec5SDimitry Andric using BaseT = typename concat_iterator::iterator_facade_base; 10350b57cec5SDimitry Andric 10360b57cec5SDimitry Andric /// We store both the current and end iterators for each concatenated 10370b57cec5SDimitry Andric /// sequence in a tuple of pairs. 10380b57cec5SDimitry Andric /// 10390b57cec5SDimitry Andric /// Note that something like iterator_range seems nice at first here, but the 10400b57cec5SDimitry Andric /// range properties are of little benefit and end up getting in the way 10410b57cec5SDimitry Andric /// because we need to do mutation on the current iterators. 10420b57cec5SDimitry Andric std::tuple<IterTs...> Begins; 10430b57cec5SDimitry Andric std::tuple<IterTs...> Ends; 10440b57cec5SDimitry Andric 10450b57cec5SDimitry Andric /// Attempts to increment a specific iterator. 10460b57cec5SDimitry Andric /// 10470b57cec5SDimitry Andric /// Returns true if it was able to increment the iterator. Returns false if 10480b57cec5SDimitry Andric /// the iterator is already at the end iterator. 10490b57cec5SDimitry Andric template <size_t Index> bool incrementHelper() { 10500b57cec5SDimitry Andric auto &Begin = std::get<Index>(Begins); 10510b57cec5SDimitry Andric auto &End = std::get<Index>(Ends); 10520b57cec5SDimitry Andric if (Begin == End) 10530b57cec5SDimitry Andric return false; 10540b57cec5SDimitry Andric 10550b57cec5SDimitry Andric ++Begin; 10560b57cec5SDimitry Andric return true; 10570b57cec5SDimitry Andric } 10580b57cec5SDimitry Andric 10590b57cec5SDimitry Andric /// Increments the first non-end iterator. 10600b57cec5SDimitry Andric /// 10610b57cec5SDimitry Andric /// It is an error to call this with all iterators at the end. 10628bcb0991SDimitry Andric template <size_t... Ns> void increment(std::index_sequence<Ns...>) { 10630b57cec5SDimitry Andric // Build a sequence of functions to increment each iterator if possible. 10640b57cec5SDimitry Andric bool (concat_iterator::*IncrementHelperFns[])() = { 10650b57cec5SDimitry Andric &concat_iterator::incrementHelper<Ns>...}; 10660b57cec5SDimitry Andric 10670b57cec5SDimitry Andric // Loop over them, and stop as soon as we succeed at incrementing one. 10680b57cec5SDimitry Andric for (auto &IncrementHelperFn : IncrementHelperFns) 10690b57cec5SDimitry Andric if ((this->*IncrementHelperFn)()) 10700b57cec5SDimitry Andric return; 10710b57cec5SDimitry Andric 10720b57cec5SDimitry Andric llvm_unreachable("Attempted to increment an end concat iterator!"); 10730b57cec5SDimitry Andric } 10740b57cec5SDimitry Andric 10750b57cec5SDimitry Andric /// Returns null if the specified iterator is at the end. Otherwise, 10760b57cec5SDimitry Andric /// dereferences the iterator and returns the address of the resulting 10770b57cec5SDimitry Andric /// reference. 10780b57cec5SDimitry Andric template <size_t Index> ValueT *getHelper() const { 10790b57cec5SDimitry Andric auto &Begin = std::get<Index>(Begins); 10800b57cec5SDimitry Andric auto &End = std::get<Index>(Ends); 10810b57cec5SDimitry Andric if (Begin == End) 10820b57cec5SDimitry Andric return nullptr; 10830b57cec5SDimitry Andric 10840b57cec5SDimitry Andric return &*Begin; 10850b57cec5SDimitry Andric } 10860b57cec5SDimitry Andric 10870b57cec5SDimitry Andric /// Finds the first non-end iterator, dereferences, and returns the resulting 10880b57cec5SDimitry Andric /// reference. 10890b57cec5SDimitry Andric /// 10900b57cec5SDimitry Andric /// It is an error to call this with all iterators at the end. 10918bcb0991SDimitry Andric template <size_t... Ns> ValueT &get(std::index_sequence<Ns...>) const { 10920b57cec5SDimitry Andric // Build a sequence of functions to get from iterator if possible. 10930b57cec5SDimitry Andric ValueT *(concat_iterator::*GetHelperFns[])() const = { 10940b57cec5SDimitry Andric &concat_iterator::getHelper<Ns>...}; 10950b57cec5SDimitry Andric 10960b57cec5SDimitry Andric // Loop over them, and return the first result we find. 10970b57cec5SDimitry Andric for (auto &GetHelperFn : GetHelperFns) 10980b57cec5SDimitry Andric if (ValueT *P = (this->*GetHelperFn)()) 10990b57cec5SDimitry Andric return *P; 11000b57cec5SDimitry Andric 11010b57cec5SDimitry Andric llvm_unreachable("Attempted to get a pointer from an end concat iterator!"); 11020b57cec5SDimitry Andric } 11030b57cec5SDimitry Andric 11040b57cec5SDimitry Andric public: 11055ffd83dbSDimitry Andric /// Constructs an iterator from a sequence of ranges. 11060b57cec5SDimitry Andric /// 11070b57cec5SDimitry Andric /// We need the full range to know how to switch between each of the 11080b57cec5SDimitry Andric /// iterators. 11090b57cec5SDimitry Andric template <typename... RangeTs> 11100b57cec5SDimitry Andric explicit concat_iterator(RangeTs &&... Ranges) 11110b57cec5SDimitry Andric : Begins(std::begin(Ranges)...), Ends(std::end(Ranges)...) {} 11120b57cec5SDimitry Andric 11130b57cec5SDimitry Andric using BaseT::operator++; 11140b57cec5SDimitry Andric 11150b57cec5SDimitry Andric concat_iterator &operator++() { 11168bcb0991SDimitry Andric increment(std::index_sequence_for<IterTs...>()); 11170b57cec5SDimitry Andric return *this; 11180b57cec5SDimitry Andric } 11190b57cec5SDimitry Andric 11208bcb0991SDimitry Andric ValueT &operator*() const { 11218bcb0991SDimitry Andric return get(std::index_sequence_for<IterTs...>()); 11228bcb0991SDimitry Andric } 11230b57cec5SDimitry Andric 11240b57cec5SDimitry Andric bool operator==(const concat_iterator &RHS) const { 11250b57cec5SDimitry Andric return Begins == RHS.Begins && Ends == RHS.Ends; 11260b57cec5SDimitry Andric } 11270b57cec5SDimitry Andric }; 11280b57cec5SDimitry Andric 11290b57cec5SDimitry Andric namespace detail { 11300b57cec5SDimitry Andric 11310b57cec5SDimitry Andric /// Helper to store a sequence of ranges being concatenated and access them. 11320b57cec5SDimitry Andric /// 11330b57cec5SDimitry Andric /// This is designed to facilitate providing actual storage when temporaries 11340b57cec5SDimitry Andric /// are passed into the constructor such that we can use it as part of range 11350b57cec5SDimitry Andric /// based for loops. 11360b57cec5SDimitry Andric template <typename ValueT, typename... RangeTs> class concat_range { 11370b57cec5SDimitry Andric public: 11380b57cec5SDimitry Andric using iterator = 11390b57cec5SDimitry Andric concat_iterator<ValueT, 11400b57cec5SDimitry Andric decltype(std::begin(std::declval<RangeTs &>()))...>; 11410b57cec5SDimitry Andric 11420b57cec5SDimitry Andric private: 11430b57cec5SDimitry Andric std::tuple<RangeTs...> Ranges; 11440b57cec5SDimitry Andric 11454824e7fdSDimitry Andric template <size_t... Ns> 11464824e7fdSDimitry Andric iterator begin_impl(std::index_sequence<Ns...>) { 11474824e7fdSDimitry Andric return iterator(std::get<Ns>(Ranges)...); 11484824e7fdSDimitry Andric } 11494824e7fdSDimitry Andric template <size_t... Ns> 11504824e7fdSDimitry Andric iterator begin_impl(std::index_sequence<Ns...>) const { 11510b57cec5SDimitry Andric return iterator(std::get<Ns>(Ranges)...); 11520b57cec5SDimitry Andric } 11538bcb0991SDimitry Andric template <size_t... Ns> iterator end_impl(std::index_sequence<Ns...>) { 11540b57cec5SDimitry Andric return iterator(make_range(std::end(std::get<Ns>(Ranges)), 11550b57cec5SDimitry Andric std::end(std::get<Ns>(Ranges)))...); 11560b57cec5SDimitry Andric } 11574824e7fdSDimitry Andric template <size_t... Ns> iterator end_impl(std::index_sequence<Ns...>) const { 11584824e7fdSDimitry Andric return iterator(make_range(std::end(std::get<Ns>(Ranges)), 11594824e7fdSDimitry Andric std::end(std::get<Ns>(Ranges)))...); 11604824e7fdSDimitry Andric } 11610b57cec5SDimitry Andric 11620b57cec5SDimitry Andric public: 11630b57cec5SDimitry Andric concat_range(RangeTs &&... Ranges) 11640b57cec5SDimitry Andric : Ranges(std::forward<RangeTs>(Ranges)...) {} 11650b57cec5SDimitry Andric 11664824e7fdSDimitry Andric iterator begin() { 11674824e7fdSDimitry Andric return begin_impl(std::index_sequence_for<RangeTs...>{}); 11684824e7fdSDimitry Andric } 11694824e7fdSDimitry Andric iterator begin() const { 11704824e7fdSDimitry Andric return begin_impl(std::index_sequence_for<RangeTs...>{}); 11714824e7fdSDimitry Andric } 11724824e7fdSDimitry Andric iterator end() { 11734824e7fdSDimitry Andric return end_impl(std::index_sequence_for<RangeTs...>{}); 11744824e7fdSDimitry Andric } 11754824e7fdSDimitry Andric iterator end() const { 11764824e7fdSDimitry Andric return end_impl(std::index_sequence_for<RangeTs...>{}); 11774824e7fdSDimitry Andric } 11780b57cec5SDimitry Andric }; 11790b57cec5SDimitry Andric 11800b57cec5SDimitry Andric } // end namespace detail 11810b57cec5SDimitry Andric 11820b57cec5SDimitry Andric /// Concatenated range across two or more ranges. 11830b57cec5SDimitry Andric /// 11840b57cec5SDimitry Andric /// The desired value type must be explicitly specified. 11850b57cec5SDimitry Andric template <typename ValueT, typename... RangeTs> 11860b57cec5SDimitry Andric detail::concat_range<ValueT, RangeTs...> concat(RangeTs &&... Ranges) { 11870b57cec5SDimitry Andric static_assert(sizeof...(RangeTs) > 1, 11880b57cec5SDimitry Andric "Need more than one range to concatenate!"); 11890b57cec5SDimitry Andric return detail::concat_range<ValueT, RangeTs...>( 11900b57cec5SDimitry Andric std::forward<RangeTs>(Ranges)...); 11910b57cec5SDimitry Andric } 11920b57cec5SDimitry Andric 11935ffd83dbSDimitry Andric /// A utility class used to implement an iterator that contains some base object 11945ffd83dbSDimitry Andric /// and an index. The iterator moves the index but keeps the base constant. 11955ffd83dbSDimitry Andric template <typename DerivedT, typename BaseT, typename T, 11965ffd83dbSDimitry Andric typename PointerT = T *, typename ReferenceT = T &> 11975ffd83dbSDimitry Andric class indexed_accessor_iterator 11985ffd83dbSDimitry Andric : public llvm::iterator_facade_base<DerivedT, 11995ffd83dbSDimitry Andric std::random_access_iterator_tag, T, 12005ffd83dbSDimitry Andric std::ptrdiff_t, PointerT, ReferenceT> { 12015ffd83dbSDimitry Andric public: 12025ffd83dbSDimitry Andric ptrdiff_t operator-(const indexed_accessor_iterator &rhs) const { 12035ffd83dbSDimitry Andric assert(base == rhs.base && "incompatible iterators"); 12045ffd83dbSDimitry Andric return index - rhs.index; 12055ffd83dbSDimitry Andric } 12065ffd83dbSDimitry Andric bool operator==(const indexed_accessor_iterator &rhs) const { 12075ffd83dbSDimitry Andric return base == rhs.base && index == rhs.index; 12085ffd83dbSDimitry Andric } 12095ffd83dbSDimitry Andric bool operator<(const indexed_accessor_iterator &rhs) const { 12105ffd83dbSDimitry Andric assert(base == rhs.base && "incompatible iterators"); 12115ffd83dbSDimitry Andric return index < rhs.index; 12125ffd83dbSDimitry Andric } 12135ffd83dbSDimitry Andric 12145ffd83dbSDimitry Andric DerivedT &operator+=(ptrdiff_t offset) { 12155ffd83dbSDimitry Andric this->index += offset; 12165ffd83dbSDimitry Andric return static_cast<DerivedT &>(*this); 12175ffd83dbSDimitry Andric } 12185ffd83dbSDimitry Andric DerivedT &operator-=(ptrdiff_t offset) { 12195ffd83dbSDimitry Andric this->index -= offset; 12205ffd83dbSDimitry Andric return static_cast<DerivedT &>(*this); 12215ffd83dbSDimitry Andric } 12225ffd83dbSDimitry Andric 12235ffd83dbSDimitry Andric /// Returns the current index of the iterator. 12245ffd83dbSDimitry Andric ptrdiff_t getIndex() const { return index; } 12255ffd83dbSDimitry Andric 12265ffd83dbSDimitry Andric /// Returns the current base of the iterator. 12275ffd83dbSDimitry Andric const BaseT &getBase() const { return base; } 12285ffd83dbSDimitry Andric 12295ffd83dbSDimitry Andric protected: 12305ffd83dbSDimitry Andric indexed_accessor_iterator(BaseT base, ptrdiff_t index) 12315ffd83dbSDimitry Andric : base(base), index(index) {} 12325ffd83dbSDimitry Andric BaseT base; 12335ffd83dbSDimitry Andric ptrdiff_t index; 12345ffd83dbSDimitry Andric }; 12355ffd83dbSDimitry Andric 12365ffd83dbSDimitry Andric namespace detail { 12375ffd83dbSDimitry Andric /// The class represents the base of a range of indexed_accessor_iterators. It 12385ffd83dbSDimitry Andric /// provides support for many different range functionalities, e.g. 12395ffd83dbSDimitry Andric /// drop_front/slice/etc.. Derived range classes must implement the following 12405ffd83dbSDimitry Andric /// static methods: 12415ffd83dbSDimitry Andric /// * ReferenceT dereference_iterator(const BaseT &base, ptrdiff_t index) 12425ffd83dbSDimitry Andric /// - Dereference an iterator pointing to the base object at the given 12435ffd83dbSDimitry Andric /// index. 12445ffd83dbSDimitry Andric /// * BaseT offset_base(const BaseT &base, ptrdiff_t index) 12455ffd83dbSDimitry Andric /// - Return a new base that is offset from the provide base by 'index' 12465ffd83dbSDimitry Andric /// elements. 12475ffd83dbSDimitry Andric template <typename DerivedT, typename BaseT, typename T, 12485ffd83dbSDimitry Andric typename PointerT = T *, typename ReferenceT = T &> 12495ffd83dbSDimitry Andric class indexed_accessor_range_base { 12505ffd83dbSDimitry Andric public: 1251349cc55cSDimitry Andric using RangeBaseT = indexed_accessor_range_base; 12525ffd83dbSDimitry Andric 12535ffd83dbSDimitry Andric /// An iterator element of this range. 12545ffd83dbSDimitry Andric class iterator : public indexed_accessor_iterator<iterator, BaseT, T, 12555ffd83dbSDimitry Andric PointerT, ReferenceT> { 12565ffd83dbSDimitry Andric public: 12575ffd83dbSDimitry Andric // Index into this iterator, invoking a static method on the derived type. 12585ffd83dbSDimitry Andric ReferenceT operator*() const { 12595ffd83dbSDimitry Andric return DerivedT::dereference_iterator(this->getBase(), this->getIndex()); 12605ffd83dbSDimitry Andric } 12615ffd83dbSDimitry Andric 12625ffd83dbSDimitry Andric private: 12635ffd83dbSDimitry Andric iterator(BaseT owner, ptrdiff_t curIndex) 1264349cc55cSDimitry Andric : iterator::indexed_accessor_iterator(owner, curIndex) {} 12655ffd83dbSDimitry Andric 12665ffd83dbSDimitry Andric /// Allow access to the constructor. 12675ffd83dbSDimitry Andric friend indexed_accessor_range_base<DerivedT, BaseT, T, PointerT, 12685ffd83dbSDimitry Andric ReferenceT>; 12695ffd83dbSDimitry Andric }; 12705ffd83dbSDimitry Andric 12715ffd83dbSDimitry Andric indexed_accessor_range_base(iterator begin, iterator end) 12725ffd83dbSDimitry Andric : base(offset_base(begin.getBase(), begin.getIndex())), 12735ffd83dbSDimitry Andric count(end.getIndex() - begin.getIndex()) {} 12745ffd83dbSDimitry Andric indexed_accessor_range_base(const iterator_range<iterator> &range) 12755ffd83dbSDimitry Andric : indexed_accessor_range_base(range.begin(), range.end()) {} 12765ffd83dbSDimitry Andric indexed_accessor_range_base(BaseT base, ptrdiff_t count) 12775ffd83dbSDimitry Andric : base(base), count(count) {} 12785ffd83dbSDimitry Andric 12795ffd83dbSDimitry Andric iterator begin() const { return iterator(base, 0); } 12805ffd83dbSDimitry Andric iterator end() const { return iterator(base, count); } 1281fe6060f1SDimitry Andric ReferenceT operator[](size_t Index) const { 1282fe6060f1SDimitry Andric assert(Index < size() && "invalid index for value range"); 1283fe6060f1SDimitry Andric return DerivedT::dereference_iterator(base, static_cast<ptrdiff_t>(Index)); 12845ffd83dbSDimitry Andric } 12855ffd83dbSDimitry Andric ReferenceT front() const { 12865ffd83dbSDimitry Andric assert(!empty() && "expected non-empty range"); 12875ffd83dbSDimitry Andric return (*this)[0]; 12885ffd83dbSDimitry Andric } 12895ffd83dbSDimitry Andric ReferenceT back() const { 12905ffd83dbSDimitry Andric assert(!empty() && "expected non-empty range"); 12915ffd83dbSDimitry Andric return (*this)[size() - 1]; 12925ffd83dbSDimitry Andric } 12935ffd83dbSDimitry Andric 12945ffd83dbSDimitry Andric /// Compare this range with another. 129581ad6265SDimitry Andric template <typename OtherT> 129681ad6265SDimitry Andric friend bool operator==(const indexed_accessor_range_base &lhs, 129781ad6265SDimitry Andric const OtherT &rhs) { 129881ad6265SDimitry Andric return std::equal(lhs.begin(), lhs.end(), rhs.begin(), rhs.end()); 12995ffd83dbSDimitry Andric } 130081ad6265SDimitry Andric template <typename OtherT> 130181ad6265SDimitry Andric friend bool operator!=(const indexed_accessor_range_base &lhs, 130281ad6265SDimitry Andric const OtherT &rhs) { 130381ad6265SDimitry Andric return !(lhs == rhs); 13045ffd83dbSDimitry Andric } 13055ffd83dbSDimitry Andric 13065ffd83dbSDimitry Andric /// Return the size of this range. 13075ffd83dbSDimitry Andric size_t size() const { return count; } 13085ffd83dbSDimitry Andric 13095ffd83dbSDimitry Andric /// Return if the range is empty. 13105ffd83dbSDimitry Andric bool empty() const { return size() == 0; } 13115ffd83dbSDimitry Andric 13125ffd83dbSDimitry Andric /// Drop the first N elements, and keep M elements. 13135ffd83dbSDimitry Andric DerivedT slice(size_t n, size_t m) const { 13145ffd83dbSDimitry Andric assert(n + m <= size() && "invalid size specifiers"); 13155ffd83dbSDimitry Andric return DerivedT(offset_base(base, n), m); 13165ffd83dbSDimitry Andric } 13175ffd83dbSDimitry Andric 13185ffd83dbSDimitry Andric /// Drop the first n elements. 13195ffd83dbSDimitry Andric DerivedT drop_front(size_t n = 1) const { 13205ffd83dbSDimitry Andric assert(size() >= n && "Dropping more elements than exist"); 13215ffd83dbSDimitry Andric return slice(n, size() - n); 13225ffd83dbSDimitry Andric } 13235ffd83dbSDimitry Andric /// Drop the last n elements. 13245ffd83dbSDimitry Andric DerivedT drop_back(size_t n = 1) const { 13255ffd83dbSDimitry Andric assert(size() >= n && "Dropping more elements than exist"); 13265ffd83dbSDimitry Andric return DerivedT(base, size() - n); 13275ffd83dbSDimitry Andric } 13285ffd83dbSDimitry Andric 13295ffd83dbSDimitry Andric /// Take the first n elements. 13305ffd83dbSDimitry Andric DerivedT take_front(size_t n = 1) const { 13315ffd83dbSDimitry Andric return n < size() ? drop_back(size() - n) 13325ffd83dbSDimitry Andric : static_cast<const DerivedT &>(*this); 13335ffd83dbSDimitry Andric } 13345ffd83dbSDimitry Andric 13355ffd83dbSDimitry Andric /// Take the last n elements. 13365ffd83dbSDimitry Andric DerivedT take_back(size_t n = 1) const { 13375ffd83dbSDimitry Andric return n < size() ? drop_front(size() - n) 13385ffd83dbSDimitry Andric : static_cast<const DerivedT &>(*this); 13395ffd83dbSDimitry Andric } 13405ffd83dbSDimitry Andric 13415ffd83dbSDimitry Andric /// Allow conversion to any type accepting an iterator_range. 13425ffd83dbSDimitry Andric template <typename RangeT, typename = std::enable_if_t<std::is_constructible< 13435ffd83dbSDimitry Andric RangeT, iterator_range<iterator>>::value>> 13445ffd83dbSDimitry Andric operator RangeT() const { 13455ffd83dbSDimitry Andric return RangeT(iterator_range<iterator>(*this)); 13465ffd83dbSDimitry Andric } 13475ffd83dbSDimitry Andric 13485ffd83dbSDimitry Andric /// Returns the base of this range. 13495ffd83dbSDimitry Andric const BaseT &getBase() const { return base; } 13505ffd83dbSDimitry Andric 13515ffd83dbSDimitry Andric private: 13525ffd83dbSDimitry Andric /// Offset the given base by the given amount. 13535ffd83dbSDimitry Andric static BaseT offset_base(const BaseT &base, size_t n) { 13545ffd83dbSDimitry Andric return n == 0 ? base : DerivedT::offset_base(base, n); 13555ffd83dbSDimitry Andric } 13565ffd83dbSDimitry Andric 13575ffd83dbSDimitry Andric protected: 13585ffd83dbSDimitry Andric indexed_accessor_range_base(const indexed_accessor_range_base &) = default; 13595ffd83dbSDimitry Andric indexed_accessor_range_base(indexed_accessor_range_base &&) = default; 13605ffd83dbSDimitry Andric indexed_accessor_range_base & 13615ffd83dbSDimitry Andric operator=(const indexed_accessor_range_base &) = default; 13625ffd83dbSDimitry Andric 13635ffd83dbSDimitry Andric /// The base that owns the provided range of values. 13645ffd83dbSDimitry Andric BaseT base; 13655ffd83dbSDimitry Andric /// The size from the owning range. 13665ffd83dbSDimitry Andric ptrdiff_t count; 13675ffd83dbSDimitry Andric }; 13685ffd83dbSDimitry Andric } // end namespace detail 13695ffd83dbSDimitry Andric 13705ffd83dbSDimitry Andric /// This class provides an implementation of a range of 13715ffd83dbSDimitry Andric /// indexed_accessor_iterators where the base is not indexable. Ranges with 13725ffd83dbSDimitry Andric /// bases that are offsetable should derive from indexed_accessor_range_base 13735ffd83dbSDimitry Andric /// instead. Derived range classes are expected to implement the following 13745ffd83dbSDimitry Andric /// static method: 13755ffd83dbSDimitry Andric /// * ReferenceT dereference(const BaseT &base, ptrdiff_t index) 13765ffd83dbSDimitry Andric /// - Dereference an iterator pointing to a parent base at the given index. 13775ffd83dbSDimitry Andric template <typename DerivedT, typename BaseT, typename T, 13785ffd83dbSDimitry Andric typename PointerT = T *, typename ReferenceT = T &> 13795ffd83dbSDimitry Andric class indexed_accessor_range 13805ffd83dbSDimitry Andric : public detail::indexed_accessor_range_base< 13815ffd83dbSDimitry Andric DerivedT, std::pair<BaseT, ptrdiff_t>, T, PointerT, ReferenceT> { 13825ffd83dbSDimitry Andric public: 13835ffd83dbSDimitry Andric indexed_accessor_range(BaseT base, ptrdiff_t startIndex, ptrdiff_t count) 13845ffd83dbSDimitry Andric : detail::indexed_accessor_range_base< 13855ffd83dbSDimitry Andric DerivedT, std::pair<BaseT, ptrdiff_t>, T, PointerT, ReferenceT>( 13865ffd83dbSDimitry Andric std::make_pair(base, startIndex), count) {} 13875ffd83dbSDimitry Andric using detail::indexed_accessor_range_base< 13885ffd83dbSDimitry Andric DerivedT, std::pair<BaseT, ptrdiff_t>, T, PointerT, 13895ffd83dbSDimitry Andric ReferenceT>::indexed_accessor_range_base; 13905ffd83dbSDimitry Andric 13915ffd83dbSDimitry Andric /// Returns the current base of the range. 13925ffd83dbSDimitry Andric const BaseT &getBase() const { return this->base.first; } 13935ffd83dbSDimitry Andric 13945ffd83dbSDimitry Andric /// Returns the current start index of the range. 13955ffd83dbSDimitry Andric ptrdiff_t getStartIndex() const { return this->base.second; } 13965ffd83dbSDimitry Andric 13975ffd83dbSDimitry Andric /// See `detail::indexed_accessor_range_base` for details. 13985ffd83dbSDimitry Andric static std::pair<BaseT, ptrdiff_t> 13995ffd83dbSDimitry Andric offset_base(const std::pair<BaseT, ptrdiff_t> &base, ptrdiff_t index) { 14005ffd83dbSDimitry Andric // We encode the internal base as a pair of the derived base and a start 14015ffd83dbSDimitry Andric // index into the derived base. 14025ffd83dbSDimitry Andric return std::make_pair(base.first, base.second + index); 14035ffd83dbSDimitry Andric } 14045ffd83dbSDimitry Andric /// See `detail::indexed_accessor_range_base` for details. 14055ffd83dbSDimitry Andric static ReferenceT 14065ffd83dbSDimitry Andric dereference_iterator(const std::pair<BaseT, ptrdiff_t> &base, 14075ffd83dbSDimitry Andric ptrdiff_t index) { 14085ffd83dbSDimitry Andric return DerivedT::dereference(base.first, base.second + index); 14095ffd83dbSDimitry Andric } 14105ffd83dbSDimitry Andric }; 14115ffd83dbSDimitry Andric 1412349cc55cSDimitry Andric namespace detail { 1413349cc55cSDimitry Andric /// Return a reference to the first or second member of a reference. Otherwise, 1414349cc55cSDimitry Andric /// return a copy of the member of a temporary. 1415349cc55cSDimitry Andric /// 1416349cc55cSDimitry Andric /// When passing a range whose iterators return values instead of references, 1417349cc55cSDimitry Andric /// the reference must be dropped from `decltype((elt.first))`, which will 1418349cc55cSDimitry Andric /// always be a reference, to avoid returning a reference to a temporary. 1419349cc55cSDimitry Andric template <typename EltTy, typename FirstTy> class first_or_second_type { 1420349cc55cSDimitry Andric public: 1421bdd1243dSDimitry Andric using type = std::conditional_t<std::is_reference<EltTy>::value, FirstTy, 1422349cc55cSDimitry Andric std::remove_reference_t<FirstTy>>; 1423349cc55cSDimitry Andric }; 1424349cc55cSDimitry Andric } // end namespace detail 1425349cc55cSDimitry Andric 1426e8d8bef9SDimitry Andric /// Given a container of pairs, return a range over the first elements. 1427e8d8bef9SDimitry Andric template <typename ContainerTy> auto make_first_range(ContainerTy &&c) { 1428349cc55cSDimitry Andric using EltTy = decltype((*std::begin(c))); 1429349cc55cSDimitry Andric return llvm::map_range(std::forward<ContainerTy>(c), 1430349cc55cSDimitry Andric [](EltTy elt) -> typename detail::first_or_second_type< 1431349cc55cSDimitry Andric EltTy, decltype((elt.first))>::type { 1432e8d8bef9SDimitry Andric return elt.first; 1433e8d8bef9SDimitry Andric }); 1434e8d8bef9SDimitry Andric } 1435e8d8bef9SDimitry Andric 14365ffd83dbSDimitry Andric /// Given a container of pairs, return a range over the second elements. 14375ffd83dbSDimitry Andric template <typename ContainerTy> auto make_second_range(ContainerTy &&c) { 1438349cc55cSDimitry Andric using EltTy = decltype((*std::begin(c))); 14395ffd83dbSDimitry Andric return llvm::map_range( 14405ffd83dbSDimitry Andric std::forward<ContainerTy>(c), 1441349cc55cSDimitry Andric [](EltTy elt) -> 1442349cc55cSDimitry Andric typename detail::first_or_second_type<EltTy, 1443349cc55cSDimitry Andric decltype((elt.second))>::type { 14445ffd83dbSDimitry Andric return elt.second; 14455ffd83dbSDimitry Andric }); 14465ffd83dbSDimitry Andric } 14475ffd83dbSDimitry Andric 14480b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 14490b57cec5SDimitry Andric // Extra additions to <utility> 14500b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 14510b57cec5SDimitry Andric 1452*06c3fb27SDimitry Andric /// Function object to check whether the first component of a container 1453*06c3fb27SDimitry Andric /// supported by std::get (like std::pair and std::tuple) compares less than the 1454*06c3fb27SDimitry Andric /// first component of another container. 14550b57cec5SDimitry Andric struct less_first { 14560b57cec5SDimitry Andric template <typename T> bool operator()(const T &lhs, const T &rhs) const { 1457*06c3fb27SDimitry Andric return std::less<>()(std::get<0>(lhs), std::get<0>(rhs)); 14580b57cec5SDimitry Andric } 14590b57cec5SDimitry Andric }; 14600b57cec5SDimitry Andric 1461*06c3fb27SDimitry Andric /// Function object to check whether the second component of a container 1462*06c3fb27SDimitry Andric /// supported by std::get (like std::pair and std::tuple) compares less than the 1463*06c3fb27SDimitry Andric /// second component of another container. 14640b57cec5SDimitry Andric struct less_second { 14650b57cec5SDimitry Andric template <typename T> bool operator()(const T &lhs, const T &rhs) const { 1466*06c3fb27SDimitry Andric return std::less<>()(std::get<1>(lhs), std::get<1>(rhs)); 14670b57cec5SDimitry Andric } 14680b57cec5SDimitry Andric }; 14690b57cec5SDimitry Andric 14700b57cec5SDimitry Andric /// \brief Function object to apply a binary function to the first component of 14710b57cec5SDimitry Andric /// a std::pair. 14720b57cec5SDimitry Andric template<typename FuncTy> 14730b57cec5SDimitry Andric struct on_first { 14740b57cec5SDimitry Andric FuncTy func; 14750b57cec5SDimitry Andric 14760b57cec5SDimitry Andric template <typename T> 14775ffd83dbSDimitry Andric decltype(auto) operator()(const T &lhs, const T &rhs) const { 14780b57cec5SDimitry Andric return func(lhs.first, rhs.first); 14790b57cec5SDimitry Andric } 14800b57cec5SDimitry Andric }; 14810b57cec5SDimitry Andric 14820b57cec5SDimitry Andric /// Utility type to build an inheritance chain that makes it easy to rank 14830b57cec5SDimitry Andric /// overload candidates. 14840b57cec5SDimitry Andric template <int N> struct rank : rank<N - 1> {}; 14850b57cec5SDimitry Andric template <> struct rank<0> {}; 14860b57cec5SDimitry Andric 14870b57cec5SDimitry Andric /// traits class for checking whether type T is one of any of the given 14880b57cec5SDimitry Andric /// types in the variadic list. 1489fe6060f1SDimitry Andric template <typename T, typename... Ts> 1490bdd1243dSDimitry Andric using is_one_of = std::disjunction<std::is_same<T, Ts>...>; 14910b57cec5SDimitry Andric 14920b57cec5SDimitry Andric /// traits class for checking whether type T is a base class for all 14930b57cec5SDimitry Andric /// the given types in the variadic list. 1494fe6060f1SDimitry Andric template <typename T, typename... Ts> 1495bdd1243dSDimitry Andric using are_base_of = std::conjunction<std::is_base_of<T, Ts>...>; 1496fe6060f1SDimitry Andric 1497fe6060f1SDimitry Andric namespace detail { 1498fe6060f1SDimitry Andric template <typename... Ts> struct Visitor; 1499fe6060f1SDimitry Andric 1500fe6060f1SDimitry Andric template <typename HeadT, typename... TailTs> 1501fe6060f1SDimitry Andric struct Visitor<HeadT, TailTs...> : remove_cvref_t<HeadT>, Visitor<TailTs...> { 1502fe6060f1SDimitry Andric explicit constexpr Visitor(HeadT &&Head, TailTs &&...Tail) 1503fe6060f1SDimitry Andric : remove_cvref_t<HeadT>(std::forward<HeadT>(Head)), 1504fe6060f1SDimitry Andric Visitor<TailTs...>(std::forward<TailTs>(Tail)...) {} 1505fe6060f1SDimitry Andric using remove_cvref_t<HeadT>::operator(); 1506fe6060f1SDimitry Andric using Visitor<TailTs...>::operator(); 15070b57cec5SDimitry Andric }; 15080b57cec5SDimitry Andric 1509fe6060f1SDimitry Andric template <typename HeadT> struct Visitor<HeadT> : remove_cvref_t<HeadT> { 1510fe6060f1SDimitry Andric explicit constexpr Visitor(HeadT &&Head) 1511fe6060f1SDimitry Andric : remove_cvref_t<HeadT>(std::forward<HeadT>(Head)) {} 1512fe6060f1SDimitry Andric using remove_cvref_t<HeadT>::operator(); 15130b57cec5SDimitry Andric }; 1514fe6060f1SDimitry Andric } // namespace detail 1515fe6060f1SDimitry Andric 1516fe6060f1SDimitry Andric /// Returns an opaquely-typed Callable object whose operator() overload set is 1517fe6060f1SDimitry Andric /// the sum of the operator() overload sets of each CallableT in CallableTs. 1518fe6060f1SDimitry Andric /// 1519fe6060f1SDimitry Andric /// The type of the returned object derives from each CallableT in CallableTs. 1520fe6060f1SDimitry Andric /// The returned object is constructed by invoking the appropriate copy or move 1521fe6060f1SDimitry Andric /// constructor of each CallableT, as selected by overload resolution on the 1522fe6060f1SDimitry Andric /// corresponding argument to makeVisitor. 1523fe6060f1SDimitry Andric /// 1524fe6060f1SDimitry Andric /// Example: 1525fe6060f1SDimitry Andric /// 1526fe6060f1SDimitry Andric /// \code 1527fe6060f1SDimitry Andric /// auto visitor = makeVisitor([](auto) { return "unhandled type"; }, 1528fe6060f1SDimitry Andric /// [](int i) { return "int"; }, 1529fe6060f1SDimitry Andric /// [](std::string s) { return "str"; }); 1530fe6060f1SDimitry Andric /// auto a = visitor(42); // `a` is now "int". 1531fe6060f1SDimitry Andric /// auto b = visitor("foo"); // `b` is now "str". 1532fe6060f1SDimitry Andric /// auto c = visitor(3.14f); // `c` is now "unhandled type". 1533fe6060f1SDimitry Andric /// \endcode 1534fe6060f1SDimitry Andric /// 1535fe6060f1SDimitry Andric /// Example of making a visitor with a lambda which captures a move-only type: 1536fe6060f1SDimitry Andric /// 1537fe6060f1SDimitry Andric /// \code 1538fe6060f1SDimitry Andric /// std::unique_ptr<FooHandler> FH = /* ... */; 1539fe6060f1SDimitry Andric /// auto visitor = makeVisitor( 1540fe6060f1SDimitry Andric /// [FH{std::move(FH)}](Foo F) { return FH->handle(F); }, 1541fe6060f1SDimitry Andric /// [](int i) { return i; }, 1542fe6060f1SDimitry Andric /// [](std::string s) { return atoi(s); }); 1543fe6060f1SDimitry Andric /// \endcode 1544fe6060f1SDimitry Andric template <typename... CallableTs> 1545fe6060f1SDimitry Andric constexpr decltype(auto) makeVisitor(CallableTs &&...Callables) { 1546fe6060f1SDimitry Andric return detail::Visitor<CallableTs...>(std::forward<CallableTs>(Callables)...); 1547fe6060f1SDimitry Andric } 15480b57cec5SDimitry Andric 15490b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 15501fd87a68SDimitry Andric // Extra additions to <algorithm> 15510b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 15520b57cec5SDimitry Andric 15535ffd83dbSDimitry Andric // We have a copy here so that LLVM behaves the same when using different 15545ffd83dbSDimitry Andric // standard libraries. 15555ffd83dbSDimitry Andric template <class Iterator, class RNG> 15565ffd83dbSDimitry Andric void shuffle(Iterator first, Iterator last, RNG &&g) { 15575ffd83dbSDimitry Andric // It would be better to use a std::uniform_int_distribution, 15585ffd83dbSDimitry Andric // but that would be stdlib dependent. 1559fe6060f1SDimitry Andric typedef 1560fe6060f1SDimitry Andric typename std::iterator_traits<Iterator>::difference_type difference_type; 1561fe6060f1SDimitry Andric for (auto size = last - first; size > 1; ++first, (void)--size) { 1562fe6060f1SDimitry Andric difference_type offset = g() % size; 1563fe6060f1SDimitry Andric // Avoid self-assignment due to incorrect assertions in libstdc++ 1564fe6060f1SDimitry Andric // containers (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85828). 1565fe6060f1SDimitry Andric if (offset != difference_type(0)) 1566fe6060f1SDimitry Andric std::iter_swap(first, first + offset); 1567fe6060f1SDimitry Andric } 15685ffd83dbSDimitry Andric } 15695ffd83dbSDimitry Andric 15700b57cec5SDimitry Andric /// Adapt std::less<T> for array_pod_sort. 15710b57cec5SDimitry Andric template<typename T> 15720b57cec5SDimitry Andric inline int array_pod_sort_comparator(const void *P1, const void *P2) { 15730b57cec5SDimitry Andric if (std::less<T>()(*reinterpret_cast<const T*>(P1), 15740b57cec5SDimitry Andric *reinterpret_cast<const T*>(P2))) 15750b57cec5SDimitry Andric return -1; 15760b57cec5SDimitry Andric if (std::less<T>()(*reinterpret_cast<const T*>(P2), 15770b57cec5SDimitry Andric *reinterpret_cast<const T*>(P1))) 15780b57cec5SDimitry Andric return 1; 15790b57cec5SDimitry Andric return 0; 15800b57cec5SDimitry Andric } 15810b57cec5SDimitry Andric 15820b57cec5SDimitry Andric /// get_array_pod_sort_comparator - This is an internal helper function used to 15830b57cec5SDimitry Andric /// get type deduction of T right. 15840b57cec5SDimitry Andric template<typename T> 15850b57cec5SDimitry Andric inline int (*get_array_pod_sort_comparator(const T &)) 15860b57cec5SDimitry Andric (const void*, const void*) { 15870b57cec5SDimitry Andric return array_pod_sort_comparator<T>; 15880b57cec5SDimitry Andric } 15890b57cec5SDimitry Andric 1590480093f4SDimitry Andric #ifdef EXPENSIVE_CHECKS 1591480093f4SDimitry Andric namespace detail { 1592480093f4SDimitry Andric 1593480093f4SDimitry Andric inline unsigned presortShuffleEntropy() { 1594480093f4SDimitry Andric static unsigned Result(std::random_device{}()); 1595480093f4SDimitry Andric return Result; 1596480093f4SDimitry Andric } 1597480093f4SDimitry Andric 1598480093f4SDimitry Andric template <class IteratorTy> 1599480093f4SDimitry Andric inline void presortShuffle(IteratorTy Start, IteratorTy End) { 1600480093f4SDimitry Andric std::mt19937 Generator(presortShuffleEntropy()); 1601fe6060f1SDimitry Andric llvm::shuffle(Start, End, Generator); 1602480093f4SDimitry Andric } 1603480093f4SDimitry Andric 1604480093f4SDimitry Andric } // end namespace detail 1605480093f4SDimitry Andric #endif 1606480093f4SDimitry Andric 16070b57cec5SDimitry Andric /// array_pod_sort - This sorts an array with the specified start and end 16080b57cec5SDimitry Andric /// extent. This is just like std::sort, except that it calls qsort instead of 16090b57cec5SDimitry Andric /// using an inlined template. qsort is slightly slower than std::sort, but 16100b57cec5SDimitry Andric /// most sorts are not performance critical in LLVM and std::sort has to be 16110b57cec5SDimitry Andric /// template instantiated for each type, leading to significant measured code 16120b57cec5SDimitry Andric /// bloat. This function should generally be used instead of std::sort where 16130b57cec5SDimitry Andric /// possible. 16140b57cec5SDimitry Andric /// 16150b57cec5SDimitry Andric /// This function assumes that you have simple POD-like types that can be 16160b57cec5SDimitry Andric /// compared with std::less and can be moved with memcpy. If this isn't true, 16170b57cec5SDimitry Andric /// you should use std::sort. 16180b57cec5SDimitry Andric /// 16190b57cec5SDimitry Andric /// NOTE: If qsort_r were portable, we could allow a custom comparator and 16200b57cec5SDimitry Andric /// default to std::less. 16210b57cec5SDimitry Andric template<class IteratorTy> 16220b57cec5SDimitry Andric inline void array_pod_sort(IteratorTy Start, IteratorTy End) { 16230b57cec5SDimitry Andric // Don't inefficiently call qsort with one element or trigger undefined 16240b57cec5SDimitry Andric // behavior with an empty sequence. 16250b57cec5SDimitry Andric auto NElts = End - Start; 16260b57cec5SDimitry Andric if (NElts <= 1) return; 16270b57cec5SDimitry Andric #ifdef EXPENSIVE_CHECKS 1628480093f4SDimitry Andric detail::presortShuffle<IteratorTy>(Start, End); 16290b57cec5SDimitry Andric #endif 16300b57cec5SDimitry Andric qsort(&*Start, NElts, sizeof(*Start), get_array_pod_sort_comparator(*Start)); 16310b57cec5SDimitry Andric } 16320b57cec5SDimitry Andric 16330b57cec5SDimitry Andric template <class IteratorTy> 16340b57cec5SDimitry Andric inline void array_pod_sort( 16350b57cec5SDimitry Andric IteratorTy Start, IteratorTy End, 16360b57cec5SDimitry Andric int (*Compare)( 16370b57cec5SDimitry Andric const typename std::iterator_traits<IteratorTy>::value_type *, 16380b57cec5SDimitry Andric const typename std::iterator_traits<IteratorTy>::value_type *)) { 16390b57cec5SDimitry Andric // Don't inefficiently call qsort with one element or trigger undefined 16400b57cec5SDimitry Andric // behavior with an empty sequence. 16410b57cec5SDimitry Andric auto NElts = End - Start; 16420b57cec5SDimitry Andric if (NElts <= 1) return; 16430b57cec5SDimitry Andric #ifdef EXPENSIVE_CHECKS 1644480093f4SDimitry Andric detail::presortShuffle<IteratorTy>(Start, End); 16450b57cec5SDimitry Andric #endif 16460b57cec5SDimitry Andric qsort(&*Start, NElts, sizeof(*Start), 16470b57cec5SDimitry Andric reinterpret_cast<int (*)(const void *, const void *)>(Compare)); 16480b57cec5SDimitry Andric } 16490b57cec5SDimitry Andric 16505ffd83dbSDimitry Andric namespace detail { 16515ffd83dbSDimitry Andric template <typename T> 16525ffd83dbSDimitry Andric // We can use qsort if the iterator type is a pointer and the underlying value 16535ffd83dbSDimitry Andric // is trivially copyable. 1654bdd1243dSDimitry Andric using sort_trivially_copyable = std::conjunction< 16555ffd83dbSDimitry Andric std::is_pointer<T>, 1656e8d8bef9SDimitry Andric std::is_trivially_copyable<typename std::iterator_traits<T>::value_type>>; 16575ffd83dbSDimitry Andric } // namespace detail 16585ffd83dbSDimitry Andric 16590b57cec5SDimitry Andric // Provide wrappers to std::sort which shuffle the elements before sorting 16600b57cec5SDimitry Andric // to help uncover non-deterministic behavior (PR35135). 1661bdd1243dSDimitry Andric template <typename IteratorTy> 16620b57cec5SDimitry Andric inline void sort(IteratorTy Start, IteratorTy End) { 1663bdd1243dSDimitry Andric if constexpr (detail::sort_trivially_copyable<IteratorTy>::value) { 1664bdd1243dSDimitry Andric // Forward trivially copyable types to array_pod_sort. This avoids a large 1665bdd1243dSDimitry Andric // amount of code bloat for a minor performance hit. 1666bdd1243dSDimitry Andric array_pod_sort(Start, End); 1667bdd1243dSDimitry Andric } else { 16680b57cec5SDimitry Andric #ifdef EXPENSIVE_CHECKS 1669480093f4SDimitry Andric detail::presortShuffle<IteratorTy>(Start, End); 16700b57cec5SDimitry Andric #endif 16710b57cec5SDimitry Andric std::sort(Start, End); 16720b57cec5SDimitry Andric } 16735ffd83dbSDimitry Andric } 16745ffd83dbSDimitry Andric 16750b57cec5SDimitry Andric template <typename Container> inline void sort(Container &&C) { 16760b57cec5SDimitry Andric llvm::sort(adl_begin(C), adl_end(C)); 16770b57cec5SDimitry Andric } 16780b57cec5SDimitry Andric 16790b57cec5SDimitry Andric template <typename IteratorTy, typename Compare> 16800b57cec5SDimitry Andric inline void sort(IteratorTy Start, IteratorTy End, Compare Comp) { 16810b57cec5SDimitry Andric #ifdef EXPENSIVE_CHECKS 1682480093f4SDimitry Andric detail::presortShuffle<IteratorTy>(Start, End); 16830b57cec5SDimitry Andric #endif 16840b57cec5SDimitry Andric std::sort(Start, End, Comp); 16850b57cec5SDimitry Andric } 16860b57cec5SDimitry Andric 16870b57cec5SDimitry Andric template <typename Container, typename Compare> 16880b57cec5SDimitry Andric inline void sort(Container &&C, Compare Comp) { 16890b57cec5SDimitry Andric llvm::sort(adl_begin(C), adl_end(C), Comp); 16900b57cec5SDimitry Andric } 16910b57cec5SDimitry Andric 16920b57cec5SDimitry Andric /// Get the size of a range. This is a wrapper function around std::distance 16930b57cec5SDimitry Andric /// which is only enabled when the operation is O(1). 16940b57cec5SDimitry Andric template <typename R> 16955ffd83dbSDimitry Andric auto size(R &&Range, 1696e8d8bef9SDimitry Andric std::enable_if_t< 1697e8d8bef9SDimitry Andric std::is_base_of<std::random_access_iterator_tag, 1698e8d8bef9SDimitry Andric typename std::iterator_traits<decltype( 1699e8d8bef9SDimitry Andric Range.begin())>::iterator_category>::value, 17005ffd83dbSDimitry Andric void> * = nullptr) { 17010b57cec5SDimitry Andric return std::distance(Range.begin(), Range.end()); 17020b57cec5SDimitry Andric } 17030b57cec5SDimitry Andric 1704*06c3fb27SDimitry Andric namespace detail { 1705*06c3fb27SDimitry Andric template <typename Range> 1706*06c3fb27SDimitry Andric using check_has_free_function_size = 1707*06c3fb27SDimitry Andric decltype(adl_size(std::declval<Range &>())); 1708*06c3fb27SDimitry Andric 1709*06c3fb27SDimitry Andric template <typename Range> 1710*06c3fb27SDimitry Andric static constexpr bool HasFreeFunctionSize = 1711*06c3fb27SDimitry Andric is_detected<check_has_free_function_size, Range>::value; 1712*06c3fb27SDimitry Andric } // namespace detail 1713*06c3fb27SDimitry Andric 1714*06c3fb27SDimitry Andric /// Returns the size of the \p Range, i.e., the number of elements. This 1715*06c3fb27SDimitry Andric /// implementation takes inspiration from `std::ranges::size` from C++20 and 1716*06c3fb27SDimitry Andric /// delegates the size check to `adl_size` or `std::distance`, in this order of 1717*06c3fb27SDimitry Andric /// preference. Unlike `llvm::size`, this function does *not* guarantee O(1) 1718*06c3fb27SDimitry Andric /// running time, and is intended to be used in generic code that does not know 1719*06c3fb27SDimitry Andric /// the exact range type. 1720*06c3fb27SDimitry Andric template <typename R> constexpr size_t range_size(R &&Range) { 1721*06c3fb27SDimitry Andric if constexpr (detail::HasFreeFunctionSize<R>) 1722*06c3fb27SDimitry Andric return adl_size(Range); 1723*06c3fb27SDimitry Andric else 1724*06c3fb27SDimitry Andric return static_cast<size_t>(std::distance(adl_begin(Range), adl_end(Range))); 1725*06c3fb27SDimitry Andric } 1726*06c3fb27SDimitry Andric 17270b57cec5SDimitry Andric /// Provide wrappers to std::for_each which take ranges instead of having to 17280b57cec5SDimitry Andric /// pass begin/end explicitly. 1729e8d8bef9SDimitry Andric template <typename R, typename UnaryFunction> 1730e8d8bef9SDimitry Andric UnaryFunction for_each(R &&Range, UnaryFunction F) { 1731e8d8bef9SDimitry Andric return std::for_each(adl_begin(Range), adl_end(Range), F); 17320b57cec5SDimitry Andric } 17330b57cec5SDimitry Andric 17340b57cec5SDimitry Andric /// Provide wrappers to std::all_of which take ranges instead of having to pass 17350b57cec5SDimitry Andric /// begin/end explicitly. 17360b57cec5SDimitry Andric template <typename R, typename UnaryPredicate> 17370b57cec5SDimitry Andric bool all_of(R &&Range, UnaryPredicate P) { 17380b57cec5SDimitry Andric return std::all_of(adl_begin(Range), adl_end(Range), P); 17390b57cec5SDimitry Andric } 17400b57cec5SDimitry Andric 17410b57cec5SDimitry Andric /// Provide wrappers to std::any_of which take ranges instead of having to pass 17420b57cec5SDimitry Andric /// begin/end explicitly. 17430b57cec5SDimitry Andric template <typename R, typename UnaryPredicate> 17440b57cec5SDimitry Andric bool any_of(R &&Range, UnaryPredicate P) { 17450b57cec5SDimitry Andric return std::any_of(adl_begin(Range), adl_end(Range), P); 17460b57cec5SDimitry Andric } 17470b57cec5SDimitry Andric 17480b57cec5SDimitry Andric /// Provide wrappers to std::none_of which take ranges instead of having to pass 17490b57cec5SDimitry Andric /// begin/end explicitly. 17500b57cec5SDimitry Andric template <typename R, typename UnaryPredicate> 17510b57cec5SDimitry Andric bool none_of(R &&Range, UnaryPredicate P) { 17520b57cec5SDimitry Andric return std::none_of(adl_begin(Range), adl_end(Range), P); 17530b57cec5SDimitry Andric } 17540b57cec5SDimitry Andric 17550b57cec5SDimitry Andric /// Provide wrappers to std::find which take ranges instead of having to pass 17560b57cec5SDimitry Andric /// begin/end explicitly. 17575ffd83dbSDimitry Andric template <typename R, typename T> auto find(R &&Range, const T &Val) { 17580b57cec5SDimitry Andric return std::find(adl_begin(Range), adl_end(Range), Val); 17590b57cec5SDimitry Andric } 17600b57cec5SDimitry Andric 17610b57cec5SDimitry Andric /// Provide wrappers to std::find_if which take ranges instead of having to pass 17620b57cec5SDimitry Andric /// begin/end explicitly. 17630b57cec5SDimitry Andric template <typename R, typename UnaryPredicate> 17645ffd83dbSDimitry Andric auto find_if(R &&Range, UnaryPredicate P) { 17650b57cec5SDimitry Andric return std::find_if(adl_begin(Range), adl_end(Range), P); 17660b57cec5SDimitry Andric } 17670b57cec5SDimitry Andric 17680b57cec5SDimitry Andric template <typename R, typename UnaryPredicate> 17695ffd83dbSDimitry Andric auto find_if_not(R &&Range, UnaryPredicate P) { 17700b57cec5SDimitry Andric return std::find_if_not(adl_begin(Range), adl_end(Range), P); 17710b57cec5SDimitry Andric } 17720b57cec5SDimitry Andric 17730b57cec5SDimitry Andric /// Provide wrappers to std::remove_if which take ranges instead of having to 17740b57cec5SDimitry Andric /// pass begin/end explicitly. 17750b57cec5SDimitry Andric template <typename R, typename UnaryPredicate> 17765ffd83dbSDimitry Andric auto remove_if(R &&Range, UnaryPredicate P) { 17770b57cec5SDimitry Andric return std::remove_if(adl_begin(Range), adl_end(Range), P); 17780b57cec5SDimitry Andric } 17790b57cec5SDimitry Andric 17800b57cec5SDimitry Andric /// Provide wrappers to std::copy_if which take ranges instead of having to 17810b57cec5SDimitry Andric /// pass begin/end explicitly. 17820b57cec5SDimitry Andric template <typename R, typename OutputIt, typename UnaryPredicate> 17830b57cec5SDimitry Andric OutputIt copy_if(R &&Range, OutputIt Out, UnaryPredicate P) { 17840b57cec5SDimitry Andric return std::copy_if(adl_begin(Range), adl_end(Range), Out, P); 17850b57cec5SDimitry Andric } 17860b57cec5SDimitry Andric 1787bdd1243dSDimitry Andric /// Return the single value in \p Range that satisfies 1788bdd1243dSDimitry Andric /// \p P(<member of \p Range> *, AllowRepeats)->T * returning nullptr 1789bdd1243dSDimitry Andric /// when no values or multiple values were found. 1790bdd1243dSDimitry Andric /// When \p AllowRepeats is true, multiple values that compare equal 1791bdd1243dSDimitry Andric /// are allowed. 1792bdd1243dSDimitry Andric template <typename T, typename R, typename Predicate> 1793bdd1243dSDimitry Andric T *find_singleton(R &&Range, Predicate P, bool AllowRepeats = false) { 1794bdd1243dSDimitry Andric T *RC = nullptr; 1795bdd1243dSDimitry Andric for (auto *A : Range) { 1796bdd1243dSDimitry Andric if (T *PRC = P(A, AllowRepeats)) { 1797bdd1243dSDimitry Andric if (RC) { 1798bdd1243dSDimitry Andric if (!AllowRepeats || PRC != RC) 1799bdd1243dSDimitry Andric return nullptr; 1800bdd1243dSDimitry Andric } else 1801bdd1243dSDimitry Andric RC = PRC; 1802bdd1243dSDimitry Andric } 1803bdd1243dSDimitry Andric } 1804bdd1243dSDimitry Andric return RC; 1805bdd1243dSDimitry Andric } 1806bdd1243dSDimitry Andric 1807bdd1243dSDimitry Andric /// Return a pair consisting of the single value in \p Range that satisfies 1808bdd1243dSDimitry Andric /// \p P(<member of \p Range> *, AllowRepeats)->std::pair<T*, bool> returning 1809bdd1243dSDimitry Andric /// nullptr when no values or multiple values were found, and a bool indicating 1810bdd1243dSDimitry Andric /// whether multiple values were found to cause the nullptr. 1811bdd1243dSDimitry Andric /// When \p AllowRepeats is true, multiple values that compare equal are 1812bdd1243dSDimitry Andric /// allowed. The predicate \p P returns a pair<T *, bool> where T is the 1813bdd1243dSDimitry Andric /// singleton while the bool indicates whether multiples have already been 1814bdd1243dSDimitry Andric /// found. It is expected that first will be nullptr when second is true. 1815bdd1243dSDimitry Andric /// This allows using find_singleton_nested within the predicate \P. 1816bdd1243dSDimitry Andric template <typename T, typename R, typename Predicate> 1817bdd1243dSDimitry Andric std::pair<T *, bool> find_singleton_nested(R &&Range, Predicate P, 1818bdd1243dSDimitry Andric bool AllowRepeats = false) { 1819bdd1243dSDimitry Andric T *RC = nullptr; 1820bdd1243dSDimitry Andric for (auto *A : Range) { 1821bdd1243dSDimitry Andric std::pair<T *, bool> PRC = P(A, AllowRepeats); 1822bdd1243dSDimitry Andric if (PRC.second) { 1823bdd1243dSDimitry Andric assert(PRC.first == nullptr && 1824bdd1243dSDimitry Andric "Inconsistent return values in find_singleton_nested."); 1825bdd1243dSDimitry Andric return PRC; 1826bdd1243dSDimitry Andric } 1827bdd1243dSDimitry Andric if (PRC.first) { 1828bdd1243dSDimitry Andric if (RC) { 1829bdd1243dSDimitry Andric if (!AllowRepeats || PRC.first != RC) 1830bdd1243dSDimitry Andric return {nullptr, true}; 1831bdd1243dSDimitry Andric } else 1832bdd1243dSDimitry Andric RC = PRC.first; 1833bdd1243dSDimitry Andric } 1834bdd1243dSDimitry Andric } 1835bdd1243dSDimitry Andric return {RC, false}; 1836bdd1243dSDimitry Andric } 1837bdd1243dSDimitry Andric 18380b57cec5SDimitry Andric template <typename R, typename OutputIt> 18390b57cec5SDimitry Andric OutputIt copy(R &&Range, OutputIt Out) { 18400b57cec5SDimitry Andric return std::copy(adl_begin(Range), adl_end(Range), Out); 18410b57cec5SDimitry Andric } 18420b57cec5SDimitry Andric 1843bdd1243dSDimitry Andric /// Provide wrappers to std::replace_copy_if which take ranges instead of having 1844bdd1243dSDimitry Andric /// to pass begin/end explicitly. 1845bdd1243dSDimitry Andric template <typename R, typename OutputIt, typename UnaryPredicate, typename T> 1846bdd1243dSDimitry Andric OutputIt replace_copy_if(R &&Range, OutputIt Out, UnaryPredicate P, 1847bdd1243dSDimitry Andric const T &NewValue) { 1848bdd1243dSDimitry Andric return std::replace_copy_if(adl_begin(Range), adl_end(Range), Out, P, 1849bdd1243dSDimitry Andric NewValue); 1850bdd1243dSDimitry Andric } 1851bdd1243dSDimitry Andric 1852bdd1243dSDimitry Andric /// Provide wrappers to std::replace_copy which take ranges instead of having to 1853bdd1243dSDimitry Andric /// pass begin/end explicitly. 1854bdd1243dSDimitry Andric template <typename R, typename OutputIt, typename T> 1855bdd1243dSDimitry Andric OutputIt replace_copy(R &&Range, OutputIt Out, const T &OldValue, 1856bdd1243dSDimitry Andric const T &NewValue) { 1857bdd1243dSDimitry Andric return std::replace_copy(adl_begin(Range), adl_end(Range), Out, OldValue, 1858bdd1243dSDimitry Andric NewValue); 1859bdd1243dSDimitry Andric } 1860bdd1243dSDimitry Andric 1861e8d8bef9SDimitry Andric /// Provide wrappers to std::move which take ranges instead of having to 1862e8d8bef9SDimitry Andric /// pass begin/end explicitly. 1863e8d8bef9SDimitry Andric template <typename R, typename OutputIt> 1864e8d8bef9SDimitry Andric OutputIt move(R &&Range, OutputIt Out) { 1865e8d8bef9SDimitry Andric return std::move(adl_begin(Range), adl_end(Range), Out); 1866e8d8bef9SDimitry Andric } 1867e8d8bef9SDimitry Andric 1868*06c3fb27SDimitry Andric namespace detail { 1869*06c3fb27SDimitry Andric template <typename Range, typename Element> 1870*06c3fb27SDimitry Andric using check_has_member_contains_t = 1871*06c3fb27SDimitry Andric decltype(std::declval<Range &>().contains(std::declval<const Element &>())); 1872*06c3fb27SDimitry Andric 1873*06c3fb27SDimitry Andric template <typename Range, typename Element> 1874*06c3fb27SDimitry Andric static constexpr bool HasMemberContains = 1875*06c3fb27SDimitry Andric is_detected<check_has_member_contains_t, Range, Element>::value; 1876*06c3fb27SDimitry Andric 1877*06c3fb27SDimitry Andric template <typename Range, typename Element> 1878*06c3fb27SDimitry Andric using check_has_member_find_t = 1879*06c3fb27SDimitry Andric decltype(std::declval<Range &>().find(std::declval<const Element &>()) != 1880*06c3fb27SDimitry Andric std::declval<Range &>().end()); 1881*06c3fb27SDimitry Andric 1882*06c3fb27SDimitry Andric template <typename Range, typename Element> 1883*06c3fb27SDimitry Andric static constexpr bool HasMemberFind = 1884*06c3fb27SDimitry Andric is_detected<check_has_member_find_t, Range, Element>::value; 1885*06c3fb27SDimitry Andric 1886*06c3fb27SDimitry Andric } // namespace detail 1887*06c3fb27SDimitry Andric 1888*06c3fb27SDimitry Andric /// Returns true if \p Element is found in \p Range. Delegates the check to 1889*06c3fb27SDimitry Andric /// either `.contains(Element)`, `.find(Element)`, or `std::find`, in this 1890*06c3fb27SDimitry Andric /// order of preference. This is intended as the canonical way to check if an 1891*06c3fb27SDimitry Andric /// element exists in a range in generic code or range type that does not 1892*06c3fb27SDimitry Andric /// expose a `.contains(Element)` member. 18930b57cec5SDimitry Andric template <typename R, typename E> 18940b57cec5SDimitry Andric bool is_contained(R &&Range, const E &Element) { 1895*06c3fb27SDimitry Andric if constexpr (detail::HasMemberContains<R, E>) 1896*06c3fb27SDimitry Andric return Range.contains(Element); 1897*06c3fb27SDimitry Andric else if constexpr (detail::HasMemberFind<R, E>) 1898*06c3fb27SDimitry Andric return Range.find(Element) != Range.end(); 1899*06c3fb27SDimitry Andric else 1900*06c3fb27SDimitry Andric return std::find(adl_begin(Range), adl_end(Range), Element) != 1901*06c3fb27SDimitry Andric adl_end(Range); 19020b57cec5SDimitry Andric } 19030b57cec5SDimitry Andric 1904*06c3fb27SDimitry Andric /// Returns true iff \p Element exists in \p Set. This overload takes \p Set as 1905*06c3fb27SDimitry Andric /// an initializer list and is `constexpr`-friendly. 1906*06c3fb27SDimitry Andric template <typename T, typename E> 1907*06c3fb27SDimitry Andric constexpr bool is_contained(std::initializer_list<T> Set, const E &Element) { 190881ad6265SDimitry Andric // TODO: Use std::find when we switch to C++20. 1909*06c3fb27SDimitry Andric for (const T &V : Set) 1910*06c3fb27SDimitry Andric if (V == Element) 191181ad6265SDimitry Andric return true; 191281ad6265SDimitry Andric return false; 191381ad6265SDimitry Andric } 191481ad6265SDimitry Andric 19155ffd83dbSDimitry Andric /// Wrapper function around std::is_sorted to check if elements in a range \p R 19165ffd83dbSDimitry Andric /// are sorted with respect to a comparator \p C. 19175ffd83dbSDimitry Andric template <typename R, typename Compare> bool is_sorted(R &&Range, Compare C) { 19185ffd83dbSDimitry Andric return std::is_sorted(adl_begin(Range), adl_end(Range), C); 19195ffd83dbSDimitry Andric } 19205ffd83dbSDimitry Andric 19215ffd83dbSDimitry Andric /// Wrapper function around std::is_sorted to check if elements in a range \p R 19225ffd83dbSDimitry Andric /// are sorted in non-descending order. 19235ffd83dbSDimitry Andric template <typename R> bool is_sorted(R &&Range) { 19245ffd83dbSDimitry Andric return std::is_sorted(adl_begin(Range), adl_end(Range)); 19255ffd83dbSDimitry Andric } 19265ffd83dbSDimitry Andric 19270b57cec5SDimitry Andric /// Wrapper function around std::count to count the number of times an element 19280b57cec5SDimitry Andric /// \p Element occurs in the given range \p Range. 19295ffd83dbSDimitry Andric template <typename R, typename E> auto count(R &&Range, const E &Element) { 19300b57cec5SDimitry Andric return std::count(adl_begin(Range), adl_end(Range), Element); 19310b57cec5SDimitry Andric } 19320b57cec5SDimitry Andric 19330b57cec5SDimitry Andric /// Wrapper function around std::count_if to count the number of times an 19340b57cec5SDimitry Andric /// element satisfying a given predicate occurs in a range. 19350b57cec5SDimitry Andric template <typename R, typename UnaryPredicate> 19365ffd83dbSDimitry Andric auto count_if(R &&Range, UnaryPredicate P) { 19370b57cec5SDimitry Andric return std::count_if(adl_begin(Range), adl_end(Range), P); 19380b57cec5SDimitry Andric } 19390b57cec5SDimitry Andric 19400b57cec5SDimitry Andric /// Wrapper function around std::transform to apply a function to a range and 19410b57cec5SDimitry Andric /// store the result elsewhere. 1942e8d8bef9SDimitry Andric template <typename R, typename OutputIt, typename UnaryFunction> 1943e8d8bef9SDimitry Andric OutputIt transform(R &&Range, OutputIt d_first, UnaryFunction F) { 1944e8d8bef9SDimitry Andric return std::transform(adl_begin(Range), adl_end(Range), d_first, F); 19450b57cec5SDimitry Andric } 19460b57cec5SDimitry Andric 19470b57cec5SDimitry Andric /// Provide wrappers to std::partition which take ranges instead of having to 19480b57cec5SDimitry Andric /// pass begin/end explicitly. 19490b57cec5SDimitry Andric template <typename R, typename UnaryPredicate> 19505ffd83dbSDimitry Andric auto partition(R &&Range, UnaryPredicate P) { 19510b57cec5SDimitry Andric return std::partition(adl_begin(Range), adl_end(Range), P); 19520b57cec5SDimitry Andric } 19530b57cec5SDimitry Andric 19540b57cec5SDimitry Andric /// Provide wrappers to std::lower_bound which take ranges instead of having to 19550b57cec5SDimitry Andric /// pass begin/end explicitly. 19565ffd83dbSDimitry Andric template <typename R, typename T> auto lower_bound(R &&Range, T &&Value) { 19570b57cec5SDimitry Andric return std::lower_bound(adl_begin(Range), adl_end(Range), 19580b57cec5SDimitry Andric std::forward<T>(Value)); 19590b57cec5SDimitry Andric } 19600b57cec5SDimitry Andric 19610b57cec5SDimitry Andric template <typename R, typename T, typename Compare> 19625ffd83dbSDimitry Andric auto lower_bound(R &&Range, T &&Value, Compare C) { 19630b57cec5SDimitry Andric return std::lower_bound(adl_begin(Range), adl_end(Range), 19640b57cec5SDimitry Andric std::forward<T>(Value), C); 19650b57cec5SDimitry Andric } 19660b57cec5SDimitry Andric 19670b57cec5SDimitry Andric /// Provide wrappers to std::upper_bound which take ranges instead of having to 19680b57cec5SDimitry Andric /// pass begin/end explicitly. 19695ffd83dbSDimitry Andric template <typename R, typename T> auto upper_bound(R &&Range, T &&Value) { 19700b57cec5SDimitry Andric return std::upper_bound(adl_begin(Range), adl_end(Range), 19710b57cec5SDimitry Andric std::forward<T>(Value)); 19720b57cec5SDimitry Andric } 19730b57cec5SDimitry Andric 19740b57cec5SDimitry Andric template <typename R, typename T, typename Compare> 19755ffd83dbSDimitry Andric auto upper_bound(R &&Range, T &&Value, Compare C) { 19760b57cec5SDimitry Andric return std::upper_bound(adl_begin(Range), adl_end(Range), 19770b57cec5SDimitry Andric std::forward<T>(Value), C); 19780b57cec5SDimitry Andric } 19790b57cec5SDimitry Andric 19800b57cec5SDimitry Andric template <typename R> 19810b57cec5SDimitry Andric void stable_sort(R &&Range) { 19820b57cec5SDimitry Andric std::stable_sort(adl_begin(Range), adl_end(Range)); 19830b57cec5SDimitry Andric } 19840b57cec5SDimitry Andric 19850b57cec5SDimitry Andric template <typename R, typename Compare> 19860b57cec5SDimitry Andric void stable_sort(R &&Range, Compare C) { 19870b57cec5SDimitry Andric std::stable_sort(adl_begin(Range), adl_end(Range), C); 19880b57cec5SDimitry Andric } 19890b57cec5SDimitry Andric 19900b57cec5SDimitry Andric /// Binary search for the first iterator in a range where a predicate is false. 19910b57cec5SDimitry Andric /// Requires that C is always true below some limit, and always false above it. 19920b57cec5SDimitry Andric template <typename R, typename Predicate, 19930b57cec5SDimitry Andric typename Val = decltype(*adl_begin(std::declval<R>()))> 19945ffd83dbSDimitry Andric auto partition_point(R &&Range, Predicate P) { 19950b57cec5SDimitry Andric return std::partition_point(adl_begin(Range), adl_end(Range), P); 19960b57cec5SDimitry Andric } 19970b57cec5SDimitry Andric 1998fe6060f1SDimitry Andric template<typename Range, typename Predicate> 1999fe6060f1SDimitry Andric auto unique(Range &&R, Predicate P) { 2000fe6060f1SDimitry Andric return std::unique(adl_begin(R), adl_end(R), P); 2001fe6060f1SDimitry Andric } 2002fe6060f1SDimitry Andric 2003fe6060f1SDimitry Andric /// Wrapper function around std::equal to detect if pair-wise elements between 2004fe6060f1SDimitry Andric /// two ranges are the same. 2005fe6060f1SDimitry Andric template <typename L, typename R> bool equal(L &&LRange, R &&RRange) { 2006fe6060f1SDimitry Andric return std::equal(adl_begin(LRange), adl_end(LRange), adl_begin(RRange), 2007fe6060f1SDimitry Andric adl_end(RRange)); 2008fe6060f1SDimitry Andric } 2009fe6060f1SDimitry Andric 2010bdd1243dSDimitry Andric /// Returns true if all elements in Range are equal or when the Range is empty. 2011bdd1243dSDimitry Andric template <typename R> bool all_equal(R &&Range) { 2012bdd1243dSDimitry Andric auto Begin = adl_begin(Range); 2013bdd1243dSDimitry Andric auto End = adl_end(Range); 2014bdd1243dSDimitry Andric return Begin == End || std::equal(Begin + 1, End, Begin); 2015bdd1243dSDimitry Andric } 2016bdd1243dSDimitry Andric 2017bdd1243dSDimitry Andric /// Returns true if all Values in the initializer lists are equal or the list 2018bdd1243dSDimitry Andric // is empty. 2019bdd1243dSDimitry Andric template <typename T> bool all_equal(std::initializer_list<T> Values) { 2020bdd1243dSDimitry Andric return all_equal<std::initializer_list<T>>(std::move(Values)); 20210b57cec5SDimitry Andric } 20220b57cec5SDimitry Andric 20230b57cec5SDimitry Andric /// Provide a container algorithm similar to C++ Library Fundamentals v2's 20240b57cec5SDimitry Andric /// `erase_if` which is equivalent to: 20250b57cec5SDimitry Andric /// 20260b57cec5SDimitry Andric /// C.erase(remove_if(C, pred), C.end()); 20270b57cec5SDimitry Andric /// 20280b57cec5SDimitry Andric /// This version works for any container with an erase method call accepting 20290b57cec5SDimitry Andric /// two iterators. 20300b57cec5SDimitry Andric template <typename Container, typename UnaryPredicate> 20310b57cec5SDimitry Andric void erase_if(Container &C, UnaryPredicate P) { 20320b57cec5SDimitry Andric C.erase(remove_if(C, P), C.end()); 20330b57cec5SDimitry Andric } 20340b57cec5SDimitry Andric 2035e8d8bef9SDimitry Andric /// Wrapper function to remove a value from a container: 2036e8d8bef9SDimitry Andric /// 2037e8d8bef9SDimitry Andric /// C.erase(remove(C.begin(), C.end(), V), C.end()); 2038e8d8bef9SDimitry Andric template <typename Container, typename ValueType> 2039e8d8bef9SDimitry Andric void erase_value(Container &C, ValueType V) { 2040e8d8bef9SDimitry Andric C.erase(std::remove(C.begin(), C.end(), V), C.end()); 2041e8d8bef9SDimitry Andric } 2042e8d8bef9SDimitry Andric 2043e8d8bef9SDimitry Andric /// Wrapper function to append a range to a container. 2044e8d8bef9SDimitry Andric /// 2045e8d8bef9SDimitry Andric /// C.insert(C.end(), R.begin(), R.end()); 2046e8d8bef9SDimitry Andric template <typename Container, typename Range> 2047e8d8bef9SDimitry Andric inline void append_range(Container &C, Range &&R) { 2048*06c3fb27SDimitry Andric C.insert(C.end(), adl_begin(R), adl_end(R)); 2049e8d8bef9SDimitry Andric } 2050e8d8bef9SDimitry Andric 20510b57cec5SDimitry Andric /// Given a sequence container Cont, replace the range [ContIt, ContEnd) with 20520b57cec5SDimitry Andric /// the range [ValIt, ValEnd) (which is not from the same container). 20530b57cec5SDimitry Andric template<typename Container, typename RandomAccessIterator> 20540b57cec5SDimitry Andric void replace(Container &Cont, typename Container::iterator ContIt, 20550b57cec5SDimitry Andric typename Container::iterator ContEnd, RandomAccessIterator ValIt, 20560b57cec5SDimitry Andric RandomAccessIterator ValEnd) { 20570b57cec5SDimitry Andric while (true) { 20580b57cec5SDimitry Andric if (ValIt == ValEnd) { 20590b57cec5SDimitry Andric Cont.erase(ContIt, ContEnd); 20600b57cec5SDimitry Andric return; 20610b57cec5SDimitry Andric } else if (ContIt == ContEnd) { 20620b57cec5SDimitry Andric Cont.insert(ContIt, ValIt, ValEnd); 20630b57cec5SDimitry Andric return; 20640b57cec5SDimitry Andric } 20650b57cec5SDimitry Andric *ContIt++ = *ValIt++; 20660b57cec5SDimitry Andric } 20670b57cec5SDimitry Andric } 20680b57cec5SDimitry Andric 20690b57cec5SDimitry Andric /// Given a sequence container Cont, replace the range [ContIt, ContEnd) with 20700b57cec5SDimitry Andric /// the range R. 20710b57cec5SDimitry Andric template<typename Container, typename Range = std::initializer_list< 20720b57cec5SDimitry Andric typename Container::value_type>> 20730b57cec5SDimitry Andric void replace(Container &Cont, typename Container::iterator ContIt, 20740b57cec5SDimitry Andric typename Container::iterator ContEnd, Range R) { 20750b57cec5SDimitry Andric replace(Cont, ContIt, ContEnd, R.begin(), R.end()); 20760b57cec5SDimitry Andric } 20770b57cec5SDimitry Andric 20785ffd83dbSDimitry Andric /// An STL-style algorithm similar to std::for_each that applies a second 20795ffd83dbSDimitry Andric /// functor between every pair of elements. 20805ffd83dbSDimitry Andric /// 20815ffd83dbSDimitry Andric /// This provides the control flow logic to, for example, print a 20825ffd83dbSDimitry Andric /// comma-separated list: 20835ffd83dbSDimitry Andric /// \code 20845ffd83dbSDimitry Andric /// interleave(names.begin(), names.end(), 20855ffd83dbSDimitry Andric /// [&](StringRef name) { os << name; }, 20865ffd83dbSDimitry Andric /// [&] { os << ", "; }); 20875ffd83dbSDimitry Andric /// \endcode 20885ffd83dbSDimitry Andric template <typename ForwardIterator, typename UnaryFunctor, 20895ffd83dbSDimitry Andric typename NullaryFunctor, 2090bdd1243dSDimitry Andric typename = std::enable_if_t< 20915ffd83dbSDimitry Andric !std::is_constructible<StringRef, UnaryFunctor>::value && 2092bdd1243dSDimitry Andric !std::is_constructible<StringRef, NullaryFunctor>::value>> 20935ffd83dbSDimitry Andric inline void interleave(ForwardIterator begin, ForwardIterator end, 20945ffd83dbSDimitry Andric UnaryFunctor each_fn, NullaryFunctor between_fn) { 20955ffd83dbSDimitry Andric if (begin == end) 20965ffd83dbSDimitry Andric return; 20975ffd83dbSDimitry Andric each_fn(*begin); 20985ffd83dbSDimitry Andric ++begin; 20995ffd83dbSDimitry Andric for (; begin != end; ++begin) { 21005ffd83dbSDimitry Andric between_fn(); 21015ffd83dbSDimitry Andric each_fn(*begin); 21025ffd83dbSDimitry Andric } 21035ffd83dbSDimitry Andric } 21045ffd83dbSDimitry Andric 21055ffd83dbSDimitry Andric template <typename Container, typename UnaryFunctor, typename NullaryFunctor, 2106bdd1243dSDimitry Andric typename = std::enable_if_t< 21075ffd83dbSDimitry Andric !std::is_constructible<StringRef, UnaryFunctor>::value && 2108bdd1243dSDimitry Andric !std::is_constructible<StringRef, NullaryFunctor>::value>> 21095ffd83dbSDimitry Andric inline void interleave(const Container &c, UnaryFunctor each_fn, 21105ffd83dbSDimitry Andric NullaryFunctor between_fn) { 21115ffd83dbSDimitry Andric interleave(c.begin(), c.end(), each_fn, between_fn); 21125ffd83dbSDimitry Andric } 21135ffd83dbSDimitry Andric 21145ffd83dbSDimitry Andric /// Overload of interleave for the common case of string separator. 21155ffd83dbSDimitry Andric template <typename Container, typename UnaryFunctor, typename StreamT, 21165ffd83dbSDimitry Andric typename T = detail::ValueOfRange<Container>> 21175ffd83dbSDimitry Andric inline void interleave(const Container &c, StreamT &os, UnaryFunctor each_fn, 21185ffd83dbSDimitry Andric const StringRef &separator) { 21195ffd83dbSDimitry Andric interleave(c.begin(), c.end(), each_fn, [&] { os << separator; }); 21205ffd83dbSDimitry Andric } 21215ffd83dbSDimitry Andric template <typename Container, typename StreamT, 21225ffd83dbSDimitry Andric typename T = detail::ValueOfRange<Container>> 21235ffd83dbSDimitry Andric inline void interleave(const Container &c, StreamT &os, 21245ffd83dbSDimitry Andric const StringRef &separator) { 21255ffd83dbSDimitry Andric interleave( 21265ffd83dbSDimitry Andric c, os, [&](const T &a) { os << a; }, separator); 21275ffd83dbSDimitry Andric } 21285ffd83dbSDimitry Andric 21295ffd83dbSDimitry Andric template <typename Container, typename UnaryFunctor, typename StreamT, 21305ffd83dbSDimitry Andric typename T = detail::ValueOfRange<Container>> 21315ffd83dbSDimitry Andric inline void interleaveComma(const Container &c, StreamT &os, 21325ffd83dbSDimitry Andric UnaryFunctor each_fn) { 21335ffd83dbSDimitry Andric interleave(c, os, each_fn, ", "); 21345ffd83dbSDimitry Andric } 21355ffd83dbSDimitry Andric template <typename Container, typename StreamT, 21365ffd83dbSDimitry Andric typename T = detail::ValueOfRange<Container>> 21375ffd83dbSDimitry Andric inline void interleaveComma(const Container &c, StreamT &os) { 21385ffd83dbSDimitry Andric interleaveComma(c, os, [&](const T &a) { os << a; }); 21395ffd83dbSDimitry Andric } 21405ffd83dbSDimitry Andric 21410b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 21420b57cec5SDimitry Andric // Extra additions to <memory> 21430b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 21440b57cec5SDimitry Andric 21450b57cec5SDimitry Andric struct FreeDeleter { 21460b57cec5SDimitry Andric void operator()(void* v) { 21470b57cec5SDimitry Andric ::free(v); 21480b57cec5SDimitry Andric } 21490b57cec5SDimitry Andric }; 21500b57cec5SDimitry Andric 21510b57cec5SDimitry Andric template<typename First, typename Second> 21520b57cec5SDimitry Andric struct pair_hash { 21530b57cec5SDimitry Andric size_t operator()(const std::pair<First, Second> &P) const { 21540b57cec5SDimitry Andric return std::hash<First>()(P.first) * 31 + std::hash<Second>()(P.second); 21550b57cec5SDimitry Andric } 21560b57cec5SDimitry Andric }; 21570b57cec5SDimitry Andric 21580b57cec5SDimitry Andric /// Binary functor that adapts to any other binary functor after dereferencing 21590b57cec5SDimitry Andric /// operands. 21600b57cec5SDimitry Andric template <typename T> struct deref { 21610b57cec5SDimitry Andric T func; 21620b57cec5SDimitry Andric 21630b57cec5SDimitry Andric // Could be further improved to cope with non-derivable functors and 21640b57cec5SDimitry Andric // non-binary functors (should be a variadic template member function 21650b57cec5SDimitry Andric // operator()). 21665ffd83dbSDimitry Andric template <typename A, typename B> auto operator()(A &lhs, B &rhs) const { 21670b57cec5SDimitry Andric assert(lhs); 21680b57cec5SDimitry Andric assert(rhs); 21690b57cec5SDimitry Andric return func(*lhs, *rhs); 21700b57cec5SDimitry Andric } 21710b57cec5SDimitry Andric }; 21720b57cec5SDimitry Andric 21730b57cec5SDimitry Andric namespace detail { 21740b57cec5SDimitry Andric 2175*06c3fb27SDimitry Andric /// Tuple-like type for `zip_enumerator` dereference. 2176*06c3fb27SDimitry Andric template <typename... Refs> struct enumerator_result; 21770b57cec5SDimitry Andric 2178*06c3fb27SDimitry Andric template <typename... Iters> 2179*06c3fb27SDimitry Andric using EnumeratorTupleType = enumerator_result<decltype(*declval<Iters>())...>; 21800b57cec5SDimitry Andric 2181*06c3fb27SDimitry Andric /// Zippy iterator that uses the second iterator for comparisons. For the 2182*06c3fb27SDimitry Andric /// increment to be safe, the second range has to be the shortest. 2183*06c3fb27SDimitry Andric /// Returns `enumerator_result` on dereference to provide `.index()` and 2184*06c3fb27SDimitry Andric /// `.value()` member functions. 2185*06c3fb27SDimitry Andric /// Note: Because the dereference operator returns `enumerator_result` as a 2186*06c3fb27SDimitry Andric /// value instead of a reference and does not strictly conform to the C++17's 2187*06c3fb27SDimitry Andric /// definition of forward iterator. However, it satisfies all the 2188*06c3fb27SDimitry Andric /// forward_iterator requirements that the `zip_common` and `zippy` depend on 2189*06c3fb27SDimitry Andric /// and fully conforms to the C++20 definition of forward iterator. 2190*06c3fb27SDimitry Andric /// This is similar to `std::vector<bool>::iterator` that returns bit reference 2191*06c3fb27SDimitry Andric /// wrappers on dereference. 2192*06c3fb27SDimitry Andric template <typename... Iters> 2193*06c3fb27SDimitry Andric struct zip_enumerator : zip_common<zip_enumerator<Iters...>, 2194*06c3fb27SDimitry Andric EnumeratorTupleType<Iters...>, Iters...> { 2195*06c3fb27SDimitry Andric static_assert(sizeof...(Iters) >= 2, "Expected at least two iteratees"); 2196*06c3fb27SDimitry Andric using zip_common<zip_enumerator<Iters...>, EnumeratorTupleType<Iters...>, 2197*06c3fb27SDimitry Andric Iters...>::zip_common; 21980b57cec5SDimitry Andric 2199*06c3fb27SDimitry Andric bool operator==(const zip_enumerator &Other) const { 2200*06c3fb27SDimitry Andric return std::get<1>(this->iterators) == std::get<1>(Other.iterators); 22010b57cec5SDimitry Andric } 22020b57cec5SDimitry Andric }; 22030b57cec5SDimitry Andric 2204*06c3fb27SDimitry Andric template <typename... Refs> struct enumerator_result<std::size_t, Refs...> { 2205*06c3fb27SDimitry Andric static constexpr std::size_t NumRefs = sizeof...(Refs); 2206*06c3fb27SDimitry Andric static_assert(NumRefs != 0); 2207*06c3fb27SDimitry Andric // `NumValues` includes the index. 2208*06c3fb27SDimitry Andric static constexpr std::size_t NumValues = NumRefs + 1; 2209*06c3fb27SDimitry Andric 2210*06c3fb27SDimitry Andric // Tuple type whose element types are references for each `Ref`. 2211*06c3fb27SDimitry Andric using range_reference_tuple = std::tuple<Refs...>; 2212*06c3fb27SDimitry Andric // Tuple type who elements are references to all values, including both 2213*06c3fb27SDimitry Andric // the index and `Refs` reference types. 2214*06c3fb27SDimitry Andric using value_reference_tuple = std::tuple<std::size_t, Refs...>; 2215*06c3fb27SDimitry Andric 2216*06c3fb27SDimitry Andric enumerator_result(std::size_t Index, Refs &&...Rs) 2217*06c3fb27SDimitry Andric : Idx(Index), Storage(std::forward<Refs>(Rs)...) {} 2218*06c3fb27SDimitry Andric 2219*06c3fb27SDimitry Andric /// Returns the 0-based index of the current position within the original 2220*06c3fb27SDimitry Andric /// input range(s). 2221*06c3fb27SDimitry Andric std::size_t index() const { return Idx; } 2222*06c3fb27SDimitry Andric 2223*06c3fb27SDimitry Andric /// Returns the value(s) for the current iterator. This does not include the 2224*06c3fb27SDimitry Andric /// index. 2225*06c3fb27SDimitry Andric decltype(auto) value() const { 2226*06c3fb27SDimitry Andric if constexpr (NumRefs == 1) 2227*06c3fb27SDimitry Andric return std::get<0>(Storage); 2228*06c3fb27SDimitry Andric else 2229*06c3fb27SDimitry Andric return Storage; 2230bdd1243dSDimitry Andric } 2231bdd1243dSDimitry Andric 2232*06c3fb27SDimitry Andric /// Returns the value at index `I`. This case covers the index. 2233*06c3fb27SDimitry Andric template <std::size_t I, typename = std::enable_if_t<I == 0>> 2234*06c3fb27SDimitry Andric friend std::size_t get(const enumerator_result &Result) { 2235*06c3fb27SDimitry Andric return Result.Idx; 22360b57cec5SDimitry Andric } 22370b57cec5SDimitry Andric 2238*06c3fb27SDimitry Andric /// Returns the value at index `I`. This case covers references to the 2239*06c3fb27SDimitry Andric /// iteratees. 2240*06c3fb27SDimitry Andric template <std::size_t I, typename = std::enable_if_t<I != 0>> 2241*06c3fb27SDimitry Andric friend decltype(auto) get(const enumerator_result &Result) { 2242*06c3fb27SDimitry Andric // Note: This is a separate function from the other `get`, instead of an 2243*06c3fb27SDimitry Andric // `if constexpr` case, to work around an MSVC 19.31.31XXX compiler 2244*06c3fb27SDimitry Andric // (Visual Studio 2022 17.1) return type deduction bug. 2245*06c3fb27SDimitry Andric return std::get<I - 1>(Result.Storage); 22460b57cec5SDimitry Andric } 22470b57cec5SDimitry Andric 2248*06c3fb27SDimitry Andric template <typename... Ts> 2249*06c3fb27SDimitry Andric friend bool operator==(const enumerator_result &Result, 2250*06c3fb27SDimitry Andric const std::tuple<std::size_t, Ts...> &Other) { 2251*06c3fb27SDimitry Andric static_assert(NumRefs == sizeof...(Ts), "Size mismatch"); 2252*06c3fb27SDimitry Andric if (Result.Idx != std::get<0>(Other)) 2253*06c3fb27SDimitry Andric return false; 2254*06c3fb27SDimitry Andric return Result.is_value_equal(Other, std::make_index_sequence<NumRefs>{}); 22550b57cec5SDimitry Andric } 22560b57cec5SDimitry Andric 22570b57cec5SDimitry Andric private: 2258*06c3fb27SDimitry Andric template <typename Tuple, std::size_t... Idx> 2259*06c3fb27SDimitry Andric bool is_value_equal(const Tuple &Other, std::index_sequence<Idx...>) const { 2260*06c3fb27SDimitry Andric return ((std::get<Idx>(Storage) == std::get<Idx + 1>(Other)) && ...); 2261*06c3fb27SDimitry Andric } 2262*06c3fb27SDimitry Andric 2263*06c3fb27SDimitry Andric std::size_t Idx; 2264*06c3fb27SDimitry Andric // Make this tuple mutable to avoid casts that obfuscate const-correctness 2265*06c3fb27SDimitry Andric // issues. Const-correctness of references is taken care of by `zippy` that 2266*06c3fb27SDimitry Andric // defines const-non and const iterator types that will propagate down to 2267*06c3fb27SDimitry Andric // `enumerator_result`'s `Refs`. 2268*06c3fb27SDimitry Andric // Note that unlike the results of `zip*` functions, `enumerate`'s result are 2269*06c3fb27SDimitry Andric // supposed to be modifiable even when defined as 2270*06c3fb27SDimitry Andric // `const`. 2271*06c3fb27SDimitry Andric mutable range_reference_tuple Storage; 22720b57cec5SDimitry Andric }; 22730b57cec5SDimitry Andric 2274*06c3fb27SDimitry Andric /// Infinite stream of increasing 0-based `size_t` indices. 2275*06c3fb27SDimitry Andric struct index_stream { 2276*06c3fb27SDimitry Andric struct iterator : iterator_facade_base<iterator, std::forward_iterator_tag, 2277*06c3fb27SDimitry Andric const iterator> { 2278*06c3fb27SDimitry Andric iterator &operator++() { 2279*06c3fb27SDimitry Andric assert(Index != std::numeric_limits<std::size_t>::max() && 2280*06c3fb27SDimitry Andric "Attempting to increment end iterator"); 2281*06c3fb27SDimitry Andric ++Index; 2282*06c3fb27SDimitry Andric return *this; 22834824e7fdSDimitry Andric } 22840b57cec5SDimitry Andric 2285*06c3fb27SDimitry Andric // Note: This dereference operator returns a value instead of a reference 2286*06c3fb27SDimitry Andric // and does not strictly conform to the C++17's definition of forward 2287*06c3fb27SDimitry Andric // iterator. However, it satisfies all the forward_iterator requirements 2288*06c3fb27SDimitry Andric // that the `zip_common` depends on and fully conforms to the C++20 2289*06c3fb27SDimitry Andric // definition of forward iterator. 2290*06c3fb27SDimitry Andric std::size_t operator*() const { return Index; } 2291*06c3fb27SDimitry Andric 2292*06c3fb27SDimitry Andric friend bool operator==(const iterator &Lhs, const iterator &Rhs) { 2293*06c3fb27SDimitry Andric return Lhs.Index == Rhs.Index; 22944824e7fdSDimitry Andric } 22950b57cec5SDimitry Andric 2296*06c3fb27SDimitry Andric std::size_t Index = 0; 2297*06c3fb27SDimitry Andric }; 2298*06c3fb27SDimitry Andric 2299*06c3fb27SDimitry Andric iterator begin() const { return {}; } 2300*06c3fb27SDimitry Andric iterator end() const { 2301*06c3fb27SDimitry Andric // We approximate 'infinity' with the max size_t value, which should be good 2302*06c3fb27SDimitry Andric // enough to index over any container. 2303*06c3fb27SDimitry Andric iterator It; 2304*06c3fb27SDimitry Andric It.Index = std::numeric_limits<std::size_t>::max(); 2305*06c3fb27SDimitry Andric return It; 2306*06c3fb27SDimitry Andric } 23070b57cec5SDimitry Andric }; 23080b57cec5SDimitry Andric 23090b57cec5SDimitry Andric } // end namespace detail 23100b57cec5SDimitry Andric 2311*06c3fb27SDimitry Andric /// Given two or more input ranges, returns a new range whose values are are 2312*06c3fb27SDimitry Andric /// tuples (A, B, C, ...), such that A is the 0-based index of the item in the 2313*06c3fb27SDimitry Andric /// sequence, and B, C, ..., are the values from the original input ranges. All 2314*06c3fb27SDimitry Andric /// input ranges are required to have equal lengths. Note that the returned 2315*06c3fb27SDimitry Andric /// iterator allows for the values (B, C, ...) to be modified. Example: 23160b57cec5SDimitry Andric /// 2317*06c3fb27SDimitry Andric /// ```c++ 2318*06c3fb27SDimitry Andric /// std::vector<char> Letters = {'A', 'B', 'C', 'D'}; 2319*06c3fb27SDimitry Andric /// std::vector<int> Vals = {10, 11, 12, 13}; 2320*06c3fb27SDimitry Andric /// 2321*06c3fb27SDimitry Andric /// for (auto [Index, Letter, Value] : enumerate(Letters, Vals)) { 2322*06c3fb27SDimitry Andric /// printf("Item %zu - %c: %d\n", Index, Letter, Value); 2323*06c3fb27SDimitry Andric /// Value -= 10; 23240b57cec5SDimitry Andric /// } 2325*06c3fb27SDimitry Andric /// ``` 2326bdd1243dSDimitry Andric /// 23270b57cec5SDimitry Andric /// Output: 2328*06c3fb27SDimitry Andric /// Item 0 - A: 10 2329*06c3fb27SDimitry Andric /// Item 1 - B: 11 2330*06c3fb27SDimitry Andric /// Item 2 - C: 12 2331*06c3fb27SDimitry Andric /// Item 3 - D: 13 23320b57cec5SDimitry Andric /// 2333*06c3fb27SDimitry Andric /// or using an iterator: 2334*06c3fb27SDimitry Andric /// ```c++ 2335*06c3fb27SDimitry Andric /// for (auto it : enumerate(Vals)) { 2336*06c3fb27SDimitry Andric /// it.value() += 10; 2337*06c3fb27SDimitry Andric /// printf("Item %zu: %d\n", it.index(), it.value()); 2338*06c3fb27SDimitry Andric /// } 2339*06c3fb27SDimitry Andric /// ``` 2340*06c3fb27SDimitry Andric /// 2341*06c3fb27SDimitry Andric /// Output: 2342*06c3fb27SDimitry Andric /// Item 0: 20 2343*06c3fb27SDimitry Andric /// Item 1: 21 2344*06c3fb27SDimitry Andric /// Item 2: 22 2345*06c3fb27SDimitry Andric /// Item 3: 23 2346*06c3fb27SDimitry Andric /// 2347*06c3fb27SDimitry Andric template <typename FirstRange, typename... RestRanges> 2348*06c3fb27SDimitry Andric auto enumerate(FirstRange &&First, RestRanges &&...Rest) { 2349*06c3fb27SDimitry Andric if constexpr (sizeof...(Rest) != 0) { 2350*06c3fb27SDimitry Andric #ifndef NDEBUG 2351*06c3fb27SDimitry Andric // Note: Create an array instead of an initializer list to work around an 2352*06c3fb27SDimitry Andric // Apple clang 14 compiler bug. 2353*06c3fb27SDimitry Andric size_t sizes[] = {range_size(First), range_size(Rest)...}; 2354*06c3fb27SDimitry Andric assert(all_equal(sizes) && "Ranges have different length"); 2355*06c3fb27SDimitry Andric #endif 2356*06c3fb27SDimitry Andric } 2357*06c3fb27SDimitry Andric using enumerator = detail::zippy<detail::zip_enumerator, detail::index_stream, 2358*06c3fb27SDimitry Andric FirstRange, RestRanges...>; 2359*06c3fb27SDimitry Andric return enumerator(detail::index_stream{}, std::forward<FirstRange>(First), 2360*06c3fb27SDimitry Andric std::forward<RestRanges>(Rest)...); 23610b57cec5SDimitry Andric } 23620b57cec5SDimitry Andric 23630b57cec5SDimitry Andric namespace detail { 23640b57cec5SDimitry Andric 2365349cc55cSDimitry Andric template <typename Predicate, typename... Args> 2366349cc55cSDimitry Andric bool all_of_zip_predicate_first(Predicate &&P, Args &&...args) { 2367349cc55cSDimitry Andric auto z = zip(args...); 2368349cc55cSDimitry Andric auto it = z.begin(); 2369349cc55cSDimitry Andric auto end = z.end(); 2370349cc55cSDimitry Andric while (it != end) { 2371bdd1243dSDimitry Andric if (!std::apply([&](auto &&...args) { return P(args...); }, *it)) 2372349cc55cSDimitry Andric return false; 2373349cc55cSDimitry Andric ++it; 2374349cc55cSDimitry Andric } 2375349cc55cSDimitry Andric return it.all_equals(end); 2376349cc55cSDimitry Andric } 2377349cc55cSDimitry Andric 2378349cc55cSDimitry Andric // Just an adaptor to switch the order of argument and have the predicate before 2379349cc55cSDimitry Andric // the zipped inputs. 2380349cc55cSDimitry Andric template <typename... ArgsThenPredicate, size_t... InputIndexes> 2381349cc55cSDimitry Andric bool all_of_zip_predicate_last( 2382349cc55cSDimitry Andric std::tuple<ArgsThenPredicate...> argsThenPredicate, 2383349cc55cSDimitry Andric std::index_sequence<InputIndexes...>) { 2384349cc55cSDimitry Andric auto constexpr OutputIndex = 2385349cc55cSDimitry Andric std::tuple_size<decltype(argsThenPredicate)>::value - 1; 2386349cc55cSDimitry Andric return all_of_zip_predicate_first(std::get<OutputIndex>(argsThenPredicate), 2387349cc55cSDimitry Andric std::get<InputIndexes>(argsThenPredicate)...); 2388349cc55cSDimitry Andric } 2389349cc55cSDimitry Andric 2390349cc55cSDimitry Andric } // end namespace detail 2391349cc55cSDimitry Andric 2392349cc55cSDimitry Andric /// Compare two zipped ranges using the provided predicate (as last argument). 2393349cc55cSDimitry Andric /// Return true if all elements satisfy the predicate and false otherwise. 2394349cc55cSDimitry Andric // Return false if the zipped iterator aren't all at end (size mismatch). 2395349cc55cSDimitry Andric template <typename... ArgsAndPredicate> 2396349cc55cSDimitry Andric bool all_of_zip(ArgsAndPredicate &&...argsAndPredicate) { 2397349cc55cSDimitry Andric return detail::all_of_zip_predicate_last( 2398349cc55cSDimitry Andric std::forward_as_tuple(argsAndPredicate...), 2399349cc55cSDimitry Andric std::make_index_sequence<sizeof...(argsAndPredicate) - 1>{}); 2400349cc55cSDimitry Andric } 2401349cc55cSDimitry Andric 24020b57cec5SDimitry Andric /// Return true if the sequence [Begin, End) has exactly N items. Runs in O(N) 24030b57cec5SDimitry Andric /// time. Not meant for use with random-access iterators. 24045ffd83dbSDimitry Andric /// Can optionally take a predicate to filter lazily some items. 24055ffd83dbSDimitry Andric template <typename IterTy, 24065ffd83dbSDimitry Andric typename Pred = bool (*)(const decltype(*std::declval<IterTy>()) &)> 24070b57cec5SDimitry Andric bool hasNItems( 24080b57cec5SDimitry Andric IterTy &&Begin, IterTy &&End, unsigned N, 24095ffd83dbSDimitry Andric Pred &&ShouldBeCounted = 24105ffd83dbSDimitry Andric [](const decltype(*std::declval<IterTy>()) &) { return true; }, 24115ffd83dbSDimitry Andric std::enable_if_t< 2412e8d8bef9SDimitry Andric !std::is_base_of<std::random_access_iterator_tag, 2413e8d8bef9SDimitry Andric typename std::iterator_traits<std::remove_reference_t< 2414e8d8bef9SDimitry Andric decltype(Begin)>>::iterator_category>::value, 24155ffd83dbSDimitry Andric void> * = nullptr) { 24165ffd83dbSDimitry Andric for (; N; ++Begin) { 24170b57cec5SDimitry Andric if (Begin == End) 24180b57cec5SDimitry Andric return false; // Too few. 24195ffd83dbSDimitry Andric N -= ShouldBeCounted(*Begin); 24205ffd83dbSDimitry Andric } 24215ffd83dbSDimitry Andric for (; Begin != End; ++Begin) 24225ffd83dbSDimitry Andric if (ShouldBeCounted(*Begin)) 24235ffd83dbSDimitry Andric return false; // Too many. 24245ffd83dbSDimitry Andric return true; 24250b57cec5SDimitry Andric } 24260b57cec5SDimitry Andric 24270b57cec5SDimitry Andric /// Return true if the sequence [Begin, End) has N or more items. Runs in O(N) 24280b57cec5SDimitry Andric /// time. Not meant for use with random-access iterators. 24295ffd83dbSDimitry Andric /// Can optionally take a predicate to lazily filter some items. 24305ffd83dbSDimitry Andric template <typename IterTy, 24315ffd83dbSDimitry Andric typename Pred = bool (*)(const decltype(*std::declval<IterTy>()) &)> 24320b57cec5SDimitry Andric bool hasNItemsOrMore( 24330b57cec5SDimitry Andric IterTy &&Begin, IterTy &&End, unsigned N, 24345ffd83dbSDimitry Andric Pred &&ShouldBeCounted = 24355ffd83dbSDimitry Andric [](const decltype(*std::declval<IterTy>()) &) { return true; }, 24365ffd83dbSDimitry Andric std::enable_if_t< 2437e8d8bef9SDimitry Andric !std::is_base_of<std::random_access_iterator_tag, 2438e8d8bef9SDimitry Andric typename std::iterator_traits<std::remove_reference_t< 2439e8d8bef9SDimitry Andric decltype(Begin)>>::iterator_category>::value, 24405ffd83dbSDimitry Andric void> * = nullptr) { 24415ffd83dbSDimitry Andric for (; N; ++Begin) { 24420b57cec5SDimitry Andric if (Begin == End) 24430b57cec5SDimitry Andric return false; // Too few. 24445ffd83dbSDimitry Andric N -= ShouldBeCounted(*Begin); 24455ffd83dbSDimitry Andric } 24460b57cec5SDimitry Andric return true; 24470b57cec5SDimitry Andric } 24480b57cec5SDimitry Andric 24495ffd83dbSDimitry Andric /// Returns true if the sequence [Begin, End) has N or less items. Can 24505ffd83dbSDimitry Andric /// optionally take a predicate to lazily filter some items. 24515ffd83dbSDimitry Andric template <typename IterTy, 24525ffd83dbSDimitry Andric typename Pred = bool (*)(const decltype(*std::declval<IterTy>()) &)> 24535ffd83dbSDimitry Andric bool hasNItemsOrLess( 24545ffd83dbSDimitry Andric IterTy &&Begin, IterTy &&End, unsigned N, 24555ffd83dbSDimitry Andric Pred &&ShouldBeCounted = [](const decltype(*std::declval<IterTy>()) &) { 24565ffd83dbSDimitry Andric return true; 24575ffd83dbSDimitry Andric }) { 24585ffd83dbSDimitry Andric assert(N != std::numeric_limits<unsigned>::max()); 24595ffd83dbSDimitry Andric return !hasNItemsOrMore(Begin, End, N + 1, ShouldBeCounted); 24605ffd83dbSDimitry Andric } 24615ffd83dbSDimitry Andric 24625ffd83dbSDimitry Andric /// Returns true if the given container has exactly N items 24635ffd83dbSDimitry Andric template <typename ContainerTy> bool hasNItems(ContainerTy &&C, unsigned N) { 24645ffd83dbSDimitry Andric return hasNItems(std::begin(C), std::end(C), N); 24655ffd83dbSDimitry Andric } 24665ffd83dbSDimitry Andric 24675ffd83dbSDimitry Andric /// Returns true if the given container has N or more items 24685ffd83dbSDimitry Andric template <typename ContainerTy> 24695ffd83dbSDimitry Andric bool hasNItemsOrMore(ContainerTy &&C, unsigned N) { 24705ffd83dbSDimitry Andric return hasNItemsOrMore(std::begin(C), std::end(C), N); 24715ffd83dbSDimitry Andric } 24725ffd83dbSDimitry Andric 24735ffd83dbSDimitry Andric /// Returns true if the given container has N or less items 24745ffd83dbSDimitry Andric template <typename ContainerTy> 24755ffd83dbSDimitry Andric bool hasNItemsOrLess(ContainerTy &&C, unsigned N) { 24765ffd83dbSDimitry Andric return hasNItemsOrLess(std::begin(C), std::end(C), N); 24775ffd83dbSDimitry Andric } 24785ffd83dbSDimitry Andric 24790b57cec5SDimitry Andric /// Returns a raw pointer that represents the same address as the argument. 24800b57cec5SDimitry Andric /// 24815ffd83dbSDimitry Andric /// This implementation can be removed once we move to C++20 where it's defined 24825ffd83dbSDimitry Andric /// as std::to_address(). 24830b57cec5SDimitry Andric /// 24840b57cec5SDimitry Andric /// The std::pointer_traits<>::to_address(p) variations of these overloads has 24850b57cec5SDimitry Andric /// not been implemented. 24865ffd83dbSDimitry Andric template <class Ptr> auto to_address(const Ptr &P) { return P.operator->(); } 24870b57cec5SDimitry Andric template <class T> constexpr T *to_address(T *P) { return P; } 24880b57cec5SDimitry Andric 24890b57cec5SDimitry Andric } // end namespace llvm 24900b57cec5SDimitry Andric 2491bdd1243dSDimitry Andric namespace std { 2492*06c3fb27SDimitry Andric template <typename... Refs> 2493*06c3fb27SDimitry Andric struct tuple_size<llvm::detail::enumerator_result<Refs...>> 2494*06c3fb27SDimitry Andric : std::integral_constant<std::size_t, sizeof...(Refs)> {}; 2495bdd1243dSDimitry Andric 2496*06c3fb27SDimitry Andric template <std::size_t I, typename... Refs> 2497*06c3fb27SDimitry Andric struct tuple_element<I, llvm::detail::enumerator_result<Refs...>> 2498*06c3fb27SDimitry Andric : std::tuple_element<I, std::tuple<Refs...>> {}; 2499*06c3fb27SDimitry Andric 2500*06c3fb27SDimitry Andric template <std::size_t I, typename... Refs> 2501*06c3fb27SDimitry Andric struct tuple_element<I, const llvm::detail::enumerator_result<Refs...>> 2502*06c3fb27SDimitry Andric : std::tuple_element<I, std::tuple<Refs...>> {}; 2503bdd1243dSDimitry Andric 2504bdd1243dSDimitry Andric } // namespace std 2505bdd1243dSDimitry Andric 25060b57cec5SDimitry Andric #endif // LLVM_ADT_STLEXTRAS_H 2507