Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
667 khor 1
/* ----------------------------------------------------------------------- *
2
 * This file is part of GEL, http://www.imm.dtu.dk/GEL
3
 * Copyright (C) the authors and DTU Informatics
4
 * For license and list of authors, see ../../doc/intro.pdf
5
 * ----------------------------------------------------------------------- */
6
 
7
/**
8
 * @file Iterators.h
9
 * @brief Contains class for iterating over mesh entities in a HMesh.
10
 */
11
#ifndef __HMESH_ITERATORS_H__
12
#define __HMESH_ITERATORS_H__
13
 
14
#include "ItemVector.h"
15
 
16
namespace HMesh
17
{
18
    /** Traverse the entities of an HMesh in the order they are stored in the
19
      data structure. */
20
    template<typename ITEM>
21
    class IDIterator
22
    {
23
    public:
24
        typedef ItemID<ITEM> ID;
25
 
26
        // typedefs to accommodiate stl compliance
27
        typedef std::bidirectional_iterator_tag iterator_category;
28
        typedef ptrdiff_t difference_type;
29
        typedef ID value_type;
30
        typedef value_type reference;
31
        typedef value_type* pointer;
32
 
33
        /// constructor (default: skipping enabled)
34
        IDIterator(const ItemVector<ITEM>& _item_vector, ID _id, bool _skip = true);
35
 
36
        /// prefix increment 
37
        IDIterator& operator ++();		
38
        /// postfix increment
39
        IDIterator& operator ++(int);
40
        /// prefix decrement
41
        IDIterator& operator --();
42
        /// postfix decrement
43
        IDIterator& operator --(int);
44
 
45
        /// equal to
46
        bool operator ==(const IDIterator& other) const;
47
        /// not equal to
48
        bool operator !=(const IDIterator& other) const;
49
 
50
        /// indirection
51
        reference operator *();
52
        /// member by pointer
53
        pointer operator ->();
54
        /// cast
55
        //operator VertexID() const;
56
 
57
    private:
58
        const ItemVector<ITEM>* item_vector;
59
        ID id;
60
        bool skip;
61
    };
62
 
63
    template<typename ITEM>
64
    class IDIteratorPair
65
    {
66
    public:
67
        IDIteratorPair(IDIterator<ITEM> _f, IDIterator<ITEM> _l): f(_f), l(_l) {}
68
        IDIterator<ITEM> begin() {return f;}
69
        IDIterator<ITEM> end() {return l;}
70
    private:
71
        IDIterator<ITEM> f,l;
72
    };
73
 
74
 
75
 
76
    /*-----------------------------------------
77
     * IDIterator template implementation
78
     *-----------------------------------------*/
79
 
80
    template<typename ITEM>
81
    inline IDIterator<ITEM>::IDIterator(const ItemVector<ITEM>& _item_vector, ID _id, bool _skip) 
82
        : item_vector(&_item_vector), id(_id), skip(_skip){}
83
 
84
    template<typename ITEM>
85
    inline IDIterator<ITEM>& IDIterator<ITEM>::operator ++(int)
86
    { return ++(*this); }
87
 
88
    template<typename ITEM>
89
    inline IDIterator<ITEM>& IDIterator<ITEM>::operator --(int)
90
    { return --(*this); }
91
 
92
    template<typename ITEM>
93
    inline bool IDIterator<ITEM>::operator ==(const IDIterator<ITEM>& other) const
94
    { return item_vector == other.item_vector && id == other.id; }
95
 
96
    template<typename ITEM>
97
    inline bool IDIterator<ITEM>::operator !=(const IDIterator<ITEM>& other) const
98
    { return item_vector != other.item_vector || id != other.id; }
99
 
100
    template<typename ITEM>
101
    inline ItemID<ITEM> IDIterator<ITEM>::operator *()
102
    { return id; }
103
 
104
    template<typename ITEM>
105
    inline ItemID<ITEM>* IDIterator<ITEM>::operator ->()
106
    { return &id; }
107
 
108
    template<typename ITEM>
109
    inline IDIterator<ITEM>& IDIterator<ITEM>::operator ++()
110
    {
111
        id = item_vector->index_next(id, skip);
112
        return *this;
113
    }
114
 
115
    template<typename ITEM>
116
    inline IDIterator<ITEM>& IDIterator<ITEM>::operator --()
117
    {
118
        id = item_vector->index_prev(id, skip);
119
        return *this;
120
    }
121
}
122
 
123
#endif