zip iterators work. now have .take

v1
Brett 2024-09-29 18:26:34 -04:00
parent 2c0ace639f
commit 5f61e2be4c
8 changed files with 392 additions and 67 deletions

View File

@ -1,4 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="CMakePythonSetting">
<option name="pythonIntegrationState" value="YES" />
</component>
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" /> <component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project> </project>

32
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,32 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/cmake-build-debug/BLT_TESTS",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}

75
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,75 @@
{
"files.associations": {
"iostream": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"any": "cpp",
"array": "cpp",
"atomic": "cpp",
"strstream": "cpp",
"bit": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"string": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ostream": "cpp",
"scoped_allocator": "cpp",
"semaphore": "cpp",
"shared_mutex": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cfenv": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"variant": "cpp"
}
}

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20) cmake_minimum_required(VERSION 3.20)
include(cmake/color.cmake) include(cmake/color.cmake)
set(BLT_VERSION 1.0.3) set(BLT_VERSION 1.0.4)
set(BLT_TARGET BLT) set(BLT_TARGET BLT)

View File

@ -20,28 +20,23 @@
#define BLT_ITERATOR_ZIP #define BLT_ITERATOR_ZIP
#include <blt/iterator/iter_common.h> #include <blt/iterator/iter_common.h>
#include <blt/meta/meta.h>
#include <blt/meta/iterator.h>
#include <tuple> #include <tuple>
namespace blt namespace blt
{ {
namespace iterator namespace iterator
{ {
template<typename Tag, typename... Iter> template<typename Tag, typename... Iter>
class zip_wrapper; class zip_wrapper;
template<typename Tag, typename... Iter>
class zip_iterator_storage;
template<typename Tag, typename... Iter>
class zip_iterator_storage_rev;
template<typename... Iter> template<typename... Iter>
class zip_wrapper<std::forward_iterator_tag, Iter...> class zip_wrapper<std::input_iterator_tag, Iter...>
{ {
public: public:
using iterator_category = std::forward_iterator_tag; using iterator_category = std::input_iterator_tag;
using value_type = std::tuple<blt::meta::deref_return_t<Iter>...>; using value_type = std::tuple<meta::deref_return_t<Iter>...>;
using difference_type = blt::ptrdiff_t; using difference_type = blt::ptrdiff_t;
using pointer = value_type; using pointer = value_type;
using reference = value_type; using reference = value_type;
@ -49,9 +44,9 @@ namespace blt
explicit zip_wrapper(Iter... iter): iter(std::make_tuple(iter...)) explicit zip_wrapper(Iter... iter): iter(std::make_tuple(iter...))
{} {}
std::tuple<blt::meta::deref_return_t<Iter>...> operator*() const std::tuple<meta::deref_return_t<Iter>...> operator*() const
{ {
return std::apply([](auto& ... i) { return std::make_tuple(*i...); }, iter); return std::apply([](auto& ... i) { return std::tuple<meta::deref_return_t<Iter>...>{*i...}; }, iter);
} }
friend bool operator==(const zip_wrapper& a, const zip_wrapper& b) friend bool operator==(const zip_wrapper& a, const zip_wrapper& b)
@ -87,17 +82,30 @@ namespace blt
}; };
template<typename... Iter> template<typename... Iter>
class zip_wrapper<std::bidirectional_iterator_tag, Iter...> : public zip_wrapper<std::forward_iterator_tag, Iter...> class zip_wrapper<std::forward_iterator_tag, Iter...> : public zip_wrapper<std::input_iterator_tag, Iter...>
{ {
public: public:
using zip_wrapper<std::forward_iterator_tag, Iter...>::zip_wrapper; using iterator_category = std::forward_iterator_tag;
using value_type = std::tuple<meta::deref_return_t<Iter>...>;
using iterator_category = std::bidirectional_iterator_tag;
using value_type = std::tuple<blt::meta::deref_return_t<Iter>...>;
using difference_type = blt::ptrdiff_t; using difference_type = blt::ptrdiff_t;
using pointer = value_type; using pointer = value_type;
using reference = value_type; using reference = value_type;
using zip_wrapper<std::input_iterator_tag, Iter...>::zip_wrapper;
};
template<typename... Iter>
class zip_wrapper<std::bidirectional_iterator_tag, Iter...> : public zip_wrapper<std::forward_iterator_tag, Iter...>
{
public:
using iterator_category = std::bidirectional_iterator_tag;
using value_type = std::tuple<meta::deref_return_t<Iter>...>;
using difference_type = blt::ptrdiff_t;
using pointer = value_type;
using reference = value_type;
public:
using zip_wrapper<std::forward_iterator_tag, Iter...>::zip_wrapper;
zip_wrapper& operator--() zip_wrapper& operator--()
{ {
std::apply([](auto& ... i) { ((--i), ...); }, this->iter); std::apply([](auto& ... i) { ((--i), ...); }, this->iter);
@ -115,25 +123,25 @@ namespace blt
template<typename... Iter> template<typename... Iter>
class zip_wrapper<std::random_access_iterator_tag, Iter...> : public zip_wrapper<std::bidirectional_iterator_tag, Iter...> class zip_wrapper<std::random_access_iterator_tag, Iter...> : public zip_wrapper<std::bidirectional_iterator_tag, Iter...>
{ {
public:
using iterator_category = std::random_access_iterator_tag;
using value_type = std::tuple<meta::deref_return_t<Iter>...>;
using difference_type = blt::ptrdiff_t;
using pointer = value_type;
using reference = value_type;
private: private:
template<typename T, T... n> template<typename T, T... n>
static blt::ptrdiff_t sub(const zip_wrapper& a, const zip_wrapper& b, static blt::ptrdiff_t sub(const zip_wrapper& a, const zip_wrapper& b,
std::integer_sequence<T, n...>) std::integer_sequence<T, n...>)
{ {
auto min = std::min(std::get<n>(a.iter) - std::get<n>(b.iter)...); blt::ptrdiff_t min = std::numeric_limits<blt::ptrdiff_t>::max();
((min = std::min(min, std::get<n>(a.iter) - std::get<n>(b.iter))), ...);
return min; return min;
} }
public: public:
using zip_wrapper<std::bidirectional_iterator_tag, Iter...>::zip_wrapper; using zip_wrapper<std::bidirectional_iterator_tag, Iter...>::zip_wrapper;
using iterator_category = std::random_access_iterator_tag;
using value_type = std::tuple<blt::meta::deref_return_t<Iter>...>;
using difference_type = blt::ptrdiff_t;
using pointer = value_type;
using reference = value_type;
using zip_bidirectional_iterator<Iter...>::zip_bidirectional_iterator;
zip_wrapper& operator+=(blt::ptrdiff_t n) zip_wrapper& operator+=(blt::ptrdiff_t n)
{ {
std::apply([n](auto& ... i) { ((i += n), ...); }, this->iter); std::apply([n](auto& ... i) { ((i += n), ...); }, this->iter);
@ -148,22 +156,22 @@ namespace blt
friend zip_wrapper operator+(const zip_wrapper& a, blt::ptrdiff_t n) friend zip_wrapper operator+(const zip_wrapper& a, blt::ptrdiff_t n)
{ {
return std::apply([n](auto& ... i) { return zip_random_access_iterator{(i + n)...}; }, a.iter); return std::apply([n](auto& ... i) { return zip_wrapper{(i + n)...}; }, a.iter);
} }
friend zip_wrapper operator+(blt::ptrdiff_t n, const zip_wrapper& a) friend zip_wrapper operator+(blt::ptrdiff_t n, const zip_wrapper& a)
{ {
return std::apply([n](auto& ... i) { return zip_random_access_iterator{(i + n)...}; }, a.iter); return std::apply([n](auto& ... i) { return zip_wrapper{(i + n)...}; }, a.iter);
} }
friend zip_wrapper operator-(const zip_wrapper& a, blt::ptrdiff_t n) friend zip_wrapper operator-(const zip_wrapper& a, blt::ptrdiff_t n)
{ {
return std::apply([n](auto& ... i) { return zip_random_access_iterator{(i - n)...}; }, a.iter); return std::apply([n](auto& ... i) { return zip_wrapper{(i - n)...}; }, a.iter);
} }
friend zip_wrapper operator-(blt::ptrdiff_t n, const zip_wrapper& a) friend zip_wrapper operator-(blt::ptrdiff_t n, const zip_wrapper& a)
{ {
return std::apply([n](auto& ... i) { return zip_random_access_iterator{(i - n)...}; }, a.iter); return std::apply([n](auto& ... i) { return zip_wrapper{(i - n)...}; }, a.iter);
} }
friend blt::ptrdiff_t operator-(const zip_wrapper& a, const zip_wrapper& b) friend blt::ptrdiff_t operator-(const zip_wrapper& a, const zip_wrapper& b)
@ -197,43 +205,199 @@ namespace blt
} }
}; };
template<typename... Iter> template<typename Iter>
class zip_iterator_storage<std::forward_iterator_tag, Iter...> auto get_base(Iter iter)
{ {
if constexpr (meta::is_reverse_iterator_v<Iter>)
};
template<typename... Iter>
class zip_iterator_storage<std::bidirectional_iterator_tag, Iter...>
{ {
return std::move(iter).base();
}; } else
template<typename... Iter>
class zip_iterator_storage<std::random_access_iterator_tag, Iter...>
{ {
return std::move(iter);
}
}
}; template<typename Derived>
class take_impl
template<typename... Iter>
class zip_iterator_storage_rev<std::forward_iterator_tag, Iter...>
{ {
private:
}; template<bool check>
auto take_base(blt::size_t n)
template<typename... Iter>
class zip_iterator_storage_rev<std::bidirectional_iterator_tag, Iter...>
{ {
static_assert(!std::is_same_v<typename Derived::iterator_category, std::input_iterator_tag>,
"Cannot .take() on an input iterator!");
auto* d = static_cast<Derived*>(this);
auto begin = d->begin();
auto end = d->end();
}; // take variant for forward and bidirectional iterators
if constexpr (std::is_same_v<typename Derived::iterator_category, std::forward_iterator_tag> ||
template<typename... Iter> std::is_same_v<typename Derived::iterator_category, std::bidirectional_iterator_tag>)
class zip_iterator_storage_rev<std::random_access_iterator_tag, Iter...>
{ {
// with these guys we have to loop forward to move the iterators. an unfortunate inefficiency
auto new_end = begin;
for (blt::size_t i = 0; i < n; i++)
{
if constexpr (check)
{
if (new_end == end)
break;
}
++new_end;
}
return Derived{get_base(std::move(begin)), get_base(std::move(new_end))};
} else if constexpr (std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>)
{
// random access iterators can have math directly applied to them.
if constexpr (check)
{
return Derived{get_base(begin),
get_base(begin + std::min(n, static_cast<blt::size_t>(std::distance(begin, end))))};
} else
{
return Derived{get_base(begin), get_base(begin + n)};
}
}
}
public:
auto take(blt::size_t n)
{ return take_base<false>(n); }
auto take_or(blt::size_t n)
{ return take_base<true>(n); }
}; };
} }
template<typename Iter>
struct iterator_pair
{
using type = Iter;
iterator_pair(Iter begin, Iter end): begin(std::move(begin)), end(std::move(end))
{}
Iter begin;
Iter end;
};
template<typename... Iter>
class zip_iterator_storage;
template<typename... Iter>
class zip_iterator_storage_rev;
template<typename... Iter>
class zip_iterator_storage : public iterator::take_impl<zip_iterator_storage<Iter...>>
{
public:
using iterator_category = meta::lowest_iterator_category_t<Iter...>;
public:
zip_iterator_storage(iterator_pair<Iter>... iterator_pairs):
m_begins(std::move(iterator_pairs.begin)...), m_ends(std::move(iterator_pairs.end)...)
{}
zip_iterator_storage(iterator::zip_wrapper<iterator_category, Iter...> begins, iterator::zip_wrapper<iterator_category, Iter...> ends):
m_begins(std::move(begins)), m_ends(std::move(ends))
{}
auto rev()
{
static_assert((std::is_same_v<iterator_category, std::bidirectional_iterator_tag> ||
std::is_same_v<iterator_category, std::random_access_iterator_tag>),
".rev() must be used with bidirectional (or better) iterators!");
return zip_iterator_storage_rev{m_ends, m_begins};
}
auto skip(blt::size_t n)
{
if constexpr (std::is_same_v<iterator_category, std::input_iterator_tag>)
{
}
}
auto begin() const
{
return m_begins;
}
auto end() const
{
return m_ends;
}
private:
iterator::zip_wrapper<iterator_category, Iter...> m_begins;
iterator::zip_wrapper<iterator_category, Iter...> m_ends;
};
template<typename... Iter>
class zip_iterator_storage_rev : public iterator::take_impl<zip_iterator_storage_rev<Iter...>>
{
public:
using iterator_category = meta::lowest_iterator_category_t<Iter...>;
public:
zip_iterator_storage_rev(iterator_pair<Iter>... iterator_pairs): m_begins(iterator_pairs.begin...), m_ends(iterator_pairs.end...)
{
static_assert((std::is_same_v<iterator_category, std::bidirectional_iterator_tag> ||
std::is_same_v<iterator_category, std::random_access_iterator_tag>),
"reverse iteration is only supported on bidirectional or better iterators!");
}
zip_iterator_storage_rev(iterator::zip_wrapper<iterator_category, Iter...> begins,
iterator::zip_wrapper<iterator_category, Iter...> ends): m_begins(std::move(begins)), m_ends(std::move(ends))
{
static_assert((std::is_same_v<iterator_category, std::bidirectional_iterator_tag> ||
std::is_same_v<iterator_category, std::random_access_iterator_tag>),
"reverse iteration is only supported on bidirectional or better iterators!");
}
auto rev()
{
return zip_iterator_storage{m_ends.base(), m_begins.base()};
}
auto begin() const
{
return m_begins;
}
auto end() const
{
return m_ends;
}
private:
std::reverse_iterator<iterator::zip_wrapper<iterator_category, Iter...>> m_begins;
std::reverse_iterator<iterator::zip_wrapper<iterator_category, Iter...>> m_ends;
};
/*
* CTAD for the zip containers
*/
template<typename... Iter>
zip_iterator_storage(iterator_pair<Iter>...) -> zip_iterator_storage<Iter...>;
template<typename... Iter>
zip_iterator_storage(std::initializer_list<Iter>...) -> zip_iterator_storage<Iter...>;
template<typename... Iter>
zip_iterator_storage_rev(iterator_pair<Iter>...) -> zip_iterator_storage_rev<Iter...>;
template<typename... Iter>
zip_iterator_storage_rev(std::initializer_list<Iter>...) -> zip_iterator_storage_rev<Iter...>;
/*
* Helper functions for creating zip containers
*/
template<typename... Container>
auto zip(Container& ... container)
{
return blt::zip_iterator_storage{iterator_pair{container.begin(), container.end()}...};
}
} }
#endif //BLT_ITERATOR_ZIP #endif //BLT_ITERATOR_ZIP

View File

@ -114,6 +114,18 @@ namespace blt::meta
template<typename... Iter> template<typename... Iter>
using lowest_iterator_category_t = typename lowest_iterator_category<Iter...>::type; using lowest_iterator_category_t = typename lowest_iterator_category<Iter...>::type;
template<typename Iter>
struct is_reverse_iterator : std::false_type
{
};
template<typename Iter>
struct is_reverse_iterator<std::reverse_iterator<Iter>> : std::true_type
{};
template<typename Iter>
inline constexpr bool is_reverse_iterator_v = is_reverse_iterator<Iter>::value;
} }
#endif //BLT_META_ITERATOR_H #endif //BLT_META_ITERATOR_H

@ -1 +1 @@
Subproject commit 7ef2e733416953b222851f9a360d7fc72d068ee5 Subproject commit 93201da2ba5a6aba0a6e57ada64973555629b3e3

View File

@ -24,13 +24,17 @@
#include <blt/iterator/iterator.h> #include <blt/iterator/iterator.h>
#include <blt/format/boxing.h> #include <blt/format/boxing.h>
#include <array> #include <array>
#include <forward_list>
constexpr auto increasing_reverse_pairs = [](blt::size_t i, blt::size_t index, blt::size_t size) { return i == 0 ? index : (size - 1) - index; }; constexpr auto increasing_reverse_pairs =
constexpr auto increasing_pairs = [](blt::size_t, blt::size_t index, blt::size_t) { return index; }; [](blt::size_t i, blt::size_t index, blt::size_t size) { return i == 0 ? index : (size - 1) - index; };
constexpr auto decreasing_pairs = [](blt::size_t, blt::size_t index, blt::size_t size) { return size - index; }; constexpr auto increasing_pairs =
[](blt::size_t, blt::size_t index, blt::size_t) { return index; };
constexpr auto decreasing_pairs =
[](blt::size_t, blt::size_t index, blt::size_t size) { return size - index; };
template<blt::size_t n, typename Func> template<blt::size_t n, typename Func>
std::array<blt::vec2, n> make_array(Func&& func) std::array<blt::vec2, n> make_array(Func func)
{ {
std::array<blt::vec2, n> array; std::array<blt::vec2, n> array;
for (auto&& [index, value] : blt::enumerate(array)) for (auto&& [index, value] : blt::enumerate(array))
@ -38,11 +42,22 @@ std::array<blt::vec2, n> make_array(Func&& func)
return array; return array;
} }
template<blt::size_t n, typename Func>
std::forward_list<blt::vec2> make_list(Func func)
{
std::forward_list<blt::vec2> array;
for (auto index : blt::range(0ul, n))
array.push_front(blt::vec2(func(0, index, n), func(1, index, n)));
return array;
}
constexpr blt::size_t array_size = 10; constexpr blt::size_t array_size = 10;
auto array_1 = make_array<array_size>(increasing_reverse_pairs); auto array_1 = make_array<array_size>(increasing_reverse_pairs);
auto array_2 = make_array<array_size>(increasing_pairs); auto array_2 = make_array<array_size>(increasing_pairs);
auto array_3 = make_array<array_size>(decreasing_pairs); auto array_3 = make_array<array_size>(decreasing_pairs);
auto list_1 = make_list<array_size>(increasing_reverse_pairs);
void test_enumerate() void test_enumerate()
{ {
blt::log_box_t box(std::cout, "Enumerate Tests", 25); blt::log_box_t box(std::cout, "Enumerate Tests", 25);
@ -111,6 +126,30 @@ void test_pairs()
void test_zip() void test_zip()
{ {
blt::log_box_t box(std::cout, "Zip Tests", 25); blt::log_box_t box(std::cout, "Zip Tests", 25);
for (auto [a1, a2, a3] : blt::zip(array_1, array_2, array_3))
{
BLT_TRACE_STREAM << a1 << " : " << a2 << " : " << a3 << "\n";
}
BLT_TRACE("================================");
for (auto [a1, a2, a3] : blt::zip(array_1, array_2, array_3).take(3))
{
BLT_TRACE_STREAM << a1 << " : " << a2 << " : " << a3 << "\n";
}
BLT_TRACE("================================");
for (auto [a1, a2, a3] : blt::zip(array_1, array_2, array_3).take(3).rev())
{
BLT_TRACE_STREAM << a1 << " : " << a2 << " : " << a3 << "\n";
}
BLT_TRACE("================================");
for (auto [a1, a2, a3] : blt::zip(array_1, array_2, array_3).take_or(13))
{
BLT_TRACE_STREAM << a1 << " : " << a2 << " : " << a3 << "\n";
}
BLT_TRACE("================================");
for (auto [a1, a2, a3] : blt::zip(array_1, array_2, array_3).rev().take(3))
{
BLT_TRACE_STREAM << a1 << " : " << a2 << " : " << a3 << "\n";
}
} }
int main() int main()