-
Notifications
You must be signed in to change notification settings - Fork 4
Hash: Move to template #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
e40e50a
ec0a4b6
63857df
e788628
d3ef0a1
f60b2d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,79 +27,81 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
namespace cppcore { | ||
|
||
//------------------------------------------------------------------------------------------------- | ||
/// @class Hash | ||
/// @class THash | ||
/// @ingroup CPPCore | ||
/// | ||
/// @brief This class is used to calculate the hash value for a given integer or character buffer. | ||
//------------------------------------------------------------------------------------------------- | ||
class Hash { | ||
template<class T> | ||
class THash { | ||
public: | ||
/// @brief The default class constructor. | ||
Hash(); | ||
THash(); | ||
|
||
/// @brief The class constructor with a given hash value. | ||
/// @param hash [in] An integer value to compute the hash from. | ||
explicit Hash(unsigned int hash); | ||
explicit THash(T hash); | ||
|
||
/// @brief The class constructor with a given char buffer. | ||
/// @param value [in] A character buffer to compute the hash from. | ||
/// @param base [in] The table base. | ||
explicit Hash(const char *buffer, unsigned int base); | ||
explicit THash(const char *buffer, T base); | ||
|
||
/// @brief The class constructor with a given unsigned int value. | ||
/// @param value [in] An unsigned int value to compute the hash from. | ||
/// @param base [in] The table base. | ||
explicit Hash(unsigned int value, unsigned int base); | ||
explicit THash(T value, T base); | ||
|
||
/// @brief The class destructor. | ||
~Hash(); | ||
~THash() = default; | ||
|
||
/// @brief Computes the hash value for a given character buffer. | ||
/// @param buffer [in] The buffer. | ||
/// @param base [in] The table base. | ||
/// @return The hash value. | ||
static unsigned int toHash(const char *buffer, unsigned int base); | ||
static T toHash(const char *buffer, T base); | ||
|
||
/// @brief Computes the hash value for a given unsigned int value. | ||
/// @param buffer [in] The unsigned int value. | ||
/// @param base [in] The table base. | ||
/// @return The hash value. | ||
static unsigned int toHash(unsigned int value, unsigned int base); | ||
static T toHash(T value, T base); | ||
|
||
/// brief Returns the stored hash value. | ||
/// @return The hash value. | ||
unsigned int hashValue() const; | ||
T hashValue() const; | ||
|
||
private: | ||
unsigned int m_hash; | ||
T m_hash; | ||
}; | ||
|
||
inline Hash::Hash() : | ||
template <class T> | ||
inline THash<T>::THash() : | ||
m_hash(0) { | ||
// empty | ||
} | ||
|
||
inline Hash::Hash(unsigned int hash) : | ||
template <class T> | ||
inline THash<T>::THash(T hash) : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential data loss when assigning In the constructor |
||
m_hash(hash) { | ||
// empty | ||
} | ||
|
||
inline Hash::Hash(const char *buffer, unsigned int base) : | ||
m_hash(Hash::toHash(buffer, base)) { | ||
template <class T> | ||
inline THash<T>::THash(const char *buffer, T base) : | ||
m_hash(THash::toHash(buffer, base)) { | ||
// empty | ||
} | ||
|
||
inline Hash::Hash(unsigned int value, unsigned int base) : | ||
m_hash(Hash::toHash(value, base)) { | ||
template <class T> | ||
inline THash<T>::THash(T value, T base) : | ||
m_hash(THash::toHash(value, base)) { | ||
// empty | ||
} | ||
|
||
inline Hash::~Hash() { | ||
// empty | ||
} | ||
|
||
inline unsigned int Hash::toHash(const char *buffer, unsigned int base) { | ||
unsigned int hash(0); | ||
template <class T> | ||
inline T THash<T>::toHash(const char *buffer, T base) { | ||
T hash = 0; | ||
Comment on lines
+103
to
+104
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Constrain template parameter The method Add the following static assertion inside the class definition: static_assert(std::is_integral<T>::value, "THash requires T to be an integral type."); Remember to include the #include <type_traits> |
||
if (nullptr == buffer) { | ||
return hash; | ||
} | ||
|
@@ -113,12 +115,14 @@ inline unsigned int Hash::toHash(const char *buffer, unsigned int base) { | |
return hash; | ||
} | ||
|
||
inline unsigned int Hash::toHash(unsigned int value, unsigned int base) { | ||
const unsigned int hash(value % base); | ||
template <class T> | ||
inline T THash<T>::toHash(T value, T base) { | ||
const T hash = value % base; | ||
return hash; | ||
} | ||
|
||
inline unsigned int Hash::hashValue() const { | ||
template <class T> | ||
inline T THash<T>::hashValue() const { | ||
return m_hash; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent types between
hashValue()
return type andm_hash
The method
hashValue()
returns typeT
, but the member variablem_hash
is declared asunsigned int
. This could lead to implicit conversions and potential data loss ifT
differs fromunsigned int
. Consider changingm_hash
to typeT
to ensure type consistency throughout the class.Apply this diff to update the member variable type: