Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

312 lines
9.8 KiB

  1. //===- llvm/ADT/STLExtras.h - Useful STL related functions ------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains some templates that are useful if you are working with the
  11. // STL at all.
  12. //
  13. // No library is required when using these functions.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_ADT_STLEXTRAS_H
  17. #define LLVM_ADT_STLEXTRAS_H
  18. #include <cstddef> // for std::size_t
  19. #include <cstdlib> // for qsort
  20. #include <functional>
  21. #include <iterator>
  22. #include <utility> // for std::pair
  23. namespace llvm {
  24. //===----------------------------------------------------------------------===//
  25. // Extra additions to <functional>
  26. //===----------------------------------------------------------------------===//
  27. template<class Ty>
  28. struct identity : public std::unary_function<Ty, Ty> {
  29. Ty &operator()(Ty &self) const {
  30. return self;
  31. }
  32. const Ty &operator()(const Ty &self) const {
  33. return self;
  34. }
  35. };
  36. template<class Ty>
  37. struct less_ptr : public std::binary_function<Ty, Ty, bool> {
  38. bool operator()(const Ty* left, const Ty* right) const {
  39. return *left < *right;
  40. }
  41. };
  42. template<class Ty>
  43. struct greater_ptr : public std::binary_function<Ty, Ty, bool> {
  44. bool operator()(const Ty* left, const Ty* right) const {
  45. return *right < *left;
  46. }
  47. };
  48. // deleter - Very very very simple method that is used to invoke operator
  49. // delete on something. It is used like this:
  50. //
  51. // for_each(V.begin(), B.end(), deleter<Interval>);
  52. //
  53. template <class T>
  54. inline void deleter(T *Ptr) {
  55. delete Ptr;
  56. }
  57. //===----------------------------------------------------------------------===//
  58. // Extra additions to <iterator>
  59. //===----------------------------------------------------------------------===//
  60. // mapped_iterator - This is a simple iterator adapter that causes a function to
  61. // be dereferenced whenever operator* is invoked on the iterator.
  62. //
  63. template <class RootIt, class UnaryFunc>
  64. class mapped_iterator {
  65. RootIt current;
  66. UnaryFunc Fn;
  67. public:
  68. typedef typename std::iterator_traits<RootIt>::iterator_category
  69. iterator_category;
  70. typedef typename std::iterator_traits<RootIt>::difference_type
  71. difference_type;
  72. typedef typename UnaryFunc::result_type value_type;
  73. typedef void pointer;
  74. //typedef typename UnaryFunc::result_type *pointer;
  75. typedef void reference; // Can't modify value returned by fn
  76. typedef RootIt iterator_type;
  77. typedef mapped_iterator<RootIt, UnaryFunc> _Self;
  78. inline const RootIt &getCurrent() const { return current; }
  79. inline const UnaryFunc &getFunc() const { return Fn; }
  80. inline explicit mapped_iterator(const RootIt &I, UnaryFunc F)
  81. : current(I), Fn(F) {}
  82. inline mapped_iterator(const mapped_iterator &It)
  83. : current(It.current), Fn(It.Fn) {}
  84. inline value_type operator*() const { // All this work to do this
  85. return Fn(*current); // little change
  86. }
  87. _Self& operator++() { ++current; return *this; }
  88. _Self& operator--() { --current; return *this; }
  89. _Self operator++(int) { _Self __tmp = *this; ++current; return __tmp; }
  90. _Self operator--(int) { _Self __tmp = *this; --current; return __tmp; }
  91. _Self operator+ (difference_type n) const {
  92. return _Self(current + n, Fn);
  93. }
  94. _Self& operator+= (difference_type n) { current += n; return *this; }
  95. _Self operator- (difference_type n) const {
  96. return _Self(current - n, Fn);
  97. }
  98. _Self& operator-= (difference_type n) { current -= n; return *this; }
  99. reference operator[](difference_type n) const { return *(*this + n); }
  100. inline bool operator!=(const _Self &X) const { return !operator==(X); }
  101. inline bool operator==(const _Self &X) const { return current == X.current; }
  102. inline bool operator< (const _Self &X) const { return current < X.current; }
  103. inline difference_type operator-(const _Self &X) const {
  104. return current - X.current;
  105. }
  106. };
  107. template <class _Iterator, class Func>
  108. inline mapped_iterator<_Iterator, Func>
  109. operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
  110. const mapped_iterator<_Iterator, Func>& X) {
  111. return mapped_iterator<_Iterator, Func>(X.getCurrent() - N, X.getFunc());
  112. }
  113. // map_iterator - Provide a convenient way to create mapped_iterators, just like
  114. // make_pair is useful for creating pairs...
  115. //
  116. template <class ItTy, class FuncTy>
  117. inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
  118. return mapped_iterator<ItTy, FuncTy>(I, F);
  119. }
  120. // next/prior - These functions unlike std::advance do not modify the
  121. // passed iterator but return a copy.
  122. //
  123. // next(myIt) returns copy of myIt incremented once
  124. // next(myIt, n) returns copy of myIt incremented n times
  125. // prior(myIt) returns copy of myIt decremented once
  126. // prior(myIt, n) returns copy of myIt decremented n times
  127. template <typename ItTy, typename Dist>
  128. inline ItTy next(ItTy it, Dist n)
  129. {
  130. std::advance(it, n);
  131. return it;
  132. }
  133. template <typename ItTy>
  134. inline ItTy next(ItTy it)
  135. {
  136. return ++it;
  137. }
  138. template <typename ItTy, typename Dist>
  139. inline ItTy prior(ItTy it, Dist n)
  140. {
  141. std::advance(it, -n);
  142. return it;
  143. }
  144. template <typename ItTy>
  145. inline ItTy prior(ItTy it)
  146. {
  147. return --it;
  148. }
  149. //===----------------------------------------------------------------------===//
  150. // Extra additions to <utility>
  151. //===----------------------------------------------------------------------===//
  152. // tie - this function ties two objects and returns a temporary object
  153. // that is assignable from a std::pair. This can be used to make code
  154. // more readable when using values returned from functions bundled in
  155. // a std::pair. Since an example is worth 1000 words:
  156. //
  157. // typedef std::map<int, int> Int2IntMap;
  158. //
  159. // Int2IntMap myMap;
  160. // Int2IntMap::iterator where;
  161. // bool inserted;
  162. // tie(where, inserted) = myMap.insert(std::make_pair(123,456));
  163. //
  164. // if (inserted)
  165. // // do stuff
  166. // else
  167. // // do other stuff
  168. template <typename T1, typename T2>
  169. struct tier {
  170. typedef T1 &first_type;
  171. typedef T2 &second_type;
  172. first_type first;
  173. second_type second;
  174. tier(first_type f, second_type s) : first(f), second(s) { }
  175. tier& operator=(const std::pair<T1, T2>& p) {
  176. first = p.first;
  177. second = p.second;
  178. return *this;
  179. }
  180. };
  181. template <typename T1, typename T2>
  182. inline tier<T1, T2> tie(T1& f, T2& s) {
  183. return tier<T1, T2>(f, s);
  184. }
  185. //===----------------------------------------------------------------------===//
  186. // Extra additions for arrays
  187. //===----------------------------------------------------------------------===//
  188. /// Find where an array ends (for ending iterators)
  189. /// This returns a pointer to the byte immediately
  190. /// after the end of an array.
  191. template<class T, std::size_t N>
  192. inline T *array_endof(T (&x)[N]) {
  193. return x+N;
  194. }
  195. /// Find the length of an array.
  196. template<class T, std::size_t N>
  197. inline size_t array_lengthof(T (&)[N]) {
  198. return N;
  199. }
  200. /// array_pod_sort_comparator - This is helper function for array_pod_sort,
  201. /// which just uses operator< on T.
  202. template<typename T>
  203. inline int array_pod_sort_comparator(const void *P1, const void *P2) {
  204. if (*reinterpret_cast<const T*>(P1) < *reinterpret_cast<const T*>(P2))
  205. return -1;
  206. if (*reinterpret_cast<const T*>(P2) < *reinterpret_cast<const T*>(P1))
  207. return 1;
  208. return 0;
  209. }
  210. /// get_array_pod_sort_comparator - This is an internal helper function used to
  211. /// get type deduction of T right.
  212. template<typename T>
  213. inline int (*get_array_pod_sort_comparator(const T &))
  214. (const void*, const void*) {
  215. return array_pod_sort_comparator<T>;
  216. }
  217. /// array_pod_sort - This sorts an array with the specified start and end
  218. /// extent. This is just like std::sort, except that it calls qsort instead of
  219. /// using an inlined template. qsort is slightly slower than std::sort, but
  220. /// most sorts are not performance critical in LLVM and std::sort has to be
  221. /// template instantiated for each type, leading to significant measured code
  222. /// bloat. This function should generally be used instead of std::sort where
  223. /// possible.
  224. ///
  225. /// This function assumes that you have simple POD-like types that can be
  226. /// compared with operator< and can be moved with memcpy. If this isn't true,
  227. /// you should use std::sort.
  228. ///
  229. /// NOTE: If qsort_r were portable, we could allow a custom comparator and
  230. /// default to std::less.
  231. template<class IteratorTy>
  232. inline void array_pod_sort(IteratorTy Start, IteratorTy End) {
  233. // Don't dereference start iterator of empty sequence.
  234. if (Start == End) return;
  235. qsort(&*Start, End-Start, sizeof(*Start),
  236. get_array_pod_sort_comparator(*Start));
  237. }
  238. template<class IteratorTy>
  239. inline void array_pod_sort(IteratorTy Start, IteratorTy End,
  240. int (*Compare)(const void*, const void*)) {
  241. // Don't dereference start iterator of empty sequence.
  242. if (Start == End) return;
  243. qsort(&*Start, End-Start, sizeof(*Start), Compare);
  244. }
  245. //===----------------------------------------------------------------------===//
  246. // Extra additions to <algorithm>
  247. //===----------------------------------------------------------------------===//
  248. /// For a container of pointers, deletes the pointers and then clears the
  249. /// container.
  250. template<typename Container>
  251. void DeleteContainerPointers(Container &C) {
  252. for (typename Container::iterator I = C.begin(), E = C.end(); I != E; ++I)
  253. delete *I;
  254. C.clear();
  255. }
  256. /// In a container of pairs (usually a map) whose second element is a pointer,
  257. /// deletes the second elements and then clears the container.
  258. template<typename Container>
  259. void DeleteContainerSeconds(Container &C) {
  260. for (typename Container::iterator I = C.begin(), E = C.end(); I != E; ++I)
  261. delete I->second;
  262. C.clear();
  263. }
  264. } // End llvm namespace
  265. #endif