FreeNOS
ListIterator.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef __LIBSTD_LISTITERATOR_H
19 #define __LIBSTD_LISTITERATOR_H
20 
21 #include "Macros.h"
22 #include "Iterator.h"
23 #include "List.h"
24 #include "Assert.h"
25 
37 template <class T> class ListIterator : public Iterator<T>
38 {
39  public:
40 
47  : m_list(*list)
48  {
49 
50  m_current = ZERO;
51  reset();
52  }
53 
60  : m_list(list)
61  {
62 
63  m_current = ZERO;
64  reset();
65  }
66 
72  ListIterator(const List<T> & list)
73  : m_list((List<T> &) list)
74  {
75 
76  m_current = ZERO;
77  reset();
78  }
79 
83  virtual void reset()
84  {
85  m_current = m_list.head();
86  m_next = m_current;
87  }
88 
94  virtual bool hasNext() const
95  {
96  return m_next != ZERO;
97  }
98 
104  virtual bool hasCurrent() const
105  {
106  return m_current != ZERO;
107  }
108 
114  virtual const T & current() const
115  {
116  return m_current->data;
117  }
118 
124  virtual T & current()
125  {
126  return m_current->data;
127  }
128 
140  virtual T & next()
141  {
142 
143  m_current = m_next;
144  m_next = m_current->next;
145  return m_current->data;
146  }
147 
153  virtual bool remove()
154  {
155  // Do we have a current item?
156  if (!m_current)
157  return false;
158 
159  // Update iterator administration
160  class List<T>::Node *node = m_current;
162  m_next = m_current;
163 
164  // Delete the node on the List
165  m_list.remove(node);
166  return true;
167  }
168 
175  virtual void operator++(int num)
176  {
177  if (m_current)
178  {
179  m_current = m_current->next;
180 
181  if (m_current)
182  m_next = m_current->next;
183  else
184  m_next = ZERO;
185  }
186  }
187 
188  private:
189 
192 
194  class List<T>::Node *m_current;
195 
197  class List<T>::Node *m_next;
198 };
199 
205 #endif /* __LIBSTD_LISTITERATOR_H */
Macros.h
ListIterator::next
virtual T & next()
Fetch the next item.
Definition: ListIterator.h:140
ListIterator::remove
virtual bool remove()
Remove the current item from the List.
Definition: ListIterator.h:153
ListIterator::m_current
class List< T >::Node * m_current
Current node.
Definition: ListIterator.h:194
Iterator
Abstracts an iteration process.
Definition: Iterator.h:34
Assert.h
ListIterator::ListIterator
ListIterator(const List< T > &list)
Constant class constructor.
Definition: ListIterator.h:72
ListIterator::current
virtual T & current()
Get current item in the List.
Definition: ListIterator.h:124
ListIterator::current
virtual const T & current() const
Get current item in the List.
Definition: ListIterator.h:114
ListIterator::hasCurrent
virtual bool hasCurrent() const
Check if there is a current item on the List.
Definition: ListIterator.h:104
ListIterator::ListIterator
ListIterator(List< T > &list)
Class constructor.
Definition: ListIterator.h:59
Iterator.h
ListIterator::hasNext
virtual bool hasNext() const
Check if there is more on the List to iterate.
Definition: ListIterator.h:94
ListIterator::ListIterator
ListIterator(List< T > *list)
Class constructor.
Definition: ListIterator.h:46
ListIterator::reset
virtual void reset()
Reset the iterator.
Definition: ListIterator.h:83
List
Simple linked list template class.
Definition: List.h:36
ListIterator::m_list
List< T > & m_list
Points to the List to iterate.
Definition: ListIterator.h:191
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
ListIterator::m_next
class List< T >::Node * m_next
Next node.
Definition: ListIterator.h:197
List.h
ListIterator
Iterate through a List.
Definition: ListIterator.h:37