Compare commits

...

3 Commits

Author SHA1 Message Date
Brett 1759b5f42f wow working iterators! 2024-10-01 16:46:15 -04:00
Brett bfcb357059 Merge remote-tracking branch 'refs/remotes/origin/main' 2024-10-01 14:41:01 -04:00
Brett 4caaecd850 abs in vec/mat 2024-10-01 14:40:27 -04:00
11 changed files with 374 additions and 983 deletions

View File

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

View File

@ -21,7 +21,10 @@
#include <type_traits>
#include <iterator>
#include <blt/std/types.h>
#include <blt/iterator/fwddecl.h>
#include <blt/meta/meta.h>
#include <blt/meta/iterator.h>
namespace blt::iterator
{
@ -37,10 +40,7 @@ namespace blt::iterator
base_wrapper operator--(int)
{
static_assert(std::is_same_v<typename Derived::iterator_category, std::bidirectional_iterator_tag> ||
std::is_same_v<typename Derived::iterator_category,
std::random_access_iterator_tag>,
"Iterator must allow random access");
static_assert(meta::is_bidirectional_or_better_category_v<typename Derived::iterator_category>, "Iterator must allow random access");
auto tmp = *this;
--*this;
return tmp;
@ -48,8 +48,7 @@ namespace blt::iterator
auto operator[](blt::ptrdiff_t n) const
{
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
static_assert(meta::is_random_access_iterator_category_v<typename Derived::iterator_category>, "Iterator must allow random access");
return *(*this + n);
}
@ -60,29 +59,25 @@ namespace blt::iterator
friend bool operator<(const base_wrapper& a, const base_wrapper& b)
{
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
static_assert(meta::is_random_access_iterator_category_v<typename Derived::iterator_category>, "Iterator must allow random access");
return b - a > 0;
}
friend bool operator>(const base_wrapper& a, const base_wrapper& b)
{
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
static_assert(meta::is_random_access_iterator_category_v<typename Derived::iterator_category>, "Iterator must allow random access");
return b < a;
}
friend bool operator>=(const base_wrapper& a, base_wrapper& b)
{
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
static_assert(meta::is_random_access_iterator_category_v<typename Derived::iterator_category>, "Iterator must allow random access");
return !(a < b); // NOLINT
}
friend bool operator<=(const base_wrapper& a, const base_wrapper& b)
{
static_assert(std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
static_assert(meta::is_random_access_iterator_category_v<typename Derived::iterator_category>, "Iterator must allow random access");
return !(a > b); // NOLINT
}
@ -127,6 +122,28 @@ namespace blt::iterator
{
return *this->iter;
}
// zip_wrapper& operator++()
// {
// return *this;
// }
//
// zip_wrapper& operator--()
// {
// return *this;
// }
//
// friend zip_wrapper operator+(const zip_wrapper& a, blt::ptrdiff_t n)
// {
// static_assert(std::is_same_v<iterator_category, std::random_access_iterator_tag>,
// "Iterator must allow random access");
// }
//
// friend zip_wrapper operator-(const zip_wrapper& a, blt::ptrdiff_t n)
// {
// static_assert(std::is_same_v<iterator_category, std::random_access_iterator_tag>,
// "Iterator must allow random access");
// }
};
namespace impl
@ -142,8 +159,17 @@ namespace blt::iterator
auto begin = d->begin();
auto end = d->end();
if constexpr (std::is_same_v<typename Derived::iterator_category, std::forward_iterator_tag> ||
std::is_same_v<typename Derived::iterator_category, std::bidirectional_iterator_tag>)
if constexpr (meta::is_random_access_iterator_category_v<typename Derived::iterator_category>)
{
// random access iterators can have math directly applied to them.
if constexpr (check)
{
return Derived{begin + std::min(static_cast<blt::ptrdiff_t>(n), std::distance(begin, end)), end};
} else
{
return Derived{begin + n, end};
}
} else
{
for (blt::size_t i = 0; i < n; i++)
{
@ -155,16 +181,6 @@ namespace blt::iterator
++begin;
}
return Derived{std::move(begin), std::move(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{begin + std::min(static_cast<blt::ptrdiff_t>(n), std::distance(begin, end)), end};
} else
{
return Derived{begin + n, end};
}
}
}
@ -183,15 +199,15 @@ namespace blt::iterator
template<bool check>
auto take_base(blt::size_t n)
{
static_assert(!std::is_same_v<typename Derived::iterator_category, std::input_iterator_tag>,
static_assert(!meta::is_input_iterator_category_v<typename Derived::iterator_category>,
"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> ||
std::is_same_v<typename Derived::iterator_category, std::bidirectional_iterator_tag>)
if constexpr (meta::is_forward_iterator_category_v<typename Derived::iterator_category> ||
meta::is_bidirectional_iterator_category_v<typename Derived::iterator_category>)
{
// with these guys we have to loop forward to move the iterators. an unfortunate inefficiency
auto new_end = begin;
@ -205,7 +221,7 @@ namespace blt::iterator
++new_end;
}
return Derived{std::move(begin), std::move(new_end)};
} else if constexpr (std::is_same_v<typename Derived::iterator_category, std::random_access_iterator_tag>)
} else if constexpr (meta::is_random_access_iterator_category_v<typename Derived::iterator_category>)
{
// random access iterators can have math directly applied to them.
if constexpr (check)
@ -232,7 +248,7 @@ namespace blt::iterator
public impl::skip_t<iterator_container<IterBase>>
{
public:
using iterator_category = typename IterBase::iterator_category;
using iterator_category = typename std::iterator_traits<IterBase>::iterator_category;
using iterator = IterBase;
iterator_container(IterBase begin, IterBase end): m_begin(std::move(begin)), m_end(std::move(end))
@ -242,15 +258,39 @@ namespace blt::iterator
iterator_container(Iter&& begin, Iter&& end): m_begin(std::forward<Iter>(begin)), m_end(std::forward<Iter>(end))
{}
auto rev()
auto rev() const
{
static_assert((std::is_same_v<typename IterBase::iterator_category, std::bidirectional_iterator_tag> ||
std::is_same_v<typename IterBase::iterator_category, std::random_access_iterator_tag>),
static_assert(meta::is_bidirectional_or_better_category_v<iterator_category>,
".rev() must be used with bidirectional (or better) iterators!");
return iterator_container<std::reverse_iterator<IterBase>>{std::reverse_iterator<IterBase>{end()},
std::reverse_iterator<IterBase>{begin()}};
}
template<typename... Iter>
auto zip(iterator_pair<Iter>... iterator_pairs) const
{
return zip_iterator_container(iterator_pair<decltype(begin())>{begin(), end()}, iterator_pairs...);
}
template<typename... Container>
auto zip(Container& ... containers) const
{
return zip_iterator_container(iterator_pair<decltype(begin())>{begin(), end()},
iterator_pair{containers.begin(), containers.end()}...);
}
template<typename... Container>
auto zip(const Container& ... containers) const
{
return zip_iterator_container(iterator_pair<decltype(begin())>{begin(), end()},
iterator_pair{containers.begin(), containers.end()}...);
}
auto enumerate() const
{
return enumerate_iterator_container{begin(), end(), static_cast<blt::size_t>(std::distance(begin(), end()))};
}
auto begin() const
{
return m_begin;

View File

@ -20,7 +20,6 @@
#define BLT_ITERATOR_ENUMERATE_H
#include <blt/iterator/common.h>
#include <blt/meta/meta.h>
#include <tuple>
namespace blt
@ -43,17 +42,110 @@ namespace blt
class enumerate_wrapper : public passthrough_wrapper<Iter, enumerate_wrapper<Iter>>
{
public:
using passthrough_wrapper<Iter, enumerate_wrapper<Iter>>::passthrough_wrapper;
enumerate_wrapper(blt::size_t index, Iter iter): passthrough_wrapper<Iter, enumerate_wrapper<Iter>>(std::move(iter)), index(index)
{}
using iterator_category = typename std::iterator_traits<Iter>::iterator_category;
using value_type = enumerate_item<meta::deref_return_t<Iter>>;
using difference_type = blt::ptrdiff_t;
using pointer = value_type;
using reference = value_type;
enumerate_item<meta::deref_return_t<Iter>> operator*() const
{
return *this->iter;
return {index, *this->iter};
}
enumerate_wrapper& operator++()
{
++index;
++this->iter;
return *this;
}
enumerate_wrapper& operator--()
{
--index;
--this->iter;
return *this;
}
friend enumerate_wrapper operator+(const enumerate_wrapper& a, blt::ptrdiff_t n)
{
static_assert(meta::is_random_access_iterator_v<Iter>, "Iterator must allow random access");
auto copy = a;
copy.index += n;
copy.iter = copy.iter + n;
return copy;
}
friend enumerate_wrapper operator-(const enumerate_wrapper& a, blt::ptrdiff_t n)
{
static_assert(meta::is_random_access_iterator_v<Iter>, "Iterator must allow random access");
auto copy = a;
copy.index -= n;
copy.iter = copy.iter - n;
return copy;
}
private:
blt::size_t index;
};
}
template<typename Iter>
class enumerate_iterator_container : public iterator::iterator_container<iterator::enumerate_wrapper<Iter>>
{
public:
using iterator::iterator_container<iterator::enumerate_wrapper<Iter>>::iterator_container;
enumerate_iterator_container(Iter begin, Iter end, blt::size_t size):
iterator::iterator_container<iterator::enumerate_wrapper<Iter>>(
iterator::enumerate_wrapper<Iter>{0, std::move(begin)}, iterator::enumerate_wrapper<Iter>{size, std::move(end)})
{}
};
template<typename Iter>
enumerate_iterator_container(Iter, Iter, blt::size_t) -> enumerate_iterator_container<Iter>;
namespace iterator::impl
{
template<typename Derived>
class enumerate_t
{
public:
auto enumerate()
{
auto* d = static_cast<Derived*>(this);
return enumerate_iterator_container{d->begin(), d->end(), static_cast<blt::size_t>(std::distance(d->begin(), d->end()))};
}
};
}
template<typename T>
static inline auto enumerate(T& container)
{
return enumerate_iterator_container{container.begin(), container.end(), container.size()};
}
template<typename T>
static inline auto enumerate(const T& container)
{
return enumerate_iterator_container{container.begin(), container.end(), container.size()};
}
template<typename T, blt::size_t size>
static inline auto enumerate(const T(& container)[size])
{
return enumerate_iterator_container{&container[0], &container[size], size};
}
template<typename T, blt::size_t size>
static inline auto enumerate(T(& container)[size])
{
return enumerate_iterator_container{&container[0], &container[size], size};
}
}
#endif //BLT_ITERATOR_ENUMERATE_H

View File

@ -0,0 +1,61 @@
#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_FWDDECL_H
#define BLT_ITERATOR_FWDDECL_H
namespace blt
{
template<typename... Iter>
class zip_iterator_container;
template<typename Iter>
class enumerate_iterator_container;
namespace iterator
{
template<typename Iter>
struct iterator_pair;
template<typename T>
struct enumerate_item;
template<typename Iter>
class enumerate_wrapper;
template<typename... Iter>
struct zip_wrapper;
namespace impl
{
template<typename Derived>
class skip_t;
template<typename Derived>
class take_t;
template<typename Derived>
class zip_t;
template<typename Derived>
class enumerate_t;
}
}
}
#endif //BLT_ITERATOR_FWDDECL_H

View File

@ -19,12 +19,9 @@
#ifndef BLT_ITERATOR_H
#define BLT_ITERATOR_H
#include <blt/std/types.h>
#include <blt/std/logging.h>
#include <blt/iterator/common.h>
#include <blt/iterator/zip.h>
#include <blt/meta/meta.h>
#include <blt/meta/iterator.h>
#include <blt/iterator/enumerate.h>
#include <type_traits>
#include <iterator>
#include <tuple>
@ -32,723 +29,30 @@
namespace blt
{
// forward declare useful types
template<typename Iter, typename = std::void_t<>>
class enumerator;
template<typename Iter, typename = std::void_t<>>
class enumerator_rev;
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;
template<typename... Iter>
class zip_iterator;
template<typename... Iter>
class zip_iterator_rev;
namespace iterator
template<typename T>
static inline auto iterate(T& container)
{
template<typename Iter, typename = std::void_t<>>
class enumerate_wrapper;
template<typename Iter1, typename Iter2, typename = std::void_t<>>
class pair_wrapper;
/**
* struct which is returned by the enumerator.
* @tparam T type to store.
*/
template<typename T>
struct enumerate_item
{
blt::size_t index;
T value;
};
/**
* 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.
*/
template<typename Iter1, typename Iter2>
class dual_iterator_base
{
public:
explicit dual_iterator_base(Iter1 iter1, Iter2 iter2): m_iter1(std::move(iter1)), m_iter2(std::move(iter2))
{}
friend bool operator==(const dual_iterator_base& a, const dual_iterator_base& b)
{
return a.m_iter1 == b.m_iter1;
}
friend bool operator!=(const dual_iterator_base& a, const dual_iterator_base& b)
{
return a.m_iter1 != b.m_iter1;
}
auto iter1() const
{
return m_iter1;
}
auto iter2() const
{
return m_iter2;
}
protected:
Iter1 m_iter1;
Iter2 m_iter2;
};
/**
* Base class for all enumerator iterators. Handles the deference (*) operator.
* @tparam Iter iterator type
*/
template<typename Iter>
class enumerate_iterator_base : public dual_iterator_base<Iter, blt::size_t>
{
public:
explicit enumerate_iterator_base(Iter iter, blt::size_t place = 0):
dual_iterator_base<Iter, blt::size_t>(std::move(iter), place)
{}
enumerate_item<blt::meta::deref_return_t<Iter>> operator*() const
{
return {this->m_iter2, *this->m_iter1};
}
};
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;
std::pair<blt::meta::deref_return_t<Iter1>, blt::meta::deref_return_t<Iter2>> operator*() const
{
return {*this->m_iter1, *this->m_iter2};
}
};
/**
* Forward iterator base class. Contains the ++ operator.
* @tparam Base iterator base type.
*/
template<typename Base>
class forward_iterator_base : public Base
{
public:
using Base::Base;
forward_iterator_base& operator++()
{
++this->m_iter1;
++this->m_iter2;
return *this;
}
forward_iterator_base operator++(int)
{
auto tmp = *this;
++*this;
return tmp;
}
};
/**
* Bidirectional iterator base class. Contains the -- operator.
* @tparam Base iterator base type.
*/
template<typename Base>
class bidirectional_iterator_base : public Base
{
public:
using Base::Base;
bidirectional_iterator_base& operator--()
{
--this->m_iter1;
--this->m_iter2;
return *this;
}
bidirectional_iterator_base operator--(int)
{
auto tmp = *this;
--*this;
return tmp;
}
};
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>
using pair_bidirectional_iterator = bidirectional_iterator_base<pair_forward_iterator<Iter1, Iter2>>;
/**
* Enumerator wrapper class specialization for forward iterators.
* @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;
};
/**
* 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<
blt::meta::is_forward_iterator_category_v<blt::meta::lowest_iterator_category_t<Iter1, Iter2>>,
std::void_t<std::forward_iterator_tag>>> : public pair_forward_iterator<Iter1, Iter2>
{
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.
* @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:
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;
};
/**
* Pair wrapper class for bidirectional iterators or random access iterators.
* @tparam Iter iterator type.
*/
template<typename Iter1, typename Iter2>
class pair_wrapper<Iter1, Iter2, std::enable_if_t<
blt::meta::is_bidirectional_or_better_category_v<blt::meta::lowest_iterator_category_t<Iter1, Iter2>>,
std::void_t<std::bidirectional_iterator_tag>>> : public pair_bidirectional_iterator<Iter1, Iter2>
{
public:
using iterator_category = std::bidirectional_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_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
*/
template<typename IterWrapper, typename CompleteClass>
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_;
}
/**
* 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;
return CompleteClass{begin.iter1(),
this->end_.iter1(),
begin.iter2(),
this->end_.iter2()};
}
/**
* 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;
return CompleteClass{this->begin_.iter1(),
end.iter1(),
this->begin_.iter2(),
end.iter2()};
}
protected:
IterWrapper begin_;
IterWrapper end_;
};
/**
* Reversible (bidirectional) base class storing the begin / end iterators.
* @tparam Iter iterator type.
* @tparam IterWrapper wrapper used to iterate.
* @tparam CompleteClass completed class returned from skip/take methods
* @tparam CompleteClassRev reverse version of CompleteClass, returned from rev
*/
template<typename IterWrapper, typename CompleteClass, typename CompleteClassRev>
class iterator_storage_reversible : public iterator_storage_base<IterWrapper, CompleteClass>
{
public:
explicit iterator_storage_reversible(IterWrapper begin, IterWrapper end):
iterator_storage_base<IterWrapper, CompleteClass>{std::move(begin), std::move(end)}
{}
/**
* Reverses the enumerators direction.
*/
auto rev() const
{
return CompleteClassRev{this->end_.iter1(),
this->begin_.iter1(),
this->end_.iter2(),
this->begin_.iter2()};
}
};
/**
* 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.
* @tparam Iter iterator type.
* @tparam IterWrapper wrapper used to iterate.
* @tparam CompleteClass completed class returned from skip/take methods
* @tparam CompleteClassRev reverse version of CompleteClass, returned from rev
*/
template<typename IterWrapper, typename CompleteClass, typename CompleteClassRev>
class iterator_storage_random_access : public iterator_storage_reversible<IterWrapper, CompleteClass, CompleteClassRev>
{
public:
using iterator_storage_reversible<IterWrapper, CompleteClass, CompleteClassRev>::iterator_storage_reversible;
auto skip(blt::size_t amount)
{
return CompleteClass{this->begin_.iter1() + amount,
this->end_.iter1(),
this->begin_.iter2() + amount,
this->end_.iter2()};
}
auto take(blt::size_t amount)
{
return CompleteClass{this->begin_.iter1(),
this->begin_.iter1() + amount,
this->begin_.iter2(),
this->begin_.iter2() + amount};
}
};
/**
* Reversible (bidirectional) base class for storing the begin/end iterators, operates in reverse for reverse iteration.
* @tparam Iter iterator type.
* @tparam IterWrapper wrapper used to iterate (std::reverse_iterator<enumerate_wrapper>).
* @tparam CompleteClass completed class returned from skip/take methods
* @tparam CompleteClassRev reverse version of CompleteClass, returned from rev
*/
template<typename IterWrapper, typename CompleteClass, typename CompleteClassRev>
class iterator_storage_reversible_rev : public iterator_storage_reversible<IterWrapper, CompleteClass, CompleteClassRev>
{
public:
using iterator_storage_reversible<IterWrapper, CompleteClass, CompleteClassRev>::iterator_storage_reversible;
auto rev() const
{
return CompleteClass{this->end_.base().iter1(),
this->begin_.base().iter1(),
this->end_.base().iter2(),
this->begin_.base().iter2()};
}
auto skip(blt::size_t amount)
{
auto begin = this->begin_.base();
for (blt::size_t i = 0; i < amount; i++)
--begin;
return CompleteClassRev{begin.iter1(),
this->end_.base().iter1(),
begin.iter2(),
this->end_.base().iter2()};
}
auto take(blt::size_t amount)
{
auto end = this->begin_.base();
for (blt::size_t i = 0; i < amount; i++)
--end;
return CompleteClassRev{
this->begin_.base().iter1(),
end.iter1(),
this->begin_.base().iter2(),
end.iter2()};
}
};
/**
* 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.
* @tparam Iter iterator type.
* @tparam IterWrapper wrapper used to iterate (std::reverse_iterator<enumerate_wrapper>).
* @tparam CompleteClass completed class returned from skip/take methods
* @tparam CompleteClassRev reverse version of CompleteClass, returned from rev
*/
template<typename IterWrapper, typename CompleteClass, typename CompleteClassRev>
class iterator_storage_random_access_rev : public iterator_storage_reversible_rev<IterWrapper, CompleteClass, CompleteClassRev>
{
public:
using iterator_storage_reversible_rev<IterWrapper, CompleteClass, CompleteClassRev>::iterator_storage_reversible_rev;
auto skip(blt::size_t amount)
{
return CompleteClassRev{this->begin_.base().iter1() - amount,
this->end_.base().iter1(),
this->begin_.base().iter2() - amount,
this->end_.base().iter2()};
}
auto take(blt::size_t amount)
{
return CompleteClassRev{this->begin_.base().iter1(),
this->begin_.base().iter1() - amount,
this->begin_.base().iter2(),
this->begin_.base().iter2() - amount};
}
};
/**
* 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()))};
}
};
return iterator::iterator_container<decltype(container.begin())>{container.begin(), container.end()};
}
/**
* 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>>>
: public iterator::iterator_storage_base<iterator::enumerate_wrapper<Iter>, enumerator<Iter>>
template<typename T>
static inline auto iterate(const T& container)
{
public:
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}}
{}
};
/**
* 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>>>
: public iterator::iterator_storage_reversible<iterator::enumerate_wrapper<Iter>, enumerator<Iter>, enumerator_rev<Iter>>
{
public:
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}}
{}
};
/**
* 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>>>
: public iterator::iterator_storage_random_access<iterator::enumerate_wrapper<Iter>, enumerator<Iter>, enumerator_rev<Iter>>
{
public:
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}}
{}
};
/**
* 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>>>
: public iterator::iterator_storage_reversible_rev<std::reverse_iterator<iterator::enumerate_wrapper<Iter>>, enumerator<Iter>, enumerator_rev<Iter>>
{
public:
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}}}
{}
};
/**
* 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>>>
: public iterator::iterator_storage_random_access_rev<std::reverse_iterator<iterator::enumerate_wrapper<Iter>>, enumerator<Iter>, enumerator_rev<Iter>>
{
public:
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}}}
{}
};
// 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>;
template<typename Iter1, typename Iter2>
class pair_iterator<Iter1, Iter2,
std::enable_if_t<
blt::meta::is_forward_iterator_category_v<blt::meta::lowest_iterator_category_t<Iter1, Iter2>>,
std::void_t<std::forward_iterator_tag>>>
: 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>>>
{
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<
blt::meta::is_bidirectional_iterator_category_v<blt::meta::lowest_iterator_category_t<Iter1, Iter2>>,
std::void_t<std::bidirectional_iterator_tag>>>
: 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>>>
{
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<
blt::meta::is_random_access_iterator_category_v<blt::meta::lowest_iterator_category_t<Iter1, Iter2>>,
std::void_t<std::random_access_iterator_tag>>>
: 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>>>
{
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<
blt::meta::is_bidirectional_iterator_category_v<blt::meta::lowest_iterator_category_t<Iter1, Iter2>>,
std::void_t<std::bidirectional_iterator_tag>>>
: 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>>>
{
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>>
{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)}}}
{}
};
template<typename Iter1, typename Iter2>
class pair_iterator_rev<Iter1, Iter2,
std::enable_if_t<
blt::meta::is_random_access_iterator_category_v<blt::meta::lowest_iterator_category_t<Iter1, Iter2>>,
std::void_t<std::random_access_iterator_tag>>>
: 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>>>
{
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>>
{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)}}}
{}
};
// 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};
return iterator::iterator_container<decltype(container.begin())>{container.begin(), container.end()};
}
template<typename T, blt::size_t size>
static inline auto enumerate(T(& container)[size])
static inline auto iterate(const T(& container)[size])
{
return enumerator{&container[0], &container[size], size};
return iterator::iterator_container<decltype(container.begin())>{&container[0], &container[size]};
}
template<typename T>
static inline auto enumerate(T& container)
template<typename T, blt::size_t size>
static inline auto iterate(T(& container)[size])
{
return enumerator{container.begin(), container.end(), container.size()};
return iterator::iterator_container<decltype(container.begin())>{&container[0], &container[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(const T& container)
{
return enumerator{container.begin(), container.end(), container.size()};
}
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()};
}
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()};
}
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]};
}
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]};
}
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()};
}
}
#endif //BLT_ITERATOR_H

View File

@ -20,8 +20,6 @@
#define BLT_ITERATOR_ZIP
#include <blt/iterator/common.h>
#include <blt/meta/meta.h>
#include <blt/meta/iterator.h>
#include <tuple>
namespace blt
@ -57,21 +55,20 @@ namespace blt
zip_wrapper& operator--()
{
static_assert(meta::is_bidirectional_or_better_category_v<iterator_category>, "Iterator must be bidirectional or better!");
std::apply([](auto& ... i) { ((--i), ...); }, this->iter);
return *this;
}
friend zip_wrapper operator+(const zip_wrapper& a, blt::ptrdiff_t n)
{
static_assert(std::is_same_v<iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
static_assert(meta::is_random_access_iterator_category_v<iterator_category>, "Iterator must allow random access");
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)
{
static_assert(std::is_same_v<iterator_category, std::random_access_iterator_tag>,
"Iterator must allow random access");
static_assert(meta::is_random_access_iterator_category_v<iterator_category>, "Iterator must allow random access");
return std::apply([n](auto& ... i) { return zip_wrapper((i - n)...); }, a.iter);
}
@ -97,56 +94,44 @@ namespace blt
return min;
}
};
// template<typename Iter>
// struct zip_wrapper<Iter> : public Iter
// {
// using iterator_category = typename std::iterator_traits<Iter>::iterator_category;
// using value_type = typename std::iterator_traits<Iter>::value_type;
// using difference_type = typename std::iterator_traits<Iter>::difference_type;
// using pointer = typename std::iterator_traits<Iter>::pointer;
// using reference = typename std::iterator_traits<Iter>::reference;
//
// using Iter::Iter;
// };
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>
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_container : public iterator::iterator_container<iterator::zip_wrapper<Iter...>>
{
public:
using iterator::iterator_container<iterator::zip_wrapper<Iter...>>::iterator_container;
explicit zip_iterator_container(iterator_pair<Iter>... iterator_pairs):
explicit zip_iterator_container(iterator::iterator_pair<Iter>... iterator_pairs):
iterator::iterator_container<iterator::zip_wrapper<Iter...>>(iterator::zip_wrapper<Iter...>{std::move(iterator_pairs.begin)...},
iterator::zip_wrapper<Iter...>{std::move(iterator_pairs.end)...})
{}
};
namespace impl
namespace iterator::impl
{
template<typename Derived>
class zip_t
{
public:
template<typename... Iter>
auto zip(iterator_pair<Iter>... iterator_pairs)
auto zip(iterator::iterator_pair<Iter>... iterator_pairs)
{
auto* d = static_cast<Derived*>(this);
return zip_iterator_container(iterator_pair<decltype(d->begin())>{d->begin(), d->end()}, iterator_pairs...);
return zip_iterator_container(iterator::iterator_pair<decltype(d->begin())>{d->begin(), d->end()}, iterator_pairs...);
}
};
}
@ -156,7 +141,7 @@ namespace blt
*/
template<typename... Iter>
zip_iterator_container(iterator_pair<Iter>...) -> zip_iterator_container<Iter...>;
zip_iterator_container(iterator::iterator_pair<Iter>...) -> zip_iterator_container<Iter...>;
template<typename... Iter>
zip_iterator_container(std::initializer_list<Iter>...) -> zip_iterator_container<Iter...>;
@ -169,8 +154,43 @@ namespace blt
template<typename... Container>
auto zip(Container& ... container)
{
return zip_iterator_container{iterator_pair{container.begin(), container.end()}...};
return zip_iterator_container{iterator::iterator_pair{std::begin(container), std::end(container)}...};
}
template<typename... Container>
auto zip(const Container& ... container)
{
return zip_iterator_container{iterator::iterator_pair{std::begin(container), std::end(container)}...};
}
template<typename T, typename G>
static inline auto in_pairs(const T& container1, const G& container2)
{
return zip_iterator_container{iterator::iterator_pair{container1.begin(), container1.end()},
iterator::iterator_pair{container2.begin(), container2.end()}};
}
template<typename T, typename G>
static inline auto in_pairs(T& container1, G& container2)
{
return zip_iterator_container{iterator::iterator_pair{container1.begin(), container1.end()},
iterator::iterator_pair{container2.begin(), container2.end()}};
}
template<typename T, typename G, blt::size_t size>
static inline auto in_pairs(const T(& container1)[size], const G(& container2)[size])
{
return zip_iterator_container{iterator::iterator_pair{&container1[0], &container1[size]},
iterator::iterator_pair{&container2[0], &container2[size]}};
}
template<typename T, typename G, blt::size_t size>
static inline auto in_pairs(T(& container1)[size], G(& container2)[size])
{
return zip_iterator_container{iterator::iterator_pair{&container1[0], &container1[size]},
iterator::iterator_pair{&container2[0], &container2[size]}};
}
}
#endif //BLT_ITERATOR_ZIP

View File

@ -122,7 +122,7 @@ namespace blt
return *this;
}
constexpr generalized_matrix<T, columns, rows> transpose() const
[[nodiscard]] constexpr generalized_matrix<T, columns, rows> transpose() const
{
generalized_matrix<T, columns, rows> mat;
@ -135,7 +135,7 @@ namespace blt
return mat;
}
T magnitude() const
[[nodiscard]] constexpr T magnitude() const
{
T ret{};
for (blt::u32 i = 0; i < columns; i++)
@ -146,7 +146,7 @@ namespace blt
return std::sqrt(ret);
}
matrix_t normalize() const
[[nodiscard]] constexpr matrix_t normalize() const
{
auto mag = magnitude();
matrix_t mat = *this;
@ -155,6 +155,14 @@ namespace blt
return mat / mag;
}
[[nodiscard]] constexpr matrix_t abs() const
{
matrix_t copy = *this;
for (auto& v : copy.data)
v = v.abs();
return copy;
}
constexpr inline const blt::vec<T, rows>& operator[](u32 column) const
{
return data[column];
@ -179,7 +187,7 @@ namespace blt
* Takes a value stored across a row, taking one from each column in the specified row
* @param row the row to extract from. defaults to the first row
*/
constexpr inline vec<T, columns> vec_from_column_row(blt::u32 row = 0) const
[[nodiscard]] constexpr inline vec<T, columns> vec_from_column_row(blt::u32 row = 0) const
{
vec<T, columns> ret;
for (blt::u32 j = 0; j < columns; j++)
@ -187,21 +195,6 @@ namespace blt
return ret;
}
/**
* Assign to this matrix from the row information in each column of a matrix
* Where columns can be assigned directly from each-other, row stored data must be assigned this way
* this was hacked together for an assignment and a better way is a TODO;
* @param to_column column in this matrix to assign to
* @param row the row place that the value is store in to assign from. Defaults to the first element in each column
*/
template<blt::u32 p>
constexpr inline matrix_t& assign_to_column_from_column_rows(generalized_matrix<T, p, rows> mat, blt::u32 to_column, blt::u32 row = 0)
{
for (blt::u32 j = 0; j < rows; j++)
data[to_column][j] = mat[j][row];
return *this;
}
constexpr inline matrix_t& operator+=(const matrix_t& other)
{
for (blt::u32 i = 0; i < columns; i++)
@ -363,6 +356,16 @@ namespace blt
{
return !(left == right);
}
auto begin() const
{
return data.begin();
}
auto end() const
{
return data.end();
}
private:
blt::vec<T, rows> data[columns];

View File

@ -124,6 +124,14 @@ namespace blt
return elements[3];
}
[[nodiscard]] constexpr inline vec<T, size> abs() const
{
auto copy = *this;
for (auto& v : copy.elements)
v = std::abs(v);
return copy;
}
[[nodiscard]] constexpr inline T magnitude() const
{
T total = 0;

View File

@ -122,7 +122,8 @@ namespace blt::meta
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;

View File

@ -19,151 +19,6 @@
namespace blt
{
namespace itr
{
template<typename Begin, typename End>
class itr_container
{
public:
itr_container(Begin&& begin, End&& end): begin_(std::forward<Begin>(begin)), end_(std::forward<End>(end))
{}
Begin begin()
{
return begin_;
}
End end()
{
return end_;
}
private:
Begin begin_;
End end_;
};
// TODO: cleanup! all of this! add support for reversing
template<typename C1_TYPE, typename C2_TYPE>
class pair_iterator
{
public:
using c1_ref = blt::meta::deref_return_t<C1_TYPE>;
using c2_ref = blt::meta::deref_return_t<C2_TYPE>;
using iterator_category = std::forward_iterator_tag;
using value_type = std::pair<c1_ref, c2_ref>;
using difference_type = blt::ptrdiff_t;
using pointer = void*;
using reference = value_type&;
using const_reference = const value_type&;
explicit pair_iterator(C1_TYPE c1, C2_TYPE c2): current_c1_iter(c1), current_c2_iter(c2)
{}
pair_iterator& operator++()
{
++current_c1_iter;
++current_c2_iter;
return *this;
}
bool operator==(pair_iterator other) const
{
return current_c1_iter == other.current_c1_iter && current_c2_iter == other.current_c2_iter;
}
bool operator!=(pair_iterator other) const
{
return current_c1_iter != other.current_c1_iter || current_c2_iter != other.current_c2_iter;
}
value_type operator*() const
{
return {*current_c1_iter, *current_c2_iter};
};
value_type operator*()
{
return {*current_c1_iter, *current_c2_iter};
};
private:
C1_TYPE current_c1_iter;
C2_TYPE current_c2_iter;
};
}
template<typename Begin, typename End>
static inline auto iterate(Begin&& begin, End&& end)
{
return itr::itr_container<Begin, End>{std::forward<Begin>(begin), std::forward<End>(end)};
}
template<typename Begin, typename End>
static inline auto reverse_iterate(Begin&& begin, End&& end)
{
return itr::itr_container{std::reverse_iterator(std::forward<Begin>(end)), std::reverse_iterator(std::forward<End>(begin))};
}
template<typename C1_ITER, typename C2_ITER, template<typename, typename> typename iterator = itr::pair_iterator>
class pair_enumerator
{
public:
explicit pair_enumerator(C1_ITER c1_begin, C1_ITER c1_end, C2_ITER c2_begin, C2_ITER c2_end):
begin_(std::move(c1_begin), std::move(c2_begin)), end_(std::move(c1_end), std::move(c2_end))
{
auto size_c1 = c1_end - c1_begin;
auto size_c2 = c2_end - c2_begin;
if (size_c1 != size_c2)
throw std::runtime_error("Iterator sizes don't match!");
}
iterator<C1_ITER, C2_ITER> begin()
{
return begin_;
}
iterator<C1_ITER, C2_ITER> end()
{
return end_;
}
private:
iterator<C1_ITER, C2_ITER> begin_;
iterator<C1_ITER, C2_ITER> end_;
};
// template<typename T, typename G>
// static inline auto in_pairs(const T& container1, const G& container2)
// {
// return pair_enumerator{container1.begin(), container1.end(), container2.begin(), container2.end()};
// }
//
// template<typename T, typename G>
// static inline auto in_pairs(T& container1, G& container2)
// {
// return pair_enumerator{container1.begin(), container1.end(), container2.begin(), container2.end()};
// }
//
// template<typename T, typename G, blt::size_t size>
// static inline auto in_pairs(const T(& container1)[size], const G(& container2)[size])
// {
// return pair_enumerator{&container1[0], &container1[size], &container2[0], &container2[size]};
// }
//
// template<typename T, typename G, blt::size_t size>
// static inline auto in_pairs(T(& container1)[size], G(& container2)[size])
// {
// return pair_enumerator{&container1[0], &container1[size], &container2[0], &container2[size]};
// }
//
// template<typename T, typename G>
// static inline auto in_pairs(T&& container1, G&& container2)
// {
// return pair_enumerator{container1.begin(), container1.end(), container2.begin(), container2.end()};
// }
template<typename T>
struct range
{
@ -239,41 +94,6 @@ namespace blt
}
};
template<typename I>
class itr_offset
{
private:
I begin_;
I end_;
public:
template<typename T>
itr_offset(I begin, I end, T offset): begin_(begin), end_(end)
{
for (T t = 0; t < offset; t++)
++begin_;
}
template<typename C, typename T>
itr_offset(C& container, T offset): begin_(container.begin()), end_(container.end())
{
for (T t = 0; t < offset; t++)
++begin_;
}
auto begin()
{
return begin_;
}
auto end()
{
return end_;
}
};
template<typename C, typename T>
itr_offset(C, T) -> itr_offset<typename C::iterator>;
inline constexpr std::size_t dynamic_extent = std::numeric_limits<std::size_t>::max();
template<typename T, std::size_t extent = dynamic_extent>

View File

@ -23,6 +23,7 @@
#include <blt/iterator/zip.h>
#include <blt/iterator/enumerate.h>
#include <blt/iterator/iterator.h>
#include <blt/std/ranges.h>
#include <blt/format/boxing.h>
#include <array>
#include <forward_list>
@ -122,6 +123,10 @@ void test_enumerate()
void test_pairs()
{
blt::log_box_t box(std::cout, "Pairs Tests", 25);
for (auto [a1, a2] : blt::in_pairs(array_1, array_2))
{
BLT_TRACE_STREAM << a1 << " : " << a2 << "\n";
}
}
void test_zip()
@ -158,6 +163,41 @@ void test_zip()
}
}
void test_iterate()
{
blt::log_box_t box(std::cout, "Iterate Tests", 25);
for (auto v : blt::iterate(array_1))
{
BLT_TRACE_STREAM << "Element: " << v << "\n";
}
BLT_TRACE("================================");
for (auto v : blt::iterate(array_1).skip(5))
{
BLT_TRACE_STREAM << "Element: " << v << "\n";
}
BLT_TRACE("================================");
for (auto v : blt::iterate(array_1).take(5))
{
BLT_TRACE_STREAM << "Element: " << v << "\n";
}
BLT_TRACE("================================");
for (auto v : blt::iterate(array_1).rev())
{
BLT_TRACE_STREAM << "Element: " << v << "\n";
}
BLT_TRACE("================================");
for (auto [a, b] : blt::iterate(array_1).zip(list_1))
{
BLT_TRACE_STREAM << "Zip: " << a << " " << b << "\n";
}
BLT_TRACE("================================");
for (auto [i, data] : blt::iterate(array_1).zip(list_1).skip(3).take(4).enumerate())
{
auto [a, b] = data;
BLT_TRACE_STREAM << "Zip (" << i << "): " << a << " " << b << "\n";
}
}
int main()
{
test_enumerate();
@ -165,4 +205,6 @@ int main()
test_pairs();
std::cout << std::endl;
test_zip();
std::cout << std::endl;
test_iterate();
}