[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 |
---|
[5100] | 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 */ |
---|
[4579] | 39 | inline const 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"); |
---|
[4579] | 92 | Entry* walker = this->firstEntry; |
---|
| 93 | Entry* previous; |
---|
| 94 | while (walker) |
---|
[2807] | 95 | { |
---|
[4579] | 96 | previous = walker; |
---|
| 97 | walker = walker->next; |
---|
| 98 | delete previous; |
---|
| 99 | } |
---|
| 100 | if (finalized) |
---|
[5100] | 101 | delete[] this->array; |
---|
[4579] | 102 | } |
---|
[2807] | 103 | |
---|
[4579] | 104 | /** |
---|
[4836] | 105 | * finalizes an array. |
---|
[4579] | 106 | This Function creates the array, and makes it ready to be sent to the application. |
---|
| 107 | */ |
---|
| 108 | template<class T> |
---|
[4746] | 109 | void Array<T>::finalizeArray () |
---|
[4579] | 110 | { |
---|
[5100] | 111 | PRINTF(5)("Finalizing array. Length: %i\n", entryCount); |
---|
| 112 | if (!(this->array = new T [this->entryCount])) |
---|
| 113 | PRINTF(1)("could not allocate %i data Blocks\n", this->entryCount); |
---|
[4579] | 114 | Entry* walker = this->firstEntry; |
---|
| 115 | for (int i=0; i<this->entryCount; i++) |
---|
| 116 | { |
---|
| 117 | this->array[i] = walker->value; |
---|
| 118 | walker = walker->next; |
---|
| 119 | } |
---|
[4580] | 120 | walker = this->firstEntry; |
---|
| 121 | Entry* previous; |
---|
| 122 | while (walker) |
---|
| 123 | { |
---|
| 124 | previous = walker; |
---|
| 125 | walker = walker->next; |
---|
| 126 | delete previous; |
---|
| 127 | } |
---|
| 128 | this->firstEntry = NULL; |
---|
[4579] | 129 | this->finalized = true; |
---|
| 130 | } |
---|
[4577] | 131 | |
---|
[4579] | 132 | /** |
---|
[4836] | 133 | * adds a new Entry to the Array |
---|
| 134 | * @param entry Entry to add. |
---|
[4579] | 135 | */ |
---|
| 136 | template<class T> |
---|
| 137 | void Array<T>::addEntry (T entry) |
---|
| 138 | { |
---|
| 139 | if (!this->finalized) |
---|
| 140 | { |
---|
| 141 | PRINTF(5)("adding new Entry to Array: %f\n", entry); |
---|
[4577] | 142 | |
---|
[4579] | 143 | this->currentEntry->value = entry; |
---|
| 144 | this->currentEntry->next = new Entry; |
---|
| 145 | this->currentEntry = currentEntry->next; |
---|
| 146 | this->currentEntry->next = NULL; |
---|
| 147 | ++this->entryCount; |
---|
| 148 | } |
---|
| 149 | else |
---|
[4580] | 150 | PRINTF(2)("adding failed, because array has already been finalized\n"); |
---|
[4579] | 151 | } |
---|
[2776] | 152 | |
---|
[4579] | 153 | /** |
---|
[4836] | 154 | * Adds 3 entries at once (convenience) |
---|
[4579] | 155 | */ |
---|
| 156 | template<class T> |
---|
| 157 | void Array<T>::addEntry (T entry0, T entry1, T entry2) |
---|
| 158 | { |
---|
| 159 | this->addEntry(entry0); |
---|
| 160 | this->addEntry(entry1); |
---|
| 161 | this->addEntry(entry2); |
---|
| 162 | } |
---|
| 163 | |
---|
[4791] | 164 | |
---|
[4793] | 165 | /** |
---|
[4836] | 166 | * gets back the index of the entry in the array. value check |
---|
| 167 | * @param entry: the entry to look up |
---|
| 168 | * @returns the index in the array, -1 if not found |
---|
[4793] | 169 | */ |
---|
[4791] | 170 | template<class T> |
---|
| 171 | int Array<T>::getIndex(T* entry) const |
---|
| 172 | { |
---|
[4792] | 173 | if( unlikely(this->finalized == false)) |
---|
| 174 | return -1; |
---|
[4791] | 175 | |
---|
[4792] | 176 | for(int i = 0; i < this->entryCount; ++i) |
---|
| 177 | { |
---|
[4793] | 178 | if( unlikely(*entry == this->array[i])) |
---|
[4792] | 179 | return i; |
---|
| 180 | } |
---|
[4791] | 181 | } |
---|
| 182 | |
---|
| 183 | |
---|
[4579] | 184 | /** |
---|
[4836] | 185 | * Simple debug info about the Array |
---|
[4579] | 186 | */ |
---|
| 187 | template<class T> |
---|
[4746] | 188 | void Array<T>::debug () const |
---|
[4579] | 189 | { |
---|
| 190 | PRINT(0)("entryCount=%i, address=%p\n", this->entryCount, this->array); |
---|
| 191 | } |
---|
[4791] | 192 | |
---|
[2776] | 193 | #endif |
---|