for_each_with_index.hpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Boost.Geometry
  2. // Copyright (c) 2023 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Use, modification and distribution is subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GEOMETRY_UTIL_FOR_EACH_WITH_INDEX_HPP
  7. #define BOOST_GEOMETRY_UTIL_FOR_EACH_WITH_INDEX_HPP
  8. #include <boost/range/begin.hpp>
  9. #include <boost/range/end.hpp>
  10. #include <boost/range/size_type.hpp>
  11. namespace boost { namespace geometry
  12. {
  13. #ifndef DOXYGEN_NO_DETAIL
  14. namespace detail
  15. {
  16. // Utility function to implement a Kotlin like range based for loop
  17. template <typename Container, typename Function>
  18. inline void for_each_with_index(Container const& container, Function func)
  19. {
  20. typename boost::range_size<Container>::type index = 0;
  21. for (auto it = boost::begin(container); it != boost::end(container); ++it, ++index)
  22. {
  23. func(index, *it);
  24. }
  25. }
  26. template <typename Container, typename Function>
  27. inline void for_each_with_index(Container& container, Function func)
  28. {
  29. typename boost::range_size<Container>::type index = 0;
  30. for (auto it = boost::begin(container); it != boost::end(container); ++it, ++index)
  31. {
  32. func(index, *it);
  33. }
  34. }
  35. } // namespace detail
  36. #endif // DOXYGEN_NO_DETAIL
  37. }} // namespace boost::geometry
  38. #endif // BOOST_GEOMETRY_UTIL_FOR_EACH_WITH_INDEX_HPP