Add next() and prior() iterator utility functions. Unlike std::advance
authorAlkis Evlogimenos <alkis@evlogimenos.com>
Sat, 14 Feb 2004 01:17:28 +0000 (01:17 +0000)
committerAlkis Evlogimenos <alkis@evlogimenos.com>
Sat, 14 Feb 2004 01:17:28 +0000 (01:17 +0000)
they do not modify the passed iterator but return a copy.

next(myIt) returns copy of myIt incremented once
next(myIt, n) returns copy of myIt incremented n times
prior(myIt) returns copy of myIt decremented once
prior(myIt, n) returns copy of myIt decremented n times

While at it remove obsolete implementation of mapped_iterator.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@11429 91177308-0d34-0410-b5e6-96231b3b80d8

include/Support/STLExtras.h
include/llvm/ADT/STLExtras.h

index 5ebceb346a082482d70598b49a270ce45bca1d58..1a53768317c3a8a1096d1fa30488b18438d9e16c 100644 (file)
@@ -75,7 +75,6 @@ static inline void deleter(T *Ptr) {
 //
 // It turns out that this is disturbingly similar to boost::transform_iterator
 //
-#if 1
 template <class RootIt, class UnaryFunc>
 class mapped_iterator {
   RootIt current;
@@ -131,28 +130,6 @@ operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
   return mapped_iterator<_Iterator, Func>(X.getCurrent() - N);
 }
 
-#else
-
-// This fails to work, because some iterators are not classes, for example
-// vector iterators are commonly value_type **'s
-template <class RootIt, class UnaryFunc>
-class mapped_iterator : public RootIt {
-  UnaryFunc Fn;
-public:
-  typedef typename UnaryFunc::result_type value_type;
-  typedef typename UnaryFunc::result_type *pointer;
-  typedef void reference;        // Can't modify value returned by fn
-
-  typedef mapped_iterator<RootIt, UnaryFunc> _Self;
-  typedef RootIt super;
-  inline explicit mapped_iterator(const RootIt &I) : super(I) {}
-  inline mapped_iterator(const super &It) : super(It) {}
-
-  inline value_type operator*() const {     // All this work to do 
-    return Fn(super::operator*());   // this little thing
-  }
-};
-#endif
 
 // map_iterator - Provide a convenient way to create mapped_iterators, just like
 // make_pair is useful for creating pairs...
@@ -163,6 +140,43 @@ inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
 }
 
 
+// next/prior - These functions unlike std::advance do not modify the
+// passed iterator but return a copy.
+//
+// next(myIt) returns copy of myIt incremented once
+// next(myIt, n) returns copy of myIt incremented n times
+// prior(myIt) returns copy of myIt decremented once
+// prior(myIt, n) returns copy of myIt decremented n times
+
+template <typename ItTy, typename Dist>
+inline ItTy next(ItTy it, Dist n)
+{
+  std::advance(it, n);
+  return it;
+}
+
+template <typename ItTy>
+inline ItTy next(ItTy it)
+{
+  std::advance(it, 1);
+  return it;
+}
+
+template <typename ItTy, typename Dist>
+inline ItTy prior(ItTy it, Dist n)
+{
+  std::advance(it, -n);
+  return it;
+}
+
+template <typename ItTy>
+inline ItTy prior(ItTy it)
+{
+  std::advance(it, -1);
+  return it;
+}
+
+
 //===----------------------------------------------------------------------===//
 //     Extra additions to <algorithm>
 //===----------------------------------------------------------------------===//
index 5ebceb346a082482d70598b49a270ce45bca1d58..1a53768317c3a8a1096d1fa30488b18438d9e16c 100644 (file)
@@ -75,7 +75,6 @@ static inline void deleter(T *Ptr) {
 //
 // It turns out that this is disturbingly similar to boost::transform_iterator
 //
-#if 1
 template <class RootIt, class UnaryFunc>
 class mapped_iterator {
   RootIt current;
@@ -131,28 +130,6 @@ operator+(typename mapped_iterator<_Iterator, Func>::difference_type N,
   return mapped_iterator<_Iterator, Func>(X.getCurrent() - N);
 }
 
-#else
-
-// This fails to work, because some iterators are not classes, for example
-// vector iterators are commonly value_type **'s
-template <class RootIt, class UnaryFunc>
-class mapped_iterator : public RootIt {
-  UnaryFunc Fn;
-public:
-  typedef typename UnaryFunc::result_type value_type;
-  typedef typename UnaryFunc::result_type *pointer;
-  typedef void reference;        // Can't modify value returned by fn
-
-  typedef mapped_iterator<RootIt, UnaryFunc> _Self;
-  typedef RootIt super;
-  inline explicit mapped_iterator(const RootIt &I) : super(I) {}
-  inline mapped_iterator(const super &It) : super(It) {}
-
-  inline value_type operator*() const {     // All this work to do 
-    return Fn(super::operator*());   // this little thing
-  }
-};
-#endif
 
 // map_iterator - Provide a convenient way to create mapped_iterators, just like
 // make_pair is useful for creating pairs...
@@ -163,6 +140,43 @@ inline mapped_iterator<ItTy, FuncTy> map_iterator(const ItTy &I, FuncTy F) {
 }
 
 
+// next/prior - These functions unlike std::advance do not modify the
+// passed iterator but return a copy.
+//
+// next(myIt) returns copy of myIt incremented once
+// next(myIt, n) returns copy of myIt incremented n times
+// prior(myIt) returns copy of myIt decremented once
+// prior(myIt, n) returns copy of myIt decremented n times
+
+template <typename ItTy, typename Dist>
+inline ItTy next(ItTy it, Dist n)
+{
+  std::advance(it, n);
+  return it;
+}
+
+template <typename ItTy>
+inline ItTy next(ItTy it)
+{
+  std::advance(it, 1);
+  return it;
+}
+
+template <typename ItTy, typename Dist>
+inline ItTy prior(ItTy it, Dist n)
+{
+  std::advance(it, -n);
+  return it;
+}
+
+template <typename ItTy>
+inline ItTy prior(ItTy it)
+{
+  std::advance(it, -1);
+  return it;
+}
+
+
 //===----------------------------------------------------------------------===//
 //     Extra additions to <algorithm>
 //===----------------------------------------------------------------------===//