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 
59  exint find(const UT_StringRef &str, Comparator compare) const
61  UTmakeUnsafeRef(str), compare); }
62 
63  void sort(bool forward, bool numbered);
64 
65  // We specialize the default case explicitly instead of using default
66  // parameters or else someone calling the sort(Comparator) method
67  // can unexpectedly call the wrong sort function if given the wrong
68  // comparison function pointer. This way, we can get a compiler error.
69  void sort() { sort(true, true); }
70 
71  // This method allows arbitrary sorting of the array.
72  void sort(Comparator compare)
74 
75  template <typename Compare>
76  void sort(Compare is_less) { stdsort(is_less); }
77 
78  exint sortedInsert(const char *str,
79  bool forward = true,
80  bool numbered = true);
81 
82  template <typename Compare>
83  void sortedInsert(const char *str, Compare is_less)
84  {
85  UT_StringHolder entry(str);
87  entry, is_less);
88  }
89 
90  exint sortedFind(const char *str,
91  bool forward = true,
92  bool numbered = true) const;
93 
94  exint getLongestPrefixLength() const;
95 
96  // Join the string array elements together, using the passed string
97  // as a separator between elements, and return the result.
98  void join(const char *sep, UT_String &result) const;
99 
100  // Join the string array elements together, using the first passed string
101  // as a separator between all elements except the last pair, in which case
102  // the second separator string is used, and return the result.
103  // (This can be used to create English join strings 'foo, bar and baz')
104  void join(const char *sep, const char *sep_last,
105  UT_String &result) const;
106 
107  // Compute a hash value from the hash for each string in the array.
108  // The hash value is order-independent - that is {"Cf", "Of"} will hash
109  // to the same value as {"Of", "Cf"}.
110  unsigned hash() const;
111 
112  template <typename I>
114  {
115  public:
117  : myValues(values)
118  {}
119  bool operator()(I a, I b) const
120  { return ::strcmp(myValues(a), myValues(b)) < 0; }
121  private:
122  const UT_StringArray &myValues;
123  };
124 
125  template <typename I>
127  {
128  IndexedCompare<I> compare(*this);
129  std::stable_sort(indices.getArray(),
130  indices.getArray() + indices.size(), compare);
131  }
132 };
133 
134 // For UT::ArraySet.
135 namespace UT
136 {
137 template <typename T>
138 struct DefaultClearer;
139 
140 template <>
142 {
143  static void clear(UT_StringArray &v) { v.setCapacity(0); }
144  static bool isClear(const UT_StringArray &v) { return v.capacity() == 0; }
146  {
147  new ((void *)p) UT_StringArray();
148  }
149  static const bool clearNeedsDestruction = false;
150 };
151 }
152 
153 #endif // __UT_StringArray_h__
GLdouble s
Definition: glew.h:1390
exint find(const T &t, exint s=0) const
Definition: UT_ArrayImpl.h:698
bool operator()(I a, I b) 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
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:294
#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:451
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:756
int64 exint
Definition: SYS_Types.h:120
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:448
void sort(Comparator compare)
exint sortedInsert(const T &t, Comparator compare)
Definition: UT_ArrayImpl.h:219
exint find(const UT_StringRef &str, Comparator compare) const
GLdouble GLdouble GLdouble b
Definition: glew.h:9122
GLfloat GLfloat p
Definition: glew.h:16321
#define SYS_NOEXCEPT
Definition: SYS_Compiler.h:55
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:731
UT_StringArray(Parent &&src) SYS_NOEXCEPT
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:619