HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
inlined_containers.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License.
3 
4 #pragma once
5 
6 #include <cmath>
7 
9 
10 #ifndef DISABLE_ABSEIL
11 
12 #ifdef _MSC_VER
13 #pragma warning(push)
14 // C4127: conditional expression is constant
15 #pragma warning(disable : 4127)
16 // C4324: structure was padded due to alignment specifier
17 // Usage of alignas causes some internal padding in places.
18 #pragma warning(disable : 4324)
19 #endif // _MSC_VER
20 
21 #include <absl/container/flat_hash_set.h>
22 #include <absl/container/flat_hash_map.h>
23 
24 #include <absl/container/node_hash_set.h>
25 #include <absl/container/node_hash_map.h>
26 
27 #ifdef _MSC_VER
28 #pragma warning(pop)
29 #endif // _MSC_VER
30 
31 #else // DISABLE_ABSEIL
32 
33 #include <unordered_set>
34 #include <unordered_map>
35 #include <set>
36 #include <map>
37 
38 #endif // DISABLE_ABSEIL
39 
40 namespace onnxruntime {
41 
42 #ifndef DISABLE_ABSEIL
43 // InlinedHashSet and InlinedHashMap are preferred
44 // hash based containers. They store their values in the
45 // buckets array that is allocated in one shot. It eliminates
46 // per-node new/delete calls. Always call reserve() on any hash set/map
47 // when the number of items is known in advance.
48 // This does not allocate a dummy 'end' node on default construction.
49 template <typename T, typename Allocator>
50 class InlinedHashSet : public absl::flat_hash_set<T,
51  absl::container_internal::hash_default_hash<T>,
52  absl::container_internal::hash_default_eq<T>,
53  Allocator> {
54  using Base = absl::flat_hash_set<T,
55  absl::container_internal::hash_default_hash<T>,
56  absl::container_internal::hash_default_eq<T>,
57  Allocator>;
58 
59  public:
60  using Base::Base;
61 };
62 
63 template <typename Key, typename Value,
64  typename Allocator>
65 class InlinedHashMap : public absl::flat_hash_map<Key, Value,
66  absl::container_internal::hash_default_hash<Key>,
67  absl::container_internal::hash_default_eq<Key>,
68  Allocator> {
69  using Base = absl::flat_hash_map<Key, Value,
70  absl::container_internal::hash_default_hash<Key>,
71  absl::container_internal::hash_default_eq<Key>,
72  Allocator>;
73 
74  public:
75  using Base::Base;
76 };
77 
78 // Use this hash set/map where pointer stability is required, otherwise use
79 // InlinedHashSet and InlinedHashMap
80 // This does not allocate a dummy 'end' node on default construction.
81 // Use reserve() when the number of elements is known.
82 template <typename T, typename Allocator>
83 class NodeHashSet : public absl::node_hash_set<T,
84  absl::container_internal::hash_default_hash<T>,
85  absl::container_internal::hash_default_eq<T>,
86  Allocator> {
87  using Base = absl::node_hash_set<T,
88  absl::container_internal::hash_default_hash<T>,
89  absl::container_internal::hash_default_eq<T>,
90  Allocator>;
91 
92  public:
93  using Base::Base;
94 };
95 
96 template <typename Key, typename Value, typename Allocator>
97 class NodeHashMap : public absl::node_hash_map<Key, Value,
98  absl::container_internal::hash_default_hash<Key>,
99  absl::container_internal::hash_default_eq<Key>,
100  Allocator> {
101  using Base = absl::node_hash_map<Key, Value,
102  absl::container_internal::hash_default_hash<Key>,
103  absl::container_internal::hash_default_eq<Key>,
104  Allocator>;
105 
106  public:
107  using Base::Base;
108 };
109 
110 #else // DISABLE_ABSEIL
111 
112 template <typename T, typename Allocator>
113 class InlinedHashSet : public std::unordered_set<T,
114  std::hash<T>,
115  std::equal_to<T>,
116  Allocator> {
117  using Base = std::unordered_set<T,
118  std::hash<T>,
119  std::equal_to<T>,
120  Allocator>;
121 
122  public:
123  using Base::Base;
124 };
125 
126 template <typename Key, typename Value,
127  typename Allocator>
128 class InlinedHashMap : public std::unordered_map<Key, Value,
129  std::hash<Key>,
130  std::equal_to<Key>,
131  Allocator> {
132  using Base = std::unordered_map<Key, Value,
133  std::hash<Key>,
134  std::equal_to<Key>,
135  Allocator>;
136 
137  public:
138  using Base::Base;
139 };
140 
141 // Use this hash set/map where pointer stability is required, otherwise use
142 // InlinedHashSet and InlinedHashMap
143 // This does not allocate a dummy 'end' node on default construction.
144 // Use reserve() when the number of elements is known.
145 template <typename T, typename Allocator>
146 class NodeHashSet : public std::unordered_set<T,
147  std::hash<T>,
148  std::equal_to<T>,
149  Allocator> {
150  using Base = std::unordered_set<T,
151  std::hash<T>,
152  std::equal_to<T>,
153  Allocator>;
154 
155  public:
156  using Base::Base;
157 };
158 
159 template <typename Key, typename Value, typename Allocator>
160 class NodeHashMap : public std::unordered_map<Key, Value,
161  std::hash<Key>,
162  std::equal_to<Key>,
163  Allocator> {
164  using Base = std::unordered_map<Key, Value,
165  std::hash<Key>,
166  std::equal_to<Key>,
167  Allocator>;
168 
169  public:
170  using Base::Base;
171 };
172 
173 #endif // DISABLE_ABSEIL
174 
175 } // namespace onnxruntime
A generic, discriminated value, whose type may be queried dynamically.
Definition: Value.h:44