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