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_Set.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_StringSet is a simple specialization of a UT_Set 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 class UT_StringSet : public UT_Set<UT_StringHolder>
45 {
47 
48 public:
55  typedef Parent::size_type size_type;
56 
57  UT_StringSet() = default;
58 
59  /// Constructs a set from an iterator range.
60  template <typename InputIt>
61  UT_StringSet(InputIt first, InputIt last) : Parent(first, last) {}
62 
63  /// Constructs a set from an initializer list.
64  UT_StringSet(std::initializer_list<UT_StringHolder> init) : Parent(init) {}
65 
67  { return Parent::erase(pos); }
69  { return Parent::erase(first, last); }
70 
72  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
73  UT_STRINGREF_WRAPPER_RANGE(const_iterator, equal_range, const)
75  UT_STRINGREF_WRAPPER(iterator, find, )
76  UT_STRINGREF_WRAPPER(const_iterator, find, const)
77 
78  bool contains(const UT_StringRef &ref) const
79  { return count(ref) > 0; }
80 
81  template <typename S>
82  bool containsAll(const S &src) const
83  {
84  return std::all_of(std::begin(src), std::end(src),
85  [this](const auto v) { return contains(v); });
86  }
87 
88  /// Set-wise boolean operations.
90  { for (const_iterator it = src.begin(); it != src.end(); ++it)
91  insert(*it);
92  return *this; }
95  for (const_iterator it = src.begin(); it != src.end(); ++it)
96  if (contains(*it))
97  result.insert(*it);
98  *this = std::move(result);
99  return *this; }
101  { for (const_iterator it = src.begin(); it != src.end(); ++it)
102  erase(*it);
103  return *this; }
104 };
105 
106 class UT_SortedStringSet : public UT_SortedSet<UT_StringHolder>
107 {
109 
110 public:
113  typedef Parent::size_type size_type;
114 
116  { return Parent::erase(pos); }
118  { return Parent::erase(first, last); }
119 
121  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
122  UT_STRINGREF_WRAPPER_RANGE(const_iterator, equal_range, const)
124  UT_STRINGREF_WRAPPER(iterator, find, )
125  UT_STRINGREF_WRAPPER(const_iterator, find, const)
126  UT_STRINGREF_WRAPPER(iterator, lower_bound, )
127  UT_STRINGREF_WRAPPER(const_iterator, lower_bound, const)
128  UT_STRINGREF_WRAPPER(iterator, upper_bound, )
129  UT_STRINGREF_WRAPPER(const_iterator, upper_bound, const)
130 
131  bool contains(const UT_StringRef &ref) const
132  { return count(ref) > 0; }
133 
134  template <typename S>
135  bool containsAll(const S &src) const
136  {
137  return std::all_of(std::begin(src), std::end(src),
138  [this](const auto v) { return contains(v); });
139  }
140 
141  /// Set-wise boolean operations.
143  { for (const_iterator it = src.begin(); it != src.end(); ++it)
144  insert(*it);
145  return *this; }
148  for (const_iterator it = src.begin(); it != src.end(); ++it)
149  if (contains(*it))
150  result.insert(*it);
151  *this = std::move(result);
152  return *this; }
154  { for (const_iterator it = src.begin(); it != src.end(); ++it)
155  erase(*it);
156  return *this; }
157 };
158 
159 #undef UT_STRINGREF_WRAPPER
160 #undef UT_STRINGREF_WRAPPER_RANGE
161 
162 #endif
GLint first
Definition: glcorearb.h:405
Parent::key_type key_type
Definition: UT_StringSet.h:49
UT_StringSet(std::initializer_list< UT_StringHolder > init)
Constructs a set from an initializer list.
Definition: UT_StringSet.h:64
Definition: UT_Set.h:58
UT_StringSet()=default
Parent::key_equal key_equal
Definition: UT_StringSet.h:52
bool containsAll(const S &src) const
Definition: UT_StringSet.h:82
Base::key_type key_type
Definition: UT_Set.h:154
Parent::hasher hasher
Definition: UT_StringSet.h:51
const GLdouble * v
Definition: glcorearb.h:837
Parent::size_type size_type
Definition: UT_StringSet.h:55
Parent::size_type size_type
Definition: UT_StringSet.h:113
UT_StringSet & operator-=(const UT_StringSet &src)
Definition: UT_StringSet.h:100
Base::const_iterator const_iterator
Definition: UT_Set.h:159
bool contains(const UT_StringRef &ref) const
Definition: UT_StringSet.h:78
**But if you need a result
Definition: thread.h:622
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2138
OIIO_FORCEINLINE vbool4 insert(const vbool4 &a, bool val)
Helper: substitute val for a[i].
Definition: simd.h:3556
iterator erase(const_iterator first, const_iterator last)
Definition: UT_StringSet.h:117
UT_StringSet & operator|=(const UT_StringSet &src)
Set-wise boolean operations.
Definition: UT_StringSet.h:89
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:146
Base::const_iterator const_iterator
Definition: UT_Set.h:230
Parent::const_iterator const_iterator
Definition: UT_StringSet.h:54
#define UT_STRINGREF_WRAPPER(return_type, name, qualifier)
Definition: UT_StringSet.h:25
Parent::const_iterator const_iterator
Definition: UT_StringSet.h:111
Base::key_equal key_equal
Definition: UT_Set.h:157
iterator erase(const_iterator pos)
Definition: UT_StringSet.h:115
__hostdev__ uint64_t last(uint32_t i) const
Definition: NanoVDB.h:5976
Parent::value_type value_type
Definition: UT_StringSet.h:50
UT_StringSet & operator&=(const UT_StringSet &src)
Definition: UT_StringSet.h:93
UT_StringSet(InputIt first, InputIt last)
Constructs a set from an iterator range.
Definition: UT_StringSet.h:61
iterator erase(const_iterator first, const_iterator last)
Definition: UT_StringSet.h:68
UT_SortedStringSet & operator-=(const UT_SortedStringSet &src)
Definition: UT_StringSet.h:153
iterator erase(const_iterator pos)
Definition: UT_StringSet.h:66
#define UT_STRINGREF_WRAPPER_RANGE(iterator_type, name, qualifier)
Definition: UT_StringSet.h:33
bool contains(const UT_StringRef &ref) const
Definition: UT_StringSet.h:131
bool containsAll(const S &src) const
Definition: UT_StringSet.h:135
UT_SortedStringSet & operator|=(const UT_SortedStringSet &src)
Set-wise boolean operations.
Definition: UT_StringSet.h:142
Parent::iterator iterator
Definition: UT_StringSet.h:53
Base::hasher hasher
Definition: UT_Set.h:156
GLint GLsizei count
Definition: glcorearb.h:405
GLenum src
Definition: glcorearb.h:1793
Base::iterator iterator
Definition: UT_Set.h:158
Parent::iterator iterator
Definition: UT_StringSet.h:112
PcpNodeRef_ChildrenIterator begin(const PcpNodeRef::child_const_range &r)
Support for range-based for loops for PcpNodeRef children ranges.
Definition: node.h:566