Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/height_map/src/lib/util/array.h @ 5989

Last change on this file since 5989 was 5466, checked in by bensch, 19 years ago

orxonox/trunk: some changes from class to typename in Templates

File size: 5.0 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 * This array is very performant and usefull, if you need a Dynamic Array,
23 * that you fill once, and then only read the pushed in values to it again.
24 */
25
26#ifndef _ARRAY_H
27#define _ARRAY_H
28#include "debug.h"
29
30using namespace std;
31
32//! tArray Class that handles dynamic-type arrays.
33template<class T> class tArray
34{
35  public:
36    tArray ();
37    ~tArray();
38
39    void finalizeArray ();
40    void addEntry (T entry);
41    void addEntry(T entry0, T entry1, T entry2);
42
43    /** @returns The array */
44    inline T* getArray () const { return this->array; };
45    inline const T getEntry(unsigned int number) const;
46    /** * @returns The Count of entries in the Array*/
47    inline unsigned int getCount()const { return this->entryCount; };
48    inline int getIndex(T* entry) const;
49    inline bool isFinalized() const { return this->finalized; }
50    void debug() const ;
51
52  private:
53    //! One entry of the Array
54    struct Entry
55    {
56      T            value;          //!< The value of this Entry.
57      Entry*       next;           //!< Pointer to the Next entry.
58    };
59
60    T*            array;           //!< The array that will be produced when finalizing the Array.
61    unsigned int  entryCount;      //!< The count of Entries in this Array.
62    bool          finalized;       //!< If this variable is set to true, the Array can not be changed anymore. true if finalized, false else (initially).
63    Entry*        firstEntry;      //!< Pointer to the first Entry of this Array
64    Entry*        currentEntry;    //!< Pointer to the current Entry of this Array. The one Entry we are working with.
65};
66
67
68/**
69 *  creates a new Array
70*/
71template<class T>
72tArray<T>::tArray ()
73{
74  PRINTF(5)("crating new tArray\n");
75  this->firstEntry = new Entry;
76  this->firstEntry->next =NULL;
77  this->currentEntry = this->firstEntry;
78  this->finalized = false;
79  this->entryCount = 0; //0 means one entry
80}
81
82template<class T>
83    const T tArray<T>::getEntry(unsigned int number) const
84{
85  if (this->finalized && number < this->entryCount)
86    return this->array[number];
87}
88
89/**
90 *  deletes an Array.
91   It does this by first deleting all the array-entries, and then delete the array[] itself
92*/
93template<class T>
94tArray<T>::~tArray()
95{
96  PRINTF(5)("deleting array\n");
97  if (!this->finalized)
98  {
99    Entry* walker = this->firstEntry;
100    Entry* previous;
101    while (walker)
102    {
103      previous = walker;
104      walker = walker->next;
105      delete previous;
106    }
107  }
108  if (this->finalized)
109    delete[] this->array;
110}
111
112/**
113 *  finalizes an array.
114   This Function creates the array, and makes it ready to be sent to the application.
115*/
116template<class T>
117void tArray<T>::finalizeArray ()
118{
119  if (this->finalized)
120    return;
121  PRINTF(5)("Finalizing array. Length: %i\n", entryCount);
122  if (!(this->array = new T [this->entryCount]))
123    PRINTF(1)("could not allocate %i data Blocks\n", this->entryCount);
124  Entry* walker = this->firstEntry;
125  for (int i=0; i < this->entryCount; i++)
126  {
127    this->array[i] = walker->value;
128    walker = walker->next;
129  }
130  walker = this->firstEntry;
131  Entry* previous;
132  while (walker)
133  {
134    previous = walker;
135    walker = walker->next;
136    delete previous;
137  }
138  this->firstEntry = NULL;
139  this->finalized = true;
140}
141
142/**
143 *  adds a new Entry to the Array
144 * @param entry Entry to add.
145*/
146template<class T>
147void tArray<T>::addEntry (T entry)
148{
149  if (!this->finalized)
150  {
151    PRINTF(5)("adding new Entry to Array: %f\n", entry);
152
153    this->currentEntry->value = entry;
154    this->currentEntry->next = new Entry;
155    this->currentEntry = currentEntry->next;
156    this->currentEntry->next = NULL;
157    ++this->entryCount;
158  }
159  else
160    PRINTF(2)("adding failed, because array has already been finalized\n");
161}
162
163/**
164 *  Adds 3 entries at once (convenience)
165*/
166template<class T>
167void tArray<T>::addEntry (T entry0, T entry1, T entry2)
168{
169  this->addEntry(entry0);
170  this->addEntry(entry1);
171  this->addEntry(entry2);
172}
173
174
175/**
176 *  gets back the index of the entry in the array. value check
177 * @param entry: the entry to look up
178 * @returns the index in the array, -1 if not found
179 */
180template<class T>
181int tArray<T>::getIndex(T* entry) const
182{
183  if( unlikely(this->finalized == false))
184    return -1;
185
186  for(int i = 0; i < this->entryCount; ++i)
187  {
188    if( unlikely(*entry == this->array[i]))
189      return i;
190  }
191}
192
193
194/**
195 *  Simple debug info about the Array
196*/
197template<class T>
198void tArray<T>::debug () const
199{
200  PRINT(0)("entryCount=%i, address=%p\n", this->entryCount, this->array);
201}
202
203#endif
Note: See TracBrowser for help on using the repository browser.