├── README.md ├── LICENSE └── enumerate.hpp /README.md: -------------------------------------------------------------------------------- 1 | # enumerate 2 | Range-based for loop with indices retained 3 | 4 | Purpose and usage: https://blog.therocode.net/2018/10/for-each-with-index 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Tobias Widlund 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /enumerate.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * MIT License 3 | * 4 | * Copyright (c) 2018 Tobias Widlund 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in all 14 | * copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | * SOFTWARE. 23 | */ 24 | 25 | #pragma once 26 | #include 27 | #include 28 | #include 29 | 30 | namespace en 31 | { 32 | template 33 | struct enumerate_wrapper 34 | { 35 | using iterator_type = std::conditional_t, typename container_type::const_iterator, typename container_type::iterator>; 36 | using pointer_type = std::conditional_t, typename container_type::const_pointer, typename container_type::pointer>; 37 | using reference_type = std::conditional_t, typename container_type::const_reference, typename container_type::reference>; 38 | 39 | constexpr enumerate_wrapper(container_type& c): container(c) 40 | { 41 | 42 | } 43 | 44 | struct enumerate_wrapper_iter 45 | { 46 | size_t index; 47 | iterator_type value; 48 | 49 | 50 | constexpr bool operator!=(const iterator_type& other) const 51 | { 52 | return value != other; 53 | } 54 | constexpr enumerate_wrapper_iter& operator++() 55 | { 56 | ++index; 57 | ++value; 58 | return *this; 59 | } 60 | 61 | constexpr std::pair operator*() { 62 | return std::pair{index, *value}; 63 | } 64 | }; 65 | 66 | constexpr enumerate_wrapper_iter begin() 67 | { 68 | return {0, std::begin(container)}; 69 | } 70 | 71 | constexpr iterator_type end() 72 | { 73 | return std::end(container); 74 | } 75 | container_type& container; 76 | }; 77 | 78 | template 79 | constexpr auto enumerate(container_type& c) 80 | { 81 | return enumerate_wrapper(c); 82 | } 83 | 84 | template 85 | constexpr auto const_enumerate(const container_type& c) 86 | { 87 | return enumerate_wrapper(c); 88 | } 89 | } 90 | --------------------------------------------------------------------------------