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  iterator erase(const_iterator pos) { return Parent::erase(pos); }
56  {
57  return Parent::erase(first, last);
58  }
59 
61  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
64  UT_STRINGREF_WRAPPER(const_iterator, find, const)
65 
67  { return count(ref) > 0; }
68 
69  bool contains(const UT_StringSet &src) const
70  { for (const_iterator it = src.begin(); it != src.end(); ++it)
71  if (!contains(*it))
72  return false;
73  return true; }
74 
75  /// Set-wise boolean operations.
77  { for (const_iterator it = src.begin(); it != src.end(); ++it)
78  insert(*it);
79  return *this; }
81  { UT_StringSet result;
82  for (const_iterator it = src.begin(); it != src.end(); ++it)
83  if (contains(*it))
84  result.insert(*it);
85  *this = std::move(result);
86  return *this; }
88  { for (const_iterator it = src.begin(); it != src.end(); ++it)
89  erase(*it);
90  return *this; }
91 
92 };
93 
94 class UT_SortedStringSet : public UT_SortedSet<UT_StringHolder>
95 {
97 
98 public:
101  typedef Parent::size_type size_type;
102 
103  // NOTE: When we compile against a more modern standard library on OSX, the
104  // signature can be changed to return an iterator to the following element
105  // (the signature was changed in C++11).
106  void erase(const_iterator pos) { Parent::erase(pos); }
108  {
109  Parent::erase(first, last);
110  }
111 
113  UT_STRINGREF_WRAPPER_RANGE(iterator, equal_range, )
116  UT_STRINGREF_WRAPPER(iterator, find, )
117  UT_STRINGREF_WRAPPER(const_iterator, find, const)
118  UT_STRINGREF_WRAPPER(iterator, lower_bound, )
119  UT_STRINGREF_WRAPPER(const_iterator, lower_bound, const)
120  UT_STRINGREF_WRAPPER(iterator, upper_bound, )
121  UT_STRINGREF_WRAPPER(const_iterator, upper_bound, const)
122 
124  { return count(ref) > 0; }
125 
126  bool contains(const UT_SortedStringSet &src) const
127  { for (const_iterator it = src.begin(); it != src.end(); ++it)
128  if (!contains(*it))
129  return false;
130  return true; }
131 
132  /// Set-wise boolean operations.
134  { for (const_iterator it = src.begin(); it != src.end(); ++it)
135  insert(*it);
136  return *this; }
138  { UT_SortedStringSet result;
139  for (const_iterator it = src.begin(); it != src.end(); ++it)
140  if (contains(*it))
141  result.insert(*it);
142  *this = std::move(result);
143  return *this; }
145  { for (const_iterator it = src.begin(); it != src.end(); ++it)
146  erase(*it);
147  return *this; }
148 };
149 
150 #undef UT_STRINGREF_WRAPPER
151 #undef UT_STRINGREF_WRAPPER_RANGE
152 
153 #endif
bool contains(const UT_StringSet &src) const
Definition: UT_StringSet.h:69
GLint first
Definition: glcorearb.h:404
Definition: UT_Set.h:58
bool contains(const UT_SortedStringSet &src) const
Definition: UT_StringSet.h:126
void erase(const_iterator first, const_iterator last)
Definition: UT_StringSet.h:107
Parent::size_type size_type
Definition: UT_StringSet.h:52
Parent::size_type size_type
Definition: UT_StringSet.h:101
UT_StringSet & operator-=(const UT_StringSet &src)
Definition: UT_StringSet.h:87
Base::const_iterator const_iterator
Definition: UT_Set.h:146
bool contains(const UT_StringRef &ref) const
Definition: UT_StringSet.h:66
UT_StringSet & operator|=(const UT_StringSet &src)
Set-wise boolean operations.
Definition: UT_StringSet.h:76
GLint ref
Definition: glcorearb.h:123
UT_SortedStringSet & operator&=(const UT_SortedStringSet &src)
Definition: UT_StringSet.h:137
Base::const_iterator const_iterator
Definition: UT_Set.h:219
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:99
GLint GLsizei count
Definition: glcorearb.h:404
UT_StringSet & operator&=(const UT_StringSet &src)
Definition: UT_StringSet.h:80
iterator erase(const_iterator first, const_iterator last)
Definition: UT_StringSet.h:55
void erase(const_iterator pos)
Definition: UT_StringSet.h:106
UT_SortedStringSet & operator-=(const UT_SortedStringSet &src)
Definition: UT_StringSet.h:144
iterator erase(const_iterator pos)
Definition: UT_StringSet.h:54
#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:123
#define const
Definition: zconf.h:214
UT_SortedStringSet & operator|=(const UT_SortedStringSet &src)
Set-wise boolean operations.
Definition: UT_StringSet.h:133
Parent::iterator iterator
Definition: UT_StringSet.h:51
GLenum src
Definition: glcorearb.h:1792
Base::iterator iterator
Definition: UT_Set.h:145
Parent::iterator iterator
Definition: UT_StringSet.h:100