Changeset 5388 in orxonox.OLD for trunk/src/lib/util
- Timestamp:
- Oct 16, 2005, 2:05:26 AM (19 years ago)
- Location:
- trunk/src/lib/util
- Files:
-
- 1 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/util/array.h
r5321 r5388 24 24 #define _ARRAY_H 25 25 #include "debug.h" 26 27 using namespace std; 26 28 27 29 //! Array Class that handles dynamic-type arrays. -
trunk/src/lib/util/t_stack.h
r5382 r5388 15 15 16 16 /*! 17 @file array.h18 @brief Contains the Array Class that handles arrays of classes.17 @file t_stack.h 18 @brief Contains the tStack Class that handles stacks of classes. 19 19 this class creates a Array of a semi-Dynamic length. 20 20 beware, that after finalizing the array may not be resized again. 21 21 */ 22 22 23 #ifndef _ARRAY_H 24 #define _ARRAY_H 25 #include "debug.h" 23 #ifndef _T_STACK_H 24 #define _T_STACK_H 26 25 27 //! Array Class that handles dynamic-type arrays. 28 template<class T> class Array 26 using namespace std; 27 28 //! Stack Class that handles dynamic-type Stacks. 29 template<class T> 30 class tStack 29 31 { 30 32 public: 31 Array();32 ~ Array();33 tStack(); 34 ~tStack(); 33 35 34 void finalizeArray (); 35 void addEntry (T entry); 36 void addEntry(T entry0, T entry1, T entry2); 36 void push(T entry); 37 T pop(); 38 T getTop(); 39 /** @returns the Size of the Stack (0 if empty) */ 40 unsigned int getSize() { return this->entryCount; }; 37 41 38 /** @returns The array */39 inline T* getArray () const { return this->array; };40 inline const T getEntry(unsigned int number) const;41 /** * @returns The Count of entries in the Array*/42 inline unsigned int getCount()const { return this->entryCount; };43 inline int getIndex(T* entry) const;44 inline bool isFinalized() const { return this->finalized; }45 42 void debug() const ; 46 43 47 44 private: 48 //! One entry of the Array49 struct Entry45 //! One entry of the Stack 46 struct tStackEntry 50 47 { 51 T value; //!< The value of this Entry.52 Entry*next; //!< Pointer to the Next entry.48 T value; //!< The value of this Entry. 49 tStackEntry* next; //!< Pointer to the Next entry. 53 50 }; 54 51 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. 52 53 unsigned int entryCount; //!< The count of Entries in this Array. 54 tStackEntry* topEntry; //!< Pointer to the first Entry of this Array 60 55 }; 61 56 62 63 57 /** 64 * creates a new Array65 */58 * creates and initializes a Stack 59 */ 66 60 template<class T> 67 Array<T>::Array()61 tStack<T>::tStack() 68 62 { 69 PRINTF(5)("crating new Array\n"); 70 this->firstEntry = new Entry; 71 this->firstEntry->next =NULL; 72 this->currentEntry = this->firstEntry; 73 this->finalized = false; 74 this->entryCount = 0; //0 means one entry 75 } 76 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]; 63 this->topEntry = NULL; 64 this->entryCount = 0; 82 65 } 83 66 84 67 /** 85 * deletes an Array. 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 { 91 PRINTF(5)("deleting array\n"); 92 if (!this->finalized) 93 { 94 Entry* walker = this->firstEntry; 95 Entry* previous; 96 while (walker) 97 { 98 previous = walker; 99 walker = walker->next; 100 delete previous; 101 } 102 } 103 if (this->finalized) 104 delete[] this->array; 105 } 106 107 /** 108 * finalizes an array. 109 This Function creates the array, and makes it ready to be sent to the application. 110 */ 111 template<class T> 112 void Array<T>::finalizeArray () 113 { 114 if (this->finalized) 115 return; 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); 119 Entry* walker = this->firstEntry; 120 for (int i=0; i < this->entryCount; i++) 121 { 122 this->array[i] = walker->value; 123 walker = walker->next; 124 } 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; 134 this->finalized = true; 135 } 136 137 /** 138 * adds a new Entry to the Array 139 * @param entry Entry to add. 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); 147 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 155 PRINTF(2)("adding failed, because array has already been finalized\n"); 156 } 157 158 /** 159 * Adds 3 entries at once (convenience) 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 169 170 /** 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 68 * delocates alocated memory from a Stack. 69 * This does not delete the entries of the Stack 174 70 */ 175 71 template<class T> 176 int Array<T>::getIndex(T* entry) const 72 tStack<T>::~tStack() 177 73 { 178 if( unlikely(this->finalized == false)) 179 return -1; 180 181 for(int i = 0; i < this->entryCount; ++i) 74 tStackEntry* delEntry; 75 while (this->topEntry != NULL) 182 76 { 183 if( unlikely(*entry == this->array[i])) 184 return i; 77 delEntry = this->topEntry; 78 this->topEntry = topEntry->next; 79 delete delEntry; 185 80 } 186 81 } … … 188 83 189 84 /** 190 * Simple debug info about the Array 191 */ 85 * pushes one Entry into the Stack. 86 * @param entry the Entry to push into the Stack 87 */ 192 88 template<class T> 193 void Array<T>::debug () const 89 void tStack<T>::push(T entry) 194 90 { 195 PRINT(0)("entryCount=%i, address=%p\n", this->entryCount, this->array); 91 tStackEntry* newEntry = new tStackEntry; 92 newEntry->value = entry; 93 newEntry->next = this->topEntry; 94 this->topEntry = newEntry; 95 96 this->entryCount++; 196 97 } 197 98 198 #endif 99 100 /** 101 * pops up the topmost enrty of the Stack, and moves the pointer to the next Stack-entry. 102 * @returns the top-most enrty. 103 */ 104 template<class T> 105 T tStack<T>::pop() 106 { 107 if (this->topEntry == NULL) 108 return 0; 109 110 tStackEntry* retEntry = this->topEntry; 111 T retVal = retEntry->value; 112 this->topEntry = this->topEntry->next; 113 delete retEntry; 114 this->entryCount--; 115 return retVal; 116 } 117 118 /** 119 * @returns the topMost entry of the Stack 120 */ 121 template<class T> 122 T tStack<T>::getTop() 123 { 124 if (this->topEntry != NULL) 125 return this->topEntry->value; 126 else 127 return NULL; 128 } 129 130 #endif /* _T_STACK_H */
Note: See TracChangeset
for help on using the changeset viewer.