-
Notifications
You must be signed in to change notification settings - Fork 6
/
indexkey.h
executable file
·78 lines (65 loc) · 2.11 KB
/
indexkey.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#ifndef _INDEX_KEY_H
#define _INDEX_KEY_H
#include <cstring>
#include <string>
template <std::size_t keySize>
class GenericKey {
public:
char data[keySize];
public:
inline void setFromString(std::string key) {
memset(data, 0, keySize);
if(key.size() >= keySize) {
memcpy(data, key.c_str(), keySize - 1);
data[keySize - 1] = '\0';
} else {
strcpy(data, key.c_str());
}
return;
}
// Constructor - Fills it with 0x00
// This is for the skiplist to initialize an empty node
GenericKey(int) { memset(data, 0x00, keySize); }
GenericKey() { memset(data, 0x00, keySize); }
// Copy constructor
GenericKey(const GenericKey &other) { memcpy(data, other.data, keySize); }
inline GenericKey &operator=(const GenericKey &other) {
memcpy(data, other.data, keySize);
return *this;
}
inline bool operator<(const GenericKey<keySize> &other) { return strcmp(data, other.data) < 0; }
inline bool operator>(const GenericKey<keySize> &other) { return strcmp(data, other.data) > 0; }
inline bool operator==(const GenericKey<keySize> &other) { return strcmp(data, other.data) == 0; }
// Derived operators
inline bool operator!=(const GenericKey<keySize> &other) { return !(*this == other); }
inline bool operator<=(const GenericKey<keySize> &other) { return !(*this > other); }
inline bool operator>=(const GenericKey<keySize> &other) { return !(*this < other); }
};
template <std::size_t keySize>
class GenericComparator {
public:
GenericComparator() {}
inline bool operator()(const GenericKey<keySize> &lhs, const GenericKey<keySize> &rhs) const {
int diff = strcmp(lhs.data, rhs.data);
return diff < 0;
}
};
template <std::size_t keySize>
class GenericEqualityChecker {
public:
GenericEqualityChecker() {}
inline bool operator()(const GenericKey<keySize> &lhs, const GenericKey<keySize> &rhs) const {
int diff = strcmp(lhs.data, rhs.data);
return diff == 0;
}
};
template <std::size_t keySize>
class GenericHasher {
public:
GenericHasher() {}
inline size_t operator()(const GenericKey<keySize> &lhs) const {
(void)lhs;
return 0UL;
}
};
#endif