17 #ifndef LLVM_ADT_STLEXTRAS_H
18 #define LLVM_ADT_STLEXTRAS_H
46 template <
typename RangeT>
49 template <
typename RangeT>
50 using ValueOfRange =
typename std::remove_reference<decltype(
60 struct identity :
public std::unary_function<Ty, Ty> {
70 struct less_ptr :
public std::binary_function<Ty, Ty, bool> {
72 return *left < *right;
77 struct greater_ptr :
public std::binary_function<Ty, Ty, bool> {
79 return *right < *left;
91 template<
typename Ret,
typename ...Params>
93 Ret (*callback)(
intptr_t callable, Params ...params);
96 template<
typename Callable>
97 static Ret callback_fn(
intptr_t callable, Params ...params) {
98 return (*reinterpret_cast<Callable*>(callable))(
99 std::forward<Params>(params)...);
105 template <
typename Callable>
107 typename std::enable_if<
108 !std::is_same<
typename std::remove_reference<Callable>::type,
110 : callback(callback_fn<typename std::remove_reference<Callable>::type>),
111 callable(reinterpret_cast<
intptr_t>(&callable)) {}
113 return callback(callable, std::forward<Params>(params)...);
116 operator bool()
const {
return callback; }
138 template <
class RootIt,
class UnaryFunc>
143 typedef typename std::iterator_traits<RootIt>::iterator_category
145 typedef typename std::iterator_traits<RootIt>::difference_type
147 typedef decltype(std::declval<UnaryFunc>()(*std::declval<RootIt>()))
157 inline const UnaryFunc &
getFunc()
const {
return Fn; }
160 : current(I), Fn(F) {}
202 return current == X.current;
207 return current - X.current;
211 template <
class Iterator,
class Func>
212 inline mapped_iterator<Iterator, Func>
222 template <
class ItTy,
class FuncTy>
232 template <
typename Inner>
233 static yes& test(Inner *
I, decltype(I->rbegin()) * =
nullptr);
236 static no& test(...);
239 static const bool value =
sizeof(test<Ty>(
nullptr)) ==
sizeof(yes);
243 template <
typename Ty>
249 template <
typename ContainerTy>
252 nullptr) -> decltype(
make_range(
C.rbegin(),
C.rend())) {
257 template <
typename IteratorTy>
259 return std::reverse_iterator<IteratorTy>(It);
265 template <
typename ContainerTy>
287 template <
typename WrappedIteratorT,
typename PredicateT>
290 filter_iterator<WrappedIteratorT, PredicateT>, WrappedIteratorT,
291 typename std::common_type<
292 std::forward_iterator_tag,
293 typename std::iterator_traits<
294 WrappedIteratorT>::iterator_category>::type> {
297 typename std::common_type<
298 std::forward_iterator_tag,
299 typename std::iterator_traits<WrappedIteratorT>::iterator_category>::
303 WrappedIteratorT
End;
309 void findNextValid() {
310 assert(Payload &&
"Payload should be engaged when findNextValid is called");
311 while (this->
I != Payload->End && !Payload->Pred(*this->I))
318 :
BaseT(std::move(Begin)),
319 Payload(PayloadType{std::move(End), std::move(Pred)}) {
328 using BaseT::operator++;
336 template <
typename RT,
typename PT>
348 template <
typename RangeT,
typename PredicateT>
349 iterator_range<filter_iterator<detail::IterOfRange<RangeT>, PredicateT>>
351 using FilterIteratorT =
354 std::end(std::forward<RangeT>(Range)),
356 FilterIteratorT(
std::end(std::forward<RangeT>(Range))));
360 template <
typename R,
typename UnaryPredicate>
361 bool all_of(R &&range, UnaryPredicate
P);
373 typedef std::tuple<decltype(*declval<Iters>())...>
type;
376 template <
typename ZipType,
typename... Iters>
378 ZipType,
typename std::common_type<std::bidirectional_iterator_tag,
379 typename std::iterator_traits<
380 Iters>::iterator_category...>::type,
383 typename std::iterator_traits<
typename std::tuple_element<
384 0, std::tuple<Iters...>>::type>::difference_type,
392 template <
typename ZipType,
typename... Iters>
404 template <
size_t... Ns>
406 return std::tuple<Iters...>(std::next(std::get<Ns>(
iterators))...);
409 template <
size_t... Ns>
411 return std::tuple<Iters...>(std::prev(std::get<Ns>(
iterators))...);
425 return *
reinterpret_cast<ZipType *
>(
this);
430 "All inner iterators must be at least bidirectional.");
432 return *
reinterpret_cast<ZipType *
>(
this);
436 template <
typename... Iters>
447 template <
typename... Iters>
449 template <
size_t... Ns>
451 return all_of(std::initializer_list<bool>{std::get<Ns>(this->
iterators) !=
466 template <
template <
typename...>
class ItType,
typename...
Args>
class zippy {
468 using iterator = ItType<decltype(std::begin(std::declval<Args>()))...>;
476 std::tuple<
Args...> ts;
493 template <
typename T,
typename U,
typename...
Args>
497 std::forward<T>(
t), std::forward<U>(u), std::forward<Args>(args)...);
502 template <
typename T,
typename U,
typename...
Args>
506 std::forward<T>(
t), std::forward<U>(u), std::forward<Args>(args)...);
519 template <
typename ValueT,
typename... IterTs>
522 std::forward_iterator_tag, ValueT> {
523 typedef typename concat_iterator::iterator_facade_base BaseT;
531 std::tuple<std::pair<IterTs, IterTs>...> IterPairs;
537 template <
size_t Index>
bool incrementHelper() {
538 auto &IterPair = std::get<Index>(IterPairs);
539 if (IterPair.first == IterPair.second)
552 &concat_iterator::incrementHelper<Ns>...};
555 for (
auto &IncrementHelperFn : IncrementHelperFns)
556 if ((this->*IncrementHelperFn)())
565 template <
size_t Index>
ValueT *getHelper()
const {
566 auto &IterPair = std::get<Index>(IterPairs);
567 if (IterPair.first == IterPair.second)
570 return &*IterPair.first;
580 &concat_iterator::getHelper<Ns>...};
583 for (
auto &GetHelperFn : GetHelperFns)
584 if (
ValueT *
P = (this->*GetHelperFn)())
587 llvm_unreachable(
"Attempted to get a pointer from an end concat iterator!");
595 template <
typename... RangeTs>
599 using BaseT::operator++;
608 return IterPairs == RHS.IterPairs;
621 decltype(
std::begin(std::declval<RangeTs &>()))...>
625 std::tuple<RangeTs...> Ranges;
628 return iterator(std::get<Ns>(Ranges)...);
632 std::end(std::get<Ns>(Ranges)))...);
639 : Ranges(std::forward<RangeTs>(Ranges)...) {}
646 template <
typename ValueT,
typename... RangeTs>
648 static_assert(
sizeof...(RangeTs) > 1,
649 "Need more than one range to concatenate!");
651 std::forward<RangeTs>(Ranges)...);
661 template <
typename T>
bool operator()(
const T &lhs,
const T &rhs)
const {
662 return lhs.first < rhs.first;
669 template <
typename T>
bool operator()(
const T &lhs,
const T &rhs)
const {
670 return lhs.second < rhs.second;
680 static constexpr
size_t size() {
return sizeof...(I); }
684 template <
size_t...
I>
685 struct index_sequence : integer_sequence<std::size_t, I...> {};
687 template <std::size_t
N, std::size_t...
I>
689 template <std::size_t...
I>
693 template <
class... Ts>
707 template <
typename T,
typename U,
typename... Ts>
719 template <
typename T,
typename U,
typename... Ts>
730 template <
class T, std::
size_t N>
738 if (std::less<T>()(*reinterpret_cast<const T*>(P1),
739 *reinterpret_cast<const T*>(P2)))
741 if (std::less<T>()(*reinterpret_cast<const T*>(P2),
742 *reinterpret_cast<const T*>(P1)))
751 (
const void*,
const void*) {
752 return array_pod_sort_comparator<T>;
770 template<
class IteratorTy>
774 auto NElts = End - Start;
775 if (NElts <= 1)
return;
779 template <
class IteratorTy>
781 IteratorTy Start, IteratorTy
End,
783 const typename std::iterator_traits<IteratorTy>::value_type *,
784 const typename std::iterator_traits<IteratorTy>::value_type *)) {
787 auto NElts = End - Start;
788 if (NElts <= 1)
return;
789 qsort(&*Start, NElts,
sizeof(*Start),
790 reinterpret_cast<int (*)(
const void *,
const void *)
>(
Compare));
799 template<
typename Container>
808 template<
typename Container>
817 template <
typename R,
typename UnaryPredicate>
824 template <
typename R,
typename UnaryPredicate>
831 template <
typename R,
typename UnaryPredicate>
838 template <
typename R,
typename T>
845 template <
typename R,
typename UnaryPredicate>
850 template <
typename R,
typename UnaryPredicate>
857 template <
typename R,
typename UnaryPredicate>
864 template <
typename R,
typename OutputIt,
typename UnaryPredicate>
865 OutputIt
copy_if(R &&Range, OutputIt Out, UnaryPredicate
P) {
871 template <
typename R,
typename E>
879 template <
typename R,
typename E>
880 auto count(R &&Range,
const E &Element) ->
typename std::iterator_traits<
881 decltype(
std::begin(Range))>::difference_type {
887 template <
typename R,
typename UnaryPredicate>
888 auto count_if(R &&Range, UnaryPredicate
P) ->
typename std::iterator_traits<
889 decltype(
std::begin(Range))>::difference_type {
895 template <
typename R,
typename OutputIt,
typename UnaryPredicate>
896 OutputIt
transform(R &&Range, OutputIt d_first, UnaryPredicate
P) {
902 template <
typename R,
typename UnaryPredicate>
910 template <
unsigned Size,
typename R>
911 SmallVector<typename std::remove_const<detail::ValueOfRange<R>>::type,
Size>
923 template <
typename Container,
typename UnaryPredicate>
941 template <
class T,
class...
Args>
942 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
944 return std::unique_ptr<T>(
new T(std::forward<Args>(args)...));
956 typename std::enable_if<std::is_array<T>::value && std::extent<T>::value == 0,
957 std::unique_ptr<T>>::type
959 return std::unique_ptr<T>(
new typename std::remove_extent<T>::type[n]());
963 template <
class T,
class...
Args>
964 typename std::enable_if<std::extent<T>::value != 0>::type
973 template<
typename First,
typename Second>
976 return std::hash<First>()(P.first) * 31 + std::hash<Second>()(P.second);
982 template <
typename A,
typename B>
bool operator()(
A &&a,
B &&b)
const {
983 return std::forward<A>(a) < std::forward<B>(b);
989 template <
typename A,
typename B>
bool operator()(
A &&a,
B &&b)
const {
990 return std::forward<A>(a) == std::forward<B>(b);
996 template <
typename T>
struct deref {
1001 template <
typename A,
typename B>
1005 return func(*lhs, *rhs);
1017 : Index(Index), Iter(Iter) {}
1020 Index = Other.Index;
1025 std::size_t
index()
const {
return Index; }
1034 template <
typename R>
1035 class enumerator_iter
1037 enumerator_iter<R>, std::forward_iterator_tag, result_pair<R>,
1038 typename std::iterator_traits<IterOfRange<R>>::difference_type,
1039 typename std::iterator_traits<IterOfRange<R>>::pointer,
1040 typename std::iterator_traits<IterOfRange<R>>::reference> {
1041 using result_type = result_pair<R>;
1045 : Result(std::numeric_limits<
size_t>::
max(), EndIter) { }
1048 : Result(Index, Iter) {}
1064 return Result.Iter == RHS.Result.Iter;
1068 Result = Other.Result;
1078 explicit enumerator(R &&Range) : TheRange(std::forward<R>(Range)) {}
1112 template <
typename F,
typename Tuple, std::size_t...
I>
1114 -> decltype(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(
t))...)) {
1115 return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(
t))...);
1122 template <
typename F,
typename Tuple>
1124 std::forward<F>(f), std::forward<Tuple>(
t),
1126 std::tuple_size<
typename std::decay<Tuple>::type>::value>{})) {
1127 using Indices = build_index_impl<
1128 std::tuple_size<typename std::decay<Tuple>::type>::value>;
result_pair(std::size_t Index, IterOfRange< R > Iter)
void DeleteContainerSeconds(Container &C)
In a container of pairs (usually a map) whose second element is a pointer, deletes the second element...
const value_type operator*() const
void DeleteContainerPointers(Container &C)
For a container of pointers, deletes the pointers and then clears the container.
bool operator==(const zip_first< Iters...> &other) const
const_iterator end(StringRef path)
Get end iterator over path.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
const Ty & operator()(const Ty &self) const
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
zip_common(Iters &&...ts)
APInt operator+(APInt a, const APInt &b)
typename Base::value_type value_type
typename std::remove_reference< decltype(*std::begin(std::declval< RangeT & >()))>::type ValueOfRange
Function object to check whether the second component of a std::pair compares less than the second co...
OutputIt copy_if(R &&Range, OutputIt Out, UnaryPredicate P)
Provide wrappers to std::copy_if which take ranges instead of having to pass begin/end explicitly...
auto remove_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::remove_if which take ranges instead of having to pass begin/end explicitly...
typename iterator::value_type value_type
result_type & operator*()
mapped_iterator operator+(difference_type n) const
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
int(*)(const void *, const void *) get_array_pod_sort_comparator(const T &)
get_array_pod_sort_comparator - This is an internal helper function used to get type deduction of T r...
Ret operator()(Params...params) const
An efficient, type-erasing, non-owning reference to a callable.
A functor like C++14's std::less<void> in its absence.
enumerator_iter(IterOfRange< R > EndIter)
zip_shortest(Iters &&...ts)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
filter_iterator & operator++()
typename iterator::pointer pointer
int array_pod_sort_comparator(const void *P1, const void *P2)
Adapt std::less<T> for array_pod_sort.
Metafunction to determine if T& or T has a member called rbegin().
function_ref(Callable &&callable, typename std::enable_if< !std::is_same< typename std::remove_reference< Callable >::type, function_ref >::value >::type *=nullptr)
mapped_iterator(const RootIt &I, UnaryFunc F)
detail::zippy< detail::zip_first, T, U, Args...> zip_first(T &&t, U &&u, Args &&...args)
zip iterator that, for the sake of efficiency, assumes the first iteratee to be the shortest...
auto find_if_not(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
bool operator()(A &&a, B &&b) const
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly...
bool operator()(const T &lhs, const T &rhs) const
decltype(iterators) tup_inc(index_sequence< Ns...>) const
ValueT & operator*() const
SmallVector< typename std::remove_const< detail::ValueOfRange< R > >::type, Size > to_vector(R &&Range)
Given a range of type R, iterate the entire range and return a SmallVector with elements of the vecto...
Alias for the common case of a sequence of size_ts.
mapped_iterator< ItTy, FuncTy > map_iterator(const ItTy &I, FuncTy F)
std::iterator_traits< RootIt >::iterator_category iterator_category
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
mapped_iterator & operator--()
concat_iterator(RangeTs &&...Ranges)
Constructs an iterator from a squence of ranges.
enumerator_iter< R > & operator++()
auto apply_tuple(F &&f, Tuple &&t) -> decltype(detail::apply_tuple_impl(std::forward< F >(f), std::forward< Tuple >(t), build_index_impl< std::tuple_size< typename std::decay< Tuple >::type >::value >
Given an input tuple (a1, a2, ..., an), pass the arguments of the tuple variadically to f as if by ca...
auto count(R &&Range, const E &Element) -> typename std::iterator_traits< decltype(std::begin(Range))>::difference_type
Wrapper function around std::count to count the number of times an element Element occurs in the give...
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
enumerator_iter(std::size_t Index, IterOfRange< R > Iter)
Helper to store a sequence of ranges being concatenated and access them.
traits class for checking whether type T is one of any of the given types in the variadic list...
ValueOfRange< R > & value()
typename iterator::reference reference
bool operator()(A &&a, B &&b) const
ItType< decltype(std::begin(std::declval< Args >()))...> iterator
std::iterator_traits< RootIt >::difference_type difference_type
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
An iterator adaptor that filters the elements of given inner iterators.
CRTP base class for adapting an iterator to a different type.
mapped_iterator & operator++()
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
decltype(iterators) tup_dec(index_sequence< Ns...>) const
size_t operator()(const std::pair< First, Second > &P) const
Helper to determine if type T has a member called rbegin().
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
detail::zippy< detail::zip_shortest, T, U, Args...> zip(T &&t, U &&u, Args &&...args)
zip iterator for two or more iteratable types.
static const unsigned End
mapped_iterator operator++(int)
bool operator<(const mapped_iterator &X) const
decltype(std::declval< UnaryFunc >()(*std::declval< RootIt >())) typedef value_type
detail::concat_range< ValueT, RangeTs...> concat(RangeTs &&...Ranges)
Concatenated range across two or more ranges.
Ty & operator()(Ty &self) const
static constexpr size_t size()
value_type operator*() const
A functor like C++14's std::equal<void> in its absence.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
bool operator()(const T &lhs, const T &rhs) const
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
const UnaryFunc & getFunc() const
mapped_iterator & operator+=(difference_type n)
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
Creates a compile-time integer sequence for a parameter pack.
typename iterator::difference_type difference_type
bool operator()(const Ty *left, const Ty *right) const
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
A range adaptor for a pair of iterators.
difference_type operator-(const mapped_iterator &X) const
bool operator==(const zip_shortest< Iters...> &other) const
bool operator()(const Ty *left, const Ty *right) const
Represents a compile-time sequence of integers.
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
enumerator_iter< R > & operator=(const enumerator_iter< R > &Other)
concat_iterator & operator++()
mapped_iterator operator--(int)
const RootIt & getCurrent() const
result_pair< R > & operator=(const result_pair< R > &Other)
bool operator==(const mapped_iterator &X) const
bool operator==(const enumerator_iter< R > &RHS) const
auto partition(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::partition which take ranges instead of having to pass begin/end explicitly...
typename iterator::iterator_category iterator_category
decltype(std::begin(std::declval< RangeT & >())) IterOfRange
bool operator!=(const mapped_iterator &X) const
friend iterator_range< filter_iterator< detail::IterOfRange< RT >, PT > > make_filter_range(RT &&, PT)
const ValueOfRange< R > & value() const
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
std::tuple< Iters...> iterators
OutputIt transform(R &&Range, OutputIt d_first, UnaryPredicate P)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
mapped_iterator & operator-=(difference_type n)
std::reverse_iterator< IteratorTy > make_reverse_iterator(IteratorTy It)
value_type deref(index_sequence< Ns...>) const
traits class for checking whether type T is a base class for all the given types in the variadic list...
enumerator_iter< R > begin()
concat_range(RangeTs &&...Ranges)
enumerator_iter< R > end()
const result_type & operator*() const
auto operator()(A &lhs, B &rhs) const -> decltype(func(*lhs,*rhs))
Binary functor that adapts to any other binary functor after dereferencing operands.
Iterator wrapper that concatenates sequences together.
std::tuple< decltype(*declval< Iters >))...> type
Utility type to build an inheritance chain that makes it easy to rank overload candidates.
bool operator==(const concat_iterator &RHS) const
concat_iterator< ValueT, decltype(std::begin(std::declval< RangeTs & >)))...> iterator
auto find_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
reference operator[](difference_type n) const
auto apply_tuple_impl(F &&f, Tuple &&t, index_sequence< I...>) -> decltype(std::forward< F >(f)(std::get< I >(std::forward< Tuple >(t))...))
Function object to check whether the first component of a std::pair compares less than the first comp...
auto count_if(R &&Range, UnaryPredicate P) -> typename std::iterator_traits< decltype(std::begin(Range))>::difference_type
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
std::size_t index() const
detail::enumerator< R > enumerate(R &&TheRange)
Given an input range, returns a new range whose values are are pair (A,B) such that A is the 0-based ...
mapped_iterator operator-(difference_type n) const
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.