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