Boost logo

Boost :

From: jsiek_at_[hidden]
Date: 1999-11-28 15:04:13


Howard Hinnant writes:
> One minor quibble, and it's only with your test_iter, not with your
> proposed boost code. Needs:
>
> friend Distance operator - (const self& x, const self& y) {return x._i -
> y._i;}

Thanks for catching that, I'll also add a test case for it.

> I especially like how Dave's addable automatically took care of:
>
> friend self operator + (Distance n, const self& rhs);
>
> For some reason I always forget that one.

The stuff from operators.hpp worked flawlessly. Guess that attests that
operators.hpp was really done right.


// (C) Copyright Jeremy Siek 1999. Permission to copy, use, modify,
// sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.

#ifndef BOOST_ITERATOR_OPS_H
#define BOOST_ITERATOR_OPS_H

#include <boost/operators.hpp>

#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
namespace boost
{
#endif

template <class DerivedClass>
struct postfix_inc_operator
{
  friend DerivedClass operator++(DerivedClass& x, int)
  {
    DerivedClass tmp(x);
    ++x;
    return tmp;
  }
};
  

template <class DerivedClass>
struct postfix_dec_operator
{
  friend DerivedClass operator--(DerivedClass& x, int)
  {
    DerivedClass tmp(x);
    --x;
    return tmp;
  }
};

template <class DerivedClass>
struct member_operator
{
  typedef typename std::iterator_traits<DerivedClass>::value_type value_type;
  value_type* operator->() const {
    return &*static_cast<const DerivedClass&>(*this);
  }
};

template <class DerivedClass>
class random_access_operators
{
  typedef typename std::iterator_traits<DerivedClass>::reference Ref;
  typedef typename std::iterator_traits<DerivedClass>::difference_type
    Distance;
public:
  friend DerivedClass operator+(const DerivedClass& x, Distance n)
  {
    DerivedClass tmp = x;
    tmp += n;
    return tmp;
  }

  friend DerivedClass operator+(Distance n, const DerivedClass& x)
  {
    return x + n;
  }

  friend DerivedClass operator-(const DerivedClass& x, Distance n)
  {
    DerivedClass tmp = x;
    tmp -= n;
    return tmp;
  }

  friend DerivedClass operator-(Distance n, const DerivedClass& x)
  {
    return x - n;
  }

  Ref operator[](Distance n) const {
    return *(static_cast<const DerivedClass&>(*this) + n);
  }
};

template <class DerivedClass>
struct foward_iterator_helper :
  public equality_comparable<DerivedClass,DerivedClass>,
  public postfix_inc_operator<DerivedClass>,
  public member_operator<DerivedClass> { };

template <class DerivedClass>
struct bidirectional_iterator_helper :
  public equality_comparable<DerivedClass,DerivedClass>,
  public postfix_inc_operator<DerivedClass>,
  public postfix_dec_operator<DerivedClass>,
  public member_operator<DerivedClass> { };

template <class DerivedClass>
struct random_access_iterator_helper :
  public equality_comparable<DerivedClass,DerivedClass>,
  public less_than_comparable<DerivedClass,DerivedClass>,
  public postfix_inc_operator<DerivedClass>,
  public postfix_dec_operator<DerivedClass>,
  public member_operator<DerivedClass>,
  public random_access_operators<DerivedClass> { };

#ifndef BOOST_NO_OPERATORS_IN_NAMESPACE
} // namespace boost
#else
namespace boost {
  using ::postfix_inc_operator;
  using ::postfix_dec_operator;
  using ::member_operator;
  using ::random_access_operators;
  using ::forward_iterator_helper;
  using ::bidirectional_iterator_helper;
  using ::random_access_iterator_helper;
}
#endif

#endif /* BOOST_ITERATOR_OPS_H */


// (C) Copyright Jeremy Siek 1999. Permission to copy, use, modify,
// sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.

#include <string>
#include <iostream>
#include <iterator>
#include <boost/array.hpp>
#include <mtl/iterator_ops.hpp>

using namespace std;
using namespace boost;

template <class T>
struct test_iter
  : public boost::random_access_iterator_helper<test_iter<T>, T, int, T*, T&>
{
  typedef test_iter self;
  typedef typename self::reference Reference;
  typedef typename self::difference_type Distance;
public:
  test_iter(T* i) : _i(i) { }
  test_iter(const self& x) : _i(x._i) { }
  self& operator=(const self& x) { _i = x._i; return *this; }
  Reference operator*() const { return *_i; }
  self& operator++() { ++_i; return *this; }
  self& operator--() { --_i; return *this; }
  self& operator+=(Distance n) { _i += n; return *this; }
  self& operator-=(Distance n) { _i -= n; return *this; }
  friend bool operator==(const self& x, const self& y) { return x._i == y._i; }
  friend bool operator<(const self& x, const self& y) { return x._i < y._i; }
  friend Distance operator-(const self& x, const self& y) {
    return x._i - y._i; }
protected:
  T* _i;
};

int
main()
{
  string array[] = { "apple", "orange", "pear", "peach", "grape", "plum" };
  test_iter<string> i = begin(array), ie = end(array);

  // test i++
  while (i != ie)
    cout << *i++ << " ";
  cout << endl;
  i = begin(array);

  // test i--
  while (ie != i) {
    ie--;
    cout << *ie << " ";
  }
  cout << endl;
  ie = end(array);

  // test i->m
  while (i != ie) {
    cout << i->size() << " ";
    ++i;
  }
  cout << endl;
  i = begin(array);

  // test i + n
  while (i < ie) {
    cout << *i << " ";
    i = i + 2;
  }
  cout << endl;
  i = begin(array);

  // test n + i
  while (i < ie) {
    cout << *i << " ";
    i = 2 + i;
  }
  cout << endl;

  i = begin(array);

  // test i - n
  while (ie > i) {
    ie = ie - 2;
    cout << *ie << " ";
  }
  cout << endl;
  ie = end(array);

  // test i[n]
  for (int j = 0; j < size(array) ; ++j)
    cout << i[j] << " ";
  cout << endl;

  // test i - j
  cout << "distance between "
       << i[1] << " and " << i[4]
       << " is " << (i + 4) - (i + 1) << endl;

  return 0;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk