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
Line 
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
16/*!
17  @file array.h
18  @brief Contains the tArray Class that handles arrays of classes.
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
23#ifndef _ARRAY_H
24#define _ARRAY_H
25#include "debug.h"
26
27using namespace std;
28
29//! tArray Class that handles dynamic-type arrays.
30template<class T> class tArray
31{
32  public:
33    tArray ();
34    ~tArray();
35
36    void finalizeArray ();
37    void addEntry (T entry);
38    void addEntry(T entry0, T entry1, T entry2);
39
40    /** @returns The array */
41    inline T* getArray () const { return this->array; };
42    inline const T getEntry(unsigned int number) const;
43    /** * @returns The Count of entries in the Array*/
44    inline unsigned int getCount()const { return this->entryCount; };
45    inline int getIndex(T* entry) const;
46    inline bool isFinalized() const { return this->finalized; }
47    void debug() const ;
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/**
66 *  creates a new Array
67*/
68template<class T>
69tArray<T>::tArray ()
70{
71  PRINTF(5)("crating new tArray\n");
72  this->firstEntry = new Entry;
73  this->firstEntry->next =NULL;
74  this->currentEntry = this->firstEntry;
75  this->finalized = false;
76  this->entryCount = 0; //0 means one entry
77}
78
79template<class T>
80    const T tArray<T>::getEntry(unsigned int number) const
81{
82  if (this->finalized && number < this->entryCount)
83    return this->array[number];
84}
85
86/**
87 *  deletes an Array.
88   It does this by first deleting all the array-entries, and then delete the array[] itself
89*/
90template<class T>
91tArray<T>::~tArray()
92{
93  PRINTF(5)("deleting array\n");
94  if (!this->finalized)
95  {
96    Entry* walker = this->firstEntry;
97    Entry* previous;
98    while (walker)
99    {
100      previous = walker;
101      walker = walker->next;
102      delete previous;
103    }
104  }
105  if (this->finalized)
106    delete[] this->array;
107}
108
109/**
110 *  finalizes an array.
111   This Function creates the array, and makes it ready to be sent to the application.
112*/
113template<class T>
114void tArray<T>::finalizeArray ()
115{
116  if (this->finalized)
117    return;
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);
121  Entry* walker = this->firstEntry;
122  for (int i=0; i < this->entryCount; i++)
123  {
124    this->array[i] = walker->value;
125    walker = walker->next;
126  }
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;
136  this->finalized = true;
137}
138
139/**
140 *  adds a new Entry to the Array
141 * @param entry Entry to add.
142*/
143template<class T>
144void tArray<T>::addEntry (T entry)
145{
146  if (!this->finalized)
147  {
148    PRINTF(5)("adding new Entry to Array: %f\n", entry);
149
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
157    PRINTF(2)("adding failed, because array has already been finalized\n");
158}
159
160/**
161 *  Adds 3 entries at once (convenience)
162*/
163template<class T>
164void tArray<T>::addEntry (T entry0, T entry1, T entry2)
165{
166  this->addEntry(entry0);
167  this->addEntry(entry1);
168  this->addEntry(entry2);
169}
170
171
172/**
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
176 */
177template<class T>
178int tArray<T>::getIndex(T* entry) const
179{
180  if( unlikely(this->finalized == false))
181    return -1;
182
183  for(int i = 0; i < this->entryCount; ++i)
184  {
185    if( unlikely(*entry == this->array[i]))
186      return i;
187  }
188}
189
190
191/**
192 *  Simple debug info about the Array
193*/
194template<class T>
195void tArray<T>::debug () const
196{
197  PRINT(0)("entryCount=%i, address=%p\n", this->entryCount, this->array);
198}
199
200#endif
Note: See TracBrowser for help on using the repository browser.