Lolly 1.4.28
Loading...
Searching...
No Matches
iterator.ipp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : iterator.cpp
4 * DESCRIPTION: dynamic iterators
5 * COPYRIGHT : (C) 1999 Joris van der Hoeven
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12#ifndef ITERATOR_CC
13#define ITERATOR_CC
14#include "hashmap.hpp"
15#include "hashset.hpp"
16#include "iterator.hpp"
17
18template <class T>
19int
21 if (busy ()) return -1;
22 else return 0;
23}
24
25template <class T>
28 bool flag= false;
29 out << "[ ";
30 while (it->busy ()) {
31 if (flag) out << ", ";
32 else flag= true;
33 out << it->next ();
34 }
35 return out << " ]";
36}
37
38// hashset_iterator
39template <class T> class hashset_iterator_rep : public iterator_rep<T> {
41 int i;
43 void spool ();
44
45public:
47 bool busy ();
48 T next ();
49 T current ();
50 void increase ();
51};
52
53template <class T>
56
57template <class T>
58void
60 if (i >= h->n) return;
61 while (is_nil (l)) {
62 if ((++i) >= h->n) return;
63 l= h->a[i];
64 }
65}
66
67template <class T>
68bool
70 spool ();
71 return i < h->n;
72}
73
74template <class T>
75T
77 ASSERT (busy (), "end of iterator");
78 T x (l->item);
79 l= l->next;
80 return x;
81}
82
83template <class T>
84T
86 ASSERT (busy (), "end of iterator");
87 T x (l->item);
88 return x;
89}
90
91template <class T>
92void
94 l= l->next;
95}
96
97template <class T>
102// end hashset_iterator
103
104// hashmap_iterator
105template <class T, class U>
108 int i;
110 void spool ();
111
112public:
114 bool busy ();
115 T next ();
116 T current ();
117 void increase ();
118};
119
120template <class T, class U>
123
124template <class T, class U>
125void
127 if (i >= h->n) return;
128 while (is_nil (l)) {
129 if ((++i) >= h->n) return;
130 l= h->a[i];
131 }
132}
133
134template <class T, class U>
135bool
137 spool ();
138 return i < h->n;
139}
140
141template <class T, class U>
142T
144 ASSERT (busy (), "end of iterator");
145 T x (l->item.key);
146 l= l->next;
147 return x;
148}
149
150template <class T, class U>
151T
153 ASSERT (busy (), "end of iterator");
154 T x (l->item.key);
155 return x;
156}
157
158template <class T, class U>
159void
161 l= l->next;
162}
163
164template <class T, class U>
169// hashmap_iterator
170
171#endif // defined ITERATOR_CC
#define ASSERT(cond, msg)
Macro used to assert that a condition is true, and throw an exception with an error message if the co...
Definition basic.hpp:85
bool busy()
Returns whether the iterator has another element.
Definition iterator.ipp:136
hashmap_iterator_rep(hashmap< T, U > h)
Definition iterator.ipp:121
void increase()
Increase the iterator.
Definition iterator.ipp:160
T next()
Returns the current element of the iterator, and move the iterator forward.
Definition iterator.ipp:143
T current()
Returns the current element of the iterator.
Definition iterator.ipp:152
list< hashentry< T, U > > l
Definition iterator.ipp:109
hashmap< T, U > h
Definition iterator.ipp:107
void increase()
Increase the iterator.
Definition iterator.ipp:93
bool busy()
Returns whether the iterator has another element.
Definition iterator.ipp:69
hashset_iterator_rep(hashset< T > h)
Definition iterator.ipp:54
T next()
Returns the current element of the iterator, and move the iterator forward.
Definition iterator.ipp:76
T current()
Returns the current element of the iterator.
Definition iterator.ipp:85
An abstract base class for iterator implementation.
Definition iterator.hpp:21
virtual int remains()
Returns the number of elements remaining in the iterator.
Definition iterator.ipp:20
The list class represents a linked list.
Definition list.hpp:48
iterator< T > iterate(hashset< T > h)
Generates an iterator for a container of type hashset<T>.
Definition iterator.ipp:99
tm_ostream & operator<<(tm_ostream &out, iterator< T > it)
Outputs the iterator object to an output stream.
Definition iterator.ipp:27
bool is_nil(list< T > l)
Check if a list is nil (i.e., an empty list).