/* * WiVRn VR streaming * Copyright (C) 2022 Guillaume Meunier * Copyright (C) 2022 Patrick Nicolas * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #pragma once #include #include #include namespace utils { template struct enumerate_range { struct iterator { using underlying_iterator = std::conditional_t, typename T::const_iterator, typename T::iterator>; using underlying_value_type = std::conditional_t, const typename T::value_type, typename T::value_type>; size_t index; underlying_iterator it; std::pair operator*() const { return {index, *it}; } iterator & operator++() { ++it; ++index; return *this; } bool operator==(const iterator & other) const { return it == other.it; } bool operator!=(const iterator & other) const { return it != other.it; } }; T & range; iterator begin() const { if constexpr (std::is_const_v) return iterator{0, std::cbegin(range)}; else return iterator{0, std::begin(range)}; } iterator end() const { if constexpr (std::is_const_v) return iterator{0, std::cend(range)}; else return iterator{0, std::end(range)}; } }; template enumerate_range enumerate(const T & t) { return {t}; } template enumerate_range enumerate(T & t) { return {t}; } template struct zip_range { struct iterator { using iterator1 = decltype(std::begin(std::declval())); using iterator2 = decltype(std::begin(std::declval())); using value1 = decltype(*std::declval()); using value2 = decltype(*std::declval()); iterator1 iter1; iterator2 iter2; std::pair operator*() const { return {*iter1, *iter2}; } iterator & operator++() { ++iter1; ++iter2; return *this; } bool operator==(const iterator & other) const { return iter1 == other.iter1 || iter2 == other.iter2; } bool operator!=(const iterator & other) const { return !(*this == other); } }; T1 & range1; T2 & range2; iterator begin() const { return iterator{std::begin(range1), std::begin(range2)}; } iterator end() const { return iterator{std::end(range1), std::end(range2)}; } }; template zip_range zip(T1 & range1, T2 & range2) { return zip_range{range1, range2}; } } // namespace utils