HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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  * COMMENTS: General purpose reference counting class
9  *
10  */
11 
12 #ifndef __UT_StringArray_h__
13 #define __UT_StringArray_h__
14 
15 #include "UT_API.h"
16 #include "UT_Array.h"
17 #include "UT_String.h"
18 #include "UT_StringHolder.h"
19 #include "UT_Swap.h"
20 
21 #include <iosfwd>
22 #include <string>
23 #include <vector>
24 #include <initializer_list>
25 
26 
27 class UT_OStream;
28 
30 {
31 public:
33  typedef int (*Comparator)(const UT_StringHolder *, const UT_StringHolder *s2);
34 
36  explicit UT_StringArray(const char **src);
38  UT_StringArray(std::initializer_list<UT_StringHolder> il);
40 
41  ~UT_StringArray();
42 
43  void swap(UT_StringArray &other)
44  { myStrings.swap(other.myStrings); }
45 
46  int64 getMemoryUsage(bool inclusive) const
47  {
48  int64 mem = inclusive ? sizeof(*this) : 0;
49  mem += UTarrayDeepMemoryUsage(
50  myStrings, false);
51  return mem;
52  }
53 
54  /// this merges two string arrays into this and leaving other empty
55  void merge( UT_StringArray &other );
56 
57  /// Append 'other' to the end of this array
58  exint concat(const UT_StringArray &other)
59  {
60  return myStrings.concat(other.myStrings);
61  }
62 
63  void clear();
64 
65  /// Append a new string and return its index in the string array.
66  exint append(const UT_StringHolder &str);
67  exint insert(const UT_StringHolder &str, exint idx);
68  void appendMultiple(const UT_StringHolder &str,
69  exint count);
70  exint remove(exint idx);
71  void removeLast();
72 
73  // Note this DOES set the number of entries.
74  void resize(exint nsize);
75  exint entries() const
76  { return myStrings.entries(); }
77  void setSize(exint n);
78  exint size() const
79  { return myStrings.size(); }
80  exint capacity() const
81  { return myStrings.capacity(); }
82  void setCapacity(exint newcapacity);
83  void bumpCapacity(exint mincapacity)
84  { myStrings.bumpCapacity(mincapacity); }
85 
86  // Convert from and to a vector of strings.
87  void fromStdVectorOfStrings(
88  const std::vector<std::string> &vec);
89  void toStdVectorOfStrings(
90  std::vector<std::string> &vec) const;
91 
92  UT_StringArray &operator=(const UT_StringArray &src) = default;
93  UT_StringArray &operator=(UT_StringArray &&src) = default;
94  UT_StringArray &operator+=(const UT_StringArray &src);
95  UT_StringHolder &operator()(exint idx);
96  const UT_StringHolder &operator()(exint idx) const;
98  { return myStrings[idx]; }
99  const UT_StringHolder &operator[](exint idx) const
100  { return myStrings[idx]; }
101  UT_StringHolder &last();
102  const UT_StringHolder &last() const;
103  bool operator==(const UT_StringArray &src) const;
104  bool operator!=(const UT_StringArray &src) const;
105 
106  void reverse()
107  { myStrings.reverse(); }
108 
109  exint find(const UT_StringRef &str, exint s=0) const
110  { return myStrings.find(UTmakeUnsafeRef(str), s); }
111 
112  void sort(bool forward, bool numbered);
113 
114  // We specialize the default case explicitly instead of using default
115  // parameters or else someone calling the sort(Comparator) method
116  // can unexpectedly call the wrong sort function if given the wrong
117  // comparison function pointer. This way, we can get a compiler error.
118  void sort() { sort(true, true); }
119 
120  // This method allows arbitrary sorting of the array.
121  void sort(Comparator compare);
122 
123  template <typename Compare>
124  void sort(Compare is_less)
125  { myStrings.stdsort(is_less); }
126 
127  exint sortedInsert(const char *str,
128  bool forward = true,
129  bool numbered = true);
130  template <typename Compare>
131  void sortedInsert(const char *str, Compare is_less)
132  {
133  UT_StringHolder entry(str);
134  myStrings.sortedInsert(entry, is_less);
135  }
136 
137  exint sortedFind(const char *str,
138  bool forward = true,
139  bool numbered = true) const;
140 
141  void sortedRemoveDuplicates();
142 
143  exint getLongestPrefixLength() const;
144 
145  /// Removes all matching elements from the list, shuffling down and changing
146  /// the size appropriately.
147  /// Returns the number of elements left.
148  template <typename IsEqual>
149  exint removeIf(IsEqual is_equal)
150  { return myStrings.removeIf(is_equal); }
151 
152  // Join the string array elements together, using the passed string
153  // as a separator between elements, and return the result.
154  void join(const char *sep, UT_String &result) const;
155 
156  // Join the string array elements together, using the first passed string
157  // as a separator between all elements except the last pair, in which case
158  // the second separator string is used, and return the result.
159  // (This can be used to create English join strings 'foo, bar and baz')
160  void join(const char *sep, const char *sep_last,
161  UT_String &result) const;
162 
163  // Compute a hash value from the hash for each string in the array.
164  // The hash value is order-independent - that is {"Cf", "Of"} will hash
165  // to the same value as {"Of", "Cf"}.
166  unsigned hash() const;
167 
168  const UT_StringHolder *array() const { return myStrings.array(); }
169  UT_StringHolder *array() { return myStrings.array(); }
170 
173 
174  const_iterator begin() const { return myStrings.begin(); }
175  iterator begin() { return myStrings.begin(); }
176  const_iterator end() const { return myStrings.end(); }
177  iterator end() { return myStrings.end(); }
178 
179  template <typename I>
181  {
182  public:
184  : myValues(values)
185  {}
186  bool operator()(I a, I b) const
187  { return ::strcmp(myValues(a), myValues(b)) < 0; }
188  private:
189  const UT_StringArray &myValues;
190  };
191 
192  template <typename I>
194  {
195  IndexedCompare<I> compare(*this);
196  std::stable_sort(indices.getArray(),
197  indices.getArray() + indices.size(), compare);
198  }
199 
200 private:
201  template<typename OS>
202  friend OS &operator<<(OS &os,
203  const UT_StringArray &d);
204 
205  UT_Array<UT_StringHolder> myStrings;
206 };
207 
209 
210 template<typename OS>
211 inline OS &
212 operator<<(OS &os, const UT_StringArray &d)
213 {
214  os << d.myStrings;
215  return os;
216 }
217 
218 // Overload for custom formatting of a UT_StringArray with UTformat
219 UT_API size_t format(char *buffer, size_t bufsize, const UT_StringArray &v);
220 
221 
222 #endif
bool operator()(I a, I b) const
GLenum GLuint GLsizei bufsize
Definition: glcorearb.h:1817
GLsizei GLenum const void * indices
Definition: glcorearb.h:405
UT_StringHolder & operator[](exint idx)
OS & operator<<(OS &os, const UT_StringArray &d)
UT_Array< UT_StringHolder >::const_iterator const_iterator
void swap(UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &a, UT::ArraySet< Key, MULTI, MAX_LOAD_FACTOR_256, Clearer, Hash, KeyEqual > &b)
Definition: UT_ArraySet.h:1561
void bumpCapacity(exint mincapacity)
const GLdouble * v
Definition: glcorearb.h:836
int64 getMemoryUsage(bool inclusive) const
GLboolean GLboolean GLboolean GLboolean a
Definition: glcorearb.h:1221
#define UT_API
Definition: UT_API.h:12
GLuint buffer
Definition: glcorearb.h:659
exint size() const
Definition: UT_Array.h:444
exint concat(const UT_StringArray &other)
Append 'other' to the end of this array.
exint find(const UT_StringRef &str, exint s=0) const
exint removeIf(IsEqual is_equal)
IndexedCompare(const UT_StringArray &values)
bool operator==(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:137
long long int64
Definition: SYS_Types.h:106
GLdouble n
Definition: glcorearb.h:2007
const UT_StringHolder & operator[](exint idx) const
const_iterator begin() const
int64 exint
Definition: SYS_Types.h:115
exint entries() const
SYS_FORCE_INLINE const UT_StringHolder & UTmakeUnsafeRef(const UT_StringRef &ref)
Convert a UT_StringRef into a UT_StringHolder that is a shallow reference.
iterator begin()
GLenum GLsizei GLsizei GLint * values
Definition: glcorearb.h:1601
UT_SWAPPER_CLASS(UT_StringArray)
UT_Array< UT_StringHolder >::iterator iterator
GLboolean GLboolean GLboolean b
Definition: glcorearb.h:1221
GLint GLsizei count
Definition: glcorearb.h:404
exint size() const
UT_StringHolder value_type
iterator end()
png_voidp png_voidp s2
Definition: png.h:2193
exint capacity() const
typedef int
Definition: png.h:1175
const_iterator end() const
#define SYS_NOEXCEPT
Definition: SYS_Compiler.h:49
void stableSortIndices(UT_Array< I > &indices) const
const UT_StringHolder * array() const
UT_API size_t format(char *buffer, size_t bufsize, const UT_StringArray &v)
void sortedInsert(const char *str, Compare is_less)
bool operator!=(const BaseDimensions< T > &a, const BaseDimensions< Y > &b)
Definition: Dimensions.h:165
UT_StringHolder * array()
void sort(Compare is_less)
T * getArray() const
Definition: UT_Array.h:604
GLenum src
Definition: glcorearb.h:1792