NVIDIA DeepStream SDK API Reference

9.2 Release
9.2/includes/opentelemetry/sdk/common/attributemap_hash.h
Go to the documentation of this file.
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #pragma once
5 
6 #include <stddef.h>
7 #include <functional>
8 #include <map>
9 #include <string>
10 #include <utility>
11 #include <vector>
12 
13 #include "opentelemetry/nostd/variant.h"
14 #include "opentelemetry/sdk/common/attribute_utils.h"
15 #include "opentelemetry/version.h"
16 
18 namespace sdk
19 {
20 namespace common
21 {
22 
23 template <class T>
24 inline void GetHash(size_t &seed, const T &arg)
25 {
26  std::hash<T> hasher;
27  // reference -
28  // https://www.boost.org/doc/libs/1_37_0/doc/html/hash/reference.html#boost.hash_combine
29  seed ^= hasher(arg) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
30 }
31 
32 template <class T>
33 inline void GetHash(size_t &seed, const std::vector<T> &arg)
34 {
35  for (auto v : arg)
36  {
37  GetHash<T>(seed, v);
38  }
39 }
40 
41 // Specialization for const char*
42 // this creates an intermediate string.
43 template <>
44 inline void GetHash<const char *>(size_t &seed, const char *const &arg)
45 {
46  std::hash<std::string> hasher;
47  seed ^= hasher(std::string(arg)) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
48 }
49 
50 struct GetHashForAttributeValueVisitor
51 {
52  GetHashForAttributeValueVisitor(size_t &seed) : seed_(seed) {}
53  template <class T>
54  void operator()(T &v)
55  {
56  GetHash(seed_, v);
57  }
58  size_t &seed_;
59 };
60 
61 // Calculate hash of keys and values of attribute map
62 inline size_t GetHashForAttributeMap(const OrderedAttributeMap &attribute_map)
63 {
64  size_t seed = 0UL;
65  for (auto &kv : attribute_map)
66  {
67  GetHash(seed, kv.first);
68  nostd::visit(GetHashForAttributeValueVisitor(seed), kv.second);
69  }
70  return seed;
71 }
72 
73 template <class T>
74 inline size_t GetHash(T value)
75 {
76  std::hash<T> hasher;
77  return hasher(value);
78 }
79 
80 } // namespace common
81 } // namespace sdk
sdk::common::GetHashForAttributeValueVisitor::seed_
size_t & seed_
Definition: includes/opentelemetry/sdk/common/attributemap_hash.h:58
sdk::common::GetHashForAttributeValueVisitor::GetHashForAttributeValueVisitor
GetHashForAttributeValueVisitor(size_t &seed)
Definition: 9.2/includes/opentelemetry/sdk/common/attributemap_hash.h:52
sdk::common::GetHashForAttributeMap
size_t GetHashForAttributeMap(const OrderedAttributeMap &attribute_map)
Definition: includes/opentelemetry/sdk/common/attributemap_hash.h:62
OPENTELEMETRY_BEGIN_NAMESPACE
#define OPENTELEMETRY_BEGIN_NAMESPACE
Definition: includes/opentelemetry/version.h:21
OPENTELEMETRY_END_NAMESPACE
#define OPENTELEMETRY_END_NAMESPACE
Definition: includes/opentelemetry/version.h:24
sdk::common::GetHashForAttributeValueVisitor::operator()
void operator()(T &v)
Definition: 9.2/includes/opentelemetry/sdk/common/attributemap_hash.h:54
nostd::visit
constexpr auto visit(_Callable &&_Obj, _Variants &&..._Args)
Definition: includes/opentelemetry/std/variant.h:196
sdk
Definition: includes/opentelemetry/sdk/common/atomic_shared_ptr.h:13
sdk::common::GetHash< const char * >
void GetHash< const char * >(size_t &seed, const char *const &arg)
Definition: includes/opentelemetry/sdk/common/attributemap_hash.h:44
common
Definition: includes/opentelemetry/common/attribute_value.h:14
sdk::common::GetHash
void GetHash(size_t &seed, const T &arg)
Definition: includes/opentelemetry/sdk/common/attributemap_hash.h:24