HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 #ifndef __UT_StringSet_h__
12 #define __UT_StringSet_h__
13 
14 #include "UT_API.h"
15 #include "UT_ArraySet.h"
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:
51  typedef Parent::size_type size_type;
52 
53  iterator erase(const_iterator pos) { return Parent::erase(pos); }
55  {
56  return Parent::erase(first, last);
57  }
58 
60  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
63  UT_STRINGREF_WRAPPER(const_iterator, find, const)
64 
66  { return count(ref) > 0; }
67 
68  bool contains(const UT_StringSet &src) const
69  { for (const_iterator it = src.begin(); it != src.end(); ++it)
70  if (!contains(*it))
71  return false;
72  return true; }
73 
74  /// Set-wise boolean operations.
76  { for (const_iterator it = src.begin(); it != src.end(); ++it)
77  insert(*it);
78  return *this; }
80  { UT_StringSet result;
81  for (const_iterator it = src.begin(); it != src.end(); ++it)
82  if (contains(*it))
83  result.insert(*it);
84  *this = std::move(result);
85  return *this; }
87  { for (const_iterator it = src.begin(); it != src.end(); ++it)
88  erase(*it);
89  return *this; }
90 
91 };
92 
93 class UT_SortedStringSet : public UT_SortedSet<UT_StringHolder>
94 {
96 
97 public:
100  typedef Parent::size_type size_type;
101 
102  // NOTE: When we compile against a more modern standard library on OSX, the
103  // signature can be changed to return an iterator to the following element
104  // (the signature was changed in C++11).
105  void erase(const_iterator pos) { Parent::erase(pos); }
107  {
108  Parent::erase(first, last);
109  }
110 
112  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
115  UT_STRINGREF_WRAPPER(iterator, find, )
116  UT_STRINGREF_WRAPPER(const_iterator, find, const)
117  UT_STRINGREF_WRAPPER(iterator, lower_bound, )
118  UT_STRINGREF_WRAPPER(const_iterator, lower_bound, const)
119  UT_STRINGREF_WRAPPER(iterator, upper_bound, )
120  UT_STRINGREF_WRAPPER(const_iterator, upper_bound, const)
121 
123  { return count(ref) > 0; }
124 
125  bool contains(const UT_SortedStringSet &src) const
126  { for (const_iterator it = src.begin(); it != src.end(); ++it)
127  if (!contains(*it))
128  return false;
129  return true; }
130 
131  /// Set-wise boolean operations.
133  { for (const_iterator it = src.begin(); it != src.end(); ++it)
134  insert(*it);
135  return *this; }
137  { UT_SortedStringSet result;
138  for (const_iterator it = src.begin(); it != src.end(); ++it)
139  if (contains(*it))
140  result.insert(*it);
141  *this = std::move(result);
142  return *this; }
144  { for (const_iterator it = src.begin(); it != src.end(); ++it)
145  erase(*it);
146  return *this; }
147 };
148 
149 /// UT_StringSet is a simple specialization of a UT_Set that has
150 /// UT_StringHolder as its key type which allows C strings to be used.
151 /// If you know that a string will not be destroyed during the set's
152 /// lifetime, UTmakeUnsafeRef can be used to insert a shallow reference.
153 class UT_ArrayStringSet : public UT::ArraySet<UT_StringHolder>
154 {
155 public:
157 
158  typedef Parent::const_iterator const_iterator;
161 
162  iterator erase(iterator pos) { return Parent::erase(pos); }
164  {
165  return Parent::erase(first, last);
166  }
167 
172  UT_STRINGREF_WRAPPER(const_iterator, find, const)
173 
175  { return count(ref) > 0; }
176 
177  bool contains(const UT_ArrayStringSet &src) const
178  { for (const_iterator it = src.begin(); it != src.end(); ++it)
179  if (!contains(*it))
180  return false;
181  return true; }
182 
183  /// Set-wise boolean operations.
185  { for (const_iterator it = src.begin(); it != src.end(); ++it)
186  insert(*it);
187  return *this; }
189  { UT_ArrayStringSet result;
190  for (const_iterator it = src.begin(); it != src.end(); ++it)
191  if (contains(*it))
192  result.insert(*it);
193  *this = std::move(result);
194  return *this; }
196  { for (const_iterator it = src.begin(); it != src.end(); ++it)
197  erase(*it);
198  return *this; }
199 
200 };
201 
202 namespace UT {
203 template<>
205  : public DefaultClearer<typename UT_ArrayStringSet::Parent>
206 {};
207 }
208 
209 #undef UT_STRINGREF_WRAPPER
210 #undef UT_STRINGREF_WRAPPER_RANGE
211 
212 #endif
bool contains(const UT_StringSet &src) const
Definition: UT_StringSet.h:68
GLint first
Definition: glcorearb.h:404
bool contains(const UT_SortedStringSet &src) const
Definition: UT_StringSet.h:125
std::pair< iterator, bool > insert(const value_type &value)
Definition: UT_ArraySet.h:1006
void erase(const_iterator first, const_iterator last)
Definition: UT_StringSet.h:106
Parent::size_type size_type
Definition: UT_StringSet.h:51
Parent::size_type size_type
Definition: UT_StringSet.h:100
UT_StringSet & operator-=(const UT_StringSet &src)
Definition: UT_StringSet.h:86
Base::const_iterator const_iterator
Definition: UT_Set.h:146
bool contains(const UT_StringRef &ref) const
Definition: UT_StringSet.h:65
iterator end()
Returns a non-const end iterator for the set.
Definition: UT_ArraySet.h:717
bool contains(const UT_StringRef &ref) const
Definition: UT_StringSet.h:174
iterator erase(iterator iter)
Definition: UT_ArraySet.h:1083
UT_StringSet & operator|=(const UT_StringSet &src)
Set-wise boolean operations.
Definition: UT_StringSet.h:75
GLint ref
Definition: glcorearb.h:123
Parent::size_type size_type
Definition: UT_StringSet.h:160
iterator begin()
Returns a non-const iterator for the beginning of the set.
Definition: UT_ArraySet.h:702
UT_SortedStringSet & operator&=(const UT_SortedStringSet &src)
Definition: UT_StringSet.h:136
Base::const_iterator const_iterator
Definition: UT_Set.h:219
Parent::const_iterator const_iterator
Definition: UT_StringSet.h:49
#define UT_STRINGREF_WRAPPER(return_type, name, qualifier)
Definition: UT_StringSet.h:25
iterator find(const UT_StringHolder &key)
Definition: UT_ArraySet.h:738
Parent::const_iterator const_iterator
Definition: UT_StringSet.h:98
GLint GLsizei count
Definition: glcorearb.h:404
bool contains(const UT_ArrayStringSet &src) const
Definition: UT_StringSet.h:177
size_type count(const UT_StringHolder &key) const
Definition: UT_ArraySet.h:798
UT_StringSet & operator&=(const UT_StringSet &src)
Definition: UT_StringSet.h:79
iterator erase(const_iterator first, const_iterator last)
Definition: UT_StringSet.h:54
Parent::const_iterator const_iterator
Definition: UT_StringSet.h:158
UT_ArrayStringSet & operator|=(const UT_ArrayStringSet &src)
Set-wise boolean operations.
Definition: UT_StringSet.h:184
void erase(const_iterator pos)
Definition: UT_StringSet.h:105
UT_SortedStringSet & operator-=(const UT_SortedStringSet &src)
Definition: UT_StringSet.h:143
iterator erase(const_iterator pos)
Definition: UT_StringSet.h:53
UT_ArrayStringSet & operator-=(const UT_ArrayStringSet &src)
Definition: UT_StringSet.h:195
#define UT_STRINGREF_WRAPPER_RANGE(iterator_type, name, qualifier)
Definition: UT_StringSet.h:33
std::pair< const_iterator, const_iterator > equal_range(const UT_StringHolder &key) const
Definition: UT_ArraySet.h:868
UT::ArraySet< UT_StringHolder > Parent
Definition: UT_StringSet.h:156
bool contains(const UT_StringRef &ref) const
Definition: UT_StringSet.h:122
iterator_t< false > iterator
Iterator type for iterating over non-constant elements.
Definition: UT_ArraySet.h:685
#define const
Definition: zconf.h:214
UT_SortedStringSet & operator|=(const UT_SortedStringSet &src)
Set-wise boolean operations.
Definition: UT_StringSet.h:132
Parent::iterator iterator
Definition: UT_StringSet.h:159
Parent::iterator iterator
Definition: UT_StringSet.h:50
UT_ArrayStringSet & operator&=(const UT_ArrayStringSet &src)
Definition: UT_StringSet.h:188
iterator erase(iterator pos)
Definition: UT_StringSet.h:162
iterator erase(iterator first, iterator last)
Definition: UT_StringSet.h:163
GLenum src
Definition: glcorearb.h:1792
Base::iterator iterator
Definition: UT_Set.h:145
Parent::iterator iterator
Definition: UT_StringSet.h:99