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  void join(const char *sep, UT_WorkBuffer &result) const;
101 
102  // Join the string array elements together, using the first passed string
103  // as a separator between all elements except the last pair, in which case
104  // the second separator string is used, and return the result.
105  // (This can be used to create English join strings 'foo, bar and baz')
106  void join(const char *sep, const char *sep_last,
107  UT_String &result) const;
108  void join(const char *sep, const char *sep_last,
109  UT_WorkBuffer &result) const;
110 
111  // Compute a hash value from the hash for each string in the array.
112  // The hash value is order-independent - that is {"Cf", "Of"} will hash
113  // to the same value as {"Of", "Cf"}.
114  unsigned hash() const;
115 
116  template <typename I>
118  {
119  public:
121  : myValues(values)
122  {}
123  bool operator()(I a, I b) const
124  { return ::strcmp(myValues(a), myValues(b)) < 0; }
125  private:
126  const UT_StringArray &myValues;
127  };
128 
129  template <typename I>
131  {
132  IndexedCompare<I> compare(*this);
133  std::stable_sort(indices.getArray(),
134  indices.getArray() + indices.size(), compare);
135  }
136 };
137 
138 // For UT::ArraySet.
139 namespace UT
140 {
141 template <typename T>
142 struct DefaultClearer;
143 
144 template <>
146 {
147  static void clear(UT_StringArray &v) { v.setCapacity(0); }
148  static bool isClear(const UT_StringArray &v) { return v.capacity() == 0; }
150  {
151  new ((void *)p) UT_StringArray();
152  }
153  static const bool clearNeedsDestruction = false;
154 };
155 }
156 
157 #endif // __UT_StringArray_h__
exint find(const T &t, exint s=0) const
Definition: UT_ArrayImpl.h:729
bool operator()(I a, I b) const
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
exint sortedFind(const UT_StringRef &str, Comparator compare) const
UT_StringArray(const Parent &src)
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:306
#define UT_API
Definition: UT_API.h:14
OIIO_API bool remove(string_view path, std::string &err)
GLenum src
Definition: glcorearb.h:1792
exint size() const
Definition: UT_Array.h:479
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1601
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:790
GLuint64EXT * result
Definition: glew.h:14311
FMT_CONSTEXPR bool find(Ptr first, Ptr last, T value, Ptr &out)
Definition: format.h:2929
const GLdouble * v
Definition: glcorearb.h:836
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
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:476
GLsizei GLenum const void * indices
Definition: glcorearb.h:405
void sort(Comparator compare)
exint sortedInsert(const T &t, Comparator compare)
Definition: UT_ArrayImpl.h:227
GLfloat GLfloat p
Definition: glew.h:16656
UT_StringArray(Parent &&src) noexcept
void stableSortIndices(UT_Array< I > &indices) const
void sort(Comparator compare)
Definition: UT_ArrayImpl.h:763
void sortedInsert(const char *str, Compare is_less)
static bool isClear(const UT_StringArray &v)
#define const
Definition: zconf.h:214
static void clearConstruct(UT_StringArray *p)
GLdouble s
Definition: glew.h:1395
void sort(Compare is_less)
T * getArray() const
Definition: UT_Array.h:649
arg_join< It, Sentinel, char > join(It begin, Sentinel end, string_view sep)
Definition: format.h:3681
exint sortedFind(const T &t, Comparator compare) const
Definition: UT_ArrayImpl.h:740