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