#pragma once /* * Created by Brett on 06/02/24. * Licensed under GNU General Public License V3.0 * See LICENSE file for license detail */ #ifndef BLT_RANGES_H #define BLT_RANGES_H #include #include #include #include #include #include #include #include namespace blt { template struct enumerate_item { blt::size_t index; T value; }; template class enumerate_iterator_base { public: explicit enumerate_iterator_base(Iter iter, blt::size_t place = 0): iter(std::move(iter)), index(place) {} enumerate_item> operator*() const { return {index, *this->iter}; } friend bool operator==(const enumerate_iterator_base& a, const enumerate_iterator_base& b) { return a.iter == b.iter; } friend bool operator!=(const enumerate_iterator_base& a, const enumerate_iterator_base& b) { return a.iter != b.iter; } auto base() const { return iter; } auto get_index() const { return index; } protected: Iter iter; blt::size_t index; }; template class enumerate_forward_iterator : public enumerate_iterator_base { public: using enumerate_iterator_base::enumerate_iterator_base; enumerate_forward_iterator& operator++() { ++this->iter; ++this->index; return *this; } enumerate_forward_iterator operator++(int) { auto tmp = *this; ++*this; return tmp; } }; template class enumerate_bidirectional_iterator : public enumerate_forward_iterator { public: using enumerate_forward_iterator::enumerate_forward_iterator; enumerate_bidirectional_iterator& operator--() { --this->iter; --this->index; return *this; } enumerate_bidirectional_iterator operator--(int) { auto tmp = *this; --*this; return tmp; } }; template> class enumerate_wrapper; template class enumerate_wrapper, std::void_t>> : public enumerate_forward_iterator { public: using iterator_category = std::forward_iterator_tag; using value_type = enumerate_item>; using difference_type = typename std::iterator_traits::difference_type; using pointer = value_type; using reference = value_type; using iterator_type = Iter; using enumerate_forward_iterator::enumerate_forward_iterator; }; template class enumerate_wrapper, std::void_t>> : public enumerate_bidirectional_iterator { public: using iterator_category = typename std::iterator_traits::iterator_category; using value_type = enumerate_item>; using difference_type = typename std::iterator_traits::difference_type; using pointer = value_type; using reference = value_type; using iterator_type = Iter; using enumerate_bidirectional_iterator::enumerate_bidirectional_iterator; }; template class enumerator_base { public: explicit enumerator_base(Iter begin, Iter end): begin_(std::move(begin)), end_(std::move(end)) {} explicit enumerator_base(IterWrapper begin, IterWrapper end): begin_(std::move(begin)), end_(std::move(end)) {} auto begin() { return begin_; } auto end() { return end_; } protected: IterWrapper begin_; IterWrapper end_; }; template class enumerator_reversible : public enumerator_base { public: explicit enumerator_reversible(Iter begin, Iter end, blt::size_t container_size): enumerator_base(std::move(begin), std::move(end)), container_size(container_size) {} explicit enumerator_reversible(Iter begin, Iter end, blt::size_t begin_index, blt::size_t end_index): enumerator_base(IterWrapper{enumerate_wrapper(std::move(begin), begin_index)}, IterWrapper{enumerate_wrapper(std::move(end), end_index)}), container_size(std::abs(static_cast(end_index) - static_cast(begin_index))) {} auto rev() { return Reverse{this->end_.base(), this->begin_.base(), this->container_size, 0ul}; } protected: blt::size_t container_size; }; template> class enumerator; template> class enumerator_rev; template class enumerator, std::void_t>> : public enumerator_base> { public: using enumerator_base>::enumerator_base; }; template class enumerator, std::void_t>> : public enumerator_reversible, enumerator_rev> { public: using enumerator_reversible, enumerator_rev>::enumerator_reversible; }; template class enumerator, std::void_t>> : public enumerator_reversible, enumerator_rev> { public: using enumerator_reversible, enumerator_rev>::enumerator_reversible; }; template class enumerator_rev, std::void_t>> : public enumerator_reversible>, enumerator> { public: using enumerator_reversible>, enumerator>::enumerator_reversible; }; template class enumerator_rev, std::void_t>> : public enumerator_reversible>, enumerator> { public: using enumerator_reversible>, enumerator>::enumerator_reversible; }; template enumerator(Iter, Iter) -> enumerator; template enumerator(Iter, Iter, blt::size_t) -> enumerator; template enumerator(Iter, Iter, blt::size_t, blt::size_t) -> enumerator; template static inline auto enumerate(const T& container) { return enumerator{container.begin(), container.end(), container.size()}; } template static inline auto enumerate(const T(& container)[size]) { return enumerator{&container[0], &container[size], size}; } template static inline auto enumerate(T(& container)[size]) { return enumerator{&container[0], &container[size], size}; } template static inline auto enumerate(T& container) { return enumerator{container.begin(), container.end(), container.size()}; } template static inline auto enumerate(T&& container) { return enumerator{container.begin(), container.end(), container.size()}; } namespace itr { template class itr_container { public: itr_container(Begin&& begin, End&& end): begin_(std::forward(begin)), end_(std::forward(end)) {} Begin begin() { return begin_; } End end() { return end_; } private: Begin begin_; End end_; }; // TODO: cleanup! all of this! add support for reversing template class pair_iterator { public: using c1_ref = blt::meta::deref_return_t; using c2_ref = blt::meta::deref_return_t; using iterator_category = std::forward_iterator_tag; using value_type = std::pair; 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 static inline auto iterate(Begin&& begin, End&& end) { return itr::itr_container{std::forward(begin), std::forward(end)}; } template static inline auto reverse_iterate(Begin&& begin, End&& end) { return itr::itr_container{std::reverse_iterator(std::forward(end)), std::reverse_iterator(std::forward(begin))}; } template 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 begin() { return begin_; } iterator end() { return end_; } private: iterator begin_; iterator end_; }; template static inline auto in_pairs(const T& container1, const G& container2) { return pair_enumerator{container1.begin(), container1.end(), container2.begin(), container2.end()}; } template static inline auto in_pairs(T& container1, G& container2) { return pair_enumerator{container1.begin(), container1.end(), container2.begin(), container2.end()}; } template 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 static inline auto in_pairs(T(& container1)[size], G(& container2)[size]) { return pair_enumerator{&container1[0], &container1[size], &container2[0], &container2[size]}; } template static inline auto in_pairs(T&& container1, G&& container2) { return pair_enumerator{container1.begin(), container1.end(), container2.begin(), container2.end()}; } template struct range { public: struct range_itr { public: using iterator_category = std::bidirectional_iterator_tag; using difference_type = T; using value_type = T; using pointer = T*; using reference = T&; private: T current; bool forward; public: explicit range_itr(T current, bool forward): current(current), forward(forward) {} value_type operator*() const { return current; } value_type operator->() { return current; } range_itr& operator++() { if (forward) current++; else current--; return *this; } range_itr& operator--() { if (forward) current--; else current++; return *this; } friend bool operator==(const range_itr& a, const range_itr& b) { return a.current == b.current; } friend bool operator!=(const range_itr& a, const range_itr& b) { return a.current != b.current; } }; private: T _begin; T _end; T offset = 0; public: range(T begin, T end): _begin(begin), _end(end), offset(end < begin ? 1 : 0) {} range_itr begin() { return range_itr(_begin - offset, offset == 0); } range_itr end() { // not sure if i like this return range_itr(_end - offset, offset == 0); } }; template class itr_offset { private: I begin_; I end_; public: template itr_offset(I begin, I end, T offset): begin_(begin), end_(end) { for (T t = 0; t < offset; t++) ++begin_; } template 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 itr_offset(C, T) -> itr_offset; inline constexpr std::size_t dynamic_extent = std::numeric_limits::max(); template class span; // https://codereview.stackexchange.com/questions/217814/c17-span-implementation namespace span_detail { // detect specializations of span template struct is_span : std::false_type { }; template struct is_span> : std::true_type { }; template inline constexpr bool is_span_v = is_span::value; // detect specializations of std::array template struct is_array : std::false_type { }; template struct is_array> : std::true_type { }; template inline constexpr bool is_array_v = is_array::value; // detect container template struct is_cont : std::false_type { }; template struct is_cont>, std::enable_if_t>, std::enable_if_t>, decltype(data(std::declval())), decltype(size(std::declval())) >> : std::true_type { }; template inline constexpr bool is_cont_v = is_cont::value; } template class span { public: using element_type = T; using value_type = std::remove_cv_t; using size_type = blt::size_t; using difference_type = std::ptrdiff_t; using pointer = T*; using const_pointer = const T*; using reference = T&; using const_reference = const T&; using iterator = T*; using const_iterator = const T*; using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; private: size_type size_; pointer data_; public: constexpr span() noexcept: size_(0), data_(nullptr) {} constexpr span(T* data, size_type count): size_(count), data_(data) {} template = true> constexpr explicit span(It first, size_type count): size_(count), data_(&*first) {} template = true> constexpr span(It first, size_type count): size_(count), data_(&*first) {} template = true> constexpr explicit span(It first, End last): size_(&*last - &*first), data_(&*first) {} template = true> constexpr span(It first, End last): size_(&*last - &*first), data_(&*first) {} template()))>(*)[], T(*)[]>), bool> = true> constexpr span(element_type (& arr)[N]) noexcept: size_{N}, data_{arr} // NOLINT {} template()))>(*)[], T(*)[]>), bool> = true> constexpr span(std::array& arr) noexcept: size_(N), data_{arr.data()} // NOLINT {} template()))>(*)[], T(*)[]>), bool> = true> constexpr span(const std::array& arr) noexcept: size_(N), data_{arr.data()} // NOLINT {} template>, typename std::enable_if_t< extent != dynamic_extent && span_detail::is_cont_v && std::is_convertible_v()))>(*)[], T(*)[]>, bool> = true> explicit constexpr span(R&& range): size_(std::size(range)), data_(std::data(range)) {} template>, typename std::enable_if_t< extent == dynamic_extent && span_detail::is_cont_v && std::is_convertible_v()))>(*)[], T(*)[]>, bool> = true> constexpr span(R&& range): size_(std::size(range)), data_(std::data(range)) // NOLINT {} template, bool> = true> explicit constexpr span(std::initializer_list il) noexcept: size_(il.size()), data_(&il.begin()) // NOLINT {} template, bool> = true> explicit span(std::initializer_list il) noexcept: size_(il.size()), data_(&il.begin()) // NOLINT {} template, bool> = true> explicit constexpr span(const span& source) noexcept: size_{source.size()}, data_{source.data()} {} template, bool> = true> constexpr span(const span& source) noexcept: size_{source.size()}, data_{source.data()} // NOLINT {} constexpr span& operator=(const span& copy) { size_ = copy.size(); data_ = copy.data(); return *this; } constexpr span(const span& other) noexcept = default; constexpr iterator begin() const noexcept { return data(); } constexpr iterator end() const noexcept { return data() + size(); } constexpr const_iterator cbegin() const noexcept { return data(); } constexpr const_iterator cend() const noexcept { return data() + size(); } constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator{end()}; } constexpr reverse_iterator rend() const noexcept { return reverse_iterator{begin()}; } constexpr const_reverse_iterator crbegin() const noexcept { return reverse_iterator{cend()}; } constexpr const_reverse_iterator crend() const noexcept { return reverse_iterator{cbegin()}; } friend constexpr iterator begin(span s) noexcept { return s.begin(); } friend constexpr iterator end(span s) noexcept { return s.end(); } [[nodiscard]] constexpr size_type size() const noexcept { return size_; } [[nodiscard]] constexpr size_type size_bytes() const noexcept { return size() * sizeof(T); } [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; } constexpr reference operator[](size_type idx) const { return *(data() + idx); } constexpr reference front() const { return *data(); } constexpr reference back() const { return *(data() + (size() - 1)); } constexpr pointer data() const noexcept { return data_; } constexpr span first(size_type cnt) const { return {data(), cnt}; } constexpr span last(size_type cnt) const { return {data() + (size() - cnt), cnt}; } constexpr span subspan(size_type off, size_type cnt = dynamic_extent) const { return {data() + off, cnt == dynamic_extent ? size() - off : cnt}; } }; template span(T (&)[N]) -> span; template span(std::array&) -> span; template span(const std::array&) -> span; template span(Cont&) -> span; template span(const Cont&) -> span; } #endif //BLT_RANGES_H