>xabsl   The Extensible Agent Behavior Specification Language

XabslEngine Class Library Reference

 

XabslArray.h

Go to the documentation of this file.
00001 /** 
00002 * @file XabslArray.h
00003 *
00004 * Declaration and implementation of template class NamedArray
00005 *
00006 * @author <a href="http://www.martin-loetzsch.de">Martin Loetzsch</a>
00007 * @author <a href="http://www.informatik.hu-berlin.de/~juengel">Matthias JŸngel</a>
00008 * @author <a href="http://www.tzi.de/~roefer/">Thomas Ršfer</a>
00009 */
00010 
00011 
00012 #ifndef __XabslArray_h_
00013 #define __XabslArray_h_
00014 
00015 #include <stdlib.h>
00016 #include <string.h>
00017 
00018 namespace xabsl 
00019 {
00020 
00021 /**
00022 * @class NamedItem
00023 * A class that has a text label
00024 * @author <a href="http://www.martin-loetzsch.de">Martin Loetzsch</a>
00025 */
00026 class NamedItem
00027 {
00028 public:
00029   /** 
00030   * Constructor
00031   * @param name The name of the item
00032   */
00033   NamedItem(const char* name)
00034   {
00035       n = new char[strlen(name)+1];
00036       strcpy(n,name);
00037   }
00038 
00039   /** Destructor. Deletes the name */
00040   virtual ~NamedItem() { delete[] n; }
00041 
00042   /** The text label */
00043   char* n;
00044 };
00045 
00046 /**
00047 * An element of an NamedArray.
00048 *
00049 * @author <a href="http://www.martin-loetzsch.de">Martin Loetzsch</a>
00050 * @author <a href="http://www.informatik.hu-berlin.de/~juengel">Matthias JŸngel</a>
00051 * @author <a href="http://www.tzi.de/~roefer/">Thomas Ršfer</a>
00052 */
00053 template<class T> class NamedArrayElement : public NamedItem
00054 {
00055   public:
00056     /**
00057     * Constructor.
00058     * @param name A string label for the element.
00059     * @param element The new element.
00060     */
00061     NamedArrayElement(const char* name, T element)
00062       : NamedItem(name), e(element) {}
00063     
00064     /** 
00065     * Destructor. If the element is a pointer, it has to be 
00066     * deleted externally 
00067     */
00068     virtual ~NamedArrayElement() {}
00069     
00070     /** The element */
00071     T e;
00072 };
00073 
00074 /**
00075 * The class implements a dynamic array.
00076 *
00077 * @author <a href="http://www.martin-loetzsch.de">Martin Loetzsch</a>
00078 * @author <a href="http://www.informatik.hu-berlin.de/~juengel">Matthias JŸngel</a>
00079 * @author <a href="http://www.tzi.de/~roefer/">Thomas Ršfer</a>
00080 * @author <a href="http://www.sim.informatik.tu-darmstadt.de/pers/card/risler.html">Max Risler</a>
00081 */
00082 template <class T> class Array
00083 {
00084   public:
00085     /** Constructor */
00086     Array() 
00087     {
00088       usedSize = 0;
00089       allocatedSize = 2;
00090       data = new T[allocatedSize];
00091     }
00092     
00093     /** Destructor */
00094     virtual ~Array() 
00095     { 
00096       delete[] data;
00097     }
00098 
00099     /** Assignment operator
00100     *\param other The other Array that is assigned to this one
00101     *\return A reference to this object after the assignment.
00102     */
00103     Array<T>& operator=(const Array<T>& other)
00104     {
00105       delete[] data;
00106       usedSize = other.usedSize;
00107       allocatedSize = other.allocatedSize;
00108       data = new T[allocatedSize];
00109       for (int i = 0; i < usedSize; ++i)
00110         data[i]=other.data[i];
00111       return *this;
00112     }
00113 
00114     /** Copy constructor
00115     *\param other The other vector that is copied to this one
00116     */
00117     Array(const Array<T>& other) 
00118     {
00119       data = new T[0];
00120       *this = other;
00121     }
00122 
00123     /** Clears the array */
00124     void clear()
00125     {
00126       delete[] data;
00127       usedSize = 0;
00128       allocatedSize = 2;
00129       data = new T[allocatedSize];
00130     }
00131         
00132     /** 
00133     * Returns the value for a given array position.
00134     * Note that the function crashes if the required position is bigger than the 
00135     * size of the array.
00136     */
00137     T getElement(int pos) const
00138     {
00139       return data[pos];
00140     }
00141     
00142     /**
00143     * The function appends a new element to the array.
00144     * @param element The new element.
00145     */
00146     void append(T element)
00147     {
00148       if(usedSize == allocatedSize)
00149       {
00150         allocatedSize += allocatedSize / 2; // note that allocatedSize must be at least 2
00151         T* temp = new T[allocatedSize];
00152         for(int i = 0; i < getSize(); ++i)
00153           temp[i] = data[i];
00154         delete[] data;
00155         data = temp;
00156       }
00157       data[usedSize++] = element;
00158     }
00159     
00160     /**
00161     * The function sets the value of an element in the array.
00162     * Note that the function crashes if the element does not exist.
00163     * @param pos The position of the element in the array.
00164     * @param value The new element.
00165     */
00166     void setElement(int pos, T value)
00167     {
00168       data[pos] = value;
00169     }
00170 
00171     /**
00172     * The function returns the number of elements in the array.
00173     * @return The length of the list.
00174     */
00175     int getSize() const {return usedSize;}
00176     
00177     /** 
00178     * Returns the value for a given array position.
00179     * Note that the function crashes if the required position is bigger than the 
00180     * size of the array.
00181     */
00182     const T& operator[](int pos) const
00183     {
00184       return data[pos];
00185     }
00186     T& operator[](int pos)
00187     {
00188       while (pos >= usedSize) append(T());
00189       return data[pos];
00190     }
00191     
00192     /** Removes the last element of the array */
00193     void removeLast()
00194     {
00195       if (usedSize > 0) 
00196         usedSize--;
00197     }
00198 
00199     operator Array<const T>&() {return this;}
00200 
00201   protected:
00202     /** The array */
00203     T* data;
00204 
00205     /** The number of elements in the array */
00206     int usedSize, allocatedSize;
00207 };
00208 
00209 /**
00210 * The class implements a dynamic array. Each array element can have a text label.
00211 *
00212 * @author <a href="http://www.martin-loetzsch.de">Martin Loetzsch</a>
00213 * @author <a href="http://www.informatik.hu-berlin.de/~juengel">Matthias JŸngel</a>
00214 * @author <a href="http://www.tzi.de/~roefer/">Thomas Ršfer</a>
00215 */
00216 template <class T> class NamedArray : public Array<NamedArrayElement<T>*>
00217 {
00218   public:
00219     /** Destructor */
00220     virtual ~NamedArray() 
00221     { 
00222       for(int i = 0; i < this->getSize(); ++i)
00223         delete this->data[i];
00224     }
00225 
00226     /** Assignment operator
00227     *\param other The other NamedArray that is assigned to this one
00228     *\return A reference to this object after the assignment.
00229     */
00230     NamedArray<T>& operator=(const NamedArray<T>& other)
00231     {
00232       for(int i = 0; i < this->usedSize; ++i)
00233         delete this->data[i];
00234       delete[] this->data;
00235       this->usedSize = other.usedSize;
00236       this->allocatedSize = other.allocatedSize;
00237       this->data = new T[this->allocatedSize];
00238       for (int i = 0; i < this->usedSize; ++i)
00239         this->data[i]=new NamedArrayElement<T>(other.data[i]->n,other.data[i]->e);
00240       return *this;
00241     }
00242 
00243     /** Clears the array */
00244     void clear()
00245     {
00246       for(int i = 0; i < this->getSize(); ++i)
00247         delete this->data[i];
00248       Array<NamedArrayElement<T>*>::clear();
00249     }
00250 
00251     /** 
00252     * Returns the value for a given name. 
00253     * If no element exists for the name, the default value is returned.
00254     * @param name The name element
00255     * @param defaultValue The value that is returned if no element exists for the name.
00256     * @return Either the element found or the default value. 
00257     */
00258     T getElement(const char* name, T defaultValue) const
00259     {
00260       int pos = find(name);
00261       if(pos < 0) 
00262         return defaultValue;
00263       else
00264         return getElement(pos)->e;
00265     }
00266     
00267     /** 
00268     * Returns the value for a given name. 
00269     * Note that the function crashes if the element does not exist.
00270     * @param name The name of the element
00271     */
00272     T& getElement(const char* name) const
00273     {
00274       return getElement(find(name));
00275     }
00276 
00277     /** 
00278     * Returns the value for a given array position.
00279     * Note that the function crashes if the required position is bigger than the 
00280     * size of the array.
00281     */
00282     T& getElement(int pos) const
00283     {
00284       return this->data[pos]->e;
00285     }
00286     
00287     /** 
00288     * Returns a pointer to the array element for a given name.
00289     * Note that the function crashes if the element does not exist
00290     * @param name the name of the element 
00291     */
00292     NamedArrayElement<T>* getPElement(const char* name)
00293     {
00294       return this->data[find(name)];
00295     }
00296 
00297     /** Returns the name of an element */
00298     const char* getName(int pos) const
00299     { 
00300       return this->data[pos]->n;
00301     }
00302 
00303     /**
00304     * The function appends a new element to the array.
00305     * @param name A string label for the element.
00306     * @param element The new element.
00307     */
00308     void append(const char* name, T element)
00309     {
00310       Array<NamedArrayElement<T>*>::append(new NamedArrayElement<T>(name,element));
00311     }
00312     void append(T element)
00313     {
00314       append("",element);
00315     }
00316     
00317     /**
00318     * The function sets the value of an element in the array.
00319     * Note that the function crashes if the element does not exist.
00320     * @param name A string label for the element.
00321     * @param value The new element.
00322     */
00323     void setElement(const char* name, T value)
00324     {
00325       setElement(find(name),value);
00326     }
00327 
00328     /**
00329     * The function sets the value of an element in the array.
00330     * Note that the function crashes if the element does not exist.
00331     * @param pos The position of the element in the array.
00332     * @param value The new element.
00333     */
00334     void setElement(int pos, T value)
00335     {
00336       this->data[pos]->e = value;
00337     }
00338     
00339     /** 
00340     * Returns the value for a given array position.
00341     * Note that the function crashes if the required position is bigger than the 
00342     * size of the array.
00343     */
00344     T operator[](int pos) const
00345     {
00346       return getElement(pos);
00347     }
00348     
00349     /** 
00350     * Returns the value for a given name. 
00351     * Note that the function crashes if the element does not exist.
00352     * @param name The name of the element
00353     */
00354     T operator[](const char* name) const
00355     {
00356       return getElement(name);
00357     }
00358 
00359     /** Returns whether an element for the given name exists */
00360     bool exists(const char* name) const
00361     {
00362       return find(name) >= 0;
00363     }
00364 
00365     /** Removes the last element of the array */
00366     void removeLast()
00367     {
00368       if (this->usedSize > 0) 
00369         delete this->data[--this->usedSize];
00370     }
00371 
00372   protected:
00373     /** 
00374     * Returns the index of an element with the given name.
00375     * @param name The name that is searched for.
00376     * @return The index of the element of -1 if the name does not exist.
00377     */
00378     int find(const char* name) const
00379     {
00380       for(int i = 0; i < this->getSize(); ++i)
00381         if(!strcmp(getName(i),name)) 
00382           return i;
00383       return -1;
00384     }
00385 };
00386 
00387 
00388 } // namespace
00389 
00390 #endif // __XabslArray_h_
00391 
00392 
00393 
00394 
00395 
00396 

Up | Main Page | Generated at Wed Aug 19 17:32:28 2009.