1 //===- llvm/ADT/STLExtras.h - Useful STL related functions ------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains some templates that are useful if you are working with the
12 // No library is required when using these functions.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_ADT_STLEXTRAS_H
17 #define LLVM_ADT_STLEXTRAS_H
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/iterator.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/Config/abi-breaking.h"
23 #include "llvm/Support/ErrorHandling.h"
30 #include <initializer_list>
35 #include <type_traits>
38 #ifdef EXPENSIVE_CHECKS
39 #include <random> // for std::mt19937
44 // Only used by compiler if both template types are the same. Useful when
45 // using SFINAE to test for the existence of member functions.
46 template <typename T, T> struct SameType;
50 template <typename RangeT>
51 using IterOfRange = decltype(std::begin(std::declval<RangeT &>()));
53 template <typename RangeT>
54 using ValueOfRange = typename std::remove_reference<decltype(
55 *std::begin(std::declval<RangeT &>()))>::type;
57 } // end namespace detail
59 //===----------------------------------------------------------------------===//
60 // Extra additions to <type_traits>
61 //===----------------------------------------------------------------------===//
64 struct negation : std::integral_constant<bool, !bool(T::value)> {};
66 template <typename...> struct conjunction : std::true_type {};
67 template <typename B1> struct conjunction<B1> : B1 {};
68 template <typename B1, typename... Bn>
69 struct conjunction<B1, Bn...>
70 : std::conditional<bool(B1::value), conjunction<Bn...>, B1>::type {};
72 template <typename T> struct make_const_ptr {
74 typename std::add_pointer<typename std::add_const<T>::type>::type;
77 template <typename T> struct make_const_ref {
78 using type = typename std::add_lvalue_reference<
79 typename std::add_const<T>::type>::type;
82 /// Utilities for detecting if a given trait holds for some set of arguments
83 /// 'Args'. For example, the given trait could be used to detect if a given type
84 /// has a copy assignment operator:
86 /// using has_copy_assign_t = decltype(std::declval<T&>()
87 /// = std::declval<const T&>());
88 /// bool fooHasCopyAssign = is_detected<has_copy_assign_t, FooClass>::value;
90 template <typename...> using void_t = void;
91 template <class, template <class...> class Op, class... Args> struct detector {
92 using value_t = std::false_type;
94 template <template <class...> class Op, class... Args>
95 struct detector<void_t<Op<Args...>>, Op, Args...> {
96 using value_t = std::true_type;
98 } // end namespace detail
100 template <template <class...> class Op, class... Args>
101 using is_detected = typename detail::detector<void, Op, Args...>::value_t;
103 /// Check if a Callable type can be invoked with the given set of arg types.
105 template <typename Callable, typename... Args>
107 decltype(std::declval<Callable &>()(std::declval<Args>()...));
108 } // namespace detail
110 template <typename Callable, typename... Args>
111 using is_invocable = is_detected<detail::is_invocable, Callable, Args...>;
113 /// This class provides various trait information about a callable object.
114 /// * To access the number of arguments: Traits::num_args
115 /// * To access the type of an argument: Traits::arg_t<Index>
116 /// * To access the type of the result: Traits::result_t
117 template <typename T, bool isClass = std::is_class<T>::value>
118 struct function_traits : public function_traits<decltype(&T::operator())> {};
120 /// Overload for class function types.
121 template <typename ClassType, typename ReturnType, typename... Args>
122 struct function_traits<ReturnType (ClassType::*)(Args...) const, false> {
123 /// The number of arguments to this function.
124 enum { num_args = sizeof...(Args) };
126 /// The result type of this function.
127 using result_t = ReturnType;
129 /// The type of an argument to this function.
130 template <size_t Index>
131 using arg_t = typename std::tuple_element<Index, std::tuple<Args...>>::type;
133 /// Overload for class function types.
134 template <typename ClassType, typename ReturnType, typename... Args>
135 struct function_traits<ReturnType (ClassType::*)(Args...), false>
136 : function_traits<ReturnType (ClassType::*)(Args...) const> {};
137 /// Overload for non-class function types.
138 template <typename ReturnType, typename... Args>
139 struct function_traits<ReturnType (*)(Args...), false> {
140 /// The number of arguments to this function.
141 enum { num_args = sizeof...(Args) };
143 /// The result type of this function.
144 using result_t = ReturnType;
146 /// The type of an argument to this function.
148 using arg_t = typename std::tuple_element<i, std::tuple<Args...>>::type;
150 /// Overload for non-class function type references.
151 template <typename ReturnType, typename... Args>
152 struct function_traits<ReturnType (&)(Args...), false>
153 : public function_traits<ReturnType (*)(Args...)> {};
155 //===----------------------------------------------------------------------===//
156 // Extra additions to <functional>
157 //===----------------------------------------------------------------------===//
159 template <class Ty> struct identity {
160 using argument_type = Ty;
162 Ty &operator()(Ty &self) const {
165 const Ty &operator()(const Ty &self) const {
170 /// An efficient, type-erasing, non-owning reference to a callable. This is
171 /// intended for use as the type of a function parameter that is not used
172 /// after the function in question returns.
174 /// This class does not own the callable, so it is not in general safe to store
176 template<typename Fn> class function_ref;
178 template<typename Ret, typename ...Params>
179 class function_ref<Ret(Params...)> {
180 Ret (*callback)(intptr_t callable, Params ...params) = nullptr;
183 template<typename Callable>
184 static Ret callback_fn(intptr_t callable, Params ...params) {
185 return (*reinterpret_cast<Callable*>(callable))(
186 std::forward<Params>(params)...);
190 function_ref() = default;
191 function_ref(std::nullptr_t) {}
193 template <typename Callable>
197 !std::is_same<std::remove_cv_t<std::remove_reference_t<Callable>>,
198 function_ref>::value> * = nullptr)
199 : callback(callback_fn<typename std::remove_reference<Callable>::type>),
200 callable(reinterpret_cast<intptr_t>(&callable)) {}
202 Ret operator()(Params ...params) const {
203 return callback(callable, std::forward<Params>(params)...);
206 explicit operator bool() const { return callback; }
209 // deleter - Very very very simple method that is used to invoke operator
210 // delete on something. It is used like this:
212 // for_each(V.begin(), B.end(), deleter<Interval>);
214 inline void deleter(T *Ptr) {
218 //===----------------------------------------------------------------------===//
219 // Extra additions to <iterator>
220 //===----------------------------------------------------------------------===//
222 namespace adl_detail {
226 template <typename ContainerTy>
227 decltype(auto) adl_begin(ContainerTy &&container) {
228 return begin(std::forward<ContainerTy>(container));
233 template <typename ContainerTy>
234 decltype(auto) adl_end(ContainerTy &&container) {
235 return end(std::forward<ContainerTy>(container));
240 template <typename T>
241 void adl_swap(T &&lhs, T &&rhs) noexcept(noexcept(swap(std::declval<T>(),
242 std::declval<T>()))) {
243 swap(std::forward<T>(lhs), std::forward<T>(rhs));
246 } // end namespace adl_detail
248 template <typename ContainerTy>
249 decltype(auto) adl_begin(ContainerTy &&container) {
250 return adl_detail::adl_begin(std::forward<ContainerTy>(container));
253 template <typename ContainerTy>
254 decltype(auto) adl_end(ContainerTy &&container) {
255 return adl_detail::adl_end(std::forward<ContainerTy>(container));
258 template <typename T>
259 void adl_swap(T &&lhs, T &&rhs) noexcept(
260 noexcept(adl_detail::adl_swap(std::declval<T>(), std::declval<T>()))) {
261 adl_detail::adl_swap(std::forward<T>(lhs), std::forward<T>(rhs));
264 /// Test whether \p RangeOrContainer is empty. Similar to C++17 std::empty.
265 template <typename T>
266 constexpr bool empty(const T &RangeOrContainer) {
267 return adl_begin(RangeOrContainer) == adl_end(RangeOrContainer);
270 /// Returns true if the given container only contains a single element.
271 template <typename ContainerTy> bool hasSingleElement(ContainerTy &&C) {
272 auto B = std::begin(C), E = std::end(C);
273 return B != E && std::next(B) == E;
276 /// Return a range covering \p RangeOrContainer with the first N elements
278 template <typename T> auto drop_begin(T &&RangeOrContainer, size_t N) {
279 return make_range(std::next(adl_begin(RangeOrContainer), N),
280 adl_end(RangeOrContainer));
283 // mapped_iterator - This is a simple iterator adapter that causes a function to
284 // be applied whenever operator* is invoked on the iterator.
286 template <typename ItTy, typename FuncTy,
287 typename FuncReturnTy =
288 decltype(std::declval<FuncTy>()(*std::declval<ItTy>()))>
289 class mapped_iterator
290 : public iterator_adaptor_base<
291 mapped_iterator<ItTy, FuncTy>, ItTy,
292 typename std::iterator_traits<ItTy>::iterator_category,
293 typename std::remove_reference<FuncReturnTy>::type> {
295 mapped_iterator(ItTy U, FuncTy F)
296 : mapped_iterator::iterator_adaptor_base(std::move(U)), F(std::move(F)) {}
298 ItTy getCurrent() { return this->I; }
300 FuncReturnTy operator*() const { return F(*this->I); }
306 // map_iterator - Provide a convenient way to create mapped_iterators, just like
307 // make_pair is useful for creating pairs...
308 template <class ItTy, class FuncTy>
309 inline mapped_iterator<ItTy, FuncTy> map_iterator(ItTy I, FuncTy F) {
310 return mapped_iterator<ItTy, FuncTy>(std::move(I), std::move(F));
313 template <class ContainerTy, class FuncTy>
314 auto map_range(ContainerTy &&C, FuncTy F) {
315 return make_range(map_iterator(C.begin(), F), map_iterator(C.end(), F));
318 /// Helper to determine if type T has a member called rbegin().
319 template <typename Ty> class has_rbegin_impl {
323 template <typename Inner>
324 static yes& test(Inner *I, decltype(I->rbegin()) * = nullptr);
327 static no& test(...);
330 static const bool value = sizeof(test<Ty>(nullptr)) == sizeof(yes);
333 /// Metafunction to determine if T& or T has a member called rbegin().
334 template <typename Ty>
335 struct has_rbegin : has_rbegin_impl<typename std::remove_reference<Ty>::type> {
338 // Returns an iterator_range over the given container which iterates in reverse.
339 // Note that the container must have rbegin()/rend() methods for this to work.
340 template <typename ContainerTy>
341 auto reverse(ContainerTy &&C,
342 std::enable_if_t<has_rbegin<ContainerTy>::value> * = nullptr) {
343 return make_range(C.rbegin(), C.rend());
346 // Returns a std::reverse_iterator wrapped around the given iterator.
347 template <typename IteratorTy>
348 std::reverse_iterator<IteratorTy> make_reverse_iterator(IteratorTy It) {
349 return std::reverse_iterator<IteratorTy>(It);
352 // Returns an iterator_range over the given container which iterates in reverse.
353 // Note that the container must have begin()/end() methods which return
354 // bidirectional iterators for this to work.
355 template <typename ContainerTy>
356 auto reverse(ContainerTy &&C,
357 std::enable_if_t<!has_rbegin<ContainerTy>::value> * = nullptr) {
358 return make_range(llvm::make_reverse_iterator(std::end(C)),
359 llvm::make_reverse_iterator(std::begin(C)));
362 /// An iterator adaptor that filters the elements of given inner iterators.
364 /// The predicate parameter should be a callable object that accepts the wrapped
365 /// iterator's reference type and returns a bool. When incrementing or
366 /// decrementing the iterator, it will call the predicate on each element and
367 /// skip any where it returns false.
370 /// int A[] = { 1, 2, 3, 4 };
371 /// auto R = make_filter_range(A, [](int N) { return N % 2 == 1; });
372 /// // R contains { 1, 3 }.
375 /// Note: filter_iterator_base implements support for forward iteration.
376 /// filter_iterator_impl exists to provide support for bidirectional iteration,
377 /// conditional on whether the wrapped iterator supports it.
378 template <typename WrappedIteratorT, typename PredicateT, typename IterTag>
379 class filter_iterator_base
380 : public iterator_adaptor_base<
381 filter_iterator_base<WrappedIteratorT, PredicateT, IterTag>,
383 typename std::common_type<
384 IterTag, typename std::iterator_traits<
385 WrappedIteratorT>::iterator_category>::type> {
386 using BaseT = iterator_adaptor_base<
387 filter_iterator_base<WrappedIteratorT, PredicateT, IterTag>,
389 typename std::common_type<
390 IterTag, typename std::iterator_traits<
391 WrappedIteratorT>::iterator_category>::type>;
394 WrappedIteratorT End;
397 void findNextValid() {
398 while (this->I != End && !Pred(*this->I))
402 // Construct the iterator. The begin iterator needs to know where the end
403 // is, so that it can properly stop when it gets there. The end iterator only
404 // needs the predicate to support bidirectional iteration.
405 filter_iterator_base(WrappedIteratorT Begin, WrappedIteratorT End,
407 : BaseT(Begin), End(End), Pred(Pred) {
412 using BaseT::operator++;
414 filter_iterator_base &operator++() {
421 /// Specialization of filter_iterator_base for forward iteration only.
422 template <typename WrappedIteratorT, typename PredicateT,
423 typename IterTag = std::forward_iterator_tag>
424 class filter_iterator_impl
425 : public filter_iterator_base<WrappedIteratorT, PredicateT, IterTag> {
426 using BaseT = filter_iterator_base<WrappedIteratorT, PredicateT, IterTag>;
429 filter_iterator_impl(WrappedIteratorT Begin, WrappedIteratorT End,
431 : BaseT(Begin, End, Pred) {}
434 /// Specialization of filter_iterator_base for bidirectional iteration.
435 template <typename WrappedIteratorT, typename PredicateT>
436 class filter_iterator_impl<WrappedIteratorT, PredicateT,
437 std::bidirectional_iterator_tag>
438 : public filter_iterator_base<WrappedIteratorT, PredicateT,
439 std::bidirectional_iterator_tag> {
440 using BaseT = filter_iterator_base<WrappedIteratorT, PredicateT,
441 std::bidirectional_iterator_tag>;
442 void findPrevValid() {
443 while (!this->Pred(*this->I))
448 using BaseT::operator--;
450 filter_iterator_impl(WrappedIteratorT Begin, WrappedIteratorT End,
452 : BaseT(Begin, End, Pred) {}
454 filter_iterator_impl &operator--() {
463 template <bool is_bidirectional> struct fwd_or_bidi_tag_impl {
464 using type = std::forward_iterator_tag;
467 template <> struct fwd_or_bidi_tag_impl<true> {
468 using type = std::bidirectional_iterator_tag;
471 /// Helper which sets its type member to forward_iterator_tag if the category
472 /// of \p IterT does not derive from bidirectional_iterator_tag, and to
473 /// bidirectional_iterator_tag otherwise.
474 template <typename IterT> struct fwd_or_bidi_tag {
475 using type = typename fwd_or_bidi_tag_impl<std::is_base_of<
476 std::bidirectional_iterator_tag,
477 typename std::iterator_traits<IterT>::iterator_category>::value>::type;
480 } // namespace detail
482 /// Defines filter_iterator to a suitable specialization of
483 /// filter_iterator_impl, based on the underlying iterator's category.
484 template <typename WrappedIteratorT, typename PredicateT>
485 using filter_iterator = filter_iterator_impl<
486 WrappedIteratorT, PredicateT,
487 typename detail::fwd_or_bidi_tag<WrappedIteratorT>::type>;
489 /// Convenience function that takes a range of elements and a predicate,
490 /// and return a new filter_iterator range.
492 /// FIXME: Currently if RangeT && is a rvalue reference to a temporary, the
493 /// lifetime of that temporary is not kept by the returned range object, and the
494 /// temporary is going to be dropped on the floor after the make_iterator_range
495 /// full expression that contains this function call.
496 template <typename RangeT, typename PredicateT>
497 iterator_range<filter_iterator<detail::IterOfRange<RangeT>, PredicateT>>
498 make_filter_range(RangeT &&Range, PredicateT Pred) {
499 using FilterIteratorT =
500 filter_iterator<detail::IterOfRange<RangeT>, PredicateT>;
502 FilterIteratorT(std::begin(std::forward<RangeT>(Range)),
503 std::end(std::forward<RangeT>(Range)), Pred),
504 FilterIteratorT(std::end(std::forward<RangeT>(Range)),
505 std::end(std::forward<RangeT>(Range)), Pred));
508 /// A pseudo-iterator adaptor that is designed to implement "early increment"
511 /// This is *not a normal iterator* and should almost never be used directly. It
512 /// is intended primarily to be used with range based for loops and some range
515 /// The iterator isn't quite an `OutputIterator` or an `InputIterator` but
516 /// somewhere between them. The constraints of these iterators are:
518 /// - On construction or after being incremented, it is comparable and
519 /// dereferencable. It is *not* incrementable.
520 /// - After being dereferenced, it is neither comparable nor dereferencable, it
521 /// is only incrementable.
523 /// This means you can only dereference the iterator once, and you can only
524 /// increment it once between dereferences.
525 template <typename WrappedIteratorT>
526 class early_inc_iterator_impl
527 : public iterator_adaptor_base<early_inc_iterator_impl<WrappedIteratorT>,
528 WrappedIteratorT, std::input_iterator_tag> {
530 iterator_adaptor_base<early_inc_iterator_impl<WrappedIteratorT>,
531 WrappedIteratorT, std::input_iterator_tag>;
533 using PointerT = typename std::iterator_traits<WrappedIteratorT>::pointer;
536 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
537 bool IsEarlyIncremented = false;
541 early_inc_iterator_impl(WrappedIteratorT I) : BaseT(I) {}
543 using BaseT::operator*;
544 typename BaseT::reference operator*() {
545 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
546 assert(!IsEarlyIncremented && "Cannot dereference twice!");
547 IsEarlyIncremented = true;
552 using BaseT::operator++;
553 early_inc_iterator_impl &operator++() {
554 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
555 assert(IsEarlyIncremented && "Cannot increment before dereferencing!");
556 IsEarlyIncremented = false;
561 using BaseT::operator==;
562 bool operator==(const early_inc_iterator_impl &RHS) const {
563 #if LLVM_ENABLE_ABI_BREAKING_CHECKS
564 assert(!IsEarlyIncremented && "Cannot compare after dereferencing!");
566 return BaseT::operator==(RHS);
570 /// Make a range that does early increment to allow mutation of the underlying
571 /// range without disrupting iteration.
573 /// The underlying iterator will be incremented immediately after it is
574 /// dereferenced, allowing deletion of the current node or insertion of nodes to
575 /// not disrupt iteration provided they do not invalidate the *next* iterator --
576 /// the current iterator can be invalidated.
578 /// This requires a very exact pattern of use that is only really suitable to
579 /// range based for loops and other range algorithms that explicitly guarantee
580 /// to dereference exactly once each element, and to increment exactly once each
582 template <typename RangeT>
583 iterator_range<early_inc_iterator_impl<detail::IterOfRange<RangeT>>>
584 make_early_inc_range(RangeT &&Range) {
585 using EarlyIncIteratorT =
586 early_inc_iterator_impl<detail::IterOfRange<RangeT>>;
587 return make_range(EarlyIncIteratorT(std::begin(std::forward<RangeT>(Range))),
588 EarlyIncIteratorT(std::end(std::forward<RangeT>(Range))));
591 // forward declarations required by zip_shortest/zip_first/zip_longest
592 template <typename R, typename UnaryPredicate>
593 bool all_of(R &&range, UnaryPredicate P);
594 template <typename R, typename UnaryPredicate>
595 bool any_of(R &&range, UnaryPredicate P);
601 // We have to alias this since inlining the actual type at the usage site
602 // in the parameter list of iterator_facade_base<> below ICEs MSVC 2017.
603 template<typename... Iters> struct ZipTupleType {
604 using type = std::tuple<decltype(*declval<Iters>())...>;
607 template <typename ZipType, typename... Iters>
608 using zip_traits = iterator_facade_base<
609 ZipType, typename std::common_type<std::bidirectional_iterator_tag,
610 typename std::iterator_traits<
611 Iters>::iterator_category...>::type,
612 // ^ TODO: Implement random access methods.
613 typename ZipTupleType<Iters...>::type,
614 typename std::iterator_traits<typename std::tuple_element<
615 0, std::tuple<Iters...>>::type>::difference_type,
616 // ^ FIXME: This follows boost::make_zip_iterator's assumption that all
617 // inner iterators have the same difference_type. It would fail if, for
618 // instance, the second field's difference_type were non-numeric while the
620 typename ZipTupleType<Iters...>::type *,
621 typename ZipTupleType<Iters...>::type>;
623 template <typename ZipType, typename... Iters>
624 struct zip_common : public zip_traits<ZipType, Iters...> {
625 using Base = zip_traits<ZipType, Iters...>;
626 using value_type = typename Base::value_type;
628 std::tuple<Iters...> iterators;
631 template <size_t... Ns> value_type deref(std::index_sequence<Ns...>) const {
632 return value_type(*std::get<Ns>(iterators)...);
635 template <size_t... Ns>
636 decltype(iterators) tup_inc(std::index_sequence<Ns...>) const {
637 return std::tuple<Iters...>(std::next(std::get<Ns>(iterators))...);
640 template <size_t... Ns>
641 decltype(iterators) tup_dec(std::index_sequence<Ns...>) const {
642 return std::tuple<Iters...>(std::prev(std::get<Ns>(iterators))...);
646 zip_common(Iters &&... ts) : iterators(std::forward<Iters>(ts)...) {}
648 value_type operator*() { return deref(std::index_sequence_for<Iters...>{}); }
650 const value_type operator*() const {
651 return deref(std::index_sequence_for<Iters...>{});
654 ZipType &operator++() {
655 iterators = tup_inc(std::index_sequence_for<Iters...>{});
656 return *reinterpret_cast<ZipType *>(this);
659 ZipType &operator--() {
660 static_assert(Base::IsBidirectional,
661 "All inner iterators must be at least bidirectional.");
662 iterators = tup_dec(std::index_sequence_for<Iters...>{});
663 return *reinterpret_cast<ZipType *>(this);
667 template <typename... Iters>
668 struct zip_first : public zip_common<zip_first<Iters...>, Iters...> {
669 using Base = zip_common<zip_first<Iters...>, Iters...>;
671 bool operator==(const zip_first<Iters...> &other) const {
672 return std::get<0>(this->iterators) == std::get<0>(other.iterators);
675 zip_first(Iters &&... ts) : Base(std::forward<Iters>(ts)...) {}
678 template <typename... Iters>
679 class zip_shortest : public zip_common<zip_shortest<Iters...>, Iters...> {
680 template <size_t... Ns>
681 bool test(const zip_shortest<Iters...> &other,
682 std::index_sequence<Ns...>) const {
683 return all_of(std::initializer_list<bool>{std::get<Ns>(this->iterators) !=
684 std::get<Ns>(other.iterators)...},
689 using Base = zip_common<zip_shortest<Iters...>, Iters...>;
691 zip_shortest(Iters &&... ts) : Base(std::forward<Iters>(ts)...) {}
693 bool operator==(const zip_shortest<Iters...> &other) const {
694 return !test(other, std::index_sequence_for<Iters...>{});
698 template <template <typename...> class ItType, typename... Args> class zippy {
700 using iterator = ItType<decltype(std::begin(std::declval<Args>()))...>;
701 using iterator_category = typename iterator::iterator_category;
702 using value_type = typename iterator::value_type;
703 using difference_type = typename iterator::difference_type;
704 using pointer = typename iterator::pointer;
705 using reference = typename iterator::reference;
708 std::tuple<Args...> ts;
710 template <size_t... Ns>
711 iterator begin_impl(std::index_sequence<Ns...>) const {
712 return iterator(std::begin(std::get<Ns>(ts))...);
714 template <size_t... Ns> iterator end_impl(std::index_sequence<Ns...>) const {
715 return iterator(std::end(std::get<Ns>(ts))...);
719 zippy(Args &&... ts_) : ts(std::forward<Args>(ts_)...) {}
721 iterator begin() const {
722 return begin_impl(std::index_sequence_for<Args...>{});
724 iterator end() const { return end_impl(std::index_sequence_for<Args...>{}); }
727 } // end namespace detail
729 /// zip iterator for two or more iteratable types.
730 template <typename T, typename U, typename... Args>
731 detail::zippy<detail::zip_shortest, T, U, Args...> zip(T &&t, U &&u,
733 return detail::zippy<detail::zip_shortest, T, U, Args...>(
734 std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...);
737 /// zip iterator that, for the sake of efficiency, assumes the first iteratee to
739 template <typename T, typename U, typename... Args>
740 detail::zippy<detail::zip_first, T, U, Args...> zip_first(T &&t, U &&u,
742 return detail::zippy<detail::zip_first, T, U, Args...>(
743 std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...);
747 template <typename Iter>
748 Iter next_or_end(const Iter &I, const Iter &End) {
754 template <typename Iter>
755 auto deref_or_none(const Iter &I, const Iter &End) -> llvm::Optional<
756 std::remove_const_t<std::remove_reference_t<decltype(*I)>>> {
762 template <typename Iter> struct ZipLongestItemType {
764 llvm::Optional<typename std::remove_const<typename std::remove_reference<
765 decltype(*std::declval<Iter>())>::type>::type>;
768 template <typename... Iters> struct ZipLongestTupleType {
769 using type = std::tuple<typename ZipLongestItemType<Iters>::type...>;
772 template <typename... Iters>
773 class zip_longest_iterator
774 : public iterator_facade_base<
775 zip_longest_iterator<Iters...>,
776 typename std::common_type<
777 std::forward_iterator_tag,
778 typename std::iterator_traits<Iters>::iterator_category...>::type,
779 typename ZipLongestTupleType<Iters...>::type,
780 typename std::iterator_traits<typename std::tuple_element<
781 0, std::tuple<Iters...>>::type>::difference_type,
782 typename ZipLongestTupleType<Iters...>::type *,
783 typename ZipLongestTupleType<Iters...>::type> {
785 using value_type = typename ZipLongestTupleType<Iters...>::type;
788 std::tuple<Iters...> iterators;
789 std::tuple<Iters...> end_iterators;
791 template <size_t... Ns>
792 bool test(const zip_longest_iterator<Iters...> &other,
793 std::index_sequence<Ns...>) const {
795 std::initializer_list<bool>{std::get<Ns>(this->iterators) !=
796 std::get<Ns>(other.iterators)...},
800 template <size_t... Ns> value_type deref(std::index_sequence<Ns...>) const {
802 deref_or_none(std::get<Ns>(iterators), std::get<Ns>(end_iterators))...);
805 template <size_t... Ns>
806 decltype(iterators) tup_inc(std::index_sequence<Ns...>) const {
807 return std::tuple<Iters...>(
808 next_or_end(std::get<Ns>(iterators), std::get<Ns>(end_iterators))...);
812 zip_longest_iterator(std::pair<Iters &&, Iters &&>... ts)
813 : iterators(std::forward<Iters>(ts.first)...),
814 end_iterators(std::forward<Iters>(ts.second)...) {}
816 value_type operator*() { return deref(std::index_sequence_for<Iters...>{}); }
818 value_type operator*() const {
819 return deref(std::index_sequence_for<Iters...>{});
822 zip_longest_iterator<Iters...> &operator++() {
823 iterators = tup_inc(std::index_sequence_for<Iters...>{});
827 bool operator==(const zip_longest_iterator<Iters...> &other) const {
828 return !test(other, std::index_sequence_for<Iters...>{});
832 template <typename... Args> class zip_longest_range {
835 zip_longest_iterator<decltype(adl_begin(std::declval<Args>()))...>;
836 using iterator_category = typename iterator::iterator_category;
837 using value_type = typename iterator::value_type;
838 using difference_type = typename iterator::difference_type;
839 using pointer = typename iterator::pointer;
840 using reference = typename iterator::reference;
843 std::tuple<Args...> ts;
845 template <size_t... Ns>
846 iterator begin_impl(std::index_sequence<Ns...>) const {
847 return iterator(std::make_pair(adl_begin(std::get<Ns>(ts)),
848 adl_end(std::get<Ns>(ts)))...);
851 template <size_t... Ns> iterator end_impl(std::index_sequence<Ns...>) const {
852 return iterator(std::make_pair(adl_end(std::get<Ns>(ts)),
853 adl_end(std::get<Ns>(ts)))...);
857 zip_longest_range(Args &&... ts_) : ts(std::forward<Args>(ts_)...) {}
859 iterator begin() const {
860 return begin_impl(std::index_sequence_for<Args...>{});
862 iterator end() const { return end_impl(std::index_sequence_for<Args...>{}); }
864 } // namespace detail
866 /// Iterate over two or more iterators at the same time. Iteration continues
867 /// until all iterators reach the end. The llvm::Optional only contains a value
868 /// if the iterator has not reached the end.
869 template <typename T, typename U, typename... Args>
870 detail::zip_longest_range<T, U, Args...> zip_longest(T &&t, U &&u,
872 return detail::zip_longest_range<T, U, Args...>(
873 std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...);
876 /// Iterator wrapper that concatenates sequences together.
878 /// This can concatenate different iterators, even with different types, into
879 /// a single iterator provided the value types of all the concatenated
880 /// iterators expose `reference` and `pointer` types that can be converted to
881 /// `ValueT &` and `ValueT *` respectively. It doesn't support more
882 /// interesting/customized pointer or reference types.
884 /// Currently this only supports forward or higher iterator categories as
885 /// inputs and always exposes a forward iterator interface.
886 template <typename ValueT, typename... IterTs>
887 class concat_iterator
888 : public iterator_facade_base<concat_iterator<ValueT, IterTs...>,
889 std::forward_iterator_tag, ValueT> {
890 using BaseT = typename concat_iterator::iterator_facade_base;
892 /// We store both the current and end iterators for each concatenated
893 /// sequence in a tuple of pairs.
895 /// Note that something like iterator_range seems nice at first here, but the
896 /// range properties are of little benefit and end up getting in the way
897 /// because we need to do mutation on the current iterators.
898 std::tuple<IterTs...> Begins;
899 std::tuple<IterTs...> Ends;
901 /// Attempts to increment a specific iterator.
903 /// Returns true if it was able to increment the iterator. Returns false if
904 /// the iterator is already at the end iterator.
905 template <size_t Index> bool incrementHelper() {
906 auto &Begin = std::get<Index>(Begins);
907 auto &End = std::get<Index>(Ends);
915 /// Increments the first non-end iterator.
917 /// It is an error to call this with all iterators at the end.
918 template <size_t... Ns> void increment(std::index_sequence<Ns...>) {
919 // Build a sequence of functions to increment each iterator if possible.
920 bool (concat_iterator::*IncrementHelperFns[])() = {
921 &concat_iterator::incrementHelper<Ns>...};
923 // Loop over them, and stop as soon as we succeed at incrementing one.
924 for (auto &IncrementHelperFn : IncrementHelperFns)
925 if ((this->*IncrementHelperFn)())
928 llvm_unreachable("Attempted to increment an end concat iterator!");
931 /// Returns null if the specified iterator is at the end. Otherwise,
932 /// dereferences the iterator and returns the address of the resulting
934 template <size_t Index> ValueT *getHelper() const {
935 auto &Begin = std::get<Index>(Begins);
936 auto &End = std::get<Index>(Ends);
943 /// Finds the first non-end iterator, dereferences, and returns the resulting
946 /// It is an error to call this with all iterators at the end.
947 template <size_t... Ns> ValueT &get(std::index_sequence<Ns...>) const {
948 // Build a sequence of functions to get from iterator if possible.
949 ValueT *(concat_iterator::*GetHelperFns[])() const = {
950 &concat_iterator::getHelper<Ns>...};
952 // Loop over them, and return the first result we find.
953 for (auto &GetHelperFn : GetHelperFns)
954 if (ValueT *P = (this->*GetHelperFn)())
957 llvm_unreachable("Attempted to get a pointer from an end concat iterator!");
961 /// Constructs an iterator from a sequence of ranges.
963 /// We need the full range to know how to switch between each of the
965 template <typename... RangeTs>
966 explicit concat_iterator(RangeTs &&... Ranges)
967 : Begins(std::begin(Ranges)...), Ends(std::end(Ranges)...) {}
969 using BaseT::operator++;
971 concat_iterator &operator++() {
972 increment(std::index_sequence_for<IterTs...>());
976 ValueT &operator*() const {
977 return get(std::index_sequence_for<IterTs...>());
980 bool operator==(const concat_iterator &RHS) const {
981 return Begins == RHS.Begins && Ends == RHS.Ends;
987 /// Helper to store a sequence of ranges being concatenated and access them.
989 /// This is designed to facilitate providing actual storage when temporaries
990 /// are passed into the constructor such that we can use it as part of range
992 template <typename ValueT, typename... RangeTs> class concat_range {
995 concat_iterator<ValueT,
996 decltype(std::begin(std::declval<RangeTs &>()))...>;
999 std::tuple<RangeTs...> Ranges;
1001 template <size_t... Ns> iterator begin_impl(std::index_sequence<Ns...>) {
1002 return iterator(std::get<Ns>(Ranges)...);
1004 template <size_t... Ns> iterator end_impl(std::index_sequence<Ns...>) {
1005 return iterator(make_range(std::end(std::get<Ns>(Ranges)),
1006 std::end(std::get<Ns>(Ranges)))...);
1010 concat_range(RangeTs &&... Ranges)
1011 : Ranges(std::forward<RangeTs>(Ranges)...) {}
1013 iterator begin() { return begin_impl(std::index_sequence_for<RangeTs...>{}); }
1014 iterator end() { return end_impl(std::index_sequence_for<RangeTs...>{}); }
1017 } // end namespace detail
1019 /// Concatenated range across two or more ranges.
1021 /// The desired value type must be explicitly specified.
1022 template <typename ValueT, typename... RangeTs>
1023 detail::concat_range<ValueT, RangeTs...> concat(RangeTs &&... Ranges) {
1024 static_assert(sizeof...(RangeTs) > 1,
1025 "Need more than one range to concatenate!");
1026 return detail::concat_range<ValueT, RangeTs...>(
1027 std::forward<RangeTs>(Ranges)...);
1030 /// A utility class used to implement an iterator that contains some base object
1031 /// and an index. The iterator moves the index but keeps the base constant.
1032 template <typename DerivedT, typename BaseT, typename T,
1033 typename PointerT = T *, typename ReferenceT = T &>
1034 class indexed_accessor_iterator
1035 : public llvm::iterator_facade_base<DerivedT,
1036 std::random_access_iterator_tag, T,
1037 std::ptrdiff_t, PointerT, ReferenceT> {
1039 ptrdiff_t operator-(const indexed_accessor_iterator &rhs) const {
1040 assert(base == rhs.base && "incompatible iterators");
1041 return index - rhs.index;
1043 bool operator==(const indexed_accessor_iterator &rhs) const {
1044 return base == rhs.base && index == rhs.index;
1046 bool operator<(const indexed_accessor_iterator &rhs) const {
1047 assert(base == rhs.base && "incompatible iterators");
1048 return index < rhs.index;
1051 DerivedT &operator+=(ptrdiff_t offset) {
1052 this->index += offset;
1053 return static_cast<DerivedT &>(*this);
1055 DerivedT &operator-=(ptrdiff_t offset) {
1056 this->index -= offset;
1057 return static_cast<DerivedT &>(*this);
1060 /// Returns the current index of the iterator.
1061 ptrdiff_t getIndex() const { return index; }
1063 /// Returns the current base of the iterator.
1064 const BaseT &getBase() const { return base; }
1067 indexed_accessor_iterator(BaseT base, ptrdiff_t index)
1068 : base(base), index(index) {}
1074 /// The class represents the base of a range of indexed_accessor_iterators. It
1075 /// provides support for many different range functionalities, e.g.
1076 /// drop_front/slice/etc.. Derived range classes must implement the following
1078 /// * ReferenceT dereference_iterator(const BaseT &base, ptrdiff_t index)
1079 /// - Dereference an iterator pointing to the base object at the given
1081 /// * BaseT offset_base(const BaseT &base, ptrdiff_t index)
1082 /// - Return a new base that is offset from the provide base by 'index'
1084 template <typename DerivedT, typename BaseT, typename T,
1085 typename PointerT = T *, typename ReferenceT = T &>
1086 class indexed_accessor_range_base {
1089 indexed_accessor_range_base<DerivedT, BaseT, T, PointerT, ReferenceT>;
1091 /// An iterator element of this range.
1092 class iterator : public indexed_accessor_iterator<iterator, BaseT, T,
1093 PointerT, ReferenceT> {
1095 // Index into this iterator, invoking a static method on the derived type.
1096 ReferenceT operator*() const {
1097 return DerivedT::dereference_iterator(this->getBase(), this->getIndex());
1101 iterator(BaseT owner, ptrdiff_t curIndex)
1102 : indexed_accessor_iterator<iterator, BaseT, T, PointerT, ReferenceT>(
1105 /// Allow access to the constructor.
1106 friend indexed_accessor_range_base<DerivedT, BaseT, T, PointerT,
1110 indexed_accessor_range_base(iterator begin, iterator end)
1111 : base(offset_base(begin.getBase(), begin.getIndex())),
1112 count(end.getIndex() - begin.getIndex()) {}
1113 indexed_accessor_range_base(const iterator_range<iterator> &range)
1114 : indexed_accessor_range_base(range.begin(), range.end()) {}
1115 indexed_accessor_range_base(BaseT base, ptrdiff_t count)
1116 : base(base), count(count) {}
1118 iterator begin() const { return iterator(base, 0); }
1119 iterator end() const { return iterator(base, count); }
1120 ReferenceT operator[](unsigned index) const {
1121 assert(index < size() && "invalid index for value range");
1122 return DerivedT::dereference_iterator(base, index);
1124 ReferenceT front() const {
1125 assert(!empty() && "expected non-empty range");
1128 ReferenceT back() const {
1129 assert(!empty() && "expected non-empty range");
1130 return (*this)[size() - 1];
1133 /// Compare this range with another.
1134 template <typename OtherT> bool operator==(const OtherT &other) const {
1136 static_cast<size_t>(std::distance(other.begin(), other.end())) &&
1137 std::equal(begin(), end(), other.begin());
1139 template <typename OtherT> bool operator!=(const OtherT &other) const {
1140 return !(*this == other);
1143 /// Return the size of this range.
1144 size_t size() const { return count; }
1146 /// Return if the range is empty.
1147 bool empty() const { return size() == 0; }
1149 /// Drop the first N elements, and keep M elements.
1150 DerivedT slice(size_t n, size_t m) const {
1151 assert(n + m <= size() && "invalid size specifiers");
1152 return DerivedT(offset_base(base, n), m);
1155 /// Drop the first n elements.
1156 DerivedT drop_front(size_t n = 1) const {
1157 assert(size() >= n && "Dropping more elements than exist");
1158 return slice(n, size() - n);
1160 /// Drop the last n elements.
1161 DerivedT drop_back(size_t n = 1) const {
1162 assert(size() >= n && "Dropping more elements than exist");
1163 return DerivedT(base, size() - n);
1166 /// Take the first n elements.
1167 DerivedT take_front(size_t n = 1) const {
1168 return n < size() ? drop_back(size() - n)
1169 : static_cast<const DerivedT &>(*this);
1172 /// Take the last n elements.
1173 DerivedT take_back(size_t n = 1) const {
1174 return n < size() ? drop_front(size() - n)
1175 : static_cast<const DerivedT &>(*this);
1178 /// Allow conversion to any type accepting an iterator_range.
1179 template <typename RangeT, typename = std::enable_if_t<std::is_constructible<
1180 RangeT, iterator_range<iterator>>::value>>
1181 operator RangeT() const {
1182 return RangeT(iterator_range<iterator>(*this));
1185 /// Returns the base of this range.
1186 const BaseT &getBase() const { return base; }
1189 /// Offset the given base by the given amount.
1190 static BaseT offset_base(const BaseT &base, size_t n) {
1191 return n == 0 ? base : DerivedT::offset_base(base, n);
1195 indexed_accessor_range_base(const indexed_accessor_range_base &) = default;
1196 indexed_accessor_range_base(indexed_accessor_range_base &&) = default;
1197 indexed_accessor_range_base &
1198 operator=(const indexed_accessor_range_base &) = default;
1200 /// The base that owns the provided range of values.
1202 /// The size from the owning range.
1205 } // end namespace detail
1207 /// This class provides an implementation of a range of
1208 /// indexed_accessor_iterators where the base is not indexable. Ranges with
1209 /// bases that are offsetable should derive from indexed_accessor_range_base
1210 /// instead. Derived range classes are expected to implement the following
1212 /// * ReferenceT dereference(const BaseT &base, ptrdiff_t index)
1213 /// - Dereference an iterator pointing to a parent base at the given index.
1214 template <typename DerivedT, typename BaseT, typename T,
1215 typename PointerT = T *, typename ReferenceT = T &>
1216 class indexed_accessor_range
1217 : public detail::indexed_accessor_range_base<
1218 DerivedT, std::pair<BaseT, ptrdiff_t>, T, PointerT, ReferenceT> {
1220 indexed_accessor_range(BaseT base, ptrdiff_t startIndex, ptrdiff_t count)
1221 : detail::indexed_accessor_range_base<
1222 DerivedT, std::pair<BaseT, ptrdiff_t>, T, PointerT, ReferenceT>(
1223 std::make_pair(base, startIndex), count) {}
1224 using detail::indexed_accessor_range_base<
1225 DerivedT, std::pair<BaseT, ptrdiff_t>, T, PointerT,
1226 ReferenceT>::indexed_accessor_range_base;
1228 /// Returns the current base of the range.
1229 const BaseT &getBase() const { return this->base.first; }
1231 /// Returns the current start index of the range.
1232 ptrdiff_t getStartIndex() const { return this->base.second; }
1234 /// See `detail::indexed_accessor_range_base` for details.
1235 static std::pair<BaseT, ptrdiff_t>
1236 offset_base(const std::pair<BaseT, ptrdiff_t> &base, ptrdiff_t index) {
1237 // We encode the internal base as a pair of the derived base and a start
1238 // index into the derived base.
1239 return std::make_pair(base.first, base.second + index);
1241 /// See `detail::indexed_accessor_range_base` for details.
1243 dereference_iterator(const std::pair<BaseT, ptrdiff_t> &base,
1245 return DerivedT::dereference(base.first, base.second + index);
1249 /// Given a container of pairs, return a range over the second elements.
1250 template <typename ContainerTy> auto make_second_range(ContainerTy &&c) {
1251 return llvm::map_range(
1252 std::forward<ContainerTy>(c),
1253 [](decltype((*std::begin(c))) elt) -> decltype((elt.second)) {
1258 //===----------------------------------------------------------------------===//
1259 // Extra additions to <utility>
1260 //===----------------------------------------------------------------------===//
1262 /// Function object to check whether the first component of a std::pair
1263 /// compares less than the first component of another std::pair.
1265 template <typename T> bool operator()(const T &lhs, const T &rhs) const {
1266 return lhs.first < rhs.first;
1270 /// Function object to check whether the second component of a std::pair
1271 /// compares less than the second component of another std::pair.
1272 struct less_second {
1273 template <typename T> bool operator()(const T &lhs, const T &rhs) const {
1274 return lhs.second < rhs.second;
1278 /// \brief Function object to apply a binary function to the first component of
1280 template<typename FuncTy>
1284 template <typename T>
1285 decltype(auto) operator()(const T &lhs, const T &rhs) const {
1286 return func(lhs.first, rhs.first);
1290 /// Utility type to build an inheritance chain that makes it easy to rank
1291 /// overload candidates.
1292 template <int N> struct rank : rank<N - 1> {};
1293 template <> struct rank<0> {};
1295 /// traits class for checking whether type T is one of any of the given
1296 /// types in the variadic list.
1297 template <typename T, typename... Ts> struct is_one_of {
1298 static const bool value = false;
1301 template <typename T, typename U, typename... Ts>
1302 struct is_one_of<T, U, Ts...> {
1303 static const bool value =
1304 std::is_same<T, U>::value || is_one_of<T, Ts...>::value;
1307 /// traits class for checking whether type T is a base class for all
1308 /// the given types in the variadic list.
1309 template <typename T, typename... Ts> struct are_base_of {
1310 static const bool value = true;
1313 template <typename T, typename U, typename... Ts>
1314 struct are_base_of<T, U, Ts...> {
1315 static const bool value =
1316 std::is_base_of<T, U>::value && are_base_of<T, Ts...>::value;
1319 //===----------------------------------------------------------------------===//
1320 // Extra additions for arrays
1321 //===----------------------------------------------------------------------===//
1323 // We have a copy here so that LLVM behaves the same when using different
1324 // standard libraries.
1325 template <class Iterator, class RNG>
1326 void shuffle(Iterator first, Iterator last, RNG &&g) {
1327 // It would be better to use a std::uniform_int_distribution,
1328 // but that would be stdlib dependent.
1329 for (auto size = last - first; size > 1; ++first, (void)--size)
1330 std::iter_swap(first, first + g() % size);
1333 /// Find the length of an array.
1334 template <class T, std::size_t N>
1335 constexpr inline size_t array_lengthof(T (&)[N]) {
1339 /// Adapt std::less<T> for array_pod_sort.
1340 template<typename T>
1341 inline int array_pod_sort_comparator(const void *P1, const void *P2) {
1342 if (std::less<T>()(*reinterpret_cast<const T*>(P1),
1343 *reinterpret_cast<const T*>(P2)))
1345 if (std::less<T>()(*reinterpret_cast<const T*>(P2),
1346 *reinterpret_cast<const T*>(P1)))
1351 /// get_array_pod_sort_comparator - This is an internal helper function used to
1352 /// get type deduction of T right.
1353 template<typename T>
1354 inline int (*get_array_pod_sort_comparator(const T &))
1355 (const void*, const void*) {
1356 return array_pod_sort_comparator<T>;
1359 #ifdef EXPENSIVE_CHECKS
1362 inline unsigned presortShuffleEntropy() {
1363 static unsigned Result(std::random_device{}());
1367 template <class IteratorTy>
1368 inline void presortShuffle(IteratorTy Start, IteratorTy End) {
1369 std::mt19937 Generator(presortShuffleEntropy());
1370 std::shuffle(Start, End, Generator);
1373 } // end namespace detail
1376 /// array_pod_sort - This sorts an array with the specified start and end
1377 /// extent. This is just like std::sort, except that it calls qsort instead of
1378 /// using an inlined template. qsort is slightly slower than std::sort, but
1379 /// most sorts are not performance critical in LLVM and std::sort has to be
1380 /// template instantiated for each type, leading to significant measured code
1381 /// bloat. This function should generally be used instead of std::sort where
1384 /// This function assumes that you have simple POD-like types that can be
1385 /// compared with std::less and can be moved with memcpy. If this isn't true,
1386 /// you should use std::sort.
1388 /// NOTE: If qsort_r were portable, we could allow a custom comparator and
1389 /// default to std::less.
1390 template<class IteratorTy>
1391 inline void array_pod_sort(IteratorTy Start, IteratorTy End) {
1392 // Don't inefficiently call qsort with one element or trigger undefined
1393 // behavior with an empty sequence.
1394 auto NElts = End - Start;
1395 if (NElts <= 1) return;
1396 #ifdef EXPENSIVE_CHECKS
1397 detail::presortShuffle<IteratorTy>(Start, End);
1399 qsort(&*Start, NElts, sizeof(*Start), get_array_pod_sort_comparator(*Start));
1402 template <class IteratorTy>
1403 inline void array_pod_sort(
1404 IteratorTy Start, IteratorTy End,
1406 const typename std::iterator_traits<IteratorTy>::value_type *,
1407 const typename std::iterator_traits<IteratorTy>::value_type *)) {
1408 // Don't inefficiently call qsort with one element or trigger undefined
1409 // behavior with an empty sequence.
1410 auto NElts = End - Start;
1411 if (NElts <= 1) return;
1412 #ifdef EXPENSIVE_CHECKS
1413 detail::presortShuffle<IteratorTy>(Start, End);
1415 qsort(&*Start, NElts, sizeof(*Start),
1416 reinterpret_cast<int (*)(const void *, const void *)>(Compare));
1420 template <typename T>
1421 // We can use qsort if the iterator type is a pointer and the underlying value
1422 // is trivially copyable.
1423 using sort_trivially_copyable = conjunction<
1425 is_trivially_copyable<typename std::iterator_traits<T>::value_type>>;
1426 } // namespace detail
1428 // Provide wrappers to std::sort which shuffle the elements before sorting
1429 // to help uncover non-deterministic behavior (PR35135).
1430 template <typename IteratorTy,
1431 std::enable_if_t<!detail::sort_trivially_copyable<IteratorTy>::value,
1433 inline void sort(IteratorTy Start, IteratorTy End) {
1434 #ifdef EXPENSIVE_CHECKS
1435 detail::presortShuffle<IteratorTy>(Start, End);
1437 std::sort(Start, End);
1440 // Forward trivially copyable types to array_pod_sort. This avoids a large
1441 // amount of code bloat for a minor performance hit.
1442 template <typename IteratorTy,
1443 std::enable_if_t<detail::sort_trivially_copyable<IteratorTy>::value,
1445 inline void sort(IteratorTy Start, IteratorTy End) {
1446 array_pod_sort(Start, End);
1449 template <typename Container> inline void sort(Container &&C) {
1450 llvm::sort(adl_begin(C), adl_end(C));
1453 template <typename IteratorTy, typename Compare>
1454 inline void sort(IteratorTy Start, IteratorTy End, Compare Comp) {
1455 #ifdef EXPENSIVE_CHECKS
1456 detail::presortShuffle<IteratorTy>(Start, End);
1458 std::sort(Start, End, Comp);
1461 template <typename Container, typename Compare>
1462 inline void sort(Container &&C, Compare Comp) {
1463 llvm::sort(adl_begin(C), adl_end(C), Comp);
1466 //===----------------------------------------------------------------------===//
1467 // Extra additions to <algorithm>
1468 //===----------------------------------------------------------------------===//
1470 /// Get the size of a range. This is a wrapper function around std::distance
1471 /// which is only enabled when the operation is O(1).
1472 template <typename R>
1473 auto size(R &&Range,
1474 std::enable_if_t<std::is_same<typename std::iterator_traits<decltype(
1475 Range.begin())>::iterator_category,
1476 std::random_access_iterator_tag>::value,
1477 void> * = nullptr) {
1478 return std::distance(Range.begin(), Range.end());
1481 /// Provide wrappers to std::for_each which take ranges instead of having to
1482 /// pass begin/end explicitly.
1483 template <typename R, typename UnaryPredicate>
1484 UnaryPredicate for_each(R &&Range, UnaryPredicate P) {
1485 return std::for_each(adl_begin(Range), adl_end(Range), P);
1488 /// Provide wrappers to std::all_of which take ranges instead of having to pass
1489 /// begin/end explicitly.
1490 template <typename R, typename UnaryPredicate>
1491 bool all_of(R &&Range, UnaryPredicate P) {
1492 return std::all_of(adl_begin(Range), adl_end(Range), P);
1495 /// Provide wrappers to std::any_of which take ranges instead of having to pass
1496 /// begin/end explicitly.
1497 template <typename R, typename UnaryPredicate>
1498 bool any_of(R &&Range, UnaryPredicate P) {
1499 return std::any_of(adl_begin(Range), adl_end(Range), P);
1502 /// Provide wrappers to std::none_of which take ranges instead of having to pass
1503 /// begin/end explicitly.
1504 template <typename R, typename UnaryPredicate>
1505 bool none_of(R &&Range, UnaryPredicate P) {
1506 return std::none_of(adl_begin(Range), adl_end(Range), P);
1509 /// Provide wrappers to std::find which take ranges instead of having to pass
1510 /// begin/end explicitly.
1511 template <typename R, typename T> auto find(R &&Range, const T &Val) {
1512 return std::find(adl_begin(Range), adl_end(Range), Val);
1515 /// Provide wrappers to std::find_if which take ranges instead of having to pass
1516 /// begin/end explicitly.
1517 template <typename R, typename UnaryPredicate>
1518 auto find_if(R &&Range, UnaryPredicate P) {
1519 return std::find_if(adl_begin(Range), adl_end(Range), P);
1522 template <typename R, typename UnaryPredicate>
1523 auto find_if_not(R &&Range, UnaryPredicate P) {
1524 return std::find_if_not(adl_begin(Range), adl_end(Range), P);
1527 /// Provide wrappers to std::remove_if which take ranges instead of having to
1528 /// pass begin/end explicitly.
1529 template <typename R, typename UnaryPredicate>
1530 auto remove_if(R &&Range, UnaryPredicate P) {
1531 return std::remove_if(adl_begin(Range), adl_end(Range), P);
1534 /// Provide wrappers to std::copy_if which take ranges instead of having to
1535 /// pass begin/end explicitly.
1536 template <typename R, typename OutputIt, typename UnaryPredicate>
1537 OutputIt copy_if(R &&Range, OutputIt Out, UnaryPredicate P) {
1538 return std::copy_if(adl_begin(Range), adl_end(Range), Out, P);
1541 template <typename R, typename OutputIt>
1542 OutputIt copy(R &&Range, OutputIt Out) {
1543 return std::copy(adl_begin(Range), adl_end(Range), Out);
1546 /// Wrapper function around std::find to detect if an element exists
1548 template <typename R, typename E>
1549 bool is_contained(R &&Range, const E &Element) {
1550 return std::find(adl_begin(Range), adl_end(Range), Element) != adl_end(Range);
1553 /// Wrapper function around std::is_sorted to check if elements in a range \p R
1554 /// are sorted with respect to a comparator \p C.
1555 template <typename R, typename Compare> bool is_sorted(R &&Range, Compare C) {
1556 return std::is_sorted(adl_begin(Range), adl_end(Range), C);
1559 /// Wrapper function around std::is_sorted to check if elements in a range \p R
1560 /// are sorted in non-descending order.
1561 template <typename R> bool is_sorted(R &&Range) {
1562 return std::is_sorted(adl_begin(Range), adl_end(Range));
1565 /// Wrapper function around std::count to count the number of times an element
1566 /// \p Element occurs in the given range \p Range.
1567 template <typename R, typename E> auto count(R &&Range, const E &Element) {
1568 return std::count(adl_begin(Range), adl_end(Range), Element);
1571 /// Wrapper function around std::count_if to count the number of times an
1572 /// element satisfying a given predicate occurs in a range.
1573 template <typename R, typename UnaryPredicate>
1574 auto count_if(R &&Range, UnaryPredicate P) {
1575 return std::count_if(adl_begin(Range), adl_end(Range), P);
1578 /// Wrapper function around std::transform to apply a function to a range and
1579 /// store the result elsewhere.
1580 template <typename R, typename OutputIt, typename UnaryPredicate>
1581 OutputIt transform(R &&Range, OutputIt d_first, UnaryPredicate P) {
1582 return std::transform(adl_begin(Range), adl_end(Range), d_first, P);
1585 /// Provide wrappers to std::partition which take ranges instead of having to
1586 /// pass begin/end explicitly.
1587 template <typename R, typename UnaryPredicate>
1588 auto partition(R &&Range, UnaryPredicate P) {
1589 return std::partition(adl_begin(Range), adl_end(Range), P);
1592 /// Provide wrappers to std::lower_bound which take ranges instead of having to
1593 /// pass begin/end explicitly.
1594 template <typename R, typename T> auto lower_bound(R &&Range, T &&Value) {
1595 return std::lower_bound(adl_begin(Range), adl_end(Range),
1596 std::forward<T>(Value));
1599 template <typename R, typename T, typename Compare>
1600 auto lower_bound(R &&Range, T &&Value, Compare C) {
1601 return std::lower_bound(adl_begin(Range), adl_end(Range),
1602 std::forward<T>(Value), C);
1605 /// Provide wrappers to std::upper_bound which take ranges instead of having to
1606 /// pass begin/end explicitly.
1607 template <typename R, typename T> auto upper_bound(R &&Range, T &&Value) {
1608 return std::upper_bound(adl_begin(Range), adl_end(Range),
1609 std::forward<T>(Value));
1612 template <typename R, typename T, typename Compare>
1613 auto upper_bound(R &&Range, T &&Value, Compare C) {
1614 return std::upper_bound(adl_begin(Range), adl_end(Range),
1615 std::forward<T>(Value), C);
1618 template <typename R>
1619 void stable_sort(R &&Range) {
1620 std::stable_sort(adl_begin(Range), adl_end(Range));
1623 template <typename R, typename Compare>
1624 void stable_sort(R &&Range, Compare C) {
1625 std::stable_sort(adl_begin(Range), adl_end(Range), C);
1628 /// Binary search for the first iterator in a range where a predicate is false.
1629 /// Requires that C is always true below some limit, and always false above it.
1630 template <typename R, typename Predicate,
1631 typename Val = decltype(*adl_begin(std::declval<R>()))>
1632 auto partition_point(R &&Range, Predicate P) {
1633 return std::partition_point(adl_begin(Range), adl_end(Range), P);
1636 /// Wrapper function around std::equal to detect if all elements
1637 /// in a container are same.
1638 template <typename R>
1639 bool is_splat(R &&Range) {
1640 size_t range_size = size(Range);
1641 return range_size != 0 && (range_size == 1 ||
1642 std::equal(adl_begin(Range) + 1, adl_end(Range), adl_begin(Range)));
1645 /// Provide a container algorithm similar to C++ Library Fundamentals v2's
1646 /// `erase_if` which is equivalent to:
1648 /// C.erase(remove_if(C, pred), C.end());
1650 /// This version works for any container with an erase method call accepting
1652 template <typename Container, typename UnaryPredicate>
1653 void erase_if(Container &C, UnaryPredicate P) {
1654 C.erase(remove_if(C, P), C.end());
1657 /// Given a sequence container Cont, replace the range [ContIt, ContEnd) with
1658 /// the range [ValIt, ValEnd) (which is not from the same container).
1659 template<typename Container, typename RandomAccessIterator>
1660 void replace(Container &Cont, typename Container::iterator ContIt,
1661 typename Container::iterator ContEnd, RandomAccessIterator ValIt,
1662 RandomAccessIterator ValEnd) {
1664 if (ValIt == ValEnd) {
1665 Cont.erase(ContIt, ContEnd);
1667 } else if (ContIt == ContEnd) {
1668 Cont.insert(ContIt, ValIt, ValEnd);
1671 *ContIt++ = *ValIt++;
1675 /// Given a sequence container Cont, replace the range [ContIt, ContEnd) with
1677 template<typename Container, typename Range = std::initializer_list<
1678 typename Container::value_type>>
1679 void replace(Container &Cont, typename Container::iterator ContIt,
1680 typename Container::iterator ContEnd, Range R) {
1681 replace(Cont, ContIt, ContEnd, R.begin(), R.end());
1684 /// An STL-style algorithm similar to std::for_each that applies a second
1685 /// functor between every pair of elements.
1687 /// This provides the control flow logic to, for example, print a
1688 /// comma-separated list:
1690 /// interleave(names.begin(), names.end(),
1691 /// [&](StringRef name) { os << name; },
1692 /// [&] { os << ", "; });
1694 template <typename ForwardIterator, typename UnaryFunctor,
1695 typename NullaryFunctor,
1696 typename = typename std::enable_if<
1697 !std::is_constructible<StringRef, UnaryFunctor>::value &&
1698 !std::is_constructible<StringRef, NullaryFunctor>::value>::type>
1699 inline void interleave(ForwardIterator begin, ForwardIterator end,
1700 UnaryFunctor each_fn, NullaryFunctor between_fn) {
1705 for (; begin != end; ++begin) {
1711 template <typename Container, typename UnaryFunctor, typename NullaryFunctor,
1712 typename = typename std::enable_if<
1713 !std::is_constructible<StringRef, UnaryFunctor>::value &&
1714 !std::is_constructible<StringRef, NullaryFunctor>::value>::type>
1715 inline void interleave(const Container &c, UnaryFunctor each_fn,
1716 NullaryFunctor between_fn) {
1717 interleave(c.begin(), c.end(), each_fn, between_fn);
1720 /// Overload of interleave for the common case of string separator.
1721 template <typename Container, typename UnaryFunctor, typename StreamT,
1722 typename T = detail::ValueOfRange<Container>>
1723 inline void interleave(const Container &c, StreamT &os, UnaryFunctor each_fn,
1724 const StringRef &separator) {
1725 interleave(c.begin(), c.end(), each_fn, [&] { os << separator; });
1727 template <typename Container, typename StreamT,
1728 typename T = detail::ValueOfRange<Container>>
1729 inline void interleave(const Container &c, StreamT &os,
1730 const StringRef &separator) {
1732 c, os, [&](const T &a) { os << a; }, separator);
1735 template <typename Container, typename UnaryFunctor, typename StreamT,
1736 typename T = detail::ValueOfRange<Container>>
1737 inline void interleaveComma(const Container &c, StreamT &os,
1738 UnaryFunctor each_fn) {
1739 interleave(c, os, each_fn, ", ");
1741 template <typename Container, typename StreamT,
1742 typename T = detail::ValueOfRange<Container>>
1743 inline void interleaveComma(const Container &c, StreamT &os) {
1744 interleaveComma(c, os, [&](const T &a) { os << a; });
1747 //===----------------------------------------------------------------------===//
1748 // Extra additions to <memory>
1749 //===----------------------------------------------------------------------===//
1751 struct FreeDeleter {
1752 void operator()(void* v) {
1757 template<typename First, typename Second>
1759 size_t operator()(const std::pair<First, Second> &P) const {
1760 return std::hash<First>()(P.first) * 31 + std::hash<Second>()(P.second);
1764 /// Binary functor that adapts to any other binary functor after dereferencing
1766 template <typename T> struct deref {
1769 // Could be further improved to cope with non-derivable functors and
1770 // non-binary functors (should be a variadic template member function
1772 template <typename A, typename B> auto operator()(A &lhs, B &rhs) const {
1775 return func(*lhs, *rhs);
1781 template <typename R> class enumerator_iter;
1783 template <typename R> struct result_pair {
1784 using value_reference =
1785 typename std::iterator_traits<IterOfRange<R>>::reference;
1787 friend class enumerator_iter<R>;
1789 result_pair() = default;
1790 result_pair(std::size_t Index, IterOfRange<R> Iter)
1791 : Index(Index), Iter(Iter) {}
1793 result_pair<R>(const result_pair<R> &Other)
1794 : Index(Other.Index), Iter(Other.Iter) {}
1795 result_pair<R> &operator=(const result_pair<R> &Other) {
1796 Index = Other.Index;
1801 std::size_t index() const { return Index; }
1802 const value_reference value() const { return *Iter; }
1803 value_reference value() { return *Iter; }
1806 std::size_t Index = std::numeric_limits<std::size_t>::max();
1807 IterOfRange<R> Iter;
1810 template <typename R>
1811 class enumerator_iter
1812 : public iterator_facade_base<
1813 enumerator_iter<R>, std::forward_iterator_tag, result_pair<R>,
1814 typename std::iterator_traits<IterOfRange<R>>::difference_type,
1815 typename std::iterator_traits<IterOfRange<R>>::pointer,
1816 typename std::iterator_traits<IterOfRange<R>>::reference> {
1817 using result_type = result_pair<R>;
1820 explicit enumerator_iter(IterOfRange<R> EndIter)
1821 : Result(std::numeric_limits<size_t>::max(), EndIter) {}
1823 enumerator_iter(std::size_t Index, IterOfRange<R> Iter)
1824 : Result(Index, Iter) {}
1826 result_type &operator*() { return Result; }
1827 const result_type &operator*() const { return Result; }
1829 enumerator_iter<R> &operator++() {
1830 assert(Result.Index != std::numeric_limits<size_t>::max());
1836 bool operator==(const enumerator_iter<R> &RHS) const {
1837 // Don't compare indices here, only iterators. It's possible for an end
1838 // iterator to have different indices depending on whether it was created
1839 // by calling std::end() versus incrementing a valid iterator.
1840 return Result.Iter == RHS.Result.Iter;
1843 enumerator_iter<R>(const enumerator_iter<R> &Other) : Result(Other.Result) {}
1844 enumerator_iter<R> &operator=(const enumerator_iter<R> &Other) {
1845 Result = Other.Result;
1853 template <typename R> class enumerator {
1855 explicit enumerator(R &&Range) : TheRange(std::forward<R>(Range)) {}
1857 enumerator_iter<R> begin() {
1858 return enumerator_iter<R>(0, std::begin(TheRange));
1861 enumerator_iter<R> end() {
1862 return enumerator_iter<R>(std::end(TheRange));
1869 } // end namespace detail
1871 /// Given an input range, returns a new range whose values are are pair (A,B)
1872 /// such that A is the 0-based index of the item in the sequence, and B is
1873 /// the value from the original sequence. Example:
1875 /// std::vector<char> Items = {'A', 'B', 'C', 'D'};
1876 /// for (auto X : enumerate(Items)) {
1877 /// printf("Item %d - %c\n", X.index(), X.value());
1886 template <typename R> detail::enumerator<R> enumerate(R &&TheRange) {
1887 return detail::enumerator<R>(std::forward<R>(TheRange));
1892 template <typename F, typename Tuple, std::size_t... I>
1893 decltype(auto) apply_tuple_impl(F &&f, Tuple &&t, std::index_sequence<I...>) {
1894 return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...);
1897 } // end namespace detail
1899 /// Given an input tuple (a1, a2, ..., an), pass the arguments of the
1900 /// tuple variadically to f as if by calling f(a1, a2, ..., an) and
1901 /// return the result.
1902 template <typename F, typename Tuple>
1903 decltype(auto) apply_tuple(F &&f, Tuple &&t) {
1904 using Indices = std::make_index_sequence<
1905 std::tuple_size<typename std::decay<Tuple>::type>::value>;
1907 return detail::apply_tuple_impl(std::forward<F>(f), std::forward<Tuple>(t),
1911 /// Return true if the sequence [Begin, End) has exactly N items. Runs in O(N)
1912 /// time. Not meant for use with random-access iterators.
1913 /// Can optionally take a predicate to filter lazily some items.
1914 template<typename IterTy,
1915 typename Pred = bool (*)(const decltype(*std::declval<IterTy>()) &)>
1917 IterTy &&Begin, IterTy &&End, unsigned N,
1918 Pred &&ShouldBeCounted =
1919 [](const decltype(*std::declval<IterTy>()) &) { return true; },
1921 !std::is_same<typename std::iterator_traits<std::remove_reference_t<
1922 decltype(Begin)>>::iterator_category,
1923 std::random_access_iterator_tag>::value,
1924 void> * = nullptr) {
1925 for (; N; ++Begin) {
1927 return false; // Too few.
1928 N -= ShouldBeCounted(*Begin);
1930 for (; Begin != End; ++Begin)
1931 if (ShouldBeCounted(*Begin))
1932 return false; // Too many.
1936 /// Return true if the sequence [Begin, End) has N or more items. Runs in O(N)
1937 /// time. Not meant for use with random-access iterators.
1938 /// Can optionally take a predicate to lazily filter some items.
1939 template<typename IterTy,
1940 typename Pred = bool (*)(const decltype(*std::declval<IterTy>()) &)>
1941 bool hasNItemsOrMore(
1942 IterTy &&Begin, IterTy &&End, unsigned N,
1943 Pred &&ShouldBeCounted =
1944 [](const decltype(*std::declval<IterTy>()) &) { return true; },
1946 !std::is_same<typename std::iterator_traits<std::remove_reference_t<
1947 decltype(Begin)>>::iterator_category,
1948 std::random_access_iterator_tag>::value,
1949 void> * = nullptr) {
1950 for (; N; ++Begin) {
1952 return false; // Too few.
1953 N -= ShouldBeCounted(*Begin);
1958 /// Returns true if the sequence [Begin, End) has N or less items. Can
1959 /// optionally take a predicate to lazily filter some items.
1960 template <typename IterTy,
1961 typename Pred = bool (*)(const decltype(*std::declval<IterTy>()) &)>
1962 bool hasNItemsOrLess(
1963 IterTy &&Begin, IterTy &&End, unsigned N,
1964 Pred &&ShouldBeCounted = [](const decltype(*std::declval<IterTy>()) &) {
1967 assert(N != std::numeric_limits<unsigned>::max());
1968 return !hasNItemsOrMore(Begin, End, N + 1, ShouldBeCounted);
1971 /// Returns true if the given container has exactly N items
1972 template <typename ContainerTy> bool hasNItems(ContainerTy &&C, unsigned N) {
1973 return hasNItems(std::begin(C), std::end(C), N);
1976 /// Returns true if the given container has N or more items
1977 template <typename ContainerTy>
1978 bool hasNItemsOrMore(ContainerTy &&C, unsigned N) {
1979 return hasNItemsOrMore(std::begin(C), std::end(C), N);
1982 /// Returns true if the given container has N or less items
1983 template <typename ContainerTy>
1984 bool hasNItemsOrLess(ContainerTy &&C, unsigned N) {
1985 return hasNItemsOrLess(std::begin(C), std::end(C), N);
1988 /// Returns a raw pointer that represents the same address as the argument.
1990 /// This implementation can be removed once we move to C++20 where it's defined
1991 /// as std::to_address().
1993 /// The std::pointer_traits<>::to_address(p) variations of these overloads has
1994 /// not been implemented.
1995 template <class Ptr> auto to_address(const Ptr &P) { return P.operator->(); }
1996 template <class T> constexpr T *to_address(T *P) { return P; }
1998 } // end namespace llvm
2000 #endif // LLVM_ADT_STLEXTRAS_H