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