HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_ArrayStringSet.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_ArrayStringSet.h
7  *
8  * COMMENTS: An array-based hash set for UT_StringHolder.
9  */
10 
11 #pragma once
12 
13 #ifndef __UT_ArrayStringSet_h__
14 #define __UT_ArrayStringSet_h__
15 
16 #include "UT_ArraySet.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  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  inline std::pair<iterator_type, iterator_type> \
35  name(const UT_StringRef &key) qualifier \
36  { \
37  return Parent::name(UTmakeUnsafeRef(key)); \
38  }
39 
40 /// UT_ArrayStringSet is a simple specialization of a UT::ArraySet that has
41 /// UT_StringHolder as its key type which allows C strings to be used.
42 /// If you know that a string will not be destroyed during the set's
43 /// lifetime, UTmakeUnsafeRef can be used to insert a shallow reference.
44 /// NOTE: This class *used to* not allow empty string as a key,
45 /// but now it should work.
46 class UT_ArrayStringSet : public UT::ArraySet<UT_StringHolder>
47 {
48 public:
50 
51  typedef Parent::const_iterator const_iterator;
54 
55  // Inherit parent class constructors
56  using Parent::Parent;
57 
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 
68  UT_STRINGREF_WRAPPER_RANGE(const_iterator, equal_range, const)
70  UT_STRINGREF_WRAPPER(iterator, find, )
71  UT_STRINGREF_WRAPPER(const_iterator, find, const)
72 
73  bool contains(const UT_StringRef &ref) const
74  { return count(ref) > 0; }
75 
76  template <typename S>
77  bool containsAll(const S &src) const
78  {
79  return std::all_of(std::begin(src), std::end(src),
80  [this](const auto v) { return contains(v); });
81  }
82 
83  /// Set-wise boolean operations.
85  { for (const_iterator it = src.begin(); it != src.end(); ++it)
86  insert(*it);
87  return *this; }
90  for (const_iterator it = src.begin(); it != src.end(); ++it)
91  if (contains(*it))
92  result.insert(*it);
93  *this = std::move(result);
94  return *this; }
96  { for (const_iterator it = src.begin(); it != src.end(); ++it)
97  erase(*it);
98  return *this; }
99 
100  using Parent::insert;
101  std::pair<iterator, bool> insert(const UT_StringRef &key)
102  {
103  auto&& iter = Parent::find(UTmakeUnsafeRef(key));
104  if (iter == end())
105  return insert(UT_StringHolder(key));
106  return std::make_pair(iter, false);
107  }
108  std::pair<iterator, bool> insert(UT_StringRef &&key)
109  {
110  auto&& iter = Parent::find(UTmakeUnsafeRef(key));
111  if (iter == end())
112  return insert(UT_StringHolder(std::move(key)));
113  return std::make_pair(iter, false);
114  }
115  std::pair<iterator, bool> insert(const char *key)
116  {
117  return insert(UT_StringRef(key));
118  }
119 };
120 
121 namespace UT {
122 template<>
124  : public DefaultClearer<typename UT_ArrayStringSet::Parent>
125 {};
126 }
127 
128 #undef UT_STRINGREF_WRAPPER
129 #undef UT_STRINGREF_WRAPPER_RANGE
130 
131 #endif
GLint first
Definition: glcorearb.h:405
std::pair< iterator, bool > insert(UT_StringRef &&key)
std::pair< iterator, bool > insert(const value_type &value)
Definition: UT_ArraySet.h:972
#define UT_STRINGREF_WRAPPER_RANGE(iterator_type, name, qualifier)
const GLdouble * v
Definition: glcorearb.h:837
iterator end()
Returns a non-const end iterator for the set.
Definition: UT_ArraySet.h:676
**But if you need a result
Definition: thread.h:613
#define UT_STRINGREF_WRAPPER(return_type, name, qualifier)
bool contains(const UT_StringRef &ref) const
std::pair< iterator, bool > insert(const char *key)
iterator erase(iterator iter)
Definition: UT_ArraySet.h:1049
GLint ref
Definition: glcorearb.h:124
Parent::size_type size_type
GLuint GLuint end
Definition: glcorearb.h:475
SYS_FORCE_INLINE const UT_StringHolder & UTmakeUnsafeRef(const UT_StringRef &ref)
Convert a UT_StringRef into a UT_StringHolder that is a shallow reference.
iterator begin()
Returns a non-const iterator for the beginning of the set.
Definition: UT_ArraySet.h:661
iterator find(const UT_StringHolder &key)
Definition: UT_ArraySet.h:697
__hostdev__ uint64_t last(uint32_t i) const
Definition: NanoVDB.h:5976
bool containsAll(const S &src) const
std::pair< iterator, bool > insert(const UT_StringRef &key)
size_type count(const UT_StringHolder &key) const
Definition: UT_ArraySet.h:757
Parent::const_iterator const_iterator
UT_ArrayStringSet & operator|=(const UT_ArrayStringSet &src)
Set-wise boolean operations.
UT_ArrayStringSet & operator-=(const UT_ArrayStringSet &src)
std::pair< const_iterator, const_iterator > equal_range(const UT_StringHolder &key) const
Definition: UT_ArraySet.h:834
UT::ArraySet< UT_StringHolder > Parent
iterator_t< false > iterator
Iterator type for iterating over non-constant elements.
Definition: UT_ArraySet.h:644
Parent::iterator iterator
UT_ArrayStringSet & operator&=(const UT_ArrayStringSet &src)
GLint GLsizei count
Definition: glcorearb.h:405
iterator erase(iterator pos)
GLenum src
Definition: glcorearb.h:1793
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:558