adamdebreceni commented on code in PR #1340:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1340#discussion_r907497990
##########
libminifi/include/utils/Id.h:
##########
@@ -132,3 +134,26 @@ class NonRepeatingStringGenerator {
};
} // namespace org::apache::nifi::minifi::utils
+
+namespace std {
+template<>
+struct hash<org::apache::nifi::minifi::utils::Identifier> {
+ size_t operator()(const org::apache::nifi::minifi::utils::Identifier& id)
const noexcept {
+ constexpr int slices =
sizeof(org::apache::nifi::minifi::utils::Identifier) / sizeof(size_t);
+ const auto combine = [](size_t& seed, size_t new_hash) {
+ // from the boost hash_combine docs
+ seed ^= new_hash + 0x9e3779b9 + (seed << 6) + (seed >> 2);
+ };
+ const auto get_slice = [](const
org::apache::nifi::minifi::utils::Identifier& id, size_t idx) -> size_t {
+ size_t result{};
+ memcpy(&result, reinterpret_cast<const unsigned char*>(&id.data_) + idx
* sizeof(size_t), sizeof(size_t));
+ return result;
+ };
+ size_t hash = get_slice(id, 0);
+ for (size_t i = 1; i < slices; ++i) {
+ combine(hash, get_slice(id, i));
+ }
+ return hash;
Review Comment:
we could use `gsl::span` here, it would also verify convertibility
```suggestion
for (size_t slice : gsl::span(id.data_).as_span<const size_t>()) {
combine(hash, slice);
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]