LLVM API Documentation
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 <functional> 00021 #include <utility> // for std::pair 00022 #include <cstddef> // for std::size_t 00023 #include "llvm/ADT/iterator.h" 00024 00025 namespace llvm { 00026 00027 //===----------------------------------------------------------------------===// 00028 // Extra additions to <functional> 00029 //===----------------------------------------------------------------------===// 00030 00031 template<class Ty> 00032 struct greater_ptr : public std::binary_function<Ty, Ty, bool> { 00033 bool operator()(const Ty* left, const Ty* right) const { 00034 return *right < *left; 00035 } 00036 }; 00037 00038 // deleter - Very very very simple method that is used to invoke operator 00039 // delete on something. It is used like this: 00040 // 00041 // for_each(V.begin(), B.end(), deleter<Interval>); 00042 // 00043 template <class T> 00044 static inline void deleter(T *Ptr) { 00045 delete Ptr; 00046 } 00047 00048 00049 00050 //===----------------------------------------------------------------------===// 00051 // Extra additions to <iterator> 00052 //===----------------------------------------------------------------------===// 00053 00054 // mapped_iterator - This is a simple iterator adapter that causes a function to 00055 // be dereferenced whenever operator* is invoked on the iterator. 00056 // 00057 template <class RootIt, class UnaryFunc> 00058 class mapped_iterator { 00059 RootIt current; 00060 UnaryFunc Fn; 00061 public: 00062 typedef typename std::iterator_traits<RootIt>::iterator_category 00063 iterator_category; 00064 typedef typename std::iterator_traits<RootIt>::difference_type 00065 difference_type; 00066 typedef typename UnaryFunc::result_type value_type; 00067 00068 typedef void pointer; 00069 //typedef typename UnaryFunc::result_type *pointer; 00070 typedef void reference; // Can't modify value returned by fn 00071 00072 typedef RootIt iterator_type; 00073 typedef mapped_iterator<RootIt, UnaryFunc> _Self; 00074 00075 inline const RootIt &getCurrent() const { return current; } 00076 inline const UnaryFunc &getFunc() const { return Fn; } 00077 00078 inline explicit mapped_iterator(const RootIt &I, UnaryFunc F) 00079 : current(I), Fn(F) {} 00080 inline mapped_iterator(const mapped_iterator &It) 00081 : current(It.current), Fn(It.Fn) {} 00082 00083 inline value_type operator*() const { // All this work to do this 00084 return Fn(*current); // little change 00085 } 00086 00087 _Self& operator++() { ++current; return *this; } 00088 _Self& operator--() { --current; return *this; } 00089 _Self operator++(int) { _Self __tmp = *this; ++current; return __tmp; } 00090 _Self operator--(int) { _Self __tmp = *this; --current; return __tmp; } 00091 _Self operator+ (difference_type n) const { 00092 return _Self(current + n, Fn); 00093 } 00094 _Self& operator+= (difference_type n) { current += n; return *this; } 00095 _Self operator- (difference_type n) const { 00096 return _Self(current - n, Fn); 00097 } 00098 _Self& operator-= (difference_type n) { current -= n; return *this; } 00099 reference operator[](difference_type n) const { return *(*this + n); } 00100 00101 inline bool operator!=(const _Self &X) const { return !operator==(X); } 00102 inline bool operator==(const _Self &X) const { return current == X.current; } 00103 inline bool operator< (const _Self &X) const { return current < X.current; } 00104 00105 inline difference_type operator-(const _Self &X) const { 00106 return current - X.current; 00107 } 00108 }; 00109 00110 template <class _Iterator, class Func> 00111 inline mapped_iterator<_Iterator, Func> 00112 operator+(typename mapped_iterator<_Iterator, Func>::difference_type N, 00113 const mapped_iterator<_Iterator, Func>& X) { 00114 return mapped_iterator<_Iterator, Func>(X.getCurrent() - N, X.getFunc()); 00115 } 00116 00117 00118 // map_iterator - Provide a convenient way to create mapped_iterators, just like 00119 // make_pair is useful for creating pairs... 00120 // 00121 template <class ItTy, class FuncTy> 00122 inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) { 00123 return mapped_iterator<ItTy, FuncTy>(I, F); 00124 } 00125 00126 00127 // next/prior - These functions unlike std::advance do not modify the 00128 // passed iterator but return a copy. 00129 // 00130 // next(myIt) returns copy of myIt incremented once 00131 // next(myIt, n) returns copy of myIt incremented n times 00132 // prior(myIt) returns copy of myIt decremented once 00133 // prior(myIt, n) returns copy of myIt decremented n times 00134 00135 template <typename ItTy, typename Dist> 00136 inline ItTy next(ItTy it, Dist n) 00137 { 00138 std::advance(it, n); 00139 return it; 00140 } 00141 00142 template <typename ItTy> 00143 inline ItTy next(ItTy it) 00144 { 00145 return ++it; 00146 } 00147 00148 template <typename ItTy, typename Dist> 00149 inline ItTy prior(ItTy it, Dist n) 00150 { 00151 std::advance(it, -n); 00152 return it; 00153 } 00154 00155 template <typename ItTy> 00156 inline ItTy prior(ItTy it) 00157 { 00158 return --it; 00159 } 00160 00161 //===----------------------------------------------------------------------===// 00162 // Extra additions to <utility> 00163 //===----------------------------------------------------------------------===// 00164 00165 // tie - this function ties two objects and returns a temporary object 00166 // that is assignable from a std::pair. This can be used to make code 00167 // more readable when using values returned from functions bundled in 00168 // a std::pair. Since an example is worth 1000 words: 00169 // 00170 // typedef std::map<int, int> Int2IntMap; 00171 // 00172 // Int2IntMap myMap; 00173 // Int2IntMap::iterator where; 00174 // bool inserted; 00175 // tie(where, inserted) = myMap.insert(std::make_pair(123,456)); 00176 // 00177 // if (inserted) 00178 // // do stuff 00179 // else 00180 // // do other stuff 00181 00182 namespace 00183 { 00184 template <typename T1, typename T2> 00185 struct tier { 00186 typedef T1 &first_type; 00187 typedef T2 &second_type; 00188 00189 first_type first; 00190 second_type second; 00191 00192 tier(first_type f, second_type s) : first(f), second(s) { } 00193 tier& operator=(const std::pair<T1, T2>& p) { 00194 first = p.first; 00195 second = p.second; 00196 return *this; 00197 } 00198 }; 00199 } 00200 00201 template <typename T1, typename T2> 00202 inline tier<T1, T2> tie(T1& f, T2& s) { 00203 return tier<T1, T2>(f, s); 00204 } 00205 00206 //===----------------------------------------------------------------------===// 00207 // Extra additions for arrays 00208 //===----------------------------------------------------------------------===// 00209 00210 /// Find where an array ends (for ending iterators) 00211 /// This returns a pointer to the byte immediately 00212 /// after the end of an array. 00213 template<class T, std::size_t N> 00214 inline T *array_endof(T (&x)[N]) { 00215 return x+N; 00216 } 00217 00218 /// Find the length of an array. 00219 template<class T, std::size_t N> 00220 inline size_t array_lengthof(T (&x)[N]) { 00221 return N; 00222 } 00223 00224 /// array_pod_sort_comparator - This is helper function for array_pod_sort, 00225 /// which just uses operator< on T. 00226 template<typename T> 00227 static inline int array_pod_sort_comparator(const void *P1, const void *P2) { 00228 if (*reinterpret_cast<const T*>(P1) < *reinterpret_cast<const T*>(P2)) 00229 return -1; 00230 if (*reinterpret_cast<const T*>(P2) < *reinterpret_cast<const T*>(P1)) 00231 return 1; 00232 return 0; 00233 } 00234 00235 /// get_array_pad_sort_comparator - This is an internal helper function used to 00236 /// get type deduction of T right. 00237 template<typename T> 00238 static int (*get_array_pad_sort_comparator(const T &X)) 00239 (const void*, const void*) { 00240 return array_pod_sort_comparator<T>; 00241 } 00242 00243 00244 /// array_pod_sort - This sorts an array with the specified start and end 00245 /// extent. This is just like std::sort, except that it calls qsort instead of 00246 /// using an inlined template. qsort is slightly slower than std::sort, but 00247 /// most sorts are not performance critical in LLVM and std::sort has to be 00248 /// template instantiated for each type, leading to significant measured code 00249 /// bloat. This function should generally be used instead of std::sort where 00250 /// possible. 00251 /// 00252 /// This function assumes that you have simple POD-like types that can be 00253 /// compared with operator< and can be moved with memcpy. If this isn't true, 00254 /// you should use std::sort. 00255 /// 00256 /// NOTE: If qsort_r were portable, we could allow a custom comparator and 00257 /// default to std::less. 00258 template<class IteratorTy> 00259 static inline void array_pod_sort(IteratorTy Start, IteratorTy End) { 00260 // Don't dereference start iterator of empty sequence. 00261 if (Start == End) return; 00262 qsort(&*Start, End-Start, sizeof(*Start), 00263 get_array_pad_sort_comparator(*Start)); 00264 } 00265 00266 } // End llvm namespace 00267 00268 #endif
This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.