Remove needless dependence on boost
[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 "Support/iterator"
22
23 namespace llvm {
24
25 //===----------------------------------------------------------------------===//
26 //     Extra additions to <functional>
27 //===----------------------------------------------------------------------===//
28
29 // bind_obj - Often times you want to apply the member function of an object
30 // as a unary functor.  This macro is shorthand that makes it happen less
31 // verbosely.
32 //
33 // Example:
34 //  struct Summer { void accumulate(int x); }
35 //  vector<int> Numbers;
36 //  Summer MyS;
37 //  for_each(Numbers.begin(), Numbers.end(),
38 //           bind_obj(&MyS, &Summer::accumulate));
39 //
40 // TODO: When I get lots of extra time, convert this from an evil macro
41 //
42 #define bind_obj(OBJ, METHOD) std::bind1st(std::mem_fun(METHOD), OBJ)
43
44
45 // bitwise_or - This is a simple functor that applys operator| on its two 
46 // arguments to get a boolean result.
47 //
48 template<class Ty>
49 struct bitwise_or : public std::binary_function<Ty, Ty, bool> {
50   bool operator()(const Ty& left, const Ty& right) const {
51     return left | right;
52   }
53 };
54
55
56 // deleter - Very very very simple method that is used to invoke operator
57 // delete on something.  It is used like this: 
58 //
59 //   for_each(V.begin(), B.end(), deleter<Interval>);
60 //
61 template <class T> 
62 static inline void deleter(T *Ptr) { 
63   delete Ptr; 
64 }
65
66
67
68 //===----------------------------------------------------------------------===//
69 //     Extra additions to <iterator>
70 //===----------------------------------------------------------------------===//
71
72 // mapped_iterator - This is a simple iterator adapter that causes a function to
73 // be dereferenced whenever operator* is invoked on the iterator.
74 //
75 // It turns out that this is disturbingly similar to boost::transform_iterator
76 //
77 #if 1
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 #else
134
135 // This fails to work, because some iterators are not classes, for example
136 // vector iterators are commonly value_type **'s
137 template <class RootIt, class UnaryFunc>
138 class mapped_iterator : public RootIt {
139   UnaryFunc Fn;
140 public:
141   typedef typename UnaryFunc::result_type value_type;
142   typedef typename UnaryFunc::result_type *pointer;
143   typedef void reference;        // Can't modify value returned by fn
144
145   typedef mapped_iterator<RootIt, UnaryFunc> _Self;
146   typedef RootIt super;
147   inline explicit mapped_iterator(const RootIt &I) : super(I) {}
148   inline mapped_iterator(const super &It) : super(It) {}
149
150   inline value_type operator*() const {     // All this work to do 
151     return Fn(super::operator*());   // this little thing
152   }
153 };
154 #endif
155
156 // map_iterator - Provide a convenient way to create mapped_iterators, just like
157 // make_pair is useful for creating pairs...
158 //
159 template <class ItTy, class FuncTy>
160 inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
161   return mapped_iterator<ItTy, FuncTy>(I, F);
162 }
163
164
165 //===----------------------------------------------------------------------===//
166 //     Extra additions to <algorithm>
167 //===----------------------------------------------------------------------===//
168
169 // apply_until - Apply a functor to a sequence continually, unless the
170 // functor returns true.  Return true if the functor returned true, return false
171 // if the functor never returned true.
172 //
173 template <class InputIt, class Function>
174 bool apply_until(InputIt First, InputIt Last, Function Func) {
175   for ( ; First != Last; ++First)
176     if (Func(*First)) return true;
177   return false;
178 }
179
180
181 // reduce - Reduce a sequence values into a single value, given an initial
182 // value and an operator.
183 //
184 template <class InputIt, class Function, class ValueType>
185 ValueType reduce(InputIt First, InputIt Last, Function Func, ValueType Value) {
186   for ( ; First != Last; ++First)
187     Value = Func(*First, Value);
188   return Value;
189 }
190
191 #if 1   // This is likely to be more efficient
192
193 // reduce_apply - Reduce the result of applying a function to each value in a
194 // sequence, given an initial value, an operator, a function, and a sequence.
195 //
196 template <class InputIt, class Function, class ValueType, class TransFunc>
197 inline ValueType reduce_apply(InputIt First, InputIt Last, Function Func, 
198                               ValueType Value, TransFunc XForm) {
199   for ( ; First != Last; ++First)
200     Value = Func(XForm(*First), Value);
201   return Value;
202 }
203
204 #else  // This is arguably more elegant
205
206 // reduce_apply - Reduce the result of applying a function to each value in a
207 // sequence, given an initial value, an operator, a function, and a sequence.
208 //
209 template <class InputIt, class Function, class ValueType, class TransFunc>
210 inline ValueType reduce_apply2(InputIt First, InputIt Last, Function Func, 
211                                ValueType Value, TransFunc XForm) {
212   return reduce(map_iterator(First, XForm), map_iterator(Last, XForm),
213                 Func, Value);
214 }
215 #endif
216
217
218 // reduce_apply_bool - Reduce the result of applying a (bool returning) function
219 // to each value in a sequence.  All of the bools returned by the mapped
220 // function are bitwise or'd together, and the result is returned.
221 //
222 template <class InputIt, class Function>
223 inline bool reduce_apply_bool(InputIt First, InputIt Last, Function Func) {
224   return reduce_apply(First, Last, bitwise_or<bool>(), false, Func);
225 }
226
227
228 // map - This function maps the specified input sequence into the specified
229 // output iterator, applying a unary function in between.
230 //
231 template <class InIt, class OutIt, class Functor>
232 inline OutIt mapto(InIt Begin, InIt End, OutIt Dest, Functor F) {
233   return copy(map_iterator(Begin, F), map_iterator(End, F), Dest);
234 }
235
236
237 //===----------------------------------------------------------------------===//
238 //     Extra additions to <utility>
239 //===----------------------------------------------------------------------===//
240
241 // tie - this function ties two objects and returns a temporary object
242 // that is assignable from a std::pair. This can be used to make code
243 // more readable when using values returned from functions bundled in
244 // a std::pair. Since an example is worth 1000 words:
245 //
246 // typedef std::map<int, int> Int2IntMap;
247 // 
248 // Int2IntMap myMap;
249 // Int2IntMap::iterator where;
250 // bool inserted;
251 // tie(where, inserted) = myMap.insert(std::make_pair(123,456));
252 //
253 // if (inserted)
254 //   // do stuff
255 // else
256 //   // do other stuff
257
258 namespace
259 {
260   template <typename T1, typename T2>
261   struct tier {
262     typedef T1 &first_type;
263     typedef T2 &second_type;
264
265     first_type first;
266     second_type second;
267
268     tier(first_type f, second_type s) : first(f), second(s) { }
269     tier& operator=(const std::pair<T1, T2>& p) {
270       first = p.first;
271       second = p.second;
272       return *this;
273     }
274   };
275 }
276
277 template <typename T1, typename T2>
278 inline tier<T1, T2> tie(T1& f, T2& s) {
279   return tier<T1, T2>(f, s);
280 }
281
282 } // End llvm namespace
283
284 #endif