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