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 was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source 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 functinons.
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 "llvm/ADT/iterator"
00023 
00024 namespace llvm {
00025 
00026 //===----------------------------------------------------------------------===//
00027 //     Extra additions to <functional>
00028 //===----------------------------------------------------------------------===//
00029 
00030 template<class Ty>
00031 struct greater_ptr : public std::binary_function<Ty, Ty, bool> {
00032   bool operator()(const Ty* left, const Ty* right) const {
00033     return *right < *left;
00034   }
00035 };
00036 
00037 // deleter - Very very very simple method that is used to invoke operator
00038 // delete on something.  It is used like this:
00039 //
00040 //   for_each(V.begin(), B.end(), deleter<Interval>);
00041 //
00042 template <class T>
00043 static inline void deleter(T *Ptr) {
00044   delete Ptr;
00045 }
00046 
00047 
00048 
00049 //===----------------------------------------------------------------------===//
00050 //     Extra additions to <iterator>
00051 //===----------------------------------------------------------------------===//
00052 
00053 // mapped_iterator - This is a simple iterator adapter that causes a function to
00054 // be dereferenced whenever operator* is invoked on the iterator.
00055 //
00056 template <class RootIt, class UnaryFunc>
00057 class mapped_iterator {
00058   RootIt current;
00059   UnaryFunc Fn;
00060 public:
00061   typedef typename std::iterator_traits<RootIt>::iterator_category
00062           iterator_category;
00063   typedef typename std::iterator_traits<RootIt>::difference_type
00064           difference_type;
00065   typedef typename UnaryFunc::result_type value_type;
00066 
00067   typedef void pointer;
00068   //typedef typename UnaryFunc::result_type *pointer;
00069   typedef void reference;        // Can't modify value returned by fn
00070 
00071   typedef RootIt iterator_type;
00072   typedef mapped_iterator<RootIt, UnaryFunc> _Self;
00073 
00074   inline RootIt &getCurrent() const { return current; }
00075 
00076   inline explicit mapped_iterator(const RootIt &I, UnaryFunc F)
00077     : current(I), Fn(F) {}
00078   inline mapped_iterator(const mapped_iterator &It)
00079     : current(It.current), Fn(It.Fn) {}
00080 
00081   inline value_type operator*() const {   // All this work to do this
00082     return Fn(*current);         // little change
00083   }
00084 
00085   _Self& operator++() { ++current; return *this; }
00086   _Self& operator--() { --current; return *this; }
00087   _Self  operator++(int) { _Self __tmp = *this; ++current; return __tmp; }
00088   _Self  operator--(int) { _Self __tmp = *this; --current; return __tmp; }
00089   _Self  operator+    (difference_type n) const { return _Self(current + n); }
00090   _Self& operator+=   (difference_type n) { current += n; return *this; }
00091   _Self  operator-    (difference_type n) const { return _Self(current - n); }
00092   _Self& operator-=   (difference_type n) { current -= n; return *this; }
00093   reference operator[](difference_type n) const { return *(*this + n); }
00094 
00095   inline bool operator!=(const _Self &X) const { return !operator==(X); }
00096   inline bool operator==(const _Self &X) const { return current == X.current; }
00097   inline bool operator< (const _Self &X) const { return current <  X.current; }
00098 
00099   inline difference_type operator-(const _Self &X) const {
00100     return current - X.current;
00101   }
00102 };
00103 
00104 template <class _Iterator, class Func>
00105 inline mapped_iterator<_Iterator, Func>
00106 operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
00107           const mapped_iterator<_Iterator, Func>& X) {
00108   return mapped_iterator<_Iterator, Func>(X.getCurrent() - N);
00109 }
00110 
00111 
00112 // map_iterator - Provide a convenient way to create mapped_iterators, just like
00113 // make_pair is useful for creating pairs...
00114 //
00115 template <class ItTy, class FuncTy>
00116 inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
00117   return mapped_iterator<ItTy, FuncTy>(I, F);
00118 }
00119 
00120 
00121 // next/prior - These functions unlike std::advance do not modify the
00122 // passed iterator but return a copy.
00123 //
00124 // next(myIt) returns copy of myIt incremented once
00125 // next(myIt, n) returns copy of myIt incremented n times
00126 // prior(myIt) returns copy of myIt decremented once
00127 // prior(myIt, n) returns copy of myIt decremented n times
00128 
00129 template <typename ItTy, typename Dist>
00130 inline ItTy next(ItTy it, Dist n)
00131 {
00132   std::advance(it, n);
00133   return it;
00134 }
00135 
00136 template <typename ItTy>
00137 inline ItTy next(ItTy it)
00138 {
00139   std::advance(it, 1);
00140   return it;
00141 }
00142 
00143 template <typename ItTy, typename Dist>
00144 inline ItTy prior(ItTy it, Dist n)
00145 {
00146   std::advance(it, -n);
00147   return it;
00148 }
00149 
00150 template <typename ItTy>
00151 inline ItTy prior(ItTy it)
00152 {
00153   std::advance(it, -1);
00154   return it;
00155 }
00156 
00157 //===----------------------------------------------------------------------===//
00158 //     Extra additions to <utility>
00159 //===----------------------------------------------------------------------===//
00160 
00161 // tie - this function ties two objects and returns a temporary object
00162 // that is assignable from a std::pair. This can be used to make code
00163 // more readable when using values returned from functions bundled in
00164 // a std::pair. Since an example is worth 1000 words:
00165 //
00166 // typedef std::map<int, int> Int2IntMap;
00167 //
00168 // Int2IntMap myMap;
00169 // Int2IntMap::iterator where;
00170 // bool inserted;
00171 // tie(where, inserted) = myMap.insert(std::make_pair(123,456));
00172 //
00173 // if (inserted)
00174 //   // do stuff
00175 // else
00176 //   // do other stuff
00177 
00178 namespace
00179 {
00180   template <typename T1, typename T2>
00181   struct tier {
00182     typedef T1 &first_type;
00183     typedef T2 &second_type;
00184 
00185     first_type first;
00186     second_type second;
00187 
00188     tier(first_type f, second_type s) : first(f), second(s) { }
00189     tier& operator=(const std::pair<T1, T2>& p) {
00190       first = p.first;
00191       second = p.second;
00192       return *this;
00193     }
00194   };
00195 }
00196 
00197 template <typename T1, typename T2>
00198 inline tier<T1, T2> tie(T1& f, T2& s) {
00199   return tier<T1, T2>(f, s);
00200 }
00201 
00202 } // End llvm namespace
00203 
00204 #endif