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:
52  typedef Parent::size_type size_type;
53 
54  UT_StringSet() = default;
55 
56  /// Constructs a set from an iterator range.
57  template <typename InputIt>
58  UT_StringSet(InputIt first, InputIt last) : Parent(first, last) {}
59 
60  /// Constructs a set from an initializer list.
61  UT_StringSet(std::initializer_list<UT_StringHolder> init) : Parent(init) {}
62 
64  { return Parent::erase(pos); }
66  { return Parent::erase(first, last); }
67 
69  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
72  UT_STRINGREF_WRAPPER(iterator, find, )
73  UT_STRINGREF_WRAPPER(const_iterator, find, const)
74 
76  { return count(ref) > 0; }
77 
78  bool contains(const UT_StringSet &src) const
79  { for (const_iterator it = src.begin(); it != src.end(); ++it)
80  if (!contains(*it))
81  return false;
82  return true; }
83 
84  /// Set-wise boolean operations.
86  { for (const_iterator it = src.begin(); it != src.end(); ++it)
87  insert(*it);
88  return *this; }
91  for (const_iterator it = src.begin(); it != src.end(); ++it)
92  if (contains(*it))
93  result.insert(*it);
94  *this = std::move(result);
95  return *this; }
97  { for (const_iterator it = src.begin(); it != src.end(); ++it)
98  erase(*it);
99  return *this; }
100 };
101 
102 class UT_SortedStringSet : public UT_SortedSet<UT_StringHolder>
103 {
105 
106 public:
109  typedef Parent::size_type size_type;
110 
112  { return Parent::erase(pos); }
114  { return Parent::erase(first, last); }
115 
117  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
120  UT_STRINGREF_WRAPPER(iterator, find, )
121  UT_STRINGREF_WRAPPER(const_iterator, find, const)
122  UT_STRINGREF_WRAPPER(iterator, lower_bound, )
123  UT_STRINGREF_WRAPPER(const_iterator, lower_bound, const)
124  UT_STRINGREF_WRAPPER(iterator, upper_bound, )
125  UT_STRINGREF_WRAPPER(const_iterator, upper_bound, const)
126 
128  { return count(ref) > 0; }
129 
130  bool contains(const UT_SortedStringSet &src) const
131  { for (const_iterator it = src.begin(); it != src.end(); ++it)
132  if (!contains(*it))
133  return false;
134  return true; }
135 
136  /// Set-wise boolean operations.
138  { for (const_iterator it = src.begin(); it != src.end(); ++it)
139  insert(*it);
140  return *this; }
143  for (const_iterator it = src.begin(); it != src.end(); ++it)
144  if (contains(*it))
145  result.insert(*it);
146  *this = std::move(result);
147  return *this; }
149  { for (const_iterator it = src.begin(); it != src.end(); ++it)
150  erase(*it);
151  return *this; }
152 };
153 
154 #undef UT_STRINGREF_WRAPPER
155 #undef UT_STRINGREF_WRAPPER_RANGE
156 
157 #endif
bool contains(const UT_StringSet &src) const
Definition: UT_StringSet.h:78
GLint ref
Definition: glcorearb.h:123
vbool4 insert(const vbool4 &a, bool val)
Helper: substitute val for a[i].
Definition: simd.h:3414
UT_StringSet(std::initializer_list< UT_StringHolder > init)
Constructs a set from an initializer list.
Definition: UT_StringSet.h:61
Definition: UT_Set.h:58
bool contains(const UT_SortedStringSet &src) const
Definition: UT_StringSet.h:130
UT_StringSet()=default
GLint first
Definition: glcorearb.h:404
Parent::size_type size_type
Definition: UT_StringSet.h:52
Parent::size_type size_type
Definition: UT_StringSet.h:109
UT_StringSet & operator-=(const UT_StringSet &src)
Definition: UT_StringSet.h:96
Base::const_iterator const_iterator
Definition: UT_Set.h:144
bool contains(const UT_StringRef &ref) const
Definition: UT_StringSet.h:75
GLenum src
Definition: glcorearb.h:1792
iterator erase(const_iterator first, const_iterator last)
Definition: UT_StringSet.h:113
UT_StringSet & operator|=(const UT_StringSet &src)
Set-wise boolean operations.
Definition: UT_StringSet.h:85
FMT_CONSTEXPR bool find(Ptr first, Ptr last, T value, Ptr &out)
Definition: format.h:2929
UT_SortedStringSet & operator&=(const UT_SortedStringSet &src)
Definition: UT_StringSet.h:141
Base::const_iterator const_iterator
Definition: UT_Set.h:215
GLint GLsizei count
Definition: glcorearb.h:404
Parent::const_iterator const_iterator
Definition: UT_StringSet.h:50
#define UT_STRINGREF_WRAPPER(return_type, name, qualifier)
Definition: UT_StringSet.h:26
Parent::const_iterator const_iterator
Definition: UT_StringSet.h:107
iterator erase(const_iterator pos)
Definition: UT_StringSet.h:111
UT_StringSet & operator&=(const UT_StringSet &src)
Definition: UT_StringSet.h:89
UT_StringSet(InputIt first, InputIt last)
Constructs a set from an iterator range.
Definition: UT_StringSet.h:58
iterator erase(const_iterator first, const_iterator last)
Definition: UT_StringSet.h:65
UT_SortedStringSet & operator-=(const UT_SortedStringSet &src)
Definition: UT_StringSet.h:148
iterator erase(const_iterator pos)
Definition: UT_StringSet.h:63
#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:127
#define const
Definition: zconf.h:214
UT_SortedStringSet & operator|=(const UT_SortedStringSet &src)
Set-wise boolean operations.
Definition: UT_StringSet.h:137
Parent::iterator iterator
Definition: UT_StringSet.h:51
Base::iterator iterator
Definition: UT_Set.h:143
Parent::iterator iterator
Definition: UT_StringSet.h:108