2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2014-01-08 07:48:12 +00:00
|
|
|
Copyright (c) 2011-2014 Bolero MURAKAMI
|
2013-08-08 09:54:33 +00:00
|
|
|
https://github.com/bolero-MURAKAMI/Sprout
|
|
|
|
|
|
|
|
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
=============================================================================*/
|
2011-10-25 09:16:27 +00:00
|
|
|
#ifndef SPROUT_ITERATOR_BYTES_ITERATOR_HPP
|
|
|
|
#define SPROUT_ITERATOR_BYTES_ITERATOR_HPP
|
|
|
|
|
|
|
|
#include <iterator>
|
|
|
|
#include <sprout/config.hpp>
|
2014-04-30 07:30:26 +00:00
|
|
|
#include <sprout/workaround/std/cstddef.hpp>
|
2011-10-25 09:16:27 +00:00
|
|
|
#include <sprout/iterator/next.hpp>
|
|
|
|
#include <sprout/iterator/prev.hpp>
|
2012-06-16 06:35:10 +00:00
|
|
|
#include <sprout/iterator/distance.hpp>
|
2011-10-25 09:16:27 +00:00
|
|
|
#include <sprout/endian_traits.hpp>
|
2012-10-05 15:58:56 +00:00
|
|
|
#include <sprout/utility/swap.hpp>
|
2011-10-25 09:16:27 +00:00
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
//
|
|
|
|
// bytes_iterator
|
|
|
|
//
|
|
|
|
template<
|
|
|
|
typename Iterator,
|
2012-08-08 13:27:06 +00:00
|
|
|
typename Traits = sprout::endian_traits<typename std::iterator_traits<Iterator>::value_type>
|
2011-10-25 09:16:27 +00:00
|
|
|
>
|
|
|
|
class bytes_iterator
|
|
|
|
: public std::iterator<
|
|
|
|
typename std::iterator_traits<Iterator>::iterator_category,
|
|
|
|
unsigned char,
|
|
|
|
std::ptrdiff_t,
|
|
|
|
void,
|
|
|
|
unsigned char
|
|
|
|
>
|
|
|
|
{
|
|
|
|
public:
|
2013-02-23 06:21:27 +00:00
|
|
|
typedef Iterator iterator_type;
|
2011-10-25 09:16:27 +00:00
|
|
|
typedef Traits traits_type;
|
|
|
|
private:
|
|
|
|
typedef std::iterator<
|
|
|
|
typename std::iterator_traits<Iterator>::iterator_category,
|
|
|
|
unsigned char,
|
|
|
|
std::ptrdiff_t,
|
|
|
|
void,
|
|
|
|
unsigned char
|
2013-02-23 06:21:27 +00:00
|
|
|
> base_type;
|
2011-10-25 09:16:27 +00:00
|
|
|
public:
|
2013-02-23 06:21:27 +00:00
|
|
|
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;
|
2011-10-25 09:16:27 +00:00
|
|
|
private:
|
|
|
|
struct next_tag {};
|
|
|
|
struct prev_tag {};
|
|
|
|
struct ra_tag {};
|
|
|
|
private:
|
2013-02-23 06:21:27 +00:00
|
|
|
iterator_type it_;
|
2011-10-25 09:16:27 +00:00
|
|
|
difference_type i_;
|
|
|
|
private:
|
2013-02-23 06:21:27 +00:00
|
|
|
SPROUT_CONSTEXPR bytes_iterator(iterator_type it, difference_type i)
|
2013-10-09 14:44:25 +00:00
|
|
|
: it_(it) , i_(i)
|
2011-10-25 09:16:27 +00:00
|
|
|
{}
|
2013-02-23 06:21:27 +00:00
|
|
|
SPROUT_CONSTEXPR bytes_iterator(iterator_type it, difference_type i, next_tag)
|
2011-10-25 09:16:27 +00:00
|
|
|
: it_(i / traits_type::size() == 0 ? it : sprout::next(it))
|
|
|
|
, i_(i % traits_type::size())
|
|
|
|
{}
|
2013-02-23 06:21:27 +00:00
|
|
|
SPROUT_CONSTEXPR bytes_iterator(iterator_type it, difference_type i, prev_tag)
|
2011-10-25 09:16:27 +00:00
|
|
|
: it_((i + 1 - traits_type::size()) / traits_type::size() == 0 ? it : sprout::prev(it))
|
|
|
|
, i_(i % traits_type::size() + traits_type::size())
|
|
|
|
{}
|
2013-02-23 06:21:27 +00:00
|
|
|
SPROUT_CONSTEXPR bytes_iterator(iterator_type it, difference_type i, ra_tag)
|
2011-10-25 09:16:27 +00:00
|
|
|
: it_(i >= 0
|
|
|
|
? sprout::next(it, i / traits_type::size())
|
|
|
|
: sprout::next(it, (i + 1 - traits_type::size()) / traits_type::size())
|
|
|
|
)
|
|
|
|
, i_(i >= 0
|
|
|
|
? i % traits_type::size()
|
|
|
|
: i % traits_type::size() + traits_type::size()
|
|
|
|
)
|
|
|
|
{}
|
|
|
|
public:
|
|
|
|
SPROUT_CONSTEXPR bytes_iterator()
|
2013-02-23 06:21:27 +00:00
|
|
|
: it_(), i_()
|
2011-10-25 09:16:27 +00:00
|
|
|
{}
|
|
|
|
bytes_iterator(bytes_iterator const&) = default;
|
2013-02-23 06:21:27 +00:00
|
|
|
explicit SPROUT_CONSTEXPR bytes_iterator(iterator_type it)
|
|
|
|
: it_(it), i_()
|
2011-10-25 09:16:27 +00:00
|
|
|
{}
|
2013-02-23 06:21:27 +00:00
|
|
|
SPROUT_CONSTEXPR iterator_type const& base() const {
|
|
|
|
return it_;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR difference_type index() const {
|
|
|
|
return i_;
|
|
|
|
}
|
2011-10-25 09:16:27 +00:00
|
|
|
SPROUT_CONSTEXPR bytes_iterator next() const {
|
|
|
|
return bytes_iterator(it_, i_ + 1, next_tag());
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR bytes_iterator prev() const {
|
|
|
|
return bytes_iterator(it_, i_ - 1, prev_tag());
|
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void swap(bytes_iterator& other)
|
2014-08-14 13:55:49 +00:00
|
|
|
SPROUT_NOEXCEPT_IF_EXPR(sprout::swap(it_, other.it_))
|
2012-10-05 15:58:56 +00:00
|
|
|
{
|
|
|
|
sprout::swap(it_, other.it_);
|
|
|
|
sprout::swap(i_, other.i_);
|
2011-10-25 09:16:27 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR reference operator*() const {
|
|
|
|
return traits_type::get_byte(*it_, i_);
|
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR bytes_iterator& operator++() {
|
2011-10-25 09:16:27 +00:00
|
|
|
bytes_iterator temp(next());
|
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR bytes_iterator operator++(int) {
|
2011-10-25 09:16:27 +00:00
|
|
|
bytes_iterator result(*this);
|
|
|
|
++*this;
|
|
|
|
return result;
|
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR bytes_iterator& operator--() {
|
2011-10-25 09:16:27 +00:00
|
|
|
bytes_iterator temp(prev());
|
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR bytes_iterator operator--(int) {
|
2011-10-25 09:16:27 +00:00
|
|
|
bytes_iterator result(*this);
|
|
|
|
--*this;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR bytes_iterator operator+(difference_type n) const {
|
|
|
|
return bytes_iterator(it_, i_ + n, ra_tag());
|
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR bytes_iterator operator-(difference_type n) const {
|
|
|
|
return bytes_iterator(it_, i_ - n, ra_tag());
|
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR bytes_iterator& operator+=(difference_type n) {
|
2011-10-25 09:16:27 +00:00
|
|
|
bytes_iterator temp(it_, i_ + n, ra_tag());
|
2012-06-15 15:08:42 +00:00
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
2011-10-25 09:16:27 +00:00
|
|
|
}
|
2013-10-09 14:44:25 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR bytes_iterator& operator-=(difference_type n) {
|
2011-10-25 09:16:27 +00:00
|
|
|
bytes_iterator temp(it_, i_ - n, ra_tag());
|
2012-06-15 15:08:42 +00:00
|
|
|
temp.swap(*this);
|
|
|
|
return *this;
|
2011-10-25 09:16:27 +00:00
|
|
|
}
|
|
|
|
SPROUT_CONSTEXPR reference operator[](difference_type n) const {
|
|
|
|
return *(*this + n);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-02-23 06:21:27 +00:00
|
|
|
template<typename Iterator1, typename Iterator2, typename Traits>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator==(sprout::bytes_iterator<Iterator1, Traits> const& lhs, sprout::bytes_iterator<Iterator2, Traits> const& rhs) {
|
|
|
|
return lhs.base() == rhs.base() && lhs.index() == rhs.index();
|
|
|
|
}
|
|
|
|
template<typename Iterator1, typename Iterator2, typename Traits>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator!=(sprout::bytes_iterator<Iterator1, Traits> const& lhs, sprout::bytes_iterator<Iterator2, Traits> const& rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
template<typename Iterator1, typename Iterator2, typename Traits>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator<(sprout::bytes_iterator<Iterator1, Traits> const& lhs, sprout::bytes_iterator<Iterator2, Traits> const& rhs) {
|
|
|
|
return lhs.base() < rhs.base() || (lhs.base() == rhs.base() && lhs.index() < rhs.index());
|
|
|
|
}
|
|
|
|
template<typename Iterator1, typename Iterator2, typename Traits>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator>(sprout::bytes_iterator<Iterator1, Traits> const& lhs, sprout::bytes_iterator<Iterator2, Traits> const& rhs) {
|
|
|
|
return rhs < lhs;
|
|
|
|
}
|
|
|
|
template<typename Iterator1, typename Iterator2, typename Traits>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator<=(sprout::bytes_iterator<Iterator1, Traits> const& lhs, sprout::bytes_iterator<Iterator2, Traits> const& rhs) {
|
|
|
|
return !(rhs < lhs);
|
|
|
|
}
|
|
|
|
template<typename Iterator1, typename Iterator2, typename Traits>
|
|
|
|
inline SPROUT_CONSTEXPR bool
|
|
|
|
operator>=(sprout::bytes_iterator<Iterator1, Traits> const& lhs, sprout::bytes_iterator<Iterator2, Traits> const& rhs) {
|
|
|
|
return !(lhs < rhs);
|
|
|
|
}
|
|
|
|
template<typename Iterator1, typename Iterator2, typename Traits>
|
|
|
|
inline SPROUT_CONSTEXPR decltype(std::declval<Iterator1>() - std::declval<Iterator2>())
|
|
|
|
operator-(sprout::bytes_iterator<Iterator1, Traits> const& lhs, sprout::bytes_iterator<Iterator2, Traits> const& rhs) {
|
|
|
|
return (lhs.base() - rhs.base()) * Traits::size() + (lhs.index() - rhs.index());
|
|
|
|
}
|
|
|
|
template<typename Iterator, typename Traits>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::bytes_iterator<Iterator, Traits>
|
|
|
|
operator+(typename sprout::bytes_iterator<Iterator, Traits>::difference_type n, sprout::bytes_iterator<Iterator, Traits> const& it) {
|
|
|
|
return it + n;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// swap
|
|
|
|
//
|
|
|
|
template<typename Iterator, typename Traits>
|
2013-10-09 14:44:25 +00:00
|
|
|
inline SPROUT_CXX14_CONSTEXPR void
|
2013-02-23 06:21:27 +00:00
|
|
|
swap(sprout::bytes_iterator<Iterator, Traits>& lhs, sprout::bytes_iterator<Iterator, Traits>& rhs)
|
2014-08-14 13:55:49 +00:00
|
|
|
SPROUT_NOEXCEPT_IF_EXPR(lhs.swap(rhs))
|
2013-02-23 06:21:27 +00:00
|
|
|
{
|
|
|
|
lhs.swap(rhs);
|
|
|
|
}
|
|
|
|
|
2012-09-26 07:31:11 +00:00
|
|
|
//
|
|
|
|
// make_bytes_iterator
|
|
|
|
//
|
|
|
|
template<typename Iterator>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::bytes_iterator<Iterator>
|
|
|
|
make_bytes_iterator(Iterator it) {
|
2012-09-26 07:31:11 +00:00
|
|
|
return sprout::bytes_iterator<Iterator>(it);
|
|
|
|
}
|
|
|
|
template<typename Iterator, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::bytes_iterator<Iterator, Traits>
|
|
|
|
make_bytes_iterator(Iterator it, Traits) {
|
2012-09-26 07:31:11 +00:00
|
|
|
return sprout::bytes_iterator<Iterator, Traits>(it);
|
|
|
|
}
|
|
|
|
|
2011-10-25 09:16:27 +00:00
|
|
|
//
|
2013-02-23 06:21:27 +00:00
|
|
|
// make_big_bytes_iterator
|
|
|
|
// make_little_bytes_iterator
|
2011-10-25 09:16:27 +00:00
|
|
|
//
|
2013-02-23 06:21:27 +00:00
|
|
|
template<typename Iterator>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::bytes_iterator<Iterator, sprout::big_endian_traits<typename std::iterator_traits<Iterator>::value_type> >
|
|
|
|
make_big_bytes_iterator(Iterator it) {
|
|
|
|
return sprout::bytes_iterator<Iterator, sprout::big_endian_traits<typename std::iterator_traits<Iterator>::value_type> >(it);
|
|
|
|
}
|
|
|
|
template<typename Iterator>
|
|
|
|
inline SPROUT_CONSTEXPR sprout::bytes_iterator<Iterator, sprout::little_endian_traits<typename std::iterator_traits<Iterator>::value_type> >
|
|
|
|
make_little_bytes_iterator(Iterator it) {
|
|
|
|
return sprout::bytes_iterator<Iterator, sprout::little_endian_traits<typename std::iterator_traits<Iterator>::value_type> >(it);
|
2011-10-25 09:16:27 +00:00
|
|
|
}
|
|
|
|
|
2012-09-26 07:31:11 +00:00
|
|
|
//
|
|
|
|
// iterator_next
|
2011-10-25 09:16:27 +00:00
|
|
|
//
|
|
|
|
template<typename Iterator, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::bytes_iterator<Iterator, Traits>
|
|
|
|
iterator_next(sprout::bytes_iterator<Iterator, Traits> const& it) {
|
2011-10-25 09:16:27 +00:00
|
|
|
return it.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2012-09-26 07:31:11 +00:00
|
|
|
// iterator_prev
|
2011-10-25 09:16:27 +00:00
|
|
|
//
|
|
|
|
template<typename Iterator, typename Traits>
|
2012-10-05 15:58:56 +00:00
|
|
|
inline SPROUT_CONSTEXPR sprout::bytes_iterator<Iterator, Traits>
|
|
|
|
iterator_prev(sprout::bytes_iterator<Iterator, Traits> const& it) {
|
2011-10-25 09:16:27 +00:00
|
|
|
return it.prev();
|
|
|
|
}
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_ITERATOR_BYTES_ITERATOR_HPP
|