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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
|
/*
* Author: Sven Gothel <sgothel@jausoft.com>
* Copyright (c) 2020 Gothel Software e.K.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef TEST_DATATYPE01_HPP_
#define TEST_DATATYPE01_HPP_
#include <cassert>
#include <cstdint>
#include <cstring>
#include <random>
#include <jau/cpp_lang_util.hpp>
#include <jau/packed_attribute.hpp>
#include <jau/ordered_atomic.hpp>
#include <jau/basic_types.hpp>
using namespace jau;
__pack ( struct Addr48Bit {
uint8_t b[6]; // == sizeof(Addr48Bit)
constexpr Addr48Bit() noexcept : b{0} { }
constexpr Addr48Bit(const uint64_t encoded) noexcept
: b{static_cast<uint8_t>(encoded & 0xff),
static_cast<uint8_t>( ( encoded >> 8 ) & 0xff),
static_cast<uint8_t>( ( encoded >> 16 ) & 0xff),
static_cast<uint8_t>( ( encoded >> 24 ) & 0xff),
static_cast<uint8_t>( ( encoded >> 32 ) & 0xff),
static_cast<uint8_t>( ( encoded >> 40 ) & 0xff)} { }
Addr48Bit(const uint8_t * b_) noexcept {
memcpy(b, b_, sizeof(b));
}
// Trivially-copyable
constexpr Addr48Bit(const Addr48Bit &o) noexcept = default;
Addr48Bit(Addr48Bit &&o) noexcept = default;
constexpr Addr48Bit& operator=(const Addr48Bit &o) noexcept = default;
Addr48Bit& operator=(Addr48Bit &&o) noexcept = default;
bool next() noexcept {
for(int i=0; i<6; ++i) {
if(b[i] < 0xff ) {
++b[i];
for(int j=i-1; j>=0; --j) {
b[j]=0;
}
return true;
}
}
return false;
}
void random(std::default_random_engine& e) {
std::uniform_int_distribution<uint8_t> d(0, 255);
for(uint8_t & i : b) {
i = static_cast<uint8_t>( d(e) );
}
}
constexpr std::size_t hash_code() const noexcept {
// 31 * x == (x << 5) - x
std::size_t h = b[0];
h = ( ( h << 5 ) - h ) + b[1];
h = ( ( h << 5 ) - h ) + b[2];
h = ( ( h << 5 ) - h ) + b[3];
h = ( ( h << 5 ) - h ) + b[4];
h = ( ( h << 5 ) - h ) + b[5];
// printf("hash.Addr48Bit %zu\n", h);
return h;
}
std::string toString() const noexcept {
std::string str;
str.reserve(17);
for(int i=6-1; 0 <= i; --i) {
jau::byteHexString(str, b[i], false /* lowerCase */);
if( 0 < i ) {
str.push_back(':');
}
}
return str;
}
// constexpr_cxx20 operator std::string() const noexcept { return toString(); }
} );
JAU_TYPENAME_CUE_ALL(Addr48Bit)
std::ostream & operator << (std::ostream &out, const Addr48Bit &a) {
out << a.toString();
return out;
}
inline bool operator==(const Addr48Bit& lhs, const Addr48Bit& rhs) noexcept {
if( &lhs == &rhs ) {
return true;
}
//return !memcmp(&lhs, &rhs, sizeof(Addr48Bit));
const uint8_t * a = lhs.b;
const uint8_t * b = rhs.b;
return a[0] == b[0] &&
a[1] == b[1] &&
a[2] == b[2] &&
a[3] == b[3] &&
a[4] == b[4] &&
a[5] == b[5];
}
inline bool operator!=(const Addr48Bit& lhs, const Addr48Bit& rhs) noexcept
{ return !(lhs == rhs); }
class DataType01 {
public:
Addr48Bit address;
uint8_t type;
private:
jau::relaxed_atomic_size_t hash = 0; // default 0, cache
public:
DataType01(const Addr48Bit & address_, uint8_t type_)
: address(address_), type(type_) {}
DataType01(const uint64_t encoded) noexcept
: address(encoded), type(0) { }
constexpr DataType01() noexcept : address(), type{0} { }
DataType01(const DataType01 &o) noexcept : address(o.address), type(o.type) { }
DataType01(DataType01 &&o) noexcept {
address = o.address;
type = o.type;
}
constexpr DataType01& operator=(const DataType01 &o) noexcept {
address = o.address;
type = o.type;
return *this;
}
DataType01& operator=(DataType01 &&o) noexcept {
address = o.address;
type = o.type;
return *this;
}
int nop() const noexcept { return address.b[0]+1; }
std::size_t hash_code() const noexcept {
std::size_t h = hash;
if( 0 == h ) {
// 31 * x == (x << 5) - x
h = 31 + address.hash_code();
h = ((h << 5) - h) + type;
const_cast<DataType01 *>(this)->hash = h;
// printf("hash.dataSet01 new %zu\n", h);
} else {
// printf("hash.dataSet01 *cache* %zu\n", h);
}
return h;
}
void clearHash() { hash = 0; }
std::string toString() const noexcept {
std::string res;
return res.append("[").append(address.toString()).append(", ").append(std::to_string(type)).append("]");
}
#if 0
constexpr_cxx20 operator std::string() const noexcept {
return toString();
}
#endif
};
JAU_TYPENAME_CUE_ALL(DataType01)
std::ostream & operator << (std::ostream &out, const DataType01 &a) {
out << a.toString();
return out;
}
inline bool operator==(const DataType01& lhs, const DataType01& rhs) noexcept {
if( &lhs == &rhs ) {
return true;
}
return lhs.address == rhs.address &&
lhs.type == rhs.type;
}
inline bool operator!=(const DataType01& lhs, const DataType01& rhs) noexcept
{ return !(lhs == rhs); }
class DataType02_Memmove_Secmem {
public:
typedef std::true_type container_memmove_compliant;
typedef std::true_type enforce_secmem;
Addr48Bit address;
uint8_t type;
private:
jau::relaxed_atomic_size_t hash = 0; // default 0, cache
public:
DataType02_Memmove_Secmem(const Addr48Bit & address_, uint8_t type_)
: address(address_), type(type_) {}
DataType02_Memmove_Secmem(const uint64_t encoded) noexcept
: address(encoded), type(0) { }
constexpr DataType02_Memmove_Secmem() noexcept : address(), type{0} { }
DataType02_Memmove_Secmem(const DataType02_Memmove_Secmem &o) noexcept : address(o.address), type(o.type) { }
DataType02_Memmove_Secmem(DataType02_Memmove_Secmem &&o) noexcept {
address = o.address;
type = o.type;
}
constexpr DataType02_Memmove_Secmem& operator=(const DataType02_Memmove_Secmem &o) noexcept {
address = o.address;
type = o.type;
return *this;
}
DataType02_Memmove_Secmem& operator=(DataType02_Memmove_Secmem &&o) noexcept {
address = o.address;
type = o.type;
return *this;
}
int nop() const noexcept { return address.b[0]+1; }
std::size_t hash_code() const noexcept {
std::size_t h = hash;
if( 0 == h ) {
// 31 * x == (x << 5) - x
h = 31 + address.hash_code();
h = ((h << 5) - h) + type;
const_cast<DataType02_Memmove_Secmem *>(this)->hash = h;
// printf("hash.dataSet01 new %zu\n", h);
} else {
// printf("hash.dataSet01 *cache* %zu\n", h);
}
return h;
}
void clearHash() { hash = 0; }
std::string toString() const noexcept {
std::string res;
return res.append("[").append(address.toString()).append(", ").append(std::to_string(type)).append("]");
}
#if 0
constexpr_cxx20 operator std::string() const noexcept {
return toString();
}
#endif
};
JAU_TYPENAME_CUE_ALL(DataType02_Memmove_Secmem)
std::ostream & operator << (std::ostream &out, const DataType02_Memmove_Secmem &a) {
out << a.toString();
return out;
}
inline bool operator==(const DataType02_Memmove_Secmem& lhs, const DataType02_Memmove_Secmem& rhs) noexcept {
if( &lhs == &rhs ) {
return true;
}
return lhs.address == rhs.address &&
lhs.type == rhs.type;
}
inline bool operator!=(const DataType02_Memmove_Secmem& lhs, const DataType02_Memmove_Secmem& rhs) noexcept
{ return !(lhs == rhs); }
// injecting specialization of std::hash to namespace std of our types above
namespace std
{
template<> struct hash<Addr48Bit> {
std::size_t operator()(Addr48Bit const& a) const noexcept {
return a.hash_code();
}
};
template<> struct hash<DataType01> {
std::size_t operator()(DataType01 const& a) const noexcept {
return a.hash_code();
}
};
}
#endif /* TEST_DATATYPE01_HPP_ */
|