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
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 #ifndef INC_MI32_DOUBLEAR_H
00115 #define INC_MI32_DOUBLEAR_H
00116
00117 #ifndef INC_MI32_MIODEFNS_H
00118 #include <mi32/miodefns.h>
00119 #endif
00120
00121 #ifndef INC_MI32_MEMBUF_H
00122 #include <mi32/membuf.h>
00123 #endif
00124
00125 #ifndef INC_STRING_H
00126 #include <string.h>
00127 #define INC_STRING_H
00128 #endif
00129
00130 #ifndef INC_MI32_MIDBLARY_H
00131 #include <mi32/midblary.h>
00132 #endif
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 template <class _CT> class DOUBLE_ARRAY {
00151
00152 public:
00153
00154
00155 DOUBLE_ARRAY (
00156 ):
00157 m_data(0),
00158 m_numitems(0),
00159 m_spare(0)
00160 { }
00161
00162
00163 DOUBLE_ARRAY (
00164 const DOUBLE_ARRAY<_CT>& rhs
00165 ) :
00166 m_items(rhs.m_items),
00167 m_numitems(rhs.m_numitems),
00168 m_spare(0)
00169 {
00170 m_data = reinterpret_cast<_CT*>(m_items.GetPointer());
00171 }
00172
00173 ~DOUBLE_ARRAY (
00174 ) {}
00175
00176
00177 DOUBLE_ARRAY<_CT>& operator= (
00178 const DOUBLE_ARRAY<_CT>& rhs
00179 ) {
00180 if (this != &rhs) {
00181 m_items = rhs.m_items;
00182 m_numitems = rhs.m_numitems;
00183 m_data = reinterpret_cast<_CT*>(m_items.GetPointer());
00184 }
00185 return (*this);
00186 }
00187
00188
00189 bool operator== (
00190 const DOUBLE_ARRAY<_CT>& rhs
00191 ) const {
00192 if (m_numitems != rhs.m_numitems) return (false);
00193 if (m_numitems == 0) return (true);
00194 return (memcmp(m_data,m_data,m_numitems*sizeof(_CT)) == 0);
00195 }
00196
00197
00198 bool operator!= (
00199 const DOUBLE_ARRAY<_CT>& rhs
00200 ) const {
00201 return (!operator==(rhs));
00202 }
00203
00204
00205 operator const _CT*(
00206 ) const {
00207 return (m_data);
00208 }
00209
00210
00211 operator _CT*(
00212 ) {
00213 return (m_data);
00214 }
00215
00216
00217
00218
00219 const _CT& operator[] (
00220 int index
00221 ) const {
00222 return (m_data[index]);
00223 }
00224
00225
00226
00227
00228 _CT& operator[] (
00229 int index
00230 ) {
00231 return (m_data[index]);
00232 }
00233
00234 #ifndef GENERATING_DOXYGEN_OUTPUT
00235 #ifdef MAC_NATIVE
00236
00237
00238
00239
00240
00241 const _CT& operator[] (
00242 long index
00243 ) const {
00244 return (m_data[index]);
00245 }
00246
00247
00248
00249
00250 _CT& operator[] (
00251 long index
00252 ) {
00253 return (m_data[index]);
00254 }
00255 #endif
00256 #endif
00257
00258
00259 ERRVALUE Append (
00260 const _CT& item
00261 ) {
00262 if (m_numitems >= GetMaxItems()) {
00263 int err;
00264 if ((err = m_items.Reserve(MAX(16,2*m_numitems*(sizeof(_CT) / sizeof(double))), true)) < 0) return (err);
00265 m_data = reinterpret_cast<_CT*>(m_items.GetPointer());
00266 }
00267 m_data[m_numitems++] = item;
00268 return (0);
00269 }
00270
00271
00272
00273 ERRVALUE Assign (
00274 const _CT *items,
00275 int numitems
00276 ) {
00277 ERRVALUE err;
00278 if ((err = Reserve(numitems,true)) < 0) return (err);
00279 memcpy(m_data,items,numitems*sizeof(_CT));
00280 m_numitems = numitems;
00281 return (0);
00282 }
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294 void Attach (
00295 MIDOUBLEARRAY& items,
00296 int numitems
00297 ) {
00298 m_items.Attach(items);
00299 m_numitems = numitems;
00300 m_data = reinterpret_cast<_CT*>(m_items.GetPointer());
00301 return;
00302 }
00303
00304
00305 void Clear (
00306 ) { m_numitems = 0; }
00307
00308
00309 void ClearItems (
00310 int start = 0,
00311 int end = -1
00312 ) {
00313 if (end < start) end = m_numitems - 1;
00314 memset(m_data + start, 0, (end - start + 1) * sizeof(_CT));
00315 return;
00316 }
00317
00318
00319
00320 ERRVALUE Copy (
00321 const DOUBLE_ARRAY<_CT>& rhs
00322 ) {
00323 if (this != &rhs) {
00324 return (SetItems(rhs, rhs.m_numitems));
00325 }
00326 return (0);
00327 }
00328
00329
00330
00331 void DeleteItem (
00332 int index
00333 ) {
00334 if (index >= 0 && index < m_numitems) {
00335 --m_numitems;
00336 if (index < m_numitems) {
00337 memmove(m_data+index,m_data+index+1,(m_numitems-index)*sizeof(_CT));
00338 }
00339 }
00340 return;
00341 }
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352 void Detach (
00353 MIDOUBLEARRAY& items
00354 ) {
00355 items.Attach(m_items);
00356 m_numitems = 0;
00357 m_data = 0;
00358 return;
00359 }
00360
00361
00362
00363
00364 void Free (
00365 ) {
00366 m_items.Free();
00367 m_numitems = 0;
00368 m_data = 0;
00369 return;
00370 }
00371
00372
00373 void GetItems (
00374 _CT *items,
00375 int numitems,
00376 int firstitem = 0
00377 ) const {
00378 memcpy(items,m_data+firstitem,MIN(m_numitems-firstitem, numitems)*sizeof(_CT));
00379 return;
00380 }
00381
00382
00383 int GetMaxItems (
00384 ) const {
00385 return (m_items.GetNumReserved() / (sizeof(_CT) / sizeof(double)));
00386 }
00387
00388
00389 int GetNumItems (
00390 ) const { return (m_numitems); }
00391
00392
00393 int GetSizeInBytes (
00394 ) const { return (m_numitems*sizeof(_CT)); }
00395
00396
00397 bool IsEmpty (
00398 ) const { return (m_numitems == 0); }
00399
00400
00401
00402 void RemoveItem (
00403 INT32 item
00404 ) {
00405 if (item < m_numitems && item >= 0) {
00406 --m_numitems;
00407 if (item < m_numitems) { memmove(&m_data[item], &m_data[item+1], (m_numitems - item) * sizeof(_CT)); }
00408 }
00409 return;
00410 }
00411
00412
00413 ERRVALUE Reserve (
00414 int newmaxitems,
00415 bool clear = false,
00416 bool addinc = false
00417 ) {
00418 if (clear) m_numitems = 0;
00419 if (newmaxitems > GetMaxItems()) {
00420 ERRVALUE err;
00421 if ((err = m_items.Reserve((newmaxitems * sizeof(_CT) + sizeof(double) - 1) / sizeof(double), true)) < 0) return (err);
00422 m_data = reinterpret_cast<_CT*>(m_items.GetPointer());
00423 }
00424 return (0);
00425 }
00426
00427
00428 void ReserveExc (
00429 int newmaxitems,
00430 bool clear = false,
00431 bool addinc = false
00432 ) {
00433 if (clear) m_numitems = 0;
00434 if (newmaxitems > GetMaxItems()) {
00435 m_items.ReserveExc((newmaxitems * sizeof(_CT) + sizeof(double) - 1) / sizeof(double), true);
00436 m_data = reinterpret_cast<_CT*>(m_items.GetPointer());
00437 }
00438 return;
00439 }
00440
00441
00442
00443
00444 ERRVALUE Resize (
00445 int numitems,
00446 bool keepold = true,
00447 bool clear = false
00448 ) {
00449 int err;
00450 if ((err = Reserve(numitems,!keepold,true)) < 0) return (err);
00451 if (clear && numitems > m_numitems) memset(m_data+m_numitems,0,(numitems-m_numitems)*sizeof(_CT));
00452 m_numitems = numitems;
00453 return (0);
00454 }
00455
00456
00457
00458
00459 void ResizeExc (
00460 int numitems,
00461 bool keepold = true,
00462 bool clear = false
00463 ) {
00464 ReserveExc(numitems,!keepold,true);
00465 if (clear && numitems > m_numitems) memset(m_data+m_numitems,0,(numitems-m_numitems)*sizeof(_CT));
00466 m_numitems = numitems;
00467 return;
00468 }
00469
00470
00471 void Reverse (
00472 ) {
00473 for (int i = 0; (i < m_numitems/2); ++i) {
00474 _CT temp(m_data[i]);
00475 m_data[i] = m_data[m_numitems-i-1];
00476 m_data[m_numitems-i-1] = temp;
00477 }
00478 return;
00479 }
00480
00481
00482
00483 ERRVALUE SetItems (
00484 int firstitem,
00485 const _CT *items,
00486 int numitems
00487 ) {
00488 ERRVALUE err;
00489 if ((err = Reserve(firstitem+numitems,true)) < 0) return (err);
00490 memcpy(m_data+firstitem,items,numitems*sizeof(_CT));
00491 if (m_numitems < firstitem + numitems) m_numitems = firstitem + numitems;
00492 return (0);
00493 }
00494
00495
00496 void Swap (
00497 DOUBLE_ARRAY<_CT>& rhs
00498 ) {
00499 m_items.Swap(rhs.m_items);
00500 _CT *t_data = m_data;
00501 m_data = rhs.m_data;
00502 rhs.m_data = t_data;
00503 int t_numitems = m_numitems;
00504 m_numitems = rhs.m_numitems;
00505 rhs.m_numitems = t_numitems;
00506 return;
00507 }
00508
00509
00510 void SwapBytes (
00511 ) { for (int i = 0;(i < m_numitems);++i) ::SwapBytes(m_data[i]); }
00512
00513
00514 void TransferOwnerFrom (
00515 DOUBLE_ARRAY<_CT>& rhs
00516 ) {
00517 MIDOUBLEARRAY temp;
00518 int numitems = rhs.GetNumItems();
00519 rhs.Detach(temp);
00520 Attach(temp, numitems);
00521 return;
00522 }
00523
00524
00525 template <class _T2> void TransferOwnerFromExt (
00526 DOUBLE_ARRAY<_T2>& rhs
00527 ) {
00528 MIDOUBLEARRAY temp;
00529 int numitems = rhs.GetNumItems();
00530 rhs.Detach(temp);
00531
00532 int T1 = sizeof(_CT) / sizeof(double);
00533 int T2 = sizeof(_T2) / sizeof(double);
00534 numitems = (numitems * T2) / T1;
00535 Attach(temp, numitems);
00536 rhs.Clear();
00537 return;
00538 }
00539
00540 private:
00541 #ifndef GENERATING_DOXYGEN_OUTPUT
00542
00543 MIDOUBLEARRAY m_items;
00544 _CT* m_data;
00545 int m_numitems;
00546 int m_spare;
00547 #endif // GENERATING_DOXYGEN_OUTPUT
00548 };
00549
00550 #endif