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