Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

array.h

Go to the documentation of this file.
00001 // OMICRON ENGINE HEADER FILE
00002 //
00003 // --------------------------------------------------------------------------
00004 // Copyright (C) 2001-2002 by Bjoern Paetzel <kolrabi@gmx.de>
00005 //
00006 // This file is part of the Omicron Engine.
00007 //
00008 // The Omicron Engine is free software; you can redistribute it and/or modify
00009 // it under the terms of the  GNU General Public License  as published by the
00010 // Free Software Foundation;  either version  2  of the License,  or (at your
00011 // option) any later version.
00012 //
00013 // The Omicron Engine  is distributed in the hope that it will be useful, but
00014 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00015 // or  FITNESS FOR A PARTICULAR PURPOSE.  See the  GNU General Public License
00016 // for more details.
00017 //
00018 // You should have  received a copy of the  GNU General Public License  along
00019 // with The Omicron Engine;  if not,  write to the  Free Software Foundation,
00020 // Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
00021 //
00022 // --------------------------------------------------------------------------
00023 // Last modified:       $Date: 2002/12/07 19:02:17 $
00024 // By           :       $Author: kolrabi $
00025 // $Id: array.h,v 1.1.1.1 2002/12/07 19:02:17 kolrabi Exp $ 
00026 
00027 /*
00028 
00029   $Log: array.h,v $
00030   Revision 1.1.1.1  2002/12/07 19:02:17  kolrabi
00031   initial release
00032 
00033 
00034 */
00035  
00042 /**************************************************************************** 
00043  **************************************************************************** 
00044  * TYPES ******************************************************************** 
00045  **************************************************************************** 
00046  ****************************************************************************/ 
00047  
00053 template <class T> class array_c
00054 { 
00055 protected: 
00056  
00057     T           **data;                 
00058     long        count;                  
00059  
00060 public: 
00061  
00062     array_c()
00064     { 
00065         count = 1; 
00066         data  = new T*[count]; 
00067         memset(data, 0, sizeof(T*)*count);
00068     } 
00069 
00070     virtual
00071     ~array_c()
00073     {
00074         AssertThis;
00075 
00076         SafeArrayDelete(data); 
00077     } 
00078  
00079     long
00080     get_free()
00086     { 
00087         AssertThisV;
00088 
00089         long i; 
00090  
00091         for (i=0; i<count; i++) 
00092             if (!data[i]) 
00093                 return i; 
00094  
00095         return count; 
00096     } 
00097 
00098     long
00099     add(T* ptr)
00105     { 
00106         AssertThisV;
00107 
00108         long l = get_free(); 
00109  
00110         if (l==INVALID_INDEX) 
00111             return l; 
00112  
00113         set(l,ptr); 
00114  
00115         return l; 
00116     } 
00117 
00118     void
00119     remove(long i)
00125     { 
00126         AssertThis;
00127 
00128         if (i>=count || i<0) 
00129           return; 
00130  
00131         data[i] = NULL; 
00132     } 
00133 
00134     void
00135     remove_ptr(T* ptr, bool all = true)
00142     {
00143         AssertThis;
00144 
00145         for (slong i=0; i<count; i++)
00146         {
00147             if (data[i] == ptr)
00148             {
00149                 data[i] = NULL;
00150                 if (!all)
00151                     break;
00152             }
00153         }
00154     }
00155  
00156     void
00157     set(long i, T* ptr)
00165     { 
00166         AssertThis;
00167         AssertReturn1(i>=0)
00168  
00169         if (i>=count) 
00170         { 
00171             long newcount = i+1; 
00172  
00173             T **data2; 
00174             data2 = new T*[newcount]; 
00175             memset(data2, 0, sizeof(T*)*newcount);
00176  
00177             for (i=0; i<count; i++) 
00178                 data2[i] = data[i]; 
00179 
00180             SafeArrayDelete(data); 
00181             data = data2; 
00182  
00183             count = newcount; 
00184         } 
00185  
00186         data[i] = ptr; 
00187     } 
00188 
00189     T*
00190     get(long i)
00196     { 
00197         AssertThisV;
00198 
00199         if (i>=count || i<0) 
00200           return NULL; 
00201  
00202         return data[i]; 
00203     } 
00204 
00205     long
00206     find(T* ptr)
00212     { 
00213         AssertThisValue(INVALID_INDEX);
00214 
00215         long i; 
00216  
00217         for (i=0; i<count; i++) 
00218             if (data[i] == ptr) 
00219                 return i; 
00220  
00221         return INVALID_INDEX; 
00222     } 
00223 
00224     long
00225     get_count()
00230     {
00231         AssertThisValue(INVALID_INDEX);
00232         return count+1;
00233     };
00234 
00235     void
00236     clear()
00241     { 
00242         AssertThis;
00243 
00244         SafeArrayDelete(data); 
00245                 count = 1; 
00246                 data = new T*[count]; 
00247         memset(data, 0, sizeof(T*)*count);
00248     };
00249 };

Generated on Wed Dec 18 15:48:46 2002 for omicron engine by doxygen1.2.18