HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_StringArray.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 library (C++)
7  *
8  */
9 
10 #ifndef __UT_StringArray_h__
11 #define __UT_StringArray_h__
12 
13 #include "UT_API.h"
14 #include "UT_Array.h"
15 #include "UT_String.h"
16 #include "UT_StringHolder.h"
17 #include "UT_Swap.h"
18 
19 #include <iosfwd>
20 #include <string>
21 #include <type_traits>
22 
23 
24 class UT_API UT_StringArray : public UT_Array<UT_StringHolder>
25 {
26 public:
28 
29  // Inherit constructors
30  using Parent::Parent;
31 
33 
34  explicit UT_StringArray(const char **src);
35 
36  // Allow move/construction from parent type since we're just a wrapper
38  : Parent::UT_Array(src) {}
39 
41  : Parent::UT_Array(std::move(src)) {}
42 
43  // Copy/move constructors are not inherited and need to be specified
45  = default;
46 
47  UT_StringArray(const UT_StringArray &src) = default;
48 
49  UT_StringArray &operator=(const UT_StringArray &src) = default;
50  UT_StringArray &operator=(UT_StringArray &&src) = default;
51 
52  UT_StringArray &operator+=(const UT_StringArray &src);
53 
54  exint remove(exint idx);
55 
56  exint find(const UT_StringRef &str, exint s=0) const
58  UTmakeUnsafeRef(str), s); }
59 
61  Comparator compare) const
63  UTmakeUnsafeRef(str), compare); }
64 
65  void sort(bool forward, bool numbered);
66 
67  // We specialize the default case explicitly instead of using default
68  // parameters or else someone calling the sort(Comparator) method
69  // can unexpectedly call the wrong sort function if given the wrong
70  // comparison function pointer. This way, we can get a compiler error.
71  void sort() { sort(true, true); }
72 
75 
76  // This method allows arbitrary sorting of the array.
77  SYS_DEPRECATED_HDK_REPLACE(19.5, "Use ComparatorBool variant")
78  void sort(Comparator compare)
80 
82 
83 private:
84  // SFINAE constraint for bool comparators to avoid ambiguity
85  template <typename F>
86  using IsBoolComp = decltype(std::declval<F>()(UT_StringHolder{},
87  UT_StringHolder{}), void());
88 public:
89 
90  /// Sort string array using custom comparator
91  template <typename ComparatorBool,
92  typename = IsBoolComp<ComparatorBool>>
93  void sort(ComparatorBool is_less)
95 
96  exint sortedInsert(const char *str,
97  bool forward = true,
98  bool numbered = true);
99 
100  template <typename Compare>
101  void sortedInsert(const char *str, Compare is_less)
102  {
103  UT_StringHolder entry(str);
105  entry, is_less);
106  }
107 
108  exint sortedFind(const char *str,
109  bool forward = true,
110  bool numbered = true) const;
111 
112  exint getLongestPrefixLength() const;
113 
114  // Join the string array elements together, using the passed string
115  // as a separator between elements, and return the result.
116  void join(const char *sep, UT_String &result) const
117  { join(sep, sep, result); }
118  void join(const char *sep, UT_StringHolder &result) const
119  { join(sep, sep, result); }
120  void join(const char *sep, UT_WorkBuffer &result) const
121  { join(sep, sep, result); }
122 
123  // Join the string array elements together, using the first passed string
124  // as a separator between all elements except the last pair, in which case
125  // the second separator string is used, and return the result.
126  // (This can be used to create English join strings 'foo, bar and baz')
127  void join(const char *sep, const char *sep_last,
128  UT_String &result) const;
129  void join(const char *sep, const char *sep_last,
130  UT_StringHolder &result) const;
131  void join(const char *sep, const char *sep_last,
132  UT_WorkBuffer &result) const;
133 
134  // Compute a hash value from the hash for each string in the array.
135  // The hash value is order-independent - that is {"Cf", "Of"} will hash
136  // to the same value as {"Of", "Cf"}.
137  unsigned hash() const;
138 
139  template <typename I>
141  {
142  public:
144  : myValues(values)
145  {}
146  bool operator()(I a, I b) const
147  { return ::strcmp(myValues(a), myValues(b)) < 0; }
148  private:
149  const UT_StringArray &myValues;
150  };
151 
152  template <typename I>
154  {
155  indices.stableSort(IndexedCompare<I>(*this));
156  }
157 };
158 
159 // For UT::ArraySet.
160 namespace UT
161 {
162 template <typename T>
163 struct DefaultClearer;
164 
165 template <>
167 {
168  static void clear(UT_StringArray &v) { v.setCapacity(0); }
169  static bool isClear(const UT_StringArray &v) { return v.capacity() == 0; }
171  {
172  new ((void *)p) UT_StringArray();
173  }
174  static const bool clearNeedsDestruction = false;
175 };
176 }
177 
178 #endif // __UT_StringArray_h__
bool operator()(I a, I b) const
exint sortedFind(const UT_StringRef &str, Comparator compare) const
GLsizei GLenum const void * indices
Definition: glcorearb.h:406
#define SYS_PRAGMA_PUSH_WARN()
Definition: SYS_Pragma.h:34
void stableSort(ComparatorBool is_less={})
Definition: UT_Array.h:496
const GLdouble * v
Definition: glcorearb.h:837
OIIO_UTIL_API bool remove(string_view path, std::string &err)
UT_StringArray(const Parent &src)
#define SYS_DEPRECATED_HDK_REPLACE(__V__, __R__)
CompareResults OIIO_API compare(const ImageBuf &A, const ImageBuf &B, float failthresh, float warnthresh, float failrelative, float warnrelative, ROI roi={}, int nthreads=0)
int64 exint
Definition: SYS_Types.h:125
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1222
GLdouble s
Definition: glad.h:3009
static void clear(UT_StringArray &v)
#define UT_API
Definition: UT_API.h:14
void setCapacity(exint new_capacity)
PUGI__FN void sort(I begin, I end, const Pred &pred)
Definition: pugixml.cpp:7550
**But if you need a result
Definition: thread.h:622
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2138
exint find(const S &s, exint start=0) const
#define SYS_PRAGMA_DISABLE_DEPRECATED()
Definition: SYS_Pragma.h:48
IndexedCompare(const UT_StringArray &values)
void sort(ComparatorBool is_less={})
Sort using std::sort with bool comparator. Defaults to operator<().
Definition: UT_Array.h:467
SYS_FORCE_INLINE const UT_StringHolder & UTmakeUnsafeRef(const UT_StringRef &ref)
Convert a UT_StringRef into a UT_StringHolder that is a shallow reference.
exint capacity() const
Definition: UT_ArrayImpl.h:143
exint sortedInsert(const T &t, Comparator compare)
Definition: UT_ArrayImpl.h:831
void join(const char *sep, UT_String &result) const
#define SYS_PRAGMA_POP_WARN()
Definition: SYS_Pragma.h:35
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1222
UT_StringArray(Parent &&src) noexcept
void sort(ComparatorBool is_less)
Sort string array using custom comparator.
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1602
void stableSortIndices(UT_Array< I > &indices) const
void sortedInsert(const char *str, Compare is_less)
static bool isClear(const UT_StringArray &v)
void join(const char *sep, UT_WorkBuffer &result) const
static void clearConstruct(UT_StringArray *p)
void join(const char *sep, UT_StringHolder &result) const
auto join(It begin, Sentinel end, string_view sep) -> join_view< It, Sentinel >
Definition: format.h:4489
GLenum src
Definition: glcorearb.h:1793
exint sortedFind(const T &t, Comparator compare) const