~ubuntu-branches/ubuntu/quantal/llvm-3.1/quantal

« back to all changes in this revision

Viewing changes to include/llvm/ADT/STLExtras.h

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2012-03-29 19:09:51 UTC
  • Revision ID: package-import@ubuntu.com-20120329190951-aq83ivog4cg8bxun
Tags: upstream-3.1~svn153643
ImportĀ upstreamĀ versionĀ 3.1~svn153643

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//===- llvm/ADT/STLExtras.h - Useful STL related functions ------*- C++ -*-===//
 
2
//
 
3
//                     The LLVM Compiler Infrastructure
 
4
//
 
5
// This file is distributed under the University of Illinois Open Source
 
6
// 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 functions.
 
14
//
 
15
//===----------------------------------------------------------------------===//
 
16
 
 
17
#ifndef LLVM_ADT_STLEXTRAS_H
 
18
#define LLVM_ADT_STLEXTRAS_H
 
19
 
 
20
#include <cstddef> // for std::size_t
 
21
#include <cstdlib> // for qsort
 
22
#include <functional>
 
23
#include <iterator>
 
24
#include <utility> // for std::pair
 
25
 
 
26
namespace llvm {
 
27
 
 
28
//===----------------------------------------------------------------------===//
 
29
//     Extra additions to <functional>
 
30
//===----------------------------------------------------------------------===//
 
31
 
 
32
template<class Ty>
 
33
struct less_ptr : public std::binary_function<Ty, Ty, bool> {
 
34
  bool operator()(const Ty* left, const Ty* right) const {
 
35
    return *left < *right;
 
36
  }
 
37
};
 
38
 
 
39
template<class Ty>
 
40
struct greater_ptr : public std::binary_function<Ty, Ty, bool> {
 
41
  bool operator()(const Ty* left, const Ty* right) const {
 
42
    return *right < *left;
 
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
template <class RootIt, class UnaryFunc>
 
66
class mapped_iterator {
 
67
  RootIt current;
 
68
  UnaryFunc Fn;
 
69
public:
 
70
  typedef typename std::iterator_traits<RootIt>::iterator_category
 
71
          iterator_category;
 
72
  typedef typename std::iterator_traits<RootIt>::difference_type
 
73
          difference_type;
 
74
  typedef typename UnaryFunc::result_type value_type;
 
75
 
 
76
  typedef void pointer;
 
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 const RootIt &getCurrent() const { return current; }
 
84
  inline const UnaryFunc &getFunc() const { return Fn; }
 
85
 
 
86
  inline explicit mapped_iterator(const RootIt &I, UnaryFunc F)
 
87
    : current(I), Fn(F) {}
 
88
  inline mapped_iterator(const mapped_iterator &It)
 
89
    : current(It.current), Fn(It.Fn) {}
 
90
 
 
91
  inline value_type operator*() const {   // All this work to do this
 
92
    return Fn(*current);         // little change
 
93
  }
 
94
 
 
95
  _Self& operator++() { ++current; return *this; }
 
96
  _Self& operator--() { --current; return *this; }
 
97
  _Self  operator++(int) { _Self __tmp = *this; ++current; return __tmp; }
 
98
  _Self  operator--(int) { _Self __tmp = *this; --current; return __tmp; }
 
99
  _Self  operator+    (difference_type n) const {
 
100
    return _Self(current + n, Fn);
 
101
  }
 
102
  _Self& operator+=   (difference_type n) { current += n; return *this; }
 
103
  _Self  operator-    (difference_type n) const {
 
104
    return _Self(current - n, Fn);
 
105
  }
 
106
  _Self& operator-=   (difference_type n) { current -= n; return *this; }
 
107
  reference operator[](difference_type n) const { return *(*this + n); }
 
108
 
 
109
  inline bool operator!=(const _Self &X) const { return !operator==(X); }
 
110
  inline bool operator==(const _Self &X) const { return current == X.current; }
 
111
  inline bool operator< (const _Self &X) const { return current <  X.current; }
 
112
 
 
113
  inline difference_type operator-(const _Self &X) const {
 
114
    return current - X.current;
 
115
  }
 
116
};
 
117
 
 
118
template <class _Iterator, class Func>
 
119
inline mapped_iterator<_Iterator, Func>
 
120
operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
 
121
          const mapped_iterator<_Iterator, Func>& X) {
 
122
  return mapped_iterator<_Iterator, Func>(X.getCurrent() - N, X.getFunc());
 
123
}
 
124
 
 
125
 
 
126
// map_iterator - Provide a convenient way to create mapped_iterators, just like
 
127
// make_pair is useful for creating pairs...
 
128
//
 
129
template <class ItTy, class FuncTy>
 
130
inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
 
131
  return mapped_iterator<ItTy, FuncTy>(I, F);
 
132
}
 
133
 
 
134
 
 
135
// next/prior - These functions unlike std::advance do not modify the
 
136
// passed iterator but return a copy.
 
137
//
 
138
// next(myIt) returns copy of myIt incremented once
 
139
// next(myIt, n) returns copy of myIt incremented n times
 
140
// prior(myIt) returns copy of myIt decremented once
 
141
// prior(myIt, n) returns copy of myIt decremented n times
 
142
 
 
143
template <typename ItTy, typename Dist>
 
144
inline ItTy next(ItTy it, Dist n)
 
145
{
 
146
  std::advance(it, n);
 
147
  return it;
 
148
}
 
149
 
 
150
template <typename ItTy>
 
151
inline ItTy next(ItTy it)
 
152
{
 
153
  return ++it;
 
154
}
 
155
 
 
156
template <typename ItTy, typename Dist>
 
157
inline ItTy prior(ItTy it, Dist n)
 
158
{
 
159
  std::advance(it, -n);
 
160
  return it;
 
161
}
 
162
 
 
163
template <typename ItTy>
 
164
inline ItTy prior(ItTy it)
 
165
{
 
166
  return --it;
 
167
}
 
168
 
 
169
//===----------------------------------------------------------------------===//
 
170
//     Extra additions to <utility>
 
171
//===----------------------------------------------------------------------===//
 
172
 
 
173
// tie - this function ties two objects and returns a temporary object
 
174
// that is assignable from a std::pair. This can be used to make code
 
175
// more readable when using values returned from functions bundled in
 
176
// a std::pair. Since an example is worth 1000 words:
 
177
//
 
178
// typedef std::map<int, int> Int2IntMap;
 
179
//
 
180
// Int2IntMap myMap;
 
181
// Int2IntMap::iterator where;
 
182
// bool inserted;
 
183
// tie(where, inserted) = myMap.insert(std::make_pair(123,456));
 
184
//
 
185
// if (inserted)
 
186
//   // do stuff
 
187
// else
 
188
//   // do other stuff
 
189
template <typename T1, typename T2>
 
190
struct tier {
 
191
  typedef T1 &first_type;
 
192
  typedef T2 &second_type;
 
193
 
 
194
  first_type first;
 
195
  second_type second;
 
196
 
 
197
  tier(first_type f, second_type s) : first(f), second(s) { }
 
198
  tier& operator=(const std::pair<T1, T2>& p) {
 
199
    first = p.first;
 
200
    second = p.second;
 
201
    return *this;
 
202
  }
 
203
};
 
204
 
 
205
template <typename T1, typename T2>
 
206
inline tier<T1, T2> tie(T1& f, T2& s) {
 
207
  return tier<T1, T2>(f, s);
 
208
}
 
209
 
 
210
//===----------------------------------------------------------------------===//
 
211
//     Extra additions for arrays
 
212
//===----------------------------------------------------------------------===//
 
213
 
 
214
/// Find where an array ends (for ending iterators)
 
215
/// This returns a pointer to the byte immediately
 
216
/// after the end of an array.
 
217
template<class T, std::size_t N>
 
218
inline T *array_endof(T (&x)[N]) {
 
219
  return x+N;
 
220
}
 
221
 
 
222
/// Find the length of an array.
 
223
template<class T, std::size_t N>
 
224
inline size_t array_lengthof(T (&)[N]) {
 
225
  return N;
 
226
}
 
227
 
 
228
/// array_pod_sort_comparator - This is helper function for array_pod_sort,
 
229
/// which just uses operator< on T.
 
230
template<typename T>
 
231
static inline int array_pod_sort_comparator(const void *P1, const void *P2) {
 
232
  if (*reinterpret_cast<const T*>(P1) < *reinterpret_cast<const T*>(P2))
 
233
    return -1;
 
234
  if (*reinterpret_cast<const T*>(P2) < *reinterpret_cast<const T*>(P1))
 
235
    return 1;
 
236
  return 0;
 
237
}
 
238
 
 
239
/// get_array_pad_sort_comparator - This is an internal helper function used to
 
240
/// get type deduction of T right.
 
241
template<typename T>
 
242
static int (*get_array_pad_sort_comparator(const T &))
 
243
             (const void*, const void*) {
 
244
  return array_pod_sort_comparator<T>;
 
245
}
 
246
 
 
247
 
 
248
/// array_pod_sort - This sorts an array with the specified start and end
 
249
/// extent.  This is just like std::sort, except that it calls qsort instead of
 
250
/// using an inlined template.  qsort is slightly slower than std::sort, but
 
251
/// most sorts are not performance critical in LLVM and std::sort has to be
 
252
/// template instantiated for each type, leading to significant measured code
 
253
/// bloat.  This function should generally be used instead of std::sort where
 
254
/// possible.
 
255
///
 
256
/// This function assumes that you have simple POD-like types that can be
 
257
/// compared with operator< and can be moved with memcpy.  If this isn't true,
 
258
/// you should use std::sort.
 
259
///
 
260
/// NOTE: If qsort_r were portable, we could allow a custom comparator and
 
261
/// default to std::less.
 
262
template<class IteratorTy>
 
263
static inline void array_pod_sort(IteratorTy Start, IteratorTy End) {
 
264
  // Don't dereference start iterator of empty sequence.
 
265
  if (Start == End) return;
 
266
  qsort(&*Start, End-Start, sizeof(*Start),
 
267
        get_array_pad_sort_comparator(*Start));
 
268
}
 
269
 
 
270
template<class IteratorTy>
 
271
static inline void array_pod_sort(IteratorTy Start, IteratorTy End,
 
272
                                  int (*Compare)(const void*, const void*)) {
 
273
  // Don't dereference start iterator of empty sequence.
 
274
  if (Start == End) return;
 
275
  qsort(&*Start, End-Start, sizeof(*Start), Compare);
 
276
}
 
277
  
 
278
//===----------------------------------------------------------------------===//
 
279
//     Extra additions to <algorithm>
 
280
//===----------------------------------------------------------------------===//
 
281
 
 
282
/// For a container of pointers, deletes the pointers and then clears the
 
283
/// container.
 
284
template<typename Container>
 
285
void DeleteContainerPointers(Container &C) {
 
286
  for (typename Container::iterator I = C.begin(), E = C.end(); I != E; ++I)
 
287
    delete *I;
 
288
  C.clear();
 
289
}
 
290
 
 
291
/// In a container of pairs (usually a map) whose second element is a pointer,
 
292
/// deletes the second elements and then clears the container.
 
293
template<typename Container>
 
294
void DeleteContainerSeconds(Container &C) {
 
295
  for (typename Container::iterator I = C.begin(), E = C.end(); I != E; ++I)
 
296
    delete I->second;
 
297
  C.clear();
 
298
}
 
299
 
 
300
} // End llvm namespace
 
301
 
 
302
#endif