cleanup: C++11 range-based for loop for Hash_set<>

This commit is contained in:
Sergei Golubchik 2024-07-12 10:38:48 +02:00
parent d6add9a03d
commit 553815ea24
4 changed files with 45 additions and 20 deletions

View file

@ -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,

View file

@ -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;

View file

@ -97,10 +97,8 @@ struct st_debug_sync_globals
void clear_set()
{
Hash_set<LEX_CSTRING>::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();
}

View file

@ -85,26 +85,53 @@ public:
return reinterpret_cast<T*>(my_hash_element(const_cast<HASH*>(&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<T*>(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<T *>(my_hash_element(m_hash, m_idx));
}
T *operator->()
{
return reinterpret_cast<T *>(my_hash_element(m_hash, m_idx));
}
bool operator==(const typename Hash_set<T>::iterator &rhs)
{
return m_idx == rhs.m_idx && m_hash == rhs.m_hash;
}
bool operator!=(const typename Hash_set<T>::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: