Edinburgh Speech Tools  2.1-release
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
EST_TIterator.h
1  /************************************************************************/
2  /* */
3  /* Centre for Speech Technology Research */
4  /* University of Edinburgh, UK */
5  /* Copyright (c) 1996,1997 */
6  /* All Rights Reserved. */
7  /* */
8  /* Permission is hereby granted, free of charge, to use and distribute */
9  /* this software and its documentation without restriction, including */
10  /* without limitation the rights to use, copy, modify, merge, publish, */
11  /* distribute, sublicense, and/or sell copies of this work, and to */
12  /* permit persons to whom this work is furnished to do so, subject to */
13  /* the following conditions: */
14  /* 1. The code must retain the above copyright notice, this list of */
15  /* conditions and the following disclaimer. */
16  /* 2. Any modifications must be clearly marked as such. */
17  /* 3. Original authors' names are not deleted. */
18  /* 4. The authors' names are not used to endorse or promote products */
19  /* derived from this software without specific prior written */
20  /* permission. */
21  /* */
22  /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23  /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24  /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25  /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26  /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27  /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28  /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29  /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30  /* THIS SOFTWARE. */
31  /* */
32  /************************************************************************/
33 
34 #ifndef __EST_TITERATOR_H__
35 #define __EST_TITERATOR_H__
36 
37 /** Template class defining interface to an iterator, i.e an object
38  * which returns elements from a structure one at a time.
39  *
40  * This is template is usually hidden in the declaration of the
41  * container classes with a typedef for Entries providing a more
42  * convenient name for the iterator. However the interface is that
43  * defined here.
44  *
45  * We support two interfaces, a pointer like interface similar to
46  * specialised iteration code elsewhere in the speech tools library
47  * and to the iterators in the C++ standard template library and an
48  * interface similar to that of Enumerations in Java.
49  *
50  * @code{.cpp}
51  * MyContainer::Entries them;
52  *
53  * for(them.begin(container); them; them++)
54  * {
55  * MyContainer::Entry &it = *them;
56  * // Do Something With it
57  * }
58  * @endcode
59  *
60  * @code{.cpp}
61  * MyContainer::Entries them;
62  *
63  * them.begin(container);
64  * while (them.has_more_entries())
65  * {
66  * MyContainer::Entry &it = them.next_entry();
67  * // Do Something With it
68  * }
69  * @endcode
70  *
71  * @author Richard Caley <rjc@cstr.ed.ac.uk>
72  * @version $Id: EST_TIterator.h,v 1.6 2004/05/04 00:00:16 awb Exp $
73  */
74 
75 template <class Container, class IPointer, class Entry>
77 template <class Container, class IPointer, class Entry>
79 template <class Container, class IPointer, class Entry>
81 
82 template <class Container, class IPointer, class Entry>
84 {
85 protected:
86  /// The container we are looking at.
87  Container *cont;
88 
89  /// Position in the structure. May or may not be useful.
90  unsigned int pos;
91 
92  /** Structure defined by the container class which contains the
93  * current state of the iteration.
94  */
95  IPointer pointer;
96 
97 public:
98  /// Name for an iterator like this
100 
101  /// Create an iterator not associated with any specific container.
102  EST_TIterator() {cont=NULL;}
103 
104  /// Create an iterator ready to run over the given container.
105  EST_TIterator(const Container &over)
106  { begin(over); }
107 
108  /// Copy an iterator by assignment
109  Iter &operator = (const Iter &orig)
110  { cont=orig.cont; pos=orig.pos; pointer=orig.pointer; return *this;}
111 
112  /// Assigning a container to an iterator sets it ready to start.
113  Iter &operator = (const Container &over)
114  { begin(over); return *this;}
115 
116  /// Set the iterator ready to run over this container.
117  void begin(const Container &over)
118  {cont=((Container *)(void *)&over); beginning();}
119 
120  /// Reset to the start of the container.
121  void beginning()
122  {if (cont) cont->point_to_first(pointer); pos=0;}
123 
124  /**@name End Tests
125  */
126  ///@{
127  /// True if there are more elements to look at.
128  bool has_more_elements() const
129  {return cont && cont->points_to_something(pointer);}
130 
131  /// True when there are no more.
132  bool at_end() const
133  {return !has_more_elements();}
134 
135  /** Viewing the iterator as an integer (for instance in a test)
136  * sees a non-zero value iff there are elements still to look at.
137  */
138  operator int() const
139  {return has_more_elements();}
140  ///@}
141 
142  /**@name Moving Forward
143  */
144  ///@{
145  /// Next moves to the next entry.
146  void next()
147  {cont->move_pointer_forwards(pointer); pos++;}
148 
149  /// The increment operator does the same as next.
150  Iter &operator ++()
151  {next(); return *this;}
152  Iter operator ++(int dummy)
153  {
154  (void)dummy;
155  Iter old =*this;
156  next();
157  return old;
158  }
159  ///@}
160 
161  /**@name Access
162  */
163  ///@{
164  /// Return the element currently pointed to.
165  const Entry& current() const
166  {return cont->points_at(pointer);}
167 
168  /// The * operator returns the current element.
169  const Entry &operator *() const
170  {return current();}
171 
172 #if 0
173  // This only works for some Entry types.
174  const Entry *operator ->() const
175  {return &current();}
176 #endif
177 
178  /// Return the current element and move the pointer forwards.
179  const Entry& next_element()
180  {
181  const Entry &it = cont->points_at(pointer);
182  cont->move_pointer_forwards(pointer);
183  return it;
184  }
185 
186  /// Return the current position
187 
188  unsigned int n() const { return pos; }
189  ///@}
190 
191  friend class EST_TStructIterator <Container, IPointer, Entry>;
192  friend class EST_TRwIterator <Container, IPointer, Entry>;
193  friend class EST_TRwStructIterator <Container, IPointer, Entry>;
194 
195 };
196 
197 /** @class EST_TStructIterator
198  * @ingroup supportclasses
199  */
200 template <class Container, class IPointer, class Entry>
201 class EST_TStructIterator
202  : public EST_TIterator<Container, IPointer, Entry>
203 {
204 public:
205 
207 
208  /// Create an iterator not associated with any specific container.
209  EST_TStructIterator() {this->cont=NULL;}
210 
211  /// Copy an iterator by assignment
212  Iter &operator = (const Iter &orig)
213  { this->cont=orig.cont; this->pos=orig.pos; this->pointer=orig.pointer; return *this;}
214 
215  /// Create an iterator ready to run over the given container.
216  EST_TStructIterator(const Container &over)
217  { this->begin(over); }
218 
219  const Entry *operator ->() const
220  {return &this->current();}
221 };
222 
223 template <class Container, class IPointer, class Entry>
224 class EST_TRwIterator
225  : public EST_TIterator<Container, IPointer, Entry>
226 {
227 private:
228  /// Can't access constant containers this way.
229  // EST_TRwIterator(const Container &over) { (void) over; }
230 
231  /// Can't access constant containers this way.
232  // void begin(const Container &over) { (void) over; }
233 
234 public:
235 
237 
238  /// Create an iterator not associated with any specific container.
239  EST_TRwIterator() {this->cont=NULL;}
240 
241  /// Copy an iterator by assignment
242  Iter &operator = (const Iter &orig)
243  { this->cont=orig.cont; this->pos=orig.pos; this->pointer=orig.pointer; return *this;}
244 
245  /// Create an iterator ready to run over the given container.
246  EST_TRwIterator(Container &over)
247  { begin(over); }
248 
249  /// Set the iterator ready to run over this container.
250  void begin(Container &over)
251  {this->cont=&over; this->beginning();}
252 
253  /**@name Access
254  */
255  ///@{
256  /// Return the element currently pointed to.
257  Entry& current() const
258  {return this->cont->points_at(this->pointer);}
259 
260  /// The * operator returns the current element.
261  Entry &operator *() const
262  {return current();}
263 
264 #if 0
265  Entry *operator ->() const
266  {return &current();}
267 #endif
268 
269  /// Return the current element and move the pointer forwards.
270  Entry& next_element()
271  {
272  Entry &it = this->cont->points_at(this->pointer);
273  this->cont->move_pointer_forwards(this->pointer);
274  return it;
275  }
276 
277  ///@}
278 };
279 
280 template <class Container, class IPointer, class Entry>
282  : public EST_TRwIterator<Container, IPointer, Entry>
283 {
284 public:
285 
287 
288  /// Create an iterator not associated with any specific container.
289  EST_TRwStructIterator() {this->cont=NULL;}
290 
291  /// Copy an iterator by assignment
292  Iter &operator = (const Iter &orig)
293  { this->cont=orig.cont; this->pos=orig.pos; this->pointer=orig.pointer; return *this;}
294 
295  /// Create an iterator ready to run over the given container.
296  EST_TRwStructIterator(Container &over)
297  { this->begin(over); }
298 
299  Entry *operator ->() const
300  {return &this->current();}
301 };
302 
303 #endif
Entry & next_element()
Return the current element and move the pointer forwards.
IPointer pointer
Definition: EST_TIterator.h:95
const Entry & operator*() const
The * operator returns the current element.
Iter & operator=(const Iter &orig)
Copy an iterator by assignment.
Container * cont
The container we are looking at.
Definition: EST_TIterator.h:87
void next()
Next moves to the next entry.
unsigned int pos
Position in the structure. May or may not be useful.
Definition: EST_TIterator.h:90
const Entry & next_element()
Return the current element and move the pointer forwards.
EST_TIterator< Container, IPointer, Entry > Iter
Name for an iterator like this.
Definition: EST_TIterator.h:99
unsigned int n() const
Return the current position.
Entry & current() const
Return the element currently pointed to.
Iter & operator=(const Iter &orig)
Copy an iterator by assignment.
bool at_end() const
True when there are no more.
EST_TRwStructIterator()
Create an iterator not associated with any specific container.
EST_TRwStructIterator(Container &over)
Create an iterator ready to run over the given container.
EST_TIterator(const Container &over)
Create an iterator ready to run over the given container.
const Entry & current() const
Return the element currently pointed to.
Entry & operator*() const
The * operator returns the current element.
void beginning()
Reset to the start of the container.
void begin(const Container &over)
Set the iterator ready to run over this container.
EST_TRwIterator(Container &over)
Create an iterator ready to run over the given container.
EST_TIterator()
Create an iterator not associated with any specific container.
EST_TRwIterator()
Create an iterator not associated with any specific container.
Iter & operator=(const Iter &orig)
Copy an iterator by assignment.
EST_TStructIterator(const Container &over)
Create an iterator ready to run over the given container.
Iter & operator++()
The increment operator does the same as next.
EST_TRwIterator< Container, IPointer, Entry > Iter
Can't access constant containers this way.
void begin(Container &over)
Set the iterator ready to run over this container.
EST_TStructIterator()
Create an iterator not associated with any specific container.
Iter & operator=(const Iter &orig)
Copy an iterator by assignment.
bool has_more_elements() const
True if there are more elements to look at.