unkeyed_iterator.hpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // unkeyed_iterator.hpp
  3. // iterator
  4. //
  5. // Created by Sam Jaffe on 2/20/17.
  6. //
  7. #pragma once
  8. #include "facade.h"
  9. #include <iterator>
  10. namespace iterator {
  11. /**
  12. * At first glance, this class seems like it acts as an iterator that would be
  13. * used in the C++ equivalent of Java's Map::values() function. However,
  14. * notices that we do not assume that the input iterator is a map iterator.
  15. * The useful feature of this iterator - and the source of its name - is its
  16. * ability to be composed with recursive_iterator. This means that you can
  17. * have something crazy like a four-level map and then do:
  18. * \code
  19. * std::map<int, std::map<std::string, std::map<int, BigType>>> object;
  20. * auto const rit = make_recursive_iterator(object);
  21. * for (unkeyed_iterator<decltype(rit)> it = rit, end = {}; it != end; ++it) {
  22. * // Process only BigType, discarding all of the keys that we need to walk
  23. * }
  24. * \endcode
  25. */
  26. template <typename Iterator>
  27. class unkeyed_iterator : public facade<unkeyed_iterator<Iterator>> {
  28. private:
  29. static constexpr std::size_t const value_index =
  30. std::tuple_size<typename Iterator::value_type>::value - 1;
  31. public:
  32. unkeyed_iterator() = default;
  33. unkeyed_iterator(Iterator it) : base(it) {}
  34. decltype(auto) dereference() const { return std::get<value_index>(*base); }
  35. void increment() { ++base; }
  36. void decrement() { --base; }
  37. bool equal_to(unkeyed_iterator const & other) const {
  38. return base == other.base;
  39. }
  40. private:
  41. Iterator base;
  42. };
  43. }
  44. MAKE_ITERATOR_FACADE_TYPEDEFS_T(::iterator::unkeyed_iterator)