BLT/include/blt/iterator/iterator.h

884 lines
39 KiB
C
Raw Normal View History

#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 <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_ITERATOR_H
#define BLT_ITERATOR_H
#include <blt/std/types.h>
2024-09-25 20:52:22 -04:00
#include <blt/std/logging.h>
#include <blt/meta/meta.h>
#include <blt/meta/iterator.h>
#include <type_traits>
#include <iterator>
2024-09-27 18:18:02 -04:00
#include <tuple>
namespace blt
{
2024-09-25 03:15:01 -04:00
// forward declare useful types
template<typename Iter, typename = std::void_t<>>
class enumerator;
template<typename Iter, typename = std::void_t<>>
class enumerator_rev;
2024-09-27 15:12:08 -04:00
template<typename Iter1, typename Iter2, typename = std::void_t<>>
class pair_iterator;
template<typename Iter, typename Iter2, typename = std::void_t<>>
class pair_iterator_rev;
2024-09-27 18:18:02 -04:00
template<typename... Iter>
class zip_iterator;
template<typename... Iter>
class zip_iterator_rev;
namespace iterator
{
template<typename Iter, typename = std::void_t<>>
class enumerate_wrapper;
2024-09-26 17:02:05 -04:00
template<typename Iter1, typename Iter2, typename = std::void_t<>>
class pair_wrapper;
2024-09-27 18:18:02 -04:00
template<typename Tag, typename... Iter>
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>
class zip_forward_iterator
{
public:
explicit zip_forward_iterator(Iter... iter): iter(std::make_tuple(iter...))
{}
std::tuple<blt::meta::deref_return_t<Iter>...> operator*() const
{
return std::apply([](auto& ... i) { return std::make_tuple(*i...); }, iter);
}
friend bool operator==(const zip_forward_iterator& a, const zip_forward_iterator& b)
{
return a.iter == b.iter;
}
friend bool operator!=(const zip_forward_iterator& a, const zip_forward_iterator& b)
{
return !(a.iter == b.iter);
}
zip_forward_iterator& operator++()
{
std::apply([](auto& ... i) { ((++i), ...); }, iter);
return *this;
}
zip_forward_iterator operator++(int)
{
auto tmp = *this;
++*this;
return tmp;
}
auto base()
{
return iter;
}
protected:
std::tuple<Iter...> iter;
};
template<typename... Iter>
class zip_bidirectional_iterator : public zip_forward_iterator<Iter...>
{
public:
using zip_forward_iterator<Iter...>::zip_forward_iterator;
zip_bidirectional_iterator& operator--()
{
std::apply([](auto& ... i) { ((--i), ...); }, this->iter);
return *this;
}
zip_bidirectional_iterator operator--(int)
{
auto tmp = *this;
--*this;
return tmp;
}
};
template<typename... Iter>
class zip_wrapper<std::forward_iterator_tag, Iter...> : public zip_forward_iterator<Iter...>
{
public:
using zip_forward_iterator<Iter...>::zip_forward_iterator;
using iterator_category = std::forward_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;
};
template<typename... Iter>
class zip_wrapper<std::bidirectional_iterator_tag, Iter...> : public zip_bidirectional_iterator<Iter...>
{
public:
using zip_bidirectional_iterator<Iter...>::zip_bidirectional_iterator;
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 pointer = value_type;
using reference = value_type;
};
template<typename... Iter>
class zip_wrapper<std::random_access_iterator_tag, Iter...> : public zip_bidirectional_iterator<Iter...>
{
public:
using zip_bidirectional_iterator<Iter...>::zip_bidirectional_iterator;
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 pointer = value_type;
using reference = value_type;
};
2024-09-26 02:53:11 -04:00
/**
* struct which is returned by the enumerator.
* @tparam T type to store.
*/
template<typename T>
struct enumerate_item
{
blt::size_t index;
T value;
};
2024-09-26 02:53:11 -04:00
/**
* base class for iterators which operate on pairs of values. Handles comparison.
* @tparam Iter1 first iterator type. this will be used for comparison.
* @tparam Iter2 second iterator type. this value is not modified by this class.
*/
2024-09-25 03:15:01 -04:00
template<typename Iter1, typename Iter2>
2024-09-26 13:51:07 -04:00
class dual_iterator_base
2024-09-25 03:15:01 -04:00
{
public:
2024-09-26 13:51:07 -04:00
explicit dual_iterator_base(Iter1 iter1, Iter2 iter2): m_iter1(std::move(iter1)), m_iter2(std::move(iter2))
2024-09-25 03:15:01 -04:00
{}
2024-09-26 13:51:07 -04:00
friend bool operator==(const dual_iterator_base& a, const dual_iterator_base& b)
2024-09-25 03:15:01 -04:00
{
return a.m_iter1 == b.m_iter1;
}
2024-09-26 13:51:07 -04:00
friend bool operator!=(const dual_iterator_base& a, const dual_iterator_base& b)
2024-09-25 03:15:01 -04:00
{
return a.m_iter1 != b.m_iter1;
}
auto iter1() const
{
return m_iter1;
}
auto iter2() const
{
return m_iter2;
}
2024-09-25 20:52:22 -04:00
2024-09-25 03:15:01 -04:00
protected:
Iter1 m_iter1;
Iter2 m_iter2;
};
2024-09-26 02:53:11 -04:00
/**
* Base class for all enumerator iterators. Handles the deference (*) operator.
* @tparam Iter iterator type
*/
template<typename Iter>
2024-09-26 13:51:07 -04:00
class enumerate_iterator_base : public dual_iterator_base<Iter, blt::size_t>
{
public:
2024-09-25 20:52:22 -04:00
explicit enumerate_iterator_base(Iter iter, blt::size_t place = 0):
2024-09-26 13:51:07 -04:00
dual_iterator_base<Iter, blt::size_t>(std::move(iter), place)
{}
enumerate_item<blt::meta::deref_return_t<Iter>> operator*() const
{
2024-09-25 20:52:22 -04:00
return {this->m_iter2, *this->m_iter1};
}
2024-09-26 17:02:05 -04:00
};
template<typename Iter1, typename Iter2>
class pair_iterator_base : public dual_iterator_base<Iter1, Iter2>
{
public:
using dual_iterator_base<Iter1, Iter2>::dual_iterator_base;
2024-09-26 17:02:05 -04:00
std::pair<blt::meta::deref_return_t<Iter1>, blt::meta::deref_return_t<Iter2>> operator*() const
{
2024-09-26 17:02:05 -04:00
return {*this->m_iter1, *this->m_iter2};
}
};
2024-09-26 02:53:11 -04:00
/**
2024-09-26 17:02:05 -04:00
* Forward iterator base class. Contains the ++ operator.
* @tparam Base iterator base type.
2024-09-26 02:53:11 -04:00
*/
2024-09-26 17:02:05 -04:00
template<typename Base>
class forward_iterator_base : public Base
{
public:
2024-09-26 17:02:05 -04:00
using Base::Base;
2024-09-26 17:02:05 -04:00
forward_iterator_base& operator++()
{
2024-09-25 20:52:22 -04:00
++this->m_iter1;
++this->m_iter2;
return *this;
}
2024-09-26 17:02:05 -04:00
forward_iterator_base operator++(int)
{
auto tmp = *this;
++*this;
return tmp;
}
};
2024-09-26 02:53:11 -04:00
/**
2024-09-26 17:02:05 -04:00
* Bidirectional iterator base class. Contains the -- operator.
* @tparam Base iterator base type.
2024-09-26 02:53:11 -04:00
*/
2024-09-26 17:02:05 -04:00
template<typename Base>
class bidirectional_iterator_base : public Base
{
public:
2024-09-26 17:02:05 -04:00
using Base::Base;
2024-09-26 17:02:05 -04:00
bidirectional_iterator_base& operator--()
{
2024-09-25 20:52:22 -04:00
--this->m_iter1;
--this->m_iter2;
return *this;
}
2024-09-26 17:02:05 -04:00
bidirectional_iterator_base operator--(int)
{
auto tmp = *this;
--*this;
return tmp;
}
};
2024-09-26 17:02:05 -04:00
template<typename Iter>
using enumerate_forward_iterator = forward_iterator_base<enumerate_iterator_base<Iter>>;
template<typename Iter>
using enumerate_bidirectional_iterator = bidirectional_iterator_base<enumerate_forward_iterator<Iter>>;
template<typename Iter1, typename Iter2>
using pair_forward_iterator = forward_iterator_base<pair_iterator_base<Iter1, Iter2>>;
template<typename Iter1, typename Iter2>
2024-09-27 18:18:02 -04:00
using pair_bidirectional_iterator = bidirectional_iterator_base<pair_forward_iterator<Iter1, Iter2>>;
2024-09-26 17:02:05 -04:00
2024-09-26 02:53:11 -04:00
/**
2024-09-26 17:02:05 -04:00
* Enumerator wrapper class specialization for forward iterators.
2024-09-26 02:53:11 -04:00
* @tparam Iter iterator type
*/
template<typename Iter>
class enumerate_wrapper<Iter, std::enable_if_t<blt::meta::is_forward_iterator_v<Iter>, std::void_t<std::forward_iterator_tag>>>
: public enumerate_forward_iterator<Iter>
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = enumerate_item<blt::meta::deref_return_t<Iter>>;
using difference_type = typename std::iterator_traits<Iter>::difference_type;
using pointer = value_type;
using reference = value_type;
using enumerate_forward_iterator<Iter>::enumerate_forward_iterator;
};
2024-09-26 02:53:11 -04:00
/**
2024-09-26 17:02:05 -04:00
* Pair wrapper class specialization for forward iterators.
* @tparam Iter iterator type
*/
template<typename Iter1, typename Iter2>
class pair_wrapper<Iter1, Iter2, std::enable_if_t<
2024-09-27 18:18:02 -04:00
blt::meta::is_forward_iterator_category_v<blt::meta::lowest_iterator_category_t<Iter1, Iter2>>,
2024-09-27 15:12:08 -04:00
std::void_t<std::forward_iterator_tag>>> : public pair_forward_iterator<Iter1, Iter2>
2024-09-26 17:02:05 -04:00
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = std::pair<blt::meta::deref_return_t<Iter1>, blt::meta::deref_return_t<Iter2>>;
using difference_type = std::common_type_t<typename std::iterator_traits<Iter1>::difference_type, typename std::iterator_traits<Iter2>::difference_type>;
using pointer = value_type;
using reference = value_type;
using pair_forward_iterator<Iter1, Iter2>::pair_forward_iterator;
};
/**
* Enumerator wrapper class for bidirectional iterators or random access iterators.
2024-09-26 02:53:11 -04:00
* @tparam Iter iterator type.
*/
template<typename Iter>
class enumerate_wrapper<Iter, std::enable_if_t<blt::meta::is_bidirectional_or_better_v<Iter>, std::void_t<std::bidirectional_iterator_tag>>>
: public enumerate_bidirectional_iterator<Iter>
{
public:
2024-09-27 18:18:02 -04:00
using iterator_category = std::bidirectional_iterator_tag;
using value_type = enumerate_item<blt::meta::deref_return_t<Iter>>;
using difference_type = typename std::iterator_traits<Iter>::difference_type;
using pointer = value_type;
using reference = value_type;
using enumerate_bidirectional_iterator<Iter>::enumerate_bidirectional_iterator;
};
2024-09-26 02:53:11 -04:00
/**
2024-09-26 17:02:05 -04:00
* Pair wrapper class for bidirectional iterators or random access iterators.
2024-09-26 02:53:11 -04:00
* @tparam Iter iterator type.
*/
2024-09-26 17:02:05 -04:00
template<typename Iter1, typename Iter2>
class pair_wrapper<Iter1, Iter2, std::enable_if_t<
2024-09-27 18:18:02 -04:00
blt::meta::is_bidirectional_or_better_category_v<blt::meta::lowest_iterator_category_t<Iter1, Iter2>>,
2024-09-27 15:12:08 -04:00
std::void_t<std::bidirectional_iterator_tag>>> : public pair_bidirectional_iterator<Iter1, Iter2>
{
public:
2024-09-27 18:18:02 -04:00
using iterator_category = std::bidirectional_iterator_tag;
2024-09-26 17:02:05 -04:00
using value_type = std::pair<blt::meta::deref_return_t<Iter1>, blt::meta::deref_return_t<Iter2>>;
using difference_type = std::common_type_t<typename std::iterator_traits<Iter1>::difference_type, typename std::iterator_traits<Iter2>::difference_type>;
using pointer = value_type;
using reference = value_type;
using pair_bidirectional_iterator<Iter1, Iter2>::pair_bidirectional_iterator;
};
/**
* Base class for storing begin/end iterators.
* @tparam IterWrapper wrapper used to iterate
* @tparam CompleteClass completed class returned from skip/take methods
*/
2024-09-27 15:12:08 -04:00
template<typename IterWrapper, typename CompleteClass>
2024-09-26 17:02:05 -04:00
class iterator_storage_base
{
public:
explicit iterator_storage_base(IterWrapper begin, IterWrapper end): begin_(std::move(begin)), end_(std::move(end))
{}
auto begin()
{
return begin_;
}
auto end()
{
return end_;
}
2024-09-26 02:53:11 -04:00
/**
* Creates an enumerator that skips the first n elements.
* @param amount amount of values to skip.
*/
auto skip(blt::size_t amount)
{
auto begin = this->begin_;
for (blt::size_t i = 0; i < amount; i++)
++begin;
2024-09-26 17:02:05 -04:00
return CompleteClass{begin.iter1(),
this->end_.iter1(),
begin.iter2(),
this->end_.iter2()};
2024-09-26 02:53:11 -04:00
}
/**
* Creates an enumerator that yields the first n elements, or UB if the underlying iterator ends sooner.
* @param amount amount to take.
*/
auto take(blt::size_t amount)
{
auto end = this->begin();
for (blt::size_t i = 0; i < amount; i++)
++end;
2024-09-26 17:02:05 -04:00
return CompleteClass{this->begin_.iter1(),
end.iter1(),
this->begin_.iter2(),
end.iter2()};
2024-09-26 02:53:11 -04:00
}
2024-09-26 17:02:05 -04:00
protected:
IterWrapper begin_;
IterWrapper end_;
};
2024-09-26 02:53:11 -04:00
/**
2024-09-26 17:02:05 -04:00
* Reversible (bidirectional) base class storing the begin / end iterators.
2024-09-26 02:53:11 -04:00
* @tparam Iter iterator type.
2024-09-26 17:02:05 -04:00
* @tparam IterWrapper wrapper used to iterate.
* @tparam CompleteClass completed class returned from skip/take methods
* @tparam CompleteClassRev reverse version of CompleteClass, returned from rev
2024-09-26 02:53:11 -04:00
*/
2024-09-27 15:12:08 -04:00
template<typename IterWrapper, typename CompleteClass, typename CompleteClassRev>
class iterator_storage_reversible : public iterator_storage_base<IterWrapper, CompleteClass>
{
public:
2024-09-27 15:12:08 -04:00
explicit iterator_storage_reversible(IterWrapper begin, IterWrapper end):
iterator_storage_base<IterWrapper, CompleteClass>{std::move(begin), std::move(end)}
{}
2024-09-26 02:53:11 -04:00
/**
* Reverses the enumerators direction.
*/
auto rev() const
{
2024-09-26 17:02:05 -04:00
return CompleteClassRev{this->end_.iter1(),
this->begin_.iter1(),
this->end_.iter2(),
this->begin_.iter2()};
}
2024-09-26 02:53:11 -04:00
};
/**
2024-09-26 17:02:05 -04:00
* Random access base class storage for begin/end iterators.
* Has updated skip and take methods which make use of the random access nature of the iterator.
2024-09-26 02:53:11 -04:00
* @tparam Iter iterator type.
2024-09-26 17:02:05 -04:00
* @tparam IterWrapper wrapper used to iterate.
* @tparam CompleteClass completed class returned from skip/take methods
* @tparam CompleteClassRev reverse version of CompleteClass, returned from rev
2024-09-26 02:53:11 -04:00
*/
2024-09-27 15:12:08 -04:00
template<typename IterWrapper, typename CompleteClass, typename CompleteClassRev>
class iterator_storage_random_access : public iterator_storage_reversible<IterWrapper, CompleteClass, CompleteClassRev>
2024-09-26 02:53:11 -04:00
{
public:
2024-09-27 15:12:08 -04:00
using iterator_storage_reversible<IterWrapper, CompleteClass, CompleteClassRev>::iterator_storage_reversible;
2024-09-25 03:15:01 -04:00
2024-09-25 20:52:22 -04:00
auto skip(blt::size_t amount)
2024-09-25 03:15:01 -04:00
{
2024-09-26 17:02:05 -04:00
return CompleteClass{this->begin_.iter1() + amount,
this->end_.iter1(),
this->begin_.iter2() + amount,
this->end_.iter2()};
2024-09-25 20:52:22 -04:00
}
auto take(blt::size_t amount)
{
2024-09-26 17:02:05 -04:00
return CompleteClass{this->begin_.iter1(),
this->begin_.iter1() + amount,
this->begin_.iter2(),
this->begin_.iter2() + amount};
2024-09-25 03:15:01 -04:00
}
};
2024-09-26 02:53:11 -04:00
/**
2024-09-26 17:02:05 -04:00
* Reversible (bidirectional) base class for storing the begin/end iterators, operates in reverse for reverse iteration.
2024-09-26 02:53:11 -04:00
* @tparam Iter iterator type.
* @tparam IterWrapper wrapper used to iterate (std::reverse_iterator<enumerate_wrapper>).
2024-09-26 17:02:05 -04:00
* @tparam CompleteClass completed class returned from skip/take methods
* @tparam CompleteClassRev reverse version of CompleteClass, returned from rev
2024-09-26 02:53:11 -04:00
*/
2024-09-27 15:12:08 -04:00
template<typename IterWrapper, typename CompleteClass, typename CompleteClassRev>
class iterator_storage_reversible_rev : public iterator_storage_reversible<IterWrapper, CompleteClass, CompleteClassRev>
{
public:
2024-09-27 15:12:08 -04:00
using iterator_storage_reversible<IterWrapper, CompleteClass, CompleteClassRev>::iterator_storage_reversible;
auto rev() const
{
2024-09-26 17:02:05 -04:00
return CompleteClass{this->end_.base().iter1(),
this->begin_.base().iter1(),
this->end_.base().iter2(),
this->begin_.base().iter2()};
2024-09-25 03:15:01 -04:00
}
2024-09-25 20:52:22 -04:00
auto skip(blt::size_t amount)
2024-09-25 03:15:01 -04:00
{
2024-09-25 20:52:22 -04:00
auto begin = this->begin_.base();
for (blt::size_t i = 0; i < amount; i++)
2024-09-25 03:15:01 -04:00
--begin;
2024-09-26 17:02:05 -04:00
return CompleteClassRev{begin.iter1(),
this->end_.base().iter1(),
begin.iter2(),
this->end_.base().iter2()};
2024-09-25 20:52:22 -04:00
}
auto take(blt::size_t amount)
{
auto end = this->begin_.base();
for (blt::size_t i = 0; i < amount; i++)
--end;
2024-09-26 17:02:05 -04:00
return CompleteClassRev{
this->begin_.base().iter1(),
end.iter1(),
this->begin_.base().iter2(),
end.iter2()};
}
};
2024-09-26 02:53:11 -04:00
/**
2024-09-26 17:02:05 -04:00
* Random access base class for storing the begin/end iterator.
* Has updated skip and take methods which make use of the random access nature of the iterator.
* Operates in reverse for reverse iteration.
2024-09-26 02:53:11 -04:00
* @tparam Iter iterator type.
* @tparam IterWrapper wrapper used to iterate (std::reverse_iterator<enumerate_wrapper>).
2024-09-26 17:02:05 -04:00
* @tparam CompleteClass completed class returned from skip/take methods
* @tparam CompleteClassRev reverse version of CompleteClass, returned from rev
2024-09-26 02:53:11 -04:00
*/
2024-09-27 15:12:08 -04:00
template<typename IterWrapper, typename CompleteClass, typename CompleteClassRev>
class iterator_storage_random_access_rev : public iterator_storage_reversible_rev<IterWrapper, CompleteClass, CompleteClassRev>
2024-09-26 02:53:11 -04:00
{
public:
2024-09-27 15:12:08 -04:00
using iterator_storage_reversible_rev<IterWrapper, CompleteClass, CompleteClassRev>::iterator_storage_reversible_rev;
2024-09-26 02:53:11 -04:00
auto skip(blt::size_t amount)
{
2024-09-26 17:02:05 -04:00
return CompleteClassRev{this->begin_.base().iter1() - amount,
this->end_.base().iter1(),
this->begin_.base().iter2() - amount,
this->end_.base().iter2()};
2024-09-26 02:53:11 -04:00
}
auto take(blt::size_t amount)
{
2024-09-26 17:02:05 -04:00
return CompleteClassRev{this->begin_.base().iter1(),
this->begin_.base().iter1() - amount,
this->begin_.base().iter2(),
this->begin_.base().iter2() - amount};
2024-09-26 02:53:11 -04:00
}
};
2024-09-27 18:18:02 -04:00
/**
* Base class for types which can be converted to an enumerator
*/
template<typename Derived, typename CompleteEnumerator>
class enumerator_convertible
{
public:
auto enumerate()
{
auto* b = static_cast<Derived*>(this);
return CompleteEnumerator{b->begin(), b->end(), static_cast<blt::size_t>(std::distance(b->begin(), b->end()))};
}
};
template<typename... Iter>
class zip_iterator_storage<std::forward_iterator_tag, Iter...>
{
};
template<typename... Iter>
class zip_iterator_storage<std::bidirectional_iterator_tag, Iter...>
{
};
template<typename... Iter>
class zip_iterator_storage<std::random_access_iterator_tag, Iter...>
{
};
2024-09-26 02:53:11 -04:00
}
2024-09-26 02:53:11 -04:00
/**
* Enumerator specialization for forward iterators
*/
template<typename Iter>
class enumerator<Iter, std::enable_if_t<blt::meta::is_forward_iterator_v<Iter>, std::void_t<std::forward_iterator_tag>>>
2024-09-27 15:12:08 -04:00
: public iterator::iterator_storage_base<iterator::enumerate_wrapper<Iter>, enumerator<Iter>>
{
public:
2024-09-27 15:12:08 -04:00
explicit enumerator(Iter begin, Iter end, blt::size_t container_size):
iterator::iterator_storage_base<iterator::enumerate_wrapper<Iter>, enumerator<Iter>>
{iterator::enumerate_wrapper<Iter>{std::move(begin), 0},
iterator::enumerate_wrapper<Iter>{std::move(end), container_size}}
{}
explicit enumerator(Iter begin, Iter end, blt::size_t begin_index, blt::size_t end_index):
iterator::iterator_storage_base<iterator::enumerate_wrapper<Iter>, enumerator<Iter>>{
iterator::enumerate_wrapper<Iter>{std::move(begin), begin_index},
iterator::enumerate_wrapper<Iter>{std::move(end), end_index}}
{}
};
2024-09-26 02:53:11 -04:00
/**
* Enumerator specialization for bidirectional iterators
*/
template<typename Iter>
class enumerator<Iter, std::enable_if_t<blt::meta::is_bidirectional_iterator_v<Iter>, std::void_t<std::bidirectional_iterator_tag>>>
2024-09-27 15:12:08 -04:00
: public iterator::iterator_storage_reversible<iterator::enumerate_wrapper<Iter>, enumerator<Iter>, enumerator_rev<Iter>>
{
public:
2024-09-27 15:12:08 -04:00
explicit enumerator(Iter begin, Iter end, blt::size_t container_size):
iterator::iterator_storage_reversible<iterator::enumerate_wrapper<Iter>, enumerator<Iter>, enumerator_rev<Iter>>
{iterator::enumerate_wrapper<Iter>{std::move(begin), 0},
iterator::enumerate_wrapper<Iter>{std::move(end), container_size}}
{}
explicit enumerator(Iter begin, Iter end, blt::size_t begin_index, blt::size_t end_index):
iterator::iterator_storage_reversible<iterator::enumerate_wrapper<Iter>, enumerator<Iter>, enumerator_rev<Iter>>{
iterator::enumerate_wrapper<Iter>{std::move(begin), begin_index},
iterator::enumerate_wrapper<Iter>{std::move(end), end_index}}
{}
};
2024-09-26 02:53:11 -04:00
/**
* Enumerator specialization for random access iterators
*/
template<typename Iter>
class enumerator<Iter, std::enable_if_t<blt::meta::is_random_access_iterator_v<Iter>, std::void_t<std::random_access_iterator_tag>>>
2024-09-27 15:12:08 -04:00
: public iterator::iterator_storage_random_access<iterator::enumerate_wrapper<Iter>, enumerator<Iter>, enumerator_rev<Iter>>
{
public:
2024-09-27 15:12:08 -04:00
explicit enumerator(Iter begin, Iter end, blt::size_t container_size):
iterator::iterator_storage_random_access<iterator::enumerate_wrapper<Iter>, enumerator<Iter>, enumerator_rev<Iter>>
{iterator::enumerate_wrapper<Iter>{std::move(begin), 0},
iterator::enumerate_wrapper<Iter>{std::move(end), container_size}}
{}
explicit enumerator(Iter begin, Iter end, blt::size_t begin_index, blt::size_t end_index):
iterator::iterator_storage_random_access<iterator::enumerate_wrapper<Iter>, enumerator<Iter>, enumerator_rev<Iter>>{
iterator::enumerate_wrapper<Iter>{std::move(begin), begin_index},
iterator::enumerate_wrapper<Iter>{std::move(end), end_index}}
{}
};
2024-09-26 02:53:11 -04:00
/**
* Reverse enumerator specialization for bidirectional iterators
*/
template<typename Iter>
class enumerator_rev<Iter, std::enable_if_t<blt::meta::is_bidirectional_iterator_v<Iter>, std::void_t<std::bidirectional_iterator_tag>>>
2024-09-27 15:12:08 -04:00
: public iterator::iterator_storage_reversible_rev<std::reverse_iterator<iterator::enumerate_wrapper<Iter>>, enumerator<Iter>, enumerator_rev<Iter>>
{
public:
2024-09-27 15:12:08 -04:00
explicit enumerator_rev(Iter begin, Iter end, blt::size_t container_size):
iterator::iterator_storage_reversible_rev<std::reverse_iterator<iterator::enumerate_wrapper<Iter>>, enumerator<Iter>, enumerator_rev<Iter>>
{std::reverse_iterator<iterator::enumerate_wrapper<Iter>>{iterator::enumerate_wrapper<Iter>{std::move(begin), 0}},
std::reverse_iterator<iterator::enumerate_wrapper<Iter>>{
iterator::enumerate_wrapper<Iter>{std::move(end), container_size}}}
{}
explicit enumerator_rev(Iter begin, Iter end, blt::size_t begin_index, blt::size_t end_index):
iterator::iterator_storage_reversible_rev<std::reverse_iterator<iterator::enumerate_wrapper<Iter>>, enumerator<Iter>, enumerator_rev<Iter>>{
std::reverse_iterator<iterator::enumerate_wrapper<Iter>>{
iterator::enumerate_wrapper<Iter>{std::move(begin), begin_index}},
std::reverse_iterator<iterator::enumerate_wrapper<Iter>>{iterator::enumerate_wrapper<Iter>{std::move(end), end_index}}}
{}
};
2024-09-26 02:53:11 -04:00
/**
* Reverse enumerator specialization for random access iterators
*/
template<typename Iter>
class enumerator_rev<Iter, std::enable_if_t<blt::meta::is_random_access_iterator_v<Iter>, std::void_t<std::random_access_iterator_tag>>>
2024-09-27 15:12:08 -04:00
: public iterator::iterator_storage_random_access_rev<std::reverse_iterator<iterator::enumerate_wrapper<Iter>>, enumerator<Iter>, enumerator_rev<Iter>>
{
public:
2024-09-27 15:12:08 -04:00
explicit enumerator_rev(Iter begin, Iter end, blt::size_t container_size):
iterator::iterator_storage_random_access_rev<std::reverse_iterator<iterator::enumerate_wrapper<Iter>>, enumerator<Iter>, enumerator_rev<Iter>>
{std::reverse_iterator<iterator::enumerate_wrapper<Iter>>{iterator::enumerate_wrapper<Iter>{std::move(begin), 0}},
std::reverse_iterator<iterator::enumerate_wrapper<Iter>>{
iterator::enumerate_wrapper<Iter>{std::move(end), container_size}}}
{}
explicit enumerator_rev(Iter begin, Iter end, blt::size_t begin_index, blt::size_t end_index):
iterator::iterator_storage_random_access_rev<std::reverse_iterator<iterator::enumerate_wrapper<Iter>>, enumerator<Iter>, enumerator_rev<Iter>>{
std::reverse_iterator<iterator::enumerate_wrapper<Iter>>{
iterator::enumerate_wrapper<Iter>{std::move(begin), begin_index}},
std::reverse_iterator<iterator::enumerate_wrapper<Iter>>{iterator::enumerate_wrapper<Iter>{std::move(end), end_index}}}
{}
};
2024-09-26 02:53:11 -04:00
// CTAD for enumerators
template<typename Iter>
enumerator(Iter, Iter) -> enumerator<Iter>;
template<typename Iter>
enumerator(Iter, Iter, blt::size_t) -> enumerator<Iter>;
template<typename Iter>
enumerator(Iter, Iter, blt::size_t, blt::size_t) -> enumerator<Iter>;
2024-09-27 15:12:08 -04:00
template<typename Iter1, typename Iter2>
class pair_iterator<Iter1, Iter2,
std::enable_if_t<
2024-09-27 18:18:02 -04:00
blt::meta::is_forward_iterator_category_v<blt::meta::lowest_iterator_category_t<Iter1, Iter2>>,
2024-09-27 15:12:08 -04:00
std::void_t<std::forward_iterator_tag>>>
2024-09-27 18:18:02 -04:00
: public iterator::iterator_storage_base<iterator::pair_wrapper<Iter1, Iter2>, pair_iterator<Iter1, Iter2>>,
public iterator::enumerator_convertible<pair_iterator<Iter1, Iter2>, enumerator<iterator::pair_wrapper<Iter1, Iter2>>>
2024-09-27 15:12:08 -04:00
{
public:
explicit pair_iterator(Iter1 begin1, Iter1 end1, Iter2 begin2, Iter2 end2):
iterator::iterator_storage_base<iterator::pair_wrapper<Iter1, Iter2>, pair_iterator<Iter1, Iter2>>
{iterator::pair_wrapper<Iter1, Iter2>{std::move(begin1), std::move(begin2)},
iterator::pair_wrapper<Iter1, Iter2>{std::move(end1), std::move(end2)}}
{}
};
template<typename Iter1, typename Iter2>
class pair_iterator<Iter1, Iter2,
std::enable_if_t<
2024-09-27 18:18:02 -04:00
blt::meta::is_bidirectional_iterator_category_v<blt::meta::lowest_iterator_category_t<Iter1, Iter2>>,
2024-09-27 15:12:08 -04:00
std::void_t<std::bidirectional_iterator_tag>>>
2024-09-27 18:18:02 -04:00
: public iterator::iterator_storage_reversible<iterator::pair_wrapper<Iter1, Iter2>, pair_iterator<Iter1, Iter2>, pair_iterator_rev<Iter1, Iter2>>,
public iterator::enumerator_convertible<pair_iterator<Iter1, Iter2>, enumerator<iterator::pair_wrapper<Iter1, Iter2>>>
2024-09-27 15:12:08 -04:00
{
public:
explicit pair_iterator(Iter1 begin1, Iter1 end1, Iter2 begin2, Iter2 end2):
iterator::iterator_storage_reversible<iterator::pair_wrapper<Iter1, Iter2>, pair_iterator<Iter1, Iter2>, pair_iterator_rev<Iter1, Iter2>>
{iterator::pair_wrapper<Iter1, Iter2>{std::move(begin1), std::move(begin2)},
iterator::pair_wrapper<Iter1, Iter2>{std::move(end1), std::move(end2)}}
{}
};
template<typename Iter1, typename Iter2>
class pair_iterator<Iter1, Iter2,
std::enable_if_t<
2024-09-27 18:18:02 -04:00
blt::meta::is_random_access_iterator_category_v<blt::meta::lowest_iterator_category_t<Iter1, Iter2>>,
2024-09-27 15:12:08 -04:00
std::void_t<std::random_access_iterator_tag>>>
2024-09-27 18:18:02 -04:00
: public iterator::iterator_storage_random_access<iterator::pair_wrapper<Iter1, Iter2>, pair_iterator<Iter1, Iter2>, pair_iterator_rev<Iter1, Iter2>>,
public iterator::enumerator_convertible<pair_iterator<Iter1, Iter2>, enumerator<iterator::pair_wrapper<Iter1, Iter2>>>
2024-09-27 15:12:08 -04:00
{
public:
explicit pair_iterator(Iter1 begin1, Iter1 end1, Iter2 begin2, Iter2 end2):
iterator::iterator_storage_random_access<iterator::pair_wrapper<Iter1, Iter2>, pair_iterator<Iter1, Iter2>, pair_iterator_rev<Iter1, Iter2>>
{iterator::pair_wrapper<Iter1, Iter2>{std::move(begin1), std::move(begin2)},
iterator::pair_wrapper<Iter1, Iter2>{std::move(end1), std::move(end2)}}
{}
};
template<typename Iter1, typename Iter2>
class pair_iterator_rev<Iter1, Iter2,
std::enable_if_t<
2024-09-27 18:18:02 -04:00
blt::meta::is_bidirectional_iterator_category_v<blt::meta::lowest_iterator_category_t<Iter1, Iter2>>,
2024-09-27 15:12:08 -04:00
std::void_t<std::bidirectional_iterator_tag>>>
2024-09-27 18:18:02 -04:00
: public iterator::iterator_storage_reversible_rev<std::reverse_iterator<iterator::pair_wrapper<Iter1, Iter2>>, pair_iterator<Iter1, Iter2>, pair_iterator_rev<Iter1, Iter2>>,
public iterator::enumerator_convertible<pair_iterator<Iter1, Iter2>, enumerator<iterator::pair_wrapper<Iter1, Iter2>>>
2024-09-27 15:12:08 -04:00
{
public:
explicit pair_iterator_rev(Iter1 begin1, Iter1 end1, Iter2 begin2, Iter2 end2):
iterator::iterator_storage_reversible_rev<std::reverse_iterator<iterator::pair_wrapper<Iter1, Iter2>>, pair_iterator<Iter1, Iter2>, pair_iterator_rev<Iter1, Iter2>>
2024-09-27 18:18:02 -04:00
{std::reverse_iterator<iterator::pair_wrapper<Iter1, Iter2>>{
iterator::pair_wrapper<Iter1, Iter2>{std::move(begin1), std::move(begin2)}},
std::reverse_iterator<iterator::pair_wrapper<Iter1, Iter2>>{
iterator::pair_wrapper<Iter1, Iter2>{std::move(end1), std::move(end2)}}}
2024-09-27 15:12:08 -04:00
{}
};
template<typename Iter1, typename Iter2>
class pair_iterator_rev<Iter1, Iter2,
std::enable_if_t<
2024-09-27 18:18:02 -04:00
blt::meta::is_random_access_iterator_category_v<blt::meta::lowest_iterator_category_t<Iter1, Iter2>>,
2024-09-27 15:12:08 -04:00
std::void_t<std::random_access_iterator_tag>>>
2024-09-27 18:18:02 -04:00
: public iterator::iterator_storage_random_access_rev<std::reverse_iterator<iterator::pair_wrapper<Iter1, Iter2>>, pair_iterator<Iter1, Iter2>, pair_iterator_rev<Iter1, Iter2>>,
public iterator::enumerator_convertible<pair_iterator<Iter1, Iter2>, enumerator<iterator::pair_wrapper<Iter1, Iter2>>>
2024-09-27 15:12:08 -04:00
{
public:
explicit pair_iterator_rev(Iter1 begin1, Iter1 end1, Iter2 begin2, Iter2 end2):
iterator::iterator_storage_random_access_rev<std::reverse_iterator<iterator::pair_wrapper<Iter1, Iter2>>, pair_iterator<Iter1, Iter2>, pair_iterator_rev<Iter1, Iter2>>
2024-09-27 18:18:02 -04:00
{std::reverse_iterator<iterator::pair_wrapper<Iter1, Iter2>>{
iterator::pair_wrapper<Iter1, Iter2>{std::move(begin1), std::move(begin2)}},
std::reverse_iterator<iterator::pair_wrapper<Iter1, Iter2>>{
iterator::pair_wrapper<Iter1, Iter2>{std::move(end1), std::move(end2)}}}
2024-09-27 15:12:08 -04:00
{}
};
// CTAD for pair iterators
template<typename Iter1, typename Iter2>
pair_iterator(Iter1, Iter1, Iter2, Iter2) -> pair_iterator<Iter1, Iter2>;
template<typename T, blt::size_t size>
static inline auto enumerate(const T(& container)[size])
{
return enumerator{&container[0], &container[size], size};
}
template<typename T, blt::size_t size>
static inline auto enumerate(T(& container)[size])
{
return enumerator{&container[0], &container[size], size};
}
template<typename T>
static inline auto enumerate(T& container)
{
return enumerator{container.begin(), container.end(), container.size()};
}
template<typename T>
static inline auto enumerate(T&& container)
{
return enumerator{container.begin(), container.end(), container.size()};
}
2024-09-26 02:53:11 -04:00
template<typename T>
static inline auto enumerate(const T& container)
{
return enumerator{container.begin(), container.end(), container.size()};
}
2024-09-27 18:18:02 -04:00
2024-09-27 15:12:08 -04:00
template<typename T, typename G>
static inline auto in_pairs(const T& container1, const G& container2)
{
return pair_iterator{container1.begin(), container1.end(), container2.begin(), container2.end()};
}
2024-09-27 18:18:02 -04:00
2024-09-27 15:12:08 -04:00
template<typename T, typename G>
static inline auto in_pairs(T& container1, G& container2)
{
return pair_iterator{container1.begin(), container1.end(), container2.begin(), container2.end()};
}
2024-09-27 18:18:02 -04:00
2024-09-27 15:12:08 -04:00
template<typename T, typename G, blt::size_t size>
static inline auto in_pairs(const T(& container1)[size], const G(& container2)[size])
{
return pair_iterator{&container1[0], &container1[size], &container2[0], &container2[size]};
}
2024-09-27 18:18:02 -04:00
2024-09-27 15:12:08 -04:00
template<typename T, typename G, blt::size_t size>
static inline auto in_pairs(T(& container1)[size], G(& container2)[size])
{
return pair_iterator{&container1[0], &container1[size], &container2[0], &container2[size]};
}
2024-09-27 18:18:02 -04:00
2024-09-27 15:12:08 -04:00
template<typename T, typename G>
static inline auto in_pairs(T&& container1, G&& container2)
{
return pair_iterator{container1.begin(), container1.end(), container2.begin(), container2.end()};
}
2024-09-26 02:53:11 -04:00
}
#endif //BLT_ITERATOR_H