diff --git a/quicktex/util/ranges.h b/quicktex/util/ranges.h index 32b3210..84f1cd2 100644 --- a/quicktex/util/ranges.h +++ b/quicktex/util/ranges.h @@ -35,10 +35,10 @@ namespace quicktex { // std::ranges::range is not usable by default in libc++ 13 template -concept range = std::is_constructible_v && requires(T &t) { - t.begin(); - t.end(); - }; +concept range = requires(T &t) { + t.begin(); + t.end(); + }; template concept sized = requires(T &t) { std::size(t); }; @@ -83,22 +83,6 @@ size_t distance(T range) { return std::distance(range.begin(), range.end()); } -template - requires std::input_or_output_iterator -class view { - public: - view() : _begin(), _end() {} - view(II begin, II end) : _begin(begin), _end(end) {} - - inline size_t size() { return distance(_begin, _end); } - inline II begin() { return _begin; } - inline II end() { return _end; } - - private: - II _begin; - II _end; -}; - template class index_iterator_base { public: typedef long long difference_type; @@ -113,12 +97,12 @@ template class index_iterator_base { return old; } D &operator--() { - _index++; + _index--; return static_cast(*this); } D operator--(int) { D old = static_cast(*this); - _index++; + _index--; return old; } diff --git a/quicktex/util/subrange.h b/quicktex/util/subrange.h new file mode 100644 index 0000000..39aa3ab --- /dev/null +++ b/quicktex/util/subrange.h @@ -0,0 +1,97 @@ +/* Quicktex Texture Compression Library + Copyright (C) 2021 Andrew Cassidy + Partially derived from rgbcx.h written by Richard Geldreich + 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 . + */ + +#pragma once + +#include +#include + +#include "util/ranges.h" + +namespace quicktex { + +template S = I> struct subrange { + public: + using iterator_type = I; + using sentinel_type = S; + using value_type = std::iter_value_t; + using reference_type = std::iter_reference_t; + using difference_type = std::iter_difference_t; + + constexpr subrange(const I& b, const S& e) : _begin(b), _end(e) {} + + constexpr I begin() const { return _begin; } + constexpr S end() const { return _end; } + constexpr bool empty() const { return _begin == _end; } + constexpr difference_type size() const { return std::distance(_end, _begin); } + + explicit constexpr operator bool() const { return !empty(); } + + constexpr subrange& advance(difference_type n) { + assert(n >= 0 || std::bidirectional_iterator); // forward iterators cannot be decremented + + if (n > 0) { + for (int i = 0; i < n && _begin != _end; i++) { _begin++; } + } else { + for (int i = 0; i > n && _begin != _end; i--) { _begin--; } + } + return *this; + } + + constexpr subrange next(difference_type n = 1) const { + auto tmp = *this; + return tmp.advance(n); + } + + template + requires std::bidirectional_iterator + constexpr subrange prev(difference_type n = 1) const { + return next(-n); + } + + template + requires std::random_access_iterator + constexpr reference_type operator[](difference_type i) { + assert(i >= 0 && i < size()); + return _begin[i]; + } + + template + requires std::random_access_iterator + constexpr const reference_type operator[](difference_type i) const { + assert(i >= 0 && i < size()); + return _begin[i]; + } + + template + requires std::contiguous_iterator + constexpr value_type* data() { + return std::to_address(_begin); + } + template + requires std::contiguous_iterator + constexpr value_type const* data() const { + return std::to_address(_begin); + } + + private: + I _begin; + S _end; +}; +} // namespace quicktex \ No newline at end of file