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 
55  exint find(const UT_StringRef &str, exint s=0) const
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 #endif // __UT_StringArray_h__
exint find(const T &t, exint s=0) const
Definition: UT_ArrayImpl.h:693
bool operator()(I a, I b) const
GLsizei GLenum const void * indices
Definition: glcorearb.h:405
UT_StringArray(const Parent &src)
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
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
exint size() const
Definition: UT_Array.h:451
IndexedCompare(const UT_StringArray &values)
int64 exint
Definition: SYS_Types.h:116
SYS_FORCE_INLINE const UT_StringHolder & UTmakeUnsafeRef(const UT_StringRef &ref)
Convert a UT_StringRef into a UT_StringHolder that is a shallow reference.
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
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1601
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
#define SYS_NOEXCEPT
Definition: SYS_Compiler.h:55
void stableSortIndices(UT_Array< I > &indices) const
void sort(Comparator compare)
Definition: UT_ArrayImpl.h:726
UT_StringArray(Parent &&src) SYS_NOEXCEPT
void sortedInsert(const char *str, Compare is_less)
#define const
Definition: zconf.h:214
void sort(Compare is_less)
T * getArray() const
Definition: UT_Array.h:611
GLenum src
Definition: glcorearb.h:1792