[PATCH] D33651: [ADT] Add convenience functions for making forward and reverse ranges with an STL container.

Frederich Munch via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon May 29 10:57:49 PDT 2017


marsupial created this revision.

https://reviews.llvm.org/D33651

Files:
  include/llvm/ADT/iterator_range.h


Index: include/llvm/ADT/iterator_range.h
===================================================================
--- include/llvm/ADT/iterator_range.h
+++ include/llvm/ADT/iterator_range.h
@@ -63,6 +63,29 @@
 iterator_range<decltype(begin(std::declval<T>()))> drop_begin(T &&t, int n) {
   return make_range(std::next(begin(t), n), end(t));
 }
+
+/// \brief Convenience function for iterating over stl-like containers.
+///
+/// This provides a bit of syntactic sugar to make using ranges in loops a bit
+/// easier.
+template <typename T> iterator_range<typename T::iterator> make_range(T &C) {
+  return iterator_range<typename T::iterator>(C.begin(), C.end());
+}
+
+template <typename T>
+iterator_range<typename T::reverse_iterator> reverse_range(T &C) {
+  return iterator_range<typename T::reverse_iterator>(C.rbegin(), C.rend());
+}
+
+template <typename T>
+iterator_range<typename T::const_iterator> make_range(const T &C) {
+  return iterator_range<typename T::iterator>(C.begin(), C.end());
+}
+
+template <typename T>
+iterator_range<typename T::const_reverse_iterator> reverse_range(const T &C) {
+  return iterator_range<typename T::reverse_iterator>(C.rbegin(), C.rend());
+}
 }
 
 #endif


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33651.100625.patch
Type: text/x-patch
Size: 1201 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170529/ef0a090e/attachment.bin>


More information about the llvm-commits mailing list