values_iterator.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //
  2. // unkeyed_iterator.h
  3. // iterator
  4. //
  5. // Created by Sam Jaffe on 2/20/17.
  6. //
  7. #pragma once
  8. #include <iterator/forwards.h>
  9. #include <iterator/proxy.h>
  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 = RecursiveIterator(object);
  21. * for (ValuesIterator<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 ValuesIterator : public Proxy<Iterator, ValuesIterator<Iterator>> {
  28. public:
  29. using Proxy<Iterator, ValuesIterator<Iterator>>::Proxy;
  30. decltype(auto) dereference() const {
  31. using value_type = typename Iterator::value_type;
  32. constexpr auto index = std::tuple_size<value_type>::value - 1;
  33. return std::get<index>(*this->impl());
  34. }
  35. };
  36. template <typename It> ValuesIterator(It) -> ValuesIterator<It>;
  37. }