1 //===- llvm/ADT/STLExtras.h - Useful STL related functions ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains some templates that are useful if you are working with the
13 // No library is required when using these functions.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_ADT_STLEXTRAS_H
18 #define LLVM_ADT_STLEXTRAS_H
20 #include <cstddef> // for std::size_t
21 #include <cstdlib> // for qsort
24 #include <utility> // for std::pair
28 //===----------------------------------------------------------------------===//
29 // Extra additions to <functional>
30 //===----------------------------------------------------------------------===//
33 struct identity : public std::unary_function<Ty, Ty> {
34 Ty &operator()(Ty &self) const {
37 const Ty &operator()(const Ty &self) const {
43 struct less_ptr : public std::binary_function<Ty, Ty, bool> {
44 bool operator()(const Ty* left, const Ty* right) const {
45 return *left < *right;
50 struct greater_ptr : public std::binary_function<Ty, Ty, bool> {
51 bool operator()(const Ty* left, const Ty* right) const {
52 return *right < *left;
56 // deleter - Very very very simple method that is used to invoke operator
57 // delete on something. It is used like this:
59 // for_each(V.begin(), B.end(), deleter<Interval>);
62 inline void deleter(T *Ptr) {
68 //===----------------------------------------------------------------------===//
69 // Extra additions to <iterator>
70 //===----------------------------------------------------------------------===//
72 // mapped_iterator - This is a simple iterator adapter that causes a function to
73 // be dereferenced whenever operator* is invoked on the iterator.
75 template <class RootIt, class UnaryFunc>
76 class mapped_iterator {
80 typedef typename std::iterator_traits<RootIt>::iterator_category
82 typedef typename std::iterator_traits<RootIt>::difference_type
84 typedef typename UnaryFunc::result_type value_type;
87 //typedef typename UnaryFunc::result_type *pointer;
88 typedef void reference; // Can't modify value returned by fn
90 typedef RootIt iterator_type;
91 typedef mapped_iterator<RootIt, UnaryFunc> _Self;
93 inline const RootIt &getCurrent() const { return current; }
94 inline const UnaryFunc &getFunc() const { return Fn; }
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) {}
101 inline value_type operator*() const { // All this work to do this
102 return Fn(*current); // little change
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 {
110 return _Self(current + n, Fn);
112 _Self& operator+= (difference_type n) { current += n; return *this; }
113 _Self operator- (difference_type n) const {
114 return _Self(current - n, Fn);
116 _Self& operator-= (difference_type n) { current -= n; return *this; }
117 reference operator[](difference_type n) const { return *(*this + n); }
119 inline bool operator!=(const _Self &X) const { return !operator==(X); }
120 inline bool operator==(const _Self &X) const { return current == X.current; }
121 inline bool operator< (const _Self &X) const { return current < X.current; }
123 inline difference_type operator-(const _Self &X) const {
124 return current - X.current;
128 template <class _Iterator, class Func>
129 inline mapped_iterator<_Iterator, Func>
130 operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
131 const mapped_iterator<_Iterator, Func>& X) {
132 return mapped_iterator<_Iterator, Func>(X.getCurrent() - N, X.getFunc());
136 // map_iterator - Provide a convenient way to create mapped_iterators, just like
137 // make_pair is useful for creating pairs...
139 template <class ItTy, class FuncTy>
140 inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
141 return mapped_iterator<ItTy, FuncTy>(I, F);
144 //===----------------------------------------------------------------------===//
145 // Extra additions to <utility>
146 //===----------------------------------------------------------------------===//
148 /// \brief Function object to check whether the first component of a std::pair
149 /// compares less than the first component of another std::pair.
151 template <typename T> bool operator()(const T &lhs, const T &rhs) const {
152 return lhs.first < rhs.first;
156 /// \brief Function object to check whether the second component of a std::pair
157 /// compares less than the second component of another std::pair.
159 template <typename T> bool operator()(const T &lhs, const T &rhs) const {
160 return lhs.second < rhs.second;
164 //===----------------------------------------------------------------------===//
165 // Extra additions for arrays
166 //===----------------------------------------------------------------------===//
168 /// Find where an array ends (for ending iterators)
169 /// This returns a pointer to the byte immediately
170 /// after the end of an array.
171 template<class T, std::size_t N>
172 inline T *array_endof(T (&x)[N]) {
176 /// Find the length of an array.
177 template<class T, std::size_t N>
178 inline size_t array_lengthof(T (&)[N]) {
182 /// array_pod_sort_comparator - This is helper function for array_pod_sort,
183 /// which just uses operator< on T.
185 inline int array_pod_sort_comparator(const void *P1, const void *P2) {
186 if (*reinterpret_cast<const T*>(P1) < *reinterpret_cast<const T*>(P2))
188 if (*reinterpret_cast<const T*>(P2) < *reinterpret_cast<const T*>(P1))
193 /// get_array_pod_sort_comparator - This is an internal helper function used to
194 /// get type deduction of T right.
196 inline int (*get_array_pod_sort_comparator(const T &))
197 (const void*, const void*) {
198 return array_pod_sort_comparator<T>;
202 /// array_pod_sort - This sorts an array with the specified start and end
203 /// extent. This is just like std::sort, except that it calls qsort instead of
204 /// using an inlined template. qsort is slightly slower than std::sort, but
205 /// most sorts are not performance critical in LLVM and std::sort has to be
206 /// template instantiated for each type, leading to significant measured code
207 /// bloat. This function should generally be used instead of std::sort where
210 /// This function assumes that you have simple POD-like types that can be
211 /// compared with operator< and can be moved with memcpy. If this isn't true,
212 /// you should use std::sort.
214 /// NOTE: If qsort_r were portable, we could allow a custom comparator and
215 /// default to std::less.
216 template<class IteratorTy>
217 inline void array_pod_sort(IteratorTy Start, IteratorTy End) {
218 // Don't dereference start iterator of empty sequence.
219 if (Start == End) return;
220 qsort(&*Start, End-Start, sizeof(*Start),
221 get_array_pod_sort_comparator(*Start));
224 template <class IteratorTy>
225 inline void array_pod_sort(
226 IteratorTy Start, IteratorTy End,
228 const typename std::iterator_traits<IteratorTy>::value_type *,
229 const typename std::iterator_traits<IteratorTy>::value_type *)) {
230 // Don't dereference start iterator of empty sequence.
231 if (Start == End) return;
232 qsort(&*Start, End - Start, sizeof(*Start),
233 reinterpret_cast<int (*)(const void *, const void *)>(Compare));
236 //===----------------------------------------------------------------------===//
237 // Extra additions to <algorithm>
238 //===----------------------------------------------------------------------===//
240 /// For a container of pointers, deletes the pointers and then clears the
242 template<typename Container>
243 void DeleteContainerPointers(Container &C) {
244 for (typename Container::iterator I = C.begin(), E = C.end(); I != E; ++I)
249 /// In a container of pairs (usually a map) whose second element is a pointer,
250 /// deletes the second elements and then clears the container.
251 template<typename Container>
252 void DeleteContainerSeconds(Container &C) {
253 for (typename Container::iterator I = C.begin(), E = C.end(); I != E; ++I)
258 } // End llvm namespace