Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/util/array.h @ 5405

Last change on this file since 5405 was 5390, checked in by bensch, 19 years ago

orxonox/trunk: renamed Array to tArray

File size: 4.8 KB
RevLine 
[4579]1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Benjamin Grauer
13   co-programmer: ...
14*/
15
[2842]16/*!
[5262]17  @file array.h
[5390]18  @brief Contains the tArray Class that handles arrays of classes.
[2842]19  this class creates a Array of a semi-Dynamic length.
20  beware, that after finalizing the array may not be resized again.
21*/
22
[2776]23#ifndef _ARRAY_H
24#define _ARRAY_H
[4579]25#include "debug.h"
[2776]26
[5388]27using namespace std;
28
[5390]29//! tArray Class that handles dynamic-type arrays.
30template<class T> class tArray
[2754]31{
[4579]32  public:
[5390]33    tArray ();
34    ~tArray();
[2754]35
[4746]36    void finalizeArray ();
[4579]37    void addEntry (T entry);
38    void addEntry(T entry0, T entry1, T entry2);
[4577]39
[4836]40    /** @returns The array */
[5321]41    inline T* getArray () const { return this->array; };
[5100]42    inline const T getEntry(unsigned int number) const;
[4836]43    /** * @returns The Count of entries in the Array*/
[4746]44    inline unsigned int getCount()const { return this->entryCount; };
[4791]45    inline int getIndex(T* entry) const;
[4799]46    inline bool isFinalized() const { return this->finalized; }
[4746]47    void debug() const ;
[4579]48
49  private:
50    //! One entry of the Array
51    struct Entry
52    {
53      T            value;          //!< The value of this Entry.
54      Entry*       next;           //!< Pointer to the Next entry.
55    };
56
57    T*            array;           //!< The array that will be produced when finalizing the Array.
58    unsigned int  entryCount;      //!< The count of Entries in this Array.
59    bool          finalized;       //!< If this variable is set to true, the Array can not be changed anymore. true if finalized, false else (initially).
60    Entry*        firstEntry;      //!< Pointer to the first Entry of this Array
61    Entry*        currentEntry;    //!< Pointer to the current Entry of this Array. The one Entry we are working with.
62};
63
64
65/**
[4836]66 *  creates a new Array
[4579]67*/
68template<class T>
[5390]69tArray<T>::tArray ()
[4579]70{
[5390]71  PRINTF(5)("crating new tArray\n");
[4579]72  this->firstEntry = new Entry;
73  this->firstEntry->next =NULL;
[4580]74  this->currentEntry = this->firstEntry;
[4579]75  this->finalized = false;
76  this->entryCount = 0; //0 means one entry
77}
78
[5100]79template<class T>
[5390]80    const T tArray<T>::getEntry(unsigned int number) const
[5100]81{
82  if (this->finalized && number < this->entryCount)
83    return this->array[number];
84}
85
[4579]86/**
[4836]87 *  deletes an Array.
[4579]88   It does this by first deleting all the array-entries, and then delete the array[] itself
89*/
90template<class T>
[5390]91tArray<T>::~tArray()
[4579]92{
[5100]93  PRINTF(5)("deleting array\n");
[5262]94  if (!this->finalized)
[2807]95  {
[5262]96    Entry* walker = this->firstEntry;
97    Entry* previous;
98    while (walker)
99    {
100      previous = walker;
101      walker = walker->next;
102      delete previous;
103    }
[4579]104  }
[5262]105  if (this->finalized)
[5100]106    delete[] this->array;
[4579]107}
[2807]108
[4579]109/**
[4836]110 *  finalizes an array.
[4579]111   This Function creates the array, and makes it ready to be sent to the application.
112*/
113template<class T>
[5390]114void tArray<T>::finalizeArray ()
[4579]115{
[5262]116  if (this->finalized)
117    return;
[5100]118  PRINTF(5)("Finalizing array. Length: %i\n", entryCount);
119  if (!(this->array = new T [this->entryCount]))
120    PRINTF(1)("could not allocate %i data Blocks\n", this->entryCount);
[4579]121  Entry* walker = this->firstEntry;
[5262]122  for (int i=0; i < this->entryCount; i++)
[4579]123  {
124    this->array[i] = walker->value;
125    walker = walker->next;
126  }
[4580]127  walker = this->firstEntry;
128  Entry* previous;
129  while (walker)
130  {
131    previous = walker;
132    walker = walker->next;
133    delete previous;
134  }
135  this->firstEntry = NULL;
[4579]136  this->finalized = true;
137}
[4577]138
[4579]139/**
[4836]140 *  adds a new Entry to the Array
141 * @param entry Entry to add.
[4579]142*/
143template<class T>
[5390]144void tArray<T>::addEntry (T entry)
[4579]145{
146  if (!this->finalized)
147  {
148    PRINTF(5)("adding new Entry to Array: %f\n", entry);
[4577]149
[4579]150    this->currentEntry->value = entry;
151    this->currentEntry->next = new Entry;
152    this->currentEntry = currentEntry->next;
153    this->currentEntry->next = NULL;
154    ++this->entryCount;
155  }
156  else
[4580]157    PRINTF(2)("adding failed, because array has already been finalized\n");
[4579]158}
[2776]159
[4579]160/**
[4836]161 *  Adds 3 entries at once (convenience)
[4579]162*/
163template<class T>
[5390]164void tArray<T>::addEntry (T entry0, T entry1, T entry2)
[4579]165{
166  this->addEntry(entry0);
167  this->addEntry(entry1);
168  this->addEntry(entry2);
169}
170
[4791]171
[4793]172/**
[4836]173 *  gets back the index of the entry in the array. value check
174 * @param entry: the entry to look up
175 * @returns the index in the array, -1 if not found
[4793]176 */
[4791]177template<class T>
[5390]178int tArray<T>::getIndex(T* entry) const
[4791]179{
[4792]180  if( unlikely(this->finalized == false))
181    return -1;
[4791]182
[4792]183  for(int i = 0; i < this->entryCount; ++i)
184  {
[4793]185    if( unlikely(*entry == this->array[i]))
[4792]186      return i;
187  }
[4791]188}
189
190
[4579]191/**
[4836]192 *  Simple debug info about the Array
[4579]193*/
194template<class T>
[5390]195void tArray<T>::debug () const
[4579]196{
197  PRINT(0)("entryCount=%i, address=%p\n", this->entryCount, this->array);
198}
[4791]199
[2776]200#endif
Note: See TracBrowser for help on using the repository browser.