HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_ArrayStringSet.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_ArrayStringSet.h
7  *
8  * COMMENTS: An array-based hash set for UT_StringHolder.
9  */
10 
11 #pragma once
12 
13 #ifndef __UT_ArrayStringSet_h__
14 #define __UT_ArrayStringSet_h__
15 
16 #include "UT_ArraySet.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_ArrayStringSet is a simple specialization of a UT::ArraySet 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 /// NOTE: This class *used to* not allow empty string as a key,
45 /// but now it should work.
46 class UT_ArrayStringSet : public UT::ArraySet<UT_StringHolder>
47 {
48 public:
50 
51  typedef Parent::const_iterator const_iterator;
54 
55  // Inherit parent class constructors
56  using Parent::Parent;
57 
58  iterator erase(iterator pos) { return Parent::erase(pos); }
59 #if 0 // The parent function isn't fully implemented yet.
61  {
62  return Parent::erase(first, last);
63  }
64 #endif
65 
70  UT_STRINGREF_WRAPPER(iterator, find, )
71  UT_STRINGREF_WRAPPER(const_iterator, find, const)
72 
74  { return count(ref) > 0; }
75 
76  bool contains(const UT_ArrayStringSet &src) const
77  { for (const_iterator it = src.begin(); it != src.end(); ++it)
78  if (!contains(*it))
79  return false;
80  return true; }
81 
82  /// Set-wise boolean operations.
84  { for (const_iterator it = src.begin(); it != src.end(); ++it)
85  insert(*it);
86  return *this; }
89  for (const_iterator it = src.begin(); it != src.end(); ++it)
90  if (contains(*it))
91  result.insert(*it);
92  *this = std::move(result);
93  return *this; }
95  { for (const_iterator it = src.begin(); it != src.end(); ++it)
96  erase(*it);
97  return *this; }
98 
99  using Parent::insert;
100  std::pair<iterator, bool> insert(const UT_StringRef &key)
101  {
102  auto&& iter = Parent::find(UTmakeUnsafeRef(key));
103  if (iter == end())
104  return insert(UT_StringHolder(key));
105  return std::make_pair(iter, false);
106  }
107  std::pair<iterator, bool> insert(UT_StringRef &&key)
108  {
109  auto&& iter = Parent::find(UTmakeUnsafeRef(key));
110  if (iter == end())
111  return insert(UT_StringHolder(std::move(key)));
112  return std::make_pair(iter, false);
113  }
114  std::pair<iterator, bool> insert(const char *key)
115  {
116  return insert(UT_StringRef(key));
117  }
118 };
119 
120 namespace UT {
121 template<>
123  : public DefaultClearer<typename UT_ArrayStringSet::Parent>
124 {};
125 }
126 
127 #undef UT_STRINGREF_WRAPPER
128 #undef UT_STRINGREF_WRAPPER_RANGE
129 
130 #endif
std::pair< iterator, bool > insert(UT_StringRef &&key)
std::pair< iterator, bool > insert(const value_type &value)
Definition: UT_ArraySet.h:962
GLenum src
Definition: glew.h:2410
GLenum GLint ref
Definition: glew.h:1845
#define UT_STRINGREF_WRAPPER_RANGE(iterator_type, name, qualifier)
const GLint * first
Definition: glew.h:1528
iterator end()
Returns a non-const end iterator for the set.
Definition: UT_ArraySet.h:673
#define UT_STRINGREF_WRAPPER(return_type, name, qualifier)
bool contains(const UT_StringRef &ref) const
std::pair< iterator, bool > insert(const char *key)
iterator erase(iterator iter)
Definition: UT_ArraySet.h:1039
Parent::size_type size_type
SYS_FORCE_INLINE const UT_StringHolder & UTmakeUnsafeRef(const UT_StringRef &ref)
Convert a UT_StringRef into a UT_StringHolder that is a shallow reference.
iterator begin()
Returns a non-const iterator for the beginning of the set.
Definition: UT_ArraySet.h:658
iterator find(const UT_StringHolder &key)
Definition: UT_ArraySet.h:694
bool contains(const UT_ArrayStringSet &src) const
std::pair< iterator, bool > insert(const UT_StringRef &key)
size_type count(const UT_StringHolder &key) const
Definition: UT_ArraySet.h:754
Parent::const_iterator const_iterator
UT_ArrayStringSet & operator|=(const UT_ArrayStringSet &src)
Set-wise boolean operations.
UT_ArrayStringSet & operator-=(const UT_ArrayStringSet &src)
std::pair< const_iterator, const_iterator > equal_range(const UT_StringHolder &key) const
Definition: UT_ArraySet.h:824
GLuint GLuint GLsizei count
Definition: glew.h:1253
UT::ArraySet< UT_StringHolder > Parent
GLuint64EXT * result
Definition: glew.h:14007
iterator_t< false > iterator
Iterator type for iterating over non-constant elements.
Definition: UT_ArraySet.h:641
#define const
Definition: zconf.h:214
Parent::iterator iterator
UT_ArrayStringSet & operator&=(const UT_ArrayStringSet &src)
iterator erase(iterator pos)