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