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