1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/** 
* @file XabslArray.h
*
* Declaration and implementation of template class NamedArray
*
* @author <a href="http://www.martin-loetzsch.de">Martin Loetzsch</a>
* @author <a href="http://www.informatik.hu-berlin.de/~juengel">Matthias Juengel</a>
* @author <a href="http://www.tzi.de/~roefer/">Thomas Roefer</a>
*/


#ifndef __XabslArray_h_
#define __XabslArray_h_

#include <stdlib.h>
#include <string.h>

namespace xabsl 
{

/**
* @class NamedItem
* A class that has a text label
* @author <a href="http://www.martin-loetzsch.de">Martin Loetzsch</a>
*/
class NamedItem<--- class 'NamedItem' does not have a copy constructor which is recommended since the class contains a pointer to allocated memory.
{
public:
  /** 
  * Constructor
  * @param name The name of the item
  */
  NamedItem(const char* name)
  {
      n = new char[strlen(name)+1];
      strcpy(n,name);
  }

  /** Destructor. Deletes the name */
  virtual ~NamedItem() { delete[] n; }

  /** The text label */
  char* n;
};

/**
* An element of an NamedArray.
*
* @author <a href="http://www.martin-loetzsch.de">Martin Loetzsch</a>
* @author <a href="http://www.informatik.hu-berlin.de/~juengel">Matthias Juengel</a>
* @author <a href="http://www.tzi.de/~roefer/">Thomas Roefer</a>
*/
template<class T> class NamedArrayElement : public NamedItem
{
  public:
    /**
    * Constructor.
    * @param name A string label for the element.
    * @param element The new element.
    */
    NamedArrayElement(const char* name, T element)
      : NamedItem(name), e(element) {}
    
    /** 
    * Destructor. If the element is a pointer, it has to be 
    * deleted externally 
    */
    virtual ~NamedArrayElement() {}
    
    /** The element */
    T e;
};

/**
* The class implements a dynamic array.
*
* @author <a href="http://www.martin-loetzsch.de">Martin Loetzsch</a>
* @author <a href="http://www.informatik.hu-berlin.de/~juengel">Matthias Juengel</a>
* @author <a href="http://www.tzi.de/~roefer/">Thomas Roefer</a>
* @author <a href="http://www.sim.informatik.tu-darmstadt.de/pers/card/risler.html">Max Risler</a>
*/
template <class T> class Array
{
  public:
    /** Constructor */
    Array() 
    {
      usedSize = 0;
      allocatedSize = 2;
      data = new T[allocatedSize];
    }
    
    /** Destructor */
    virtual ~Array() 
    { 
      delete[] data;
    }

    /** Assignment operator
    *\param other The other Array that is assigned to this one
    *\return A reference to this object after the assignment.
    */
    Array<T>& operator=(const Array<T>& other)
    {
      delete[] data;
      usedSize = other.usedSize;
      allocatedSize = other.allocatedSize;
      data = new T[allocatedSize];
      for (int i = 0; i < usedSize; i++)
        data[i]=other.data[i];
      return *this;
    }

    /** Copy constructor
    *\param other The other vector that is copied to this one
    */
    Array(const Array<T>& other) 
    {
      data = new T[0];
      *this = other;
    }

    /** Clears the array */
    void clear()
    {
      delete[] data;
      usedSize = 0;
      allocatedSize = 2;
      data = new T[allocatedSize];
    }
        
    /** 
    * Returns the value for a given array position.
    * Note that the function crashes if the required position is bigger than the 
    * size of the array.
    */
    T getElement(int pos) const
    {
      return data[pos];
    }
    
    /**
    * The function appends a new element to the array.
    * @param element The new element.
    */
    void append(T element)
    {
      if(usedSize == allocatedSize)
      {
        allocatedSize += allocatedSize / 2; // note that allocatedSize must be at least 2
        T* temp = new T[allocatedSize];
        for(int i = 0; i < getSize(); ++i)
          temp[i] = data[i];
        delete[] data;
        data = temp;
      }
      data[usedSize++] = element;
    }
    
    /**
    * The function sets the value of an element in the array.
    * Note that the function crashes if the element does not exist.
    * @param pos The position of the element in the array.
    * @param value The new element.
    */
    void setElement(int pos, T value)
    {
      data[pos] = value;
    }

    /**
    * The function returns the number of elements in the array.
    * @return The length of the list.
    */
    int getSize() const {return usedSize;}
    
    /** 
    * Returns the value for a given array position.
    * Note that the function crashes if the required position is bigger than the 
    * size of the array.
    */
    const T& operator[](int pos) const
    {
      return data[pos];
    }
    T& operator[](int pos)
    {
      while (pos >= usedSize) append(T());
      return data[pos];
    }
    
    /** Removes the last element of the array */
    void removeLast()
    {
      if (usedSize > 0) 
        usedSize--;
    }

    // TODO: is this operator needed??
//    operator Array<const T>&() {return this;}

  protected:
    /** The array */
    T* data;

    /** The number of elements in the array */
    int usedSize, allocatedSize;
};

/**
* The class implements a dynamic array. Each array element can have a text label.
*
* @author <a href="http://www.martin-loetzsch.de">Martin Loetzsch</a>
* @author <a href="http://www.informatik.hu-berlin.de/~juengel">Matthias Juengel</a>
* @author <a href="http://www.tzi.de/~roefer/">Thomas Roefer</a>
*/
template <class T> class NamedArray : public Array<NamedArrayElement<T>*>
{
  public:
    /** Destructor */
    virtual ~NamedArray()
    {
      for(int i = 0; i < this->getSize(); ++i)
        delete this->data[i];
    }

    /** Assignment operator
    *\param other The other NamedArray that is assigned to this one
    *\return A reference to this object after the assignment.
    */
    NamedArray<T>& operator=(const NamedArray<T>& other)
    {
      for(int i = 0; i < this->usedSize; ++i)
        delete this->data[i];
      delete[] this->data;
      this->usedSize = other.usedSize;
      this->allocatedSize = other.allocatedSize;
      this->data = new T[this->allocatedSize];
      for (int i = 0; i < this->usedSize; i++)
        this->data[i]=new NamedArrayElement<T>(other.data[i]->n,other.data[i]->e);
      return *this;
    }

    /** Clears the array */
    void clear()
    {
      for(int i = 0; i < this->getSize(); ++i)
        delete this->data[i];
      Array<NamedArrayElement<T>*>::clear();
    }

    /** 
    * Returns the value for a given name. 
    * If no element exists for the name, the default value is returned.
    * @param name The name element
    * @param defaultValue The value that is returned if no element exists for the name.
    * @return Either the element found or the default value. 
    */
    T getElement(const char* name, T defaultValue) const
    {
      int pos = find(name);
      if(pos < 0)
        return defaultValue;
      else
        return getElement(pos)->e;
    }
    
    /** 
    * Returns the value for a given name. 
    * Note that the function crashes if the element does not exist.
    * @param name The name of the element
    */
    T& getElement(const char* name) const
    {
      return getElement(find(name));
    }

    /** 
    * Returns the value for a given array position.
    * Note that the function crashes if the required position is bigger than the 
    * size of the array.
    */
    T& getElement(int pos) const
    {
      return this->data[pos]->e;
    }
    
    /** 
    * Returns a pointer to the array element for a given name.
    * Note that the function crashes if the element does not exist
    * @param name the name of the element 
    */
    NamedArrayElement<T>* getPElement(const char* name)
    {
      return this->data[find(name)];
    }

    /** Returns the name of an element */
    const char* getName(int pos) const
    {
      return this->data[pos]->n;
    }

    /**
    * The function appends a new element to the array.
    * @param name A string label for the element.
    * @param element The new element.
    */
    void append(const char* name, T element)
    {
      Array<NamedArrayElement<T>*>::append(new NamedArrayElement<T>(name,element));
    }
    void append(T element)
    {
      append("",element);
    }
    
    /**
    * The function sets the value of an element in the array.
    * Note that the function crashes if the element does not exist.
    * @param name A string label for the element.
    * @param value The new element.
    */
    void setElement(const char* name, T value)
    {
      setElement(find(name),value);
    }

    /**
    * The function sets the value of an element in the array.
    * Note that the function crashes if the element does not exist.
    * @param pos The position of the element in the array.
    * @param value The new element.
    */
    void setElement(int pos, T value)
    {
      this->data[pos]->e = value;
    }
    
    /** 
    * Returns the value for a given array position.
    * Note that the function crashes if the required position is bigger than the 
    * size of the array.
    */
    T operator[](int pos) const
    {
      return getElement(pos);
    }
    
    /** 
    * Returns the value for a given name. 
    * Note that the function crashes if the element does not exist.
    * @param name The name of the element
    */
    T operator[](const char* name) const
    {
      return getElement(name);
    }

    /** Returns whether an element for the given name exists */
    bool exists(const char* name) const
    {
      return find(name) >= 0;
    }

    /** Removes the last element of the array */
    void removeLast()
    {
      if (this->usedSize > 0)
        delete this->data[--(this->usedSize)];
    }

  protected:
    /** 
    * Returns the index of an element with the given name.
    * @param name The name that is searched for.
    * @return The index of the element of -1 if the name does not exist.
    */
    int find(const char* name) const
    {
      for(int i = 0; i < this->getSize(); ++i)
        if(!strcmp(getName(i),name))
          return i;
      return -1;
    }
};


} // namespace

#endif // __XabslArray_h_