00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __UT_PtrArray_h__
00025 #define __UT_PtrArray_h__
00026
00027 #ifdef WIN32
00028 #pragma warning(disable:4251)
00029 #pragma warning(disable:4275)
00030 #endif
00031
00032 #include "UT_ArrayHelp.h"
00033 #include "UT_PtrArrayRaw.h"
00034 #include "UT_Algorithm.h"
00035 #include "UT_MakePtrConst.h"
00036 #include <algorithm>
00037
00038 template <typename utPtr>
00039 class UT_PtrArray {
00040
00041
00042
00043 public:
00044 typedef int (*Comparator)(const utPtr *, const utPtr *);
00045
00046
00047
00048
00049 explicit UT_PtrArray(unsigned int sz=0) : array(sz) {}
00050 #if defined(SOLARIS)
00051 ~UT_PtrArray() {}
00052 #else
00053 virtual ~UT_PtrArray() {}
00054 #endif
00055
00056
00057
00058 UT_PtrArray(const UT_PtrArray<utPtr> &a) : array(a.array) { }
00059
00060 void swap( UT_PtrArray< utPtr > &other ) { array.swap( other.array ); }
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 unsigned int append(void) { return array.append(); }
00074 unsigned int append(utPtr t)
00075 { return array.append((const void*)t); }
00076 unsigned int append(utPtr t, unsigned short checkForDup)
00077 { return array.append((const void*)t, checkForDup); }
00078
00079 unsigned int insert(unsigned idx) { return array.insert(idx); }
00080 unsigned int insert(utPtr t, unsigned index)
00081 {
00082 return array.insert((const void *)t, index);
00083 }
00084
00085 unsigned int sortedInsert(utPtr t, Comparator compare )
00086 {
00087 return array.sortedInsert((const void *)t,
00088 (ut_ptr_compare_func_t)compare);
00089 }
00090 unsigned int uniqueSortedInsert(utPtr t, Comparator compare )
00091 {
00092 return array.uniqueSortedInsert((const void *)t,
00093 (ut_ptr_compare_func_t)compare);
00094 }
00095 bool hasSortedSubset(
00096 UT_PtrArray<utPtr> const& other,
00097 Comparator compare = (Comparator)&UTcomparePointers
00098 ) const
00099 {
00100 return array.hasSortedSubset(
00101 other.array,
00102 (ut_ptr_compare_func_t)compare);
00103 }
00104 void sortedIntersection(
00105 UT_PtrArray<utPtr> const& other,
00106 Comparator compare = (Comparator)&UTcomparePointers
00107 )
00108 {
00109 array.sortedIntersection(
00110 other.array,
00111 (ut_ptr_compare_func_t)compare);
00112 }
00113 void sortedIntersection(
00114 UT_PtrArray<utPtr> const& other,
00115 UT_PtrArray<utPtr> &result,
00116 Comparator compare = (Comparator)&UTcomparePointers
00117 ) const
00118 {
00119 array.sortedIntersection(
00120 other.array, result.array,
00121 (ut_ptr_compare_func_t)compare);
00122 }
00123 void sortedUnion(
00124 UT_PtrArray<utPtr> const& other,
00125 Comparator compare = (Comparator)&UTcomparePointers
00126 )
00127 {
00128 array.sortedUnion(other.array,
00129 (ut_ptr_compare_func_t)compare);
00130 }
00131 void sortedUnion(
00132 UT_PtrArray<utPtr> const& other,
00133 UT_PtrArray<utPtr> &result,
00134 Comparator compare = (Comparator)&UTcomparePointers
00135 ) const
00136 {
00137 array.sortedUnion(other.array, result.array,
00138 (ut_ptr_compare_func_t)compare);
00139 }
00140
00141
00142
00143
00144
00145
00146
00147
00148 unsigned int uniqueSortedInsert(utPtr t)
00149 {
00150 return array.uniqueSortedInsert((const void *)t);
00151 }
00152
00153
00154
00155 unsigned int heapPush(utPtr t, Comparator compare )
00156 {
00157 return array.heapPush(t,
00158 (ut_ptr_compare_func_t)compare);
00159 }
00160
00161
00162 utPtr heapPop(Comparator compare)
00163 {
00164 return (utPtr)array.heapPop(
00165 (ut_ptr_compare_func_t)compare);
00166 }
00167
00168
00169 utPtr heapMax() const
00170 {
00171 return (utPtr)array.heapMax();
00172 }
00173
00174 unsigned int concat(const UT_PtrArray<utPtr> &a)
00175 {
00176 return array.concat(a.array);
00177 }
00178
00179
00180
00181 unsigned int multipleInsert(unsigned int index, unsigned int count)
00182 {
00183 return array.multipleInsert(index, count);
00184 }
00185
00186
00187
00188 unsigned int insertAt(utPtr t, unsigned int index)
00189 {
00190 return array.insert((const void *)t, index);
00191 }
00192
00193
00194
00195
00196
00197
00198 int remove(utPtr t) { return array.remove((const void*)t); }
00199 int remove(unsigned int index) { return array.remove(index); }
00200 int removeIndex(unsigned int index) { return array.remove(index); }
00201 utPtr removeLast() { return (utPtr)array.removeLast() ; }
00202
00203
00204
00205
00206 int removeZeros() { return array.removeZeros(); }
00207
00208
00209 void sortAndRemoveDuplicates()
00210 { array.sortAndRemoveDuplicates(); }
00211
00212
00213
00214 int shift(unsigned int srcIdx,
00215 unsigned int destIdx,
00216 unsigned int howMany)
00217 {
00218 return array.shift(srcIdx, destIdx, howMany);
00219 }
00220
00221
00222 void move(int srcIdx, int destIdx, int howMany)
00223 {
00224 array.move(srcIdx, destIdx, howMany);
00225 }
00226
00227
00228
00229 void collapse() { array.collapse(); }
00230
00231
00232 void cycle(int howMany) { array.cycle(howMany); }
00233
00234
00235 void reverse() { array.reverse(); }
00236
00237
00238
00239
00240
00241 int find(utPtr t, unsigned int s = 0) const
00242 {
00243 return array.find((const void *)t, s);
00244 }
00245 int find(utPtr t, Comparator compare) const
00246 {
00247 return array.find((const void *)t,
00248 (ut_ptr_compare_func_t)compare);
00249 }
00250
00251
00252
00253
00254
00255 int findString(const char *str) const
00256 {
00257 return array.findString(str);
00258 }
00259
00260
00261
00262
00263 void sort(Comparator compare = (Comparator)&UTcomparePointers)
00264 {
00265 array.sort((ut_ptr_compare_func_t)compare);
00266 }
00267
00268
00269
00270
00271
00272
00273
00274
00275 template<typename ComparatorBool>
00276 void stableSort(ComparatorBool compare)
00277 {
00278
00279
00280 if (entries() < 2)
00281 return;
00282
00283
00284
00285
00286 typedef typename UT_MakePtrConst<utPtr>::type utCPtr;
00287
00288 std::stable_sort((utCPtr *)&array(0),
00289 (utCPtr *)&array(0) + array.entries(),
00290 compare);
00291 }
00292
00293
00294
00295 void resize(unsigned int sz, unsigned short copyFlag=1)
00296 {
00297 array.resize(sz, copyFlag);
00298 }
00299 void resizeIfNeeded(uint sz, bool copyFlag=true)
00300 {
00301 if (sz > capacity())
00302 resize(sz, copyFlag);
00303 }
00304
00305
00306
00307 uint capacity(void) const { return array.arrSize; }
00308 int64 getMemoryUsage() const { return capacity()*sizeof(void*); }
00309 uint entries(void) const { return array.nbrEntries; }
00310 bool isEmpty(void) const { return array.nbrEntries==0; }
00311
00312
00313 void entries(unsigned int ne) { array.nbrEntries = ne; }
00314
00315 void clear() { entries(0); }
00316
00317
00318
00319
00320 UT_PtrArray<utPtr> &operator=(const UT_PtrArray<utPtr> &a)
00321 {
00322 array = a.array;
00323 return *this;
00324 }
00325
00326
00327
00328
00329
00330
00331 int operator==(const UT_PtrArray<utPtr> &a) const
00332 {
00333 return array == a.array;
00334 }
00335 int operator!=(const UT_PtrArray<utPtr> &a) const
00336 {
00337 return !(array == a.array);
00338 }
00339 int isEqual(const UT_PtrArray<utPtr> &a,
00340 int (*compare)(const void *t1, const void *t2)
00341 ) const
00342 {
00343 return array.isEqual(a.array, compare);
00344 }
00345
00346
00347
00348 utPtr &operator()(unsigned int i)
00349 {
00350 return (utPtr &)array(i);
00351 }
00352 utPtr operator()(unsigned int i) const
00353 {
00354 return (utPtr)array(i);
00355 }
00356 utPtr &operator[](unsigned int i)
00357 {
00358 return (utPtr &)array[i];
00359 }
00360 utPtr operator[](unsigned int i) const
00361 {
00362 return (utPtr)array[i];
00363 }
00364
00365 utPtr &last()
00366 {
00367 return (utPtr &)array(array.nbrEntries-1);
00368 }
00369 utPtr last() const
00370 {
00371 return (utPtr)array(array.nbrEntries-1);
00372 }
00373
00374
00375
00376
00377
00378 unsigned int apply(int (*fcn)(utPtr t, void *d), void *d)
00379 {
00380 return array.apply((int (*)(void *, void*))fcn, d);
00381 }
00382
00383 UT_PtrArrayRaw &getRawArray() { return array; }
00384 const UT_PtrArrayRaw &getRawArray() const { return array; }
00385 utPtr *getArray(void) const { return (utPtr *)array.arr; }
00386
00387 protected:
00388
00389
00390 void setSize(unsigned int sz) { array.arrSize = sz; }
00391
00392 private:
00393
00394 UT_PtrArrayRaw array;
00395 };
00396
00397 typedef UT_PtrArray<const char *> UT_StringList;
00398
00399
00400 UT_SWAPPER_TEMPLATE(UT_PtrArray);
00401
00402 #endif