home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / PowerPlant / Memory Allocator / Templates / dynarray.t < prev   
Encoding:
Text File  |  1995-10-24  |  3.2 KB  |  116 lines  |  [TEXT/CWIE]

  1. //    ----------------------------------------------------------------------------
  2. //    dynarray.t
  3. //    ----------------------------------------------------------------------------
  4. //    By Jean-François Brouillet, Copyright © 1995
  5. //
  6. //    V0.000        951023    JFB        Creation
  7. //
  8. //    No restriction on use. But please, use with caution as this is not
  9. //    thoroughly tested. 
  10.  
  11. #ifndef        __DYNARRAY__
  12. #define        __DYNARRAY__
  13.  
  14. #include    <new>
  15. #include    <string.h>
  16. #include    <exception>
  17.  
  18. //    ----------------------------------------------------------------------------
  19. //    template dynarray
  20. //    ----------------------------------------------------------------------------
  21. template <class T>
  22. class    dynarray
  23. {
  24. public:
  25. enum
  26. {
  27.     kDefaultExtraSlots    =    16
  28. };
  29. //    ----------------------------------------------------------------------------
  30. //    dynarray::dynarray
  31. //    ----------------------------------------------------------------------------
  32.                             dynarray(    const size_t inExtraSlots = kDefaultExtraSlots)
  33.                                 :    m_Array(0)
  34.                                 ,    m_NumElems(0)
  35.                                 ,    m_FreeSlots(0)
  36.                                 ,    m_MaxFreeSlots(0)
  37.                             {
  38.                                 m_Array = new T [inExtraSlots];
  39.  
  40.                                 if (m_Array)
  41.                                 {
  42.                                     m_NumElems = 0;
  43.                                     m_FreeSlots = inExtraSlots;
  44.                                     m_MaxFreeSlots = inExtraSlots;
  45.                                 }
  46.                                 else
  47.                                     throw xalloc();
  48.                             };
  49.  
  50. //    ----------------------------------------------------------------------------
  51. //    dynarray::~dynarray
  52. //    ----------------------------------------------------------------------------
  53.                             ~dynarray()
  54.                             {
  55.                                 delete [] m_Array;
  56.                             };
  57.  
  58. //    ----------------------------------------------------------------------------
  59. //    dynarray::resize
  60. //    ----------------------------------------------------------------------------
  61. inline    void                resize(const size_t inNewElementCount)
  62.                             {
  63.                                 if (inNewElementCount > m_NumElems + m_FreeSlots)
  64.                                 {
  65.                                     T *    newArray = new T [inNewElementCount + m_MaxFreeSlots];
  66.                                     
  67.                                     if (newArray)
  68.                                     {
  69.                                         memcpy(newArray, m_Array, sizeof(T) * m_NumElems);
  70.                                         delete [] m_Array;
  71.                                         m_Array = newArray;
  72.                                         m_NumElems = inNewElementCount;
  73.                                         m_FreeSlots = m_MaxFreeSlots;
  74.                                     }
  75.                                     else
  76.                                         throw xalloc();
  77.                                 }
  78.                                 else if (inNewElementCount > m_NumElems)
  79.                                 {
  80.                                     m_FreeSlots -= inNewElementCount - m_NumElems;
  81.                                     m_NumElems = inNewElementCount;
  82.                                 }
  83.                                 else
  84.                                 {
  85.                                     m_FreeSlots += m_NumElems - inNewElementCount;
  86.                                     m_NumElems = inNewElementCount;
  87.                                 }
  88.                             };
  89.  
  90. //    ----------------------------------------------------------------------------
  91. //    dynarray::operator []
  92. //    ----------------------------------------------------------------------------
  93. inline    T &                    operator [] (const size_t inIndex)
  94.                             {
  95.                                 if (inIndex >= m_NumElems)
  96.                                     throw outofrange();
  97.                                 return m_Array[inIndex];
  98.                             };
  99.  
  100. //    ----------------------------------------------------------------------------
  101. //    dynarray::count
  102. //    ----------------------------------------------------------------------------
  103. inline    size_t                count(void)
  104.                             {
  105.                                 return m_NumElems;
  106.                             };
  107.  
  108. protected:
  109.         T *                    m_Array;
  110.         size_t                m_NumElems;
  111.         size_t                m_FreeSlots;
  112.         size_t                m_MaxFreeSlots;
  113. };
  114.  
  115.  
  116. #endif    /*    __DYNARRAY__    */