#ifndef _iterator_tpl_h_
#define _iterator_tpl_h_
namespace iterator_tpl {
#define SETUP_ITERATORS(C, T, S) \
SETUP_MUTABLE_ITERATOR(C, T, S) \
SETUP_CONST_ITERATOR(C, T, S)
#define SETUP_MUTABLE_ITERATOR(C, T, S) \
typedef iterator_tpl::iterator<C, T, S> iterator; \
iterator begin() { return iterator::begin(this); } \
iterator end() { return iterator::end(this); }
#define SETUP_CONST_ITERATOR(C, T, S) \
typedef iterator_tpl::const_iterator<C, T, S> const_iterator; \
const_iterator begin() const { return const_iterator::begin(this); } \
const_iterator end() const { return const_iterator::end(this); }
#define SETUP_REVERSE_ITERATORS(C, T, S) \
struct S##_reversed : public S { \
inline void next (const C* ref) { S::prev(ref); } \
inline void prev (const C* ref) { S::next(ref); } \
inline void begin(const C* ref) { S::end(ref); S::prev(ref);} \
inline void end (const C* ref) { S::begin(ref); S::prev(ref);} \
}; \
SETUP_MUTABLE_RITERATOR(C, T, S) \
SETUP_CONST_RITERATOR(C, T, S)
#define SETUP_MUTABLE_RITERATOR(C, T, S) \
typedef iterator_tpl::iterator<C, T, S##_reversed > reverse_iterator; \
reverse_iterator rbegin() { return reverse_iterator::begin(this); } \
reverse_iterator rend() { return reverse_iterator::end(this); } \
#define SETUP_CONST_RITERATOR(C, T, S) \
typedef iterator_tpl::const_iterator<C, T, S##_reversed > const_reverse_iterator; \
const_reverse_iterator rbegin() const { \
return const_reverse_iterator::begin(this); \
} \
const_reverse_iterator rend() const { \
return const_reverse_iterator::end(this); \
}
#define STL_TYPEDEFS(T) \
typedef std::ptrdiff_t difference_type; \
typedef size_t size_type; \
typedef T value_type; \
typedef T* pointer; \
typedef const T* const_pointer; \
typedef T& reference; \
typedef const T& const_reference
template <class C, typename T, class S>
struct const_iterator;
template <class C, typename T, class S>
struct iterator {
C* ref;
S state;
void next() { state.next(ref); }
void begin() { state.begin(ref); }
void end() { state.end(ref); }
T get() { return state.get(ref); }
bool cmp(const S& s) const { return state.cmp(s); }
void prev() { state.prev(ref); }
public:
static iterator begin(C* ref) {
iterator it(ref);
it.begin();
return it;
}
static iterator end(C* ref) {
iterator it(ref);
it.end();
return it;
}
protected:
iterator(C* ref) : ref(ref) {}
public:
iterator() {}
public:
T operator*() { return get(); }
iterator& operator++() { next(); return *this; }
iterator operator++(int) { iterator temp(*this); next(); return temp; }
iterator& operator--() { prev(); return *this; }
iterator operator--(int) { iterator temp(*this); prev(); return temp; }
bool operator!=(const iterator& other) const {
return ref != other.ref || cmp(other.state);
}
bool operator==(const iterator& other) const {
return !operator!=(other);
}
friend struct iterator_tpl::const_iterator<C,T,S>;
bool operator!=(const const_iterator<C,T,S>& other) const {
return ref != other.ref || cmd(other.state);
}
bool operator==(const const_iterator<C,T,S>& other) const {
return !operator!=(other);
}
};
template <class C, typename T, class S>
struct iterator<C,T&,S> {
C* ref;
S state;
void next() { state.next(ref); }
void begin() { state.begin(ref); }
void end() { state.end(ref); }
T& get() { return state.get(ref); }
bool cmp(const S& s) const { return state.cmp(s); }
void prev() { state.prev(ref); }
public:
static iterator begin(C* ref) {
iterator it(ref);
it.begin();
return it;
}
static iterator end(C* ref) {
iterator it(ref);
it.end();
return it;
}
protected:
iterator(C* ref) : ref(ref) {}
public:
iterator() {}
public:
T& operator*() { return get(); }
T* operator->() { return &get(); }
iterator& operator++() { next(); return *this; }
iterator operator++(int) { iterator temp(*this); next(); return temp; }
iterator& operator--() { prev(); return *this; }
iterator operator--(int) { iterator temp(*this); prev(); return temp; }
bool operator!=(const iterator& other) const {
return ref != other.ref || cmp(other.state);
}
bool operator==(const iterator& other) const {
return !operator!=(other);
}
friend struct iterator_tpl::const_iterator<C,T&,S>;
bool operator!=(const const_iterator<C,T&,S>& other) const {
return ref != other.ref || cmd(other.state);
}
bool operator==(const const_iterator<C,T&,S>& other) const {
return !operator!=(other);
}
};
template <class C, typename T, class S>
struct const_iterator {
const C* ref;
S state;
void next() { state.next(ref); }
void begin() { state.begin(ref); }
void end() { state.end(ref); }
const T get() { return state.get(ref); }
bool cmp(const S& s) const { return state.cmp(s); }
void prev() { state.prev(ref); }
public:
static const_iterator begin(const C* ref) {
const_iterator it(ref);
it.begin();
return it;
}
static const_iterator end(const C* ref) {
const_iterator it(ref);
it.end();
return it;
}
protected:
const_iterator(const C* ref) : ref(ref) {}
public:
const_iterator() {}
const_iterator(const iterator<C,T,S>& other) : ref(other.ref) {
state = other.state;
}
public:
const T operator*() { return get(); }
const_iterator& operator++() { next(); return *this; }
const_iterator operator++(int) { const_iterator temp(*this); next(); return temp; }
const_iterator& operator--() { prev(); return *this; }
const_iterator operator--(int) { const_iterator temp(*this); prev(); return temp; }
bool operator!=(const const_iterator& other) const {
return ref != other.ref || cmp(other.state);
}
bool operator==(const const_iterator& other) const {
return !operator!=(other);
}
const_iterator& operator=(const iterator<C,T,S>& other) {
ref = other.ref;
state = other.state;
return *this;
}
friend struct iterator_tpl::iterator<C,T,S>;
bool operator!=(const iterator<C,T,S>& other) const {
return ref != other.ref || cmp(other.state);
}
bool operator==(const iterator<C,T,S>& other) const {
return !operator!=(other);
}
};
template <class C, typename T, class S>
struct const_iterator<C,T&,S> {
const C* ref;
S state;
void next() { state.next(ref); }
void begin() { state.begin(ref); }
void end() { state.end(ref); }
const T& get() { return state.get(ref); }
bool cmp(const S& s) const { return state.cmp(s); }
void prev() { state.prev(ref); }
public:
static const_iterator begin(const C* ref) {
const_iterator it(ref);
it.begin();
return it;
}
static const_iterator end(const C* ref) {
const_iterator it(ref);
it.end();
return it;
}
protected:
const_iterator(const C* ref) : ref(ref) {}
public:
const_iterator() {}
const_iterator(const iterator<C,T&,S>& other) : ref(other.ref) {
state = other.state;
}
public:
const T& operator*() { return get(); }
const T* operator->() { return &get(); }
const_iterator& operator++() { next(); return *this; }
const_iterator operator++(int) { const_iterator temp(*this); next(); return temp; }
const_iterator& operator--() { prev(); return *this; }
const_iterator operator--(int) { const_iterator temp(*this); prev(); return temp; }
bool operator!=(const const_iterator& other) const {
return ref != other.ref || cmp(other.state);
}
bool operator==(const const_iterator& other) const {
return !operator!=(other);
}
const_iterator& operator=(const iterator<C,T&,S>& other) {
ref = other.ref;
state = other.state;
return *this;
}
friend struct iterator_tpl::iterator<C,T&,S>;
bool operator!=(const iterator<C,T&,S>& other) const {
return ref != other.ref || cmp(other.state);
}
bool operator==(const iterator<C,T&,S>& other) const {
return !operator!=(other);
}
};
}
#endif