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, )
76  UT_STRINGREF_WRAPPER(iterator, find, )
77  UT_STRINGREF_WRAPPER(const_iterator, find, const)
78 
80  { return count(ref) > 0; }
81 
82  bool contains(const UT_StringSet &src) const
83  { for (const_iterator it = src.begin(); it != src.end(); ++it)
84  if (!contains(*it))
85  return false;
86  return true; }
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, )
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 
132  { return count(ref) > 0; }
133 
134  bool contains(const UT_SortedStringSet &src) const
135  { for (const_iterator it = src.begin(); it != src.end(); ++it)
136  if (!contains(*it))
137  return false;
138  return true; }
139 
140  /// Set-wise boolean operations.
142  { for (const_iterator it = src.begin(); it != src.end(); ++it)
143  insert(*it);
144  return *this; }
147  for (const_iterator it = src.begin(); it != src.end(); ++it)
148  if (contains(*it))
149  result.insert(*it);
150  *this = std::move(result);
151  return *this; }
153  { for (const_iterator it = src.begin(); it != src.end(); ++it)
154  erase(*it);
155  return *this; }
156 };
157 
158 #undef UT_STRINGREF_WRAPPER
159 #undef UT_STRINGREF_WRAPPER_RANGE
160 
161 #endif
bool contains(const UT_StringSet &src) const
Definition: UT_StringSet.h:82
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
bool contains(const UT_SortedStringSet &src) const
Definition: UT_StringSet.h:134
UT_StringSet()=default
Parent::key_equal key_equal
Definition: UT_StringSet.h:53
Base::key_type key_type
Definition: UT_Set.h:154
Parent::hasher hasher
Definition: UT_StringSet.h:52
Parent::size_type size_type
Definition: UT_StringSet.h:56
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: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:117
UT_StringSet & operator|=(const UT_StringSet &src)
Set-wise boolean operations.
Definition: UT_StringSet.h:89
GLint ref
Definition: glcorearb.h:124
Base::value_type value_type
Definition: UT_Set.h:155
UT_SortedStringSet & operator&=(const UT_SortedStringSet &src)
Definition: UT_StringSet.h:145
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:111
Base::key_equal key_equal
Definition: UT_Set.h:157
iterator erase(const_iterator pos)
Definition: UT_StringSet.h:115
Parent::value_type value_type
Definition: UT_StringSet.h:51
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: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:152
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:131
#define const
Definition: zconf.h:214
UT_SortedStringSet & operator|=(const UT_SortedStringSet &src)
Set-wise boolean operations.
Definition: UT_StringSet.h:141
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:112