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 * 00008 * Cristin Barghiel 00009 * Side Effects Software Inc 00010 * 123 Front Street West, Suite 1401 00011 * Toronto, Ontario 00012 * Canada M5J 2M2 00013 * 416-504-9876 00014 * 00015 * NAME: Utility Library (C++) 00016 * 00017 * COMMENTS: 00018 */ 00019 00020 #ifndef __UT_Vector3Array_h__ 00021 #define __UT_Vector3Array_h__ 00022 00023 #include "UT_API.h" 00024 #include <string.h> 00025 #include <malloc.h> 00026 #include "UT_Assert.h" 00027 #include "UT_Vector3.h" 00028 00029 class UT_API UT_Vector3Array { 00030 public: 00031 /// Trivial constructor and destructor: 00032 explicit UT_Vector3Array(unsigned int sz = 0) 00033 : arrSize(sz), nbrEntries(0) 00034 { 00035 arr = (sz) ? (UT_Vector3*)calloc(sizeof(UT_Vector3),sz) : 0; 00036 } 00037 UT_Vector3Array(unsigned int sz, unsigned int count); 00038 00039 virtual ~UT_Vector3Array(void); 00040 00041 /// Copy constructor that uses operator '=' to assign each of a's 00042 /// UT_Vector3 to this array. 00043 UT_Vector3Array(const UT_Vector3Array &a); 00044 00045 /// Append an element to the current entries and return its index in the 00046 /// array, or insert the element at a specified position; if necessary, 00047 /// insert() grows the array to accommodate the element. The insert 00048 /// methods use the assignment operator '=' to place the UT_Vector3 into 00049 /// the right spot; be aware that '=' works differently on objects and 00050 /// pointers. If UT_Vector3 is an object that uses the default '=' 00051 /// operator, the result of insert() will be a shallow copy of t, and 00052 /// not t itself. So, if what you really want to do is create a new 00053 /// object inside the array, first grow the array to a good size, 00054 /// then create your object as follows: 00055 /// UT_Vector3Array<GeoPoint> arr(3); 00056 /// GeoPoint *p = new (&arr[0]) GeoPoint(....); 00057 /// Use the subscript operators instead of insert() if you are appending 00058 /// to the array, or if you don't mind overwriting the element already 00059 /// inserted at the given index. 00060 unsigned int append(void) { return insert(nbrEntries); } 00061 unsigned int append(const UT_Vector3& t); 00062 unsigned int insert(unsigned int idx); 00063 unsigned int insert(const UT_Vector3& t, unsigned int idx); 00064 00065 /// Takes another pointer array and concatenate it onto my end 00066 unsigned int concat(const UT_Vector3Array &a); 00067 00068 /// Insert an element "count" times at the given index. Return the index. 00069 unsigned int multipleInsert(unsigned int idx, unsigned int count); 00070 00071 /// Two more insert() methods that check for duplicates in the array, 00072 /// assuming that the array is sorted. 00073 unsigned int append(const UT_Vector3& t, 00074 int (*compare)(const UT_Vector3 *t1, 00075 const UT_Vector3 *t2) 00076 ); 00077 unsigned int insert(const UT_Vector3& t, 00078 unsigned int index, 00079 int (*compare)(const UT_Vector3 *t1, 00080 const UT_Vector3 *t2) 00081 ); 00082 00083 /// An alias for unique element insertion at a certain index. Also used by 00084 /// the other insertion methods. 00085 unsigned int insertAt(const UT_Vector3 &t, unsigned int idx); 00086 00087 /// Remove one element from the array given the element itself or its 00088 /// position in the list, and fill the gap by shifting the elements down 00089 /// by one position. Both methods return the index if successful, 00090 /// and (int)-1 if the element is not found. The method that takes 00091 /// an index argument does not return a reference to the object for safety 00092 /// purposes: if UT_Vector3 is an object, then when the array is shrunk the 00093 /// memory previously used by UT_Vector3 will be overwritten by the next 00094 /// element. 00095 // @{ 00096 int remove(const UT_Vector3& t); 00097 int remove(unsigned int aindex) 00098 { 00099 return (aindex < nbrEntries) 00100 ? removeAt(aindex) : -1; 00101 } 00102 // @} 00103 00104 /// More remove() methods that search for the object to be removed 00105 /// with a user-defined comparison function. These methods assume the 00106 /// array is sorted. t1 and t2 must be pointers to UT_Vector3. The methods 00107 /// return the last (removed) object index. 00108 int remove(const UT_Vector3& t, 00109 int (*compare)(const UT_Vector3 *t1, 00110 const UT_Vector3 *t2)); 00111 /// Alias for remove (unsigned int index), to be consistent with 00112 /// rest of the UT_xxxArray classes 00113 int removeIndex(unsigned int aindex) 00114 { return remove(aindex); } 00115 /// Move howMany objects starting at index srcIndex to destIndex; 00116 /// Method returns 0 if OK, -1 if overflow. 00117 int shift(unsigned int srcIdx, 00118 unsigned int destIdx, 00119 unsigned int howMany); 00120 /// Performs a cyclical shift of the entries by howMany 00121 void cycle(int howMany); 00122 00123 // Quickly set the array to a single value 00124 void constant(fpreal v=0); 00125 void constant(const UT_Vector3 &v); 00126 00127 /// Search for t in one of two ways: linearly using the '==' operator, or 00128 /// binary using the function specified in the parameter list respectively. 00129 /// find() returns the index of the matching element or (int)-1. 00130 /// Parameter s indicates the index to start the search at. 00131 int find(const UT_Vector3 &t, unsigned int s = 0) const; 00132 int find(const UT_Vector3 &t, 00133 int (*compare)(const UT_Vector3 *t1, 00134 const UT_Vector3 *t2), 00135 unsigned int s = 0) const; 00136 00137 /// Search for 'any' until compare() returns 0 and return the index; 00138 /// otherwise return -1. The array is assumed to be sorted. 00139 int find(const void *any, 00140 int (*compare)(const UT_Vector3 *t1, 00141 const UT_Vector3 *t2), 00142 unsigned int s = 0) const; 00143 00144 /// The fastest search possible, which does pointer arithmetic to find the 00145 /// index of the element. WARNING: index() does no out-of-bounds checking. 00146 int index(const UT_Vector3 &t) const { return &t - arr; } 00147 int safeIndex(const UT_Vector3 &t) const 00148 { 00149 return (&t >= arr && (&t - arr < (int)nbrEntries)) 00150 ? &t - arr : -1; 00151 } 00152 00153 /// Sort the array using a comparison function that you must provide. t1 00154 /// and t2 are pointers to UT_Vector3. 00155 void sort(int (*compare)(const UT_Vector3 *t1, 00156 const UT_Vector3 *t2)); 00157 00158 /// Resize the array, ie. grow it or shrink it. If copyFlag is 1, the 00159 /// function copies the data after reallocating space for the array. 00160 void resize(unsigned int sz, unsigned short copyFlag=1); 00161 void resizeIfNeeded(uint sz, bool copyFlag=true) 00162 { 00163 if (sz > capacity()) 00164 resize(sz, copyFlag); 00165 } 00166 00167 /// Query the allocated length of the array or the number of elements 00168 /// in the array: capacity() >= entries(). 00169 uint capacity(void) const { return arrSize; } 00170 int64 getMemoryUsage() const { return capacity()*sizeof(UT_Vector3);} 00171 uint entries(void) const { return nbrEntries; } 00172 bool isEmpty(void) const { return nbrEntries==0; } 00173 00174 /// Set the number of entries. Normally, you should not have to use it. 00175 void entries(unsigned int ne) { nbrEntries = ne; } 00176 00177 /// Assign array a to this array by copying each of a's UT_Vector3 with the 00178 /// '=' operator. 00179 UT_Vector3Array &operator=(const UT_Vector3Array &a); 00180 00181 /// Compare two array and return 1 if they are equal and 0 otherwise. 00182 /// Two UT_Vector3 are checked against each other using operator '==' or 00183 /// compare() respectively. Arrays of different size but equal number of 00184 /// entries are checked for member-wise equality. 00185 /// t1 and t2 must point to UT_Vector3. 00186 int operator==(const UT_Vector3Array &a) const; 00187 int isEqual(const UT_Vector3Array &a, 00188 int (*compare)(const UT_Vector3 *t1, 00189 const UT_Vector3 *t2)) const; 00190 00191 /// Subscript operators. operator() does NOT do any bound checking, while 00192 /// the non-const operator[] grows the array to accommodate the given index 00193 UT_Vector3 &operator()(unsigned int i) 00194 { 00195 UT_ASSERT_P(i < nbrEntries); 00196 return arr[i]; 00197 } 00198 const UT_Vector3 &operator()(unsigned int i) const 00199 { 00200 UT_ASSERT_P(i < nbrEntries); 00201 return arr[i]; 00202 } 00203 UT_Vector3 &operator[](unsigned int i); 00204 const UT_Vector3 &operator[](unsigned int i) const 00205 { 00206 UT_ASSERT_P(i < nbrEntries); 00207 return arr[i]; 00208 } 00209 UT_Vector3 &last() 00210 { 00211 UT_ASSERT_P(nbrEntries); 00212 return arr[nbrEntries-1]; 00213 } 00214 UT_Vector3 last() const 00215 { 00216 return nbrEntries ? arr[nbrEntries-1] 00217 : UT_Vector3(); 00218 } 00219 00220 /// Apply a user-defined function to each element of the array 00221 /// as int as the function returns zero. If applyFct returns 00222 /// 1, apply() stops traversing the list and returns the current 00223 /// index; otherwise, apply() returns the number of entries. 00224 unsigned int apply(int (*applyFct)(UT_Vector3 &t, void *d), void *d); 00225 00226 const UT_Vector3 *getRawArray(void) const { return arr; } 00227 UT_Vector3 *array(void) { return arr; } 00228 00229 private: 00230 /// Pointer to a list of elements of type UT_Vector3: 00231 UT_Vector3 *arr; 00232 00233 /// The number of elements we have allocated space for versus the actual 00234 /// number of entries in the array: 00235 unsigned int arrSize; 00236 unsigned int nbrEntries; 00237 00238 /// The guts of the remove() methods. 00239 int removeAt(unsigned int index); 00240 }; 00241 00242 #endif
1.5.9