Eliminated the Unique class in favor of NonCopyable and NonCopyableV
[oota-llvm.git] / include / llvm / Support / 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 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<cfg::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 public:
72   typedef typename iterator_traits<RootIt>::iterator_category
73           iterator_category;
74   typedef typename iterator_traits<RootIt>::difference_type
75           difference_type;
76   typedef typename UnaryFunc::result_type value_type;
77   typedef typename UnaryFunc::result_type *pointer;
78   typedef void reference;        // Can't modify value returned by fn
79
80   typedef RootIt iterator_type;
81   typedef mapped_iterator<RootIt, UnaryFunc> _Self;
82
83   inline RootIt &getCurrent() const { return current; }
84
85   inline explicit mapped_iterator(const RootIt &I) : current(I) {}
86   inline mapped_iterator(const mapped_iterator &It) : current(It.current) {}
87
88   inline value_type operator*() const {   // All this work to do this 
89     return UnaryFunc()(*current);         // little change
90   }
91
92   _Self& operator++() { ++current; return *this; }
93   _Self& operator--() { --current; return *this; }
94   _Self  operator++(int) { _Self __tmp = *this; ++current; return __tmp; }
95   _Self  operator--(int) { _Self __tmp = *this; --current; return __tmp; }
96   _Self  operator+    (difference_type n) const { return _Self(current + n); }
97   _Self& operator+=   (difference_type n) { current += n; return *this; }
98   _Self  operator-    (difference_type n) const { return _Self(current - n); }
99   _Self& operator-=   (difference_type n) { current -= n; return *this; }
100   reference operator[](difference_type n) const { return *(*this + n); }  
101
102   inline bool operator==(const _Self &X) const { return current == X.current; }
103   inline bool operator< (const _Self &X) const { return current <  X.current; }
104
105   inline difference_type operator-(const _Self &X) const {
106     return current - X.current;
107   }
108 };
109
110 template <class _Iterator, class Func>
111 inline mapped_iterator<_Iterator, Func> 
112 operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
113           const mapped_iterator<_Iterator, Func>& X) {
114   return mapped_iterator<_Iterator, Func>(X.getCurrent() - N);
115 }
116
117 #else
118
119 // This fails to work, because some iterators are not classes, for example
120 // vector iterators are commonly value_type **'s
121 template <class RootIt, class UnaryFunc>
122 class mapped_iterator : public RootIt {
123 public:
124   typedef typename UnaryFunc::result_type value_type;
125   typedef typename UnaryFunc::result_type *pointer;
126   typedef void reference;        // Can't modify value returned by fn
127
128   typedef mapped_iterator<RootIt, UnaryFunc> _Self;
129   typedef RootIt super;
130   inline explicit mapped_iterator(const RootIt &I) : super(I) {}
131   inline mapped_iterator(const super &It) : super(It) {}
132
133   inline value_type operator*() const {     // All this work to do 
134     return UnaryFunc(super::operator*());   // this little thing
135   }
136 };
137 #endif
138
139 // map_iterator - Provide a convenient way to create mapped_iterators, just like
140 // make_pair is useful for creating pairs...
141 //
142 template <class ItTy, class FuncTy>
143 inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
144   return mapped_iterator<ItTy, FuncTy>(I);
145 }
146
147
148 //===----------------------------------------------------------------------===//
149 //     Extra additions to <algorithm>
150 //===----------------------------------------------------------------------===//
151
152 // apply_until - Apply a functor to a sequence continually, unless the
153 // functor returns true.  Return true if the functor returned true, return false
154 // if the functor never returned true.
155 //
156 template <class InputIt, class Function>
157 bool apply_until(InputIt First, InputIt Last, Function Func) {
158   for ( ; First != Last; ++First)
159     if (Func(*First)) return true;
160   return false;
161 }
162
163
164 // reduce - Reduce a sequence values into a single value, given an initial
165 // value and an operator.
166 //
167 template <class InputIt, class Function, class ValueType>
168 ValueType reduce(InputIt First, InputIt Last, Function Func, ValueType Value) {
169   for ( ; First != Last; ++First)
170     Value = Func(*First, Value);
171   return Value;
172 }
173
174 #if 1   // This is likely to be more efficient
175
176 // reduce_apply - Reduce the result of applying a function to each value in a
177 // sequence, given an initial value, an operator, a function, and a sequence.
178 //
179 template <class InputIt, class Function, class ValueType, class TransFunc>
180 inline ValueType reduce_apply(InputIt First, InputIt Last, Function Func, 
181                               ValueType Value, TransFunc XForm) {
182   for ( ; First != Last; ++First)
183     Value = Func(XForm(*First), Value);
184   return Value;
185 }
186
187 #else  // This is arguably more elegant
188
189 // reduce_apply - Reduce the result of applying a function to each value in a
190 // sequence, given an initial value, an operator, a function, and a sequence.
191 //
192 template <class InputIt, class Function, class ValueType, class TransFunc>
193 inline ValueType reduce_apply2(InputIt First, InputIt Last, Function Func, 
194                                ValueType Value, TransFunc XForm) {
195   return reduce(map_iterator(First, XForm), map_iterator(Last, XForm),
196                 Func, Value);
197 }
198 #endif
199
200
201 // reduce_apply_bool - Reduce the result of applying a (bool returning) function
202 // to each value in a sequence.  All of the bools returned by the mapped
203 // function are bitwise or'd together, and the result is returned.
204 //
205 template <class InputIt, class Function>
206 inline bool reduce_apply_bool(InputIt First, InputIt Last, Function Func) {
207   return reduce_apply(First, Last, bitwise_or<bool>(), false, Func);
208 }
209
210 #endif