mi32/simplear.h

Go to the documentation of this file.
00001 /**
00002  * \file simplear.h <mi32/simplear.h>
00003  * \brief Definitions for SIMPLE_ARRAY template
00004  *
00005  * \if NODOC
00006  * $Id: simplear.h_v 1.42 2003/09/22 19:09:40 dwilliss Exp $
00007  *
00008  * $Log: simplear.h_v $
00009  * Revision 1.42  2003/09/22 19:09:40  dwilliss
00010  * Version of array index operator that takes long for SUN64 to avoid gcc
00011  * error
00012  *
00013  * Revision 1.41  2003/09/15 13:49:56  fileserver!dwilliss
00014  * Doxygen
00015  *
00016  * Revision 1.40  2003/07/28 13:41:45  mju
00017  * Remove deprecated methods.
00018  *
00019  * Revision 1.39  2003/07/25 20:58:42  mju
00020  * Add Assign() method, deprecate old SetItems().
00021  * Add SetItems variant which does not shrink array.
00022  *
00023  * Revision 1.38  2003/06/24 21:47:30  scowan
00024  * Compiler optimization error also in remove fast method.
00025  *
00026  * Revision 1.37  2003/06/18 14:27:26  mju
00027  * Fix compiler optimization bug in RemoveItem by decrement numitems before memmove.
00028  *
00029  * Revision 1.36  2003/06/17 21:49:27  mju
00030  * *** empty log message ***
00031  *
00032  * Revision 1.35  2003/04/09 19:12:52  dwilliss
00033  * Don't use _T in templates.  Mac's ctype defines a global _T
00034  *
00035  * Revision 1.34  2002/10/03 16:49:19  mju
00036  * Remove allocincrement stuff and deprecate method to set.
00037  *
00038  * Revision 1.33  2002/09/30 17:32:57  mju
00039  * Remove redundant swapitems methods as is equivalent to Reverse().
00040  *
00041  * Revision 1.32  2002/09/12 14:54:24  scowan
00042  * Added has item method.
00043  *
00044  * Revision 1.31  2002/07/05 21:53:53  scowan
00045  * resize memset in case _CT is an object with deprecated memset stuff.
00046  *
00047  * Revision 1.30  2002/06/20 15:51:51  scowan
00048  * Prevent ABR.
00049  *
00050  * Revision 1.29  2002/04/02 18:24:29  mju
00051  * Add IsEmpty() method.
00052  * Clarify what will be cleared when 'clear' set to true in Resize().
00053  *
00054  * Revision 1.28  2002/03/18 15:19:21  dwilliss
00055  * Need to include stdansic.h if we want to be sure memcpy has a prototype
00056  *
00057  * Revision 1.27  2001/12/18 17:40:31  scowan
00058  * Set allocincrement in constructors.
00059  *
00060  * Revision 1.26  2001/10/30 16:52:50  dwilliss
00061  * Documented what the CompFunc for Sort() is expected to return
00062  *
00063  * Revision 1.25  2001/09/07 17:15:22  scowan
00064  * Change sort compare function to return int.
00065  *
00066  * Revision 1.24  2001/05/24 20:50:34  scowan
00067  * Adjuect some method names.
00068  *
00069  * Revision 1.23  2001/05/24 18:00:09  scowan
00070  * Added multiple helper methods.
00071  *
00072  * Revision 1.22  2001/02/06 17:35:26  mju
00073  * Add ReserveExc and AppendExc methods.
00074  *
00075  * Revision 1.21  2001/01/30 15:01:10  mju
00076  * Add Get/SetItem methods as array/cast operators confused if array type is pointer.
00077  *
00078  * Revision 1.20  2000/12/04 16:31:25  mju
00079  * Fix genitor 'end ignore' tag.
00080  *
00081  * Revision 1.19  2000/10/13 16:45:07  dwilliss
00082  * Added array operater overloads taking long on Mac only to avoid stupid
00083  * ambiguous function overload error
00084  *
00085  * Revision 1.18  2000/08/23 15:53:16  scowan
00086  * Do an bouns check on Clear Items().
00087  *
00088  * Revision 1.17  2000/08/18 14:41:03  scowan
00089  * Removed additional array operators because the other platforms went Huh!?!?
00090  * Stupid VC6.0.
00091  *
00092  * Revision 1.16  2000/08/17 16:01:00  scowan
00093  * Added unsigned int versions of array operator methods, was causeing a
00094  * strange error to occur if an unsigned int was used to access a simple array
00095  * entry.
00096  *
00097  * Revision 1.15  2000/08/09 13:46:54  mju
00098  * Add Append() methods using C-style array and SIMPLE_ARRAY.
00099  *
00100  * Revision 1.14  2000/08/02 21:43:38  mju
00101  * Change Resize() to only clear new entries if keepold 'true'.
00102  *
00103  * Revision 1.13  2000/08/01 14:04:54  dwilliss
00104  * Added a Copy() method.
00105  * Fixed error in methods taking/returning SIMPLE_ARRAY.  Needs to specify
00106  *   the template type.
00107  *
00108  * Revision 1.12  2000/07/27 17:18:14  mju
00109  * Retire SetNumItems().
00110  *
00111  * Revision 1.11  2000/07/27 15:40:36  dwilliss
00112  * Wrote Attach()/Detach() methods
00113  *
00114  * Revision 1.10  2000/07/26 16:08:36  dwilliss
00115  * Fixed compile error in Free()
00116  *
00117  * Revision 1.9  2000/07/21 22:25:36  scowan
00118  * Added Free() method
00119  *
00120  * Revision 1.8  2000/07/18 20:56:57  mju
00121  * Add Resize() and deprecate SetNumItems().
00122  *
00123  * Revision 1.7  2000/06/22 14:52:55  mju
00124  * Fix ClearItems to not clear past ending item.
00125  *
00126  * Revision 1.5  2000/06/21 16:39:24  scowan
00127  * Set items must return a value error fix.
00128  *
00129  * Revision 1.3  2000/06/21 15:23:31  scowan
00130  * Added swap bytes method.
00131  *
00132  * Revision 1.2  2000/05/11 20:33:22  mju
00133  * Change GetSizeInBytes to return required size, not allocated size.
00134  * Add Swap() method.
00135  *
00136  * Revision 1.1  2000/05/10 13:26:15  mju
00137  * Initial revision
00138  *
00139  * \endif
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>    //! For MmAlloc/Realloc/Free defns
00147 #endif
00148 
00149 #ifndef  INC_MI32_STDANSIC_H
00150 #include <mi32/stdansic.h>    //! memcpy()
00151 #endif
00152 
00153 #ifndef  INC_MI32_MEMBUF_H
00154 #include <mi32/membuf.h>      //! For SwapBytes() defn
00155 #endif
00156 
00157 //! Simple array template.
00158 //!
00159 //! This template is designed to contain an array of items which can be allocated
00160 //! using MmAlloc/MmRealloc.  The primary purpose of this template is to replace
00161 //! allocated arrays where direct access to both array elements and the ability
00162 //! to access a pointer to the first element (as for a C array) is required.
00163 //! Do not use this template with classes which require constructors or have
00164 //! virtual methods or which do not have compiler-generated assignment operators.
00165 //!
00166 //! For maximum efficiency, only minimal validation of access to array elements is performed.
00167 //! No initialization of array items is performed by the template.
00168 //!
00169 //! For doubles and objects based on doubles, use DOUBLE_ARRAY<> in mi32/doublear.h
00170 //!
00171 //! Instances of this class are passed to DLLs, so do not add or remove members.
00172 
00173 template <class _CT> class SIMPLE_ARRAY {
00174    public:
00175 
00176       //! Default constructor, creates empty array.
00177       SIMPLE_ARRAY (
00178          ):
00179          m_items(0),
00180          m_numitems(0),
00181          m_numalloc(0),
00182          m_spare(0)
00183          { }
00184 
00185       //! Copy constructor.
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       //! Destructor.
00197       ~SIMPLE_ARRAY (
00198          ) {
00199          if (m_items != 0) free(m_items);
00200          }
00201 
00202       //! Assignment.
00203       SIMPLE_ARRAY<_CT>& operator= (
00204          const SIMPLE_ARRAY<_CT>& rhs
00205          ) {
00206          if (this != &rhs) {
00207             if (m_numalloc < rhs.m_numalloc) {
00208                //! Free the old array instead of using realloc to avoid expensive copy.
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       //! Equality operator.
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       //! Inequality operator.
00229       bool operator!= (
00230          const SIMPLE_ARRAY<_CT>& rhs
00231          ) const {
00232          return (!operator==(rhs));
00233          }
00234 
00235       //! Cast to const array of template type.
00236       operator const _CT*(
00237          ) const {
00238          return (m_items);
00239          }
00240 
00241       //! Cast to array of template type.
00242       operator _CT*(
00243          ) {
00244          return (m_items);
00245          }
00246 
00247       //! Read (const) access to array element.
00248       //!
00249       //! @note Does not verify that index is within valid range.
00250       const _CT& operator[] (
00251          int index
00252          ) const {
00253          return (m_items[index]);
00254          }
00255 
00256       //! Write (non-const) access to array element.
00257       //!
00258       //! @note Does not verify that index is within valid range.
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       // Fixes ambiguity error on Macintosh.
00268       
00269       // Read (const) access to array element.
00270       //
00271       // @note Does not verify that index is within valid range.
00272       const _CT& operator[] (
00273          long index
00274          ) const {
00275          return (m_items[index]);
00276          }
00277 
00278       // Write (non-const) access to array element.
00279       //
00280       // @note Does not verify that index is within valid range.
00281       _CT& operator[] (
00282          long index
00283          ) {
00284          return (m_items[index]);
00285          }
00286    #endif
00287       #endif //!< GENERATING_DOXYGEN_OUTPUT
00288 
00289       //! Append item to end of array, extending if necessary.
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       //! Append items from C array, reserving space if needed.
00302       ERRVALUE Append (
00303          const _CT *items,                //!< Items to append
00304          int numitems                     //!< Number of items to append
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       //! Append items from SIMPLE_ARRAY reserving space if needed.
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       //! Append item to end of array, extending if necessary.
00325       //! Throws exception if out of memory.
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       //! Append items from C array, reserving space if needed.
00337       //! Throws exception if out of memory.
00338       void AppendExc (
00339          const _CT *items,                //!< Items to append
00340          int numitems                     //!< Number of items to append
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       //! Append items from SIMPLE_ARRAY reserving space if needed.
00349       //! Throws exception if out of memory.
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       //! Append item to end of array if unique, extending if necessary.
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       //! Append item to end of array if unique, extending if necessary.
00371       //! Throws exception if out of memory.
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       //! Assign from C array, reserving space if needed.
00384       //! Resizes the array to the specified number of items.
00385       ERRVALUE Assign (
00386          const _CT *items,                //!< Items to copy to array
00387          int numitems                     //!< Number of items to copy
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       //! Attach a buffer to the SIMPLE_ARRAY.
00397       //! After passing a buffer to Attach(), the SIMPLE_ARRAY "<b>owns</b>" the
00398       //! buffer and will free it in its destructor.  
00399       //! @param items Reference to a pointer to the templated data type.
00400       //! The reason it's a reference is that after recording the pointer,
00401       //! Attach() will set <i>your</i> pointer to 0.  This will prevent you
00402       //! from accidently freeing a pointer you no longer own.  If you want
00403       //! it back, call the Detach() method
00404       void Attach (
00405          _CT *& items,                 
00406          int numitems               //!< Number of items in array
00407          ) {
00408          Free();
00409          m_items = items;
00410          m_numitems = m_numalloc = numitems;
00411          items = 0;
00412          return;
00413          }
00414 
00415 
00416       //! Clear the array, does not free resources.
00417       void Clear (
00418          ) {
00419          m_numitems = 0;
00420          return;
00421          }
00422 
00423       //! Clear items in the array
00424       void ClearItems (
00425          int start = 0,
00426          int end = -1                  //!< Default -1, use number of items
00427          ) {
00428          if (end < start) end = m_numitems - 1;
00429          if (end >= start) {     //! Start might be larger than end
00430             memset(m_items + start, 0, (end - start + 1) * sizeof(_CT));
00431             }
00432          return;
00433          }
00434 
00435 
00436       //! Copy the contents of another SIMPLE_ARRAY.
00437       //! Same as assignment, but returns an error code instead of throwing an exception.
00438       ERRVALUE Copy (
00439          const SIMPLE_ARRAY<_CT>& rhs
00440          ) {
00441          if (this != &rhs) {
00442             if (m_numalloc < rhs.m_numalloc) {
00443                //! Free the old array instead of using realloc to avoid expensive copy.
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       //! Detach the buffer from the SIMPLE_ARRAY.
00456       //!
00457       //! This turns ownership of the buffer over to the caller, who is then
00458       //! responsibile for seeing that it gets disposed of.
00459       //! After calling Detach(), the SIMPLE_ARRAY will behave as if it had
00460       //! just been constructed.  In other words, it will be a NULL pointer
00461       //! pointing to 0 items.
00462       //! This means that if you want to know how many items are in the
00463       //! array returned, you'd better find out @{before} you call Detach()!
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       //! Free the array.
00473       void Free (
00474          ) {
00475          m_numalloc = m_numitems = 0;
00476          MmFree(m_items);
00477          return;
00478          }
00479 
00480       //! Get array item, does not verify index range.
00481       const _CT& GetItem (
00482          int index
00483          ) const { return (m_items[index]); }
00484 
00485       //! Get maximum number of items in array.
00486       int GetMaxItems (
00487          ) const { return (m_numalloc); }
00488 
00489       //! Get number of items in array.
00490       int GetNumItems (
00491          ) const { return (m_numitems); }
00492 
00493       //! Get size of array required to hold current number of items in bytes.
00494       int GetSizeInBytes (
00495          ) const { return (m_numitems*sizeof(_CT)); }
00496 
00497       //! Determine if an item exists in the array.
00498       //! Item must have a valid operator==()
00499       //! @return 'True' if item found
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       //! Insert multiple items into the array
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       //! Determine if array is empty.
00521       bool IsEmpty (
00522          ) const { return (GetNumItems() == 0); }
00523 
00524       //! Remove duplicate entries in the list, assume not sorted    
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       //! Remove an item by placing the end item in its place.
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       //! Remove item at specified index, maintaining order.
00551       //! Note that this is inefficient for large arrays.
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       //! Remove items matching the item passed in.  _CT must have a operator==() defined
00563       //! Maintains item order
00564       //! @return 'True' if one or more items was removed
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];      //!< Avoid expensive copy if not needed
00572                num ++;
00573                }
00574             }
00575          bool retval = (m_numitems != num);
00576          m_numitems = num;
00577          return (retval);
00578          }
00579 
00580       //! Reserve space in array for up to specified number of items.
00581       ERRVALUE Reserve (
00582          int newmaxitems,                 //!< New maximum number of items
00583          bool clear = false               //!< Clear the array by resetting number of items to 0
00584          ) {
00585          if (clear) m_numitems = 0;
00586          if (newmaxitems > m_numalloc) {
00587             int err;
00588             if (m_numitems == 0) {
00589                //! Free the old array to avoid expensive copy in reallocation
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       //! Reserve space in array for up to specified number of items.
00599       //! Throws exception if out of memory.
00600       void ReserveExc (
00601          int newmaxitems,                 //!< New maximum number of items
00602          bool clear = false               //!< Clear the array by resetting number of items to 0
00603          ) {
00604          if (clear) m_numitems = 0;
00605          if (newmaxitems > m_numalloc) {
00606             if (m_numitems == 0) {
00607                //! Free the old array to avoid expensive copy in reallocation
00608                MmFree(m_items);
00609                }
00610             MmReallocExc((void**)&m_items,newmaxitems*sizeof(_CT));
00611             m_numalloc = newmaxitems;
00612             }
00613          return;
00614          }
00615 
00616       //! Resize array to specified number of items.
00617       //! If the new size is larger than the previous size and the array is
00618       //! not cleared the new array entries will have indeterminant values.
00619       ERRVALUE Resize (
00620          int numitems,                    //!< New number of items desired
00621          bool keepold = true,             //!< Keep old array entries
00622          bool clear = false               //!< Clear new array items to zeros
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       //! Reverse order of items in array.
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       //! Set single item, does not verify index range.
00643       void SetItem (
00644          int index,                       //!< Item index, must be in range from 0 to NumItems-1
00645          const _CT& item                     //!< Item value to set
00646          ) {
00647          m_items[index] = item;
00648          }
00649 
00650       //! Set array items from C array, expanding if needed.
00651       //! Will not reduce the number of items in the array.
00652       ERRVALUE SetItems (
00653          int firstitem,                   //!< First array item to replace
00654          const _CT *items,                //!< Items to copy to array
00655          int numitems                     //!< Number of items to copy
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       //! Sort the items in the array.
00665       //! CompFunc should return 1 if the first item < the 2nd item
00666       //! Otherwise, return 0.
00667       void Sort (
00668          int (*CompFunc)(_CT*, _CT*, void*),
00669          void *data = 0
00670          ) {
00671          //! Compare function cannot return 'bool', must be 'int'.
00672          heapsort(m_items, m_numitems, sizeof(_CT), reinterpret_cast<int (*)(void*, void*, void*)>(CompFunc), data);
00673          }
00674          
00675       //! Swap (exchange) two SIMPLE_ARRAYs.
00676       //! This exchanges the internal buffers so minimal copying is performed for efficiency.
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       //! Swap bytes in a SIMPLE_ARRAY, assume that there is an overload to swap a _CT
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;                       //!< The actual array
00700       int m_numitems;                     //!< Number of valid items
00701       int m_numalloc;                     //!< Number of items allocated
00702       int m_spare;
00703       #endif // GENERATING_DOXYGEN_OUTPUT
00704 
00705    };
00706 
00707 #endif   //!< INC_MI32_SIMPLEAR_H

Generated on Thu Aug 12 06:19:07 2004 for TNTsdk by doxygen 1.3.4-20031026