home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / include / garray.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  6.8 KB  |  232 lines

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19.  
  20. // 
  21. // Warning:  This is a C++ file.  
  22. //
  23. //
  24. // This implements cross platform Growable arrays of Pointers.
  25. //
  26. #ifndef _GARRAY_H_
  27. #define _GARRAY_H_
  28.  
  29. //
  30. //    On Unix (well at least Solaris) we are having troubles with
  31. //    templates, so hey, we won't use them...djw.
  32. //
  33. //    On Mac we are having troubles as well, so add me to the list.
  34. //    Now, why even have templates?...jar
  35. //
  36. #if ! ( defined(XP_WIN16) || defined(XP_UNIX) || defined(XP_MAC) )
  37. #define TEMPLATE_SUPPORT    1
  38. #endif
  39.  
  40.  
  41. class CXP_GrowableArray {
  42. protected:
  43.     void **m_pData;
  44.     int m_iSize;
  45.     int m_iAllocSize;
  46.  
  47.     int NewSize( int iMinSize){
  48.         int iNewSize = MAX( m_iAllocSize,16) ;
  49.         while( iNewSize < iMinSize ){
  50.             iNewSize = iNewSize+iNewSize;
  51.         }
  52.         return iNewSize;
  53.     }
  54.  
  55.     //
  56.     // this is the routine that does the actual work.  Should be in 
  57.     //  its own file.
  58.     //
  59.     void GuaranteeSize(int iSize){ 
  60.         if(m_iAllocSize <= iSize){
  61.             int iNewSize = NewSize( iSize );
  62.             if( m_iAllocSize ){
  63.                 void ** pNewData = new void*[iNewSize];
  64.                 XP_BCOPY( m_pData, pNewData, m_iAllocSize * sizeof(void*) );
  65.                 delete [] m_pData;
  66.                 m_pData = pNewData;
  67.             }
  68.             else{
  69.                 m_pData = new void*[iNewSize];
  70.             }
  71.             m_iAllocSize = iNewSize;
  72.         } 
  73.     }
  74.  
  75. public:
  76.     CXP_GrowableArray(int iStartSize=0): m_pData(0),m_iSize(0),m_iAllocSize(0){ 
  77.         if( iStartSize ){
  78.             GuaranteeSize( iStartSize );
  79.         }
  80.     };
  81.  
  82.     ~CXP_GrowableArray(){ delete [] m_pData; }
  83.  
  84.     int Size(){ return m_iSize; }
  85.  
  86.     void SetSize( int iSize ){ 
  87.         GuaranteeSize( iSize );
  88.         m_iSize = iSize;
  89.     }
  90.  
  91.     void* operator[](int nIndex) const { return m_pData[nIndex]; }
  92.     void*& operator[](int nIndex){ return m_pData[nIndex]; }
  93.  
  94.     int Add(void* newElement){ 
  95.         GuaranteeSize(m_iSize+1);
  96.         m_pData[m_iSize] = newElement;
  97.         /* Return index to last item in list */
  98.         return m_iSize++;
  99.     }
  100.  
  101.     int Delete( int nIndex ){ 
  102.         if( nIndex < m_iSize )
  103.         {
  104.             for( int i = nIndex; i < m_iSize-1; i++ )
  105.             {
  106.                /* Suffle remaining pointers down */
  107.                m_pData[nIndex] = m_pData[nIndex+1];
  108.             }
  109.             m_iSize--;
  110.         }
  111.         /* Return index to last item in list */
  112.         return (m_iSize-1);
  113.     }
  114.  
  115.     int Delete( void* element ){ 
  116.         for( int i = 0; i < m_iSize; i++ )
  117.         {
  118.             if( m_pData[i] == element )
  119.             {
  120.                 return Delete(i);
  121.             }
  122.         }
  123.         return (m_iSize-1);
  124.     }
  125.  
  126.     int Find( void* element ){ 
  127.         for( int i = 0; i < m_iSize; i++ )
  128.         {
  129.             if( m_pData[i] == element )
  130.             {
  131.                 return i;
  132.             }
  133.         }
  134.         return -1;
  135.     }
  136.  
  137.     void Empty(){
  138.         m_iSize = 0;
  139.     }
  140. };
  141.  
  142. class CXP_PtrStack : public CXP_GrowableArray{
  143. public:
  144.     int m_iTop;
  145.     CXP_PtrStack(): m_iTop(-1){}
  146.     Bool IsEmpty(){ return m_iTop == -1; }
  147.     void Push( void* t ){ 
  148.         if( ++m_iTop >= Size() ) {
  149.             Add( t );
  150.         }
  151.         else {
  152.             (*this)[m_iTop] = t;
  153.         }
  154.     }
  155.     void* Top(){ return (*this)[m_iTop]; }
  156.     void* Pop(){ return (*this)[m_iTop--];}
  157.     void Reset(){ m_iTop = -1; }
  158.     int StackSize() { return m_iTop + 1; }
  159. };
  160.  
  161. #ifdef TEMPLATE_SUPPORT
  162.  
  163. template<class PTRTYPE>
  164. class TXP_GrowableArray: public CXP_GrowableArray {
  165. public:
  166.     PTRTYPE operator[](int nIndex) const { return (PTRTYPE)(int32)m_pData[nIndex]; }
  167.     PTRTYPE& operator[](int nIndex){ return *(PTRTYPE*)&m_pData[nIndex]; }
  168.     int Add(PTRTYPE newElement){ return CXP_GrowableArray::Add( (void*) newElement ); }
  169. };
  170.  
  171. #define Declare_GrowableArray(NAME,PTRTYPE) \
  172.     typedef TXP_GrowableArray<PTRTYPE> TXP_GrowableArray_##NAME;
  173.  
  174. #else
  175.  
  176. #define Declare_GrowableArray(NAME,PTRTYPE)                                 \
  177.     class TXP_GrowableArray_##NAME: public CXP_GrowableArray {                          \
  178.     public:                                                                             \
  179.         PTRTYPE operator[](int nIndex) const { return (PTRTYPE)(int32)m_pData[nIndex]; }\
  180.         PTRTYPE& operator[](int nIndex){ return *(PTRTYPE*)&m_pData[nIndex]; }          \
  181.         int Add(PTRTYPE newElement){ return CXP_GrowableArray::Add( (void*) newElement ); } \
  182.     };                                                                                 \
  183.  
  184.  
  185. #endif
  186.  
  187. //
  188. // PtrStack Imlementation
  189. //
  190. #ifdef TEMPLATE_SUPPORT
  191. template<class PTRTYPE>
  192. class TXP_PtrStack : public TXP_GrowableArray<PTRTYPE> {
  193. public:
  194.     int m_iTop;
  195.     TXP_PtrStack(): m_iTop(-1){}
  196.     Bool IsEmpty(){ return m_iTop == -1; }
  197.     void Push( PTRTYPE t ){ 
  198.         if( ++m_iTop >= Size() ) {
  199.             Add( t );
  200.         }
  201.         else {
  202.             (*this)[m_iTop] = t;
  203.         }
  204.     }
  205.     PTRTYPE Top(){ return (*this)[m_iTop]; }
  206.     PTRTYPE Pop(){ return (*this)[m_iTop--];}
  207.     void Reset(){ m_iTop = -1; }
  208.     int StackSize(){ return m_iTop + 1; }
  209. };
  210.  
  211. #define Declare_PtrStack(NAME,PTRTYPE) \
  212.     typedef TXP_PtrStack<PTRTYPE> TXP_PtrStack_##NAME;
  213.  
  214. #else // No template support
  215.  
  216. #define Declare_PtrStack(NAME, PTRTYPE) \
  217.     class TXP_PtrStack_##NAME : public CXP_PtrStack {                   \
  218.     public:                                                             \
  219.         void Push( PTRTYPE t ){ CXP_PtrStack::Push((void*)(int32)t); }  \
  220.         PTRTYPE Top(){ return (PTRTYPE)(int32)CXP_PtrStack::Top(); }    \
  221.         PTRTYPE Pop(){ return (PTRTYPE)(int32)CXP_PtrStack::Pop(); }    \
  222.         PTRTYPE operator[](int nIndex) const { return (PTRTYPE)(int32)m_pData[nIndex]; }\
  223.         PTRTYPE& operator[](int nIndex){ return *(PTRTYPE*)&m_pData[nIndex]; }          \
  224.         int Add(PTRTYPE newElement){ return CXP_GrowableArray::Add( (void*)(int32)newElement ); } \
  225.     };                                                                  \
  226.  
  227.  
  228. #endif
  229.  
  230. #endif
  231.  
  232.