RangeIteration.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // David Eberly, Geometric Tools, Redmond WA 98052
  2. // Copyright (c) 1998-2020
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // https://www.boost.org/LICENSE_1_0.txt
  5. // https://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  6. // Version: 4.0.2019.08.13
  7. #pragma once
  8. #include <iterator>
  9. #include <type_traits>
  10. // For information on range-based for-loops, see
  11. // http://en.cppreference.com/w/cpp/language/range-for
  12. namespace WwiseGTE
  13. {
  14. // The function WwiseGTE::reverse supports reverse iteration in range-based
  15. // for-loops using the auto keyword. For example,
  16. //
  17. // std::vector<int> numbers(4);
  18. // int i = 0;
  19. // for (auto& number : numbers)
  20. // {
  21. // number = i++;
  22. // std::cout << number << ' ';
  23. // }
  24. // // Output: 0 1 2 3
  25. //
  26. // for (auto& number : WwiseGTE::reverse(numbers))
  27. // {
  28. // std::cout << number << ' ';
  29. // }
  30. // // Output: 3 2 1 0
  31. template <typename Iterator>
  32. class ReversalObject
  33. {
  34. public:
  35. ReversalObject(Iterator begin, Iterator end)
  36. :
  37. mBegin(begin),
  38. mEnd(end)
  39. {
  40. }
  41. Iterator begin() const { return mBegin; }
  42. Iterator end() const { return mEnd; }
  43. private:
  44. Iterator mBegin, mEnd;
  45. };
  46. template
  47. <
  48. typename Iterable,
  49. typename Iterator = decltype(std::begin(std::declval<Iterable>())),
  50. typename ReverseIterator = std::reverse_iterator<Iterator>
  51. >
  52. ReversalObject<ReverseIterator> reverse(Iterable && range)
  53. {
  54. return ReversalObject<ReverseIterator>(
  55. ReverseIterator(std::end(range)),
  56. ReverseIterator(std::begin(range)));
  57. }
  58. }