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