Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/moleculelist.cpp

    r7fd416 r1513a74  
    2020#include "memoryallocator.hpp"
    2121#include "periodentafel.hpp"
    22 #include "Helpers/Assert.hpp"
    23 
    24 #include "Helpers/Assert.hpp"
     22#include "World.hpp"
    2523
    2624/*********************************** Functions for class MoleculeListClass *************************/
     
    4038MoleculeListClass::~MoleculeListClass()
    4139{
    42   DoLog(4) && (Log() << Verbose(4) << "Clearing ListOfMolecules." << endl);
    43   for(MoleculeList::iterator MolRunner = ListOfMolecules.begin(); MolRunner != ListOfMolecules.end(); ++MolRunner)
    44     (*MolRunner)->signOff(this);
     40  DoLog(3) && (Log() << Verbose(3) << this << ": Freeing ListOfMolcules." << endl);
     41  for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
     42    DoLog(4) && (Log() << Verbose(4) << "ListOfMolecules: Freeing " << *ListRunner << "." << endl);
     43    world->destroyMolecule(*ListRunner);
     44  }
     45  DoLog(4) && (Log() << Verbose(4) << "Freeing ListOfMolecules." << endl);
    4546  ListOfMolecules.clear(); // empty list
    4647};
     
    4849/** Insert a new molecule into the list and set its number.
    4950 * \param *mol molecule to add to list.
     51 * \return true - add successful
    5052 */
    5153void MoleculeListClass::insert(molecule *mol)
     
    5759};
    5860
    59 /** Erases a molecule from the list.
    60  * \param *mol molecule to add to list.
    61  */
    62 void MoleculeListClass::erase(molecule *mol)
    63 {
    64   OBSERVE;
    65   mol->signOff(this);
    66   ListOfMolecules.remove(mol);
    67 };
    68 
    6961/** Compare whether two molecules are equal.
    7062 * \param *a molecule one
     
    7769  int Count, Counter, aCounter, bCounter;
    7870  int flag;
     71  atom *aWalker = NULL;
     72  atom *bWalker = NULL;
    7973
    8074  // sort each atom list and put the numbers into a list, then go through
    8175  //Log() << Verbose(0) << "Comparing fragment no. " << *(molecule **)a << " to " << *(molecule **)b << "." << endl;
    82   // Yes those types are awkward... but check it for yourself it checks out this way
    83   molecule *const *mol1_ptr= static_cast<molecule *const *>(a);
    84   molecule *mol1 = *mol1_ptr;
    85   molecule *const *mol2_ptr= static_cast<molecule *const *>(b);
    86   molecule *mol2 = *mol2_ptr;
    87   if (mol1->getAtomCount() < mol2->getAtomCount()) {
     76  if ((**(molecule **) a).AtomCount < (**(molecule **) b).AtomCount) {
    8877    return -1;
    8978  } else {
    90     if (mol1->getAtomCount() > mol2->getAtomCount())
     79    if ((**(molecule **) a).AtomCount > (**(molecule **) b).AtomCount)
    9180      return +1;
    9281    else {
    93       Count = mol1->getAtomCount();
     82      Count = (**(molecule **) a).AtomCount;
    9483      aList = new int[Count];
    9584      bList = new int[Count];
    9685
    9786      // fill the lists
     87      aWalker = (**(molecule **) a).start;
     88      bWalker = (**(molecule **) b).start;
    9889      Counter = 0;
    9990      aCounter = 0;
    10091      bCounter = 0;
    101       molecule::const_iterator aiter = mol1->begin();
    102       molecule::const_iterator biter = mol2->begin();
    103       for (;(aiter != mol1->end()) && (biter != mol2->end());
    104           ++aiter, ++biter) {
    105         if ((*aiter)->GetTrueFather() == NULL)
     92      while ((aWalker->next != (**(molecule **) a).end) && (bWalker->next != (**(molecule **) b).end)) {
     93        aWalker = aWalker->next;
     94        bWalker = bWalker->next;
     95        if (aWalker->GetTrueFather() == NULL)
    10696          aList[Counter] = Count + (aCounter++);
    10797        else
    108           aList[Counter] = (*aiter)->GetTrueFather()->nr;
    109         if ((*biter)->GetTrueFather() == NULL)
     98          aList[Counter] = aWalker->GetTrueFather()->nr;
     99        if (bWalker->GetTrueFather() == NULL)
    110100          bList[Counter] = Count + (bCounter++);
    111101        else
    112           bList[Counter] = (*biter)->GetTrueFather()->nr;
     102          bList[Counter] = bWalker->GetTrueFather()->nr;
    113103        Counter++;
    114104      }
    115105      // check if AtomCount was for real
    116106      flag = 0;
    117       if ((aiter == mol1->end()) && (biter != mol2->end())) {
     107      if ((aWalker->next == (**(molecule **) a).end) && (bWalker->next != (**(molecule **) b).end)) {
    118108        flag = -1;
    119109      } else {
    120         if ((aiter != mol1->end()) && (biter == mol2->end()))
     110        if ((aWalker->next != (**(molecule **) a).end) && (bWalker->next == (**(molecule **) b).end))
    121111          flag = 1;
    122112      }
     
    152142void MoleculeListClass::Enumerate(ostream *out)
    153143{
     144  atom *Walker = NULL;
    154145  periodentafel *periode = World::getInstance().getPeriode();
    155146  std::map<atomicNumber_t,unsigned int> counts;
     
    167158      // count atoms per element and determine size of bounding sphere
    168159      size=0.;
    169       for (molecule::const_iterator iter = (*ListRunner)->begin(); iter != (*ListRunner)->end(); ++iter) {
    170         counts[(*iter)->type->getNumber()]++;
    171         if ((*iter)->x.DistanceSquared(Origin) > size)
    172           size = (*iter)->x.DistanceSquared(Origin);
     160      Walker = (*ListRunner)->start;
     161      while (Walker->next != (*ListRunner)->end) {
     162        Walker = Walker->next;
     163        counts[Walker->type->getNumber()]++;
     164        if (Walker->x.DistanceSquared(Origin) > size)
     165          size = Walker->x.DistanceSquared(Origin);
    173166      }
    174167      // output Index, Name, number of atoms, chemical formula
    175       (*out) << ((*ListRunner)->ActiveFlag ? "*" : " ") << (*ListRunner)->IndexNr << "\t" << (*ListRunner)->name << "\t\t" << (*ListRunner)->getAtomCount() << "\t";
     168      (*out) << ((*ListRunner)->ActiveFlag ? "*" : " ") << (*ListRunner)->IndexNr << "\t" << (*ListRunner)->name << "\t\t" << (*ListRunner)->AtomCount << "\t";
    176169
    177170      std::map<atomicNumber_t,unsigned int>::reverse_iterator iter;
     
    209202
    210203  // put all molecules of src into mol
    211   molecule::iterator runner;
    212   for (molecule::iterator iter = srcmol->begin(); iter != srcmol->end(); ++iter) {
    213     runner = iter++;
    214     srcmol->UnlinkAtom((*runner));
    215     mol->AddAtom((*runner));
     204  atom *Walker = srcmol->start;
     205  atom *NextAtom = Walker->next;
     206  while (NextAtom != srcmol->end) {
     207    Walker = NextAtom;
     208    NextAtom = Walker->next;
     209    srcmol->UnlinkAtom(Walker);
     210    mol->AddAtom(Walker);
    216211  }
    217212
     
    233228
    234229  // put all molecules of src into mol
    235   atom *Walker = NULL;
    236   for (molecule::iterator iter = srcmol->begin(); iter != srcmol->end(); ++iter) {
    237     Walker = mol->AddCopyAtom((*iter));
     230  atom *Walker = srcmol->start;
     231  atom *NextAtom = Walker->next;
     232  while (NextAtom != srcmol->end) {
     233    Walker = NextAtom;
     234    NextAtom = Walker->next;
     235    Walker = mol->AddCopyAtom(Walker);
    238236    Walker->father = Walker;
    239237  }
     
    332330
    333331  // prepare index list for bonds
    334   atom ** CopyAtoms = new atom*[srcmol->getAtomCount()];
    335   for(int i=0;i<srcmol->getAtomCount();i++)
     332  srcmol->CountAtoms();
     333  atom ** CopyAtoms = new atom*[srcmol->AtomCount];
     334  for(int i=0;i<srcmol->AtomCount;i++)
    336335    CopyAtoms[i] = NULL;
    337336
    338337  // for each of the source atoms check whether we are in- or outside and add copy atom
     338  atom *Walker = srcmol->start;
    339339  int nr=0;
    340   for (molecule::const_iterator iter = srcmol->begin(); iter != srcmol->end(); ++iter) {
    341     DoLog(2) && (Log() << Verbose(2) << "INFO: Current Walker is " << **iter << "." << endl);
    342     if (!TesselStruct->IsInnerPoint((*iter)->x, LCList)) {
    343       CopyAtoms[(*iter)->nr] = (*iter)->clone();
    344       mol->AddAtom(CopyAtoms[(*iter)->nr]);
     340  while (Walker->next != srcmol->end) {
     341    Walker = Walker->next;
     342    DoLog(2) && (Log() << Verbose(2) << "INFO: Current Walker is " << *Walker << "." << endl);
     343    if (!TesselStruct->IsInnerPoint(Walker->x, LCList)) {
     344      CopyAtoms[Walker->nr] = Walker->clone();
     345      mol->AddAtom(CopyAtoms[Walker->nr]);
    345346      nr++;
    346347    } else {
     
    348349    }
    349350  }
    350   DoLog(1) && (Log() << Verbose(1) << nr << " of " << srcmol->getAtomCount() << " atoms have been merged.");
     351  DoLog(1) && (Log() << Verbose(1) << nr << " of " << srcmol->AtomCount << " atoms have been merged.");
    351352
    352353  // go through all bonds and add as well
    353   for(molecule::iterator AtomRunner = srcmol->begin(); AtomRunner != srcmol->end(); ++AtomRunner)
    354     for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); BondRunner != (*AtomRunner)->ListOfBonds.end(); ++BondRunner)
    355       if ((*BondRunner)->leftatom == *AtomRunner) {
    356         DoLog(3) && (Log() << Verbose(3) << "Adding Bond between " << *CopyAtoms[(*BondRunner)->leftatom->nr] << " and " << *CopyAtoms[(*BondRunner)->rightatom->nr]<< "." << endl);
    357         mol->AddBond(CopyAtoms[(*BondRunner)->leftatom->nr], CopyAtoms[(*BondRunner)->rightatom->nr], (*BondRunner)->BondDegree);
    358       }
     354  bond *Binder = srcmol->first;
     355  while(Binder->next != srcmol->last) {
     356    Binder = Binder->next;
     357    DoLog(3) && (Log() << Verbose(3) << "Adding Bond between " << *CopyAtoms[Binder->leftatom->nr] << " and " << *CopyAtoms[Binder->rightatom->nr]<< "." << endl);
     358    mol->AddBond(CopyAtoms[Binder->leftatom->nr], CopyAtoms[Binder->rightatom->nr], Binder->BondDegree);
     359  }
    359360  delete(LCList);
    360361  return true;
     
    381382bool MoleculeListClass::AddHydrogenCorrection(char *path)
    382383{
     384  atom *Walker = NULL;
     385  atom *Runner = NULL;
    383386  bond *Binder = NULL;
    384387  double ***FitConstant = NULL, **correction = NULL;
     
    424427
    425428  // 0b. allocate memory for constants
    426   FitConstant = new double**[3];
     429  FitConstant = Calloc<double**>(3, "MoleculeListClass::AddHydrogenCorrection: ***FitConstant");
    427430  for (int k = 0; k < 3; k++) {
    428     FitConstant[k] = new double*[a];
     431    FitConstant[k] = Calloc<double*>(a, "MoleculeListClass::AddHydrogenCorrection: **FitConstant[]");
    429432    for (int i = a; i--;) {
    430       FitConstant[k][i] = new double[b];
    431       for (int j = b; j--;) {
    432         FitConstant[k][i][j] = 0.;
    433       }
     433      FitConstant[k][i] = Calloc<double>(b, "MoleculeListClass::AddHydrogenCorrection: *FitConstant[][]");
    434434    }
    435435  }
     
    477477
    478478  // 0d. allocate final correction matrix
    479   correction = new double*[a];
     479  correction = Calloc<double*>(a, "MoleculeListClass::AddHydrogenCorrection: **correction");
    480480  for (int i = a; i--;)
    481     correction[i] = new double[b];
     481    correction[i] = Calloc<double>(b, "MoleculeListClass::AddHydrogenCorrection: *correction[]");
    482482
    483483  // 1a. go through every molecule in the list
     
    488488        correction[k][j] = 0.;
    489489    // 2. take every hydrogen that is a saturated one
    490     for (molecule::const_iterator iter = (*ListRunner)->begin(); iter != (*ListRunner)->end(); ++iter) {
    491       //Log() << Verbose(1) << "(*iter): " << *(*iter) << " with first bond " << *((*iter)->ListOfBonds.begin()) << "." << endl;
    492       if (((*iter)->type->Z == 1) && (((*iter)->father == NULL)
    493           || ((*iter)->father->type->Z != 1))) { // if it's a hydrogen
    494         for (molecule::const_iterator runner = (*ListRunner)->begin(); runner != (*ListRunner)->end(); ++runner) {
    495           //Log() << Verbose(2) << "Runner: " << *(*runner) << " with first bond " << *((*iter)->ListOfBonds.begin()) << "." << endl;
     490    Walker = (*ListRunner)->start;
     491    while (Walker->next != (*ListRunner)->end) {
     492      Walker = Walker->next;
     493      //Log() << Verbose(1) << "Walker: " << *Walker << " with first bond " << *(Walker->ListOfBonds.begin()) << "." << endl;
     494      if ((Walker->type->Z == 1) && ((Walker->father == NULL)
     495          || (Walker->father->type->Z != 1))) { // if it's a hydrogen
     496        Runner = (*ListRunner)->start;
     497        while (Runner->next != (*ListRunner)->end) {
     498          Runner = Runner->next;
     499          //Log() << Verbose(2) << "Runner: " << *Runner << " with first bond " << *(Walker->ListOfBonds.begin()) << "." << endl;
    496500          // 3. take every other hydrogen that is the not the first and not bound to same bonding partner
    497           Binder = *((*runner)->ListOfBonds.begin());
    498           if (((*runner)->type->Z == 1) && ((*runner)->nr > (*iter)->nr) && (Binder->GetOtherAtom((*runner)) != Binder->GetOtherAtom((*iter)))) { // (hydrogens have only one bonding partner!)
     501          Binder = *(Runner->ListOfBonds.begin());
     502          if ((Runner->type->Z == 1) && (Runner->nr > Walker->nr) && (Binder->GetOtherAtom(Runner) != Binder->GetOtherAtom(Walker))) { // (hydrogens have only one bonding partner!)
    499503            // 4. evaluate the morse potential for each matrix component and add up
    500             distance = (*runner)->x.distance((*iter)->x);
    501             //Log() << Verbose(0) << "Fragment " << (*ListRunner)->name << ": " << *(*runner) << "<= " << distance << "=>" << *(*iter) << ":" << endl;
     504            distance = Runner->x.distance(Walker->x);
     505            //Log() << Verbose(0) << "Fragment " << (*ListRunner)->name << ": " << *Runner << "<= " << distance << "=>" << *Walker << ":" << endl;
    502506            for (int k = 0; k < a; k++) {
    503507              for (int j = 0; j < b; j++) {
     
    527531    FragmentNumber = FixedDigitNumber(ListOfMolecules.size(), (*ListRunner)->IndexNr);
    528532    line += FragmentNumber;
    529     delete[] (FragmentNumber);
     533    delete (FragmentNumber);
    530534    line += HCORRECTIONSUFFIX;
    531535    output.open(line.c_str());
     
    538542    output.close();
    539543  }
    540   for (int i = a; i--;)
    541     delete[](correction[i]);
    542   delete[](correction);
    543 
    544544  line = path;
    545545  line.append("/");
     
    556556  for (int k = 0; k < 3; k++) {
    557557    for (int i = a; i--;) {
    558       delete[](FitConstant[k][i]);
    559     }
    560     delete[](FitConstant[k]);
    561   }
    562   delete[](FitConstant);
     558      Free(&FitConstant[k][i]);
     559    }
     560    Free(&FitConstant[k]);
     561  }
     562  Free(&FitConstant);
    563563  DoLog(0) && (Log() << Verbose(0) << "done." << endl);
    564564  return true;
     
    577577  ofstream ForcesFile;
    578578  stringstream line;
     579  atom *Walker = NULL;
    579580  periodentafel *periode=World::getInstance().getPeriode();
    580581
     
    589590      periodentafel::const_iterator elemIter;
    590591      for(elemIter=periode->begin();elemIter!=periode->end();++elemIter){
    591         if ((*ListRunner)->ElementsInMolecule[(*elemIter).first]) { // if this element got atoms
    592           for(molecule::iterator atomIter = (*ListRunner)->begin(); atomIter !=(*ListRunner)->end();++atomIter){
    593             if ((*atomIter)->type->getNumber() == (*elemIter).first) {
    594               if (((*atomIter)->GetTrueFather() != NULL) && ((*atomIter)->GetTrueFather() != (*atomIter))) {// if there is a rea
     592          if ((*ListRunner)->ElementsInMolecule[(*elemIter).first]) { // if this element got atoms
     593          Walker = (*ListRunner)->start;
     594          while (Walker->next != (*ListRunner)->end) { // go through every atom of this element
     595            Walker = Walker->next;
     596            if (Walker->type->getNumber() == (*elemIter).first) {
     597              if ((Walker->GetTrueFather() != NULL) && (Walker->GetTrueFather() != Walker)) {// if there is a rea
    595598                //Log() << Verbose(0) << "Walker is " << *Walker << " with true father " << *( Walker->GetTrueFather()) << ", it
    596                 ForcesFile << SortIndex[(*atomIter)->GetTrueFather()->nr] << "\t";
     599                ForcesFile << SortIndex[Walker->GetTrueFather()->nr] << "\t";
    597600              } else
    598601                // otherwise a -1 to indicate an added saturation hydrogen
     
    619622 * \param *configuration standard configuration to attach atoms in fragment molecule to.
    620623 * \param *SortIndex Index to map from the BFS labeling to the sequence how of Ion_Type in the config
     624 * \param DoPeriodic true - call ScanForPeriodicCorrection, false - don't
     625 * \param DoCentering true - call molecule::CenterEdge(), false - don't
    621626 * \return true - success (each file was written), false - something went wrong.
    622627 */
     
    628633  bool result = true;
    629634  bool intermediateResult = true;
     635  atom *Walker = NULL;
    630636  Vector BoxDimension;
    631637  char *FragmentNumber = NULL;
     
    668674    // list atoms in fragment for debugging
    669675    DoLog(2) && (Log() << Verbose(2) << "Contained atoms: ");
    670     for (molecule::const_iterator iter = (*ListRunner)->begin(); iter != (*ListRunner)->end(); ++iter) {
    671       DoLog(0) && (Log() << Verbose(0) << (*iter)->getName() << " ");
     676    Walker = (*ListRunner)->start;
     677    while (Walker->next != (*ListRunner)->end) {
     678      Walker = Walker->next;
     679      DoLog(0) && (Log() << Verbose(0) << Walker->Name << " ");
    672680    }
    673681    DoLog(0) && (Log() << Verbose(0) << endl);
     
    716724    //outputFragment.close();
    717725    //outputFragment.clear();
    718     delete[](FragmentNumber);
     726    Free(&FragmentNumber);
    719727  }
    720728  DoLog(0) && (Log() << Verbose(0) << " done." << endl);
     
    748756void MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs(const periodentafel * const periode, config * const configuration)
    749757{
    750   // 0a. remove all present molecules
    751   vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
    752   for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
    753     erase(*MolRunner);
     758  molecule *mol = World::getInstance().createMolecule();
     759  atom *Walker = NULL;
     760  atom *Advancer = NULL;
     761  bond *Binder = NULL;
     762  bond *Stepper = NULL;
     763  // 0. gather all atoms into single molecule
     764  for (MoleculeList::iterator MolRunner = ListOfMolecules.begin(); !ListOfMolecules.empty(); MolRunner = ListOfMolecules.begin()) {
     765    // shift all atoms to new molecule
     766    Advancer = (*MolRunner)->start->next;
     767    while (Advancer != (*MolRunner)->end) {
     768      Walker = Advancer;
     769      Advancer = Advancer->next;
     770      DoLog(3) && (Log() << Verbose(3) << "Re-linking " << *Walker << "..." << endl);
     771      unlink(Walker);
     772      Walker->father = Walker;
     773      mol->AddAtom(Walker);    // counting starts at 1
     774    }
     775    // remove all bonds
     776    Stepper = (*MolRunner)->first->next;
     777    while (Stepper != (*MolRunner)->last) {
     778      Binder = Stepper;
     779      Stepper = Stepper->next;
     780      delete(Binder);
     781    }
     782    // remove the molecule
    754783    World::getInstance().destroyMolecule(*MolRunner);
    755   }
    756   // 0b. remove all bonds and construct a molecule with all atoms
    757   molecule *mol = World::getInstance().createMolecule();
    758   vector <atom *> allatoms = World::getInstance().getAllAtoms();
    759   for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
    760     for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
    761       delete(*BondRunner);
    762     mol->AddAtom(*AtomRunner);
     784    ListOfMolecules.erase(MolRunner);
    763785  }
    764786
     
    788810  const int MolCount = Subgraphs->next->Count();
    789811  char number[MAXSTRINGSIZE];
    790   molecule **molecules = new molecule *[MolCount];
    791   MoleculeLeafClass *MolecularWalker = Subgraphs;
     812  molecule **molecules = Malloc<molecule *>(MolCount, "config::Load() - **molecules");
    792813  for (int i=0;i<MolCount;i++) {
    793     MolecularWalker = MolecularWalker->next;
    794814    molecules[i] = World::getInstance().createMolecule();
    795815    molecules[i]->ActiveFlag = true;
     
    800820    }
    801821    DoLog(1) && (Log() << Verbose(1) << "MolName is " << molecules[i]->name << endl);
    802     for (molecule::iterator iter = MolecularWalker->Leaf->begin(); iter != MolecularWalker->Leaf->end(); ++iter) {
    803       DoLog(1) && (Log() << Verbose(1) << **iter << endl);
    804     }
    805822    insert(molecules[i]);
    806823  }
     
    808825  // 4b. create and fill map of which atom is associated to which connected molecule (note, counting starts at 1)
    809826  int FragmentCounter = 0;
    810   map<int, atom *> AtomToFragmentMap;
    811   MolecularWalker = Subgraphs;
     827  int *MolMap = Calloc<int>(mol->AtomCount, "config::Load() - *MolMap");
     828  MoleculeLeafClass *MolecularWalker = Subgraphs;
     829  Walker = NULL;
    812830  while (MolecularWalker->next != NULL) {
    813831    MolecularWalker = MolecularWalker->next;
    814     for (molecule::iterator iter = MolecularWalker->Leaf->begin(); !MolecularWalker->Leaf->empty(); iter = MolecularWalker->Leaf->begin()) {
    815       atom * Walker = *iter;
    816       DoLog(1) && (Log() << Verbose(1) << "Re-linking " << Walker << "..." << endl);
    817       MolecularWalker->Leaf->erase(iter);
    818       molecules[FragmentCounter]->AddAtom(Walker);    // counting starts at 1
     832    Walker = MolecularWalker->Leaf->start;
     833    while (Walker->next != MolecularWalker->Leaf->end) {
     834      Walker = Walker->next;
     835      MolMap[Walker->GetTrueFather()->nr] = FragmentCounter+1;
    819836    }
    820837    FragmentCounter++;
    821838  }
    822   World::getInstance().destroyMolecule(mol);
    823 
     839
     840  // 4c. relocate atoms to new molecules and remove from Leafs
     841  Walker = NULL;
     842  while (mol->start->next != mol->end) {
     843    Walker = mol->start->next;
     844    if ((Walker->nr <0) || (Walker->nr >= mol->AtomCount)) {
     845      DoeLog(0) && (eLog()<< Verbose(0) << "Index of atom " << *Walker << " is invalid!" << endl);
     846      performCriticalExit();
     847    }
     848    FragmentCounter = MolMap[Walker->nr];
     849    if (FragmentCounter != 0) {
     850      DoLog(3) && (Log() << Verbose(3) << "Re-linking " << *Walker << "..." << endl);
     851      unlink(Walker);
     852      molecules[FragmentCounter-1]->AddAtom(Walker);    // counting starts at 1
     853    } else {
     854      DoeLog(0) && (eLog()<< Verbose(0) << "Atom " << *Walker << " not associated to molecule!" << endl);
     855      performCriticalExit();
     856    }
     857  }
    824858  // 4d. we don't need to redo bonds, as they are connected subgraphs and still maintain their ListOfBonds, but we have to remove them from first..last list
    825   // TODO: check whether this is really not needed anymore
     859  Binder = mol->first;
     860  while (mol->first->next != mol->last) {
     861    Binder = mol->first->next;
     862    Walker = Binder->leftatom;
     863    unlink(Binder);
     864    link(Binder,molecules[MolMap[Walker->nr]-1]->last);   // counting starts at 1
     865  }
    826866  // 4e. free Leafs
    827867  MolecularWalker = Subgraphs;
     
    831871  }
    832872  delete(MolecularWalker);
    833   delete[](molecules);
     873  Free(&MolMap);
     874  Free(&molecules);
    834875  DoLog(1) && (Log() << Verbose(1) << "I scanned " << FragmentCounter << " molecules." << endl);
    835876};
     
    841882int MoleculeListClass::CountAllAtoms() const
    842883{
     884  atom *Walker = NULL;
    843885  int AtomNo = 0;
    844886  for (MoleculeList::const_iterator MolWalker = ListOfMolecules.begin(); MolWalker != ListOfMolecules.end(); MolWalker++) {
    845     AtomNo += (*MolWalker)->size();
     887    Walker = (*MolWalker)->start;
     888    while (Walker->next != (*MolWalker)->end) {
     889      Walker = Walker->next;
     890      AtomNo++;
     891    }
    846892  }
    847893  return AtomNo;
     
    10181064bool MoleculeLeafClass::FillBondStructureFromReference(const molecule * const reference, int &FragmentCounter, atom ***&ListOfLocalAtoms, bool FreeList)
    10191065{
     1066  atom *Walker = NULL;
    10201067  atom *OtherWalker = NULL;
    10211068  atom *Father = NULL;
     
    10251072  DoLog(1) && (Log() << Verbose(1) << "Begin of FillBondStructureFromReference." << endl);
    10261073  // fill ListOfLocalAtoms if NULL was given
    1027   if (!FillListOfLocalAtoms(ListOfLocalAtoms, FragmentCounter, reference->getAtomCount(), FreeList)) {
     1074  if (!FillListOfLocalAtoms(ListOfLocalAtoms, FragmentCounter, reference->AtomCount, FreeList)) {
    10281075    DoLog(1) && (Log() << Verbose(1) << "Filling of ListOfLocalAtoms failed." << endl);
    10291076    return false;
     
    10331080    DoLog(1) && (Log() << Verbose(1) << "Creating adjacency list for subgraph " << Leaf << "." << endl);
    10341081    // remove every bond from the list
    1035     for(molecule::iterator AtomRunner = Leaf->begin(); AtomRunner != Leaf->end(); ++AtomRunner)
    1036       for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
    1037         if ((*BondRunner)->leftatom == *AtomRunner)
    1038           delete((*BondRunner));
    1039 
    1040     for(molecule::const_iterator iter = Leaf->begin(); iter != Leaf->end(); ++iter) {
    1041       Father = (*iter)->GetTrueFather();
     1082    bond *Binder = NULL;
     1083    while (Leaf->last->previous != Leaf->first) {
     1084      Binder = Leaf->last->previous;
     1085      Binder->leftatom->UnregisterBond(Binder);
     1086      Binder->rightatom->UnregisterBond(Binder);
     1087      removewithoutcheck(Binder);
     1088    }
     1089
     1090    Walker = Leaf->start;
     1091    while (Walker->next != Leaf->end) {
     1092      Walker = Walker->next;
     1093      Father = Walker->GetTrueFather();
    10421094      AtomNo = Father->nr; // global id of the current walker
    10431095      for (BondList::const_iterator Runner = Father->ListOfBonds.begin(); Runner != Father->ListOfBonds.end(); (++Runner)) {
    1044         OtherWalker = ListOfLocalAtoms[FragmentCounter][(*Runner)->GetOtherAtom((*iter)->GetTrueFather())->nr]; // local copy of current bond partner of walker
     1096        OtherWalker = ListOfLocalAtoms[FragmentCounter][(*Runner)->GetOtherAtom(Walker->GetTrueFather())->nr]; // local copy of current bond partner of walker
    10451097        if (OtherWalker != NULL) {
    1046           if (OtherWalker->nr > (*iter)->nr)
    1047             Leaf->AddBond((*iter), OtherWalker, (*Runner)->BondDegree);
     1098          if (OtherWalker->nr > Walker->nr)
     1099            Leaf->AddBond(Walker, OtherWalker, (*Runner)->BondDegree);
    10481100        } else {
    1049           DoLog(1) && (Log() << Verbose(1) << "OtherWalker = ListOfLocalAtoms[" << FragmentCounter << "][" << (*Runner)->GetOtherAtom((*iter)->GetTrueFather())->nr << "] is NULL!" << endl);
     1101          DoLog(1) && (Log() << Verbose(1) << "OtherWalker = ListOfLocalAtoms[" << FragmentCounter << "][" << (*Runner)->GetOtherAtom(Walker->GetTrueFather())->nr << "] is NULL!" << endl);
    10501102          status = false;
    10511103        }
     
    10561108  if ((FreeList) && (ListOfLocalAtoms != NULL)) {
    10571109    // free the index lookup list
    1058     delete[](ListOfLocalAtoms[FragmentCounter]);
     1110    Free(&ListOfLocalAtoms[FragmentCounter]);
    10591111    if (FragmentCounter == 0) // first fragments frees the initial pointer to list
    1060       delete[](ListOfLocalAtoms);
     1112      Free(&ListOfLocalAtoms);
    10611113  }
    10621114  DoLog(1) && (Log() << Verbose(1) << "End of FillBondStructureFromReference." << endl);
     
    10741126bool MoleculeLeafClass::FillRootStackForSubgraphs(KeyStack *&RootStack, bool *AtomMask, int &FragmentCounter)
    10751127{
    1076   atom *Father = NULL;
     1128  atom *Walker = NULL, *Father = NULL;
    10771129
    10781130  if (RootStack != NULL) {
     
    10801132    if (&(RootStack[FragmentCounter]) != NULL) {
    10811133      RootStack[FragmentCounter].clear();
    1082       for(molecule::const_iterator iter = Leaf->begin(); iter != Leaf->end(); ++iter) {
    1083         Father = (*iter)->GetTrueFather();
     1134      Walker = Leaf->start;
     1135      while (Walker->next != Leaf->end) { // go through all (non-hydrogen) atoms
     1136        Walker = Walker->next;
     1137        Father = Walker->GetTrueFather();
    10841138        if (AtomMask[Father->nr]) // apply mask
    10851139#ifdef ADDHYDROGEN
    1086           if ((*iter)->type->Z != 1) // skip hydrogen
     1140          if (Walker->type->Z != 1) // skip hydrogen
    10871141#endif
    1088           RootStack[FragmentCounter].push_front((*iter)->nr);
     1142          RootStack[FragmentCounter].push_front(Walker->nr);
    10891143      }
    10901144      if (next != NULL)
     
    11171171    // allocate and set each field to NULL
    11181172    const int Counter = Count();
    1119     ASSERT(FragmentCounter < Counter, "FillListOfLocalAtoms: FragmenCounter greater than present fragments.");
    1120     ListOfLocalAtoms = new atom**[Counter];
     1173    ListOfLocalAtoms = Calloc<atom**>(Counter, "MoleculeLeafClass::FillListOfLocalAtoms - ***ListOfLocalAtoms");
    11211174    if (ListOfLocalAtoms == NULL) {
    11221175      FreeList = FreeList && false;
    11231176      status = false;
    11241177    }
    1125     for (int i=0;i<Counter;i++)
    1126       ListOfLocalAtoms[i] = NULL;
    11271178  }
    11281179
    11291180  if ((ListOfLocalAtoms != NULL) && (ListOfLocalAtoms[FragmentCounter] == NULL)) { // allocate and fill list of this fragment/subgraph
    1130     status = status && Leaf->CreateFatherLookupTable(ListOfLocalAtoms[FragmentCounter], GlobalAtomCount);
     1181    status = status && CreateFatherLookupTable(Leaf->start, Leaf->end, ListOfLocalAtoms[FragmentCounter], GlobalAtomCount);
    11311182    FreeList = FreeList && true;
    11321183  }
     
    11521203  DoLog(1) && (Log() << Verbose(1) << "Begin of AssignKeySetsToFragment." << endl);
    11531204  // fill ListOfLocalAtoms if NULL was given
    1154   if (!FillListOfLocalAtoms(ListOfLocalAtoms, FragmentCounter, reference->getAtomCount(), FreeList)) {
     1205  if (!FillListOfLocalAtoms(ListOfLocalAtoms, FragmentCounter, reference->AtomCount, FreeList)) {
    11551206    DoLog(1) && (Log() << Verbose(1) << "Filling of ListOfLocalAtoms failed." << endl);
    11561207    return false;
     
    11601211  if (FragmentList == NULL) {
    11611212    KeySetCounter = Count();
    1162     FragmentList = new Graph*[KeySetCounter];
    1163     for (int i=0;i<KeySetCounter;i++)
    1164       FragmentList[i] = NULL;
     1213    FragmentList = Calloc<Graph*>(KeySetCounter, "MoleculeLeafClass::AssignKeySetsToFragment - **FragmentList");
    11651214    KeySetCounter = 0;
    11661215  }
     
    11961245  if ((FreeList) && (ListOfLocalAtoms != NULL)) {
    11971246    // free the index lookup list
    1198     delete[](ListOfLocalAtoms[FragmentCounter]);
     1247    Free(&ListOfLocalAtoms[FragmentCounter]);
    11991248    if (FragmentCounter == 0) // first fragments frees the initial pointer to list
    1200       delete[](ListOfLocalAtoms);
     1249      Free(&ListOfLocalAtoms);
    12011250  }
    12021251  DoLog(1) && (Log() << Verbose(1) << "End of AssignKeySetsToFragment." << endl);
Note: See TracChangeset for help on using the changeset viewer.