Index: src/molecule_fragmentation.cpp
===================================================================
--- src/molecule_fragmentation.cpp	(revision d2943b9740e9b45b8ed74a7c23e842ec060bb5db)
+++ src/molecule_fragmentation.cpp	(revision 14e73aa8344b12c5bad0a9b1eaaffda56d596001)
@@ -1156,35 +1156,14 @@
 };
 
-
-
-/** Creates a list of all unique fragments of certain vertex size from a given graph \a Fragment for a given root vertex in the context of \a this molecule.
- * -# initialises UniqueFragments structure
- * -# fills edge list via BFS
- * -# creates the fragment by calling recursive function SPFragmentGenerator with UniqueFragments structure, 0 as
- root distance, the edge set, its dimension and the current suborder
- * -# Free'ing structure
- * Note that we may use the fact that the atoms are SP-ordered on the atomstack. I.e. when popping always the last, we first get all
- * with SP of 2, then those with SP of 3, then those with SP of 4 and so on.
- * \param *out output stream for debugging
+/** Initializes FragmenSearch.
+ * Sets UniqueFragments::ShortestPathList entries to zero, UniqueFragments::BondsPerSPCount to zero (except zero level to 1) and
+ * adds initial bond UniqueFragments::Root to UniqueFragments::Root to UniqueFragments::BondsPerSPList
+ * \param *out output stream
  * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
- * \param FragmentSearch UniqueFragments structure containing TEFactor, root atom and so on
- * \param RestrictedKeySet Restricted vertex set to use in context of molecule
- * \return number of inserted fragments
- * \note ShortestPathList in FragmentSearch structure is probably due to NumberOfAtomsSPLevel and SP not needed anymore
- */
-int molecule::PowerSetGenerator(ofstream *out, int Order, struct UniqueFragments &FragmentSearch, KeySet RestrictedKeySet)
-{
-  int SP, AtomKeyNr;
-  atom *Walker = NULL, *OtherWalker = NULL, *Predecessor = NULL;
-  bond *Binder = NULL;
-  bond *CurrentEdge = NULL;
-  bond **BondsList = NULL;
-  int RootKeyNr = FragmentSearch.Root->GetTrueFather()->nr;
-  int Counter = FragmentSearch.FragmentCounter;
-  int RemainingWalkers;
-
-  *out << endl;
-  *out << Verbose(0) << "Begin of PowerSetGenerator with order " << Order << " at Root " << *FragmentSearch.Root << "." << endl;
-
+ * \param FragmentSearch UniqueFragments
+ * \sa FreeSPList()
+ */
+void InitialiseSPList(ofstream *out, int Order, struct UniqueFragments &FragmentSearch)
+{
   // prepare Label and SP arrays of the BFS search
   FragmentSearch.ShortestPathList[FragmentSearch.Root->nr] = 0;
@@ -1194,8 +1173,44 @@
     FragmentSearch.BondsPerSPCount[i] = 0;
   FragmentSearch.BondsPerSPCount[0] = 1;
-  Binder = new bond(FragmentSearch.Root, FragmentSearch.Root);
+  bond *Binder = new bond(FragmentSearch.Root, FragmentSearch.Root);
   add(Binder, FragmentSearch.BondsPerSPList[1]);
-
-  // do a BFS search to fill the SP lists and label the found vertices
+};
+
+/** Resets UniqueFragments::ShortestPathList and cleans bonds from UniqueFragments::BondsPerSPList.
+ * \param *out output stream
+ * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
+ * \param FragmentSearch UniqueFragments
+ * \sa InitialiseSPList()
+ */
+void FreeSPList(ofstream *out, int Order, struct UniqueFragments &FragmentSearch)
+{
+  bond *Binder = NULL;
+  *out << Verbose(0) << "Free'ing all found lists. and resetting index lists" << endl;
+  for(int i=Order;i--;) {
+    *out << Verbose(1) << "Current SP level is " << i << ": ";
+    Binder = FragmentSearch.BondsPerSPList[2*i];
+    while (Binder->next != FragmentSearch.BondsPerSPList[2*i+1]) {
+      Binder = Binder->next;
+      // *out << "Removing atom " << Binder->leftatom->nr << " and " << Binder->rightatom->nr << "." << endl; // make sure numbers are local
+      FragmentSearch.ShortestPathList[Binder->leftatom->nr] = -1;
+      FragmentSearch.ShortestPathList[Binder->rightatom->nr] = -1;
+    }
+    // delete added bonds
+    cleanup(FragmentSearch.BondsPerSPList[2*i], FragmentSearch.BondsPerSPList[2*i+1]);
+    // also start and end node
+    *out << "cleaned." << endl;
+  }
+};
+
+
+/** Fills the Bonds per Shortest Path List and set the vertex labels.
+ * \param *out output stream
+ * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
+ * \param FragmentSearch UniqueFragments
+ * \param *mol molecule with atoms and bonds
+ * \param RestrictedKeySet Restricted vertex set to use in context of molecule
+ */
+void FillSPListandLabelVertices(ofstream *out, int Order, struct UniqueFragments &FragmentSearch, molecule *mol, KeySet RestrictedKeySet)
+{
   // Actually, we should construct a spanning tree vom the root atom and select all edges therefrom and put them into
   // according shortest path lists. However, we don't. Rather we fill these lists right away, as they do form a spanning
@@ -1204,5 +1219,14 @@
   // In another picture, the bonds always contain a direction by rightatom being the one more distant from root and hence
   // naturally leftatom forming its predecessor, preventing the BFS"seeker" from continuing in the wrong direction.
-  *out << endl;
+  int AtomKeyNr = -1;
+  atom *Walker = NULL;
+  atom *OtherWalker = NULL;
+  atom *Predecessor = NULL;
+  bond *Binder = NULL;
+  bond *CurrentEdge = NULL;
+  int RootKeyNr = FragmentSearch.Root->GetTrueFather()->nr;
+  int RemainingWalkers = -1;
+  int SP = -1;
+
   *out << Verbose(0) << "Starting BFS analysis ..." << endl;
   for (SP = 0; SP < (Order-1); SP++) {
@@ -1226,6 +1250,6 @@
       // go through all its bonds
       *out << Verbose(1) << "Going through all bonds of Walker." << endl;
-      for (int i=0;i<NumberOfBondsPerAtom[AtomKeyNr];i++) {
-        Binder = ListOfBondsPerAtom[AtomKeyNr][i];
+      for (int i=0;i<mol->NumberOfBondsPerAtom[AtomKeyNr];i++) {
+        Binder = mol->ListOfBondsPerAtom[AtomKeyNr][i];
         OtherWalker = Binder->GetOtherAtom(Walker);
         if ((RestrictedKeySet.find(OtherWalker->nr) != RestrictedKeySet.end())
@@ -1254,6 +1278,14 @@
     }
   }
-
-  // outputting all list for debugging
+};
+
+/** prints the Bonds per Shortest Path list in UniqueFragments.
+ * \param *out output stream
+ * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
+ * \param FragmentSearch UniqueFragments
+ */
+void OutputSPList(ofstream *out, int Order, struct UniqueFragments &FragmentSearch)
+{
+  bond *Binder = NULL;
   *out << Verbose(0) << "Printing all found lists." << endl;
   for(int i=1;i<Order;i++) {    // skip the root edge in the printing
@@ -1265,7 +1297,15 @@
     }
   }
-
-  // creating fragments with the found edge sets  (may be done in reverse order, faster)
-  SP = -1;  // the Root <-> Root edge must be subtracted!
+};
+
+/** Simply counts all bonds in all UniqueFragments::BondsPerSPList lists.
+ * \param *out output stream
+ * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
+ * \param FragmentSearch UniqueFragments
+ */
+int CountNumbersInBondsList(ofstream *out, int Order, struct UniqueFragments &FragmentSearch)
+{
+  bond *Binder = NULL;
+  int SP = -1;  // the Root <-> Root edge must be subtracted!
   for(int i=Order;i--;) { // sum up all found edges
     Binder = FragmentSearch.BondsPerSPList[2*i];
@@ -1275,4 +1315,40 @@
     }
   }
+  return SP;
+};
+
+/** Creates a list of all unique fragments of certain vertex size from a given graph \a Fragment for a given root vertex in the context of \a this molecule.
+ * -# initialises UniqueFragments structure
+ * -# fills edge list via BFS
+ * -# creates the fragment by calling recursive function SPFragmentGenerator with UniqueFragments structure, 0 as
+ root distance, the edge set, its dimension and the current suborder
+ * -# Free'ing structure
+ * Note that we may use the fact that the atoms are SP-ordered on the atomstack. I.e. when popping always the last, we first get all
+ * with SP of 2, then those with SP of 3, then those with SP of 4 and so on.
+ * \param *out output stream for debugging
+ * \param Order bond order (limits BFS exploration and "number of digits" in power set generation
+ * \param FragmentSearch UniqueFragments structure containing TEFactor, root atom and so on
+ * \param RestrictedKeySet Restricted vertex set to use in context of molecule
+ * \return number of inserted fragments
+ * \note ShortestPathList in FragmentSearch structure is probably due to NumberOfAtomsSPLevel and SP not needed anymore
+ */
+int molecule::PowerSetGenerator(ofstream *out, int Order, struct UniqueFragments &FragmentSearch, KeySet RestrictedKeySet)
+{
+  bond **BondsList = NULL;
+  int Counter = FragmentSearch.FragmentCounter; // mark current value of counter
+
+  *out << endl;
+  *out << Verbose(0) << "Begin of PowerSetGenerator with order " << Order << " at Root " << *FragmentSearch.Root << "." << endl;
+
+  InitialiseSPList(out, Order, FragmentSearch);
+
+  // do a BFS search to fill the SP lists and label the found vertices
+  FillSPListandLabelVertices(out, Order, FragmentSearch, this, RestrictedKeySet);
+
+  // outputting all list for debugging
+  OutputSPList(out, Order, FragmentSearch);
+
+  // creating fragments with the found edge sets  (may be done in reverse order, faster)
+  int SP = CountNumbersInBondsList(out, Order, FragmentSearch);
   *out << Verbose(0) << "Total number of edges is " << SP << "." << endl;
   if (SP >= (Order-1)) {
@@ -1281,4 +1357,5 @@
     FragmentSearch.FragmentSet->clear();
     *out << Verbose(0) << "Preparing subset for this root and calling generator." << endl;
+
     // prepare the subset and call the generator
     BondsList = Malloc<bond*>(FragmentSearch.BondsPerSPCount[0], "molecule::PowerSetGenerator: **BondsList");
@@ -1298,19 +1375,5 @@
 
   // free'ing the bonds lists
-  *out << Verbose(0) << "Free'ing all found lists. and resetting index lists" << endl;
-  for(int i=Order;i--;) {
-    *out << Verbose(1) << "Current SP level is " << i << ": ";
-    Binder = FragmentSearch.BondsPerSPList[2*i];
-    while (Binder->next != FragmentSearch.BondsPerSPList[2*i+1]) {
-      Binder = Binder->next;
-      // *out << "Removing atom " << Binder->leftatom->nr << " and " << Binder->rightatom->nr << "." << endl; // make sure numbers are local
-      FragmentSearch.ShortestPathList[Binder->leftatom->nr] = -1;
-      FragmentSearch.ShortestPathList[Binder->rightatom->nr] = -1;
-    }
-    // delete added bonds
-    cleanup(FragmentSearch.BondsPerSPList[2*i], FragmentSearch.BondsPerSPList[2*i+1]);
-    // also start and end node
-    *out << "cleaned." << endl;
-  }
+  FreeSPList(out, Order, FragmentSearch);
 
   // return list
