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 
22 
23 class UT_API UT_StringArray : public UT_Array<UT_StringHolder>
24 {
25 public:
27 
28  // Inherit constructors
29  using Parent::Parent;
30 
32 
33  explicit UT_StringArray(const char **src);
34 
35  // Allow move/construction from parent type since we're just a wrapper
37  : Parent::UT_Array(src) {}
38 
40  : Parent::UT_Array(std::move(src)) {}
41 
42  // Copy/move constructors are not inherited and need to be specified
44  = default;
45 
47 
48  UT_StringArray &operator=(const UT_StringArray &src) = default;
49  UT_StringArray &operator=(UT_StringArray &&src) = default;
50 
51  UT_StringArray &operator+=(const UT_StringArray &src);
52 
53  exint remove(exint idx);
54 
57  UTmakeUnsafeRef(str), s); }
58 
60  Comparator compare) const
62  UTmakeUnsafeRef(str), compare); }
63 
64  void sort(bool forward, bool numbered);
65 
66  // We specialize the default case explicitly instead of using default
67  // parameters or else someone calling the sort(Comparator) method
68  // can unexpectedly call the wrong sort function if given the wrong
69  // comparison function pointer. This way, we can get a compiler error.
70  void sort() { sort(true, true); }
71 
72  // This method allows arbitrary sorting of the array.
73  void sort(Comparator compare)
75 
76  template <typename Compare>
77  void sort(Compare is_less) { stdsort(is_less); }
78 
79  exint sortedInsert(const char *str,
80  bool forward = true,
81  bool numbered = true);
82 
83  template <typename Compare>
84  void sortedInsert(const char *str, Compare is_less)
85  {
86  UT_StringHolder entry(str);
88  entry, is_less);
89  }
90 
91  exint sortedFind(const char *str,
92  bool forward = true,
93  bool numbered = true) const;
94 
95  exint getLongestPrefixLength() const;
96 
97  // Join the string array elements together, using the passed string
98  // as a separator between elements, and return the result.
99  void join(const char *sep, UT_String &result) const;
100 
101  // Join the string array elements together, using the first passed string
102  // as a separator between all elements except the last pair, in which case
103  // the second separator string is used, and return the result.
104  // (This can be used to create English join strings 'foo, bar and baz')
105  void join(const char *sep, const char *sep_last,
106  UT_String &result) const;
107 
108  // Compute a hash value from the hash for each string in the array.
109  // The hash value is order-independent - that is {"Cf", "Of"} will hash
110  // to the same value as {"Of", "Cf"}.
111  unsigned hash() const;
112 
113  template <typename I>
115  {
116  public:
118  : myValues(values)
119  {}
120  bool operator()(I a, I b) const
121  { return ::strcmp(myValues(a), myValues(b)) < 0; }
122  private:
123  const UT_StringArray &myValues;
124  };
125 
126  template <typename I>
128  {
129  IndexedCompare<I> compare(*this);
130  std::stable_sort(indices.getArray(),
131  indices.getArray() + indices.size(), compare);
132  }
133 };
134 
135 // For UT::ArraySet.
136 namespace UT
137 {
138 template <typename T>
139 struct DefaultClearer;
140 
141 template <>
143 {
144  static void clear(UT_StringArray &v) { v.setCapacity(0); }
145  static bool isClear(const UT_StringArray &v) { return v.capacity() == 0; }
147  {
148  new ((void *)p) UT_StringArray();
149  }
150  static const bool clearNeedsDestruction = false;
151 };
152 }
153 
154 #endif // __UT_StringArray_h__
GLdouble s
Definition: glew.h:1390
exint find(const T &t, exint s=0) const
Definition: UT_ArrayImpl.h:718
bool operator()(I a, I b) const
exint sortedFind(const UT_StringRef &str, Comparator compare) const
GLenum src
Definition: glew.h:2410
UT_StringArray(const Parent &src)
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:9477
GLint GLsizei const GLuint64 * values
Definition: glew.h:3612
int64 exint
Definition: SYS_Types.h:125
static void clear(UT_StringArray &v)
void stdsort(ComparatorBool is_less)
Sort using std::sort. The ComparatorBool uses the less-than semantics.
Definition: UT_Array.h:301
#define UT_API
Definition: UT_API.h:13
const GLdouble * v
Definition: glew.h:1391
OIIO_API bool remove(string_view path, std::string &err)
exint size() const
Definition: UT_Array.h:458
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 setCapacity(exint newcapacity)
Definition: UT_ArrayImpl.h:777
GLuint GLuint GLsizei GLenum const void * indices
Definition: glew.h:1253
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_Array.h:455
void sort(Comparator compare)
exint sortedInsert(const T &t, Comparator compare)
Definition: UT_ArrayImpl.h:220
UT_StringArray(Parent &&src) noexcept
GLdouble GLdouble GLdouble b
Definition: glew.h:9122
GLfloat GLfloat p
Definition: glew.h:16321
void stableSortIndices(UT_Array< I > &indices) const
FMT_CONSTEXPR bool find(Ptr first, Ptr last, T value, Ptr &out)
Definition: format.h:2104
void sort(Comparator compare)
Definition: UT_ArrayImpl.h:752
void sortedInsert(const char *str, Compare is_less)
arg_join< It, char > join(It begin, It end, string_view sep)
Definition: format.h:3326
static bool isClear(const UT_StringArray &v)
GLuint64EXT * result
Definition: glew.h:14007
#define const
Definition: zconf.h:214
static void clearConstruct(UT_StringArray *p)
void sort(Compare is_less)
T * getArray() const
Definition: UT_Array.h:628
exint sortedFind(const T &t, Comparator compare) const
Definition: UT_ArrayImpl.h:729