Skip to content

Commit 553815e

Browse files
committed
cleanup: C++11 range-based for loop for Hash_set<>
1 parent d6add9a commit 553815e

File tree

4 files changed

+45
-20
lines changed

4 files changed

+45
-20
lines changed

include/hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ my_bool my_hash_init2(PSI_memory_key psi_key, HASH *hash, size_t growth_size,
7171
void (*free_element)(void*), uint flags);
7272
void my_hash_free(HASH *tree);
7373
void my_hash_reset(HASH *hash);
74-
uchar *my_hash_element(HASH *hash, size_t idx);
74+
uchar *my_hash_element(const HASH *hash, size_t idx);
7575
uchar *my_hash_search(const HASH *info, const uchar *key, size_t length);
7676
uchar *my_hash_search_using_hash_value(const HASH *info,
7777
my_hash_value_type hash_value,

mysys/hash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ my_bool my_hash_update(HASH *hash, uchar *record, uchar *old_key,
762762
}
763763

764764

765-
uchar *my_hash_element(HASH *hash, size_t idx)
765+
uchar *my_hash_element(const HASH *hash, size_t idx)
766766
{
767767
if (idx < hash->records)
768768
return dynamic_element(&hash->array,idx,HASH_LINK*)->data;

sql/debug_sync.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,8 @@ struct st_debug_sync_globals
9797

9898
void clear_set()
9999
{
100-
Hash_set<LEX_CSTRING>::Iterator it{ds_signal_set};
101-
LEX_CSTRING *s;
102-
while ((s= it++))
103-
my_free(s);
100+
for (LEX_CSTRING &s : ds_signal_set)
101+
my_free(&s);
104102
ds_signal_set.clear();
105103
}
106104

sql/sql_hset.h

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,53 @@ class Hash_set
8585
return reinterpret_cast<T*>(my_hash_element(const_cast<HASH*>(&m_hash), i));
8686
}
8787
/** An iterator over hash elements. Is not insert-stable. */
88+
class Iterator;
89+
using value_type= T;
90+
using iterator= Iterator;
91+
using const_iterator= const Iterator;
92+
93+
Iterator begin() const { return Iterator(*this, 0); }
94+
Iterator end() const { return Iterator(*this, m_hash.records); }
95+
8896
class Iterator
8997
{
9098
public:
91-
Iterator(Hash_set &hash_set)
92-
: m_hash(&hash_set.m_hash),
93-
m_idx(0)
94-
{}
95-
/**
96-
Return the current element and reposition the iterator to the next
97-
element.
98-
*/
99-
inline T *operator++(int)
99+
using iterator_category= std::forward_iterator_tag;
100+
using value_type= T;
101+
using difference_type= std::ptrdiff_t;
102+
using pointer= T *;
103+
using reference= T &;
104+
105+
Iterator(const Hash_set &hash_set, uint idx=0) :
106+
m_hash(&hash_set.m_hash), m_idx(idx) {}
107+
108+
Iterator &operator++()
109+
{
110+
DBUG_ASSERT(m_idx < m_hash->records);
111+
m_idx++;
112+
return *this;
113+
}
114+
115+
T &operator*()
116+
{
117+
return *reinterpret_cast<T *>(my_hash_element(m_hash, m_idx));
118+
}
119+
120+
T *operator->()
121+
{
122+
return reinterpret_cast<T *>(my_hash_element(m_hash, m_idx));
123+
}
124+
125+
bool operator==(const typename Hash_set<T>::iterator &rhs)
126+
{
127+
return m_idx == rhs.m_idx && m_hash == rhs.m_hash;
128+
}
129+
bool operator!=(const typename Hash_set<T>::iterator &rhs)
100130
{
101-
if (m_idx < m_hash->records)
102-
return reinterpret_cast<T*>(my_hash_element(m_hash, m_idx++));
103-
return NULL;
131+
return m_idx != rhs.m_idx || m_hash != rhs.m_hash;
104132
}
105-
void rewind() { m_idx= 0; }
106133
private:
107-
HASH *m_hash;
134+
const HASH *m_hash;
108135
uint m_idx;
109136
};
110137
private:

0 commit comments

Comments
 (0)