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
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142 #ifndef INC_MI32_SIMPLEAR_H
00143 #define INC_MI32_SIMPLEAR_H
00144
00145 #ifndef INC_MI32_MIODEFNS_H
00146 #include <mi32/miodefns.h>
00147 #endif
00148
00149 #ifndef INC_MI32_STDANSIC_H
00150 #include <mi32/stdansic.h>
00151 #endif
00152
00153 #ifndef INC_MI32_MEMBUF_H
00154 #include <mi32/membuf.h>
00155 #endif
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 template <class _CT> class SIMPLE_ARRAY {
00174 public:
00175
00176
00177 SIMPLE_ARRAY (
00178 ):
00179 m_items(0),
00180 m_numitems(0),
00181 m_numalloc(0),
00182 m_spare(0)
00183 { }
00184
00185
00186 SIMPLE_ARRAY (
00187 const SIMPLE_ARRAY<_CT>& rhs
00188 ) {
00189 MmAllocExc((void**)&m_items,rhs.m_numalloc*sizeof(_CT));
00190 m_numalloc = rhs.m_numalloc;
00191 memcpy(m_items,rhs.m_items,rhs.m_numitems*sizeof(_CT));
00192 m_numitems = rhs.m_numitems;
00193 m_spare = rhs.m_spare;
00194 }
00195
00196
00197 ~SIMPLE_ARRAY (
00198 ) {
00199 if (m_items != 0) free(m_items);
00200 }
00201
00202
00203 SIMPLE_ARRAY<_CT>& operator= (
00204 const SIMPLE_ARRAY<_CT>& rhs
00205 ) {
00206 if (this != &rhs) {
00207 if (m_numalloc < rhs.m_numalloc) {
00208
00209 MmFree(m_items);
00210 MmAllocExc((void**)&m_items,rhs.m_numalloc*sizeof(_CT));
00211 m_numalloc = rhs.m_numalloc;
00212 }
00213 memcpy(m_items,rhs.m_items,rhs.m_numitems*sizeof(_CT));
00214 m_numitems = rhs.m_numitems;
00215 }
00216 return (*this);
00217 }
00218
00219
00220 bool operator== (
00221 const SIMPLE_ARRAY<_CT>& rhs
00222 ) const {
00223 if (m_numitems != rhs.m_numitems) return (false);
00224 if (m_numitems == 0) return (true);
00225 return (memcmp(m_items,rhs.m_items,m_numitems*sizeof(_CT)) == 0);
00226 }
00227
00228
00229 bool operator!= (
00230 const SIMPLE_ARRAY<_CT>& rhs
00231 ) const {
00232 return (!operator==(rhs));
00233 }
00234
00235
00236 operator const _CT*(
00237 ) const {
00238 return (m_items);
00239 }
00240
00241
00242 operator _CT*(
00243 ) {
00244 return (m_items);
00245 }
00246
00247
00248
00249
00250 const _CT& operator[] (
00251 int index
00252 ) const {
00253 return (m_items[index]);
00254 }
00255
00256
00257
00258
00259 _CT& operator[] (
00260 int index
00261 ) {
00262 return (m_items[index]);
00263 }
00264
00265 #ifndef GENERATING_DOXYGEN_OUTPUT
00266 #if defined(MAC_NATIVE) || defined(SUN64)
00267
00268
00269
00270
00271
00272 const _CT& operator[] (
00273 long index
00274 ) const {
00275 return (m_items[index]);
00276 }
00277
00278
00279
00280
00281 _CT& operator[] (
00282 long index
00283 ) {
00284 return (m_items[index]);
00285 }
00286 #endif
00287 #endif
00288
00289
00290 ERRVALUE Append (
00291 const _CT& item
00292 ) {
00293 if (m_numitems >= m_numalloc) {
00294 int err;
00295 if ((err = Reserve(MAX(16,2*m_numitems))) < 0) return (err);
00296 }
00297 m_items[m_numitems++] = item;
00298 return (0);
00299 }
00300
00301
00302 ERRVALUE Append (
00303 const _CT *items,
00304 int numitems
00305 ) {
00306 int err;
00307 if ((err = Reserve(m_numitems+numitems)) < 0) return (err);
00308 memcpy(m_items+m_numitems,items,numitems*sizeof(_CT));
00309 m_numitems += numitems;
00310 return (0);
00311 }
00312
00313
00314 ERRVALUE Append (
00315 const SIMPLE_ARRAY<_CT>& rhs
00316 ) {
00317 int err;
00318 if ((err = Reserve(m_numitems+rhs.m_numitems)) < 0) return (err);
00319 memcpy(m_items+m_numitems,rhs.m_items,rhs.m_numitems*sizeof(_CT));
00320 m_numitems += rhs.m_numitems;
00321 return (0);
00322 }
00323
00324
00325
00326 void AppendExc (
00327 const _CT& item
00328 ) {
00329 if (m_numitems >= m_numalloc) {
00330 ReserveExc(MAX(16,2*m_numitems));
00331 }
00332 m_items[m_numitems++] = item;
00333 return;
00334 }
00335
00336
00337
00338 void AppendExc (
00339 const _CT *items,
00340 int numitems
00341 ) {
00342 ReserveExc(m_numitems+numitems);
00343 memcpy(m_items+m_numitems,items,numitems*sizeof(_CT));
00344 m_numitems += numitems;
00345 return;
00346 }
00347
00348
00349
00350 void AppendExc (
00351 const SIMPLE_ARRAY<_CT>& rhs
00352 ) {
00353 ReserveExc(m_numitems+rhs.m_numitems);
00354 memcpy(m_items+m_numitems,rhs.m_items,rhs.m_numitems*sizeof(_CT));
00355 m_numitems += rhs.m_numitems;
00356 return;
00357 }
00358
00359
00360 ERRVALUE AppendUnique (
00361 const _CT& item
00362 ) {
00363 INT32 j;
00364 for (j = 0;(j < m_numitems);++j) {
00365 if (m_items[j] == item) break;
00366 }
00367 return ((j == m_numitems) ? Append(item) : 0);
00368 }
00369
00370
00371
00372 void AppendUniqueExc (
00373 const _CT& item
00374 ) {
00375 INT32 j;
00376 for (j = 0;(j < m_numitems);++j) {
00377 if (m_items[j] == item) break;
00378 }
00379 if (j == m_numitems) AppendExc(item);
00380 return;
00381 }
00382
00383
00384
00385 ERRVALUE Assign (
00386 const _CT *items,
00387 int numitems
00388 ) {
00389 int err;
00390 if ((err = Reserve(numitems)) < 0) return (err);
00391 memcpy(m_items,items,numitems*sizeof(_CT));
00392 m_numitems = numitems;
00393 return (0);
00394 }
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404 void Attach (
00405 _CT *& items,
00406 int numitems
00407 ) {
00408 Free();
00409 m_items = items;
00410 m_numitems = m_numalloc = numitems;
00411 items = 0;
00412 return;
00413 }
00414
00415
00416
00417 void Clear (
00418 ) {
00419 m_numitems = 0;
00420 return;
00421 }
00422
00423
00424 void ClearItems (
00425 int start = 0,
00426 int end = -1
00427 ) {
00428 if (end < start) end = m_numitems - 1;
00429 if (end >= start) {
00430 memset(m_items + start, 0, (end - start + 1) * sizeof(_CT));
00431 }
00432 return;
00433 }
00434
00435
00436
00437
00438 ERRVALUE Copy (
00439 const SIMPLE_ARRAY<_CT>& rhs
00440 ) {
00441 if (this != &rhs) {
00442 if (m_numalloc < rhs.m_numalloc) {
00443
00444 MmFree(m_items);
00445 ERRVALUE err = MmAlloc((void**)&m_items,rhs.m_numalloc*sizeof(_CT));
00446 if (err < 0) return (err);
00447 m_numalloc = rhs.m_numalloc;
00448 }
00449 memcpy(m_items,rhs.m_items,rhs.m_numitems*sizeof(_CT));
00450 m_numitems = rhs.m_numitems;
00451 }
00452 return (0);
00453 }
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464 _CT* Detach (
00465 ) {
00466 _CT* ret = m_items;
00467 m_numalloc = m_numitems = 0;
00468 m_items = 0;
00469 return (ret);
00470 }
00471
00472
00473 void Free (
00474 ) {
00475 m_numalloc = m_numitems = 0;
00476 MmFree(m_items);
00477 return;
00478 }
00479
00480
00481 const _CT& GetItem (
00482 int index
00483 ) const { return (m_items[index]); }
00484
00485
00486 int GetMaxItems (
00487 ) const { return (m_numalloc); }
00488
00489
00490 int GetNumItems (
00491 ) const { return (m_numitems); }
00492
00493
00494 int GetSizeInBytes (
00495 ) const { return (m_numitems*sizeof(_CT)); }
00496
00497
00498
00499
00500 bool HasItem (
00501 const _CT& item
00502 ) const {
00503 for (INT32 i = 0;(i < m_numitems);++i) {
00504 if (m_items[i] == item) return (true);
00505 }
00506 return (false);
00507 }
00508
00509
00510 void InsertItems (
00511 int start,
00512 int num = 1
00513 ) {
00514 ReserveExc(m_numitems+num);
00515 memmove(&m_items[start + num], &m_items[start], (m_numitems - start) * sizeof(_CT));
00516 m_numitems += num;
00517 return;
00518 }
00519
00520
00521 bool IsEmpty (
00522 ) const { return (GetNumItems() == 0); }
00523
00524
00525 void RemoveDuplicates (
00526 ) {
00527 for (INT32 i = 0;(i < m_numitems);++i) {
00528 for (INT32 k = i+1;(k < m_numitems);++k) {
00529 if (m_items[i] == m_items[k]) {
00530 m_items[k] = m_items[m_numitems-1];
00531 m_numitems --;
00532 k --;
00533 }
00534 }
00535 }
00536 return;
00537 }
00538
00539
00540 void RemoveFast (
00541 INT32 item
00542 ) {
00543 if (item >= 0 && item < m_numitems) {
00544 m_numitems --;
00545 m_items[item] = m_items[m_numitems];
00546 }
00547 return;
00548 }
00549
00550
00551
00552 void RemoveItem (
00553 INT32 item
00554 ) {
00555 if (item < m_numitems && item >= 0) {
00556 --m_numitems;
00557 if (item < m_numitems) { memmove(&m_items[item], &m_items[item+1], (m_numitems - item) * sizeof(_CT)); }
00558 }
00559 return;
00560 }
00561
00562
00563
00564
00565 bool RemoveMatchingItems (
00566 const _CT& item
00567 ) {
00568 INT32 i, num;
00569 for (num = i = 0;(i < m_numitems); ++i) {
00570 if (m_items[i] != item) {
00571 if (num < i) m_items[num] = m_items[i];
00572 num ++;
00573 }
00574 }
00575 bool retval = (m_numitems != num);
00576 m_numitems = num;
00577 return (retval);
00578 }
00579
00580
00581 ERRVALUE Reserve (
00582 int newmaxitems,
00583 bool clear = false
00584 ) {
00585 if (clear) m_numitems = 0;
00586 if (newmaxitems > m_numalloc) {
00587 int err;
00588 if (m_numitems == 0) {
00589
00590 MmFree(m_items);
00591 }
00592 if ((err = MmRealloc((void**)&m_items,newmaxitems*sizeof(_CT))) < 0) return (err);
00593 m_numalloc = newmaxitems;
00594 }
00595 return (0);
00596 }
00597
00598
00599
00600 void ReserveExc (
00601 int newmaxitems,
00602 bool clear = false
00603 ) {
00604 if (clear) m_numitems = 0;
00605 if (newmaxitems > m_numalloc) {
00606 if (m_numitems == 0) {
00607
00608 MmFree(m_items);
00609 }
00610 MmReallocExc((void**)&m_items,newmaxitems*sizeof(_CT));
00611 m_numalloc = newmaxitems;
00612 }
00613 return;
00614 }
00615
00616
00617
00618
00619 ERRVALUE Resize (
00620 int numitems,
00621 bool keepold = true,
00622 bool clear = false
00623 ) {
00624 int err;
00625 if ((err = Reserve(numitems,!keepold)) < 0) return (err);
00626 if (clear && numitems > m_numitems) memset(static_cast<void*>(m_items+m_numitems),0,(numitems-m_numitems)*sizeof(_CT));
00627 m_numitems = numitems;
00628 return (0);
00629 }
00630
00631
00632 void Reverse (
00633 ) {
00634 for (int i = 0; (i < m_numitems/2); ++i) {
00635 _CT temp(m_items[i]);
00636 m_items[i] = m_items[m_numitems-i-1];
00637 m_items[m_numitems-i-1] = temp;
00638 }
00639 return;
00640 }
00641
00642
00643 void SetItem (
00644 int index,
00645 const _CT& item
00646 ) {
00647 m_items[index] = item;
00648 }
00649
00650
00651
00652 ERRVALUE SetItems (
00653 int firstitem,
00654 const _CT *items,
00655 int numitems
00656 ) {
00657 int err;
00658 if ((err = Reserve(firstitem+numitems)) < 0) return (err);
00659 memcpy(m_items+firstitem,items,numitems*sizeof(_CT));
00660 if (m_numitems < firstitem + numitems) m_numitems = firstitem + numitems;
00661 return (0);
00662 }
00663
00664
00665
00666
00667 void Sort (
00668 int (*CompFunc)(_CT*, _CT*, void*),
00669 void *data = 0
00670 ) {
00671
00672 heapsort(m_items, m_numitems, sizeof(_CT), reinterpret_cast<int (*)(void*, void*, void*)>(CompFunc), data);
00673 }
00674
00675
00676
00677 void Swap (
00678 SIMPLE_ARRAY<_CT>& rhs
00679 ) {
00680 _CT *t_items = m_items;
00681 m_items = rhs.m_items;
00682 rhs.m_items = t_items;
00683 int t_numitems = m_numitems;
00684 m_numitems = rhs.m_numitems;
00685 rhs.m_numitems = t_numitems;
00686 int t_numalloc = m_numalloc;
00687 m_numalloc = rhs.m_numalloc;
00688 rhs.m_numalloc = t_numalloc;
00689 }
00690
00691
00692 void SwapBytes (
00693 ) {
00694 for (int i = 0;(i < m_numitems);++i) ::SwapBytes(m_items[i]);
00695 }
00696
00697 private:
00698 #ifndef GENERATING_DOXYGEN_OUTPUT
00699 _CT *m_items;
00700 int m_numitems;
00701 int m_numalloc;
00702 int m_spare;
00703 #endif // GENERATING_DOXYGEN_OUTPUT
00704
00705 };
00706
00707 #endif