Skip to content

Commit f6a8373

Browse files
committed
feat: enhanced performance
1 parent d82c3e7 commit f6a8373

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

src/alfred/data_structure/hashmap.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <utility>
66
#include <vector>
77

8+
std::vector<bool> s;
9+
810
template <class K, class V>
911
struct HashMap { // HashMap for integer keys.
1012
using u32 = unsigned int;
@@ -23,7 +25,7 @@ struct HashMap { // HashMap for integer keys.
2325
fi.assign(lim, 0), tot = 0;
2426
mask = lim - 1, e.resize(sz);
2527
}
26-
inline void inc(K x, V delta) {
28+
inline void inc(K x, const V &delta) {
2729
u32 u = hash(x) & mask;
2830
for (u32 i = fi[u]; i; i = e[i].ne) {
2931
if (e[i].key == x) {

src/alfred/data_structure/splitmix.hpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
template <class T>
55
struct Splitmix {
66
inline T operator()(T x) {
7-
if constexpr (sizeof(T) == 8) {
7+
if (sizeof(T) == 8) {
88
T z = (x += 0x9e3779b97f4a7c15);
99
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
1010
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
@@ -18,4 +18,29 @@ struct Splitmix {
1818
}
1919
};
2020

21+
// use specialized version for 64bit and 32bit for older c++ standards
22+
// as if constexpr is not supported
23+
24+
template <>
25+
struct Splitmix<unsigned long long> {
26+
unsigned long long z;
27+
inline unsigned long long operator()(unsigned long long x) {
28+
z = (x += 0x9e3779b97f4a7c15);
29+
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
30+
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
31+
return z ^ (z >> 31);
32+
}
33+
};
34+
35+
template <>
36+
struct Splitmix<unsigned int> {
37+
unsigned int z;
38+
inline unsigned int operator()(unsigned int x) {
39+
z = (x += 0x9e3779b9);
40+
z = (z ^ (z >> 16)) * 0x85ebca6b;
41+
z = (z ^ (z >> 13)) * 0xc2b2ae35;
42+
return z ^ (z >> 16);
43+
}
44+
};
45+
2146
#endif

0 commit comments

Comments
 (0)