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  void join(const char *sep, UT_WorkBuffer &result) const;
118 
119  // Join the string array elements together, using the first passed string
120  // as a separator between all elements except the last pair, in which case
121  // the second separator string is used, and return the result.
122  // (This can be used to create English join strings 'foo, bar and baz')
123  void join(const char *sep, const char *sep_last,
124  UT_String &result) const;
125  void join(const char *sep, const char *sep_last,
126  UT_WorkBuffer &result) const;
127 
128  // Compute a hash value from the hash for each string in the array.
129  // The hash value is order-independent - that is {"Cf", "Of"} will hash
130  // to the same value as {"Of", "Cf"}.
131  unsigned hash() const;
132 
133  template <typename I>
135  {
136  public:
138  : myValues(values)
139  {}
140  bool operator()(I a, I b) const
141  { return ::strcmp(myValues(a), myValues(b)) < 0; }
142  private:
143  const UT_StringArray &myValues;
144  };
145 
146  template <typename I>
148  {
149  IndexedCompare<I> compare(*this);
150  std::stable_sort(indices.getArray(),
151  indices.getArray() + indices.size(), compare);
152  }
153 };
154 
155 // For UT::ArraySet.
156 namespace UT
157 {
158 template <typename T>
159 struct DefaultClearer;
160 
161 template <>
163 {
164  static void clear(UT_StringArray &v) { v.setCapacity(0); }
165  static bool isClear(const UT_StringArray &v) { return v.capacity() == 0; }
167  {
168  new ((void *)p) UT_StringArray();
169  }
170  static const bool clearNeedsDestruction = false;
171 };
172 }
173 
174 #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
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__)
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)
**But if you need a result
Definition: thread.h:613
exint find(const S &s, exint start=0) const
exint size() const
Definition: UT_Array.h:646
#define SYS_PRAGMA_DISABLE_DEPRECATED()
Definition: SYS_Pragma.h:48
IndexedCompare(const UT_StringArray &values)
CompareResults OIIO_API compare(const ImageBuf &A, const ImageBuf &B, float failthresh, float warnthresh, ROI roi={}, int nthreads=0)
void sort(ComparatorBool is_less={})
Sort using std::sort with bool comparator. Defaults to operator<().
Definition: UT_Array.h:456
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:819
#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)
static void clearConstruct(UT_StringArray *p)
void sort(I begin, I end, const Pred &pred)
Definition: pugixml.cpp:7334
T * getArray() const
Definition: UT_Array.h:836
auto join(It begin, Sentinel end, string_view sep) -> join_view< It, Sentinel >
Definition: format.h:2559
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2089
GLenum src
Definition: glcorearb.h:1793
exint sortedFind(const T &t, Comparator compare) const