Add next() and prior() iterator utility functions. Unlike std::advance
[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
57 // deleter - Very very very simple method that is used to invoke operator
58 // delete on something.  It is used like this: 
59 //
60 //   for_each(V.begin(), B.end(), deleter<Interval>);
61 //
62 template <class T> 
63 static inline void deleter(T *Ptr) { 
64   delete Ptr; 
65 }
66
67
68
69 //===----------------------------------------------------------------------===//
70 //     Extra additions to <iterator>
71 //===----------------------------------------------------------------------===//
72
73 // mapped_iterator - This is a simple iterator adapter that causes a function to
74 // be dereferenced whenever operator* is invoked on the iterator.
75 //
76 // It turns out that this is disturbingly similar to boost::transform_iterator
77 //
78 template <class RootIt, class UnaryFunc>
79 class mapped_iterator {
80   RootIt current;
81   UnaryFunc Fn;
82 public:
83   typedef typename std::iterator_traits<RootIt>::iterator_category
84           iterator_category;
85   typedef typename std::iterator_traits<RootIt>::difference_type
86           difference_type;
87   typedef typename UnaryFunc::result_type value_type;
88
89   typedef void pointer;
90   //typedef typename UnaryFunc::result_type *pointer;
91   typedef void reference;        // Can't modify value returned by fn
92
93   typedef RootIt iterator_type;
94   typedef mapped_iterator<RootIt, UnaryFunc> _Self;
95
96   inline RootIt &getCurrent() const { return current; }
97
98   inline explicit mapped_iterator(const RootIt &I, UnaryFunc F)
99     : current(I), Fn(F) {}
100   inline mapped_iterator(const mapped_iterator &It)
101     : current(It.current), Fn(It.Fn) {}
102
103   inline value_type operator*() const {   // All this work to do this 
104     return Fn(*current);         // little change
105   }
106
107   _Self& operator++() { ++current; return *this; }
108   _Self& operator--() { --current; return *this; }
109   _Self  operator++(int) { _Self __tmp = *this; ++current; return __tmp; }
110   _Self  operator--(int) { _Self __tmp = *this; --current; return __tmp; }
111   _Self  operator+    (difference_type n) const { return _Self(current + n); }
112   _Self& operator+=   (difference_type n) { current += n; return *this; }
113   _Self  operator-    (difference_type n) const { return _Self(current - n); }
114   _Self& operator-=   (difference_type n) { current -= n; return *this; }
115   reference operator[](difference_type n) const { return *(*this + n); }  
116
117   inline bool operator!=(const _Self &X) const { return !operator==(X); }
118   inline bool operator==(const _Self &X) const { return current == X.current; }
119   inline bool operator< (const _Self &X) const { return current <  X.current; }
120
121   inline difference_type operator-(const _Self &X) const {
122     return current - X.current;
123   }
124 };
125
126 template <class _Iterator, class Func>
127 inline mapped_iterator<_Iterator, Func> 
128 operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
129           const mapped_iterator<_Iterator, Func>& X) {
130   return mapped_iterator<_Iterator, Func>(X.getCurrent() - N);
131 }
132
133
134 // map_iterator - Provide a convenient way to create mapped_iterators, just like
135 // make_pair is useful for creating pairs...
136 //
137 template <class ItTy, class FuncTy>
138 inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
139   return mapped_iterator<ItTy, FuncTy>(I, F);
140 }
141
142
143 // next/prior - These functions unlike std::advance do not modify the
144 // passed iterator but return a copy.
145 //
146 // next(myIt) returns copy of myIt incremented once
147 // next(myIt, n) returns copy of myIt incremented n times
148 // prior(myIt) returns copy of myIt decremented once
149 // prior(myIt, n) returns copy of myIt decremented n times
150
151 template <typename ItTy, typename Dist>
152 inline ItTy next(ItTy it, Dist n)
153 {
154   std::advance(it, n);
155   return it;
156 }
157
158 template <typename ItTy>
159 inline ItTy next(ItTy it)
160 {
161   std::advance(it, 1);
162   return it;
163 }
164
165 template <typename ItTy, typename Dist>
166 inline ItTy prior(ItTy it, Dist n)
167 {
168   std::advance(it, -n);
169   return it;
170 }
171
172 template <typename ItTy>
173 inline ItTy prior(ItTy it)
174 {
175   std::advance(it, -1);
176   return it;
177 }
178
179
180 //===----------------------------------------------------------------------===//
181 //     Extra additions to <algorithm>
182 //===----------------------------------------------------------------------===//
183
184 // apply_until - Apply a functor to a sequence continually, unless the
185 // functor returns true.  Return true if the functor returned true, return false
186 // if the functor never returned true.
187 //
188 template <class InputIt, class Function>
189 bool apply_until(InputIt First, InputIt Last, Function Func) {
190   for ( ; First != Last; ++First)
191     if (Func(*First)) return true;
192   return false;
193 }
194
195
196 // reduce - Reduce a sequence values into a single value, given an initial
197 // value and an operator.
198 //
199 template <class InputIt, class Function, class ValueType>
200 ValueType reduce(InputIt First, InputIt Last, Function Func, ValueType Value) {
201   for ( ; First != Last; ++First)
202     Value = Func(*First, Value);
203   return Value;
204 }
205
206 #if 1   // This is likely to be more efficient
207
208 // reduce_apply - Reduce the result of applying a function to each value in a
209 // sequence, given an initial value, an operator, a function, and a sequence.
210 //
211 template <class InputIt, class Function, class ValueType, class TransFunc>
212 inline ValueType reduce_apply(InputIt First, InputIt Last, Function Func, 
213                               ValueType Value, TransFunc XForm) {
214   for ( ; First != Last; ++First)
215     Value = Func(XForm(*First), Value);
216   return Value;
217 }
218
219 #else  // This is arguably more elegant
220
221 // reduce_apply - Reduce the result of applying a function to each value in a
222 // sequence, given an initial value, an operator, a function, and a sequence.
223 //
224 template <class InputIt, class Function, class ValueType, class TransFunc>
225 inline ValueType reduce_apply2(InputIt First, InputIt Last, Function Func, 
226                                ValueType Value, TransFunc XForm) {
227   return reduce(map_iterator(First, XForm), map_iterator(Last, XForm),
228                 Func, Value);
229 }
230 #endif
231
232
233 // reduce_apply_bool - Reduce the result of applying a (bool returning) function
234 // to each value in a sequence.  All of the bools returned by the mapped
235 // function are bitwise or'd together, and the result is returned.
236 //
237 template <class InputIt, class Function>
238 inline bool reduce_apply_bool(InputIt First, InputIt Last, Function Func) {
239   return reduce_apply(First, Last, bitwise_or<bool>(), false, Func);
240 }
241
242
243 // map - This function maps the specified input sequence into the specified
244 // output iterator, applying a unary function in between.
245 //
246 template <class InIt, class OutIt, class Functor>
247 inline OutIt mapto(InIt Begin, InIt End, OutIt Dest, Functor F) {
248   return copy(map_iterator(Begin, F), map_iterator(End, F), Dest);
249 }
250
251
252 //===----------------------------------------------------------------------===//
253 //     Extra additions to <utility>
254 //===----------------------------------------------------------------------===//
255
256 // tie - this function ties two objects and returns a temporary object
257 // that is assignable from a std::pair. This can be used to make code
258 // more readable when using values returned from functions bundled in
259 // a std::pair. Since an example is worth 1000 words:
260 //
261 // typedef std::map<int, int> Int2IntMap;
262 // 
263 // Int2IntMap myMap;
264 // Int2IntMap::iterator where;
265 // bool inserted;
266 // tie(where, inserted) = myMap.insert(std::make_pair(123,456));
267 //
268 // if (inserted)
269 //   // do stuff
270 // else
271 //   // do other stuff
272
273 namespace
274 {
275   template <typename T1, typename T2>
276   struct tier {
277     typedef T1 &first_type;
278     typedef T2 &second_type;
279
280     first_type first;
281     second_type second;
282
283     tier(first_type f, second_type s) : first(f), second(s) { }
284     tier& operator=(const std::pair<T1, T2>& p) {
285       first = p.first;
286       second = p.second;
287       return *this;
288     }
289   };
290 }
291
292 template <typename T1, typename T2>
293 inline tier<T1, T2> tie(T1& f, T2& s) {
294   return tier<T1, T2>(f, s);
295 }
296
297 } // End llvm namespace
298
299 #endif