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 "llvm/Support/Compiler.h"
21 #include <cstddef> // for std::size_t
22 #include <cstdlib> // for qsort
26 #include <utility> // for std::pair
30 //===----------------------------------------------------------------------===//
31 // Extra additions to <functional>
32 //===----------------------------------------------------------------------===//
35 struct identity : public std::unary_function<Ty, Ty> {
36 Ty &operator()(Ty &self) const {
39 const Ty &operator()(const Ty &self) const {
45 struct less_ptr : public std::binary_function<Ty, Ty, bool> {
46 bool operator()(const Ty* left, const Ty* right) const {
47 return *left < *right;
52 struct greater_ptr : public std::binary_function<Ty, Ty, bool> {
53 bool operator()(const Ty* left, const Ty* right) const {
54 return *right < *left;
58 // deleter - Very very very simple method that is used to invoke operator
59 // delete on something. It is used like this:
61 // for_each(V.begin(), B.end(), deleter<Interval>);
64 inline void deleter(T *Ptr) {
70 //===----------------------------------------------------------------------===//
71 // Extra additions to <iterator>
72 //===----------------------------------------------------------------------===//
74 // mapped_iterator - This is a simple iterator adapter that causes a function to
75 // be dereferenced whenever operator* is invoked on the iterator.
77 template <class RootIt, class UnaryFunc>
78 class mapped_iterator {
82 typedef typename std::iterator_traits<RootIt>::iterator_category
84 typedef typename std::iterator_traits<RootIt>::difference_type
86 typedef typename UnaryFunc::result_type value_type;
89 //typedef typename UnaryFunc::result_type *pointer;
90 typedef void reference; // Can't modify value returned by fn
92 typedef RootIt iterator_type;
93 typedef mapped_iterator<RootIt, UnaryFunc> _Self;
95 inline const RootIt &getCurrent() const { return current; }
96 inline const UnaryFunc &getFunc() const { return Fn; }
98 inline explicit mapped_iterator(const RootIt &I, UnaryFunc F)
99 : current(I), Fn(F) {}
100 inline mapped_iterator(const mapped_iterator &It)
101 : current(It.current), Fn(It.Fn) {}
103 inline value_type operator*() const { // All this work to do this
104 return Fn(*current); // little change
107 _Self& operator++() { ++current; return *this; }
108 _Self& operator--() { --current; return *this; }
109 _Self operator++(int) { _Self __tmp = *this; ++current; return __tmp; }
110 _Self operator--(int) { _Self __tmp = *this; --current; return __tmp; }
111 _Self operator+ (difference_type n) const {
112 return _Self(current + n, Fn);
114 _Self& operator+= (difference_type n) { current += n; return *this; }
115 _Self operator- (difference_type n) const {
116 return _Self(current - n, Fn);
118 _Self& operator-= (difference_type n) { current -= n; return *this; }
119 reference operator[](difference_type n) const { return *(*this + n); }
121 inline bool operator!=(const _Self &X) const { return !operator==(X); }
122 inline bool operator==(const _Self &X) const { return current == X.current; }
123 inline bool operator< (const _Self &X) const { return current < X.current; }
125 inline difference_type operator-(const _Self &X) const {
126 return current - X.current;
130 template <class _Iterator, class Func>
131 inline mapped_iterator<_Iterator, Func>
132 operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
133 const mapped_iterator<_Iterator, Func>& X) {
134 return mapped_iterator<_Iterator, Func>(X.getCurrent() - N, X.getFunc());
138 // map_iterator - Provide a convenient way to create mapped_iterators, just like
139 // make_pair is useful for creating pairs...
141 template <class ItTy, class FuncTy>
142 inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
143 return mapped_iterator<ItTy, FuncTy>(I, F);
146 //===----------------------------------------------------------------------===//
147 // Extra additions to <utility>
148 //===----------------------------------------------------------------------===//
150 /// \brief Function object to check whether the first component of a std::pair
151 /// compares less than the first component of another std::pair.
153 template <typename T> bool operator()(const T &lhs, const T &rhs) const {
154 return lhs.first < rhs.first;
158 /// \brief Function object to check whether the second component of a std::pair
159 /// compares less than the second component of another std::pair.
161 template <typename T> bool operator()(const T &lhs, const T &rhs) const {
162 return lhs.second < rhs.second;
166 //===----------------------------------------------------------------------===//
167 // Extra additions for arrays
168 //===----------------------------------------------------------------------===//
170 /// Find where an array ends (for ending iterators)
171 /// This returns a pointer to the byte immediately
172 /// after the end of an array.
173 template<class T, std::size_t N>
174 inline T *array_endof(T (&x)[N]) {
178 /// Find the length of an array.
179 template<class T, std::size_t N>
180 inline size_t array_lengthof(T (&)[N]) {
184 /// array_pod_sort_comparator - This is helper function for array_pod_sort,
185 /// which just uses operator< on T.
187 inline int array_pod_sort_comparator(const void *P1, const void *P2) {
188 if (*reinterpret_cast<const T*>(P1) < *reinterpret_cast<const T*>(P2))
190 if (*reinterpret_cast<const T*>(P2) < *reinterpret_cast<const T*>(P1))
195 /// get_array_pod_sort_comparator - This is an internal helper function used to
196 /// get type deduction of T right.
198 inline int (*get_array_pod_sort_comparator(const T &))
199 (const void*, const void*) {
200 return array_pod_sort_comparator<T>;
204 /// array_pod_sort - This sorts an array with the specified start and end
205 /// extent. This is just like std::sort, except that it calls qsort instead of
206 /// using an inlined template. qsort is slightly slower than std::sort, but
207 /// most sorts are not performance critical in LLVM and std::sort has to be
208 /// template instantiated for each type, leading to significant measured code
209 /// bloat. This function should generally be used instead of std::sort where
212 /// This function assumes that you have simple POD-like types that can be
213 /// compared with operator< and can be moved with memcpy. If this isn't true,
214 /// you should use std::sort.
216 /// NOTE: If qsort_r were portable, we could allow a custom comparator and
217 /// default to std::less.
218 template<class IteratorTy>
219 inline void array_pod_sort(IteratorTy Start, IteratorTy End) {
220 // Don't dereference start iterator of empty sequence.
221 if (Start == End) return;
222 qsort(&*Start, End-Start, sizeof(*Start),
223 get_array_pod_sort_comparator(*Start));
226 template <class IteratorTy>
227 inline void array_pod_sort(
228 IteratorTy Start, IteratorTy End,
230 const typename std::iterator_traits<IteratorTy>::value_type *,
231 const typename std::iterator_traits<IteratorTy>::value_type *)) {
232 // Don't dereference start iterator of empty sequence.
233 if (Start == End) return;
234 qsort(&*Start, End - Start, sizeof(*Start),
235 reinterpret_cast<int (*)(const void *, const void *)>(Compare));
238 //===----------------------------------------------------------------------===//
239 // Extra additions to <algorithm>
240 //===----------------------------------------------------------------------===//
242 /// For a container of pointers, deletes the pointers and then clears the
244 template<typename Container>
245 void DeleteContainerPointers(Container &C) {
246 for (typename Container::iterator I = C.begin(), E = C.end(); I != E; ++I)
251 /// In a container of pairs (usually a map) whose second element is a pointer,
252 /// deletes the second elements and then clears the container.
253 template<typename Container>
254 void DeleteContainerSeconds(Container &C) {
255 for (typename Container::iterator I = C.begin(), E = C.end(); I != E; ++I)
260 //===----------------------------------------------------------------------===//
261 // Extra additions to <memory>
262 //===----------------------------------------------------------------------===//
264 #if LLVM_HAS_VARIADIC_TEMPLATES
266 // Implement make_unique according to N3656.
268 /// \brief Constructs a `new T()` with the given args and returns a
269 /// `unique_ptr<T>` which owns the object.
273 /// auto p = make_unique<int>();
274 /// auto p = make_unique<std::tuple<int, int>>(0, 1);
275 template <class T, class... Args>
276 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
277 make_unique(Args &&... args) {
278 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
281 /// \brief Constructs a `new T[n]` with the given args and returns a
282 /// `unique_ptr<T[]>` which owns the object.
284 /// \param n size of the new array.
288 /// auto p = make_unique<int[]>(2); // value-initializes the array with 0's.
290 typename std::enable_if<std::is_array<T>::value && std::extent<T>::value == 0,
291 std::unique_ptr<T>>::type
292 make_unique(size_t n) {
293 return std::unique_ptr<T>(new typename std::remove_extent<T>::type[n]());
296 /// This function isn't used and is only here to provide better compile errors.
297 template <class T, class... Args>
298 typename std::enable_if<std::extent<T>::value != 0>::type
299 make_unique(Args &&...) LLVM_DELETED_FUNCTION;
304 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
306 return std::unique_ptr<T>(new T());
309 template <class T, class Arg1>
310 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
311 make_unique(Arg1 &&arg1) {
312 return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1)));
315 template <class T, class Arg1, class Arg2>
316 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
317 make_unique(Arg1 &&arg1, Arg2 &&arg2) {
318 return std::unique_ptr<T>(
319 new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2)));
322 template <class T, class Arg1, class Arg2, class Arg3>
323 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
324 make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3) {
325 return std::unique_ptr<T>(new T(std::forward<Arg1>(arg1),
326 std::forward<Arg2>(arg2),
327 std::forward<Arg3>(arg3)));
330 template <class T, class Arg1, class Arg2, class Arg3, class Arg4>
331 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
332 make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3, Arg4 &&arg4) {
333 return std::unique_ptr<T>(
334 new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
335 std::forward<Arg3>(arg3), std::forward<Arg4>(arg4)));
338 template <class T, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
339 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
340 make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3, Arg4 &&arg4, Arg5 &&arg5) {
341 return std::unique_ptr<T>(
342 new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
343 std::forward<Arg3>(arg3), std::forward<Arg4>(arg4),
344 std::forward<Arg5>(arg5)));
347 template <class T, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5,
349 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
350 make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3, Arg4 &&arg4, Arg5 &&arg5,
352 return std::unique_ptr<T>(
353 new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
354 std::forward<Arg3>(arg3), std::forward<Arg4>(arg4),
355 std::forward<Arg5>(arg5), std::forward<Arg6>(arg6)));
358 template <class T, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5,
359 class Arg6, class Arg7>
360 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
361 make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3, Arg4 &&arg4, Arg5 &&arg5,
362 Arg6 &&arg6, Arg7 &&arg7) {
363 return std::unique_ptr<T>(
364 new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
365 std::forward<Arg3>(arg3), std::forward<Arg4>(arg4),
366 std::forward<Arg5>(arg5), std::forward<Arg6>(arg6),
367 std::forward<Arg7>(arg7)));
370 template <class T, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5,
371 class Arg6, class Arg7, class Arg8>
372 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
373 make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3, Arg4 &&arg4, Arg5 &&arg5,
374 Arg6 &&arg6, Arg7 &&arg7, Arg8 &&arg8) {
375 return std::unique_ptr<T>(
376 new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
377 std::forward<Arg3>(arg3), std::forward<Arg4>(arg4),
378 std::forward<Arg5>(arg5), std::forward<Arg6>(arg6),
379 std::forward<Arg7>(arg7), std::forward<Arg8>(arg8)));
382 template <class T, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5,
383 class Arg6, class Arg7, class Arg8, class Arg9>
384 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
385 make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3, Arg4 &&arg4, Arg5 &&arg5,
386 Arg6 &&arg6, Arg7 &&arg7, Arg8 &&arg8, Arg9 &&arg9) {
387 return std::unique_ptr<T>(
388 new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
389 std::forward<Arg3>(arg3), std::forward<Arg4>(arg4),
390 std::forward<Arg5>(arg5), std::forward<Arg6>(arg6),
391 std::forward<Arg7>(arg7), std::forward<Arg8>(arg8),
392 std::forward<Arg9>(arg9)));
395 template <class T, class Arg1, class Arg2, class Arg3, class Arg4, class Arg5,
396 class Arg6, class Arg7, class Arg8, class Arg9, class Arg10>
397 typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
398 make_unique(Arg1 &&arg1, Arg2 &&arg2, Arg3 &&arg3, Arg4 &&arg4, Arg5 &&arg5,
399 Arg6 &&arg6, Arg7 &&arg7, Arg8 &&arg8, Arg9 &&arg9, Arg10 &&arg10) {
400 return std::unique_ptr<T>(
401 new T(std::forward<Arg1>(arg1), std::forward<Arg2>(arg2),
402 std::forward<Arg3>(arg3), std::forward<Arg4>(arg4),
403 std::forward<Arg5>(arg5), std::forward<Arg6>(arg6),
404 std::forward<Arg7>(arg7), std::forward<Arg8>(arg8),
405 std::forward<Arg9>(arg9), std::forward<Arg10>(arg10)));
409 typename std::enable_if<std::is_array<T>::value &&std::extent<T>::value == 0,
410 std::unique_ptr<T>>::type
411 make_unique(size_t n) {
412 return std::unique_ptr<T>(new typename std::remove_extent<T>::type[n]());
417 } // End llvm namespace