iterator.h 6.36 KB
Newer Older
1
2
3
4
5
/*! \file iterator.h
 * \brief Define the iterator class template (prototypes)
 * 
 * ----------------------------------------------------------------------------
 * 
6
 * $Id$
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
 * \author Thomas Forbriger
 * \since 08/12/2002
 * 
 * Define the iterator class template (prototypes)
 *
 * ----
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ----
 *
 * \sa aff::Iterator
 * 
 * Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt) 
 * 
 * REVISIONS and CHANGES 
 *  - 08/12/2002   V1.0   copied from libcontxx
 *  - 23/12/2002   V1.1   (thof)
 *                        - reorganized code: do not inherit from Stepper
 *  - 28/12/2002   ----   (thof)
 *                        - revised: is not affected by new style of base
 *                          classes for constant elements
 *  - 23/12/2003   V1.2   (thof)
 *                        - introduced Browser. Iterator may not be
 *                          instantiated from a const object. It is not
 *                          allowed to create a copy of SharedHeap from a
 *                          const reference to a container.
 *  - 05/07/2005   V1.3   (thof)
 *                        - promise constness of Iterator argument
 *  - 19/06/2006   V1.4   (thof)
 *                        - Browser must use const value types
 * 
 * ============================================================================
 */

// include guard
#ifndef AFF_ITERATOR_H_VERSION

#define AFF_ITERATOR_H_VERSION \
  "AFF_ITERATOR_H   V1.4"
#define AFF_ITERATOR_H_CVSID \
58
  "$Id$"
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200

#include<aff/lib/types.h>

namespace aff {

  /*! \brief Iterator
   *
   * \note
   * This class holds an intrinsic reference to the Container components.
   *
   * \param C any container with appropriate interface (e.g. aff::Array,
   *          aff::Series)
   *
   * \todo
   * Define requirements for the interface a container must provide.
   */
  template<class C>
  class Iterator {
    public:
      //! type of container
      typedef C Tcontainer;
      //! representation class
      typedef typename C::Trepresentation Trepresentation;
      //! shape class
      typedef typename C::Tshape Tshape;
      //! stepper base class
      typedef typename C::Tshape::Tstepper Tstepper;
      //! value type
      typedef typename C::Tvalue Tvalue;
      //! value type
      typedef typename C::Treference Treference;
      //! value type
      typedef typename C::Tpointer Tpointer;
      //! only this constructor
      Iterator(const C& c): 
        Mstepper(c.shape()), Mrepresentation(c.representation()) { }
      
      //! delegate to stepper
      //@{
      bool more() const { return(Mstepper.more()); }
      bool less() const { return(Mstepper.less()); }
      bool valid() const { return(Mstepper.valid()); }
      Iterator& incr() { Mstepper.incr(); return(*this); }
      Iterator& decr() { Mstepper.decr(); return(*this); }
      Iterator& tofirst() { Mstepper.tofirst(); return(*this); }
      Iterator& tolast() { Mstepper.tolast(); return(*this); }
      //@}

      //! synonym for increment
      Iterator& operator++()
      { return(this->incr()); }

      //! synonym for decrement
      Iterator& operator--()
      { return(this->decr()); }

      //! access element
      Treference operator*() 
      { return(Mrepresentation[Mstepper.current()]); }

      //! member selection operator
      Tpointer operator->() 
      { return(&(Mrepresentation[Mstepper.current()])); }

    private:
      //! Stepper to use
      Tstepper Mstepper;
      //! Representation to access
      Trepresentation Mrepresentation;
  }; // class Iterator

  /*! \brief Browser
   *
   * \note
   * This class holds an intrinsic reference to the Container components.
   *
   * \param C any container with appropriate interface (e.g. aff::Array,
   *          aff::Series)
   *
   * \todo
   * Define requirements for the interface a container must provide.
   */
  template<class C>
  class Browser {
    public:
      //! type of container
      typedef typename C::Tcontainer_of_const Tcontainer;
      //! representation class
      typedef typename Tcontainer::Trepresentation::Tcoc Trepresentation;
      //! shape class
      typedef typename Tcontainer::Tshape Tshape;
      //! stepper base class
      typedef typename Tcontainer::Tshape::Tstepper Tstepper;
      //! value type
      typedef typename Tcontainer::Tconst_value Tvalue;
      //! value type
      typedef typename Tcontainer::Tconst_reference Treference;
      //! value type
      typedef typename Tcontainer::Tconst_pointer Tpointer;
      //! only this constructor
      Browser(const Tcontainer& c): 
        Mstepper(c.shape()), Mrepresentation(c.representation()) { }
      
      //! delegate to stepper
      //@{
      bool more() const { return(Mstepper.more()); }
      bool less() const { return(Mstepper.less()); }
      bool valid() const { return(Mstepper.valid()); }
      Browser& incr() { Mstepper.incr(); return(*this); }
      Browser& decr() { Mstepper.decr(); return(*this); }
      Browser& tofirst() { Mstepper.tofirst(); return(*this); }
      Browser& tolast() { Mstepper.tolast(); return(*this); }
      //@}

      //! synonym for increment
      Browser& operator++()
      { return(this->incr()); }

      //! synonym for decrement
      Browser& operator--()
      { return(this->decr()); }

      //! access element
      Treference operator*() const
      { return(Mrepresentation[Mstepper.current()]); }

      //! member selection operator
      Tpointer operator->() const
      { return(&(Mrepresentation[Mstepper.current()])); }

    private:
      //! Stepper to use
      Tstepper Mstepper;
      //! Representation to access
      Trepresentation Mrepresentation;
  }; // class Browser

} // namespace aff

#endif // AFF_ITERATOR_H_VERSION (includeguard)

/* ----- END OF iterator.h ----- */