Skip to content

Commit 7c08340

Browse files
committed
constexpr + noexcept in Hash
1 parent f82a6d8 commit 7c08340

File tree

3 files changed

+26
-39
lines changed

3 files changed

+26
-39
lines changed

src/common/CRC32C.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
#include <nmmintrin.h>
3535

36-
unsigned int CRC32C(unsigned int length, const unsigned char* value)
36+
unsigned int CRC32C(unsigned int length, const unsigned char* value) noexcept
3737
{
3838
unsigned int hash_value = 0;
3939

src/common/classes/Hash.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@
3434

3535
using namespace Firebird;
3636

37-
unsigned int CRC32C(unsigned int length, const unsigned char* value);
37+
unsigned int CRC32C(unsigned int length, const unsigned char* value) noexcept;
3838

3939
namespace
4040
{
4141
typedef unsigned int (*hash_func_t)(unsigned int length, const UCHAR* value);
4242

43-
unsigned int basicHash(unsigned int length, const UCHAR* value)
43+
unsigned int basicHash(unsigned int length, const UCHAR* value) noexcept
4444
{
4545
unsigned int hash_value = 0;
4646

@@ -75,17 +75,17 @@ namespace
7575

7676
#if defined(_M_IX86) || defined(_M_X64) || defined(__x86_64__) || defined(__i386__)
7777

78-
bool SSE4_2Supported()
78+
bool SSE4_2Supported() noexcept
7979
{
8080
#ifdef _MSC_VER
81-
const int bit_SSE4_2 = 1 << 20;
81+
constexpr int bit_SSE4_2 = 1 << 20;
8282
// MS VC has its own definition of __cpuid
8383
int flags[4];
8484
__cpuid(flags, 1);
8585
return (flags[2] & bit_SSE4_2) != 0;
8686
#else
8787
#if defined(__clang__) && !defined(bit_SSE4_2)
88-
const int bit_SSE4_2 = bit_SSE42;
88+
constexpr int bit_SSE4_2 = bit_SSE42;
8989
#endif
9090

9191
// GCC - its own
@@ -105,7 +105,7 @@ namespace
105105

106106
}
107107

108-
unsigned int InternalHash::hash(unsigned int length, const UCHAR* value)
108+
unsigned int InternalHash::hash(unsigned int length, const UCHAR* value) noexcept
109109
{
110110
return internalHash(length, value);
111111
}

src/common/classes/Hash.h

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ namespace Firebird
7676
}
7777
};
7878

79-
const FB_SIZE_T DEFAULT_HASH_SIZE = 97; // largest prime number < 100
79+
inline constexpr FB_SIZE_T DEFAULT_HASH_SIZE = 97; // largest prime number < 100
8080

8181
template <typename C,
8282
FB_SIZE_T HASHSIZE = DEFAULT_HASH_SIZE,
@@ -94,14 +94,14 @@ namespace Firebird
9494
Entry* nextElement;
9595

9696
public:
97-
Entry() : previousElement(NULL) { }
97+
Entry() noexcept : previousElement(NULL), nextElement(NULL) { }
9898

9999
virtual ~Entry()
100100
{
101101
unLink();
102102
}
103103

104-
void link(Entry** where)
104+
void link(Entry** where) noexcept
105105
{
106106
unLink();
107107

@@ -119,7 +119,7 @@ namespace Firebird
119119
*previousElement = this;
120120
}
121121

122-
void unLink()
122+
void unLink() noexcept
123123
{
124124
// if we are linked
125125
if (previousElement)
@@ -136,12 +136,12 @@ namespace Firebird
136136
}
137137
}
138138

139-
Entry** nextPtr()
139+
Entry** nextPtr() noexcept
140140
{
141141
return &nextElement;
142142
}
143143

144-
Entry* next() const
144+
Entry* next() const noexcept
145145
{
146146
return nextElement;
147147
}
@@ -156,20 +156,13 @@ namespace Firebird
156156
virtual C* get() = 0;
157157
}; // class Entry
158158

159-
private:
160-
HashTable(const HashTable&); // not implemented
161-
162159
public:
163-
explicit HashTable(MemoryPool&)
164-
: duplicates(false)
160+
explicit HashTable(MemoryPool&) noexcept
165161
{
166-
clean();
167162
}
168163

169-
HashTable()
170-
: duplicates(false)
164+
HashTable() noexcept
171165
{
172-
clean();
173166
}
174167

175168
~HashTable()
@@ -178,6 +171,9 @@ namespace Firebird
178171
cleanup(NULL);
179172
}
180173

174+
HashTable(const HashTable&) = delete;
175+
HashTable& operator= (const HashTable&) = delete;
176+
181177
typedef void CleanupRoutine(C* toClean);
182178
void cleanup(CleanupRoutine* cleanupRoutine)
183179
{
@@ -193,14 +189,14 @@ namespace Firebird
193189
}
194190
}
195191

196-
void enableDuplicates()
192+
void enableDuplicates() noexcept
197193
{
198194
duplicates = true;
199195
}
200196

201197
private:
202-
Entry* data[HASHSIZE];
203-
bool duplicates;
198+
Entry* data[HASHSIZE]{};
199+
bool duplicates = false;
204200

205201
Entry** locate(const K& key, FB_SIZE_T h)
206202
{
@@ -219,7 +215,7 @@ namespace Firebird
219215

220216
Entry** locate(const K& key)
221217
{
222-
FB_SIZE_T hashValue = F::hash(key, HASHSIZE);
218+
const FB_SIZE_T hashValue = F::hash(key, HASHSIZE);
223219
fb_assert(hashValue < HASHSIZE);
224220
return locate(key, hashValue % HASHSIZE);
225221
}
@@ -254,15 +250,6 @@ namespace Firebird
254250
return NULL;
255251
}
256252

257-
private:
258-
// disable use of default operator=
259-
HashTable& operator= (const HashTable&);
260-
261-
void clean()
262-
{
263-
memset(data, 0, sizeof data);
264-
}
265-
266253
public:
267254
class iterator
268255
{
@@ -335,9 +322,9 @@ namespace Firebird
335322
class InternalHash
336323
{
337324
public:
338-
static unsigned int hash(unsigned int length, const UCHAR* value);
325+
static unsigned int hash(unsigned int length, const UCHAR* value) noexcept;
339326

340-
static unsigned int hash(unsigned int length, const UCHAR* value, unsigned int hashSize)
327+
static unsigned int hash(unsigned int length, const UCHAR* value, unsigned int hashSize) noexcept
341328
{
342329
return hash(length, value) % hashSize;
343330
}
@@ -358,8 +345,8 @@ namespace Firebird
358345
class WeakHashContext final : public HashContext
359346
{
360347
public:
361-
virtual void update(const void* data, FB_SIZE_T length);
362-
virtual void finish(dsc& result);
348+
void update(const void* data, FB_SIZE_T length) override;
349+
void finish(dsc& result) override;
363350

364351
private:
365352
SINT64 hashNumber = 0;

0 commit comments

Comments
 (0)