Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Tools/Xabsl2/Xabsl2Engine/Xabsl2Array.h

Go to the documentation of this file.
00001 /** 
00002 * @file Xabsl2Array.h
00003 *
00004 * Declaration and implementation of template class Xabsl2Array
00005 *
00006 * @author Matthias Jüngel
00007 * @author Martin Lötzsch
00008 * @author Thomas Röfer
00009 */
00010 
00011 
00012 #ifndef __Xabsl2Array_h_
00013 #define __Xabsl2Array_h_
00014 
00015 #include <stdlib.h>
00016 #include <string.h>
00017 
00018 /**
00019 * @class Xabsl2NamedItem
00020 * A class that has a text label
00021 * @author Martin Lötzsch
00022 */
00023 class Xabsl2NamedItem
00024 {
00025 public:
00026   /** 
00027   * Constructor
00028   * @param name The name of the item
00029   */
00030   Xabsl2NamedItem(const char* name)
00031   {
00032       n = new char[strlen(name)+1];
00033       strcpy(n,name);
00034   }
00035 
00036   /** Destructor. Deletes the name */
00037   ~Xabsl2NamedItem() { delete[] n; }
00038 
00039   /** The text label */
00040   char* n;
00041 };
00042 
00043 /**
00044 * An element of an Xabsl2Array.
00045 *
00046 * @author Matthias Jüngel
00047 * @author Martin Lötzsch
00048 * @author Thomas Röfer
00049 */
00050 template<class T> class Xabsl2ArrayElement : public Xabsl2NamedItem
00051 {
00052   public:
00053     /**
00054     * Constructor.
00055     * @param name A string label for the element.
00056     * @param element The new element.
00057     */
00058     Xabsl2ArrayElement(const char* name, T element)
00059       : Xabsl2NamedItem(name), e(element) {}
00060     
00061     /** 
00062     * Destructor. If the element is a pointer, it has to be 
00063     * deleted externally 
00064     */
00065     ~Xabsl2ArrayElement() {}
00066     
00067     /** The element */
00068     T e;
00069 };
00070 
00071 /**
00072 * The class implements a dynamic array. Each array element can have a text label.
00073 *
00074 * @author Matthias Jüngel
00075 * @author Martin Lötzsch
00076 * @author Thomas Röfer
00077 */
00078 template <class T> class Xabsl2Array
00079 {
00080   public:
00081     /** Constructor */
00082     Xabsl2Array() 
00083     {
00084       usedSize = 0;
00085       allocatedSize = 2;
00086       data = new Xabsl2ArrayElement<T>*[allocatedSize];
00087     }
00088     
00089     /** Destructor */
00090     ~Xabsl2Array() 
00091     { 
00092       for(int i = 0; i < getSize(); ++i)
00093         delete data[i];
00094       delete[] data;
00095     }
00096 
00097     /** Clears the array */
00098     void clear()
00099     {
00100       for(int i = 0; i < getSize(); ++i)
00101         delete data[i];
00102       delete[] data;
00103       usedSize = 0;
00104       allocatedSize = 2;
00105       data = new Xabsl2ArrayElement<T>*[allocatedSize];
00106     }
00107     
00108     /** 
00109     * Returns the value for a given name. 
00110     * If no element exists for the name, the default value is returned.
00111     * @param name The name element
00112     * @param defaultValue The value that is returned if no element exists for the name.
00113     * @return Either the element found or the default value. 
00114     */
00115     T getElement(const char* name, T defaultValue) const
00116     {
00117       int pos = find(name);
00118       if(pos < 0) 
00119         return defaultValue;
00120       else
00121         return getElement(pos);
00122     }
00123     
00124     /** 
00125     * Returns the value for a given name. 
00126     * Note that the function crashes if the element does not exist.
00127     * @param name The name of the element
00128     */
00129     T getElement(const char* name) const
00130     {
00131       return getElement(find(name));
00132     }
00133 
00134     /** 
00135     * Returns the value for a given array position.
00136     * Note that the function crashes if the required position is bigger than the 
00137     * size of the array.
00138     */
00139     T getElement(int pos) const
00140     {
00141       return data[pos]->e;
00142     }
00143     
00144     /** 
00145     * Returns a pointer to the array element for a given name.
00146     * Note that the function crashes if the element does not exist
00147     * @param name the name of the element 
00148     */
00149     Xabsl2ArrayElement<T>* getPElement(const char* name)
00150     {
00151       return data[find(name)];
00152     }
00153 
00154     /** Returns the name of an element */
00155     const char* getName(int pos) const
00156     { 
00157       return data[pos]->n;
00158     }
00159 
00160     /**
00161     * The function appends a new element to the array.
00162     * @param name A string label for the element.
00163     * @param element The new element.
00164     */
00165     void append(const char* name, T element)
00166     {
00167       if(usedSize == allocatedSize)
00168       {
00169         allocatedSize += allocatedSize / 2; // note that allocatedSize must be at least 2
00170         Xabsl2ArrayElement<T>** temp = new Xabsl2ArrayElement<T>*[allocatedSize];
00171         for(int i = 0; i < getSize(); ++i)
00172           temp[i] = data[i];
00173         delete[] data;
00174         data = temp;
00175       }
00176       data[usedSize++] = new Xabsl2ArrayElement<T>(name,element);
00177     }
00178     
00179     /**
00180     * The function sets the value of an element in the array.
00181     * Note that the function crashes if the element does not exist.
00182     * @param name A string label for the element.
00183     * @param value The new element.
00184     */
00185     void setElement(const char* name, T value)
00186     {
00187       setElement(find(name),value);
00188     }
00189 
00190     /**
00191     * The function sets the value of an element in the array.
00192     * Note that the function crashes if the element does not exist.
00193     * @param pos The position of the element in the array.
00194     * @param value The new element.
00195     */
00196     void setElement(int pos, T value)
00197     {
00198       data[pos]->e = value;
00199     }
00200 
00201     /**
00202     * The function returns the number of elements in the array.
00203     * @return The length of the list.
00204     */
00205     int getSize() const {return usedSize;}
00206     
00207     /** 
00208     * Returns the value for a given array position.
00209     * Note that the function crashes if the required position is bigger than the 
00210     * size of the array.
00211     */
00212     T operator[](int pos) const
00213     {
00214       return getElement(pos);
00215     }
00216     
00217     /** Returns whether an element for the given name exists */
00218     bool exists(const char* name) const
00219     {
00220       return find(name) >= 0;
00221     }
00222 
00223   protected:
00224     /** 
00225     * Returns the index of an element with the given name.
00226     * @param name The name that is searched for.
00227     * @return The index of the element of -1 if the name does not exist.
00228     */
00229     int find(const char* name) const
00230     {
00231       for(int i = 0; i < getSize(); ++i)
00232         if(!strcmp(getName(i),name)) 
00233           return i;
00234       return -1;
00235     }
00236 
00237     /** The array */
00238     Xabsl2ArrayElement<T>** data;
00239 
00240     /** The number of elements in the array */
00241     int usedSize, allocatedSize;
00242 };
00243 
00244 
00245 
00246 
00247 #endif // __Xabsl2Array_h_
00248 
00249 /*
00250 * Change Log:
00251 *
00252 * $Log: Xabsl2Array.h,v $
00253 * Revision 1.1.1.1  2004/05/22 17:37:53  cvsadm
00254 * created new repository GT2004_WM
00255 *
00256 * Revision 1.1  2003/10/07 10:13:25  cvsadm
00257 * Created GT2004 (M.J.)
00258 *
00259 * Revision 1.1.1.1  2003/07/02 09:40:29  cvsadm
00260 * created new repository for the competitions in Padova from the 
00261 * tamara CVS (Tuesday 2:00 pm)
00262 *
00263 * removed unused solutions
00264 *
00265 * Revision 1.10  2003/03/06 11:45:55  dueffert
00266 * re-order warning removed
00267 *
00268 * Revision 1.9  2003/02/05 13:31:09  loetzsch
00269 * removed crash comment. (The bug was somewhere else)
00270 *
00271 * Revision 1.8  2003/02/05 12:10:03  dueffert
00272 * crash comment added
00273 *
00274 * Revision 1.7  2003/01/13 13:19:14  loetzsch
00275 * added Xabsl2Array::getPElement(const char* name) for pointer access to elements
00276 *
00277 * Revision 1.6  2003/01/09 10:00:21  loetzsch
00278 * added clear() method
00279 *
00280 * Revision 1.5  2003/01/08 11:06:46  loetzsch
00281 * introduced Xabsl2NamedItem
00282 *
00283 * Revision 1.4  2002/12/05 16:46:52  loetzsch
00284 * Thomas Röfer replaced the linked list by an array
00285 *
00286 * Revision 1.3  2002/12/02 19:56:31  loetzsch
00287 * - Xabsl2Array now seems to work for more than 1 element
00288 * - Basic behaviors now can be registered at the engine
00289 * - Basic behaviors have access to their parameters
00290 *
00291 * Revision 1.2  2002/12/01 17:54:30  loetzsch
00292 * continued Xabsl2Engine: Xabsl2Array seems to work now
00293 *
00294 * Revision 1.1  2002/12/01 13:45:58  loetzsch
00295 * first version of Xabsl2Engine
00296 *
00297 */
00298 
00299 
00300 
00301 
00302 
00303 

Generated on Thu Sep 23 19:57:42 2004 for GT2004 by doxygen 1.3.6