Edinburgh Speech Tools  2.1-release
EST_TIterator.h
Go to the documentation of this file.
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 #include <cstddef>
38 
39 /** Template class defining interface to an iterator, i.e an object
40  * which returns elements from a structure one at a time.
41  *
42  * This is template is usually hidden in the declaration of the
43  * container classes with a typedef for Entries providing a more
44  * convenient name for the iterator. However the interface is that
45  * defined here.
46  *
47  * We support two interfaces, a pointer like interface similar to
48  * specialised iteration code elsewhere in the speech tools library
49  * and to the iterators in the C++ standard template library and an
50  * interface similar to that of Enumerations in Java.
51  *
52  * @code{.cpp}
53  * MyContainer::Entries them;
54  *
55  * for(them.begin(container); them; them++)
56  * {
57  * MyContainer::Entry &it = *them;
58  * // Do Something With it
59  * }
60  * @endcode
61  *
62  * @code{.cpp}
63  * MyContainer::Entries them;
64  *
65  * them.begin(container);
66  * while (them.has_more_entries())
67  * {
68  * MyContainer::Entry &it = them.next_entry();
69  * // Do Something With it
70  * }
71  * @endcode
72  *
73  * @author Richard Caley <rjc@cstr.ed.ac.uk>
74  * @version $Id: EST_TIterator.h,v 1.7 2013/04/13 14:17:11 awb Exp $
75  */
76 
77 template <class Container, class IPointer, class Entry>
79 template <class Container, class IPointer, class Entry>
81 template <class Container, class IPointer, class Entry>
83 
84 template <class Container, class IPointer, class Entry>
86 {
87 protected:
88  /// The container we are looking at.
89  Container *cont;
90 
91  /// Position in the structure. May or may not be useful.
92  unsigned int pos;
93 
94  /** Structure defined by the container class which contains the
95  * current state of the iteration.
96  */
97  IPointer pointer;
98 
99 public:
100  /// Name for an iterator like this
102 
103  /// Create an iterator not associated with any specific container.
104  EST_TIterator() {cont=NULL; pos=0;}
105 
106  /// Create an iterator ready to run over the given container.
107  EST_TIterator(const Container &over)
108  { begin(over); }
109 
110  /// Copy an iterator by assignment
111  Iter &operator = (const Iter &orig)
112  { cont=orig.cont; pos=orig.pos; pointer=orig.pointer; return *this;}
113 
114  /// Assigning a container to an iterator sets it ready to start.
115  Iter &operator = (const Container &over)
116  { begin(over); return *this;}
117 
118  /// Set the iterator ready to run over this container.
119  void begin(const Container &over)
120  {cont=((Container *)(void *)&over); beginning();}
121 
122  /// Reset to the start of the container.
123  void beginning()
124  {if (cont) cont->point_to_first(pointer); pos=0;}
125 
126  /**@name End Tests
127  */
128  ///@{
129  /// True if there are more elements to look at.
130  bool has_more_elements() const
131  {return cont && cont->points_to_something(pointer);}
132 
133  /// True when there are no more.
134  bool at_end() const
135  {return !has_more_elements();}
136 
137  /** Viewing the iterator as an integer (for instance in a test)
138  * sees a non-zero value iff there are elements still to look at.
139  */
140  operator int() const
141  {return has_more_elements();}
142  ///@}
143 
144  /**@name Moving Forward
145  */
146  ///@{
147  /// Next moves to the next entry.
148  void next()
149  {cont->move_pointer_forwards(pointer); pos++;}
150 
151  /// The increment operator does the same as next.
152  Iter &operator ++()
153  {next(); return *this;}
154  Iter operator ++(int dummy)
155  {
156  (void)dummy;
157  Iter old =*this;
158  next();
159  return old;
160  }
161  ///@}
162 
163  /**@name Access
164  */
165  ///@{
166  /// Return the element currently pointed to.
167  const Entry& current() const
168  {return cont->points_at(pointer);}
169 
170  /// The * operator returns the current element.
171  const Entry &operator *() const
172  {return current();}
173 
174 #if 0
175  // This only works for some Entry types.
176  const Entry *operator ->() const
177  {return &current();}
178 #endif
179 
180  /// Return the current element and move the pointer forwards.
181  const Entry& next_element()
182  {
183  const Entry &it = cont->points_at(pointer);
184  cont->move_pointer_forwards(pointer);
185  return it;
186  }
187 
188  /// Return the current position
189 
190  unsigned int n() const { return pos; }
191  ///@}
192 
193  friend class EST_TStructIterator <Container, IPointer, Entry>;
194  friend class EST_TRwIterator <Container, IPointer, Entry>;
195  friend class EST_TRwStructIterator <Container, IPointer, Entry>;
196 
197 };
198 
199 /** @class EST_TStructIterator
200  * @ingroup supportclasses
201  */
202 template <class Container, class IPointer, class Entry>
203 class EST_TStructIterator
204  : public EST_TIterator<Container, IPointer, Entry>
205 {
206 public:
207 
209 
210  /// Create an iterator not associated with any specific container.
212 
213  /// Copy an iterator by assignment
214  Iter &operator = (const Iter &orig)
215  { this->cont=orig.cont; this->pos=orig.pos; this->pointer=orig.pointer; return *this;}
216 
217  /// Create an iterator ready to run over the given container.
218  EST_TStructIterator(const Container &over)
219  { this->begin(over); }
220 
221  const Entry *operator ->() const
222  {return &this->current();}
223 };
224 
225 template <class Container, class IPointer, class Entry>
226 class EST_TRwIterator
227  : public EST_TIterator<Container, IPointer, Entry>
228 {
229 private:
230  /// Can't access constant containers this way.
231  // EST_TRwIterator(const Container &over) { (void) over; }
232 
233  /// Can't access constant containers this way.
234  // void begin(const Container &over) { (void) over; }
235 
236 public:
237 
239 
240  /// Create an iterator not associated with any specific container.
242 
243  /// Copy an iterator by assignment
244  Iter &operator = (const Iter &orig)
245  { this->cont=orig.cont; this->pos=orig.pos; this->pointer=orig.pointer; return *this;}
246 
247  /// Create an iterator ready to run over the given container.
248  EST_TRwIterator(Container &over)
249  { begin(over); }
250 
251  /// Set the iterator ready to run over this container.
252  void begin(Container &over)
253  {this->cont=&over; this->beginning();}
254 
255  /**@name Access
256  */
257  ///@{
258  /// Return the element currently pointed to.
259  Entry& current() const
260  {return this->cont->points_at(this->pointer);}
261 
262  /// The * operator returns the current element.
263  Entry &operator *() const
264  {return current();}
265 
266 #if 0
267  Entry *operator ->() const
268  {return &current();}
269 #endif
270 
271  /// Return the current element and move the pointer forwards.
272  Entry& next_element()
273  {
274  Entry &it = this->cont->points_at(this->pointer);
275  this->cont->move_pointer_forwards(this->pointer);
276  return it;
277  }
278 
279  ///@}
280 };
281 
282 template <class Container, class IPointer, class Entry>
284  : public EST_TRwIterator<Container, IPointer, Entry>
285 {
286 public:
287 
289 
290  /// Create an iterator not associated with any specific container.
292 
293  /// Copy an iterator by assignment
294  Iter &operator = (const Iter &orig)
295  { this->cont=orig.cont; this->pos=orig.pos; this->pointer=orig.pointer; return *this;}
296 
297  /// Create an iterator ready to run over the given container.
298  EST_TRwStructIterator(Container &over)
299  { this->begin(over); }
300 
301  Entry *operator ->() const
302  {return &this->current();}
303 };
304 
305 #endif
IPointer pointer
Definition: EST_TIterator.h:97
EST_TStructIterator()
Create an iterator not associated with any specific container.
const Entry & operator*() const
The * operator returns the current element.
Container * cont
The container we are looking at.
Definition: EST_TIterator.h:89
void next()
Next moves to the next entry.
EST_TRwIterator< Container, IPointer, Entry > Iter
Can&#39;t access constant containers this way.
unsigned int pos
Position in the structure. May or may not be useful.
Definition: EST_TIterator.h:92
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.
EST_TRwIterator(Container &over)
Create an iterator ready to run over the given container.
EST_TRwStructIterator(Container &over)
Create an iterator ready to run over the given container.
unsigned int n() const
Return the current position.
EST_TStructIterator(const Container &over)
Create an iterator ready to run over the given container.
EST_TRwStructIterator< Container, IPointer, Entry > Iter
void begin(Container &over)
Set the iterator ready to run over this container.
Entry & next_element()
Return the current element and move the pointer forwards.
EST_TRwIterator()
Create an iterator not associated with any specific container.
EST_TRwStructIterator()
Create an iterator not associated with any specific container.
bool at_end() const
True when there are no more.
NULL
Definition: EST_WFST.cc:55
getString int
Definition: EST_item_aux.cc:50
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.
void beginning()
Reset to the start of the container.
void begin(const Container &over)
Set the iterator ready to run over this container.
EST_TIterator()
Create an iterator not associated with any specific container.
EST_TIterator< Container, IPointer, Entry > Iter
Iter & operator=(const Iter &orig)
Copy an iterator by assignment.
Iter & operator++()
The increment operator does the same as next.
Entry & current() const
Return the element currently pointed to.
bool has_more_elements() const
True if there are more elements to look at.