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