Rework ranges library

Better matches the standard library, and iterators moved to their own file
feature/simd
Andrew Cassidy 2 years ago
parent 9b3c1d0ca3
commit c92d58d115

@ -5,6 +5,7 @@ ColumnLimit: 120
AllowShortBlocksOnASingleLine: Always
AllowShortFunctionsOnASingleLine: All
AlwaysBreakTemplateDeclarations: MultiLine
#RequiresClausePositionStyle: SingleLine # requires Clang 15 :(
#AlignConsecutiveDeclarations: true
---

@ -24,6 +24,7 @@
#include <numeric>
#include <xsimd/xsimd.hpp>
#include "util/iterator.h"
#include "util/map.h"
#include "util/math.h"
#include "util/ranges.h"
@ -417,10 +418,10 @@ class Matrix : public VecBase<std::conditional_t<N == 1, T, VecBase<T, T, N>>, T
}
protected:
class column_iterator : public index_iterator_base<column_iterator> {
class column_iterator : public index_iterator_base<column_iterator, column_type> {
public:
using value_type = column_type;
using base = index_iterator_base<column_iterator>;
using base = index_iterator_base<column_iterator, column_type>;
column_iterator(const Matrix *matrix = nullptr, int index = 0) : base(index), _matrix(matrix){};
@ -435,10 +436,10 @@ class Matrix : public VecBase<std::conditional_t<N == 1, T, VecBase<T, T, N>>, T
const Matrix *_matrix;
};
template <typename V> class linear_iterator : public index_iterator_base<linear_iterator<V>> {
template <typename V> class linear_iterator : public index_iterator_base<linear_iterator<V>, T> {
public:
using value_type = column_type;
using base = index_iterator_base<linear_iterator<V>>;
using value_type = T;
using base = index_iterator_base<linear_iterator<V>, T>;
linear_iterator(V *matrix = nullptr, int index = 0) : base(index), _matrix(matrix){};

@ -26,6 +26,7 @@
#include <numeric>
#include <type_traits>
#include "iterator.h"
#include "util/math.h"
#include "util/ranges.h"
@ -125,7 +126,7 @@ size_t unpack_into(P packed, OI begin, OI end, WI widths, bool little_endian = t
template <typename P, typename OR, typename WR>
requires std::unsigned_integral<P> && range<OR> && range<WR>
size_t unpack_into(P packed, OR &dest, const WR &widths, bool little_endian = true) {
assert(distance(widths) == distance(dest));
assert(size(widths) == size(dest));
return unpack_into(packed, dest.begin(), dest.end(), widths.begin(), little_endian);
}
@ -201,7 +202,7 @@ std::array<U, N> unpack(P packed, const std::array<size_t, N> &widths, bool litt
template <typename U, size_t N, typename P, typename WR>
requires std::unsigned_integral<P> && range<WR>
std::array<U, N> unpack(P packed, const WR &widths, bool little_endian = true) {
assert(distance(widths) == N);
assert(size(widths) == N);
return unpack<U, N>(packed, widths.begin(), little_endian);
}
@ -273,7 +274,7 @@ inline constexpr P pack(II start, II end, WI widths, bool little_endian = true)
template <typename P, typename IR, typename WR>
requires std::unsigned_integral<P> && range<IR> && range<WR>
inline constexpr P pack(IR r, WR widths, bool little_endian = true) {
assert(distance(widths) == distance(r));
assert(size(widths) == size(r));
return pack<P>(r.begin(), r.end(), widths.start(), little_endian);
}

@ -65,4 +65,4 @@ constexpr inline auto operator^=(E& a, E b) noexcept -> E& {
a = a ^ b;
return a;
}
} // namespace quicktex::util
} // namespace quicktex

@ -0,0 +1,146 @@
/* Quicktex Texture Compression Library
Copyright (C) 2021 Andrew Cassidy <drewcassidy@me.com>
Partially derived from rgbcx.h written by Richard Geldreich <richgel99@gmail.com>
and licenced under the public domain
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
namespace quicktex {
namespace detail {
template <class R> using subs_value_t = std::remove_reference_t<decltype(std::declval<R &>()[0])>;
}
template <typename D, typename T> class index_iterator_base {
public:
using value_type = T;
using size_type = int;
using difference_type = int;
D &operator++() {
_index++;
return static_cast<D &>(*this);
}
D operator++(int) {
D old = static_cast<D &>(*this);
_index++;
return old;
}
D &operator--() {
_index--;
return static_cast<D &>(*this);
}
D operator--(int) {
D old = static_cast<D &>(*this);
_index--;
return old;
}
D operator+(difference_type rhs) const {
D d = static_cast<const D &>(*this);
d._index += rhs;
return d;
}
D operator-(difference_type rhs) const {
D d = static_cast<const D &>(*this);
d._index -= rhs;
return d;
}
D &operator+=(difference_type rhs) {
*this = *this + rhs;
return *this;
}
D &operator-=(difference_type rhs) {
*this = *this - rhs;
return *this;
}
difference_type operator-(const D &rhs) const { return (difference_type)_index - rhs._index; }
friend D operator+(difference_type lhs, const D &rhs) { return rhs + lhs; }
friend auto operator<=>(const D &lhs, const D &rhs) { return lhs._index <=> rhs._index; }
T &operator[](difference_type i) { return *(static_cast<D &>(*this) + i); }
T &operator[](difference_type i) const { return *(static_cast<const D &>(*this) + i); }
protected:
int _index;
private:
friend D;
index_iterator_base(size_t index = 0) : _index(index) {}
};
template <typename R>
requires requires(const R &r) { r[0]; }
class index_iterator : public index_iterator_base<index_iterator<R>, detail::subs_value_t<R>> {
public:
using base = index_iterator_base<index_iterator<R>, detail::subs_value_t<R>>;
using typename base::difference_type;
using typename base::size_type;
using typename base::value_type;
index_iterator() : base(0), _range(nullptr) {}
index_iterator(R &range, int index) : base(index), _range(&range) {}
value_type &operator*() const {
assert(_range != nullptr);
assert(this->_index >= 0);
assert(this->_index < (size_type)_range->size());
return (*_range)[this->_index];
}
value_type *operator->() const { return &(this->operator*()); }
friend bool operator==(const index_iterator &lhs, const index_iterator &rhs) {
return (lhs._range == rhs._range) && (lhs._index == rhs._index);
}
private:
R *_range;
};
template <typename T> class const_iterator : public index_iterator_base<const_iterator<T>, const T> {
public:
using base = index_iterator_base<const_iterator<T>, const T>;
using typename base::difference_type;
using typename base::size_type;
using typename base::value_type;
const_iterator() : base(0), _value(T{}) {}
const_iterator(T value, int index = 0) : base(index), _value(value) {}
value_type &operator*() const { return _value; }
value_type *operator->() const { return &_value; }
friend bool operator==(const const_iterator &lhs, const const_iterator &rhs) {
return (lhs._value == rhs._value) && (lhs._index == rhs._index);
}
private:
T _value;
};
// const_iterator is guaranteed to be a random access iterator. it is not writable for obvious reasons
static_assert(std::random_access_iterator<const_iterator<int>>);
// index_iterator satisfied forward_iterator
static_assert(std::random_access_iterator<index_iterator<std::array<int, 4>>>);
} // namespace quicktex

@ -30,7 +30,7 @@ namespace quicktex {
namespace detail {
template <typename T>
concept simdable = subscriptable_range<T> && std::contiguous_iterator<decltype(std::declval<T>().begin())> &&
concept simdable = random_access_range<T> && std::contiguous_iterator<decltype(std::declval<T>().begin())> &&
std::is_arithmetic_v<range_value_t<T>>;
template <typename T, bool serial = false> struct chunker_impl {};
@ -74,7 +74,7 @@ struct chunker_impl<T, serial> {
};
template <typename T, bool serial>
requires subscriptable_range<T> && (!simdable<T> || serial)
requires random_access_range<T> && (!simdable<T> || serial)
struct chunker_impl<T, serial> {
// range with data that cant be SIMDed
static constexpr size_t steps = 1;

@ -29,171 +29,46 @@
#include <numeric>
#include <string>
#include <type_traits>
#include <vector>
namespace quicktex {
// std::ranges::range is not usable by default in libc++ 13
// std::ranges is not usable by default in libc++ 13
template <class T>
concept range = requires(T &t) {
t.begin();
t.end();
};
template <class T>
concept sized = requires(T &t) { std::size(t); };
using std::size;
template <range T> constexpr auto size(const T &range) { return std::distance(range.begin(), range.end()); }
template <class T>
concept sized_range = range<T> && sized<T>;
concept sized_range = range<T> && requires(T &t) { size(t); };
template <typename T>
concept const_subscriptable = requires(T &t) {
t[0];
std::size(t);
};
template <class R> using iterator_t = decltype(std::declval<R &>().begin());
template <class R> using sentinel_t = decltype(std::declval<R &>().end());
template <class R> using range_size_t = decltype(size(std::declval<R &>()));
template <class R> using range_difference_t = std::iter_difference_t<iterator_t<R>>;
template <class R> using range_value_t = std::iter_value_t<iterator_t<R>>;
template <class R> using range_reference_t = std::iter_reference_t<iterator_t<R>>;
template <class R> using range_rvalue_reference_t = std::iter_rvalue_reference_t<iterator_t<R>>;
template <class T>
concept subscriptable = const_subscriptable<T> && requires(T &t) {
t[0] = std::declval<decltype(t[0])>(); // subscript is assignable
};
template <class R>
concept input_range = range<R> && std::input_iterator<iterator_t<R>>;
template <typename T>
concept subscriptable_range = sized_range<T> && subscriptable<T>;
template <class R, typename T>
concept output_range = range<R> && (std::output_iterator<iterator_t<R>, T>);
template <typename T> struct range_value { using type = void; };
template <class R>
concept forward_range = range<R> && std::forward_iterator<iterator_t<R>>;
template <typename T>
requires requires(T &t) { std::begin(t); }
struct range_value<T> {
using type = std::iter_value_t<decltype((std::declval<T>()).begin())>;
};
template <class R>
concept bidirectional_range = range<R> && std::bidirectional_iterator<iterator_t<R>>;
template <typename T> using range_value_t = typename range_value<T>::type;
template <class R>
concept random_access_range = range<R> && std::random_access_iterator<iterator_t<R>>;
// some quick inline checks
static_assert(const_subscriptable<const std::array<int, 4>>); // const array can be subscripted
static_assert(!subscriptable<const std::array<int, 4>>); // const array cannot be assigned to
static_assert(subscriptable_range<std::array<int, 4>>); // array is subscriptable, sized, and has begin() and end()
static_assert(sized_range<std::initializer_list<int>>); // initializer list is a range and has size()
static_assert(std::same_as<range_value_t<int>, void>);
template <class R>
concept contiguous_range = range<R> && std::contiguous_iterator<iterator_t<R>>;
template <class T>
requires range<T>
size_t distance(T range) {
return std::distance(range.begin(), range.end());
}
template <typename D> class index_iterator_base {
public:
typedef long long difference_type;
D &operator++() {
_index++;
return static_cast<D &>(*this);
}
D operator++(int) {
D old = static_cast<D &>(*this);
_index++;
return old;
}
D &operator--() {
_index--;
return static_cast<D &>(*this);
}
D operator--(int) {
D old = static_cast<D &>(*this);
_index--;
return old;
}
D operator+(difference_type rhs) const {
D d = static_cast<const D &>(*this);
d._index += rhs;
return d;
}
D operator-(difference_type rhs) const {
D d = static_cast<const D &>(*this);
d._index -= rhs;
return d;
}
D &operator+=(difference_type rhs) {
*this = *this + rhs;
return *this;
}
D &operator-=(difference_type rhs) {
*this = *this - rhs;
return *this;
}
difference_type operator-(const D &rhs) const { return (difference_type)_index - rhs._index; }
friend D operator+(difference_type lhs, const D &rhs) { return rhs + lhs; }
friend auto operator<=>(const D &lhs, const D &rhs) { return lhs._index <=> rhs._index; }
auto &operator[](difference_type i) { return *(static_cast<D &>(*this) + i); }
auto operator[](difference_type i) const { return *(static_cast<const D &>(*this) + i); }
protected:
size_t _index;
private:
friend D;
index_iterator_base(size_t index = 0) : _index(index) {}
};
template <typename T> class const_iterator : public index_iterator_base<const_iterator<T>> {
public:
typedef index_iterator_base<const_iterator<T>> base;
typedef long long difference_type;
typedef T value_type;
const_iterator() : base(0), _value(T{}) {}
const_iterator(T value, size_t index = 0) : base(index), _value(value) {}
T operator*() const { return _value; }
const T *operator->() const { return &_value; }
friend bool operator==(const const_iterator &lhs, const const_iterator &rhs) {
return (lhs._value == rhs._value) && (lhs._index == rhs._index);
}
private:
T _value;
};
// const_iterator is guaranteed to be a random access iterator. it is not writable for obvious reasons
static_assert(std::random_access_iterator<const_iterator<int>>);
template <typename R>
requires subscriptable<R>
class index_iterator : public index_iterator_base<index_iterator<R>> {
public:
using base = index_iterator_base<index_iterator<R>>;
using difference_type = long long;
using value_type = range_value_t<R>;
index_iterator() : base(0), _range(nullptr) {}
index_iterator(R &range, size_t index) : base(index), _range(&range) {}
auto operator*() const {
// if we have the information, do a bounds check
if constexpr (sized<R>) { assert(this->_index < std::size(*_range)); }
return (*_range)[this->_index];
}
auto *operator->() const { return &((*_range)[this->_index]); }
friend bool operator==(const index_iterator &lhs, const index_iterator &rhs) {
return (lhs._range == rhs._range) && (lhs._index == rhs._index);
}
private:
R *_range;
};
// index_iterator satisfied forward_iterator
static_assert(std::random_access_iterator<index_iterator<std::array<int, 4>>>);
} // namespace quicktex
Loading…
Cancel
Save