mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-11-14 10:39:05 +00:00
index_iterator.hpp 追加
This commit is contained in:
parent
304c656b78
commit
3c301adae8
4 changed files with 368 additions and 3 deletions
|
@ -12,6 +12,9 @@
|
|||
#include <sprout/fixed_container/functions.hpp>
|
||||
#include <sprout/iterator.hpp>
|
||||
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL
|
||||
#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION
|
||||
# include <sprout/iterator/index_iterator.hpp>
|
||||
#endif
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
|
@ -21,8 +24,13 @@ namespace sprout {
|
|||
class array {
|
||||
public:
|
||||
typedef T value_type;
|
||||
#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION
|
||||
typedef sprout::index_iterator<array&> iterator;
|
||||
typedef sprout::index_iterator<array const&> const_iterator;
|
||||
#else
|
||||
typedef T* iterator;
|
||||
typedef T const* const_iterator;
|
||||
#endif
|
||||
typedef T& reference;
|
||||
typedef T const& const_reference;
|
||||
typedef std::size_t size_type;
|
||||
|
@ -51,6 +59,26 @@ namespace sprout {
|
|||
throw std::out_of_range("array<>: index out of range");
|
||||
}
|
||||
}
|
||||
#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION
|
||||
iterator begin() SPROUT_NOEXCEPT {
|
||||
return iterator(*this, 0);
|
||||
}
|
||||
SPROUT_CONSTEXPR const_iterator begin() const SPROUT_NOEXCEPT {
|
||||
return const_iterator(*this, 0);
|
||||
}
|
||||
SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT {
|
||||
return const_iterator(*this, 0);
|
||||
}
|
||||
iterator end() SPROUT_NOEXCEPT {
|
||||
return iterator(*this, size());
|
||||
}
|
||||
SPROUT_CONSTEXPR const_iterator end() const SPROUT_NOEXCEPT {
|
||||
return const_iterator(*this, size());
|
||||
}
|
||||
SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT {
|
||||
return const_iterator(*this, size());
|
||||
}
|
||||
#else
|
||||
iterator begin() SPROUT_NOEXCEPT {
|
||||
return &elems[0];
|
||||
}
|
||||
|
@ -69,6 +97,7 @@ namespace sprout {
|
|||
SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT {
|
||||
return &elems[0] + size();
|
||||
}
|
||||
#endif
|
||||
reverse_iterator rbegin() SPROUT_NOEXCEPT {
|
||||
return reverse_iterator(end());
|
||||
}
|
||||
|
|
217
sprout/iterator/index_iterator.hpp
Normal file
217
sprout/iterator/index_iterator.hpp
Normal file
|
@ -0,0 +1,217 @@
|
|||
#ifndef SPROUT_ITERATOR_INDEX_ITERATOR_HPP
|
||||
#define SPROUT_ITERATOR_INDEX_ITERATOR_HPP
|
||||
|
||||
#include <iterator>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/fixed_container/traits.hpp>
|
||||
#include <sprout/iterator/next.hpp>
|
||||
#include <sprout/iterator/prev.hpp>
|
||||
#include <sprout/utility/value_holder.hpp>
|
||||
#include <sprout/detail/if.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// index_iterator
|
||||
//
|
||||
template<typename Container>
|
||||
class index_iterator
|
||||
: public std::iterator<
|
||||
std::random_access_iterator_tag,
|
||||
typename sprout::fixed_container_traits<typename std::decay<Container>::type>::value_type,
|
||||
typename sprout::fixed_container_traits<typename std::decay<Container>::type>::difference_type,
|
||||
typename sprout::detail::if_c<
|
||||
std::is_const<typename std::remove_reference<Container>::type>::value,
|
||||
typename sprout::fixed_container_traits<typename std::decay<Container>::type>::const_pointer,
|
||||
typename sprout::fixed_container_traits<typename std::decay<Container>::type>::pointer
|
||||
>::type,
|
||||
typename sprout::detail::if_c<
|
||||
std::is_const<typename std::remove_reference<Container>::type>::value,
|
||||
typename sprout::fixed_container_traits<typename std::decay<Container>::type>::const_reference,
|
||||
typename sprout::fixed_container_traits<typename std::decay<Container>::type>::reference
|
||||
>::type
|
||||
>
|
||||
{
|
||||
public:
|
||||
typedef Container container_type;
|
||||
typedef typename sprout::fixed_container_traits<typename std::decay<container_type>::type> traits_type;
|
||||
typedef typename sprout::detail::if_c<
|
||||
std::is_reference<container_type>::value,
|
||||
typename std::decay<container_type>::type const&,
|
||||
typename std::decay<container_type>::type const
|
||||
>::type const_container_type;
|
||||
private:
|
||||
typedef std::iterator<
|
||||
std::random_access_iterator_tag,
|
||||
typename traits_type::value_type,
|
||||
typename traits_type::difference_type,
|
||||
typename sprout::detail::if_c<
|
||||
std::is_const<typename std::remove_reference<container_type>::type>::value,
|
||||
typename traits_type::const_pointer,
|
||||
typename traits_type::pointer
|
||||
>::type,
|
||||
typename sprout::detail::if_c<
|
||||
std::is_const<typename std::remove_reference<container_type>::type>::value,
|
||||
typename traits_type::const_reference,
|
||||
typename traits_type::reference
|
||||
>::type
|
||||
> base_type;
|
||||
public:
|
||||
typedef typename base_type::iterator_category iterator_category;
|
||||
typedef typename base_type::value_type value_type;
|
||||
typedef typename base_type::difference_type difference_type;
|
||||
typedef typename base_type::pointer pointer;
|
||||
typedef typename base_type::reference reference;
|
||||
typedef typename traits_type::size_type size_type;
|
||||
private:
|
||||
sprout::value_holder<container_type> holder_;
|
||||
size_type index_;
|
||||
private:
|
||||
SPROUT_CONSTEXPR index_iterator(sprout::value_holder<container_type> const& r, size_type index)
|
||||
: holder_(r)
|
||||
, index_(index)
|
||||
{}
|
||||
public:
|
||||
SPROUT_CONSTEXPR index_iterator()
|
||||
: holder_()
|
||||
, index_()
|
||||
{}
|
||||
index_iterator(index_iterator const&) = default;
|
||||
SPROUT_CONSTEXPR index_iterator(typename sprout::value_holder<container_type>::param_type p, size_type index)
|
||||
: holder_(p)
|
||||
, index_(index)
|
||||
{}
|
||||
operator index_iterator<const_container_type>() const {
|
||||
return index_iterator<const_container_type>(holder_.get(), index_);
|
||||
}
|
||||
SPROUT_CONSTEXPR index_iterator next() const {
|
||||
return index_iterator(holder_, index_ + 1);
|
||||
}
|
||||
SPROUT_CONSTEXPR index_iterator prev() const {
|
||||
return index_iterator(holder_, index_ - 1);
|
||||
}
|
||||
void swap(index_iterator& other) {
|
||||
using std::swap;
|
||||
swap(holder_, other.holder_);
|
||||
swap(index_, other.index_);
|
||||
}
|
||||
friend SPROUT_CONSTEXPR bool operator==(index_iterator const& lhs, index_iterator const& rhs) {
|
||||
return lhs.index_ == rhs.index_;
|
||||
}
|
||||
friend SPROUT_CONSTEXPR bool operator!=(index_iterator const& lhs, index_iterator const& rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
friend bool operator<(index_iterator const& lhs, index_iterator const& rhs) {
|
||||
return lhs.index_ < rhs.index_;
|
||||
}
|
||||
friend bool operator>(index_iterator const& lhs, index_iterator const& rhs) {
|
||||
return rhs < lhs;
|
||||
}
|
||||
friend bool operator<=(index_iterator const& lhs, index_iterator const& rhs) {
|
||||
return !(rhs < lhs);
|
||||
}
|
||||
friend bool operator>=(index_iterator const& lhs, index_iterator const& rhs) {
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
SPROUT_CONSTEXPR reference operator*() const {
|
||||
return holder_.get()[index_];
|
||||
}
|
||||
SPROUT_CONSTEXPR pointer operator->() const {
|
||||
return &holder_.get()[index_];
|
||||
}
|
||||
index_iterator& operator++() {
|
||||
index_iterator temp(next());
|
||||
temp.swap(*this);
|
||||
return *this;
|
||||
}
|
||||
index_iterator operator++(int) {
|
||||
index_iterator result(*this);
|
||||
++*this;
|
||||
return result;
|
||||
}
|
||||
index_iterator& operator--() {
|
||||
index_iterator temp(prev());
|
||||
temp.swap(*this);
|
||||
return *this;
|
||||
}
|
||||
index_iterator operator--(int) {
|
||||
index_iterator result(*this);
|
||||
--*this;
|
||||
return result;
|
||||
}
|
||||
SPROUT_CONSTEXPR index_iterator operator+(difference_type n) const {
|
||||
return index_iterator(holder_, index_ + n);
|
||||
}
|
||||
SPROUT_CONSTEXPR index_iterator operator-(difference_type n) const {
|
||||
return index_iterator(holder_, index_ - n);
|
||||
}
|
||||
index_iterator& operator+=(difference_type n) {
|
||||
index_iterator temp(holder_, index_ + n);
|
||||
temp.swap(this);
|
||||
return this;
|
||||
}
|
||||
index_iterator& operator-=(difference_type n) {
|
||||
index_iterator temp(holder_, index_ - n);
|
||||
temp.swap(this);
|
||||
return this;
|
||||
}
|
||||
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
|
||||
return holder_.get()[index_ + n];
|
||||
}
|
||||
friend difference_type operator-(index_iterator const& lhs, index_iterator const& rhs) {
|
||||
return static_cast<difference_type>(rhs.index_) - static_cast<difference_type>(lhs.index_);
|
||||
}
|
||||
friend SPROUT_CONSTEXPR index_iterator operator+(difference_type n, index_iterator const& it) {
|
||||
return it + n;
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// swap
|
||||
//
|
||||
template<typename Container>
|
||||
void swap(sprout::index_iterator<Container>& lhs, sprout::index_iterator<Container>& rhs) SPROUT_NOEXCEPT_EXPR(SPROUT_NOEXCEPT_EXPR(lhs.swap(rhs))) {
|
||||
lhs.swap(rhs);
|
||||
}
|
||||
|
||||
//
|
||||
// next
|
||||
//
|
||||
template<typename Container>
|
||||
SPROUT_CONSTEXPR sprout::index_iterator<Container> next(
|
||||
sprout::index_iterator<Container> const& it
|
||||
)
|
||||
{
|
||||
return it.next();
|
||||
}
|
||||
template<typename Container>
|
||||
SPROUT_CONSTEXPR sprout::index_iterator<Container> next(
|
||||
sprout::index_iterator<Container> const& it,
|
||||
typename sprout::index_iterator<Container>::difference_type n
|
||||
)
|
||||
{
|
||||
return it + n;
|
||||
}
|
||||
|
||||
//
|
||||
// prev
|
||||
//
|
||||
template<typename Container>
|
||||
SPROUT_CONSTEXPR sprout::index_iterator<Container> prev(
|
||||
sprout::index_iterator<Container> const& it
|
||||
)
|
||||
{
|
||||
return it.prev();
|
||||
}
|
||||
template<typename Container>
|
||||
SPROUT_CONSTEXPR sprout::index_iterator<Container> prev(
|
||||
sprout::index_iterator<Container> const& it,
|
||||
typename sprout::index_iterator<Container>::difference_type n
|
||||
)
|
||||
{
|
||||
return it - n;
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_ITERATOR_INDEX_ITERATOR_HPP
|
|
@ -4,10 +4,12 @@
|
|||
#include <cstddef>
|
||||
#include <iterator>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/iterator/next.hpp>
|
||||
#include <sprout/iterator/prev.hpp>
|
||||
#include <sprout/utility/value_holder.hpp>
|
||||
#include <sprout/detail/if.hpp>
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
|
@ -25,6 +27,11 @@ namespace sprout {
|
|||
{
|
||||
public:
|
||||
typedef T type;
|
||||
typedef typename sprout::detail::if_c<
|
||||
std::is_reference<type>::value,
|
||||
typename std::decay<type>::type const&,
|
||||
typename std::decay<type>::type const
|
||||
>::type const_type;
|
||||
private:
|
||||
typedef std::iterator<
|
||||
std::random_access_iterator_tag,
|
||||
|
@ -57,6 +64,9 @@ namespace sprout {
|
|||
: holder_(p)
|
||||
, count_(count)
|
||||
{}
|
||||
operator value_iterator<const_type>() const {
|
||||
return value_iterator<const_type>(holder_.get(), count_);
|
||||
}
|
||||
SPROUT_CONSTEXPR value_iterator next() const {
|
||||
return value_iterator(holder_, count_ != 0 ? count_ - 1 : count_);
|
||||
}
|
||||
|
|
|
@ -17,6 +17,10 @@
|
|||
#include <sprout/operation/fixed/append_front.hpp>
|
||||
#include <sprout/iterator.hpp>
|
||||
#include HDR_ALGORITHM_SSCRISK_CEL_OR_SPROUT_DETAIL
|
||||
#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION
|
||||
# include <algorithm>
|
||||
# include <sprout/iterator/index_iterator.hpp>
|
||||
#endif
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
|
@ -84,6 +88,44 @@ namespace sprout {
|
|||
static SPROUT_CONSTEXPR int_type eof() SPROUT_NOEXCEPT {
|
||||
return impl_type::eof();
|
||||
}
|
||||
#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION
|
||||
template<typename CharIterator>
|
||||
static SPROUT_CONSTEXPR int compare(CharIterator s1, CharIterator s2, std::size_t n) {
|
||||
return !n ? 0
|
||||
: lt(*s1, *s2) ? -1
|
||||
: lt(*s2, *s1) ? 1
|
||||
: compare(s1 + 1, s2 + 1, n - 1)
|
||||
;
|
||||
}
|
||||
template<typename CharIterator>
|
||||
static SPROUT_CONSTEXPR std::size_t length(CharIterator s) {
|
||||
return !*s ? 0
|
||||
: 1 + length(s + 1)
|
||||
;
|
||||
}
|
||||
template<typename CharIterator>
|
||||
static SPROUT_CONSTEXPR CharIterator find(CharIterator s, std::size_t n, char_type const& a) {
|
||||
return !n ? nullptr
|
||||
: eq(*s, a) ? s
|
||||
: find(s + 1, n - 1, a)
|
||||
;
|
||||
}
|
||||
template<typename CharIterator1, typename CharIterator2>
|
||||
static CharIterator1 move(CharIterator1 s1, CharIterator2 s2, std::size_t n) {
|
||||
std::copy_backward(s2, s2 + n, s1);
|
||||
return s1;
|
||||
}
|
||||
template<typename CharIterator1, typename CharIterator2>
|
||||
static CharIterator1 copy(CharIterator1 s1, CharIterator2 s2, std::size_t n) {
|
||||
std::copy(s2, s2 + n, s1);
|
||||
return s1;
|
||||
}
|
||||
template<typename CharIterator>
|
||||
static CharIterator assign(CharIterator s, std::size_t n, char_type a) {
|
||||
std::fill(s, s + n, a);
|
||||
return s;
|
||||
}
|
||||
#endif
|
||||
};
|
||||
|
||||
//
|
||||
|
@ -93,8 +135,13 @@ namespace sprout {
|
|||
class basic_string {
|
||||
public:
|
||||
typedef T value_type;
|
||||
#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION
|
||||
typedef sprout::index_iterator<basic_string&> iterator;
|
||||
typedef sprout::index_iterator<basic_string const&> const_iterator;
|
||||
#else
|
||||
typedef T* iterator;
|
||||
typedef T const* const_iterator;
|
||||
#endif
|
||||
typedef T& reference;
|
||||
typedef T const& const_reference;
|
||||
typedef std::size_t size_type;
|
||||
|
@ -132,6 +179,15 @@ namespace sprout {
|
|||
{
|
||||
return sprout::basic_string<T, N, Traits>{{(Indexes < n ? s[Indexes] : T())...}, n};
|
||||
}
|
||||
#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION
|
||||
static SPROUT_CONSTEXPR int compare_impl_1(const_iterator dest, size_type pos1, size_type n1, const_iterator s, size_type n2) {
|
||||
return compare_impl_2(
|
||||
traits_type::compare(dest + pos1, s, NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(n1, n2)),
|
||||
n1,
|
||||
n2
|
||||
);
|
||||
}
|
||||
#endif
|
||||
public:
|
||||
static SPROUT_CONSTEXPR basic_string<T, N, Traits> from_c_str(value_type const* s, size_type n) {
|
||||
return !(N < n)
|
||||
|
@ -168,6 +224,26 @@ namespace sprout {
|
|||
throw std::out_of_range("basic_string<>: index out of range");
|
||||
}
|
||||
}
|
||||
#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION
|
||||
iterator begin() SPROUT_NOEXCEPT {
|
||||
return iterator(*this, 0);
|
||||
}
|
||||
SPROUT_CONSTEXPR const_iterator begin() const SPROUT_NOEXCEPT {
|
||||
return const_iterator(*this, 0);
|
||||
}
|
||||
SPROUT_CONSTEXPR const_iterator cbegin() const SPROUT_NOEXCEPT {
|
||||
return const_iterator(*this, 0);
|
||||
}
|
||||
iterator end() SPROUT_NOEXCEPT {
|
||||
return iterator(*this, size());
|
||||
}
|
||||
SPROUT_CONSTEXPR const_iterator end() const SPROUT_NOEXCEPT {
|
||||
return const_iterator(*this, size());
|
||||
}
|
||||
SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT {
|
||||
return const_iterator(*this, size());
|
||||
}
|
||||
#else
|
||||
iterator begin() SPROUT_NOEXCEPT {
|
||||
return &elems[0];
|
||||
}
|
||||
|
@ -186,6 +262,7 @@ namespace sprout {
|
|||
SPROUT_CONSTEXPR const_iterator cend() const SPROUT_NOEXCEPT {
|
||||
return &elems[0] + size();
|
||||
}
|
||||
#endif
|
||||
reverse_iterator rbegin() SPROUT_NOEXCEPT {
|
||||
return const_reverse_iterator(end());
|
||||
}
|
||||
|
@ -322,14 +399,14 @@ namespace sprout {
|
|||
SPROUT_CONSTEXPR int compare(basic_string<T, N2, Traits> const& str) const {
|
||||
return compare(0, size(), str.c_str(), str.size());
|
||||
}
|
||||
SPROUT_CONSTEXPR int compare(T const* s) const {
|
||||
SPROUT_CONSTEXPR int compare(value_type const* s) const {
|
||||
return compare(0, size(), s, traits_type::length(s));
|
||||
}
|
||||
template<std::size_t N2>
|
||||
SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, basic_string<T, N2, Traits> const& str) const {
|
||||
return compare(pos1, n1, str, 0, npos);
|
||||
}
|
||||
SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, char const* s) const {
|
||||
SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, value_type const* s) const {
|
||||
return compare(pos1, n1, s, traits_type::length(s));
|
||||
}
|
||||
template<std::size_t N2>
|
||||
|
@ -339,7 +416,7 @@ namespace sprout {
|
|||
: throw "basic_string<>: index out of range"
|
||||
;
|
||||
}
|
||||
SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, char const* s, size_type n2) const {
|
||||
SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, value_type const* s, size_type n2) const {
|
||||
return !(size() < pos1)
|
||||
? compare_impl_1(c_str(), pos1, NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(n1, size() - pos1), s, n2)
|
||||
: throw "basic_string<>: index out of range"
|
||||
|
@ -353,6 +430,37 @@ namespace sprout {
|
|||
: throw "basic_string<>: index out of range"
|
||||
;
|
||||
}
|
||||
#ifdef SPROUT_CONFIG_USE_INDEX_ITERATOR_IMPLEMENTATION
|
||||
basic_string<T, N, Traits>& assign(const_iterator s, size_type n) {
|
||||
maxcheck(n);
|
||||
for (size_type i = 0; i < n; ++i) {
|
||||
traits_type::assign(elems[i], s[i]);
|
||||
}
|
||||
for (size_type i = n; i < max_size(); ++i) {
|
||||
traits_type::assign(elems[i], value_type());
|
||||
}
|
||||
len = n;
|
||||
return *this;
|
||||
}
|
||||
basic_string<T, N, Traits>& assign(const_iterator s) {
|
||||
return assign(s, traits_type::length(s));
|
||||
}
|
||||
basic_string<T, N, Traits>& operator=(const_iterator rhs) {
|
||||
return assign(rhs);
|
||||
}
|
||||
SPROUT_CONSTEXPR int compare(const_iterator s) const {
|
||||
return compare(0, size(), s, traits_type::length(s));
|
||||
}
|
||||
SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, const_iterator s) const {
|
||||
return compare(pos1, n1, s, traits_type::length(s));
|
||||
}
|
||||
SPROUT_CONSTEXPR int compare(size_type pos1, size_type n1, const_iterator s, size_type n2) const {
|
||||
return !(size() < pos1)
|
||||
? compare_impl_1(c_str(), pos1, NS_SSCRISK_CEL_OR_SPROUT_DETAIL::min(n1, size() - pos1), s, n2)
|
||||
: throw "basic_string<>: index out of range"
|
||||
;
|
||||
}
|
||||
#endif
|
||||
};
|
||||
template<typename T, std::size_t N1, std::size_t N2, typename Traits>
|
||||
SPROUT_CONSTEXPR inline bool operator==(sprout::basic_string<T, N1, Traits> const& lhs, sprout::basic_string<T, N2, Traits> const& rhs) {
|
||||
|
@ -866,3 +974,4 @@ namespace std {
|
|||
} // namespace std
|
||||
|
||||
#endif // #ifndef SPROUT_STRING_HPP
|
||||
|
||||
|
|
Loading…
Reference in a new issue