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
    00251
    00252
    00253