00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __UT_FloatArray_h__
00022 #define __UT_FloatArray_h__
00023
00024 #include "UT_API.h"
00025 #include <stdlib.h>
00026 #include <SYS/SYS_Types.h>
00027 #include "UT_Assert.h"
00028 #include "UT_ArrayHelp.h"
00029
00030 UT_API extern void UTsetCompareFloatsTolerance( float tol );
00031 UT_API extern int UTcompareFloats(const float *a, const float *b);
00032
00033 class UT_API UT_FloatArray {
00034 public:
00035 typedef int (*Comparator)(const float *, const float *);
00036
00037 UT_FloatArray(const UT_FloatArray &a);
00038 UT_FloatArray(unsigned int sz, unsigned int count)
00039 {
00040 arr = (sz) ? (float *)calloc(sizeof(float), sz) : 0;
00041 if (sz < count) count = sz;
00042 nbrEntries = count;
00043 arrSize = sz;
00044 }
00045 explicit UT_FloatArray(unsigned int sz = 0) : arrSize(sz), nbrEntries(0)
00046 {
00047 arr = (sz) ? (float *)calloc(sizeof(float), sz) : 0;
00048 }
00049 ~UT_FloatArray(void);
00050
00051 void swap( UT_FloatArray &other );
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 unsigned int append(void) { return insert(nbrEntries); }
00065 unsigned int append(float t);
00066 unsigned int insert(unsigned index);
00067 unsigned int insert(float t, unsigned index);
00068
00069
00070
00071 unsigned int sortedInsert(float t, Comparator compare );
00072 unsigned int uniqueSortedInsert(float t, Comparator compare );
00073
00074
00075
00076 unsigned int heapPush(float t, Comparator compare );
00077
00078
00079 float heapPop(Comparator compare);
00080
00081
00082 float heapMax() const
00083 {
00084 UT_ASSERT_P(nbrEntries > 0);
00085 return arr[0];
00086 }
00087
00088
00089 unsigned int concat(const UT_FloatArray &a);
00090
00091
00092 unsigned int multipleInsert(unsigned int index, unsigned int count);
00093
00094
00095
00096 unsigned int insertAt(float t, unsigned int index);
00097
00098
00099
00100
00101
00102
00103 int findAndRemove(float t);
00104 int removeIndex(unsigned int index)
00105 {
00106 return (index < nbrEntries) ? removeAt(index) : -1;
00107 }
00108
00109
00110
00111 int shift(unsigned int srcIdx,
00112 unsigned int destIdx,
00113 unsigned int howMany);
00114
00115
00116 void cycle(int howMany);
00117
00118
00119 void constant(float v=0);
00120
00121
00122
00123
00124
00125 int find(float t, unsigned int s = 0) const;
00126 int find(float t, Comparator compare) const;
00127
00128 void reverse();
00129
00130
00131
00132 void sort(Comparator compare);
00133 void sortAscending();
00134
00135
00136
00137
00138
00139 float selectNthLargest(int idx);
00140
00141
00142
00143 void resize(unsigned int sz, unsigned short copyFlag=1);
00144 void resizeIfNeeded(uint sz, bool copyFlag=true)
00145 {
00146 if (sz > capacity())
00147 resize(sz, copyFlag);
00148 }
00149
00150
00151
00152 uint capacity(void) const { return arrSize; }
00153 int64 getMemoryUsage() const { return capacity()*sizeof(float); }
00154 uint entries(void) const { return nbrEntries; }
00155 bool isEmpty(void) const { return nbrEntries==0; }
00156
00157
00158 void entries(unsigned int ne) { nbrEntries = ne; }
00159
00160
00161
00162 UT_FloatArray &operator=(const UT_FloatArray &a);
00163
00164
00165
00166
00167
00168
00169 int operator==(const UT_FloatArray &a) const;
00170 int isEqual(const UT_FloatArray &a, Comparator compare
00171 ) const;
00172
00173
00174
00175 float &operator()(unsigned int i)
00176 {
00177 UT_ASSERT_P(i < nbrEntries);
00178 return arr[i];
00179 }
00180 float operator()(unsigned int i) const
00181 {
00182 UT_ASSERT_P(i < nbrEntries);
00183 return arr[i];
00184 }
00185 float &operator[](unsigned int i)
00186 {
00187 if (i >= arrSize ) resize(bumpAlloc(i+1));
00188 if (i >= nbrEntries) nbrEntries = i+1;
00189 return arr[i];
00190 }
00191 float operator[](unsigned int i) const
00192 {
00193 UT_ASSERT_P(i < nbrEntries);
00194 return (i < nbrEntries) ? arr[i] : 0;
00195 }
00196 float &last()
00197 {
00198 UT_ASSERT_P(nbrEntries);
00199 return arr[nbrEntries-1];
00200 }
00201 float last() const
00202 {
00203 return nbrEntries ? arr[nbrEntries-1] : 0;
00204 }
00205
00206 void copyMemory(const UT_FloatArray &from) { *this = from; }
00207
00208
00209
00210
00211
00212 unsigned int apply(int (*applyFct)(float &t, void *d), void *d);
00213
00214 const float *getRawArray(void) const { return arr; }
00215 float *array(void) { return arr; }
00216 void setCapacity(unsigned int sz) { arrSize = sz; }
00217
00218
00219
00220 float *aliasArray(float *newdata)
00221 { float *data = arr; arr = newdata; return data; }
00222
00223
00224 float sum() const
00225 {
00226 float s = 0.0f;
00227 for (int i = 0; i < entries(); i++)
00228 s += arr[i];
00229 return s;
00230 }
00231
00232 void display() const;
00233 private:
00234
00235 float *arr;
00236
00237
00238
00239 unsigned int arrSize;
00240 unsigned int nbrEntries;
00241
00242
00243 int removeAt(unsigned int index);
00244
00245 void heapify(unsigned int index, Comparator compare);
00246
00247 friend class UT_FloatMatrix;
00248 };
00249
00250 #endif