/**************************************************//**
@file include/hash0hash.h
The simple hash table utility
Created 5/20/1997 Heikki Tuuri
*******************************************************/
#pragma once #include"ut0rnd.h" #include"ut0new.h"
struct hash_cell_t
{ /** singly-linked, nullptr terminated list of hash buckets */ void *node;
private: /** @return pointer to the first element
@tparam T type of the element */ template<typename T> T **begin() noexcept
{ returnreinterpret_cast<T**>(&node); } /** @return pointer to the last element @tparamTtypeoftheelement
@param next the next-element pointer in T */ template<typename T> T **end(T *T::*next) noexcept
{
T **prev; for (prev= begin<T>(); *prev; prev= &((*prev)->*next)); return prev;
}
public: /** Append an element. @tparamTtypeoftheelement @paraminsertthebeing-insertedelement
@param next the next-element pointer in T */ template<typename T> void append(T &insert, T *T::*next) noexcept
{
insert.*next= nullptr;
*end<T>(next)= &insert;
}
/** Find for an element. @tparamTtypeoftheelement @tparamUnaryPredunarypredicate @paramnextthenext-elementpointerinT @paramuunarypredicateforsearchingtheelement @returnthefirstmatchingelement
@retval nullptr if not found */ template<typename T,typename UnaryPred>
T *find(T *T::*next, UnaryPred u) const noexcept
{
T *n; for (n= static_cast<T*>(node); n && !u(n); n= n->*next); return n;
}
/** Search for a pointer to an element. @tparamTtypeoftheelement @tparamUnaryPredunarypredicate @paramnextthenext-elementpointerinT @paramuunarypredicateforsearchingtheelement @returnpointertothefirstmatchingelement,
or to the last element in the chain */ template<typename T,typename UnaryPred>
T **search(T *T::*next, UnaryPred u) noexcept
{
T **prev; for (prev= begin<T>(); !u(*prev); prev= &((*prev)->*next)); return prev;
}
/** Remove an element. @tparamTtypeoftheelement @paramprevpointertotheelementtoberemoved
@param next the next-element pointer in T */ template<typename T> void remove(T **prev, T *T::*next) noexcept
{
T &element= **prev;
*prev= element.*next;
element.*next= nullptr;
}
/** Remove an element. @tparamTtypeoftheelement @paramelementthebeing-removedelement
@param next the next-element pointer in T */ template<typename T> void remove(const T &element, T *T::*next) noexcept
{
remove(search(next, [&element](const T *p){return p==&element;}), next);
}
/** Insert an element after another. @tparamTtypeoftheelement @paramaftertheelementafterwhichtoinsert @paraminsertthebeing-insertedelement
@param next the next-element pointer in T */ template <typename T> void insert_after(T &after, T &insert, T *T::*next)
{ #ifdef UNIV_DEBUG for (const T *c= static_cast<const T *>(node); c; c= c->*next) if (c == &after) goto found;
ut_error;
found: #endif
insert.*next= after.*next;
after.*next= &insert;
}
};
/** Hash table with singly-linked overflow lists */ struct hash_table_t
{ /** number of elements in array (a prime number) */
ulint n_cells; /** the hash array */
hash_cell_t *array;
/** Create the hash table.
@param n the lower bound of n_cells */ void create(ulint n) noexcept
{
n_cells= ut_find_prime(n);
array= static_cast<hash_cell_t*>(ut_zalloc_nokey(n_cells * sizeof *array));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.