00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 #ifndef INC_MI32_DOUBLEAR_H
00103 #define INC_MI32_DOUBLEAR_H
00104
00105 #ifndef INC_MI32_MIODEFNS_H
00106 #include <mi32/miodefns.h>
00107 #endif
00108
00109 #ifndef INC_MI32_MEMBUF_H
00110 #include <mi32/membuf.h>
00111 #endif
00112
00113 #ifndef INC_STRING_H
00114 #include <string.h>
00115 #define INC_STRING_H
00116 #endif
00117
00118 #ifndef INC_MI32_MIDBLARY_H
00119 #include <mi32/midblary.h>
00120 #endif
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 template <class _CT> class DOUBLE_ARRAY {
00139
00140 public:
00141
00142
00143 DOUBLE_ARRAY (
00144 ):
00145 m_data(0),
00146 m_numitems(0),
00147 m_spare(0)
00148 { }
00149
00150
00151 DOUBLE_ARRAY (
00152 const DOUBLE_ARRAY<_CT>& rhs
00153 ) :
00154 m_items(rhs.m_items),
00155 m_numitems(rhs.m_numitems),
00156 m_spare(0)
00157 {
00158 m_data = reinterpret_cast<_CT*>(m_items.GetPointer());
00159 }
00160
00161 ~DOUBLE_ARRAY (
00162 ) {}
00163
00164
00165 DOUBLE_ARRAY<_CT>& operator= (
00166 const DOUBLE_ARRAY<_CT>& rhs
00167 ) {
00168 if (this != &rhs) {
00169 m_items = rhs.m_items;
00170 m_numitems = rhs.m_numitems;
00171 m_data = reinterpret_cast<_CT*>(m_items.GetPointer());
00172 }
00173 return (*this);
00174 }
00175
00176
00177 bool operator== (
00178 const DOUBLE_ARRAY<_CT>& rhs
00179 ) const {
00180 if (m_numitems != rhs.m_numitems) return (false);
00181 if (m_numitems == 0) return (true);
00182 return (memcmp(m_data,m_data,m_numitems*sizeof(_CT)) == 0);
00183 }
00184
00185
00186 bool operator!= (
00187 const DOUBLE_ARRAY<_CT>& rhs
00188 ) const {
00189 return (!operator==(rhs));
00190 }
00191
00192
00193 operator const _CT*(
00194 ) const {
00195 return (m_data);
00196 }
00197
00198
00199 operator _CT*(
00200 ) {
00201 return (m_data);
00202 }
00203
00204
00205
00206
00207 const _CT& operator[] (
00208 int index
00209 ) const {
00210 return (m_data[index]);
00211 }
00212
00213
00214
00215
00216 _CT& operator[] (
00217 int index
00218 ) {
00219 return (m_data[index]);
00220 }
00221
00222 #ifndef GENERATING_DOXYGEN_OUTPUT
00223 #ifdef MAC_NATIVE
00224
00225
00226
00227
00228
00229 const _CT& operator[] (
00230 long index
00231 ) const {
00232 return (m_data[index]);
00233 }
00234
00235
00236
00237
00238 _CT& operator[] (
00239 long index
00240 ) {
00241 return (m_data[index]);
00242 }
00243 #endif
00244 #endif
00245
00246
00247 ERRVALUE Append (
00248 const _CT& item
00249 ) {
00250 if (m_numitems >= GetMaxItems()) {
00251 int err;
00252 if ((err = m_items.Reserve(MAX(16,2*m_numitems*sizeof(_CT)), true)) < 0) return (err);
00253 m_data = reinterpret_cast<_CT*>(m_items.GetPointer());
00254 }
00255 m_data[m_numitems++] = item;
00256 return (0);
00257 }
00258
00259
00260
00261 ERRVALUE Assign (
00262 const _CT *items,
00263 int numitems
00264 ) {
00265 ERRVALUE err;
00266 if ((err = Reserve(numitems,true)) < 0) return (err);
00267 memcpy(m_data,items,numitems*sizeof(_CT));
00268 m_numitems = numitems;
00269 return (0);
00270 }
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282 void Attach (
00283 MIDOUBLEARRAY& items,
00284 int numitems
00285 ) {
00286 m_items.Attach(items);
00287 m_numitems = numitems;
00288 m_data = reinterpret_cast<_CT*>(m_items.GetPointer());
00289 return;
00290 }
00291
00292
00293 void Clear (
00294 ) { m_numitems = 0; }
00295
00296
00297 void ClearItems (
00298 int start = 0,
00299 int end = -1
00300 ) {
00301 if (end < start) end = m_numitems - 1;
00302 memset(m_data + start, 0, (end - start + 1) * sizeof(_CT));
00303 return;
00304 }
00305
00306
00307
00308 ERRVALUE Copy (
00309 const DOUBLE_ARRAY<_CT>& rhs
00310 ) {
00311 if (this != &rhs) {
00312 return (SetItems(rhs, rhs.m_numitems));
00313 }
00314 return (0);
00315 }
00316
00317
00318
00319 void DeleteItem (
00320 int index
00321 ) {
00322 if (index >= 0 && index < m_numitems) {
00323 --m_numitems;
00324 if (index < m_numitems) {
00325 memmove(m_data+index,m_data+index+1,(m_numitems-index)*sizeof(_CT));
00326 }
00327 }
00328 return;
00329 }
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 void Detach (
00341 MIDOUBLEARRAY& items
00342 ) {
00343 items.Attach(m_items);
00344 m_numitems = 0;
00345 m_data = 0;
00346 return;
00347 }
00348
00349
00350
00351
00352 void Free (
00353 ) {
00354 m_items.Free();
00355 m_numitems = 0;
00356 m_data = 0;
00357 return;
00358 }
00359
00360
00361 void GetItems (
00362 _CT *items,
00363 int numitems,
00364 int firstitem = 0
00365 ) const {
00366 memcpy(items,m_data+firstitem,MIN(m_numitems-firstitem, numitems)*sizeof(_CT));
00367 return;
00368 }
00369
00370
00371 int GetMaxItems (
00372 ) const {
00373 return (m_items.GetNumReserved() / (sizeof(_CT) / sizeof(double)));
00374 }
00375
00376
00377 int GetNumItems (
00378 ) const { return (m_numitems); }
00379
00380
00381 int GetSizeInBytes (
00382 ) const { return (m_numitems*sizeof(_CT)); }
00383
00384
00385 ERRVALUE Reserve (
00386 int newmaxitems,
00387 bool clear = false,
00388 bool addinc = false
00389 ) {
00390 if (clear) m_numitems = 0;
00391 if (newmaxitems > GetMaxItems()) {
00392 ERRVALUE err;
00393 if ((err = m_items.Reserve(newmaxitems * sizeof(_CT), true)) < 0) return (err);
00394 m_data = reinterpret_cast<_CT*>(m_items.GetPointer());
00395 }
00396 return (0);
00397 }
00398
00399
00400 void ReserveExc (
00401 int newmaxitems,
00402 bool clear = false,
00403 bool addinc = false
00404 ) {
00405 if (clear) m_numitems = 0;
00406 if (newmaxitems > GetMaxItems()) {
00407 m_items.ReserveExc(newmaxitems * sizeof(_CT), true);
00408 m_data = reinterpret_cast<_CT*>(m_items.GetPointer());
00409 }
00410 return;
00411 }
00412
00413
00414
00415
00416 ERRVALUE Resize (
00417 int numitems,
00418 bool keepold = true,
00419 bool clear = false
00420 ) {
00421 int err;
00422 if ((err = Reserve(numitems,!keepold,true)) < 0) return (err);
00423 if (clear && numitems > m_numitems) memset(m_data+m_numitems,0,(numitems-m_numitems)*sizeof(_CT));
00424 m_numitems = numitems;
00425 return (0);
00426 }
00427
00428
00429
00430
00431 void ResizeExc (
00432 int numitems,
00433 bool keepold = true,
00434 bool clear = false
00435 ) {
00436 ReserveExc(numitems,!keepold,true);
00437 if (clear && numitems > m_numitems) memset(m_data+m_numitems,0,(numitems-m_numitems)*sizeof(_CT));
00438 m_numitems = numitems;
00439 return;
00440 }
00441
00442
00443 void Reverse (
00444 ) {
00445 for (int i = 0; (i < m_numitems/2); ++i) {
00446 _CT temp(m_data[i]);
00447 m_data[i] = m_data[m_numitems-i-1];
00448 m_data[m_numitems-i-1] = temp;
00449 }
00450 return;
00451 }
00452
00453
00454
00455 ERRVALUE SetItems (
00456 int firstitem,
00457 const _CT *items,
00458 int numitems
00459 ) {
00460 ERRVALUE err;
00461 if ((err = Reserve(firstitem+numitems,true)) < 0) return (err);
00462 memcpy(m_data+firstitem,items,numitems*sizeof(_CT));
00463 if (m_numitems < firstitem + numitems) m_numitems = firstitem + numitems;
00464 return (0);
00465 }
00466
00467
00468 void Swap (
00469 DOUBLE_ARRAY<_CT>& rhs
00470 ) {
00471 m_items.Swap(rhs.m_items);
00472 _CT *t_data = m_data;
00473 m_data = rhs.m_data;
00474 rhs.m_data = t_data;
00475 int t_numitems = m_numitems;
00476 m_numitems = rhs.m_numitems;
00477 rhs.m_numitems = t_numitems;
00478 return;
00479 }
00480
00481
00482 void SwapBytes (
00483 ) { for (int i = 0;(i < m_numitems);++i) ::SwapBytes(m_data[i]); }
00484
00485
00486 void TransferOwnerFrom (
00487 DOUBLE_ARRAY<_CT>& rhs
00488 ) {
00489 MIDOUBLEARRAY temp;
00490 int numitems = rhs.GetNumItems();
00491 rhs.Detach(temp);
00492 Attach(temp, numitems);
00493 return;
00494 }
00495
00496
00497 template <class _T2> void TransferOwnerFromExt (
00498 DOUBLE_ARRAY<_T2>& rhs
00499 ) {
00500 MIDOUBLEARRAY temp;
00501 int numitems = rhs.GetNumItems();
00502 rhs.Detach(temp);
00503
00504 int T1 = sizeof(_CT) / sizeof(double);
00505 int T2 = sizeof(_T2) / sizeof(double);
00506 numitems = (numitems * T2) / T1;
00507 Attach(temp, numitems);
00508 rhs.Clear();
00509 return;
00510 }
00511
00512 private:
00513 #ifndef GENERATING_DOXYGEN_OUTPUT
00514
00515 MIDOUBLEARRAY m_items;
00516 _CT* m_data;
00517 int m_numitems;
00518 int m_spare;
00519 #endif // GENERATING_DOXYGEN_OUTPUT
00520 };
00521
00522 #endif