From 68451b04337199feb65e41bf63bba37b2b6ed8a2 Mon Sep 17 00:00:00 2001 From: Brett Laptop Date: Tue, 1 Oct 2024 13:27:39 -0400 Subject: [PATCH] work on enumerate --- CMakeLists.txt | 2 +- include/blt/iterator/common.h | 2 +- include/blt/iterator/enumerate.h | 59 ++++++++++++++++++++++++++++++++ include/blt/iterator/zip.h | 4 +-- tests/iterator_tests.cpp | 1 + 5 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 include/blt/iterator/enumerate.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 58db704..29decc6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 1.1.6) +set(BLT_VERSION 1.1.7) set(BLT_TARGET BLT) diff --git a/include/blt/iterator/common.h b/include/blt/iterator/common.h index 3918d09..50f3b55 100644 --- a/include/blt/iterator/common.h +++ b/include/blt/iterator/common.h @@ -115,7 +115,7 @@ namespace blt::iterator } protected: - mutable Iter iter; + Iter iter; }; template diff --git a/include/blt/iterator/enumerate.h b/include/blt/iterator/enumerate.h new file mode 100644 index 0000000..3747a70 --- /dev/null +++ b/include/blt/iterator/enumerate.h @@ -0,0 +1,59 @@ +#pragma once +/* + * Copyright (C) 2024 Brett Terpstra + * + * 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 . + */ + +#ifndef BLT_ITERATOR_ENUMERATE_H +#define BLT_ITERATOR_ENUMERATE_H + +#include +#include +#include + +namespace blt +{ + + namespace iterator + { + /** + * struct which is returned by the enumerator. + * @tparam T type to store. + */ + template + struct enumerate_item + { + blt::size_t index; + T value; + }; + + template + class enumerate_wrapper : public passthrough_wrapper> + { + public: + using passthrough_wrapper>::passthrough_wrapper; + + enumerate_item> operator*() const + { + return *this->iter; + } + private: + blt::size_t index; + }; + } + +} + +#endif //BLT_ITERATOR_ENUMERATE_H diff --git a/include/blt/iterator/zip.h b/include/blt/iterator/zip.h index e763f0c..1fec30d 100644 --- a/include/blt/iterator/zip.h +++ b/include/blt/iterator/zip.h @@ -145,8 +145,8 @@ namespace blt template auto zip(iterator_pair... iterator_pairs) { - zip_iterator_container(iterator::zip_wrapper{std::move(iterator_pairs.begin)...}, - iterator::zip_wrapper{std::move(iterator_pairs.end)...}); + auto* d = static_cast(this); + return zip_iterator_container(iterator_pairbegin())>{d->begin(), d->end()}, iterator_pairs...); } }; } diff --git a/tests/iterator_tests.cpp b/tests/iterator_tests.cpp index 0fb5198..2cc275a 100644 --- a/tests/iterator_tests.cpp +++ b/tests/iterator_tests.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include