diff --git a/include/hash.h b/include/hash.h index c0a846ac120..8158c2adb5d 100644 --- a/include/hash.h +++ b/include/hash.h @@ -71,7 +71,7 @@ my_bool my_hash_init2(PSI_memory_key psi_key, HASH *hash, size_t growth_size, void (*free_element)(void*), uint flags); void my_hash_free(HASH *tree); void my_hash_reset(HASH *hash); -uchar *my_hash_element(HASH *hash, size_t idx); +uchar *my_hash_element(const HASH *hash, size_t idx); uchar *my_hash_search(const HASH *info, const uchar *key, size_t length); uchar *my_hash_search_using_hash_value(const HASH *info, my_hash_value_type hash_value, diff --git a/mysys/hash.c b/mysys/hash.c index df49e0b17c8..4db18881fc2 100644 --- a/mysys/hash.c +++ b/mysys/hash.c @@ -762,7 +762,7 @@ my_bool my_hash_update(HASH *hash, uchar *record, uchar *old_key, } -uchar *my_hash_element(HASH *hash, size_t idx) +uchar *my_hash_element(const HASH *hash, size_t idx) { if (idx < hash->records) return dynamic_element(&hash->array,idx,HASH_LINK*)->data; diff --git a/sql/debug_sync.cc b/sql/debug_sync.cc index b6f413e2459..01177d30390 100644 --- a/sql/debug_sync.cc +++ b/sql/debug_sync.cc @@ -97,10 +97,8 @@ struct st_debug_sync_globals void clear_set() { - Hash_set::Iterator it{ds_signal_set}; - LEX_CSTRING *s; - while ((s= it++)) - my_free(s); + for (LEX_CSTRING &s : ds_signal_set) + my_free(&s); ds_signal_set.clear(); } diff --git a/sql/sql_hset.h b/sql/sql_hset.h index 41573fb5f03..80e45b160a9 100644 --- a/sql/sql_hset.h +++ b/sql/sql_hset.h @@ -85,26 +85,53 @@ public: return reinterpret_cast(my_hash_element(const_cast(&m_hash), i)); } /** An iterator over hash elements. Is not insert-stable. */ + class Iterator; + using value_type= T; + using iterator= Iterator; + using const_iterator= const Iterator; + + Iterator begin() const { return Iterator(*this, 0); } + Iterator end() const { return Iterator(*this, m_hash.records); } + class Iterator { public: - Iterator(Hash_set &hash_set) - : m_hash(&hash_set.m_hash), - m_idx(0) - {} - /** - Return the current element and reposition the iterator to the next - element. - */ - inline T *operator++(int) + using iterator_category= std::forward_iterator_tag; + using value_type= T; + using difference_type= std::ptrdiff_t; + using pointer= T *; + using reference= T &; + + Iterator(const Hash_set &hash_set, uint idx=0) : + m_hash(&hash_set.m_hash), m_idx(idx) {} + + Iterator &operator++() { - if (m_idx < m_hash->records) - return reinterpret_cast(my_hash_element(m_hash, m_idx++)); - return NULL; + DBUG_ASSERT(m_idx < m_hash->records); + m_idx++; + return *this; + } + + T &operator*() + { + return *reinterpret_cast(my_hash_element(m_hash, m_idx)); + } + + T *operator->() + { + return reinterpret_cast(my_hash_element(m_hash, m_idx)); + } + + bool operator==(const typename Hash_set::iterator &rhs) + { + return m_idx == rhs.m_idx && m_hash == rhs.m_hash; + } + bool operator!=(const typename Hash_set::iterator &rhs) + { + return m_idx != rhs.m_idx || m_hash != rhs.m_hash; } - void rewind() { m_idx= 0; } private: - HASH *m_hash; + const HASH *m_hash; uint m_idx; }; private: