next_prior.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Boost next_prior.hpp header file ---------------------------------------//
  2. // (C) Copyright Dave Abrahams and Daniel Walker 1999-2003. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/utility for documentation.
  6. // Revision History
  7. // 13 Dec 2003 Added next(x, n) and prior(x, n) (Daniel Walker)
  8. #ifndef BOOST_NEXT_PRIOR_HPP_INCLUDED
  9. #define BOOST_NEXT_PRIOR_HPP_INCLUDED
  10. #include <iterator>
  11. namespace boost {
  12. // Helper functions for classes like bidirectional iterators not supporting
  13. // operator+ and operator-
  14. //
  15. // Usage:
  16. // const std::list<T>::iterator p = get_some_iterator();
  17. // const std::list<T>::iterator prev = boost::prior(p);
  18. // const std::list<T>::iterator next = boost::next(prev, 2);
  19. // Contributed by Dave Abrahams
  20. template <class T>
  21. inline T next(T x) { return ++x; }
  22. template <class T, class Distance>
  23. inline T next(T x, Distance n)
  24. {
  25. std::advance(x, n);
  26. return x;
  27. }
  28. template <class T>
  29. inline T prior(T x) { return --x; }
  30. template <class T, class Distance>
  31. inline T prior(T x, Distance n)
  32. {
  33. std::advance(x, -n);
  34. return x;
  35. }
  36. } // namespace boost
  37. #endif // BOOST_NEXT_PRIOR_HPP_INCLUDED