HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_ArrayStringMap.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_ArrayStringMap.h
7  *
8  * COMMENTS: An array-based hash map for UT_StringHolder mapping to some type.
9  */
10 
11 #pragma once
12 
13 #ifndef __UT_ArrayStringMap_h__
14 #define __UT_ArrayStringMap_h__
15 
16 #include "UT_ArrayMap.h"
17 #include "UT_StringHolder.h"
18 
19 /// We want methods like find() to take a const UT_StringRef& instead of a
20 /// const UT_StringHolder& for the following reasons:
21 /// - This allows a const char* to be passed in without forcing a copy of the string.
22 /// - A UT_StringRef can be used without converting it to a UT_StringHolder
23 /// (which hardens the string if necessary).
24 /// - A UT_StringHolder can still be directly passed in.
25 #define UT_STRINGREF_WRAPPER(return_type, name, qualifier) \
26  SYS_FORCE_INLINE return_type name(const UT_StringRef &key) qualifier \
27  { \
28  return Parent::name(UTmakeUnsafeRef(key)); \
29  }
30 /// Specialization of the above macro for methods that return an iterator
31 /// range, since something like std::pair<iterator, iterator> is interpreted as
32 /// two arguments when being passed to a macro (due to the comma).
33 #define UT_STRINGREF_WRAPPER_RANGE(iterator_type, name, qualifier) \
34  SYS_FORCE_INLINE std::pair<iterator_type, iterator_type> \
35  name(const UT_StringRef &key) qualifier \
36  { \
37  return Parent::name(UTmakeUnsafeRef(key)); \
38  }
39 
40 /// NOTE: This class *used to* not allow empty string as a key,
41 /// but now it should work.
42 template <typename ITEM_T>
43 class UT_ArrayStringMap : public UT::ArrayMap<UT_StringHolder, ITEM_T>
44 {
45 public:
47 
49  typedef typename Parent::iterator iterator;
50  typedef typename Parent::size_type size_type;
51 
52  // Inherit parent class constructors
53  using Parent::Parent;
54 
55  // Expose the erase() overloads that take iterators.
56  // The erase(const UT_StringHolder&) overload is hidden in favour of
57  // erase(const UT_StringRef &) from UT_STRINGREF_WRAPPER.
58  iterator erase(iterator pos) { return Parent::erase(pos); }
59 #if 0 // The parent function isn't fully implemented yet.
61  {
62  return Parent::erase(first, last);
63  }
64 #endif
65 
66  UT_STRINGREF_WRAPPER(ITEM_T &, at, )
67  UT_STRINGREF_WRAPPER(const ITEM_T &, at, const)
68  UT_STRINGREF_WRAPPER(bool, contains, const)
71  UT_STRINGREF_WRAPPER_RANGE(const_iterator, equal_range, const)
72  UT_STRINGREF_WRAPPER(size_type, erase, )
73  UT_STRINGREF_WRAPPER(iterator, find, )
74  UT_STRINGREF_WRAPPER(const_iterator, find, const)
75 
76  using Parent::insert;
77  std::pair<iterator, bool> insert(const UT_StringRef &key, const ITEM_T &val)
78  {
79  auto&& iter = find(key);
80  if (iter == Parent::end())
81  return insert(UT_StringHolder(key), val);
82  return std::make_pair(iter, false);
83  }
84  std::pair<iterator, bool> insert(const UT_StringRef &key, ITEM_T &&val)
85  {
86  auto&& iter = find(key);
87  if (iter == Parent::end())
88  return insert(UT_StringHolder(key), std::move(val));
89  return std::make_pair(iter, false);
90  }
91  std::pair<iterator, bool> insert(UT_StringRef &&key, const ITEM_T &val)
92  {
93  auto&& iter = find(key);
94  if (iter == Parent::end())
95  return insert(UT_StringHolder(std::move(key)), val);
96  return std::make_pair(iter, false);
97  }
98  std::pair<iterator, bool> insert(UT_StringRef &&key, ITEM_T &&val)
99  {
100  auto&& iter = find(key);
101  if (iter == Parent::end())
102  return insert(UT_StringHolder(std::move(key)), std::move(val));
103  return std::make_pair(iter, false);
104  }
105  std::pair<iterator, bool> insert(const char *key, const ITEM_T &val)
106  {
107  return insert(UT_StringRef(key), val);
108  }
109  std::pair<iterator, bool> insert(const char *key, ITEM_T &&val)
110  {
111  return insert(UT_StringRef(key), std::move(val));
112  }
113 
114 };
115 
116 namespace UT {
117 template<typename ITEM_T>
119  : public DefaultClearer<typename UT_ArrayStringMap<ITEM_T>::Parent>
120 {};
121 }
122 
123 #undef UT_STRINGREF_WRAPPER
124 #undef UT_STRINGREF_WRAPPER_RANGE
125 
126 #endif
GLint first
Definition: glcorearb.h:405
ITEM_T & at(const UT_StringHolder &key)
Definition: UT_ArrayMap.h:219
bool contains(const UT_StringHolder &key) const
Definition: UT_ArrayMap.h:334
std::pair< iterator, bool > insert(const UT_StringRef &key, const ITEM_T &val)
std::pair< iterator, bool > insert(const UT_StringRef &key, ITEM_T &&val)
std::pair< iterator, bool > insert(const char *key, ITEM_T &&val)
std::pair< const_iterator, const_iterator > equal_range(const UT_StringHolder &key) const
Definition: UT_ArrayMap.h:413
iterator end()
Returns a non-const end iterator for the set.
Definition: UT_ArraySet.h:676
iterator find(const UT_StringHolder &key)
Definition: UT_ArrayMap.h:158
Parent::const_iterator const_iterator
Set iterator class.
Definition: UT_ArraySet.h:554
Parent::iterator iterator
size_type erase(const UT_StringHolder &key)
Definition: UT_ArrayMap.h:550
UT::ArrayMap< UT_StringHolder, ITEM_T > Parent
std::pair< iterator, bool > insert(const char *key, const ITEM_T &val)
__hostdev__ uint64_t last(uint32_t i) const
Definition: NanoVDB.h:5976
#define UT_STRINGREF_WRAPPER(return_type, name, qualifier)
typename set_type::iterator iterator
Inherit iterator and const_iterator.
Definition: UT_ArrayMap.h:103
std::pair< iterator, bool > insert(UT_StringRef &&key, const ITEM_T &val)
iterator erase(iterator pos)
GLuint GLfloat * val
Definition: glcorearb.h:1608
std::pair< iterator, bool > insert(UT_StringRef &&key, ITEM_T &&val)
Parent::size_type size_type
typename set_type::const_iterator const_iterator
Definition: UT_ArrayMap.h:106
GLint GLsizei count
Definition: glcorearb.h:405
#define UT_STRINGREF_WRAPPER_RANGE(iterator_type, name, qualifier)