Changeset View
Changeset View
Standalone View
Standalone View
source/blender/blenlib/BLI_index_range.hh
| Show All 28 Lines | |||||
| * for (int64_t i = 0; i < 10; i++) { | * for (int64_t i = 0; i < 10; i++) { | ||||
| * for (int64_t j = 0; j < 20; j++) { | * for (int64_t j = 0; j < 20; j++) { | ||||
| * for (int64_t k = 0; k < 30; k++) { | * for (int64_t k = 0; k < 30; k++) { | ||||
| * | * | ||||
| * for (int64_t i : IndexRange(10)) { | * for (int64_t i : IndexRange(10)) { | ||||
| * for (int64_t j : IndexRange(20)) { | * for (int64_t j : IndexRange(20)) { | ||||
| * for (int64_t k : IndexRange(30)) { | * for (int64_t k : IndexRange(30)) { | ||||
| * | * | ||||
| * Some containers like blender::Vector have an index_range() method. This will return the | |||||
| * IndexRange that contains all indices that can be used to access the container. This is | |||||
| * particularly useful when you want to iterate over the indices and the elements (much like | |||||
| * Python's enumerate(), just worse). Again, I think the second example here is better: | |||||
| * | |||||
| * for (int64_t i = 0; i < my_vector_with_a_long_name.size(); i++) { | |||||
| * do_something(i, my_vector_with_a_long_name[i]); | |||||
| * | |||||
| * for (int64_t i : my_vector_with_a_long_name.index_range()) { | |||||
| * do_something(i, my_vector_with_a_long_name[i]); | |||||
| * | |||||
| * Ideally this could be could be even closer to Python's enumerate(). We might get that in the | * Ideally this could be could be even closer to Python's enumerate(). We might get that in the | ||||
| * future with newer C++ versions. | * future with newer C++ versions. | ||||
| * | * | ||||
| * One other important feature is the as_span method. This method returns an Span<int64_t> | * One other important feature is the as_span method. This method returns an Span<int64_t> | ||||
| * that contains the interval as individual numbers. | * that contains the interval as individual numbers. | ||||
| */ | */ | ||||
| #include <algorithm> | #include <algorithm> | ||||
| ▲ Show 20 Lines • Show All 191 Lines • ▼ Show 20 Lines | public: | ||||
| friend std::ostream &operator<<(std::ostream &stream, IndexRange range) | friend std::ostream &operator<<(std::ostream &stream, IndexRange range) | ||||
| { | { | ||||
| stream << "[" << range.start() << ", " << range.one_after_last() << ")"; | stream << "[" << range.start() << ", " << range.one_after_last() << ")"; | ||||
| return stream; | return stream; | ||||
| } | } | ||||
| }; | }; | ||||
| template<typename Container> inline IndexRange iter_indices(const Container &container) | |||||
| { | |||||
| const int64_t size = (int64_t)container.size(); | |||||
| return IndexRange(size); | |||||
| } | |||||
| } // namespace blender | } // namespace blender | ||||