FreeNOS
HashIterator.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_HASHITERATOR_H
19 #define __LIBSTD_HASHITERATOR_H
20 
21 #include "Macros.h"
22 #include "Types.h"
23 #include "Iterator.h"
24 #include "ListIterator.h"
25 #include "HashTable.h"
26 #include "Assert.h"
27 
39 template <class K, class V> class HashIterator : public Iterator<V>
40 {
41  public:
42 
49  : m_hash(hash), m_keys(hash.keys()), m_iter(m_keys)
50  {
51  }
52 
56  virtual ~HashIterator()
57  {
58  }
59 
63  virtual void reset()
64  {
65  m_iter.reset();
66  }
67 
73  virtual bool hasNext() const
74  {
75  return m_iter.hasNext();
76  }
77 
83  virtual bool hasCurrent() const
84  {
85  return m_iter.hasCurrent();
86  }
87 
93  virtual const V & current() const
94  {
95  return m_hash[m_iter.current()];
96  }
97 
103  virtual V & current()
104  {
105  return m_hash[m_iter.current()];
106  }
107 
113  virtual const K & key()
114  {
115  return m_iter.current();
116  }
117 
126  virtual V & next()
127  {
128  return m_hash[m_iter.next()];
129  }
130 
136  virtual bool remove()
137  {
138  K key = m_iter.current();
139  m_iter.remove();
140  return m_hash.remove(key);
141  }
142 
151  virtual void operator ++(int num)
152  {
153  m_iter++;
154  }
155 
156  private:
157 
160 
163 
166 };
167 
173 #endif /* __LIBSTD_HASHITERATOR_H */
HashTable
Efficient key -> value lookups.
Definition: HashTable.h:44
HashIterator::m_keys
List< K > m_keys
List of keys to iterate.
Definition: HashIterator.h:162
Macros.h
Types.h
ListIterator::next
virtual T & next()
Fetch the next item.
Definition: ListIterator.h:140
HashIterator::remove
virtual bool remove()
Remove the current item from the underlying Container.
Definition: HashIterator.h:136
HashIterator::reset
virtual void reset()
Reset the iterator.
Definition: HashIterator.h:63
hash
Size hash(const String &key, Size mod)
Compute a hash using the FNV algorithm.
Definition: HashFunction.cpp:21
HashIterator::key
virtual const K & key()
Get the current key.
Definition: HashIterator.h:113
HashTable.h
HashIterator::HashIterator
HashIterator(HashTable< K, V > &hash)
Class constructor.
Definition: HashIterator.h:48
ListIterator::remove
virtual bool remove()
Remove the current item from the List.
Definition: ListIterator.h:153
HashIterator
Iterate through a HashTable.
Definition: HashIterator.h:39
HashIterator::hasNext
virtual bool hasNext() const
Check if there is more to iterate.
Definition: HashIterator.h:73
HashIterator::next
virtual V & next()
Fetch the next item.
Definition: HashIterator.h:126
HashIterator::operator++
virtual void operator++(int num)
Increment operator.
Definition: HashIterator.h:151
Iterator
Abstracts an iteration process.
Definition: Iterator.h:34
HashIterator::~HashIterator
virtual ~HashIterator()
Destructor.
Definition: HashIterator.h:56
Assert.h
HashIterator::current
virtual const V & current() const
Get the current value (read-only).
Definition: HashIterator.h:93
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
HashIterator::m_hash
HashTable< K, V > & m_hash
Points to the HashTable to iterate.
Definition: HashIterator.h:159
Iterator.h
ListIterator.h
ListIterator::hasNext
virtual bool hasNext() const
Check if there is more on the List to iterate.
Definition: ListIterator.h:94
ListIterator::reset
virtual void reset()
Reset the iterator.
Definition: ListIterator.h:83
HashIterator::hasCurrent
virtual bool hasCurrent() const
Check if there is a current item.
Definition: HashIterator.h:83
List< K >
HashIterator::current
virtual V & current()
Get the current value.
Definition: HashIterator.h:103
HashIterator::m_iter
ListIterator< K > m_iter
Iterator of keys.
Definition: HashIterator.h:165
ListIterator< K >