Lolly 1.4.27
Loading...
Searching...
No Matches
iterator.hpp
Go to the documentation of this file.
1
2/** \file iterator.hpp
3 * \copyright GPLv3
4 * \details dynamic iterators
5 * \author Joris van der Hoeven
6 * \date 1999
7 */
8
9#ifndef ITERATOR_H
10#define ITERATOR_H
11#include "hashmap.hpp"
12#include "hashset.hpp"
13
14extern int iterator_count;
15
16/**
17 * @class iterator_rep
18 * @brief An abstract base class for iterator implementation.
19 * @tparam T The type for the iterator's return value.
20 */
21template <class T> class iterator_rep : public abstract_struct {
22public:
23 /**
24 * @brief Constructor. Increments the iterator count.
25 */
26 inline iterator_rep<T> () { TM_DEBUG (iterator_count++); }
27
28 /**
29 * @brief Destructor. Decrements the iterator count.
30 */
31 inline virtual ~iterator_rep<T> () { TM_DEBUG (iterator_count--); }
32
33 /**
34 * @brief Returns whether the iterator has another element.
35 * @return True if the iterator has another element, false otherwise.
36 */
37 virtual bool busy ()= 0;
38
39 /**
40 * @brief Returns the current element of the iterator, and move the iterator
41 * forward
42 * @return The current element of the iterator.
43 */
44 virtual T next ()= 0;
45
46 /**
47 * @brief Returns the current element of the iterator.
48 * @return The current element of the iterator.
49 */
50 virtual T current ()= 0;
51
52 /**
53 * @brief Increase the iterator
54 */
55 virtual void increase ()= 0;
56
57 /**
58 * @brief Returns the number of elements remaining in the iterator.
59 * @return The number of elements remaining in the iterator.
60 */
61 virtual int remains ();
62};
63
64/**
65 * @class iterator
66 * @brief A template class for iterators.
67 * @tparam T The type for the iterator's return value.
68 */
69template <class T> struct iterator {
71 iterator<T> begin () { return *this; };
72 std::nullptr_t end () { return nullptr; };
73 void operator++ () { this->rep->increase (); };
74 /**
75 * @brief Returns the current element of the iterator, then move the iterator
76 * to the next element.
77 */
78 T operator* () { return this->rep->current (); };
79 bool operator!= (std::nullptr_t) { return this->rep->busy (); }
80};
82
83/**
84 * @brief Outputs the iterator object to an output stream.
85 * @tparam T The type for the iterator's return value.
86 * @param out The output stream.
87 * @param it The iterator object.
88 * @return The output stream.
89 */
90template <class T> tm_ostream& operator<< (tm_ostream& out, iterator<T> it);
91
92/**
93 * @brief Generates an iterator for a container of type hashmap<T, U>.
94 * @tparam T The type of the elements in the container.
95 * @tparam U The type of the values in the hashmap.
96 * @param h The hashmap container.
97 * @return An iterator object for the hashmap container.
98 */
99template <class T, class U> iterator<T> iterate (hashmap<T, U> h);
100
101/**
102 * @brief Generates an iterator for a container of type hashset<T>.
103 * @tparam T The type of the elements in the container.
104 * @param h The hashset container.
105 * @return An iterator object for the hashset container.
106 */
107template <class T> iterator<T> iterate (hashset<T> h);
108
109#include "iterator.ipp"
110
111#endif // defined ITERATOR_H
#define TM_DEBUG(x)
Debugging macro used to disable debugging output.
Definition classdef.hpp:13
#define ABSTRACT_TEMPLATE_CODE(PTR, TT, T)
Macro to define a templated abstract pointer type with code.
Definition classdef.hpp:336
An abstract base class for iterator implementation.
Definition iterator.hpp:21
virtual T next()=0
Returns the current element of the iterator, and move the iterator forward.
virtual void increase()=0
Increase the iterator.
virtual bool busy()=0
Returns whether the iterator has another element.
virtual int remains()
Returns the number of elements remaining in the iterator.
Definition iterator.ipp:20
virtual T current()=0
Returns the current element of the iterator.
The list class represents a linked list.
Definition list.hpp:48
list(T item)
Construct a new list object with a single item.
Definition list.hpp:137
int iterator_count
tm_ostream & operator<<(tm_ostream &out, iterator< T > it)
Outputs the iterator object to an output stream.
Definition iterator.ipp:27
iterator< T > iterate(hashmap< T, U > h)
Generates an iterator for a container of type hashmap<T, U>.
Definition iterator.ipp:166
Structure representing an abstract object with a reference count.
Definition classdef.hpp:49
A template class for iterators.
Definition iterator.hpp:69
T operator*()
Returns the current element of the iterator, then move the iterator to the next element.
Definition iterator.hpp:78
std::nullptr_t end()
Definition iterator.hpp:72
iterator< T > begin()
Definition iterator.hpp:71
void operator++()
Definition iterator.hpp:73
ABSTRACT_TEMPLATE(iterator, T)
bool operator!=(std::nullptr_t)
Definition iterator.hpp:79
base class of resources
Definition resource.hpp:23