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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
#ifndef SIBS_STRING_VIEW_HPP
#define SIBS_STRING_VIEW_HPP
#define XXH_FORCE_NATIVE_FORMAT
#include "../external/xxhash.h"
#include "types.hpp"
#include "env.hpp"
#include <cstring>
#include <unordered_map>
#include <map>
#include <cassert>
namespace sibs
{
class StringView
{
public:
StringView() : data(nullptr), size(0)
{
}
StringView(const StringView &other) : data(other.data), size(other.size)
{
}
StringView(const char *_data) : data(_data), size(strlen(_data))
{
}
StringView(const char *_data, usize _size) : data(_data), size(_size)
{
}
bool equals(const StringView &other) const
{
return size == other.size && memcmp(data, other.data, size) == 0;
}
size_t hash() const
{
#if defined(CISB_ENV_64BIT)
return XXH64(data, size, 0xdec05eba);
#else
return XXH32(data, size, 0xdec05eba);
#endif
}
char operator [] (size_t index) const
{
assert(index < size);
return data[index];
}
bool operator < (const StringView &other) const
{
return ((ssize)size - (ssize)other.size < 0) || strncmp(data, other.data, size) < 0;
}
bool operator > (const StringView &other) const
{
return !operator<(other);
}
size_t operator()() const
{
return hash();
}
bool operator()(const StringView &other) const
{
return equals(other);
}
bool operator == (const StringView &other) const
{
return equals(other);
}
const char *data;
usize size;
};
struct StringViewHash
{
size_t operator()(const StringView &stringView) const
{
return stringView.hash();
}
};
struct StringViewCompare
{
public:
bool operator()(const StringView &lhs, const StringView &rhs) const
{
return lhs.equals(rhs);
}
};
struct StringViewCompareSorted
{
public:
bool operator()(const StringView &lhs, const StringView &rhs) const
{
return lhs < rhs;
}
};
template <typename ValueType>
using StringViewMap = std::unordered_map<const StringView, ValueType, StringViewHash, StringViewCompare>;
// template <typename ValueType>
// using StringViewMapSorted = std::map<const StringView, ValueType, StringViewCompareSorted>;
}
#endif // SIBS_STRING_VIEW_HPP
|