Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

skValist.h

00001 /*
00002   Copyright 1996-2003
00003   Simon Whiteside
00004 
00005   This library is free software; you can redistribute it and/or
00006   modify it under the terms of the GNU Lesser General Public
00007   License as published by the Free Software Foundation; either
00008   version 2 of the License, or (at your option) any later version.
00009 
00010   This library is distributed in the hope that it will be useful,
00011   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013   Lesser General Public License for more details.
00014 
00015   You should have received a copy of the GNU Lesser General Public
00016   License along with this library; if not, write to the Free Software
00017   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019   * $Id: skValist_8h-source.html,v 1.17 2004/12/17 21:31:14 sdw Exp $
00020   */
00021 
00022 #ifndef skVALIST_H
00023 #define skVALIST_H
00024 
00025 
00026 #include "skGeneral.h"
00027 #include "skBoundsException.h"
00028     
00029 
00033 template <class T> class CLASSEXPORT skTVAList 
00034 #ifdef __SYMBIAN32__
00035 : public CBase
00036 #endif
00037 {         
00038  public:
00042   skTVAList();
00043 #ifndef __SYMBIAN32__
00044 
00048   skTVAList(const skTVAList<T>&);
00049 #endif
00050 
00053   virtual ~skTVAList();                                 
00058   skTVAList& operator=(const skTVAList<T>&);
00062   void moveFrom(skTVAList<T>&);
00066   void  clear();                                        
00070   USize entries() const;
00075   void  deleteElt(USize  n);
00080   void  prepend(const T &t);
00086   void  insert(const T &t,USize index);
00091   void  append(const T &t);
00095   void  remove(const T &t);     
00101   T& operator[](USize  n) const;
00106   int index(const T &t) const;
00111   bool  contains(const T &t) const;
00116   void  growTo(USize new_size);
00120   USize getArraySize() const;
00121 #ifdef __SYMBIAN32__
00122 
00127   operator TCleanupItem();
00132   static void Cleanup(TAny * s);
00133 #endif
00134  protected:
00138   int   findElt(const T &t) const;
00143   void  grow();
00148   void createArray();
00152   T*  m_Array;  
00156   USize m_ArraySize;
00160   USize m_Entries;
00161 };        
00162 
00163 #ifdef __gnuc__
00164 #define skTVALIST_PRE template <class T> 
00165 #else
00166 #define skTVALIST_PRE template <class T> 
00167 #endif
00168 
00169 
00170 //-----------------------------------------------------------------
00171 skTVALIST_PRE inline skTVAList<T>::skTVAList()
00172 //-----------------------------------------------------------------
00173   :m_Array(0),m_ArraySize(0),m_Entries(0)
00174 {
00175 }
00176 #ifndef __SYMBIAN32__
00177 //-----------------------------------------------------------------
00178 skTVALIST_PRE inline skTVAList<T>::skTVAList(const skTVAList& l)
00179 //-----------------------------------------------------------------
00180   : m_ArraySize(l.m_ArraySize),m_Entries(l.m_Entries)
00181 {
00182   if(m_ArraySize){
00183     m_Array=skARRAY_NEW(T,m_ArraySize);
00184     for (USize x=0;x<m_Entries;x++)
00185       m_Array[x]=l.m_Array[x];
00186   }else
00187     m_Array = 0;
00188 }
00189 #endif
00190 //-----------------------------------------------------------------
00191 skTVALIST_PRE inline skTVAList<T>::~skTVAList()
00192 //-----------------------------------------------------------------
00193 {
00194   if(m_Array)
00195     delete [] m_Array;
00196 }
00197 //-----------------------------------------------------------------
00198 skTVALIST_PRE inline void skTVAList<T>::createArray()
00199 //-----------------------------------------------------------------
00200 {
00201   if (m_ArraySize==0)
00202     m_ArraySize=1;
00203   if (m_ArraySize)
00204     m_Array=skARRAY_NEW(T,m_ArraySize);
00205 }
00206 //-----------------------------------------------------------------
00207 skTVALIST_PRE inline void skTVAList<T>::clear()
00208 //-----------------------------------------------------------------
00209 {
00210   m_Entries=0;
00211   if(m_Array)
00212     delete [] m_Array;
00213   m_Array=0;
00214   m_ArraySize=0;
00215 }
00216 //-----------------------------------------------------------------
00217 skTVALIST_PRE inline USize skTVAList<T>::entries() const
00218      //-----------------------------------------------------------------
00219 {
00220   return m_Entries;
00221 }
00222 //-----------------------------------------------------------------
00223 skTVALIST_PRE inline USize skTVAList<T>::getArraySize() const
00224 //-----------------------------------------------------------------
00225 {
00226   return m_ArraySize;
00227 }
00228 //-----------------------------------------------------------------
00229 skTVALIST_PRE void skTVAList<T>::deleteElt(USize  n)
00230 //-----------------------------------------------------------------
00231 {
00232   assert(n<m_Entries);
00233   if (n>=m_Entries)
00234 #ifdef EXCEPTIONS_DEFINED
00235     throw skBoundsException("Invalid index to DeleteElt",__FILE__,__LINE__);
00236 #else
00237   ExitSystem();
00238 #endif
00239   else{
00240     for (USize x=n;x<m_Entries-1;x++)
00241       m_Array[x]=m_Array[x+1];
00242     m_Entries--;
00243   }
00244 }
00245 //-----------------------------------------------------------------
00246 skTVALIST_PRE void skTVAList<T>::insert(const T &t,USize index)
00247 //-----------------------------------------------------------------
00248 {
00249   assert(index<=m_Entries);
00250   if (index>m_Entries)
00251 #ifdef EXCEPTIONS_DEFINED
00252     throw skBoundsException("Invalid index to Insert",__FILE__,__LINE__);
00253 #else
00254   ExitSystem();
00255 #endif
00256   if (m_Array==0)
00257     createArray();
00258   if (m_ArraySize==m_Entries)
00259     grow();
00260   if (index<m_Entries){
00261     for (USize x=m_Entries;x>index;x--)
00262       m_Array[x]=m_Array[x-1];
00263   }
00264   m_Array[index]=t;
00265   m_Entries++;
00266 }
00267 //-----------------------------------------------------------------
00268 skTVALIST_PRE void skTVAList<T>::prepend(const T &t)
00269 //-----------------------------------------------------------------
00270 {
00271   if (m_Array==0)
00272     createArray();
00273   if (m_ArraySize==m_Entries)
00274     grow();
00275   m_Entries++;
00276   for (USize x=m_Entries-1;x>=1;x--)
00277     m_Array[x]=m_Array[x-1];
00278   m_Array[0]=t;
00279 }
00280 //-----------------------------------------------------------------
00281 skTVALIST_PRE inline void skTVAList<T>::append(const T &t)
00282 //-----------------------------------------------------------------
00283 {
00284   if (m_Array==0)
00285     createArray();
00286   if (m_ArraySize==m_Entries)
00287     grow();
00288   m_Array[m_Entries]=t;
00289   m_Entries++;
00290 }
00291 //-----------------------------------------------------------------
00292 skTVALIST_PRE void skTVAList<T>::remove(const T &t)
00293 //-----------------------------------------------------------------
00294 {
00295   int index = findElt(t);
00296   assert(index>=0 && "VAList does not contain item"!=0);
00297   if (index>=0){
00298     for (USize x=(USize)index;x<m_Entries-1;x++)
00299       m_Array[x]=m_Array[x+1];
00300 
00301     m_Entries--;
00302   }
00303 }
00304 //-----------------------------------------------------------------
00305 skTVALIST_PRE inline T& skTVAList<T>::operator[](USize  n) const
00306 //-----------------------------------------------------------------
00307 {
00308   assert(n<m_Entries);
00309   if (n>=m_Entries)
00310 #ifdef EXCEPTIONS_DEFINED
00311     throw skBoundsException(skSTR("Invalid index to []"),__FILE__,__LINE__);
00312 #else
00313   ExitSystem();
00314 #endif
00315   return m_Array[n];
00316 }
00317 //-----------------------------------------------------------------
00318 skTVALIST_PRE inline int skTVAList<T>::index(const T &t) const
00319 //-----------------------------------------------------------------
00320 {
00321   return (USize)findElt(t);
00322 }
00323 //-----------------------------------------------------------------
00324 skTVALIST_PRE inline bool skTVAList<T>::contains(const T &t) const
00325 //-----------------------------------------------------------------
00326 {
00327   return (bool)(findElt(t) >= 0);
00328 }
00329 //-----------------------------------------------------------------
00330 skTVALIST_PRE void skTVAList<T>::moveFrom(skTVAList<T>& l)
00331 //-----------------------------------------------------------------
00332 {
00333   if (&l!=this){
00334     clear();
00335     // transfer the other list's contents across to us
00336     m_ArraySize=l.m_ArraySize;
00337     m_Entries=l.m_Entries;
00338     m_Array=l.m_Array;
00339     l.m_ArraySize=0;
00340     l.m_Entries=0;
00341     l.m_Array=0;
00342   }
00343 }
00344 //-----------------------------------------------------------------
00345 skTVALIST_PRE skTVAList<T>& skTVAList<T>::operator=(const skTVAList<T>& l)
00346 //-----------------------------------------------------------------
00347 {
00348   if (&l!=this){
00349     clear();
00350     m_ArraySize=l.m_Entries;
00351     m_Entries=l.m_Entries;
00352     if(m_Entries){
00353       m_Array=skARRAY_NEW(T,m_Entries);
00354       for (USize x=0;x<m_Entries;x++)           
00355         m_Array[x]=l.m_Array[x];
00356     }else{
00357       m_Array=0;        // don't allocate until needed
00358       m_ArraySize=0;
00359     }
00360   }
00361   return *this;
00362 }
00363 // Returns -1 if not found.
00364 //-----------------------------------------------------------------
00365 skTVALIST_PRE inline int skTVAList<T>::findElt(const T &t) const
00366 //-----------------------------------------------------------------
00367 {
00368   for (USize index=0;index<m_Entries;index++){
00369     if (m_Array[index]==t)
00370       return (int)index;                // jump out
00371   }
00372   return -1;
00373 }
00374 //-----------------------------------------------------------------
00375 skTVALIST_PRE void skTVAList<T>::grow()
00376 //-----------------------------------------------------------------
00377 {
00378   m_ArraySize *= 2;     // constant increase in size
00379   T *  new_array=skARRAY_NEW(T,m_ArraySize);
00380   if(m_Array){
00381     for (USize x=0;x<m_Entries;x++)             
00382       new_array[x]=m_Array[x];
00383     delete [] m_Array;
00384   }
00385   m_Array=new_array;
00386 }
00387 //-----------------------------------------------------------------
00388 skTVALIST_PRE void skTVAList<T>::growTo(USize new_size)
00389 //-----------------------------------------------------------------
00390 {
00391   if (new_size>m_ArraySize){
00392     m_ArraySize=new_size;
00393 
00394     T *  new_array=skARRAY_NEW(T,m_ArraySize);
00395     if(m_Array){
00396       for (USize x=0;x<m_Entries;x++)           
00397         new_array[x]=m_Array[x];
00398       delete [] m_Array;
00399     }
00400     m_Array=new_array;
00401   }
00402 }
00403 #ifdef __SYMBIAN32__
00404 //-------------------------------------------------------------------------
00405 skTVALIST_PRE skTVAList<T>::operator TCleanupItem()
00406 //-------------------------------------------------------------------------
00407 // used for leave support of local variable lists
00408 {
00409   return TCleanupItem(Cleanup,this);
00410 }
00411 //-------------------------------------------------------------------------
00412 skTVALIST_PRE void skTVAList<T>::Cleanup(TAny * s)
00413 //-------------------------------------------------------------------------
00414 // called in case there is a leave
00415 {
00416   ((skTVAList<T> *)s)->clear();
00417 }
00418 #endif
00419 
00420 #endif

Generated on Fri Dec 17 20:27:34 2004 for Simkin by doxygen1.3