diff --git a/CMakeLists.txt b/CMakeLists.txt index b10102d..0afc781 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.20) include(cmake/color.cmake) -set(BLT_VERSION 0.20.23) +set(BLT_VERSION 0.20.24) set(BLT_TEST_VERSION 0.0.1) set(BLT_TARGET BLT) diff --git a/include/blt/format/format.h b/include/blt/format/format.h index 4b5aee9..dac5446 100644 --- a/include/blt/format/format.h +++ b/include/blt/format/format.h @@ -362,6 +362,7 @@ namespace blt::string private: std::string m_tableName; int m_columnPadding; + [[maybe_unused]] int m_maxColumnWidth; std::vector columns; std::vector rows; diff --git a/include/blt/meta/iterator.h b/include/blt/meta/iterator.h new file mode 100644 index 0000000..97d82dd --- /dev/null +++ b/include/blt/meta/iterator.h @@ -0,0 +1,44 @@ +#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 . + */ + +#ifndef BLT_META_ITERATOR_H +#define BLT_META_ITERATOR_H + +#include +#include + +namespace blt::meta +{ + + template + constexpr bool is_input_iterator = std::is_same_v::iterator_cateogry, std::input_iterator_tag>; + + template + constexpr bool is_forward_iterator = std::is_same_v::iterator_cateogry, std::forward_iterator_tag>; + + template + constexpr bool is_bidirectional_iterator = std::is_same_v::iterator_cateogry, std::bidirectional_iterator_tag>; + + template + constexpr bool is_random_access_iterator = std::is_same_v::iterator_cateogry, std::random_access_iterator_tag>; + + template + constexpr bool is_bidirectional_or_better = is_bidirectional_iterator || is_random_access_iterator; +} + +#endif //BLT_META_ITERATOR_H diff --git a/include/blt/std/ranges.h b/include/blt/std/ranges.h index ae3a4fb..074bb91 100644 --- a/include/blt/std/ranges.h +++ b/include/blt/std/ranges.h @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -18,14 +19,6 @@ namespace blt { - template::iterator_cateogry> - constexpr bool is_input_or_forward_only = - std::is_same_v || std::is_same_v; - - template::iterator_category> - constexpr bool is_bidirectional_or_better = - std::is_same_v || std::is_same_v; - template struct enumerate_item { @@ -34,33 +27,28 @@ namespace blt }; template - class enumerate_base + class enumerate_iterator_base { public: - explicit enumerate_base(Iter iter, blt::size_t place = 0): iter(std::move(iter)), index(place) + explicit enumerate_iterator_base(Iter iter, blt::size_t place = 0): iter(std::move(iter)), index(place) {} - enumerate_item>* operator->() const - { - return &std::pair{index, *this->iter}; - } - enumerate_item> operator*() const { return {index, *this->iter}; } - friend bool operator==(const enumerate_base& a, const enumerate_base& b) + friend bool operator==(const enumerate_iterator_base& a, const enumerate_iterator_base& b) { return a.iter == b.iter; } - friend bool operator!=(const enumerate_base& a, const enumerate_base& b) + friend bool operator!=(const enumerate_iterator_base& a, const enumerate_iterator_base& b) { return a.iter != b.iter; } - auto get_iterator() const + auto base() const { return iter; } @@ -76,19 +64,19 @@ namespace blt }; template - class enumerate_fwd : public enumerate_base + class enumerate_forward_iterator : public enumerate_iterator_base { public: - using enumerate_base::enumerate_base; + using enumerate_iterator_base::enumerate_iterator_base; - enumerate_fwd& operator++() + enumerate_forward_iterator& operator++() { ++this->iter; ++this->index; return *this; } - enumerate_fwd operator++(int) + enumerate_forward_iterator operator++(int) { auto tmp = *this; ++*this; @@ -97,62 +85,19 @@ namespace blt }; template - class enumerate_bidirectional : public enumerate_fwd + class enumerate_bidirectional_iterator : public enumerate_forward_iterator { public: - using enumerate_fwd::enumerate_fwd; + using enumerate_forward_iterator::enumerate_forward_iterator; - enumerate_bidirectional& operator--() + enumerate_bidirectional_iterator& operator--() { --this->iter; --this->index; return *this; } - enumerate_bidirectional operator--(int) - { - auto tmp = *this; - --*this; - return tmp; - } - }; - - template - class enumerate_rev_fwd : public enumerate_base - { - public: - explicit enumerate_rev_fwd(Iter iter, blt::size_t place = 0): enumerate_base(std::move(iter), place) - {} - - enumerate_rev_fwd& operator++() - { - --this->iter; - --this->index; - return *this; - } - - enumerate_rev_fwd operator++(int) - { - auto tmp = *this; - ++*this; - return tmp; - } - }; - - template - class enumerate_rev_bidirectional : public enumerate_rev_fwd - { - public: - using enumerate_rev_fwd::enumerate_rev_fwd; - - enumerate_rev_bidirectional& operator--() - { - ++this->iter; - ++this->index; - return *this; - } - - enumerate_rev_bidirectional operator--(int) + enumerate_bidirectional_iterator operator--(int) { auto tmp = *this; --*this; @@ -164,66 +109,168 @@ namespace blt class enumerate_wrapper; template - class enumerate_wrapper, std::void_t>> : public enumerate_fwd + class enumerate_wrapper, std::void_t>> + : public enumerate_forward_iterator { public: using iterator_category = std::forward_iterator_tag; - using value_type = typename std::iterator_traits::value_type; + using value_type = enumerate_item>; using difference_type = typename std::iterator_traits::difference_type; - using pointer = typename std::iterator_traits::pointer; - using reference = typename std::iterator_traits::reference; + using pointer = value_type; + using reference = value_type; using iterator_type = Iter; - using enumerate_fwd::enumerate_fwd; + using enumerate_forward_iterator::enumerate_forward_iterator; }; template - class enumerate_wrapper, std::void_t>> - : public enumerate_bidirectional + class enumerate_wrapper, std::void_t>> + : public enumerate_bidirectional_iterator { public: - using iterator_category = std::forward_iterator_tag; - using value_type = typename std::iterator_traits::value_type; + 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 = typename std::iterator_traits::pointer; - using reference = typename std::iterator_traits::reference; + using pointer = value_type; + using reference = value_type; using iterator_type = Iter; - using enumerate_bidirectional::enumerate_bidirectional; + 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(std::move(begin), begin_index), + IterWrapper(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 enumerate_wrapper_rev; + class enumerator; + + template> + class enumerator_rev; template - class enumerate_wrapper_rev, std::void_t>> : public enumerate_rev_fwd + class enumerator, std::void_t>> + : public enumerator_base> { public: - using iterator_category = std::forward_iterator_tag; - using value_type = typename std::iterator_traits::value_type; - using difference_type = typename std::iterator_traits::difference_type; - using pointer = typename std::iterator_traits::pointer; - using reference = typename std::iterator_traits::reference; - using iterator_type = Iter; - - using enumerate_rev_fwd::enumerate_rev_fwd; + using enumerator_base>::enumerator_base; }; template - class enumerate_wrapper_rev, std::void_t>> - : public enumerate_rev_bidirectional + class enumerator, std::void_t>> + : public enumerator_reversible, enumerator_rev> { public: - using iterator_category = std::forward_iterator_tag; - using value_type = typename std::iterator_traits::value_type; - using difference_type = typename std::iterator_traits::difference_type; - using pointer = typename std::iterator_traits::pointer; - using reference = typename std::iterator_traits::reference; - using iterator_type = Iter; - - using enumerate_rev_bidirectional::enumerate_rev_bidirectional; + 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 { @@ -312,138 +359,6 @@ namespace blt return itr::itr_container{std::reverse_iterator(std::forward(end)), std::reverse_iterator(std::forward(begin))}; } - template typename Iter_type> - class enumerator_base - { - public: - explicit enumerator_base(Iter begin, Iter end): begin_(std::move(begin)), end_(std::move(end)) - {} - - explicit enumerator_base(Iter begin, Iter end, blt::size_t begin_index, blt::size_t end_index): - begin_(std::move(begin), begin_index), end_(std::move(end), end_index) - {} - - auto begin() - { - return begin_; - } - - auto end() - { - return end_; - } - - protected: - Iter_type begin_; - Iter_type end_; - }; - - 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_base - { - public: - explicit enumerator(Iter begin, Iter end, blt::size_t container_size): - enumerator_base(std::move(begin), std::move(end)), container_size(container_size) - {} - - explicit enumerator(Iter begin, Iter end, blt::size_t begin_index, blt::size_t end_index): - enumerator_base(std::move(begin), std::move(end), begin_index, end_index), - container_size(std::abs(static_cast(end_index) - static_cast(begin_index))) - {} - - auto rev() - { - auto end = this->end_.get_iterator(); - auto begin = this->begin_.get_iterator(); - --end; - --begin; - return enumerator_rev{end, begin, this->container_size - 1, 0ul}; - } - - protected: - blt::size_t container_size; - }; - - template - class enumerator_rev : public enumerator_base - { - public: - explicit enumerator_rev(Iter begin, Iter end, blt::size_t container_size): - enumerator_base(std::move(begin), std::move(end)), container_size(container_size) - {} - - explicit enumerator_rev(Iter begin, Iter end, blt::size_t begin_index, blt::size_t end_index): - enumerator_base(std::move(begin), std::move(end), begin_index, end_index), - container_size(std::abs(static_cast(end_index) - static_cast(begin_index))) - {} - - auto rev() - { - auto end = this->end_.get_iterator(); - auto begin = this->begin_.get_iterator(); - ++end; - ++begin; - return enumerator{end, begin, container_size + 1}; - } - - protected: - blt::size_t container_size; - }; - - 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()}; - } - template typename iterator = itr::pair_iterator> class pair_enumerator {