Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

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

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