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