LLVM API Documentation

STLExtras.h
Go to the documentation of this file.
00001 //===- llvm/ADT/STLExtras.h - Useful STL related functions ------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains some templates that are useful if you are working with the
00011 // STL at all.
00012 //
00013 // No library is required when using these functions.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_ADT_STLEXTRAS_H
00018 #define LLVM_ADT_STLEXTRAS_H
00019 
00020 #include "llvm/Support/Compiler.h"
00021 #include <cstddef> // for std::size_t
00022 #include <cstdlib> // for qsort
00023 #include <functional>
00024 #include <iterator>
00025 #include <memory>
00026 #include <utility> // for std::pair
00027 
00028 namespace llvm {
00029 
00030 //===----------------------------------------------------------------------===//
00031 //     Extra additions to <functional>
00032 //===----------------------------------------------------------------------===//
00033 
00034 template<class Ty>
00035 struct identity : public std::unary_function<Ty, Ty> {
00036   Ty &operator()(Ty &self) const {
00037     return self;
00038   }
00039   const Ty &operator()(const Ty &self) const {
00040     return self;
00041   }
00042 };
00043 
00044 template<class Ty>
00045 struct less_ptr : public std::binary_function<Ty, Ty, bool> {
00046   bool operator()(const Ty* left, const Ty* right) const {
00047     return *left < *right;
00048   }
00049 };
00050 
00051 template<class Ty>
00052 struct greater_ptr : public std::binary_function<Ty, Ty, bool> {
00053   bool operator()(const Ty* left, const Ty* right) const {
00054     return *right < *left;
00055   }
00056 };
00057 
00058 /// An efficient, type-erasing, non-owning reference to a callable. This is
00059 /// intended for use as the type of a function parameter that is not used
00060 /// after the function in question returns.
00061 ///
00062 /// This class does not own the callable, so it is not in general safe to store
00063 /// a function_ref.
00064 template<typename Fn> class function_ref;
00065 
00066 #if LLVM_HAS_VARIADIC_TEMPLATES
00067 
00068 template<typename Ret, typename ...Params>
00069 class function_ref<Ret(Params...)> {
00070   Ret (*callback)(intptr_t callable, Params ...params);
00071   intptr_t callable;
00072 
00073   template<typename Callable>
00074   static Ret callback_fn(intptr_t callable, Params ...params) {
00075     return (*reinterpret_cast<Callable*>(callable))(
00076         std::forward<Params>(params)...);
00077   }
00078 
00079 public:
00080   template<typename Callable>
00081   function_ref(Callable &&callable)
00082       : callback(callback_fn<typename std::remove_reference<Callable>::type>),
00083         callable(reinterpret_cast<intptr_t>(&callable)) {}
00084   Ret operator()(Params ...params) const {
00085     return callback(callable, std::forward<Params>(params)...);
00086   }
00087 };
00088 
00089 #else
00090 
00091 template<typename Ret>
00092 class function_ref<Ret()> {
00093   Ret (*callback)(intptr_t callable);
00094   intptr_t callable;
00095 
00096   template<typename Callable>
00097   static Ret callback_fn(intptr_t callable) {
00098     return (*reinterpret_cast<Callable*>(callable))();
00099   }
00100 
00101 public:
00102   template<typename Callable>
00103   function_ref(Callable &&callable)
00104       : callback(callback_fn<typename std::remove_reference<Callable>::type>),
00105         callable(reinterpret_cast<intptr_t>(&callable)) {}
00106   Ret operator()() const { return callback(callable); }
00107 };
00108 
00109 template<typename Ret, typename Param1>
00110 class function_ref<Ret(Param1)> {
00111   Ret (*callback)(intptr_t callable, Param1 param1);
00112   intptr_t callable;
00113 
00114   template<typename Callable>
00115   static Ret callback_fn(intptr_t callable, Param1 param1) {
00116     return (*reinterpret_cast<Callable*>(callable))(
00117         std::forward<Param1>(param1));
00118   }
00119 
00120 public:
00121   template<typename Callable>
00122   function_ref(Callable &&callable)
00123       : callback(callback_fn<typename std::remove_reference<Callable>::type>),
00124         callable(reinterpret_cast<intptr_t>(&callable)) {}
00125   Ret operator()(Param1 param1) {
00126     return callback(callable, std::forward<Param1>(param1));
00127   }
00128 };
00129 
00130 template<typename Ret, typename Param1, typename Param2>
00131 class function_ref<Ret(Param1, Param2)> {
00132   Ret (*callback)(intptr_t callable, Param1 param1, Param2 param2);
00133   intptr_t callable;
00134 
00135   template<typename Callable>
00136   static Ret callback_fn(intptr_t callable, Param1 param1, Param2 param2) {
00137     return (*reinterpret_cast<Callable*>(callable))(
00138         std::forward<Param1>(param1),
00139         std::forward<Param2>(param2));
00140   }
00141 
00142 public:
00143   template<typename Callable>
00144   function_ref(Callable &&callable)
00145       : callback(callback_fn<typename std::remove_reference<Callable>::type>),
00146         callable(reinterpret_cast<intptr_t>(&callable)) {}
00147   Ret operator()(Param1 param1, Param2 param2) {
00148     return callback(callable,
00149                     std::forward<Param1>(param1),
00150                     std::forward<Param2>(param2));
00151   }
00152 };
00153 
00154 template<typename Ret, typename Param1, typename Param2, typename Param3>
00155 class function_ref<Ret(Param1, Param2, Param3)> {
00156   Ret (*callback)(intptr_t callable, Param1 param1, Param2 param2, Param3 param3);
00157   intptr_t callable;
00158 
00159   template<typename Callable>
00160   static Ret callback_fn(intptr_t callable, Param1 param1, Param2 param2,
00161                          Param3 param3) {
00162     return (*reinterpret_cast<Callable*>(callable))(
00163         std::forward<Param1>(param1),
00164         std::forward<Param2>(param2),
00165         std::forward<Param3>(param3));
00166   }
00167 
00168 public:
00169   template<typename Callable>
00170   function_ref(Callable &&callable)
00171       : callback(callback_fn<typename std::remove_reference<Callable>::type>),
00172         callable(reinterpret_cast<intptr_t>(&callable)) {}
00173   Ret operator()(Param1 param1, Param2 param2, Param3 param3) {
00174     return callback(callable,
00175                     std::forward<Param1>(param1),
00176                     std::forward<Param2>(param2),
00177                     std::forward<Param3>(param3));
00178   }
00179 };
00180 
00181 #endif
00182 
00183 // deleter - Very very very simple method that is used to invoke operator
00184 // delete on something.  It is used like this:
00185 //
00186 //   for_each(V.begin(), B.end(), deleter<Interval>);
00187 //
00188 template <class T>
00189 inline void deleter(T *Ptr) {
00190   delete Ptr;
00191 }
00192 
00193 
00194 
00195 //===----------------------------------------------------------------------===//
00196 //     Extra additions to <iterator>
00197 //===----------------------------------------------------------------------===//
00198 
00199 // mapped_iterator - This is a simple iterator adapter that causes a function to
00200 // be dereferenced whenever operator* is invoked on the iterator.
00201 //
00202 template <class RootIt, class UnaryFunc>
00203 class mapped_iterator {
00204   RootIt current;
00205   UnaryFunc Fn;
00206 public:
00207   typedef typename std::iterator_traits<RootIt>::iterator_category
00208           iterator_category;
00209   typedef typename std::iterator_traits<RootIt>::difference_type
00210           difference_type;
00211   typedef typename UnaryFunc::result_type value_type;
00212 
00213   typedef void pointer;
00214   //typedef typename UnaryFunc::result_type *pointer;
00215   typedef void reference;        // Can't modify value returned by fn
00216 
00217   typedef RootIt iterator_type;
00218   typedef mapped_iterator<RootIt, UnaryFunc> _Self;
00219 
00220   inline const RootIt &getCurrent() const { return current; }
00221   inline const UnaryFunc &getFunc() const { return Fn; }
00222 
00223   inline explicit mapped_iterator(const RootIt &I, UnaryFunc F)
00224     : current(I), Fn(F) {}
00225 
00226   inline value_type operator*() const {   // All this work to do this
00227     return Fn(*current);         // little change
00228   }
00229 
00230   _Self& operator++() { ++current; return *this; }
00231   _Self& operator--() { --current; return *this; }
00232   _Self  operator++(int) { _Self __tmp = *this; ++current; return __tmp; }
00233   _Self  operator--(int) { _Self __tmp = *this; --current; return __tmp; }
00234   _Self  operator+    (difference_type n) const {
00235     return _Self(current + n, Fn);
00236   }
00237   _Self& operator+=   (difference_type n) { current += n; return *this; }
00238   _Self  operator-    (difference_type n) const {
00239     return _Self(current - n, Fn);
00240   }
00241   _Self& operator-=   (difference_type n) { current -= n; return *this; }
00242   reference operator[](difference_type n) const { return *(*this + n); }
00243 
00244   inline bool operator!=(const _Self &X) const { return !operator==(X); }
00245   inline bool operator==(const _Self &X) const { return current == X.current; }
00246   inline bool operator< (const _Self &X) const { return current <  X.current; }
00247 
00248   inline difference_type operator-(const _Self &X) const {
00249     return current - X.current;
00250   }
00251 };
00252 
00253 template <class _Iterator, class Func>
00254 inline mapped_iterator<_Iterator, Func>
00255 operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
00256           const mapped_iterator<_Iterator, Func>& X) {
00257   return mapped_iterator<_Iterator, Func>(X.getCurrent() - N, X.getFunc());
00258 }
00259 
00260 
00261 // map_iterator - Provide a convenient way to create mapped_iterators, just like
00262 // make_pair is useful for creating pairs...
00263 //
00264 template <class ItTy, class FuncTy>
00265 inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
00266   return mapped_iterator<ItTy, FuncTy>(I, F);
00267 }
00268 
00269 //===----------------------------------------------------------------------===//
00270 //     Extra additions to <utility>
00271 //===----------------------------------------------------------------------===//
00272 
00273 /// \brief Function object to check whether the first component of a std::pair
00274 /// compares less than the first component of another std::pair.
00275 struct less_first {
00276   template <typename T> bool operator()(const T &lhs, const T &rhs) const {
00277     return lhs.first < rhs.first;
00278   }
00279 };
00280 
00281 /// \brief Function object to check whether the second component of a std::pair
00282 /// compares less than the second component of another std::pair.
00283 struct less_second {
00284   template <typename T> bool operator()(const T &lhs, const T &rhs) const {
00285     return lhs.second < rhs.second;
00286   }
00287 };
00288 
00289 //===----------------------------------------------------------------------===//
00290 //     Extra additions for arrays
00291 //===----------------------------------------------------------------------===//
00292 
00293 /// Find the length of an array.
00294 template <class T, std::size_t N>
00295 LLVM_CONSTEXPR inline size_t array_lengthof(T (&)[N]) {
00296   return N;
00297 }
00298 
00299 /// Adapt std::less<T> for array_pod_sort.
00300 template<typename T>
00301 inline int array_pod_sort_comparator(const void *P1, const void *P2) {
00302   if (std::less<T>()(*reinterpret_cast<const T*>(P1),
00303                      *reinterpret_cast<const T*>(P2)))
00304     return -1;
00305   if (std::less<T>()(*reinterpret_cast<const T*>(P2),
00306                      *reinterpret_cast<const T*>(P1)))
00307     return 1;
00308   return 0;
00309 }
00310 
00311 /// get_array_pod_sort_comparator - This is an internal helper function used to
00312 /// get type deduction of T right.
00313 template<typename T>
00314 inline int (*get_array_pod_sort_comparator(const T &))
00315              (const void*, const void*) {
00316   return array_pod_sort_comparator<T>;
00317 }
00318 
00319 
00320 /// array_pod_sort - This sorts an array with the specified start and end
00321 /// extent.  This is just like std::sort, except that it calls qsort instead of
00322 /// using an inlined template.  qsort is slightly slower than std::sort, but
00323 /// most sorts are not performance critical in LLVM and std::sort has to be
00324 /// template instantiated for each type, leading to significant measured code
00325 /// bloat.  This function should generally be used instead of std::sort where
00326 /// possible.
00327 ///
00328 /// This function assumes that you have simple POD-like types that can be
00329 /// compared with std::less and can be moved with memcpy.  If this isn't true,
00330 /// you should use std::sort.
00331 ///
00332 /// NOTE: If qsort_r were portable, we could allow a custom comparator and
00333 /// default to std::less.
00334 template<class IteratorTy>
00335 inline void array_pod_sort(IteratorTy Start, IteratorTy End) {
00336   // Don't dereference start iterator of empty sequence.
00337   if (Start == End) return;
00338   qsort(&*Start, End-Start, sizeof(*Start),
00339         get_array_pod_sort_comparator(*Start));
00340 }
00341 
00342 template <class IteratorTy>
00343 inline void array_pod_sort(
00344     IteratorTy Start, IteratorTy End,
00345     int (*Compare)(
00346         const typename std::iterator_traits<IteratorTy>::value_type *,
00347         const typename std::iterator_traits<IteratorTy>::value_type *)) {
00348   // Don't dereference start iterator of empty sequence.
00349   if (Start == End) return;
00350   qsort(&*Start, End - Start, sizeof(*Start),
00351         reinterpret_cast<int (*)(const void *, const void *)>(Compare));
00352 }
00353 
00354 //===----------------------------------------------------------------------===//
00355 //     Extra additions to <algorithm>
00356 //===----------------------------------------------------------------------===//
00357 
00358 /// For a container of pointers, deletes the pointers and then clears the
00359 /// container.
00360 template<typename Container>
00361 void DeleteContainerPointers(Container &C) {
00362   for (typename Container::iterator I = C.begin(), E = C.end(); I != E; ++I)
00363     delete *I;
00364   C.clear();
00365 }
00366 
00367 /// In a container of pairs (usually a map) whose second element is a pointer,
00368 /// deletes the second elements and then clears the container.
00369 template<typename Container>
00370 void DeleteContainerSeconds(Container &C) {
00371   for (typename Container::iterator I = C.begin(), E = C.end(); I != E; ++I)
00372     delete I->second;
00373   C.clear();
00374 }
00375 
00376 //===----------------------------------------------------------------------===//
00377 //     Extra additions to <memory>
00378 //===----------------------------------------------------------------------===//
00379 
00380 #if LLVM_HAS_VARIADIC_TEMPLATES
00381 
00382 // Implement make_unique according to N3656.
00383 
00384 /// \brief Constructs a `new T()` with the given args and returns a
00385 ///        `unique_ptr<T>` which owns the object.
00386 ///
00387 /// Example:
00388 ///
00389 ///     auto p = make_unique<int>();
00390 ///     auto p = make_unique<std::tuple<int, int>>(0, 1);
00391 template <class T, class... Args>
00392 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
00393 make_unique(Args &&... args) {
00394   return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
00395 }
00396 
00397 /// \brief Constructs a `new T[n]` with the given args and returns a
00398 ///        `unique_ptr<T[]>` which owns the object.
00399 ///
00400 /// \param n size of the new array.
00401 ///
00402 /// Example:
00403 ///
00404 ///     auto p = make_unique<int[]>(2); // value-initializes the array with 0's.
00405 template <class T>
00406 typename std::enable_if<std::is_array<T>::value && std::extent<T>::value == 0,
00407                         std::unique_ptr<T>>::type
00408 make_unique(size_t n) {
00409   return std::unique_ptr<T>(new typename std::remove_extent<T>::type[n]());
00410 }
00411 
00412 /// This function isn't used and is only here to provide better compile errors.
00413 template <class T, class... Args>
00414 typename std::enable_if<std::extent<T>::value != 0>::type
00415 make_unique(Args &&...) LLVM_DELETED_FUNCTION;
00416 
00417 #else
00418 
00419 template <class T>
00420 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
00421 make_unique() {
00422   return std::unique_ptr<T>(new T());
00423 }
00424 
00425 template <class T, class Arg1>
00426 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
00427 make_unique(Arg1 &&arg1) {
00428   return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1)));
00429 }
00430 
00431 template <class T, class Arg1, class Arg2>
00432 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
00433 make_unique(Arg1 &&arg1, Arg2 &&arg2) {
00434   return std::unique_ptr<T>(
00435       new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2)));
00436 }
00437 
00438 template <class T, class Arg1, class Arg2, class Arg3>
00439 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
00440 make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3) {
00441   return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1),
00442                                   std::forward<Arg2>(arg2),
00443                                   std::forward<Arg3>(arg3)));
00444 }
00445 
00446 template <class T, class Arg1, class Arg2, class Arg3, class Arg4>
00447 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
00448 make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3, Arg4 &&arg4) {
00449   return std::unique_ptr<T>(
00450       new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
00451             std::forward<Arg3>(arg3), std::forward<Arg4>(arg4)));
00452 }
00453 
00454 template <class T, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
00455 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
00456 make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3, Arg4 &&arg4, Arg5 &&arg5) {
00457   return std::unique_ptr<T>(
00458       new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
00459             std::forward<Arg3>(arg3), std::forward<Arg4>(arg4),
00460             std::forward<Arg5>(arg5)));
00461 }
00462 
00463 template <class T, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5,
00464           class Arg6>
00465 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
00466 make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3, Arg4 &&arg4, Arg5 &&arg5,
00467             Arg6 &&arg6) {
00468   return std::unique_ptr<T>(
00469       new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
00470             std::forward<Arg3>(arg3), std::forward<Arg4>(arg4),
00471             std::forward<Arg5>(arg5), std::forward<Arg6>(arg6)));
00472 }
00473 
00474 template <class T, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5,
00475           class Arg6, class Arg7>
00476 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
00477 make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3, Arg4 &&arg4, Arg5 &&arg5,
00478             Arg6 &&arg6, Arg7 &&arg7) {
00479   return std::unique_ptr<T>(
00480       new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
00481             std::forward<Arg3>(arg3), std::forward<Arg4>(arg4),
00482             std::forward<Arg5>(arg5), std::forward<Arg6>(arg6),
00483             std::forward<Arg7>(arg7)));
00484 }
00485 
00486 template <class T, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5,
00487           class Arg6, class Arg7, class Arg8>
00488 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
00489 make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3, Arg4 &&arg4, Arg5 &&arg5,
00490             Arg6 &&arg6, Arg7 &&arg7, Arg8 &&arg8) {
00491   return std::unique_ptr<T>(
00492       new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
00493             std::forward<Arg3>(arg3), std::forward<Arg4>(arg4),
00494             std::forward<Arg5>(arg5), std::forward<Arg6>(arg6),
00495             std::forward<Arg7>(arg7), std::forward<Arg8>(arg8)));
00496 }
00497 
00498 template <class T, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5,
00499           class Arg6, class Arg7, class Arg8, class Arg9>
00500 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
00501 make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3, Arg4 &&arg4, Arg5 &&arg5,
00502             Arg6 &&arg6, Arg7 &&arg7, Arg8 &&arg8, Arg9 &&arg9) {
00503   return std::unique_ptr<T>(
00504       new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
00505             std::forward<Arg3>(arg3), std::forward<Arg4>(arg4),
00506             std::forward<Arg5>(arg5), std::forward<Arg6>(arg6),
00507             std::forward<Arg7>(arg7), std::forward<Arg8>(arg8),
00508             std::forward<Arg9>(arg9)));
00509 }
00510 
00511 template <class T, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5,
00512           class Arg6, class Arg7, class Arg8, class Arg9, class Arg10>
00513 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
00514 make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3, Arg4 &&arg4, Arg5 &&arg5,
00515             Arg6 &&arg6, Arg7 &&arg7, Arg8 &&arg8, Arg9 &&arg9, Arg10 &&arg10) {
00516   return std::unique_ptr<T>(
00517       new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
00518             std::forward<Arg3>(arg3), std::forward<Arg4>(arg4),
00519             std::forward<Arg5>(arg5), std::forward<Arg6>(arg6),
00520             std::forward<Arg7>(arg7), std::forward<Arg8>(arg8),
00521             std::forward<Arg9>(arg9), std::forward<Arg10>(arg10)));
00522 }
00523 
00524 template <class T>
00525 typename std::enable_if<std::is_array<T>::value &&std::extent<T>::value == 0,
00526                         std::unique_ptr<T>>::type
00527 make_unique(size_t n) {
00528   return std::unique_ptr<T>(new typename std::remove_extent<T>::type[n]());
00529 }
00530 
00531 #endif
00532 
00533 struct FreeDeleter {
00534   void operator()(void* v) {
00535     ::free(v);
00536   }
00537 };
00538 
00539 template<typename First, typename Second>
00540 struct pair_hash {
00541   size_t operator()(const std::pair<First, Second> &P) const {
00542     return std::hash<First>()(P.first) * 31 + std::hash<Second>()(P.second);
00543   }
00544 };
00545 
00546 } // End llvm namespace
00547 
00548 #endif