szaszm commented on code in PR #1340:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1340#discussion_r907513027


##########
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:
   I don't understand what kind of convertibility you mean here. Using 
`gsl::span::as_span` is just a reinterpret_cast internally, which usually 
results in UB because of strict aliasing violation. It's fine to use it to view 
the bytes of the object representation, and a handful of other cases, but 
usually `memcpy` is the safer option.
   
   Another alternative that I didn't think of at the time was to `const auto 
chunks = std::bit_cast<std::array<size_t, slices>>(id.data_);`, and accumulate 
the resulting array. This uses `memcpy` internally as well.



-- 
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]

Reply via email to