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  SYS_FORCE_INLINE ITEM_T get(const UT_StringRef &key, const ITEM_T &defval) const
77  {
78  return Parent::get(UTmakeUnsafeRef(key), defval);
79  }
80 
81  using Parent::insert;
82  std::pair<iterator, bool> insert(const UT_StringRef &key, const ITEM_T &val)
83  {
84  auto&& iter = find(key);
85  if (iter == Parent::end())
86  return insert(UT_StringHolder(key), val);
87  return std::make_pair(iter, false);
88  }
89  std::pair<iterator, bool> insert(const UT_StringRef &key, ITEM_T &&val)
90  {
91  auto&& iter = find(key);
92  if (iter == Parent::end())
93  return insert(UT_StringHolder(key), std::move(val));
94  return std::make_pair(iter, false);
95  }
96  std::pair<iterator, bool> insert(UT_StringRef &&key, const ITEM_T &val)
97  {
98  auto&& iter = find(key);
99  if (iter == Parent::end())
100  return insert(UT_StringHolder(std::move(key)), val);
101  return std::make_pair(iter, false);
102  }
103  std::pair<iterator, bool> insert(UT_StringRef &&key, ITEM_T &&val)
104  {
105  auto&& iter = find(key);
106  if (iter == Parent::end())
107  return insert(UT_StringHolder(std::move(key)), std::move(val));
108  return std::make_pair(iter, false);
109  }
110  std::pair<iterator, bool> insert(const char *key, const ITEM_T &val)
111  {
112  return insert(UT_StringRef(key), val);
113  }
114  std::pair<iterator, bool> insert(const char *key, ITEM_T &&val)
115  {
116  return insert(UT_StringRef(key), std::move(val));
117  }
118 
119 };
120 
121 namespace UT {
122 template<typename ITEM_T>
124  : public DefaultClearer<typename UT_ArrayStringMap<ITEM_T>::Parent>
125 {};
126 }
127 
128 #undef UT_STRINGREF_WRAPPER
129 #undef UT_STRINGREF_WRAPPER_RANGE
130 
131 #endif
GLint first
Definition: glcorearb.h:405
ITEM_T & at(const UT_StringHolder &key)
Definition: UT_ArrayMap.h:232
bool contains(const UT_StringHolder &key) const
Definition: UT_ArrayMap.h:347
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:426
iterator end()
Returns a non-const end iterator for the set.
Definition: UT_ArraySet.h:736
iterator find(const UT_StringHolder &key)
Definition: UT_ArrayMap.h:158
Parent::const_iterator const_iterator
typename set_type::size_type size_type
Definition: UT_ArrayMap.h:100
std::pair< iterator, bool > insert(const UT_StringHolder &key, const ITEM_T &val)
Definition: UT_ArrayMap.h:117
SYS_FORCE_INLINE const UT_StringHolder & UTmakeUnsafeRef(const UT_StringRef &ref)
Convert a UT_StringRef into a UT_StringHolder that is a shallow reference.
#define SYS_FORCE_INLINE
Definition: SYS_Inline.h:45
Parent::iterator iterator
size_type erase(const UT_StringHolder &key)
Definition: UT_ArrayMap.h:563
mapped_type get(const key_type &key, const mapped_type &defval) const
Definition: UT_ArrayMap.h:221
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
SYS_FORCE_INLINE ITEM_T get(const UT_StringRef &key, const ITEM_T &defval) const
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)