Changeset 6d35e4


Ignore:
Timestamp:
May 7, 2008, 1:38:13 PM (17 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
8d9c38
Parents:
db066d
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.

Location:
src
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • src/Makefile.am

    rdb066d r6d35e4  
    11SOURCE = atom.cpp bond.cpp builder.cpp config.cpp element.cpp helpers.cpp molecules.cpp moleculelist.cpp periodentafel.cpp vector.cpp verbose.cpp
    2 HEADER = defs.hpp helpers.hpp molecules.hpp
     2HEADER = defs.hpp helpers.hpp molecules.hpp stackclass.hpp
    33
    44bin_PROGRAMS = molecuilder joiner analyzer
  • src/atom.cpp

    rdb066d r6d35e4  
    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 
  • src/helpers.hpp

    rdb066d r6d35e4  
    5959char *FixedDigitNumber(const int FragmentNumber, const int digits);
    6060
    61 /********************************************** helpful structures *********************************/
     61/********************************************** helpful template functions *********************************/
     62
     63/******************************** Some templates for list management ***********************************/
     64
     65/** Adds linking of an item to a list.
     66 * \param *walker
     67 * \return true - adding succeeded, false - error in list
     68 */
     69template <typename X> void link(X *walker, X *end)
     70{
     71  X *vorher = end->previous;
     72  if (vorher != NULL)
     73    vorher->next = walker;
     74  end->previous = walker;
     75  walker->previous = vorher;
     76  walker->next = end;
     77};
     78
     79/** Removes linking of an item in a list.
     80 * \param *walker
     81 * \return true - removing succeeded, false - given item not found in list
     82 */
     83template <typename X> void unlink(X *walker)
     84{
     85  if (walker->next != NULL)
     86    walker->next->previous = walker->previous;
     87  if (walker->previous != NULL)
     88    walker->previous->next = walker->next;
     89};
     90
     91/** Adds new item before an item \a *end in a list.
     92 * \param *pointer   item to be added
     93 * \param *end  end of list
     94 * \return true - addition succeeded, false - unable to add item to list
     95 */
     96template <typename X>  bool add(X *pointer, X *end)
     97{
     98  if (end != NULL) {
     99    link(pointer, end);
     100  } else {
     101    pointer->previous = NULL;
     102    pointer->next = NULL;
     103  }
     104  return true;
     105};
     106
     107/** Finds item in list
     108 * \param *suche  search criteria
     109 * \param *start  begin of list
     110 * \param *end  end of list
     111 * \return X - if found, NULL - if not found
     112 */
     113template <typename X, typename Y> X * find(Y *suche, X *start, X *end)
     114{
     115  X *walker = start;
     116  while (walker->next != end) { // go through list
     117    walker = walker->next; // step onward beforehand
     118    if (*walker->sort == *suche) return (walker);
     119  }
     120  return NULL;
     121};
     122
     123/** Removes an item from the list without check.
     124 * \param *walker item to be removed
     125 * \return true - removing succeeded, false - given item not found in list
     126 */
     127template <typename X> void removewithoutcheck(X *walker)
     128{
     129  if (walker != NULL) {
     130    unlink(walker);
     131    delete(walker);
     132    walker = NULL;
     133  }
     134};
     135
     136/** Removes an item from the list, checks if exists.
     137 * Checks beforehand if atom is really within molecule list.
     138 * \param *pointer   item to be removed
     139 * \param *start  begin of list
     140 * \param *end  end of list
     141 * \return true - removing succeeded, false - given item not found in list
     142 */
     143template <typename X> bool remove(X *pointer, X *start, X *end)
     144{
     145  X *walker = find (pointer->sort, start, end);
     146/*  while (walker->next != pointer) { // search through list
     147    walker = walker->next;
     148    if (walker == end) return false;  // item not found in list
     149  }*/
     150  // atom found, now unlink
     151  if (walker != NULL)
     152    removewithoutcheck(walker);
     153  else
     154    return false;
     155  return true;
     156};
     157
     158/** Cleans the whole list.
     159 * \param *start begin of list
     160 * \param *end end of list
     161 * \return true - list was cleaned successfully, false - error in list structure
     162 */
     163template <typename X> bool cleanup(X *start, X *end)
     164{
     165  X *pointer = start->next;
     166  X *walker;
     167  while (pointer != end) { // go through list
     168    walker = pointer; // mark current
     169    pointer = pointer->next; // step onward beforehand
     170    // remove walker
     171    unlink(walker);
     172    delete(walker);
     173    walker = NULL;
     174  }
     175  return true;
     176};
     177
     178/** Returns the first marker in a chain list.
     179 * \param *me one arbitrary item in chain list
     180 * \return poiner to first marker
     181 */
     182template <typename X> X *GetFirst(X *me)
     183{
     184  X *Binder = me;
     185  while(Binder->previous != NULL)
     186    Binder = Binder->previous;
     187  return Binder;
     188};
     189
     190/** Returns the last marker in a chain list.
     191 * \param *me one arbitrary item in chain list
     192 * \return poiner to last marker
     193 */
     194template <typename X> X *GetLast(X *me)
     195{
     196  X *Binder = me;
     197  while(Binder->next != NULL)
     198    Binder = Binder->next;
     199  return Binder;
     200};
     201
     202/** Frees a two-dimensional array.
     203 * \param *ptr pointer to array
     204 * \param dim first dim of array
     205 */
     206template <typename X> void Free2DArray(X **ptr, int dim)
     207{
     208  int i;
     209  if (ptr != NULL) {
     210    for(i=0;i<dim;i++)
     211      if (ptr[i] != NULL)
     212        free(ptr[i]);
     213    free(ptr);
     214  }
     215};
    62216
    63217/************************************* Class Verbose & Binary *******************************/
  • src/molecules.cpp

    rdb066d r6d35e4  
    14191419MoleculeLeafClass * molecule::DepthFirstSearchAnalysis(ofstream *out, bool ReturnStack, int &MinimumRingSize)
    14201420{
    1421   AtomStackClass *AtomStack = new AtomStackClass(AtomCount);
     1421  class StackClass<atom *> *AtomStack;
     1422  AtomStack = new StackClass<atom *>(AtomCount);
     1423  class StackClass<bond *> *BackEdgeStack = new StackClass<bond *> (BondCount);
    14221424  MoleculeLeafClass *SubGraphs = new MoleculeLeafClass(NULL);
    14231425  MoleculeLeafClass *LeafWalker = SubGraphs;
    14241426  int CurrentGraphNr = 0, OldGraphNr;
    1425   int CyclicBonds;
    14261427  int ComponentNumber = 0;
    14271428  atom *Walker = NULL, *OtherAtom = NULL, *Root = start->next;
     
    14341435  ResetAllAtomNumbers();
    14351436  InitComponentNumbers();
     1437  BackEdgeStack->ClearStack();
    14361438  while (Root != end) { // if there any atoms at all
    14371439    // (1) mark all edges unused, empty stack, set atom->GraphNr = 0 for all
     
    14681470            // (4a) ... if "other" atom has been visited (GraphNr != 0), set lowpoint to minimum of both, go to (3)
    14691471            Binder->Type = BackEdge;
     1472            BackEdgeStack->Push(Binder);
    14701473            Walker->LowpointNr = ( Walker->LowpointNr < OtherAtom->GraphNr ) ? Walker->LowpointNr : OtherAtom->GraphNr;
    14711474            *out << Verbose(3) << "(4a) Visited: Setting Lowpoint of Walker[" << Walker->Name << "] to " << Walker->LowpointNr << "." << endl;
     
    15681571  }
    15691572
    1570   // correct cyclic bonds that are not included in "same LP" argument
    1571   Binder = first;
    1572   while (Binder->next != last) {
    1573     Binder = Binder->next;
    1574     Walker = Binder->leftatom;
    1575     OtherAtom = Binder->rightatom;
    1576     // now check whether both have a cyclic bond in their list
    1577     CyclicBonds = 0;   // counts cyclic bonds;
    1578     for(int i=0;i<NumberOfBondsPerAtom[Walker->nr];i++)
    1579       if ((CyclicBonds == 0) && (ListOfBondsPerAtom[Walker->nr][i]->Cyclic))
    1580         CyclicBonds = 1; 
    1581     for(int i=0;i<NumberOfBondsPerAtom[OtherAtom->nr];i++)
    1582       if ((CyclicBonds == 1) && (ListOfBondsPerAtom[OtherAtom->nr][i]->Cyclic))
    1583         CyclicBonds = 2; 
    1584     Binder->Cyclic = (Binder->Cyclic) || (CyclicBonds == 2); // set the Cyclic criterium either or ...
    1585   }
    1586  
    1587   // further analysis of the found cycles (print rings, get minimum cycle length)
     1573  // analysis of the cycles (print rings, get minimum cycle length)
    15881574  CyclicStructureAnalysis(out, MinimumRingSize);
     1575
    15891576  *out << Verbose(1) << "Final graph info for each atom is:" << endl;
    15901577  Walker = start;
     
    16241611void molecule::CyclicStructureAnalysis(ofstream *out, int &MinimumRingSize)
    16251612{
    1626   AtomStackClass *AtomStack = new AtomStackClass(AtomCount);
     1613  class StackClass<atom *> *AtomStack = new StackClass<atom *>(AtomCount);
    16271614  int LP;
    16281615  atom *Walker = NULL, *OtherAtom = NULL, *Root = NULL, *Runner = NULL;
     
    16301617  int RingSize, NumCycles;
    16311618
    1632   // go through every atom
     1619  // count number of cyclic bonds per atom and push all with 3 cyclic bonds onto stack
    16331620  AtomStack->ClearStack();
    16341621  int *NoCyclicBondsPerAtom = (int *) Malloc(sizeof(int)*AtomCount, "molecule::CyclicStructureAnalysis: *NoCyclicBondsPerAtom");
     
    20652052  line << path << "/" << FRAGMENTPREFIX << ADJACENCYFILE;
    20662053  File.open(line.str().c_str(), ios::out);
    2067   *out << Verbose(1) << "Looking at bond structure stored in adjacency file and comparing to present one ... " << endl;
     2054  *out << Verbose(1) << "Looking at bond structure stored in adjacency file and comparing to present one ... ";
    20682055  if (File != NULL) {
    20692056    // allocate storage structure
     
    21192106    status = false;
    21202107  }
     2108  *out << endl;
    21212109  Free((void **)&buffer, "molecule::CheckAdjacencyFileAgainstMolecule: *buffer");
    21222110 
     
    26202608
    26212609/** Adds atoms up to \a BondCount distance from \a *Root and notes them down in \a **AddedAtomList.
    2622  * Gray vertices are always enqueued in an AtomStackClass FIFO queue, the rest is usual BFS with adding vertices found was
     2610 * Gray vertices are always enqueued in an StackClass<atom *> FIFO queue, the rest is usual BFS with adding vertices found was
    26232611 * white and putting into queue.
    26242612 * \param *out output stream for debugging
     
    26362624  int *ShortestPathList = (int *) Malloc(sizeof(int)*AtomCount, "molecule::BreadthFirstSearchAdd: *ShortestPathList");
    26372625  enum Shading *ColorList = (enum Shading *) Malloc(sizeof(enum Shading)*AtomCount, "molecule::BreadthFirstSearchAdd: *ColorList");
    2638   AtomStackClass *AtomStack = new AtomStackClass(AtomCount);
     2626  class StackClass<atom *> *AtomStack = new StackClass<atom *>(AtomCount);
    26392627  atom *Walker = NULL, *OtherAtom = NULL;
    26402628  bond *Binder = NULL;
     
    28022790
    28032791
    2804 /** Looks through a AtomStackClass and returns the likeliest removal candiate.
     2792/** Looks through a StackClass<atom *> and returns the likeliest removal candiate.
    28052793 * \param *out output stream for debugging messages
    28062794 * \param *&Leaf KeySet to look through
     
    29222910  enum Shading *ColorVertexList = (enum Shading *) Malloc(sizeof(enum Shading)*AtomCount, "molecule::CreateListOfUniqueFragmentsOfOrder: *ColorList");
    29232911  enum Shading *ColorEdgeList = (enum Shading *) Malloc(sizeof(enum Shading)*BondCount, "molecule::CreateListOfUniqueFragmentsOfOrder: *ColorBondList");
    2924   AtomStackClass *RootStack = new AtomStackClass(AtomCount);
    2925   AtomStackClass *TouchedStack = new AtomStackClass((int)pow(4,Order)+2); // number of atoms reached from one with maximal 4 bonds plus Root itself
    2926   AtomStackClass *SnakeStack = new AtomStackClass(Order+1); // equal to Order is not possible, as then the AtomStackClass cannot discern between full and empty stack!
     2912  StackClass<atom *> *RootStack = new StackClass<atom *>(AtomCount);
     2913  StackClass<atom *> *TouchedStack = new StackClass<atom *>((int)pow(4,Order)+2); // number of atoms reached from one with maximal 4 bonds plus Root itself
     2914  StackClass<atom *> *SnakeStack = new StackClass<atom *>(Order+1); // equal to Order is not possible, as then the StackClass<atom *> cannot discern between full and empty stack!
    29272915  MoleculeLeafClass *Leaflet = NULL, *TempLeaf = NULL;
    29282916  MoleculeListClass *FragmentList = NULL;
     
    34893477  double tmp;
    34903478  vector TranslationVector;
    3491   //AtomStackClass *CompStack = NULL;
    3492   AtomStackClass *AtomStack = new AtomStackClass(AtomCount);
     3479  //class StackClass<atom *> *CompStack = NULL;
     3480  class StackClass<atom *> *AtomStack = new StackClass<atom *>(AtomCount);
    34933481  bool flag = true;
    34943482
  • src/molecules.hpp

    rdb066d r6d35e4  
    2121
    2222#include "helpers.hpp"
     23#include "stackclass.hpp"
    2324
    2425class atom;
    25 class AtomStackClass;
    2626class bond;
    2727class config;
     
    4646  bool operator() (const KeySet SubgraphA, const KeySet SubgraphB) const;
    4747};
     48
    4849//bool operator < (KeySet SubgraphA, KeySet SubgraphB);   //note: this declaration is important, otherwise normal < is used (producing wrong order)
    4950inline void InsertFragmentIntoGraph(ofstream *out, struct UniqueFragments *Fragment); // Insert a KeySet into a Graph
    5051inline void InsertGraphIntoGraph(ofstream *out, Graph &graph1, Graph &graph2, int *counter);  // Insert all KeySet's in a Graph into another Graph
    51 
    52 /******************************** Some templates for list management ***********************************/
    53 
    54 /** Adds linking of an item to a list.
    55  * \param *walker
    56  * \return true - adding succeeded, false - error in list
    57  */
    58 template <typename X> void link(X *walker, X *end)
    59 {
    60   X *vorher = end->previous;
    61   if (vorher != NULL)
    62     vorher->next = walker;
    63   end->previous = walker;
    64   walker->previous = vorher;
    65   walker->next = end;
    66 };
    67 
    68 /** Removes linking of an item in a list.
    69  * \param *walker
    70  * \return true - removing succeeded, false - given item not found in list
    71  */
    72 template <typename X> void unlink(X *walker)
    73 {
    74   if (walker->next != NULL)
    75     walker->next->previous = walker->previous;
    76   if (walker->previous != NULL)
    77     walker->previous->next = walker->next;
    78 };
    79 
    80 /** Adds new item before an item \a *end in a list.
    81  * \param *pointer   item to be added
    82  * \param *end  end of list
    83  * \return true - addition succeeded, false - unable to add item to list
    84  */
    85 template <typename X>  bool add(X *pointer, X *end)
    86 {
    87   if (end != NULL) {
    88     link(pointer, end);
    89   } else {
    90     pointer->previous = NULL;
    91     pointer->next = NULL;
    92   }
    93   return true;
    94 };
    95 
    96 /** Finds item in list
    97  * \param *suche  search criteria
    98  * \param *start  begin of list
    99  * \param *end  end of list
    100  * \return X - if found, NULL - if not found
    101  */
    102 template <typename X, typename Y> X * find(Y *suche, X *start, X *end)
    103 {
    104   X *walker = start;
    105   while (walker->next != end) { // go through list
    106     walker = walker->next; // step onward beforehand
    107     if (*walker->sort == *suche) return (walker);
    108   }
    109   return NULL;
    110 };
    111 
    112 /** Removes an item from the list without check.
    113  * \param *walker item to be removed
    114  * \return true - removing succeeded, false - given item not found in list
    115  */
    116 template <typename X> void removewithoutcheck(X *walker)
    117 {
    118   if (walker != NULL) {
    119     unlink(walker);
    120     delete(walker);
    121     walker = NULL;
    122   }
    123 };
    124 
    125 /** Removes an item from the list, checks if exists.
    126  * Checks beforehand if atom is really within molecule list.
    127  * \param *pointer   item to be removed
    128  * \param *start  begin of list
    129  * \param *end  end of list
    130  * \return true - removing succeeded, false - given item not found in list
    131  */
    132 template <typename X> bool remove(X *pointer, X *start, X *end)
    133 {
    134   X *walker = find (pointer->sort, start, end);
    135 /*  while (walker->next != pointer) { // search through list
    136     walker = walker->next;
    137     if (walker == end) return false;  // item not found in list
    138   }*/
    139   // atom found, now unlink
    140   if (walker != NULL)
    141     removewithoutcheck(walker);
    142   else
    143     return false;
    144   return true;
    145 };
    146 
    147 /** Cleans the whole list.
    148  * \param *start begin of list
    149  * \param *end end of list
    150  * \return true - list was cleaned successfully, false - error in list structure
    151  */
    152 template <typename X> bool cleanup(X *start, X *end)
    153 {
    154   X *pointer = start->next;
    155   X *walker;
    156   while (pointer != end) { // go through list
    157     walker = pointer; // mark current
    158     pointer = pointer->next; // step onward beforehand
    159     // remove walker
    160     unlink(walker);
    161     delete(walker);
    162     walker = NULL;
    163   }
    164   return true;
    165 };
    166 
    167 /** Returns the first marker in a chain list.
    168  * \param *me one arbitrary item in chain list
    169  * \return poiner to first marker
    170  */
    171 template <typename X> X *GetFirst(X *me)
    172 {
    173   X *Binder = me;
    174   while(Binder->previous != NULL)
    175     Binder = Binder->previous;
    176   return Binder;
    177 };
    178 
    179 /** Returns the last marker in a chain list.
    180  * \param *me one arbitrary item in chain list
    181  * \return poiner to last marker
    182  */
    183 template <typename X> X *GetLast(X *me)
    184 {
    185   X *Binder = me;
    186   while(Binder->next != NULL)
    187     Binder = Binder->next;
    188   return Binder;
    189 };
    190 
    191 /** Frees a two-dimensional array.
    192  * \param *ptr pointer to array
    193  * \param dim first dim of array
    194  */
    195 template <typename X> void Free2DArray(X **ptr, int dim)
    196 {
    197         int i;
    198         if (ptr != NULL) {
    199                 for(i=0;i<dim;i++)
    200                         if (ptr[i] != NULL)
    201                                 free(ptr[i]);
    202                 free(ptr);
    203         }
    204 };
    205 
    20652int CompareDoubles (const void * a, const void * b);
     53
    20754
    20855/************************************* Class definitions ****************************************/
     
    370217
    371218ostream & operator << (ostream &ost, atom &a);
    372 
    373 /* Stack of Atoms.
    374  * Is used during DepthFirstSearchAnalysis() to detect nonseparable components.
    375  */
    376 class AtomStackClass {
    377   public:
    378   AtomStackClass(int dimension);
    379   ~AtomStackClass();
    380  
    381   bool Push(atom *object);
    382   atom *PopFirst();
    383   atom *PopLast();
    384   bool AtomStackClass::RemoveItem(atom *ptr);
    385   void ClearStack();
    386   bool IsEmpty();
    387   bool IsFull();
    388         int ItemCount();
    389   void Output(ofstream *out) const;
    390   void TestImplementation(ofstream *out, atom *test);
    391  
    392   private:
    393     atom **StackList;   //!< the list containing the atom pointers
    394     int EntryCount;     //!< number of entries in the stack
    395     int CurrentLastEntry;   //!< Current last entry (newest item on stack)
    396     int CurrentFirstEntry;   //!< Current first entry (oldest item on stack)
    397     int NextFreeField;       //!< Current index of next free field
    398 };
    399 
    400219
    401220/** Bonds between atoms.
Note: See TracChangeset for help on using the changeset viewer.