-
Notifications
You must be signed in to change notification settings - Fork 498
Expand file tree
/
Copy pathbuildingtypekey.cpp
More file actions
59 lines (48 loc) · 1.97 KB
/
Copy pathbuildingtypekey.cpp
File metadata and controls
59 lines (48 loc) · 1.97 KB
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
#include "buildingplan.h"
#include "buildingtypekey.h"
#include "Debug.h"
#include "MiscUtils.h"
using std::string;
using std::vector;
namespace DFHack {
DBG_EXTERN(buildingplan, control);
}
using namespace DFHack;
// building type, subtype, custom
BuildingTypeKey::BuildingTypeKey(df::building_type type, int16_t subtype, int32_t custom)
: tuple(type, subtype, custom) { }
static BuildingTypeKey deserialize(color_ostream &out, const std::string &serialized) {
vector<string> key_parts;
split_string(&key_parts, serialized, ",");
if (key_parts.size() != 3) {
WARN(control,out).print("invalid key_str: '{}'\n", serialized);
return BuildingTypeKey(df::building_type::NONE, -1, -1);
}
return BuildingTypeKey((df::building_type)string_to_int(key_parts[0]),
string_to_int(key_parts[1]), string_to_int(key_parts[2]));
}
BuildingTypeKey::BuildingTypeKey(color_ostream &out, const std::string &serialized)
:tuple(deserialize(out, serialized)) { }
string BuildingTypeKey::serialize() const {
std::ostringstream ser;
ser << std::get<0>(*this) << ",";
ser << std::get<1>(*this) << ",";
ser << std::get<2>(*this);
return ser.str();
}
// rotates a size_t value left by count bits
// assumes count is not 0 or >= size_t_bits
// replace this with std::rotl when we move to C++20
static std::size_t rotl_size_t(size_t val, uint32_t count)
{
static const int size_t_bits = CHAR_BIT * sizeof(std::size_t);
return val << count | val >> (size_t_bits - count);
}
std::size_t BuildingTypeKeyHash::operator() (const BuildingTypeKey & key) const {
// cast first param to appease gcc-4.8, which is missing the enum
// specializations for std::hash
std::size_t h1 = std::hash<int32_t>()(static_cast<int32_t>(std::get<0>(key)));
std::size_t h2 = std::hash<int16_t>()(std::get<1>(key));
std::size_t h3 = std::hash<int32_t>()(std::get<2>(key));
return h1 ^ rotl_size_t(h2, 8) ^ rotl_size_t(h3, 16);
}