Skip to content

Commit 0fbdc25

Browse files
authored
Merge pull request #28 from kimkulling/feature/make_hash_to_template
Hash: Move to template
2 parents 12b60d1 + f60b2d7 commit 0fbdc25

File tree

3 files changed

+52
-43
lines changed

3 files changed

+52
-43
lines changed

include/cppcore/Common/Hash.h

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,79 +27,81 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2727
namespace cppcore {
2828

2929
//-------------------------------------------------------------------------------------------------
30-
/// @class Hash
30+
/// @class THash
3131
/// @ingroup CPPCore
3232
///
3333
/// @brief This class is used to calculate the hash value for a given integer or character buffer.
3434
//-------------------------------------------------------------------------------------------------
35-
class Hash {
35+
template<class T>
36+
class THash {
3637
public:
3738
/// @brief The default class constructor.
38-
Hash();
39+
THash();
3940

4041
/// @brief The class constructor with a given hash value.
4142
/// @param hash [in] An integer value to compute the hash from.
42-
explicit Hash(unsigned int hash);
43+
explicit THash(T hash);
4344

4445
/// @brief The class constructor with a given char buffer.
4546
/// @param value [in] A character buffer to compute the hash from.
4647
/// @param base [in] The table base.
47-
explicit Hash(const char *buffer, unsigned int base);
48+
explicit THash(const char *buffer, T base);
4849

4950
/// @brief The class constructor with a given unsigned int value.
5051
/// @param value [in] An unsigned int value to compute the hash from.
5152
/// @param base [in] The table base.
52-
explicit Hash(unsigned int value, unsigned int base);
53+
explicit THash(T value, T base);
5354

5455
/// @brief The class destructor.
55-
~Hash();
56+
~THash() = default;
5657

5758
/// @brief Computes the hash value for a given character buffer.
5859
/// @param buffer [in] The buffer.
5960
/// @param base [in] The table base.
6061
/// @return The hash value.
61-
static unsigned int toHash(const char *buffer, unsigned int base);
62+
static T toHash(const char *buffer, T base);
6263

6364
/// @brief Computes the hash value for a given unsigned int value.
6465
/// @param buffer [in] The unsigned int value.
6566
/// @param base [in] The table base.
6667
/// @return The hash value.
67-
static unsigned int toHash(unsigned int value, unsigned int base);
68+
static T toHash(T value, T base);
6869

6970
/// brief Returns the stored hash value.
7071
/// @return The hash value.
71-
unsigned int hashValue() const;
72+
T hashValue() const;
7273

7374
private:
74-
unsigned int m_hash;
75+
T m_hash;
7576
};
7677

77-
inline Hash::Hash() :
78+
template <class T>
79+
inline THash<T>::THash() :
7880
m_hash(0) {
7981
// empty
8082
}
8183

82-
inline Hash::Hash(unsigned int hash) :
84+
template <class T>
85+
inline THash<T>::THash(T hash) :
8386
m_hash(hash) {
8487
// empty
8588
}
8689

87-
inline Hash::Hash(const char *buffer, unsigned int base) :
88-
m_hash(Hash::toHash(buffer, base)) {
90+
template <class T>
91+
inline THash<T>::THash(const char *buffer, T base) :
92+
m_hash(THash::toHash(buffer, base)) {
8993
// empty
9094
}
9195

92-
inline Hash::Hash(unsigned int value, unsigned int base) :
93-
m_hash(Hash::toHash(value, base)) {
96+
template <class T>
97+
inline THash<T>::THash(T value, T base) :
98+
m_hash(THash::toHash(value, base)) {
9499
// empty
95100
}
96101

97-
inline Hash::~Hash() {
98-
// empty
99-
}
100-
101-
inline unsigned int Hash::toHash(const char *buffer, unsigned int base) {
102-
unsigned int hash(0);
102+
template <class T>
103+
inline T THash<T>::toHash(const char *buffer, T base) {
104+
T hash = 0;
103105
if (nullptr == buffer) {
104106
return hash;
105107
}
@@ -113,12 +115,14 @@ inline unsigned int Hash::toHash(const char *buffer, unsigned int base) {
113115
return hash;
114116
}
115117

116-
inline unsigned int Hash::toHash(unsigned int value, unsigned int base) {
117-
const unsigned int hash(value % base);
118+
template <class T>
119+
inline T THash<T>::toHash(T value, T base) {
120+
const T hash = value % base;
118121
return hash;
119122
}
120123

121-
inline unsigned int Hash::hashValue() const {
124+
template <class T>
125+
inline T THash<T>::hashValue() const {
122126
return m_hash;
123127
}
124128

include/cppcore/Container/THashMap.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ namespace cppcore {
4444
template <class T, class U, class TAlloc = TDefaultAllocator<T>>
4545
class THashMap {
4646
public:
47+
using Hash = THash<T>;
48+
4749
/// @brief The initial hash size.
4850
static constexpr size_t InitSize = 1024;
4951
/// @brief Marker for unset node keys.
@@ -176,7 +178,7 @@ inline void THashMap<T, U, TAlloc>::clear() {
176178

177179
template <class T, class U, class TAlloc>
178180
inline void THashMap<T, U, TAlloc>::insert(const T &key, const U &value) {
179-
const unsigned int hash = Hash::toHash(key, (unsigned int)m_buffersize);
181+
const T hash = Hash::toHash(key, static_cast<T>(m_buffersize));
180182
if (nullptr == m_buffer[hash]) {
181183
Node *node = new Node;
182184
node->m_key = key;
@@ -190,7 +192,7 @@ inline void THashMap<T, U, TAlloc>::insert(const T &key, const U &value) {
190192

191193
template <class T, class U, class TAlloc>
192194
inline bool THashMap<T, U, TAlloc>::remove(const T &key) {
193-
const unsigned int hash = Hash::toHash(key, (unsigned int)m_buffersize);
195+
const T hash = Hash::toHash(key, (unsigned int)m_buffersize);
194196
if (nullptr == m_buffer[hash]) {
195197
return false;
196198
}
@@ -220,7 +222,7 @@ inline bool THashMap<T, U, TAlloc>::hasKey(const T &key) const {
220222
if (0 == m_buffersize) {
221223
return false;
222224
}
223-
const unsigned int hash(Hash::toHash(key, (unsigned int)m_buffersize));
225+
const T hash = THash<T>::toHash(key, (unsigned int)m_buffersize);
224226
const Node *node = m_buffer[hash];
225227
if (nullptr == node) {
226228
return false;
@@ -245,7 +247,7 @@ inline bool THashMap<T, U, TAlloc>::hasKey(const T &key) const {
245247

246248
template <class T, class U, class TAlloc>
247249
inline bool THashMap<T, U, TAlloc>::getValue(const T &key, U &value) const {
248-
const size_t pos = Hash::toHash(key, (unsigned int)m_buffersize);
250+
const size_t pos = Hash::toHash(key, (unsigned int) m_buffersize);
249251
if (m_buffer[pos]->m_key == key) {
250252
value = m_buffer[pos]->m_value;
251253
return true;

test/common/HashTest.cpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,67 +29,70 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2929
using namespace cppcore;
3030

3131
class HashTest : public testing::Test {
32+
public:
33+
using UiHash = THash<unsigned int>;
34+
3235
protected:
3336
// empty
3437
};
3538

3639
TEST_F( HashTest, CreateTest ) {
37-
Hash myHash1;
40+
UiHash myHash1;
3841
EXPECT_EQ( myHash1.hashValue(), 0U );
3942

40-
Hash myHash2( 10U );
43+
UiHash myHash2(10U);
4144
EXPECT_EQ( myHash2.hashValue(), 10U );
4245

43-
Hash myHash3( "test", 7 );
46+
UiHash myHash3("test", 7);
4447
EXPECT_NE( myHash3.hashValue(), 0U );
4548
}
4649

4750
TEST_F( HashTest, MakeStringHashTest ) {
4851
static const unsigned int Base = 7;
49-
Hash myHash_empty;
52+
UiHash myHash_empty;
5053
EXPECT_EQ( myHash_empty.hashValue(), 0U );
5154

5255
std::string value;
5356
value = ( "huhu1" );
54-
const unsigned int hash1 = Hash::toHash( value.c_str(), Base );
57+
const unsigned int hash1 = UiHash::toHash(value.c_str(), Base);
5558
EXPECT_NE( hash1, 0U );
5659
EXPECT_LE( hash1, Base );
5760

5861
value = ( "huhu2" );
59-
const unsigned int hash2 = Hash::toHash( value.c_str(), Base );
62+
const unsigned int hash2 = UiHash::toHash(value.c_str(), Base);
6063
EXPECT_NE( hash2, 0U );
6164
EXPECT_LE( hash2, Base );
6265

6366
value = ( "huhu3" );
64-
const unsigned int hash3 = Hash::toHash( value.c_str(), Base );
67+
const unsigned int hash3 = UiHash::toHash(value.c_str(), Base);
6568
EXPECT_NE( hash3, 0U );
6669
EXPECT_LE( hash3, Base );
6770

68-
Hash myHash_inited( value.c_str(), Base );
71+
UiHash myHash_inited(value.c_str(), Base);
6972
EXPECT_EQ( myHash_inited.hashValue(), hash3 );
7073
}
7174

7275
TEST_F( HashTest, MakeUIntHashTest ) {
7376
static const unsigned int Base = 7U;
74-
Hash myHash_empty;
77+
UiHash myHash_empty;
7578
EXPECT_EQ( myHash_empty.hashValue(), 0U );
7679

7780
unsigned int value = 17U;
78-
const unsigned int hash1 = Hash::toHash( value, Base );
81+
const unsigned int hash1 = UiHash::toHash(value, Base);
7982

8083
EXPECT_NE( hash1, 0U );
8184
EXPECT_LE( hash1, Base );
8285

8386
value = 27U;
84-
const unsigned int hash2 = Hash::toHash( value, Base );
87+
const unsigned int hash2 = UiHash::toHash(value, Base);
8588
EXPECT_NE( hash2, 0U );
8689
EXPECT_LE( hash2, Base );
8790

8891
value = 37U;
89-
const unsigned int hash3 = Hash::toHash( value, Base );
92+
const unsigned int hash3 = UiHash::toHash(value, Base);
9093
EXPECT_NE( hash3, 0U );
9194
EXPECT_LE( hash3, Base );
9295

93-
Hash myHash_inited( value, Base );
96+
UiHash myHash_inited(value, Base);
9497
EXPECT_EQ( myHash_inited.hashValue(), hash3 );
9598
}

0 commit comments

Comments
 (0)