File: iterator.h

package info (click to toggle)
libwibble 0.1.19
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 832 kB
  • ctags: 1,940
  • sloc: cpp: 9,798; makefile: 163; perl: 84; sh: 11
file content (129 lines) | stat: -rw-r--r-- 3,546 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/** -*- C++ -*-
    @file wibble/iterator.h
    @author Peter Rockai <me@mornfall.net>
*/

#include <wibble/amorph.h>
#include <wibble/mixin.h>

#ifndef WIBBLE_ITERATOR_H
#define WIBBLE_ITERATOR_H

namespace wibble {

typedef bool SortabilityTag;

template< typename T, typename I >
struct IteratorTraits {
    typedef SortabilityTag Unsorted;
};

template< typename T >
struct IteratorTraits< T, typename std::set< T >::iterator > {
    typedef SortabilityTag Sorted;
};

template< typename T >
struct IteratorTraits< T, typename std::multiset< T >::iterator > {
    typedef SortabilityTag Sorted;
};

template< typename T >
struct IteratorInterface {
    virtual T current() const = 0;
    virtual void advance() = 0;
    virtual ~IteratorInterface() {}
};

template< typename T >
struct IteratorProxy {
    IteratorProxy( T _x ) : x( _x ) {}
    T x;
    const T *operator->() const { return &x; }
};

template< typename T, typename W >
struct IteratorMorph : Morph< IteratorMorph< T, W >, W, IteratorInterface< T > >
{
    typedef W Wrapped;
    IteratorMorph() {}
    IteratorMorph( const Wrapped &w )
        : Morph< IteratorMorph, Wrapped, IteratorInterface< T > >( w ) {}
    virtual void advance() { this->wrapped().advance(); }
    virtual T current() const { return this->wrapped().current(); }
};

template< typename T, typename Self >
struct IteratorMixin : mixin::Comparable< Self >
{
    Self &self() { return *static_cast< const Self * >( this ); }
    const Self &self() const { return *static_cast< const Self * >( this ); }
    typedef T ElementType;

    typedef std::forward_iterator_tag iterator_category;
    typedef T value_type;
    typedef ptrdiff_t difference_type;
    typedef T *pointer;
    typedef T &reference;
    typedef const T &const_reference;

    IteratorProxy< T > operator->() const {
        return IteratorProxy< T >(self().current()); }
    Self next() const { Self n( self() ); n.advance(); return n; }
    T operator*() const { return self().current(); }

    Self &operator++() { self().advance(); return self(); }
    Self operator++(int) {
        Self tmp = self();
        self().advance();
        return tmp;
    }
};

template< typename T, typename I >
typename IteratorTraits< T, I >::Unsorted isSortedT( I, I ) {
    return false;
}

template< typename T, typename I >
typename IteratorTraits< T, I >::Sorted isSortedT( I, I ) {
    return true;
}

template< typename T >
struct Iterator : Amorph< Iterator< T >, IteratorInterface< T >, 0 >,
    IteratorMixin< T, Iterator< T > >
{
    typedef Amorph< Iterator< T >, IteratorInterface< T >, 0 > Super;
    typedef T ElementType;

    Iterator( const IteratorInterface< T > &i ) : Super( i ) {}
    Iterator() {}
    bool operator<=( const Iterator &i ) const { return leq( i ); }

    T current() const { return this->implInterface()->current(); }
    virtual void advance() { this->implInterface()->advance(); }

    template< typename C > operator Iterator< C >();
};

template< typename It >
struct StlIterator : IteratorMixin< typename It::value_type, StlIterator< It > >
{
    typedef typename std::iterator_traits< It >::value_type Value;
    StlIterator( It i ) : m_iterator( i ) {}
    virtual void advance() { ++m_iterator; }
    virtual Value current() const { return *m_iterator; }
    bool operator==( const StlIterator< It > &o ) { return m_iterator == o.m_iterator; }
protected:
    It m_iterator;
};

template< typename I >
Iterator< typename I::value_type > iterator( I i ) {
    return StlIterator< I >( i );
}

}

#endif