00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __UT_ValArray_h__
00022 #define __UT_ValArray_h__
00023
00024 #include "UT_API.h"
00025 #include <stdlib.h>
00026 #include <vector>
00027 #include <SYS/SYS_Types.h>
00028 #include "UT_Assert.h"
00029 #include "UT_Algorithm.h"
00030 #include "UT_ArrayHelp.h"
00031 #include "UT_VectorTypes.h"
00032
00033 UT_API extern void UTsetCompareFloatsTolerance( float tol );
00034 UT_API extern int UTcompareFloats(const float *a, const float *b);
00035 UT_API extern int UTcompareInts(const int *a, const int *b);
00036
00037 template <typename T>
00038 class UT_ValArray {
00039 public:
00040 typedef int (*Comparator)(const T *, const T *);
00041
00042 UT_ValArray(const UT_ValArray<T> &a);
00043 UT_ValArray(unsigned int sz, unsigned int count)
00044 {
00045 arr = (sz) ? (T *)calloc(sizeof(T), sz) : 0;
00046 if (sz < count) count = sz;
00047 nbrEntries = count;
00048 arrSize = sz;
00049 }
00050 explicit UT_ValArray(unsigned int sz = 0) : arrSize(sz), nbrEntries(0)
00051 {
00052 arr = (sz) ? (T *)calloc(sizeof(T), sz) : 0;
00053 }
00054 ~UT_ValArray(void);
00055
00056 void swap( UT_ValArray<T> &other );
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069 unsigned int append(void) { return insert(nbrEntries); }
00070 unsigned int append(T t);
00071 void appendMultiple(T t, int count);
00072 unsigned int insert(unsigned index);
00073 unsigned int insert(T t, unsigned index);
00074
00075
00076
00077 unsigned int sortedInsert(T t, Comparator compare );
00078 unsigned int sortedInsert(T t);
00079 unsigned int uniqueSortedInsert(T t, Comparator compare );
00080 unsigned int uniqueSortedInsert(T t);
00081 unsigned int uniqueSortedInsertAscending(T t)
00082 { return uniqueSortedInsert(t); }
00083
00084
00085
00086 int uniqueSortedFind(T item) const;
00087 int uniqueSortedFindAscending(T item) const
00088 { return uniqueSortedFind(item); }
00089
00090
00091
00092
00093
00094
00095
00096 void merge( const UT_ValArray<T> &other, int direction, bool allow_dups );
00097
00098 void sortedUnion( const UT_ValArray<T> &other );
00099 void sortedUnion( const UT_ValArray<T> &other,
00100 UT_ValArray<T> &result ) const;
00101 void sortedIntersection( const UT_ValArray<T> &other );
00102 void sortedIntersection( const UT_ValArray<T> &other,
00103 UT_ValArray<T> &result ) const;
00104 void sortedSetDifference( const UT_ValArray<T> &other );
00105 void sortedSetDifference( const UT_ValArray<T> &other,
00106 UT_ValArray<T> &result ) const;
00107
00108
00109 void sortedRemoveDuplicates();
00110
00111
00112 void fromStdVector(const std::vector<T> &vec);
00113 void toStdVector(std::vector<T> &vec) const;
00114
00115
00116
00117
00118 unsigned int heapPush(T t, Comparator compare );
00119 unsigned int heapPush(T t);
00120
00121
00122 T heapPop(Comparator compare);
00123 T heapPop();
00124
00125
00126 T heapMax() const
00127 {
00128 UT_ASSERT_P(nbrEntries > 0);
00129 return arr[0];
00130 }
00131
00132
00133 unsigned int concat(const UT_ValArray<T> &a);
00134
00135
00136 unsigned int multipleInsert(unsigned int index, unsigned int count);
00137
00138
00139
00140 unsigned int insertAt(T t, unsigned int index);
00141
00142
00143
00144
00145
00146
00147 int findAndRemove(T t);
00148 int removeIndex(unsigned int index)
00149 {
00150 return (index < nbrEntries) ? removeAt(index) : -1;
00151 }
00152 int removeLast()
00153 { return nbrEntries ? removeAt(nbrEntries-1) : -1; }
00154
00155
00156
00157 int shift(unsigned int srcIdx,
00158 unsigned int destIdx,
00159 unsigned int howMany);
00160
00161
00162 void cycle(int howMany);
00163
00164
00165
00166 void constant(T v);
00167 void constant();
00168 void zero() { constant(); }
00169
00170
00171
00172
00173
00174 int find(T t, unsigned int s = 0) const;
00175 int find(T t, Comparator compare) const;
00176
00177 void reverse();
00178
00179
00180
00181 void sort(Comparator compare);
00182 void sortAscending();
00183
00184
00185
00186
00187
00188 T selectNthLargest(int idx);
00189
00190
00191
00192 void resize(unsigned int sz, unsigned short copyFlag=1);
00193 void resizeIfNeeded(uint sz, bool copyFlag=true)
00194 {
00195 if (sz > capacity())
00196 resize(sz, copyFlag);
00197 }
00198
00199
00200
00201 uint capacity(void) const { return arrSize; }
00202 int64 getMemoryUsage() const { return capacity()*sizeof(T); }
00203 uint entries(void) const { return nbrEntries; }
00204 bool isEmpty(void) const { return nbrEntries==0; }
00205
00206
00207 void entries(unsigned int ne) { nbrEntries = ne; }
00208
00209 void truncate(unsigned int ne) { if (entries() > ne) entries(ne); }
00210
00211 void clear() { entries(0); }
00212
00213
00214
00215 UT_ValArray<T> &operator=(const UT_ValArray<T> &a);
00216
00217
00218
00219
00220
00221
00222 bool operator==(const UT_ValArray<T> &a) const;
00223 bool operator!=(const UT_ValArray<T> &a) const;
00224 int isEqual(const UT_ValArray<T> &a, Comparator compare
00225 ) const;
00226
00227
00228
00229 T &operator()(unsigned int i)
00230 {
00231 UT_ASSERT_P(i < nbrEntries);
00232 return arr[i];
00233 }
00234 const T &operator()(unsigned int i) const
00235 {
00236 UT_ASSERT_P(i < nbrEntries);
00237 return arr[i];
00238 }
00239 T &operator[](unsigned int i)
00240 {
00241 if (i >= arrSize ) resize(bumpAlloc(i+1));
00242 if (i >= nbrEntries) nbrEntries = i+1;
00243 return arr[i];
00244 }
00245 const T &operator[](unsigned int i) const
00246 {
00247 UT_ASSERT_P(i < nbrEntries);
00248 return arr[i];
00249 }
00250 T &last()
00251 {
00252 UT_ASSERT_P(nbrEntries);
00253 return arr[nbrEntries-1];
00254 }
00255 T last() const
00256 {
00257 return nbrEntries ? arr[nbrEntries-1] : ((T)0);
00258 }
00259
00260 void copyMemory(const UT_ValArray<T> &from) { *this = from; }
00261
00262
00263
00264
00265
00266 unsigned int apply(int (*applyFct)(T &t, void *d), void *d);
00267
00268 const T *getRawArray(void) const { return arr; }
00269 T *array(void) { return arr; }
00270 void setCapacity(unsigned int sz) { arrSize = sz; }
00271
00272
00273
00274 T *aliasArray(T *newdata)
00275 { T *data = arr; arr = newdata; return data; }
00276
00277
00278 T sum() const
00279 {
00280 T s = 0.0f;
00281 for (int i = 0; i < entries(); i++)
00282 s += arr[i];
00283 return s;
00284 }
00285
00286 void display() const;
00287 private:
00288
00289 T *arr;
00290
00291
00292
00293 unsigned int arrSize;
00294 unsigned int nbrEntries;
00295
00296
00297 int removeAt(unsigned int index);
00298
00299 void heapify(unsigned int index, Comparator compare);
00300 void heapify(unsigned int index);
00301 };
00302
00303
00304 template <typename T, typename S>
00305 void
00306 UTconvertArray(UT_ValArray<T> &dest, const UT_ValArray<S> &src)
00307 {
00308 int n = src.entries();
00309 dest.resize(n);
00310 dest.entries(n);
00311 for (int i = 0; i < n; i++)
00312 dest(i) = T(src(i));
00313 }
00314 template <typename T, typename S>
00315 void
00316 UTconvertArray(UT_ValArray<T> &dest, const S *src, int n)
00317 {
00318 dest.resize(n);
00319 dest.entries(n);
00320 for (int i = 0; i < n; i++)
00321 dest(i) = T(src[i]);
00322 }
00323 template <typename T, typename S>
00324 void
00325 UTconvertArray(T *dest, const UT_ValArray<S> &src)
00326 {
00327
00328 int n = src.entries();
00329 for (int i = 0; i < n; i++)
00330 dest[i] = T(src(i));
00331 }
00332 template <typename T, typename S>
00333 void
00334 UTconvertArray(T *dest, const S *src, int64 n)
00335 {
00336
00337 for (int64 i = 0; i < n; i++)
00338 dest[i] = T(src[i]);
00339 }
00340
00341 #if defined( WIN32 ) || defined( LINUX ) || defined(MBSD) || defined(GAMEOS)
00342 #include "UT_ValArrayImpl.h"
00343 #endif
00344
00345 UT_SWAPPER_TEMPLATE( UT_ValArray );
00346
00347 template <>
00348 UT_API void UT_ValArray<fpreal32>::display() const;
00349 template <>
00350 UT_API void UT_ValArray<fpreal64>::display() const;
00351 template <>
00352 UT_API void UT_ValArray<int32>::display() const;
00353 template <>
00354 UT_API void UT_ValArray<fpreal32>::constant(fpreal32 val);
00355
00356 UT_API extern int UTcompareFloats(const float *a, const float *b);
00357 UT_API extern int UTcompareInts(const int *a, const int *b);
00358 #endif