| [6d35e4] | 1 | #ifndef STACKCLASS_HPP_ | 
|---|
|  | 2 | #define STACKCLASS_HPP_ | 
|---|
|  | 3 |  | 
|---|
|  | 4 | template <typename T> class StackClass; | 
|---|
|  | 5 |  | 
|---|
|  | 6 | /******************************** Functions for class StackClass ********************************/ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | /* Stack of Stuff. | 
|---|
|  | 9 | * Is used during DepthFirstSearchAnalysis() to detect nonseparable components. | 
|---|
|  | 10 | */ | 
|---|
|  | 11 | template <typename T> class StackClass { | 
|---|
| [6ac7ee] | 12 | public: | 
|---|
|  | 13 | StackClass<T>(int dimension); | 
|---|
|  | 14 | ~StackClass<T>(); | 
|---|
|  | 15 |  | 
|---|
|  | 16 | bool Push(T object); | 
|---|
|  | 17 | T PopFirst(); | 
|---|
|  | 18 | T PopLast(); | 
|---|
|  | 19 | bool RemoveItem(T ptr); | 
|---|
|  | 20 | void ClearStack(); | 
|---|
|  | 21 | bool IsEmpty(); | 
|---|
|  | 22 | bool IsFull(); | 
|---|
|  | 23 | int ItemCount(); | 
|---|
|  | 24 | void Output(ofstream *out) const; | 
|---|
|  | 25 | void TestImplementation(ofstream *out, T test); | 
|---|
|  | 26 |  | 
|---|
|  | 27 | private: | 
|---|
|  | 28 | T *StackList;    //!< the list containing the atom pointers | 
|---|
|  | 29 | int EntryCount;          //!< number of entries in the stack | 
|---|
|  | 30 | int CurrentLastEntry;    //!< Current last entry (newest item on stack) | 
|---|
|  | 31 | int CurrentFirstEntry;   //!< Current first entry (oldest item on stack) | 
|---|
|  | 32 | int NextFreeField;                       //!< Current index of next free field | 
|---|
| [6d35e4] | 33 | }; | 
|---|
|  | 34 |  | 
|---|
|  | 35 | /** Constructor of class StackClass. | 
|---|
|  | 36 | */ | 
|---|
|  | 37 | template <typename T> StackClass<T>::StackClass(int dimension) | 
|---|
|  | 38 | { | 
|---|
| [6ac7ee] | 39 | CurrentLastEntry = 0; | 
|---|
|  | 40 | CurrentFirstEntry = 0; | 
|---|
|  | 41 | NextFreeField = 0; | 
|---|
|  | 42 | EntryCount = dimension; | 
|---|
|  | 43 | StackList = (T *) Malloc(sizeof(T)*EntryCount, "StackClass::StackClass: **StackList"); | 
|---|
| [6d35e4] | 44 | }; | 
|---|
|  | 45 |  | 
|---|
|  | 46 | /** Destructor of class StackClass. | 
|---|
|  | 47 | */ | 
|---|
|  | 48 | template <typename T> StackClass<T>::~StackClass() | 
|---|
|  | 49 | { | 
|---|
| [6ac7ee] | 50 | Free((void **)&StackList, "StackClass::StackClass: **StackList"); | 
|---|
| [6d35e4] | 51 | }; | 
|---|
|  | 52 |  | 
|---|
|  | 53 | /** Pushes an object onto the stack. | 
|---|
|  | 54 | * \param *object atom to be pushed on stack | 
|---|
|  | 55 | * \return true - sucess, false - failure, meaning stack field was occupied | 
|---|
|  | 56 | */ | 
|---|
|  | 57 | template <typename T> bool StackClass<T>::Push(T object) | 
|---|
|  | 58 | { | 
|---|
| [6ac7ee] | 59 | if (!IsFull()) {                // check whether free field is really not occupied | 
|---|
|  | 60 | StackList[NextFreeField] = object; | 
|---|
|  | 61 | CurrentLastEntry = NextFreeField; | 
|---|
|  | 62 | NextFreeField = (NextFreeField + 1) % EntryCount; // step on to next free field | 
|---|
|  | 63 | return true; | 
|---|
|  | 64 | } else { | 
|---|
|  | 65 | cerr << "ERROR: Stack is full, " << "Stack: CurrentLastEntry " << CurrentLastEntry<< "\tCurrentFirstEntry " << CurrentFirstEntry << "\tNextFreeField " << NextFreeField << "\tEntryCount " << EntryCount << "!" << endl; | 
|---|
|  | 66 | return false; | 
|---|
|  | 67 | } | 
|---|
| [6d35e4] | 68 | }; | 
|---|
|  | 69 |  | 
|---|
|  | 70 | /** Pops first/oldest atom from stack. | 
|---|
|  | 71 | * First in, last out. | 
|---|
|  | 72 | * \return atom pointer from stack, NULL - if failure (no atom pointer in field) | 
|---|
|  | 73 | */ | 
|---|
|  | 74 | template <typename T> T StackClass<T>::PopFirst() | 
|---|
|  | 75 | { | 
|---|
| [6ac7ee] | 76 | T Walker = NULL; | 
|---|
|  | 77 | if (!IsEmpty()) { | 
|---|
|  | 78 | Walker = StackList[CurrentFirstEntry]; | 
|---|
|  | 79 | if (Walker == NULL) | 
|---|
|  | 80 | cerr << "ERROR: Stack's field is empty!" << endl; | 
|---|
|  | 81 | StackList[CurrentFirstEntry] = NULL; | 
|---|
|  | 82 | if (CurrentFirstEntry != CurrentLastEntry) { // hasn't last item been popped as well? | 
|---|
|  | 83 | CurrentFirstEntry = (CurrentFirstEntry + 1) % EntryCount; // step back from current free field to last used (somehow modulo does not work in -1) | 
|---|
|  | 84 | } else { | 
|---|
|  | 85 | CurrentFirstEntry = (CurrentFirstEntry + 1) % EntryCount; // step back from current free field to last used (somehow modulo does not work in -1) | 
|---|
|  | 86 | CurrentLastEntry = CurrentFirstEntry; | 
|---|
|  | 87 | } | 
|---|
|  | 88 | } else | 
|---|
|  | 89 | cerr << "ERROR: Stack is empty!" << endl; | 
|---|
|  | 90 | return Walker; | 
|---|
| [6d35e4] | 91 | }; | 
|---|
|  | 92 |  | 
|---|
|  | 93 | /** Pops last element from stack. | 
|---|
|  | 94 | * First in, first out. | 
|---|
|  | 95 | * \return element pointer from stack, NULL - if failure (no atom pointer in field) | 
|---|
|  | 96 | */ | 
|---|
|  | 97 | template <typename T> T StackClass<T>::PopLast() | 
|---|
|  | 98 | { | 
|---|
| [6ac7ee] | 99 | T Walker = NULL; | 
|---|
|  | 100 | if (!IsEmpty()) { | 
|---|
|  | 101 | Walker = StackList[CurrentLastEntry]; | 
|---|
|  | 102 | StackList[CurrentLastEntry] = NULL; | 
|---|
|  | 103 | if (Walker == NULL) | 
|---|
|  | 104 | cerr << "ERROR: Stack's field is empty!" << endl; | 
|---|
|  | 105 | NextFreeField = CurrentLastEntry; | 
|---|
|  | 106 | if (CurrentLastEntry != CurrentFirstEntry)      // has there been more than one item on stack | 
|---|
|  | 107 | CurrentLastEntry = (CurrentLastEntry + (EntryCount-1)) % EntryCount; // step back from current free field to last (modulo does not work in -1, thus go EntryCount-1 instead) | 
|---|
|  | 108 | } else { | 
|---|
|  | 109 | cerr << "ERROR: Stack is empty!" << endl; | 
|---|
|  | 110 | } | 
|---|
|  | 111 | return Walker; | 
|---|
| [6d35e4] | 112 | }; | 
|---|
|  | 113 |  | 
|---|
|  | 114 | /** Removes a certain item from the stack. | 
|---|
|  | 115 | * Item is seeked between \a CurrentFirstEntry and \a CurrentLastEntry, if found, place in stack is NULL'd and | 
|---|
|  | 116 | * all subsequent items shifted by one position downward (with wrapping taken into account). | 
|---|
|  | 117 | * \param *ptr adress of item | 
|---|
|  | 118 | * \return true - item was removed, false - item was not found | 
|---|
|  | 119 | */ | 
|---|
|  | 120 | template <typename T> bool StackClass<T>::RemoveItem(T ptr) | 
|---|
|  | 121 | { | 
|---|
| [6ac7ee] | 122 | bool found = false; | 
|---|
|  | 123 | cout << Verbose(5) << "First " << CurrentFirstEntry<< "\tLast " << CurrentLastEntry<< "\tNext " << NextFreeField<< "\tCount " << EntryCount<< "." << endl; | 
|---|
|  | 124 | int i=CurrentFirstEntry; | 
|---|
|  | 125 | if (!IsEmpty()) | 
|---|
|  | 126 | do { | 
|---|
|  | 127 | if (StackList[i] == ptr) {      // if item found, remove | 
|---|
|  | 128 | cout << Verbose(5) << "Item " << *ptr << " was number " << i << " on stack, removing it." << endl; | 
|---|
|  | 129 | found = true; | 
|---|
|  | 130 | StackList[i] = NULL; | 
|---|
|  | 131 | } | 
|---|
|  | 132 | if ((found) && (StackList[i] != NULL)) {        // means we have to shift (and not the removed item) | 
|---|
|  | 133 | if (i == 0) { // we are down to first item in stack, have to put onto last item | 
|---|
|  | 134 | cout << Verbose(5) << "Shifting item 0 to place " << EntryCount-1 << "." << endl; | 
|---|
|  | 135 | StackList[EntryCount-1] = StackList[0]; | 
|---|
|  | 136 | } else { | 
|---|
|  | 137 | cout << Verbose(5) << "Shifting item " << i << " to place " << i-1 << "." << endl; | 
|---|
|  | 138 | StackList[i-1] = StackList[i]; | 
|---|
|  | 139 | } | 
|---|
|  | 140 | } | 
|---|
|  | 141 | i=((i + 1) % EntryCount); // step on | 
|---|
|  | 142 | } while (i!=NextFreeField); | 
|---|
|  | 143 | else | 
|---|
|  | 144 | cerr << "ERROR: Stack is already empty!" << endl; | 
|---|
|  | 145 | if (found) { | 
|---|
|  | 146 | NextFreeField = CurrentLastEntry; | 
|---|
|  | 147 | if (CurrentLastEntry != CurrentFirstEntry)      // has there been more than one item on stack | 
|---|
|  | 148 | CurrentLastEntry = (CurrentLastEntry + (EntryCount-1)) % EntryCount; | 
|---|
|  | 149 | } | 
|---|
|  | 150 | return found; | 
|---|
| [6d35e4] | 151 | }; | 
|---|
|  | 152 |  | 
|---|
|  | 153 | /** Test the functionality of the stack. | 
|---|
|  | 154 | * \param *out ofstream for debugging | 
|---|
| [6ac7ee] | 155 | * \param *test one item to put on stack | 
|---|
| [6d35e4] | 156 | * \return true - all tests worked correctly | 
|---|
|  | 157 | */ | 
|---|
|  | 158 | template <typename T> void StackClass<T>::TestImplementation(ofstream *out, T test) | 
|---|
|  | 159 | { | 
|---|
| [6ac7ee] | 160 | T Walker = test; | 
|---|
|  | 161 | *out << Verbose(1) << "Testing the snake stack..." << endl; | 
|---|
|  | 162 | for (int i=0;i<EntryCount;i++) { | 
|---|
|  | 163 | *out << Verbose(2) << "Filling " << i << "th element of stack." << endl; | 
|---|
|  | 164 | Push(Walker); | 
|---|
|  | 165 | Walker=Walker->next; | 
|---|
|  | 166 | } | 
|---|
|  | 167 | *out << endl; | 
|---|
|  | 168 | Output(out); | 
|---|
|  | 169 | if (IsFull()) | 
|---|
|  | 170 | *out << "Stack is full as supposed to be!" << endl; | 
|---|
|  | 171 | else | 
|---|
|  | 172 | *out << "ERROR: Stack is not as full as supposed to be!" << endl; | 
|---|
|  | 173 | //if (StackList[(EntryCount+1)/2] != NULL) { | 
|---|
|  | 174 | *out << "Removing element in the middle ..." << endl; | 
|---|
|  | 175 | RemoveItem(StackList[(EntryCount+1)/2]); | 
|---|
|  | 176 | Output(out); | 
|---|
|  | 177 | //} | 
|---|
|  | 178 | //if (StackList[CurrentFirstEntry] != NULL) { | 
|---|
|  | 179 | *out << "Removing first element ..." << endl; | 
|---|
|  | 180 | RemoveItem(StackList[CurrentFirstEntry]); | 
|---|
|  | 181 | Output(out); | 
|---|
|  | 182 | //} | 
|---|
|  | 183 | //if (StackList[CurrentLastEntry] != NULL) { | 
|---|
|  | 184 | *out << "Removing last element ..." << endl; | 
|---|
|  | 185 | RemoveItem(StackList[CurrentLastEntry]); | 
|---|
|  | 186 | Output(out); | 
|---|
|  | 187 | //} | 
|---|
|  | 188 | *out << "Clearing stack ... " << endl; | 
|---|
|  | 189 | ClearStack(); | 
|---|
|  | 190 | Output(out); | 
|---|
|  | 191 | if (IsEmpty()) | 
|---|
|  | 192 | *out << "Stack is empty as supposed to be!" << endl; | 
|---|
|  | 193 | else | 
|---|
|  | 194 | *out << "ERROR: Stack is not as empty as supposed to be!" << endl; | 
|---|
|  | 195 | *out << "done." << endl; | 
|---|
| [6d35e4] | 196 | }; | 
|---|
|  | 197 |  | 
|---|
|  | 198 | /** Puts contents of stack into ofstream \a *out. | 
|---|
|  | 199 | * \param *out ofstream for output | 
|---|
|  | 200 | */ | 
|---|
|  | 201 | template <typename T> void StackClass<T>::Output(ofstream *out) const | 
|---|
|  | 202 | { | 
|---|
| [6ac7ee] | 203 | *out << "Contents of Stack: "; | 
|---|
|  | 204 | for(int i=0;i<EntryCount;i++) { | 
|---|
|  | 205 | *out << "\t"; | 
|---|
|  | 206 | if (i == CurrentFirstEntry) | 
|---|
|  | 207 | *out << " 1"; | 
|---|
|  | 208 | if      (i == CurrentLastEntry) | 
|---|
|  | 209 | *out << " "<< EntryCount; | 
|---|
|  | 210 | if (i ==        NextFreeField) | 
|---|
|  | 211 | *out << " F"; | 
|---|
|  | 212 | *out << ": " << StackList[i]; | 
|---|
|  | 213 | } | 
|---|
|  | 214 | *out << endl; | 
|---|
| [6d35e4] | 215 | }; | 
|---|
|  | 216 |  | 
|---|
|  | 217 | /** Checks whether stack is empty. | 
|---|
|  | 218 | * Simply checks whether StackClass::NextFreeField is equal to StackClass::CurrentFirstEntry and | 
|---|
|  | 219 | * StackClass::CurrentFirstEntry is equal to StackClass::CurrentLastEntry. | 
|---|
|  | 220 | * \return true - empty, false - not | 
|---|
|  | 221 | */ | 
|---|
|  | 222 | template <typename T> bool StackClass<T>::IsEmpty() | 
|---|
|  | 223 | { | 
|---|
| [6ac7ee] | 224 | return((NextFreeField == CurrentFirstEntry) && (CurrentLastEntry == CurrentFirstEntry)); | 
|---|
| [6d35e4] | 225 | }; | 
|---|
|  | 226 |  | 
|---|
|  | 227 | /** Checks whether stack is full. | 
|---|
|  | 228 | * Simply checks whether StackClass::NextFreeField is equal to StackClass::CurrentFirstEntry and | 
|---|
|  | 229 | * StackClass::CurrentFirstEntry is _not_ equal to StackClass::CurrentLastEntry. | 
|---|
|  | 230 | * \return true - full, false - not | 
|---|
|  | 231 | */ | 
|---|
|  | 232 | template <typename T> bool StackClass<T>::IsFull() | 
|---|
|  | 233 | { | 
|---|
| [6ac7ee] | 234 | return((NextFreeField == CurrentFirstEntry) && (CurrentLastEntry != CurrentFirstEntry)); | 
|---|
| [6d35e4] | 235 | }; | 
|---|
|  | 236 |  | 
|---|
|  | 237 | /** Returns number of items on stack. | 
|---|
|  | 238 | * Simply returns difference between StackClass::Stacklist entry StackClass::CurrentEntry-1. | 
|---|
|  | 239 | * \return number of items on stack | 
|---|
|  | 240 | * \warning will never return correct item count if stack is full, i.e. count would be StackClass::EntryCount. | 
|---|
|  | 241 | */ | 
|---|
|  | 242 | template <typename T> int StackClass<T>::ItemCount() | 
|---|
|  | 243 | { | 
|---|
| [6ac7ee] | 244 | //cout << "Stack: CurrentLastEntry " << CurrentLastEntry<< "\tCurrentFirstEntry " << CurrentFirstEntry << "\tEntryCount " << EntryCount << "." << endl; | 
|---|
|  | 245 | return((NextFreeField + (EntryCount - CurrentFirstEntry)) % EntryCount); | 
|---|
| [6d35e4] | 246 | }; | 
|---|
|  | 247 |  | 
|---|
|  | 248 | /** Clears the stack from all atoms. | 
|---|
|  | 249 | * \return true - sucess, false - failure | 
|---|
|  | 250 | */ | 
|---|
|  | 251 | template <typename T> void StackClass<T>::ClearStack() | 
|---|
|  | 252 | { | 
|---|
| [6ac7ee] | 253 | for(int i=EntryCount; i--;) | 
|---|
|  | 254 | StackList[i] = NULL; | 
|---|
|  | 255 | CurrentFirstEntry = 0; | 
|---|
|  | 256 | CurrentLastEntry = 0; | 
|---|
|  | 257 | NextFreeField = 0; | 
|---|
| [6d35e4] | 258 | }; | 
|---|
|  | 259 |  | 
|---|
|  | 260 |  | 
|---|
|  | 261 |  | 
|---|
|  | 262 | #endif /*STACKCLASS_HPP_*/ | 
|---|