Index: src/Fragmentation/AdaptivityMap.cpp
===================================================================
--- src/Fragmentation/AdaptivityMap.cpp	(revision 6d551c63a46260c606910ef3f9b4e0bccf1a3cac)
+++ src/Fragmentation/AdaptivityMap.cpp	(revision f968742be974e94db1c9b45ea28719c21790e2c1)
@@ -42,4 +42,5 @@
 
 #include "Atom/atom.hpp"
+#include "Fragmentation/AtomMask.hpp"
 #include "Helpers/defs.hpp"
 #include "Helpers/helpers.hpp"
@@ -191,10 +192,10 @@
 /** Marks all candidate sites for update if below adaptive threshold.
  * Picks a given number of highest values and set *AtomMask to true.
- * \param *AtomMask defines true/false per global Atom::Nr to mask in/out each nuclear site, used to activate given number of site to increment order adaptively
+ * \param AtomMask defines true/false per global Atom::Nr to mask in/out each nuclear site, used to activate given number of site to increment order adaptively
  * \param Order desired order
  * \param *mol molecule with atoms
  * \return true - if update is necessary, false - not
  */
-bool AdaptivityMap::MarkUpdateCandidates(bool *AtomMask, int Order, molecule *mol) const
+bool AdaptivityMap::MarkUpdateCandidates(AtomMask_t &AtomMask, int Order, molecule *mol) const
 {
   atom *Walker = NULL;
@@ -208,5 +209,5 @@
     //if (Walker->AdaptiveOrder < MinimumRingSize[Walker->getNr()]) {
       LOG(2, "Root " << No << " is still above threshold (10^{" << Order <<"}: " << runner->first << ", setting entry " << No << " of Atom mask to true.");
-      AtomMask[No] = true;
+      AtomMask.setTrue(No);
       status = true;
     //} else
Index: src/Fragmentation/AdaptivityMap.hpp
===================================================================
--- src/Fragmentation/AdaptivityMap.hpp	(revision 6d551c63a46260c606910ef3f9b4e0bccf1a3cac)
+++ src/Fragmentation/AdaptivityMap.hpp	(revision f968742be974e94db1c9b45ea28719c21790e2c1)
@@ -21,4 +21,5 @@
 #include "Fragmentation/KeySet.hpp"
 
+class AtomMask_t;
 class molecule;
 
@@ -33,5 +34,5 @@
   void ReMapAdaptiveCriteriaListToValue(molecule *mol);
   int CountLinesinFile(std::ifstream &InputFile) const;
-  bool MarkUpdateCandidates(bool *AtomMask, int Order, molecule *mol) const ;
+  bool MarkUpdateCandidates(AtomMask_t &AtomMask, int Order, molecule *mol) const ;
 
   bool IsAdaptiveCriteriaListEmpty() const;
Index: src/Fragmentation/AtomMask.hpp
===================================================================
--- src/Fragmentation/AtomMask.hpp	(revision f968742be974e94db1c9b45ea28719c21790e2c1)
+++ src/Fragmentation/AtomMask.hpp	(revision f968742be974e94db1c9b45ea28719c21790e2c1)
@@ -0,0 +1,28 @@
+/*
+ * AtomMask.hpp
+ *
+ *  Created on: Sep 7, 2012
+ *      Author: heber
+ */
+
+#ifndef ATOMMASK_HPP_
+#define ATOMMASK_HPP_
+
+
+// include config.h
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "Fragmentation/Mask.hpp"
+#include "types.hpp"
+
+/** AtomMask is a filter for a atomic ids. The bit is either true or false.
+ *
+ */
+struct AtomMask_t : public Mask_t<atomId_t>
+{
+
+};
+
+#endif /* ATOMMASK_HPP_ */
Index: src/Fragmentation/Fragmentation.cpp
===================================================================
--- src/Fragmentation/Fragmentation.cpp	(revision 6d551c63a46260c606910ef3f9b4e0bccf1a3cac)
+++ src/Fragmentation/Fragmentation.cpp	(revision f968742be974e94db1c9b45ea28719c21790e2c1)
@@ -46,4 +46,5 @@
 #include "Element/periodentafel.hpp"
 #include "Fragmentation/AdaptivityMap.hpp"
+#include "Fragmentation/AtomMask.hpp"
 #include "Fragmentation/fragmentation_helpers.hpp"
 #include "Fragmentation/Graph.hpp"
@@ -107,5 +108,5 @@
   Graph TotalGraph;     // graph with all keysets however local numbers
   int TotalNumberOfKeySets = 0;
-  bool *AtomMask = NULL;
+  AtomMask_t AtomMask;
 
   LOG(0, endl);
@@ -170,10 +171,9 @@
   // ===== 6b. prepare and go into the adaptive (Order<0), single-step (Order==0) or incremental (Order>0) cycle
   KeyStack *RootStack = new KeyStack[Subgraphs->next->Count()];
-  AtomMask = new bool[mol->getAtomCount()+1];
-  AtomMask[mol->getAtomCount()] = false;
   FragmentationToDo = false;  // if CheckOrderAtSite just ones recommends fragmentation, we will save fragments afterwards
-  while ((CheckOrder = CheckOrderAtSite(AtomMask, &ParsedFragmentList, Order, prefix))) {
+  bool LoopDoneAlready = false;
+  while ((CheckOrder = CheckOrderAtSite(AtomMask, &ParsedFragmentList, Order, prefix, LoopDoneAlready))) {
     FragmentationToDo = FragmentationToDo || CheckOrder;
-    AtomMask[mol->getAtomCount()] = true;   // last plus one entry is used as marker that we have been through this loop once already in CheckOrderAtSite()
+    LoopDoneAlready = true;   // last plus one entry is used as marker that we have been through this loop once already in CheckOrderAtSite()
     // ===== 6b. fill RootStack for each subgraph (second adaptivity check) =====
     Subgraphs->next->FillRootStackForSubgraphs(RootStack, AtomMask, (FragmentCounter = 0), saturation);
@@ -197,5 +197,4 @@
   LOG(2, "CheckOrder is " << CheckOrder << ".");
   delete[](RootStack);
-  delete[](AtomMask);
 
   // ==================================== End of FRAGMENTATION ============================================
@@ -479,20 +478,20 @@
 
 /** Checks whether the OrderAtSite is still below \a Order at some site.
- * \param *AtomMask defines true/false per global Atom::Nr to mask in/out each nuclear site, used to activate given number of site to increment order adaptively
+ * \param AtomMask defines true/false per global Atom::Nr to mask in/out each nuclear site, used to activate given number of site to increment order adaptively
  * \param *GlobalKeySetList list of keysets with global ids (valid in "this" molecule) needed for adaptive increase
  * \param Order desired Order if positive, desired exponent in threshold criteria if negative (0 is single-step)
  * \param path path to ENERGYPERFRAGMENT file (may be NULL if Order is non-negative)
+ * \param LoopDoneAlready indicate whether we have done a fragmentation loop already
  * \return true - needs further fragmentation, false - does not need fragmentation
  */
-bool Fragmentation::CheckOrderAtSite(bool *AtomMask, Graph *GlobalKeySetList, int Order, std::string path)
+bool Fragmentation::CheckOrderAtSite(AtomMask_t &AtomMask, Graph *GlobalKeySetList, int Order, std::string path, bool LoopDoneAlready)
 {
   bool status = false;
 
   // initialize mask list
-  for(int i=mol->getAtomCount();i--;)
-    AtomMask[i] = false;
+  AtomMask.clear();
 
   if (Order < 0) { // adaptive increase of BondOrder per site
-    if (AtomMask[mol->getAtomCount()] == true)  // break after one step
+    if (LoopDoneAlready)  // break after one step
       return false;
 
@@ -515,5 +514,5 @@
         if ((saturation == DontSaturate) || ((*iter)->getType()->getAtomicNumber() != 1)) // skip hydrogen
         {
-          AtomMask[(*iter)->getNr()] = true;  // include all (non-hydrogen) atoms
+          AtomMask.setTrue((*iter)->getNr());  // include all (non-hydrogen) atoms
           status = true;
         }
@@ -528,10 +527,10 @@
       if ((saturation == DontSaturate) || ((*iter)->getType()->getAtomicNumber() != 1)) // skip hydrogen
       {
-        AtomMask[(*iter)->getNr()] = true;  // include all (non-hydrogen) atoms
+        AtomMask.setTrue((*iter)->getNr());  // include all (non-hydrogen) atoms
         if ((Order != 0) && ((*iter)->AdaptiveOrder < Order)) // && ((*iter)->AdaptiveOrder < MinimumRingSize[(*iter)->getNr()]))
           status = true;
       }
     }
-    if ((!Order) && (!AtomMask[mol->getAtomCount()]))  // single stepping, just check
+    if ((!Order) && (!LoopDoneAlready))  // single stepping, just check
       status = true;
 
Index: src/Fragmentation/Fragmentation.hpp
===================================================================
--- src/Fragmentation/Fragmentation.hpp	(revision 6d551c63a46260c606910ef3f9b4e0bccf1a3cac)
+++ src/Fragmentation/Fragmentation.hpp	(revision f968742be974e94db1c9b45ea28719c21790e2c1)
@@ -25,4 +25,5 @@
 
 class atom;
+class AtomMask_t;
 class Graph;
 class KeySet;
@@ -47,5 +48,5 @@
 
   // order at site
-  bool CheckOrderAtSite(bool *AtomMask, Graph *GlobalKeySetList, int Order, std::string path);
+  bool CheckOrderAtSite(AtomMask_t &AtomMask, Graph *GlobalKeySetList, int Order, std::string path, bool LoopDoneAlready);
   bool StoreOrderAtSiteFile(std::string &path);
   bool ParseOrderAtSiteFromFile(std::string &path);
Index: src/Fragmentation/Makefile.am
===================================================================
--- src/Fragmentation/Makefile.am	(revision 6d551c63a46260c606910ef3f9b4e0bccf1a3cac)
+++ src/Fragmentation/Makefile.am	(revision f968742be974e94db1c9b45ea28719c21790e2c1)
@@ -20,4 +20,5 @@
 FRAGMENTATIONHEADER = \
 	Fragmentation/AdaptivityMap.hpp \
+	Fragmentation/AtomMask.hpp \
 	Fragmentation/BondsPerShortestPath.hpp \
 	Fragmentation/defs.hpp \
@@ -34,4 +35,6 @@
 	Fragmentation/KeySetsContainer.hpp \
 	Fragmentation/MatrixContainer.hpp \
+	Fragmentation/Mask.hpp \
+	Fragmentation/MoleculeMask.hpp \
 	Fragmentation/PowerSetGenerator.hpp \
 	Fragmentation/UniqueFragments.hpp
Index: src/Fragmentation/Mask.hpp
===================================================================
--- src/Fragmentation/Mask.hpp	(revision f968742be974e94db1c9b45ea28719c21790e2c1)
+++ src/Fragmentation/Mask.hpp	(revision f968742be974e94db1c9b45ea28719c21790e2c1)
@@ -0,0 +1,84 @@
+/*
+ * Mask.hpp
+ *
+ *  Created on: Sep 7, 2012
+ *      Author: heber
+ */
+
+#ifndef MASK_HPP_
+#define MASK_HPP_
+
+
+// include config.h
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <set>
+
+#include "CodePatterns/Assert.hpp"
+
+#include "types.hpp"
+
+/** A Mask_t is just a set of ids such that one can use the set as a filter
+ * for any type by looking them up in the map.
+ *
+ * The interface is such that we have a bitmask for every given id. Only we
+ * store only ids associated to true.
+ */
+template <typename T>
+struct Mask_t : public std::set<T>
+{
+  /** Checks whether the bit for the given \a id is true.
+   *
+   * @param id id to check
+   * @return true - bit set, false - bit not set
+   */
+  bool isTrue(const T id) const {
+    const typename std::set<T>::const_iterator iter = std::set<T>::find(id);
+    return (iter != std::set<T>::end());
+  }
+
+  /** Checks whether the bit for the given \a id is true.
+   *
+   * @param id id to check
+   * @return true - bit set, false - bit not set
+   */
+  bool isFalse(const T id) const {
+    return (!isTrue(id));
+  }
+
+  /** Setter for the bit associated to \a id.
+   *
+   * @param id id to set to true
+   */
+  void setTrue(const T id)
+  {
+    insert(id);
+  }
+
+  /** Unsetter for the bit associated to \a id.
+   *
+   * @param id id to set to false
+   */
+  void setFalse(const T id)
+  {
+    typename std::set<T>::iterator iter = std::set<T>::find(id);
+    if (iter != std::set<T>::end())
+      erase(iter);
+  }
+
+  /** Use in printing the bit mask bit by bit.
+   *
+   * @param id bit to print
+   * @return "t" or "f" whether bit is set or not.
+   */
+  const char* printBit(const T id) const
+  {
+    const typename std::set<T>::const_iterator iter = std::set<T>::find(id);
+    return (iter != std::set<T>::end() ? "t" : "f");
+  }
+};
+
+
+#endif /* MASK_HPP_ */
Index: src/Fragmentation/MoleculeMask.hpp
===================================================================
--- src/Fragmentation/MoleculeMask.hpp	(revision f968742be974e94db1c9b45ea28719c21790e2c1)
+++ src/Fragmentation/MoleculeMask.hpp	(revision f968742be974e94db1c9b45ea28719c21790e2c1)
@@ -0,0 +1,30 @@
+/*
+ * MoleculeMask.hpp
+ *
+ *  Created on: Sep 7, 2012
+ *      Author: heber
+ */
+
+#ifndef MOLECULEMASK_HPP_
+#define MOLECULEMASK_HPP_
+
+
+// include config.h
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include "Fragmentation/Mask.hpp"
+#include "types.hpp"
+
+/** MoleculeMask is a filter for a molecule ids. The bit is either true or false.
+ *
+ */
+struct MoleculeMask_t : public Mask_t<moleculeId_t>
+{
+
+};
+
+
+
+#endif /* MOLECULEMASK_HPP_ */
Index: src/Fragmentation/fragmentation_helpers.cpp
===================================================================
--- src/Fragmentation/fragmentation_helpers.cpp	(revision 6d551c63a46260c606910ef3f9b4e0bccf1a3cac)
+++ src/Fragmentation/fragmentation_helpers.cpp	(revision f968742be974e94db1c9b45ea28719c21790e2c1)
@@ -45,4 +45,5 @@
 #include "Element/element.hpp"
 #include "Fragmentation/AdaptivityMap.hpp"
+#include "Fragmentation/AtomMask.hpp"
 #include "Fragmentation/Graph.hpp"
 #include "Fragmentation/KeySet.hpp"
@@ -54,9 +55,8 @@
 
 /** print atom mask for debugging.
- * \param *out output stream for debugging
- * \param *AtomMask defines true/false per global Atom::Nr to mask in/out each nuclear site, used to activate given number of site to increment order adaptively
+ * \param AtomMask defines true/false per global Atom::Nr to mask in/out each nuclear site, used to activate given number of site to increment order adaptively
  * \param AtomCount number of entries in \a *AtomMask
  */
-void PrintAtomMask(bool *AtomMask, int AtomCount)
+void PrintAtomMask(const AtomMask_t &AtomMask, int AtomCount)
 {
   {
@@ -71,5 +71,5 @@
     output << "Atom mask is: ";
     for(int i=0;i<AtomCount;i++)
-      output << (AtomMask[i] ? "t" : "f");
+      output << AtomMask.printBit(i);
     LOG(2, output.str());
   }
Index: src/Fragmentation/fragmentation_helpers.hpp
===================================================================
--- src/Fragmentation/fragmentation_helpers.hpp	(revision 6d551c63a46260c606910ef3f9b4e0bccf1a3cac)
+++ src/Fragmentation/fragmentation_helpers.hpp	(revision f968742be974e94db1c9b45ea28719c21790e2c1)
@@ -22,9 +22,10 @@
 typedef std::deque<int> KeyStack;
 
+class AtomMask_t;
 class Graph;
 class KeySet;
 class molecule;
 
-void PrintAtomMask(bool *AtomMask, int AtomCount);
+void PrintAtomMask(const AtomMask_t &AtomMask, int AtomCount);
 
 int CombineAllOrderListIntoOne(Graph *&FragmentList, Graph ***FragmentLowerOrdersList, KeyStack &RootStack, molecule *mol);
Index: src/MoleculeLeafClass.cpp
===================================================================
--- src/MoleculeLeafClass.cpp	(revision 6d551c63a46260c606910ef3f9b4e0bccf1a3cac)
+++ src/MoleculeLeafClass.cpp	(revision f968742be974e94db1c9b45ea28719c21790e2c1)
@@ -41,4 +41,5 @@
 #include "Atom/atom.hpp"
 #include "Element/element.hpp"
+#include "Fragmentation/AtomMask.hpp"
 #include "Fragmentation/Graph.hpp"
 #include "Fragmentation/KeySet.hpp"
@@ -119,10 +120,10 @@
  * \param *out output stream for debugging
  * \param *&RootStack stack to be filled
- * \param *AtomMask defines true/false per global Atom::Nr to mask in/out each nuclear site
+ * \param AtomMask defines true/false per global Atom::Nr to mask in/out each nuclear site
  * \param &FragmentCounter counts through the fragments in this MoleculeLeafClass
  * \param saturation whether to treat hydrogen special or not
  * \return true - stack is non-empty, fragmentation necessary, false - stack is empty, no more sites to update
  */
-bool MoleculeLeafClass::FillRootStackForSubgraphs(KeyStack *&RootStack, bool *AtomMask, int &FragmentCounter, const enum HydrogenSaturation saturation)
+bool MoleculeLeafClass::FillRootStackForSubgraphs(KeyStack *&RootStack, const AtomMask_t &AtomMask, int &FragmentCounter, const enum HydrogenSaturation saturation)
 {
   if (RootStack != NULL) {
@@ -132,5 +133,5 @@
       for(molecule::const_iterator iter = Leaf->begin(); iter != Leaf->end(); ++iter) {
         const atom * const Father = (*iter)->GetTrueFather();
-        if (AtomMask[Father->getNr()]) // apply mask
+        if (AtomMask.isTrue(Father->getNr())) // apply mask
           if ((saturation == DontSaturate) || ((*iter)->getType()->getAtomicNumber() != 1)) // skip hydrogen
             RootStack[FragmentCounter].push_front((*iter)->getNr());
Index: src/MoleculeLeafClass.hpp
===================================================================
--- src/MoleculeLeafClass.hpp	(revision 6d551c63a46260c606910ef3f9b4e0bccf1a3cac)
+++ src/MoleculeLeafClass.hpp	(revision f968742be974e94db1c9b45ea28719c21790e2c1)
@@ -17,4 +17,5 @@
 
 class atom;
+class AtomMask_t;
 class Graph;
 class ListOfLocalAtoms_t;
@@ -39,5 +40,5 @@
 
   bool AddLeaf(molecule *ptr, MoleculeLeafClass *Previous);
-  bool FillRootStackForSubgraphs(KeyStack *&RootStack, bool *AtomMask, int &FragmentCounter, const enum HydrogenSaturation saturation);
+  bool FillRootStackForSubgraphs(KeyStack *&RootStack, const AtomMask_t &AtomMask, int &FragmentCounter, const enum HydrogenSaturation saturation);
   bool AssignKeySetsToFragment(molecule *reference, Graph *KeySetList, ListOfLocalAtoms_t *&ListOfLocalAtoms, Graph **&FragmentList, int &FragmentCounter, bool FreeList = false);
   void TranslateIndicesToGlobalIDs(Graph **FragmentList, int &FragmentCounter, int &TotalNumberOfKeySets, Graph &TotalGraph);
