Add comparator useful for natural comparisons on collections with
[oota-llvm.git] / include / llvm / ADT / STLExtras.h
1 //===- STLExtras.h - Useful functions when working with the STL -*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 SUPPORT_STLEXTRAS_H
18 #define SUPPORT_STLEXTRAS_H
19
20 #include <functional>
21 #include <utility> // for std::pair
22 #include "Support/iterator"
23
24 namespace llvm {
25
26 //===----------------------------------------------------------------------===//
27 //     Extra additions to <functional>
28 //===----------------------------------------------------------------------===//
29
30 // bind_obj - Often times you want to apply the member function of an object
31 // as a unary functor.  This macro is shorthand that makes it happen less
32 // verbosely.
33 //
34 // Example:
35 //  struct Summer { void accumulate(int x); }
36 //  vector<int> Numbers;
37 //  Summer MyS;
38 //  for_each(Numbers.begin(), Numbers.end(),
39 //           bind_obj(&MyS, &Summer::accumulate));
40 //
41 // TODO: When I get lots of extra time, convert this from an evil macro
42 //
43 #define bind_obj(OBJ, METHOD) std::bind1st(std::mem_fun(METHOD), OBJ)
44
45
46 // bitwise_or - This is a simple functor that applys operator| on its two 
47 // arguments to get a boolean result.
48 //
49 template<class Ty>
50 struct bitwise_or : public std::binary_function<Ty, Ty, bool> {
51   bool operator()(const Ty& left, const Ty& right) const {
52     return left | right;
53   }
54 };
55
56 template<class Ty>
57 struct less_ptr : public std::binary_function<Ty, Ty, bool> {
58   bool operator()(const Ty* left, const Ty* right) const {
59     return *left < *right;
60   }
61 };
62
63 // deleter - Very very very simple method that is used to invoke operator
64 // delete on something.  It is used like this: 
65 //
66 //   for_each(V.begin(), B.end(), deleter<Interval>);
67 //
68 template <class T> 
69 static inline void deleter(T *Ptr) { 
70   delete Ptr; 
71 }
72
73
74
75 //===----------------------------------------------------------------------===//
76 //     Extra additions to <iterator>
77 //===----------------------------------------------------------------------===//
78
79 // mapped_iterator - This is a simple iterator adapter that causes a function to
80 // be dereferenced whenever operator* is invoked on the iterator.
81 //
82 template <class RootIt, class UnaryFunc>
83 class mapped_iterator {
84   RootIt current;
85   UnaryFunc Fn;
86 public:
87   typedef typename std::iterator_traits<RootIt>::iterator_category
88           iterator_category;
89   typedef typename std::iterator_traits<RootIt>::difference_type
90           difference_type;
91   typedef typename UnaryFunc::result_type value_type;
92
93   typedef void pointer;
94   //typedef typename UnaryFunc::result_type *pointer;
95   typedef void reference;        // Can't modify value returned by fn
96
97   typedef RootIt iterator_type;
98   typedef mapped_iterator<RootIt, UnaryFunc> _Self;
99
100   inline RootIt &getCurrent() const { return current; }
101
102   inline explicit mapped_iterator(const RootIt &I, UnaryFunc F)
103     : current(I), Fn(F) {}
104   inline mapped_iterator(const mapped_iterator &It)
105     : current(It.current), Fn(It.Fn) {}
106
107   inline value_type operator*() const {   // All this work to do this 
108     return Fn(*current);         // little change
109   }
110
111   _Self& operator++() { ++current; return *this; }
112   _Self& operator--() { --current; return *this; }
113   _Self  operator++(int) { _Self __tmp = *this; ++current; return __tmp; }
114   _Self  operator--(int) { _Self __tmp = *this; --current; return __tmp; }
115   _Self  operator+    (difference_type n) const { return _Self(current + n); }
116   _Self& operator+=   (difference_type n) { current += n; return *this; }
117   _Self  operator-    (difference_type n) const { return _Self(current - n); }
118   _Self& operator-=   (difference_type n) { current -= n; return *this; }
119   reference operator[](difference_type n) const { return *(*this + n); }  
120
121   inline bool operator!=(const _Self &X) const { return !operator==(X); }
122   inline bool operator==(const _Self &X) const { return current == X.current; }
123   inline bool operator< (const _Self &X) const { return current <  X.current; }
124
125   inline difference_type operator-(const _Self &X) const {
126     return current - X.current;
127   }
128 };
129
130 template <class _Iterator, class Func>
131 inline mapped_iterator<_Iterator, Func> 
132 operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
133           const mapped_iterator<_Iterator, Func>& X) {
134   return mapped_iterator<_Iterator, Func>(X.getCurrent() - N);
135 }
136
137
138 // map_iterator - Provide a convenient way to create mapped_iterators, just like
139 // make_pair is useful for creating pairs...
140 //
141 template <class ItTy, class FuncTy>
142 inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
143   return mapped_iterator<ItTy, FuncTy>(I, F);
144 }
145
146
147 // next/prior - These functions unlike std::advance do not modify the
148 // passed iterator but return a copy.
149 //
150 // next(myIt) returns copy of myIt incremented once
151 // next(myIt, n) returns copy of myIt incremented n times
152 // prior(myIt) returns copy of myIt decremented once
153 // prior(myIt, n) returns copy of myIt decremented n times
154
155 template <typename ItTy, typename Dist>
156 inline ItTy next(ItTy it, Dist n)
157 {
158   std::advance(it, n);
159   return it;
160 }
161
162 template <typename ItTy>
163 inline ItTy next(ItTy it)
164 {
165   std::advance(it, 1);
166   return it;
167 }
168
169 template <typename ItTy, typename Dist>
170 inline ItTy prior(ItTy it, Dist n)
171 {
172   std::advance(it, -n);
173   return it;
174 }
175
176 template <typename ItTy>
177 inline ItTy prior(ItTy it)
178 {
179   std::advance(it, -1);
180   return it;
181 }
182
183
184 //===----------------------------------------------------------------------===//
185 //     Extra additions to <algorithm>
186 //===----------------------------------------------------------------------===//
187
188 // apply_until - Apply a functor to a sequence continually, unless the
189 // functor returns true.  Return true if the functor returned true, return false
190 // if the functor never returned true.
191 //
192 template <class InputIt, class Function>
193 bool apply_until(InputIt First, InputIt Last, Function Func) {
194   for ( ; First != Last; ++First)
195     if (Func(*First)) return true;
196   return false;
197 }
198
199
200 // reduce - Reduce a sequence values into a single value, given an initial
201 // value and an operator.
202 //
203 template <class InputIt, class Function, class ValueType>
204 ValueType reduce(InputIt First, InputIt Last, Function Func, ValueType Value) {
205   for ( ; First != Last; ++First)
206     Value = Func(*First, Value);
207   return Value;
208 }
209
210 #if 1   // This is likely to be more efficient
211
212 // reduce_apply - Reduce the result of applying a function to each value in a
213 // sequence, given an initial value, an operator, a function, and a sequence.
214 //
215 template <class InputIt, class Function, class ValueType, class TransFunc>
216 inline ValueType reduce_apply(InputIt First, InputIt Last, Function Func, 
217                               ValueType Value, TransFunc XForm) {
218   for ( ; First != Last; ++First)
219     Value = Func(XForm(*First), Value);
220   return Value;
221 }
222
223 #else  // This is arguably more elegant
224
225 // reduce_apply - Reduce the result of applying a function to each value in a
226 // sequence, given an initial value, an operator, a function, and a sequence.
227 //
228 template <class InputIt, class Function, class ValueType, class TransFunc>
229 inline ValueType reduce_apply2(InputIt First, InputIt Last, Function Func, 
230                                ValueType Value, TransFunc XForm) {
231   return reduce(map_iterator(First, XForm), map_iterator(Last, XForm),
232                 Func, Value);
233 }
234 #endif
235
236
237 // reduce_apply_bool - Reduce the result of applying a (bool returning) function
238 // to each value in a sequence.  All of the bools returned by the mapped
239 // function are bitwise or'd together, and the result is returned.
240 //
241 template <class InputIt, class Function>
242 inline bool reduce_apply_bool(InputIt First, InputIt Last, Function Func) {
243   return reduce_apply(First, Last, bitwise_or<bool>(), false, Func);
244 }
245
246
247 // map - This function maps the specified input sequence into the specified
248 // output iterator, applying a unary function in between.
249 //
250 template <class InIt, class OutIt, class Functor>
251 inline OutIt mapto(InIt Begin, InIt End, OutIt Dest, Functor F) {
252   return copy(map_iterator(Begin, F), map_iterator(End, F), Dest);
253 }
254
255
256 //===----------------------------------------------------------------------===//
257 //     Extra additions to <utility>
258 //===----------------------------------------------------------------------===//
259
260 // tie - this function ties two objects and returns a temporary object
261 // that is assignable from a std::pair. This can be used to make code
262 // more readable when using values returned from functions bundled in
263 // a std::pair. Since an example is worth 1000 words:
264 //
265 // typedef std::map<int, int> Int2IntMap;
266 // 
267 // Int2IntMap myMap;
268 // Int2IntMap::iterator where;
269 // bool inserted;
270 // tie(where, inserted) = myMap.insert(std::make_pair(123,456));
271 //
272 // if (inserted)
273 //   // do stuff
274 // else
275 //   // do other stuff
276
277 namespace
278 {
279   template <typename T1, typename T2>
280   struct tier {
281     typedef T1 &first_type;
282     typedef T2 &second_type;
283
284     first_type first;
285     second_type second;
286
287     tier(first_type f, second_type s) : first(f), second(s) { }
288     tier& operator=(const std::pair<T1, T2>& p) {
289       first = p.first;
290       second = p.second;
291       return *this;
292     }
293   };
294 }
295
296 template <typename T1, typename T2>
297 inline tier<T1, T2> tie(T1& f, T2& s) {
298   return tier<T1, T2>(f, s);
299 }
300
301 } // End llvm namespace
302
303 #endif