blob: 233b7438a4d51a8142d4e59b35635fbfa7702f5c [file] [log] [blame]
Chris Lattner48486892003-09-30 18:37:50 +00001//===- STLExtras.h - Useful functions when working with the STL -*- C++ -*-===//
John Criswellb2109ce2003-10-20 19:46:57 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner18d64ed2001-06-21 05:25:51 +00009//
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
Brian Gaekea9f6e4a2003-06-17 00:35:55 +000017#ifndef SUPPORT_STLEXTRAS_H
18#define SUPPORT_STLEXTRAS_H
Chris Lattner18d64ed2001-06-21 05:25:51 +000019
20#include <functional>
Brian Gaeke9bb21882003-11-23 03:50:31 +000021#include <utility> // for std::pair
Chris Lattnerdf6f5832002-10-27 19:16:27 +000022#include "Support/iterator"
Chris Lattner18d64ed2001-06-21 05:25:51 +000023
Brian Gaeked0fde302003-11-11 22:41:34 +000024namespace llvm {
25
Chris Lattner18d64ed2001-06-21 05:25:51 +000026//===----------------------------------------------------------------------===//
Chris Lattner42e018c2001-06-27 23:32:12 +000027// 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//
49template<class Ty>
Chris Lattner697954c2002-01-20 22:54:45 +000050struct bitwise_or : public std::binary_function<Ty, Ty, bool> {
Chris Lattner42e018c2001-06-27 23:32:12 +000051 bool operator()(const Ty& left, const Ty& right) const {
52 return left | right;
53 }
54};
55
56
Chris Lattner577b15f2001-07-02 01:09:41 +000057// deleter - Very very very simple method that is used to invoke operator
58// delete on something. It is used like this:
59//
Chris Lattner87650962002-04-28 16:18:32 +000060// for_each(V.begin(), B.end(), deleter<Interval>);
Chris Lattner577b15f2001-07-02 01:09:41 +000061//
62template <class T>
63static inline void deleter(T *Ptr) {
64 delete Ptr;
65}
66
67
68
Chris Lattner42e018c2001-06-27 23:32:12 +000069//===----------------------------------------------------------------------===//
Chris Lattner18d64ed2001-06-21 05:25:51 +000070// 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//
Chris Lattner18d64ed2001-06-21 05:25:51 +000076template <class RootIt, class UnaryFunc>
77class mapped_iterator {
78 RootIt current;
Chris Lattner643afb32001-09-07 16:30:28 +000079 UnaryFunc Fn;
Chris Lattner18d64ed2001-06-21 05:25:51 +000080public:
Chris Lattner697954c2002-01-20 22:54:45 +000081 typedef typename std::iterator_traits<RootIt>::iterator_category
Chris Lattner18d64ed2001-06-21 05:25:51 +000082 iterator_category;
Chris Lattner697954c2002-01-20 22:54:45 +000083 typedef typename std::iterator_traits<RootIt>::difference_type
Chris Lattner18d64ed2001-06-21 05:25:51 +000084 difference_type;
85 typedef typename UnaryFunc::result_type value_type;
Chris Lattnerd0637252002-10-13 19:30:44 +000086
87 typedef void pointer;
88 //typedef typename UnaryFunc::result_type *pointer;
Chris Lattner18d64ed2001-06-21 05:25:51 +000089 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
Chris Lattner643afb32001-09-07 16:30:28 +000096 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) {}
Chris Lattner18d64ed2001-06-21 05:25:51 +0000100
101 inline value_type operator*() const { // All this work to do this
Chris Lattner643afb32001-09-07 16:30:28 +0000102 return Fn(*current); // little change
Chris Lattner18d64ed2001-06-21 05:25:51 +0000103 }
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
Chris Lattner697954c2002-01-20 22:54:45 +0000115 inline bool operator!=(const _Self &X) const { return !operator==(X); }
Chris Lattner18d64ed2001-06-21 05:25:51 +0000116 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
124template <class _Iterator, class Func>
125inline mapped_iterator<_Iterator, Func>
126operator+(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
Chris Lattner18d64ed2001-06-21 05:25:51 +0000131
132// map_iterator - Provide a convenient way to create mapped_iterators, just like
133// make_pair is useful for creating pairs...
134//
135template <class ItTy, class FuncTy>
136inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
Chris Lattner643afb32001-09-07 16:30:28 +0000137 return mapped_iterator<ItTy, FuncTy>(I, F);
Chris Lattner18d64ed2001-06-21 05:25:51 +0000138}
139
140
Alkis Evlogimenosbc794712004-02-14 01:17:28 +0000141// next/prior - These functions unlike std::advance do not modify the
142// passed iterator but return a copy.
143//
144// next(myIt) returns copy of myIt incremented once
145// next(myIt, n) returns copy of myIt incremented n times
146// prior(myIt) returns copy of myIt decremented once
147// prior(myIt, n) returns copy of myIt decremented n times
148
149template <typename ItTy, typename Dist>
150inline ItTy next(ItTy it, Dist n)
151{
152 std::advance(it, n);
153 return it;
154}
155
156template <typename ItTy>
157inline ItTy next(ItTy it)
158{
159 std::advance(it, 1);
160 return it;
161}
162
163template <typename ItTy, typename Dist>
164inline ItTy prior(ItTy it, Dist n)
165{
166 std::advance(it, -n);
167 return it;
168}
169
170template <typename ItTy>
171inline ItTy prior(ItTy it)
172{
173 std::advance(it, -1);
174 return it;
175}
176
177
Chris Lattner18d64ed2001-06-21 05:25:51 +0000178//===----------------------------------------------------------------------===//
179// Extra additions to <algorithm>
180//===----------------------------------------------------------------------===//
181
Chris Lattner42e018c2001-06-27 23:32:12 +0000182// apply_until - Apply a functor to a sequence continually, unless the
183// functor returns true. Return true if the functor returned true, return false
184// if the functor never returned true.
185//
186template <class InputIt, class Function>
187bool apply_until(InputIt First, InputIt Last, Function Func) {
188 for ( ; First != Last; ++First)
189 if (Func(*First)) return true;
190 return false;
191}
192
193
Chris Lattner18d64ed2001-06-21 05:25:51 +0000194// reduce - Reduce a sequence values into a single value, given an initial
195// value and an operator.
196//
197template <class InputIt, class Function, class ValueType>
198ValueType reduce(InputIt First, InputIt Last, Function Func, ValueType Value) {
199 for ( ; First != Last; ++First)
200 Value = Func(*First, Value);
201 return Value;
202}
203
204#if 1 // This is likely to be more efficient
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//
209template <class InputIt, class Function, class ValueType, class TransFunc>
Chris Lattner42e018c2001-06-27 23:32:12 +0000210inline ValueType reduce_apply(InputIt First, InputIt Last, Function Func,
211 ValueType Value, TransFunc XForm) {
Chris Lattner18d64ed2001-06-21 05:25:51 +0000212 for ( ; First != Last; ++First)
213 Value = Func(XForm(*First), Value);
214 return Value;
215}
216
217#else // This is arguably more elegant
218
219// reduce_apply - Reduce the result of applying a function to each value in a
220// sequence, given an initial value, an operator, a function, and a sequence.
221//
222template <class InputIt, class Function, class ValueType, class TransFunc>
Chris Lattner42e018c2001-06-27 23:32:12 +0000223inline ValueType reduce_apply2(InputIt First, InputIt Last, Function Func,
224 ValueType Value, TransFunc XForm) {
225 return reduce(map_iterator(First, XForm), map_iterator(Last, XForm),
Chris Lattner18d64ed2001-06-21 05:25:51 +0000226 Func, Value);
227}
228#endif
229
230
Chris Lattner42e018c2001-06-27 23:32:12 +0000231// reduce_apply_bool - Reduce the result of applying a (bool returning) function
232// to each value in a sequence. All of the bools returned by the mapped
233// function are bitwise or'd together, and the result is returned.
Chris Lattner3704c8c2001-06-25 03:54:32 +0000234//
Chris Lattner42e018c2001-06-27 23:32:12 +0000235template <class InputIt, class Function>
236inline bool reduce_apply_bool(InputIt First, InputIt Last, Function Func) {
237 return reduce_apply(First, Last, bitwise_or<bool>(), false, Func);
238}
Chris Lattner18d64ed2001-06-21 05:25:51 +0000239
Chris Lattner643afb32001-09-07 16:30:28 +0000240
241// map - This function maps the specified input sequence into the specified
242// output iterator, applying a unary function in between.
243//
244template <class InIt, class OutIt, class Functor>
245inline OutIt mapto(InIt Begin, InIt End, OutIt Dest, Functor F) {
246 return copy(map_iterator(Begin, F), map_iterator(End, F), Dest);
247}
Alkis Evlogimenose292da22003-11-05 05:58:26 +0000248
249
250//===----------------------------------------------------------------------===//
251// Extra additions to <utility>
252//===----------------------------------------------------------------------===//
253
254// tie - this function ties two objects and returns a temporary object
255// that is assignable from a std::pair. This can be used to make code
256// more readable when using values returned from functions bundled in
257// a std::pair. Since an example is worth 1000 words:
258//
259// typedef std::map<int, int> Int2IntMap;
260//
261// Int2IntMap myMap;
262// Int2IntMap::iterator where;
263// bool inserted;
264// tie(where, inserted) = myMap.insert(std::make_pair(123,456));
265//
266// if (inserted)
267// // do stuff
268// else
269// // do other stuff
270
271namespace
272{
273 template <typename T1, typename T2>
274 struct tier {
Chris Lattnera3f5f802003-11-13 04:11:30 +0000275 typedef T1 &first_type;
276 typedef T2 &second_type;
Alkis Evlogimenose292da22003-11-05 05:58:26 +0000277
278 first_type first;
279 second_type second;
280
281 tier(first_type f, second_type s) : first(f), second(s) { }
282 tier& operator=(const std::pair<T1, T2>& p) {
283 first = p.first;
284 second = p.second;
285 return *this;
286 }
287 };
288}
289
290template <typename T1, typename T2>
291inline tier<T1, T2> tie(T1& f, T2& s) {
292 return tier<T1, T2>(f, s);
293}
294
Brian Gaeked0fde302003-11-11 22:41:34 +0000295} // End llvm namespace
296
Chris Lattner18d64ed2001-06-21 05:25:51 +0000297#endif