-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocking.cpp
More file actions
60 lines (51 loc) · 1.58 KB
/
Copy pathdocking.cpp
File metadata and controls
60 lines (51 loc) · 1.58 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
60
//
// Created by giuseppe on 20/12/2020.
//
#include <stdexcept>
#include <map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include "docking.h"
using namespace std::string_literals;
BitMask bitmask(std::string const& mask)
{
BitMask bm;
for (size_t exponent {0}; exponent < mask.size(); ++exponent) {
auto const index {mask.size() - 1 - exponent};
if (mask[index] == 'X') {
bm.bmask |= (1 << exponent);
}
else if (mask[index] == '1') {
bm.ones |= (1 << exponent);
}
else if (mask[index] == '0') {
bm.zeroes |= (1 << exponent);
}
else {
throw std::invalid_argument {"Bad mask: value "s + mask[index] + " at index "s + std::to_string(index)};
}
}
return bm;
}
uint64_t write(std::string const& mask,
std::map<unsigned long, uint64_t> const& data,
std::map<unsigned long /* addr */, uint64_t /* total */>& addresses)
{
if (mask.length() != 36) {
throw std::invalid_argument {"Mask must be 36-bit"};
}
if (data.empty()) {
return 0;
}
auto const bm {bitmask(mask)};
for (auto const& datum : data) {
addresses[datum.first] = 0ULL;
addresses[datum.first] |= (datum.second & bm.bmask);
addresses[datum.first] |= bm.ones;
addresses[datum.first] |= bm.zeroes;
addresses[datum.first] -= bm.zeroes;
}
return std::accumulate(addresses.cbegin(), addresses.cend(), 0UL,
[](auto init, auto const& add){ return init + add.second; });
}