Ignore:
Timestamp:
May 7, 2008, 1:38:13 PM (17 years ago)
Author:
Frederik Heber <heber@…>
Children:
a01955
Parents:
417bb5
Message:

AtomStackClass -> template <typename T> StackClass<T> change in new file stackclass.hpp, other templates to helpers.hpp

StackClass was changed to a template and as template code may only be present in header file was moved to a new
header file stackclass.hpp. New file was added to Makefile.am
Other templates (list management et al) were moved to helpers.hpp as they are more sensibly placed there.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • molecuilder/src/atom.cpp

    r417bb5 rd50d2a  
    106106};
    107107
    108 /******************************** Functions for class AtomStackClass ********************************/
    109 
    110 /** Constructor of class AtomStackClass.
    111  */
    112 AtomStackClass::AtomStackClass(int dimension)
    113 {
    114   CurrentLastEntry = 0;
    115   CurrentFirstEntry = 0;
    116   NextFreeField = 0;
    117   EntryCount = dimension;
    118   StackList = (atom **) Malloc(sizeof(atom *)*EntryCount, "AtomStackClass::AtomStackClass: **StackList");
    119 };
    120 
    121 /** Destructor of class AtomStackClass.
    122  */
    123 AtomStackClass::~AtomStackClass()
    124 {
    125   Free((void **)&StackList, "AtomStackClass::AtomStackClass: **StackList");
    126 };
    127 
    128 /** Pushes an object onto the stack.
    129  * \param *object atom to be pushed on stack
    130  * \return true - sucess, false - failure, meaning stack field was occupied
    131  */
    132 bool AtomStackClass::Push(atom *object)
    133 {
    134   if (!IsFull()) {    // check whether free field is really not occupied
    135     StackList[NextFreeField] = object;
    136     CurrentLastEntry = NextFreeField;
    137     NextFreeField = (NextFreeField + 1) % EntryCount; // step on to next free field
    138     return true;
    139   } else {
    140     cerr << "ERROR: Stack is full, " << "Stack: CurrentLastEntry " << CurrentLastEntry<< "\tCurrentFirstEntry " << CurrentFirstEntry << "\tNextFreeField " << NextFreeField << "\tEntryCount " << EntryCount << "!" << endl;
    141     return false;
    142   }
    143 };
    144 
    145 /** Pops first/oldest atom from stack.
    146  * First in, last out.
    147  * \return atom pointer from stack, NULL - if failure (no atom pointer in field)
    148  */
    149 atom *AtomStackClass::PopFirst()
    150 {
    151   atom *Walker = NULL;
    152   if (!IsEmpty()) {
    153     Walker = StackList[CurrentFirstEntry];
    154     if (Walker == NULL)
    155       cerr << "ERROR: Stack's field is empty!" << endl;
    156     StackList[CurrentFirstEntry] = NULL;
    157     if (CurrentFirstEntry != CurrentLastEntry) { // hasn't last item been popped as well?
    158       CurrentFirstEntry = (CurrentFirstEntry + 1) % EntryCount; // step back from current free field to last used (somehow modulo does not work in -1)
    159     } else {
    160       CurrentFirstEntry = (CurrentFirstEntry + 1) % EntryCount; // step back from current free field to last used (somehow modulo does not work in -1)
    161       CurrentLastEntry = CurrentFirstEntry;
    162     }
    163   } else
    164     cerr << "ERROR: Stack is empty!" << endl;
    165   return Walker;
    166 };
    167 
    168 /** Pops last atom from stack.
    169  * First in, first out.
    170  * \return atom pointer from stack, NULL - if failure (no atom pointer in field)
    171  */
    172 atom *AtomStackClass::PopLast()
    173 {
    174   atom *Walker = NULL;
    175   if (!IsEmpty()) {
    176     Walker = StackList[CurrentLastEntry];
    177     StackList[CurrentLastEntry] = NULL;
    178     if (Walker == NULL)
    179       cerr << "ERROR: Stack's field is empty!" << endl;
    180     NextFreeField = CurrentLastEntry;
    181     if (CurrentLastEntry != CurrentFirstEntry)  // has there been more than one item on stack
    182       CurrentLastEntry = (CurrentLastEntry + (EntryCount-1)) % EntryCount; // step back from current free field to last (modulo does not work in -1, thus go EntryCount-1 instead)
    183   } else {
    184     cerr << "ERROR: Stack is empty!" << endl;
    185   }
    186   return Walker;
    187 };
    188 
    189 /** Removes a certain item from the stack.
    190  * Item is seeked between \a CurrentFirstEntry and \a CurrentLastEntry, if found, place in stack is NULL'd and
    191  * all subsequent items shifted by one position downward (with wrapping taken into account).
    192  * \param *ptr adress of item
    193  * \return true - item was removed, false - item was not found
    194  */
    195 bool AtomStackClass::RemoveItem(atom *ptr)
    196 {
    197   bool found = false;
    198   cout << Verbose(5) << "First " << CurrentFirstEntry<< "\tLast " << CurrentLastEntry<< "\tNext " << NextFreeField<< "\tCount " << EntryCount<< "." << endl;
    199   int i=CurrentFirstEntry;
    200   if (!IsEmpty())
    201     do {
    202       if (StackList[i] == ptr) {  // if item found, remove
    203         cout << Verbose(5) << "Item " << *ptr << " was number " << i << " on stack, removing it." << endl;
    204         found = true;
    205         StackList[i] = NULL;
    206       }
    207       if ((found) && (StackList[i] != NULL)) {  // means we have to shift (and not the removed item)
    208         if (i == 0) { // we are down to first item in stack, have to put onto last item
    209           cout << Verbose(5) << "Shifting item 0 to place " << EntryCount-1 << "." << endl;
    210           StackList[EntryCount-1] = StackList[0];
    211         } else {
    212           cout << Verbose(5) << "Shifting item " << i << " to place " << i-1 << "." << endl;
    213           StackList[i-1] = StackList[i];
    214         }
    215       }
    216       i=((i + 1) % EntryCount); // step on
    217     } while (i!=NextFreeField);
    218   else
    219     cerr << "ERROR: Stack is already empty!" << endl;
    220   if (found) {
    221     NextFreeField = CurrentLastEntry;
    222     if (CurrentLastEntry != CurrentFirstEntry)  // has there been more than one item on stack
    223       CurrentLastEntry = (CurrentLastEntry + (EntryCount-1)) % EntryCount;
    224   }
    225   return found;
    226 };
    227 
    228 /** Test the functionality of the stack.
    229  * \param *out ofstream for debugging
    230  * \param *test one item to put on stack 
    231  * \return true - all tests worked correctly
    232  */
    233 void AtomStackClass::TestImplementation(ofstream *out, atom *test)
    234 {
    235   atom *Walker = test;
    236   *out << Verbose(1) << "Testing the snake stack..." << endl;
    237   for (int i=0;i<EntryCount;i++) {
    238     *out << Verbose(2) << "Filling " << i << "th element of stack." << endl;
    239     Push(Walker);
    240     Walker=Walker->next;
    241   }
    242   *out << endl;
    243   Output(out);
    244   if (IsFull())
    245     *out << "Stack is full as supposed to be!" << endl;
    246   else
    247     *out << "ERROR: Stack is not as full as supposed to be!" << endl;
    248   //if (StackList[(EntryCount+1)/2] != NULL) {
    249     *out << "Removing element in the middle ..." << endl;
    250     RemoveItem(StackList[(EntryCount+1)/2]);
    251     Output(out);
    252   //}
    253   //if (StackList[CurrentFirstEntry] != NULL) {
    254     *out << "Removing first element  ..." << endl;
    255     RemoveItem(StackList[CurrentFirstEntry]);
    256     Output(out);
    257   //}
    258   //if (StackList[CurrentLastEntry] != NULL) {
    259     *out << "Removing last element ..." << endl;
    260     RemoveItem(StackList[CurrentLastEntry]);
    261     Output(out);
    262   //}
    263   *out << "Clearing stack ... " << endl; 
    264   ClearStack();
    265   Output(out);
    266   if (IsEmpty())
    267     *out << "Stack is empty as supposed to be!" << endl;
    268   else
    269     *out << "ERROR: Stack is not as empty as supposed to be!" << endl;
    270   *out << "done." << endl;
    271 };
    272 
    273 /** Puts contents of stack into ofstream \a *out.
    274  * \param *out ofstream for output
    275  */
    276 void AtomStackClass::Output(ofstream *out) const
    277 {
    278   *out << "Contents of Stack: ";
    279   for(int i=0;i<EntryCount;i++) {
    280     *out << "\t";
    281     if (i == CurrentFirstEntry)
    282       *out << " 1";
    283     if  (i == CurrentLastEntry)
    284       *out << " "<< EntryCount;
    285     if (i ==  NextFreeField)
    286       *out << " F";
    287     *out << ": " << StackList[i];
    288   }
    289   *out << endl;
    290 };
    291 
    292 /** Checks whether stack is empty.
    293  * Simply checks whether AtomStackClass::NextFreeField is equal to AtomStackClass::CurrentFirstEntry and
    294  * AtomStackClass::CurrentFirstEntry is equal to AtomStackClass::CurrentLastEntry.
    295  * \return true - empty, false - not
    296  */
    297 bool AtomStackClass::IsEmpty()
    298 {
    299   return((NextFreeField == CurrentFirstEntry) && (CurrentLastEntry == CurrentFirstEntry));
    300 };
    301 
    302 /** Checks whether stack is full.
    303  * Simply checks whether AtomStackClass::NextFreeField is equal to AtomStackClass::CurrentFirstEntry and
    304  * AtomStackClass::CurrentFirstEntry is _not_ equal to AtomStackClass::CurrentLastEntry.
    305  * \return true - full, false - not
    306  */
    307 bool AtomStackClass::IsFull()
    308 {
    309   return((NextFreeField == CurrentFirstEntry) && (CurrentLastEntry != CurrentFirstEntry));
    310 };
    311 
    312 /** Returns number of items on stack.
    313  * Simply returns difference between AtomStackClass::Stacklist entry AtomStackClass::CurrentEntry-1.
    314  * \return number of items on stack
    315  * \warning will never return correct item count if stack is full, i.e. count would be AtomStackClass::EntryCount.
    316  */
    317 int AtomStackClass::ItemCount()
    318 {
    319   //cout << "Stack: CurrentLastEntry " << CurrentLastEntry<< "\tCurrentFirstEntry " << CurrentFirstEntry << "\tEntryCount " << EntryCount << "." << endl;
    320   return((NextFreeField + (EntryCount - CurrentFirstEntry)) % EntryCount);
    321 };
    322 
    323 /** Clears the stack from all atoms.
    324  * \return true - sucess, false - failure
    325  */
    326 void AtomStackClass::ClearStack()
    327 {
    328   for(int i=0;i<EntryCount; i++)
    329     StackList[i] = NULL;
    330   CurrentFirstEntry = 0;
    331   CurrentLastEntry = 0;
    332   NextFreeField = 0;
    333 };
    334 
Note: See TracChangeset for help on using the changeset viewer.