HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_StringSet.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_StringSet.h
7  *
8  * COMMENTS: No UT_API here because there's no lib implementation!
9  */
10 
11 #pragma once
12 
13 #ifndef __UT_StringSet_h__
14 #define __UT_StringSet_h__
15 
16 #include "UT_ArrayStringSet.h" // This is only included to avoid sweeping for it right now.
17 #include "UT_Set.h"
18 #include "UT_StringHolder.h"
19 
20 /// We want methods like find() to take a const UT_StringRef& instead of a
21 /// const UT_StringHolder& for the following reasons:
22 /// - This allows a const char* to be passed in without forcing a copy of the string.
23 /// - A UT_StringRef can be used without converting it to a UT_StringHolder
24 /// (which hardens the string if necessary).
25 /// - A UT_StringHolder can still be directly passed in.
26 #define UT_STRINGREF_WRAPPER(return_type, name, qualifier) \
27  inline return_type name(const UT_StringRef &key) qualifier \
28  { \
29  return Parent::name(UTmakeUnsafeRef(key)); \
30  }
31 /// Specialization of the above macro for methods that return an iterator
32 /// range, since something like std::pair<iterator, iterator> is interpreted as
33 /// two arguments when being passed to a macro (due to the comma).
34 #define UT_STRINGREF_WRAPPER_RANGE(iterator_type, name, qualifier) \
35  inline std::pair<iterator_type, iterator_type> \
36  name(const UT_StringRef &key) qualifier \
37  { \
38  return Parent::name(UTmakeUnsafeRef(key)); \
39  }
40 
41 /// UT_StringSet is a simple specialization of a UT_Set that has
42 /// UT_StringHolder as its key type which allows C strings to be used.
43 /// If you know that a string will not be destroyed during the set's
44 /// lifetime, UTmakeUnsafeRef can be used to insert a shallow reference.
45 class UT_StringSet : public UT_Set<UT_StringHolder>
46 {
48 
49 public:
56  typedef Parent::size_type size_type;
57 
58  UT_StringSet() = default;
59 
60  /// Constructs a set from an iterator range.
61  template <typename InputIt>
62  UT_StringSet(InputIt first, InputIt last) : Parent(first, last) {}
63 
64  /// Constructs a set from an initializer list.
65  UT_StringSet(std::initializer_list<UT_StringHolder> init) : Parent(init) {}
66 
68  { return Parent::erase(pos); }
70  { return Parent::erase(first, last); }
71 
73  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
74  UT_STRINGREF_WRAPPER_RANGE(const_iterator, equal_range, const)
76  UT_STRINGREF_WRAPPER(iterator, find, )
77  UT_STRINGREF_WRAPPER(const_iterator, find, const)
78 
79  bool contains(const UT_StringRef &ref) const
80  { return count(ref) > 0; }
81 
82  template <typename S>
83  bool containsAll(const S &src) const
84  {
85  return std::all_of(std::begin(src), std::end(src),
86  [this](const auto v) { return contains(v); });
87  }
88 
89  /// Set-wise boolean operations.
91  { for (const_iterator it = src.begin(); it != src.end(); ++it)
92  insert(*it);
93  return *this; }
96  for (const_iterator it = src.begin(); it != src.end(); ++it)
97  if (contains(*it))
98  result.insert(*it);
99  *this = std::move(result);
100  return *this; }
102  { for (const_iterator it = src.begin(); it != src.end(); ++it)
103  erase(*it);
104  return *this; }
105 };
106 
107 class UT_SortedStringSet : public UT_SortedSet<UT_StringHolder>
108 {
110 
111 public:
114  typedef Parent::size_type size_type;
115 
117  { return Parent::erase(pos); }
119  { return Parent::erase(first, last); }
120 
122  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
123  UT_STRINGREF_WRAPPER_RANGE(const_iterator, equal_range, const)
125  UT_STRINGREF_WRAPPER(iterator, find, )
126  UT_STRINGREF_WRAPPER(const_iterator, find, const)
127  UT_STRINGREF_WRAPPER(iterator, lower_bound, )
128  UT_STRINGREF_WRAPPER(const_iterator, lower_bound, const)
129  UT_STRINGREF_WRAPPER(iterator, upper_bound, )
130  UT_STRINGREF_WRAPPER(const_iterator, upper_bound, const)
131 
132  bool contains(const UT_StringRef &ref) const
133  { return count(ref) > 0; }
134 
135  template <typename S>
136  bool containsAll(const S &src) const
137  {
138  return std::all_of(std::begin(src), std::end(src),
139  [this](const auto v) { return contains(v); });
140  }
141 
142  /// Set-wise boolean operations.
144  { for (const_iterator it = src.begin(); it != src.end(); ++it)
145  insert(*it);
146  return *this; }
149  for (const_iterator it = src.begin(); it != src.end(); ++it)
150  if (contains(*it))
151  result.insert(*it);
152  *this = std::move(result);
153  return *this; }
155  { for (const_iterator it = src.begin(); it != src.end(); ++it)
156  erase(*it);
157  return *this; }
158 };
159 
160 #undef UT_STRINGREF_WRAPPER
161 #undef UT_STRINGREF_WRAPPER_RANGE
162 
163 #endif
GLint first
Definition: glcorearb.h:405
Parent::key_type key_type
Definition: UT_StringSet.h:50
UT_StringSet(std::initializer_list< UT_StringHolder > init)
Constructs a set from an initializer list.
Definition: UT_StringSet.h:65
Definition: UT_Set.h:58
UT_StringSet()=default
Parent::key_equal key_equal
Definition: UT_StringSet.h:53
bool containsAll(const S &src) const
Definition: UT_StringSet.h:83
Base::key_type key_type
Definition: UT_Set.h:154
Parent::hasher hasher
Definition: UT_StringSet.h:52
const GLdouble * v
Definition: glcorearb.h:837
Parent::size_type size_type
Definition: UT_StringSet.h:56
Parent::size_type size_type
Definition: UT_StringSet.h:114
UT_StringSet & operator-=(const UT_StringSet &src)
Definition: UT_StringSet.h:101
Base::const_iterator const_iterator
Definition: UT_Set.h:159
bool contains(const UT_StringRef &ref) const
Definition: UT_StringSet.h:79
**But if you need a result
Definition: thread.h:613
OIIO_FORCEINLINE vbool4 insert(const vbool4 &a, bool val)
Helper: substitute val for a[i].
Definition: simd.h:3436
iterator erase(const_iterator first, const_iterator last)
Definition: UT_StringSet.h:118
UT_StringSet & operator|=(const UT_StringSet &src)
Set-wise boolean operations.
Definition: UT_StringSet.h:90
GLint ref
Definition: glcorearb.h:124
GLuint GLuint end
Definition: glcorearb.h:475
Base::value_type value_type
Definition: UT_Set.h:155
UT_SortedStringSet & operator&=(const UT_SortedStringSet &src)
Definition: UT_StringSet.h:147
Base::const_iterator const_iterator
Definition: UT_Set.h:230
Parent::const_iterator const_iterator
Definition: UT_StringSet.h:55
#define UT_STRINGREF_WRAPPER(return_type, name, qualifier)
Definition: UT_StringSet.h:26
Parent::const_iterator const_iterator
Definition: UT_StringSet.h:112
Base::key_equal key_equal
Definition: UT_Set.h:157
iterator erase(const_iterator pos)
Definition: UT_StringSet.h:116
__hostdev__ uint64_t last(uint32_t i) const
Definition: NanoVDB.h:5976
Parent::value_type value_type
Definition: UT_StringSet.h:51
UT_StringSet & operator&=(const UT_StringSet &src)
Definition: UT_StringSet.h:94
UT_StringSet(InputIt first, InputIt last)
Constructs a set from an iterator range.
Definition: UT_StringSet.h:62
iterator erase(const_iterator first, const_iterator last)
Definition: UT_StringSet.h:69
UT_SortedStringSet & operator-=(const UT_SortedStringSet &src)
Definition: UT_StringSet.h:154
iterator erase(const_iterator pos)
Definition: UT_StringSet.h:67
#define UT_STRINGREF_WRAPPER_RANGE(iterator_type, name, qualifier)
Definition: UT_StringSet.h:34
bool contains(const UT_StringRef &ref) const
Definition: UT_StringSet.h:132
bool containsAll(const S &src) const
Definition: UT_StringSet.h:136
UT_SortedStringSet & operator|=(const UT_SortedStringSet &src)
Set-wise boolean operations.
Definition: UT_StringSet.h:143
Parent::iterator iterator
Definition: UT_StringSet.h:54
Base::hasher hasher
Definition: UT_Set.h:156
GLint GLsizei count
Definition: glcorearb.h:405
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2089
GLenum src
Definition: glcorearb.h:1793
Base::iterator iterator
Definition: UT_Set.h:158
Parent::iterator iterator
Definition: UT_StringSet.h:113
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:558