17 #ifndef LLVM_ADT_STLEXTRAS_H 18 #define LLVM_ADT_STLEXTRAS_H 30 #include "llvm/Compiler.h" 39 struct identity :
public std::unary_function<Ty, Ty> {
40 Ty &operator()(Ty &
self)
const {
43 const Ty &operator()(
const Ty &
self)
const {
49 struct less_ptr :
public std::binary_function<Ty, Ty, bool> {
50 bool operator()(
const Ty* left,
const Ty* right)
const {
51 return *left < *right;
56 struct greater_ptr :
public std::binary_function<Ty, Ty, bool> {
57 bool operator()(
const Ty* left,
const Ty* right)
const {
58 return *right < *left;
70 template<
typename Ret,
typename ...Params>
72 Ret (*callback)(intptr_t callable, Params ...params);
75 template<
typename Callable>
76 static Ret callback_fn(intptr_t callable, Params ...params) {
77 return (*reinterpret_cast<Callable*>(callable))(
78 std::forward<Params>(params)...);
82 template <
typename Callable>
84 typename std::enable_if<
85 !std::is_same<
typename std::remove_reference<Callable>::type,
87 : callback(callback_fn<
typename std::remove_reference<Callable>::type>),
88 callable(reinterpret_cast<intptr_t>(&callable)) {}
89 Ret operator()(Params ...params)
const {
90 return callback(callable, std::forward<Params>(params)...);
100 inline void deleter(T *Ptr) {
113 template <
class RootIt,
class UnaryFunc>
118 typedef typename std::iterator_traits<RootIt>::iterator_category
120 typedef typename std::iterator_traits<RootIt>::difference_type
122 typedef typename std::result_of<
123 UnaryFunc(decltype(*std::declval<RootIt>()))>
126 typedef void pointer;
128 typedef void reference;
130 typedef RootIt iterator_type;
132 inline const RootIt &getCurrent()
const {
return current; }
133 inline const UnaryFunc &getFunc()
const {
return Fn; }
136 : current(I), Fn(F) {}
138 inline value_type operator*()
const {
174 reference operator[](difference_type n)
const {
return *(*
this + n); }
176 bool operator!=(
const mapped_iterator &X)
const {
return !operator==(X); }
178 return current == X.current;
180 bool operator<(
const mapped_iterator &X)
const {
return current < X.current; }
183 return current - X.current;
187 template <
class Iterator,
class Func>
189 operator+(
typename mapped_iterator<Iterator, Func>::difference_type N,
198 template <
class ItTy,
class FuncTy>
205 template <
typename U>
static char(&f(
const U &, decltype(&U::rbegin)))[1];
206 static char(&f(...))[2];
207 const static bool value =
sizeof(f(std::declval<T>(),
nullptr)) == 1;
212 template <
typename ContainerTy>
213 auto reverse(ContainerTy &&C,
215 nullptr) -> decltype(
make_range(C.rbegin(), C.rend())) {
220 template <
typename IteratorTy>
221 std::reverse_iterator<IteratorTy> make_reverse_iterator(IteratorTy It) {
222 return std::reverse_iterator<IteratorTy>(It);
228 template <
typename ContainerTy>
232 -> decltype(
make_range(llvm::make_reverse_iterator(std::end(C)),
233 llvm::make_reverse_iterator(std::begin(C)))) {
234 return make_range(llvm::make_reverse_iterator(std::end(C)),
235 llvm::make_reverse_iterator(std::begin(C)));
245 template <
typename T>
bool operator()(
const T &lhs,
const T &rhs)
const {
246 return lhs.first < rhs.first;
253 template <
typename T>
bool operator()(
const T &lhs,
const T &rhs)
const {
254 return lhs.second < rhs.second;
262 typedef T value_type;
264 static LLVM_CONSTEXPR
size_t size() {
return sizeof...(I); }
268 template <
size_t... I>
271 template <std::size_t N, std::size_t... I>
273 template <std::size_t... I>
277 template <
class... Ts>
285 template <
class T, std::
size_t N>
286 LLVM_CONSTEXPR
inline size_t array_lengthof(T (&)[N]) {
292 inline int array_pod_sort_comparator(
const void *P1,
const void *P2) {
293 if (std::less<T>()(*reinterpret_cast<const T*>(P1),
294 *reinterpret_cast<const T*>(P2)))
296 if (std::less<T>()(*reinterpret_cast<const T*>(P2),
297 *reinterpret_cast<const T*>(P1)))
305 inline int (*get_array_pod_sort_comparator(
const T &))
306 (
const void*,
const void*) {
307 return array_pod_sort_comparator<T>;
325 template<
class IteratorTy>
326 inline void array_pod_sort(IteratorTy Start, IteratorTy End) {
329 auto NElts = End - Start;
330 if (NElts <= 1)
return;
331 qsort(&*Start, NElts,
sizeof(*Start), get_array_pod_sort_comparator(*Start));
334 template <
class IteratorTy>
335 inline void array_pod_sort(
336 IteratorTy Start, IteratorTy End,
338 const typename std::iterator_traits<IteratorTy>::value_type *,
339 const typename std::iterator_traits<IteratorTy>::value_type *)) {
342 auto NElts = End - Start;
343 if (NElts <= 1)
return;
344 qsort(&*Start, NElts,
sizeof(*Start),
345 reinterpret_cast<int (*)(
const void *,
const void *)
>(Compare));
354 template<
typename Container>
355 void DeleteContainerPointers(Container &C) {
356 for (
typename Container::iterator I = C.begin(), E = C.end(); I != E; ++I)
363 template<
typename Container>
364 void DeleteContainerSeconds(Container &C) {
365 for (
typename Container::iterator I = C.begin(), E = C.end(); I != E; ++I)
372 template<
typename R,
class UnaryPredicate>
373 bool all_of(R &&Range, UnaryPredicate &&P) {
374 return std::all_of(Range.begin(), Range.end(),
375 std::forward<UnaryPredicate>(P));
380 template <
typename R,
class UnaryPredicate>
381 bool any_of(R &&Range, UnaryPredicate &&P) {
382 return std::any_of(Range.begin(), Range.end(),
383 std::forward<UnaryPredicate>(P));
388 template <
typename R,
class UnaryPredicate>
389 bool none_of(R &&Range, UnaryPredicate &&P) {
390 return std::none_of(Range.begin(), Range.end(),
391 std::forward<UnaryPredicate>(P));
396 template<
typename R,
class T>
397 auto find(R &&Range,
const T &val) -> decltype(Range.begin()) {
398 return std::find(Range.begin(), Range.end(), val);
403 template <
typename R,
class T>
404 auto find_if(R &&Range,
const T &Pred) -> decltype(Range.begin()) {
405 return std::find_if(Range.begin(), Range.end(), Pred);
410 template<
typename R,
class UnaryPredicate>
411 auto remove_if(R &&Range, UnaryPredicate &&P) -> decltype(Range.begin()) {
412 return std::remove_if(Range.begin(), Range.end(), P);
417 template <
typename R,
typename E>
418 bool is_contained(R &&Range,
const E &Element) {
419 return std::find(Range.begin(), Range.end(), Element) != Range.end();
424 template <
typename R,
typename UnaryPredicate>
425 auto count_if(R &&Range, UnaryPredicate &&P)
426 ->
typename std::iterator_traits<decltype(Range.begin())>::difference_type {
427 return std::count_if(Range.begin(), Range.end(), P);
432 template <
typename R,
class OutputIt,
typename UnaryPredicate>
433 OutputIt transform(R &&Range, OutputIt d_first, UnaryPredicate &&P) {
434 return std::transform(Range.begin(), Range.end(), d_first,
435 std::forward<UnaryPredicate>(P));
451 template <
class T,
class... Args>
452 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
453 make_unique(Args &&... args) {
454 return std::unique_ptr<T>(
new T(std::forward<Args>(args)...));
466 typename std::enable_if<std::is_array<T>::value && std::extent<T>::value == 0,
467 std::unique_ptr<T>>::type
468 make_unique(
size_t n) {
469 return std::unique_ptr<T>(
new typename std::remove_extent<T>::type[n]());
473 template <
class T,
class... Args>
474 typename std::enable_if<std::extent<T>::value != 0>::type
475 make_unique(Args &&...) =
delete;
478 void operator()(
void* v) {
483 template<
typename First,
typename Second>
485 size_t operator()(
const std::pair<First, Second> &P)
const {
486 return std::hash<First>()(P.first) * 31 + std::hash<Second>()(P.second);
492 template <
typename A,
typename B>
bool operator()(A &&a, B &&b)
const {
493 return std::forward<A>(a) < std::forward<B>(b);
499 template <
typename A,
typename B>
bool operator()(A &&a, B &&b)
const {
500 return std::forward<A>(a) == std::forward<B>(b);
506 template <
typename T>
struct deref {
511 template <
typename A,
typename B>
512 auto operator()(A &lhs, B &rhs)
const -> decltype(func(*lhs, *rhs)) {
515 return func(*lhs, *rhs);
Definition: STLExtras.h:56
Function object to check whether the second component of a std::pair compares less than the second co...
Definition: STLExtras.h:252
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:68
A functor like C++14's std::less<void> in its absence.
Definition: STLExtras.h:491
Metafunction to determine if type T has a member called rbegin().
Definition: STLExtras.h:204
Alias for the common case of a sequence of size_ts.
Definition: STLExtras.h:269
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition: iterator_range.h:54
Definition: STLExtras.h:49
Definition: STLExtras.h:114
Definition: STLExtras.h:272
A functor like C++14's std::equal<void> in its absence.
Definition: STLExtras.h:498
Creates a compile-time integer sequence for a parameter pack.
Definition: STLExtras.h:278
Represents a compile-time sequence of integers.
Definition: STLExtras.h:261
Definition: STLExtras.h:477
Definition: STLExtras.h:39
Binary functor that adapts to any other binary functor after dereferencing operands.
Definition: STLExtras.h:506
Definition: STLExtras.h:484
Function object to check whether the first component of a std::pair compares less than the first comp...
Definition: STLExtras.h:244