00001 /* 00002 * PROPRIETARY INFORMATION. This software is proprietary to 00003 * Side Effects Software Inc., and is not to be reproduced, 00004 * transmitted, or disclosed in any way without written permission. 00005 * 00006 * Produced by: 00007 * Cristin Barghiel 00008 * Side Effects Software Inc. 00009 * 123 Front Street West, Suite 1401 00010 * Toronto, Ontario 00011 * Canada M5J 2M2 00012 * 416-504-9876 00013 * 00014 * NAME: Utility Library (C++) 00015 * 00016 * COMMENTS: 00017 * This is a class that is used by UT_PtrArray to do all the work... 00018 * 00019 */ 00020 00021 #ifndef __UT_PtrArrayRaw_h__ 00022 #define __UT_PtrArrayRaw_h__ 00023 00024 #include "UT_API.h" 00025 #include <string.h> 00026 #include <malloc.h> 00027 #include <SYS/SYS_Types.h> 00028 #include "UT_ArrayHelp.h" 00029 #include "UT_Assert.h" 00030 00031 UT_API extern int UTcomparePointers(void *const* a, void *const* b); 00032 00033 class UT_API UT_PtrArrayRaw { 00034 public: 00035 typedef int (*Comparator)(const void *, const void *); 00036 00037 // Trivial constructor and destructor. WARNING: this is really meant to 00038 // be a private class. You should normally instantiate it through its 00039 // friend(s). We made these 2 methods public to avoid compiler warnings. 00040 explicit UT_PtrArrayRaw(unsigned int sz = 0) : arrSize(sz), nbrEntries(0) 00041 { 00042 arr = (sz) ? (void **)calloc(sizeof(void *), sz) : 0; 00043 } 00044 ~UT_PtrArrayRaw(void); 00045 00046 // Copy constructor that uses operator '=' to assign each of a's Things 00047 // to this array. 00048 UT_PtrArrayRaw(const UT_PtrArrayRaw &a); 00049 00050 void swap( UT_PtrArrayRaw &other ); 00051 00052 // Append an element to the current entries and return its index in the 00053 // array, or insert the element at a specified position; if necessary, 00054 // insert() grows the array to accommodate the element. The insert 00055 // methods use the assignment operator '=' to place the Thing into the 00056 // right spot; be aware that '=' works differently on objects and pointers. 00057 // The test for duplicates uses the logical equal operator '=='; as with 00058 // '=', the behaviour of the equality operator on pointers versus objects 00059 // is not the same. 00060 // Use the subscript operators instead of insert() if you are appending 00061 // to the array, or if you don't mind overwriting the element already 00062 // inserted at the given index. 00063 unsigned int append(void) { return insert(nbrEntries); } 00064 unsigned int append(const void *t); 00065 unsigned int append(const void *t, int checkForDup); 00066 unsigned int insert(unsigned index); 00067 unsigned int insert(const void *t, unsigned index); 00068 00069 // Assuming the array is sorted, it inserts item t maintaining the sorted 00070 // state of the array. It returns the index of the inserted item. 00071 unsigned int sortedInsert(const void *t, Comparator compare); 00072 unsigned int uniqueSortedInsert(const void *t, Comparator compare); 00073 00074 bool hasSortedSubset( UT_PtrArrayRaw const& other, 00075 Comparator compare) const; 00076 00077 void sortedIntersection( UT_PtrArrayRaw const& other, 00078 Comparator compare); 00079 void sortedIntersection( UT_PtrArrayRaw const& other, 00080 UT_PtrArrayRaw &result, 00081 Comparator compare) const; 00082 void sortedUnion( UT_PtrArrayRaw const& other, 00083 Comparator compare ); 00084 void sortedUnion( UT_PtrArrayRaw const& other, 00085 UT_PtrArrayRaw &result, 00086 Comparator compare ) const; 00087 00088 // Assuming that the array is sorted without duplicates, it inserts item t 00089 // provided that it does not already exist in the array. 00090 // Returns the index of the inserted item, or the index of the existing 00091 // item if found. 00092 unsigned int uniqueSortedInsert(const void *t); 00093 00094 // Assuming the array is already a heap, it inserts item t maintaining 00095 // the heap. It returns the index of the inserted item. 00096 unsigned int heapPush(const void *t, Comparator compare ); 00097 // Assuming the array is already a heap, extracts the top (maximum) 00098 // element from the heap and returns it. 00099 void *heapPop(Comparator compare); 00100 // Assuming the array is already a heap, return the top (maximum) 00101 // element. 00102 void *heapMax() const 00103 { 00104 UT_ASSERT_P(nbrEntries > 0); 00105 return arr[0]; 00106 } 00107 00108 // Takes another pointer array and concatenate it onto my end 00109 unsigned int concat(const UT_PtrArrayRaw &a); 00110 00111 // Insert an element "count" times at the given index. Return the index. 00112 unsigned int multipleInsert(unsigned int index, unsigned int count); 00113 00114 // An alias for unique element insertion at a certain index. Also used by 00115 // the other insertion methods. 00116 unsigned int insertAt(const void *t, unsigned int index); 00117 00118 // Remove one element from the array given the element itself or its 00119 // position in the list, and fill the gap by shifting the elements down 00120 // by one position. Both methods return the index if successful, 00121 // If dupliCheck is 1, both methods search for all objects that match t or 00122 // arr[index] respectively. 00123 int remove(const void *t); 00124 int remove(unsigned int index) 00125 { 00126 return (index < nbrEntries) ? removeAt(index) : -1; 00127 } 00128 00129 int removeZeros(); 00130 00131 // A little quicker version of remove which avoids the 00132 // unneccessary shift 00133 void *removeLast() 00134 { return nbrEntries ? arr[--nbrEntries] : 0; } 00135 00136 // Sort and remove all duplicate entries 00137 void sortAndRemoveDuplicates(); 00138 00139 // Move howMany objects starting at index srcIndex to destIndex; 00140 // Method returns 0 if OK, -1 if overflow. 00141 int shift(unsigned int srcIdx, 00142 unsigned int destIdx, 00143 unsigned int howMany); 00144 // Move howMany objects starting at index srcIndex to destIndex; 00145 // Unlike shift, this method moves entries instead of overwriting them. 00146 void move(int srcIdx, int destIdx, int howMany); 00147 // Collapse the array to remove all the null pointer entries. 00148 // Note: Indices to elements may change due to shifting 00149 void collapse(); 00150 00151 // Cyclically shifts the entire array by howMany 00152 void cycle(int howMany); 00153 00154 // Reverse the entries in the array 00155 void reverse(); 00156 00157 // Search for t in one of two ways: linearly using the '==' operator, or 00158 // binary using the function specified in the parameter list respectively. 00159 // find() returns the index of the matching element or (int)-1. 00160 // Parameter s indicates the index the search should start at. 00161 int find(const void *t, unsigned int s = 0) const; 00162 int find(const void *t, int (*compare)(const void *, const void *)) const; 00163 00164 // Searches for the given string assuming that our array points to 00165 // strings. 00166 // The advantage is that we avoid all callbacks, can inline code, 00167 // so should be super fast. 00168 int findString(const char *str) const; 00169 00170 // Sort the array using a comparison function that you must provide. t1 and 00171 // t2 are pointers to Thing. 00172 void sort(Comparator compare); 00173 00174 // Resize the array, ie. grow it or shrink it. If copyFlag is 1, the 00175 // function copies the data after reallocating space for the array. 00176 void resize(unsigned int sz, unsigned short copyFlag=1); 00177 00178 // Query the allocated length of the array or the number of elements 00179 // in the array: capacity() >= entries(). 00180 uint capacity(void) const { return arrSize; } 00181 int64 getMemoryUsage() const { return capacity()*sizeof(void *); } 00182 uint entries(void) const { return nbrEntries; } 00183 bool isEmpty(void) const { return nbrEntries==0; } 00184 00185 // Set the number of entries. Normally, you should not have to use it. 00186 void entries(unsigned int ne) { nbrEntries = ne; } 00187 00188 // Assign array a to this array by copying each of a's Things with the 00189 // '=' operator. The size of this array must be greater or equal to the 00190 // entry count of array a. 00191 // WARNING: Will *NOT* reduce nbrEntries, will only increase if necessary 00192 UT_PtrArrayRaw &assignSubset(const UT_PtrArrayRaw &a); 00193 00194 // Assign array a to this array by copying each of a's Things with the 00195 // '=' operator. 00196 UT_PtrArrayRaw &operator=(const UT_PtrArrayRaw &a); 00197 00198 // Compare two array and return 1 if they are equal and 0 otherwise. 00199 // Two Things are checked against each other using operator '==' or 00200 // compare() respectively. Arrays of different size but equal number of 00201 // entries are checked for member-wise equality. 00202 // t1 and t2 must point to Things. 00203 int operator==(const UT_PtrArrayRaw &a) const; 00204 int isEqual(const UT_PtrArrayRaw &a, 00205 int (*compare)(const void *t1, const void *t2) 00206 ) const; 00207 00208 // Subscript operators. operator() does NOT do any bound checking, while 00209 // the non-const operator[] grows the array to accommodate the given index. 00210 void *&operator()(unsigned int i) 00211 { 00212 UT_ASSERT_P( i < nbrEntries ); 00213 return arr[i]; 00214 } 00215 void *operator()(unsigned int i) const 00216 { 00217 UT_ASSERT_P( i < nbrEntries ); 00218 return arr[i]; 00219 } 00220 void *&operator[](unsigned int i) 00221 { 00222 if (i >= arrSize ) resize(bumpAlloc(i+1)); 00223 if (i >= nbrEntries) nbrEntries = i+1; 00224 return arr[i]; 00225 } 00226 void *operator[](unsigned int i) const 00227 { 00228 UT_ASSERT_P( i < nbrEntries ); 00229 return (i < nbrEntries) ? arr[i] : (void *)0; 00230 } 00231 00232 // Apply a user-defined function to each element of the array 00233 // as int as the function returns zero. If applyFct returns 00234 // 1, apply() stops traversing the list and returns the current 00235 // index; otherwise, apply() returns the number of entries. 00236 unsigned int apply(int (*applyFct)(void *t, void *d), void *d); 00237 00238 00239 // Derived class should use these three methods to get a handle to the 00240 // internal data, and set the array size. 00241 void *array(void) const { return arr; } 00242 void setCapacity(unsigned int sz) { arrSize = sz; } 00243 00244 // Pointer to a list of elements of type Thing: 00245 void **arr; 00246 00247 // The number of elements we have allocated space for versus the actual 00248 // number of entries in the array: 00249 unsigned int arrSize; 00250 unsigned int nbrEntries; 00251 00252 // The guts of the remove() methods. 00253 int removeAt(unsigned int index); 00254 // The guts of the popHeap() method. 00255 void heapify(unsigned int index, Comparator compare); 00256 }; 00257 00258 #endif
1.5.9