Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TCG_ITERATOR_OPS_H
Toshihiro Shimizu 890ddd
#define TCG_ITERATOR_OPS_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// tcg includes
Toshihiro Shimizu 890ddd
#include "traits.h"
Toshihiro Shimizu 890ddd
#include "ptr.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// STD includes
Toshihiro Shimizu 890ddd
#include <iterator></iterator>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace tcg {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//****************************************************************************
Toshihiro Shimizu 890ddd
//    Traits
Toshihiro Shimizu 890ddd
//****************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename it=""></typename>
Toshihiro Shimizu 890ddd
struct iterator_traits : public std::iterator_traits<it> {</it>
Shinya Kitaoka 120a6e
  typedef It inheritable_iterator_type;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename t=""></typename>
Toshihiro Shimizu 890ddd
struct iterator_traits<t *=""> : public std::iterator_traits<t *=""> {</t></t>
Shinya Kitaoka 120a6e
  typedef ptr<t> inheritable_iterator_type;</t>
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//***********************************************************************
Toshihiro Shimizu 890ddd
//    Step Iterator class
Toshihiro Shimizu 890ddd
//***********************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  The Step Iterator class is a simple random access iterator wrapper which
Toshihiro Shimizu 890ddd
  moves by a fixed number of items.
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
\warning The size of the container referenced by the wrapped iterator should
Toshihiro Shimizu 890ddd
  always be a multiple of the specified step.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename ranit=""></typename>
Shinya Kitaoka 120a6e
class step_iterator : public std::iterator<
Shinya Kitaoka 120a6e
                          std::random_access_iterator_tag,
Shinya Kitaoka 120a6e
                          typename std::iterator_traits<ranit>::value_type,</ranit>
Shinya Kitaoka 120a6e
                          typename std::iterator_traits<ranit>::difference_type,</ranit>
Shinya Kitaoka 120a6e
                          typename std::iterator_traits<ranit>::pointer,</ranit>
Shinya Kitaoka 120a6e
                          typename std::iterator_traits<ranit>::reference> {</ranit>
Shinya Kitaoka 120a6e
  RanIt m_it;
Shinya Kitaoka 120a6e
  typename step_iterator::difference_type m_step;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  step_iterator() {}
Shinya Kitaoka 120a6e
  step_iterator(const RanIt &it, typename step_iterator::difference_type step)
Shinya Kitaoka 120a6e
      : m_it(it), m_step(step) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  step_iterator &operator++() {
Shinya Kitaoka 120a6e
    m_it += m_step;
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  step_iterator &operator--() {
Shinya Kitaoka 120a6e
    m_it -= m_step;
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  step_iterator operator++(int) {
Shinya Kitaoka 120a6e
    step_iterator it(*this);
Shinya Kitaoka 120a6e
    operator++();
Shinya Kitaoka 120a6e
    return it;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  step_iterator operator--(int) {
Shinya Kitaoka 120a6e
    step_iterator it(*this);
Shinya Kitaoka 120a6e
    operator--();
Shinya Kitaoka 120a6e
    return it;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  step_iterator &operator+=(
Shinya Kitaoka 120a6e
      const typename step_iterator::difference_type &val) {
Shinya Kitaoka 120a6e
    m_it += val * m_step;
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  step_iterator &operator-=(
Shinya Kitaoka 120a6e
      const typename step_iterator::difference_type &val) {
Shinya Kitaoka 120a6e
    m_it -= val * m_step;
Shinya Kitaoka 120a6e
    return *this;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  typename step_iterator::difference_type operator-(
Shinya Kitaoka 120a6e
      const step_iterator &it) const {
Shinya Kitaoka 120a6e
    return (m_it - it.m_it) / m_step;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  step_iterator operator+(
Shinya Kitaoka 120a6e
      const typename step_iterator::difference_type &val) const {
Shinya Kitaoka 120a6e
    step_iterator it(*this);
Shinya Kitaoka 120a6e
    it += val;
Shinya Kitaoka 120a6e
    return it;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  step_iterator operator-(
Shinya Kitaoka 120a6e
      const typename step_iterator::difference_type &val) const {
Shinya Kitaoka 120a6e
    step_iterator it(*this);
Shinya Kitaoka 120a6e
    it -= val;
Shinya Kitaoka 120a6e
    return it;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  typename step_iterator::reference operator*() const { return *m_it; }
Shinya Kitaoka 120a6e
  typename step_iterator::pointer operator->() const {
Shinya Kitaoka 120a6e
    return m_it.operator->();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  const RanIt &it() const { return m_it; }
Shinya Kitaoka 120a6e
  int step() const { return m_step; }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool operator==(const step_iterator &it) const { return m_it == it.m_it; }
Shinya Kitaoka 120a6e
  bool operator!=(const step_iterator &it) const { return !operator==(it); }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool operator<(const step_iterator &it) const { return m_it < it.m_it; }
Shinya Kitaoka 120a6e
  bool operator>(const step_iterator &it) const { return m_it > it.m_it; }
Shinya Kitaoka 120a6e
  bool operator<=(const step_iterator &it) const { return m_it <= it.m_it; }
Shinya Kitaoka 120a6e
  bool operator>=(const step_iterator &it) const { return m_it >= it.m_it; }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace tcg
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // TCG_ITERATOR_OPS_H