Added LLVM notice.
[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 //===----------------------------------------------------------------------===//
24 //     Extra additions to <functional>
25 //===----------------------------------------------------------------------===//
26
27 // bind_obj - Often times you want to apply the member function of an object
28 // as a unary functor.  This macro is shorthand that makes it happen less
29 // verbosely.
30 //
31 // Example:
32 //  struct Summer { void accumulate(int x); }
33 //  vector<int> Numbers;
34 //  Summer MyS;
35 //  for_each(Numbers.begin(), Numbers.end(),
36 //           bind_obj(&MyS, &Summer::accumulate));
37 //
38 // TODO: When I get lots of extra time, convert this from an evil macro
39 //
40 #define bind_obj(OBJ, METHOD) std::bind1st(std::mem_fun(METHOD), OBJ)
41
42
43 // bitwise_or - This is a simple functor that applys operator| on its two 
44 // arguments to get a boolean result.
45 //
46 template<class Ty>
47 struct bitwise_or : public std::binary_function<Ty, Ty, bool> {
48   bool operator()(const Ty& left, const Ty& right) const {
49     return left | right;
50   }
51 };
52
53
54 // deleter - Very very very simple method that is used to invoke operator
55 // delete on something.  It is used like this: 
56 //
57 //   for_each(V.begin(), B.end(), deleter<Interval>);
58 //
59 template <class T> 
60 static inline void deleter(T *Ptr) { 
61   delete Ptr; 
62 }
63
64
65
66 //===----------------------------------------------------------------------===//
67 //     Extra additions to <iterator>
68 //===----------------------------------------------------------------------===//
69
70 // mapped_iterator - This is a simple iterator adapter that causes a function to
71 // be dereferenced whenever operator* is invoked on the iterator.
72 //
73 // It turns out that this is disturbingly similar to boost::transform_iterator
74 //
75 #if 1
76 template <class RootIt, class UnaryFunc>
77 class mapped_iterator {
78   RootIt current;
79   UnaryFunc Fn;
80 public:
81   typedef typename std::iterator_traits<RootIt>::iterator_category
82           iterator_category;
83   typedef typename std::iterator_traits<RootIt>::difference_type
84           difference_type;
85   typedef typename UnaryFunc::result_type value_type;
86
87   typedef void pointer;
88   //typedef typename UnaryFunc::result_type *pointer;
89   typedef void reference;        // Can't modify value returned by fn
90
91   typedef RootIt iterator_type;
92   typedef mapped_iterator<RootIt, UnaryFunc> _Self;
93
94   inline RootIt &getCurrent() const { return current; }
95
96   inline explicit mapped_iterator(const RootIt &I, UnaryFunc F)
97     : current(I), Fn(F) {}
98   inline mapped_iterator(const mapped_iterator &It)
99     : current(It.current), Fn(It.Fn) {}
100
101   inline value_type operator*() const {   // All this work to do this 
102     return Fn(*current);         // little change
103   }
104
105   _Self& operator++() { ++current; return *this; }
106   _Self& operator--() { --current; return *this; }
107   _Self  operator++(int) { _Self __tmp = *this; ++current; return __tmp; }
108   _Self  operator--(int) { _Self __tmp = *this; --current; return __tmp; }
109   _Self  operator+    (difference_type n) const { return _Self(current + n); }
110   _Self& operator+=   (difference_type n) { current += n; return *this; }
111   _Self  operator-    (difference_type n) const { return _Self(current - n); }
112   _Self& operator-=   (difference_type n) { current -= n; return *this; }
113   reference operator[](difference_type n) const { return *(*this + n); }  
114
115   inline bool operator!=(const _Self &X) const { return !operator==(X); }
116   inline bool operator==(const _Self &X) const { return current == X.current; }
117   inline bool operator< (const _Self &X) const { return current <  X.current; }
118
119   inline difference_type operator-(const _Self &X) const {
120     return current - X.current;
121   }
122 };
123
124 template <class _Iterator, class Func>
125 inline mapped_iterator<_Iterator, Func> 
126 operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
127           const mapped_iterator<_Iterator, Func>& X) {
128   return mapped_iterator<_Iterator, Func>(X.getCurrent() - N);
129 }
130
131 #else
132
133 // This fails to work, because some iterators are not classes, for example
134 // vector iterators are commonly value_type **'s
135 template <class RootIt, class UnaryFunc>
136 class mapped_iterator : public RootIt {
137   UnaryFunc Fn;
138 public:
139   typedef typename UnaryFunc::result_type value_type;
140   typedef typename UnaryFunc::result_type *pointer;
141   typedef void reference;        // Can't modify value returned by fn
142
143   typedef mapped_iterator<RootIt, UnaryFunc> _Self;
144   typedef RootIt super;
145   inline explicit mapped_iterator(const RootIt &I) : super(I) {}
146   inline mapped_iterator(const super &It) : super(It) {}
147
148   inline value_type operator*() const {     // All this work to do 
149     return Fn(super::operator*());   // this little thing
150   }
151 };
152 #endif
153
154 // map_iterator - Provide a convenient way to create mapped_iterators, just like
155 // make_pair is useful for creating pairs...
156 //
157 template <class ItTy, class FuncTy>
158 inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
159   return mapped_iterator<ItTy, FuncTy>(I, F);
160 }
161
162
163 //===----------------------------------------------------------------------===//
164 //     Extra additions to <algorithm>
165 //===----------------------------------------------------------------------===//
166
167 // apply_until - Apply a functor to a sequence continually, unless the
168 // functor returns true.  Return true if the functor returned true, return false
169 // if the functor never returned true.
170 //
171 template <class InputIt, class Function>
172 bool apply_until(InputIt First, InputIt Last, Function Func) {
173   for ( ; First != Last; ++First)
174     if (Func(*First)) return true;
175   return false;
176 }
177
178
179 // reduce - Reduce a sequence values into a single value, given an initial
180 // value and an operator.
181 //
182 template <class InputIt, class Function, class ValueType>
183 ValueType reduce(InputIt First, InputIt Last, Function Func, ValueType Value) {
184   for ( ; First != Last; ++First)
185     Value = Func(*First, Value);
186   return Value;
187 }
188
189 #if 1   // This is likely to be more efficient
190
191 // reduce_apply - Reduce the result of applying a function to each value in a
192 // sequence, given an initial value, an operator, a function, and a sequence.
193 //
194 template <class InputIt, class Function, class ValueType, class TransFunc>
195 inline ValueType reduce_apply(InputIt First, InputIt Last, Function Func, 
196                               ValueType Value, TransFunc XForm) {
197   for ( ; First != Last; ++First)
198     Value = Func(XForm(*First), Value);
199   return Value;
200 }
201
202 #else  // This is arguably more elegant
203
204 // reduce_apply - Reduce the result of applying a function to each value in a
205 // sequence, given an initial value, an operator, a function, and a sequence.
206 //
207 template <class InputIt, class Function, class ValueType, class TransFunc>
208 inline ValueType reduce_apply2(InputIt First, InputIt Last, Function Func, 
209                                ValueType Value, TransFunc XForm) {
210   return reduce(map_iterator(First, XForm), map_iterator(Last, XForm),
211                 Func, Value);
212 }
213 #endif
214
215
216 // reduce_apply_bool - Reduce the result of applying a (bool returning) function
217 // to each value in a sequence.  All of the bools returned by the mapped
218 // function are bitwise or'd together, and the result is returned.
219 //
220 template <class InputIt, class Function>
221 inline bool reduce_apply_bool(InputIt First, InputIt Last, Function Func) {
222   return reduce_apply(First, Last, bitwise_or<bool>(), false, Func);
223 }
224
225
226 // map - This function maps the specified input sequence into the specified
227 // output iterator, applying a unary function in between.
228 //
229 template <class InIt, class OutIt, class Functor>
230 inline OutIt mapto(InIt Begin, InIt End, OutIt Dest, Functor F) {
231   return copy(map_iterator(Begin, F), map_iterator(End, F), Dest);
232 }
233 #endif