Index: src/Descriptors/MoleculeDescriptor.cpp
===================================================================
--- src/Descriptors/MoleculeDescriptor.cpp	(revision 1c51c861e1b8694410e5e4704ed0144951b98c2e)
+++ src/Descriptors/MoleculeDescriptor.cpp	(revision 1c51c861e1b8694410e5e4704ed0144951b98c2e)
@@ -0,0 +1,174 @@
+/*
+ * MoleculeDescriptor.cpp
+ *
+ *  Created on: Feb 5, 2010
+ *      Author: crueger
+ */
+
+#include "Descriptors/MoleculeDescriptor.hpp"
+#include "Descriptors/MoleculeDescriptor_impl.hpp"
+
+#include "World.hpp"
+
+#include "molecule.hpp"
+
+#include <boost/bind.hpp>
+#include <cassert>
+#include <iostream>
+
+using namespace std;
+
+typedef World::MoleculeSet::iterator molecules_iter_t;
+
+/************************ Forwarding object **************************************/
+
+
+MoleculeDescriptor::MoleculeDescriptor(impl_ptr _impl) :
+    impl(_impl)
+{}
+
+MoleculeDescriptor::MoleculeDescriptor(const MoleculeDescriptor& src) :
+    impl(src.get_impl())
+{}
+
+MoleculeDescriptor::~MoleculeDescriptor()
+{}
+
+MoleculeDescriptor& MoleculeDescriptor::operator=(MoleculeDescriptor &src){
+  if(&src!=this) {
+    impl=src.get_impl();
+  }
+  return *this;
+}
+
+molecule* MoleculeDescriptor::find(){
+  return impl->find();
+}
+
+std::vector<molecule*> MoleculeDescriptor::findAll(){
+  return impl->findAll();
+}
+
+MoleculeDescriptor::impl_ptr MoleculeDescriptor::get_impl() const{
+  return impl;
+}
+
+
+
+
+/**************************** implementation ********************/
+
+MoleculeDescriptor_impl::MoleculeDescriptor_impl()
+{
+}
+
+MoleculeDescriptor_impl::~MoleculeDescriptor_impl()
+{
+}
+
+World::MoleculeSet& MoleculeDescriptor_impl::getMolecules(){
+  return World::get()->molecules;
+}
+
+molecule* MoleculeDescriptor_impl::find() {
+  World::MoleculeSet molecules = getMolecules();
+  molecules_iter_t res = find_if(molecules.begin(),molecules.end(),boost::bind(&MoleculeDescriptor_impl::predicate,this,_1));
+  return (res!=molecules.end())?((*res).second):0;
+}
+
+vector<molecule*> MoleculeDescriptor_impl::findAll() {
+  vector<molecule*> res;
+  World::MoleculeSet molecules = getMolecules();
+  molecules_iter_t iter;
+  for(iter=molecules.begin();iter!=molecules.end();++iter) {
+    if(predicate(*iter)){
+      res.push_back((*iter).second);
+    }
+  }
+  return res;
+}
+
+/************************** Universe and Emptyset *****************/
+
+MoleculeAllDescriptor_impl::MoleculeAllDescriptor_impl()
+{}
+
+MoleculeAllDescriptor_impl::~MoleculeAllDescriptor_impl()
+{}
+
+bool MoleculeAllDescriptor_impl::predicate(std::pair<moleculeId_t,molecule*>){
+  return true;
+}
+
+MoleculeDescriptor AllMolecules(){
+  return MoleculeDescriptor(MoleculeDescriptor::impl_ptr(new MoleculeAllDescriptor_impl));
+}
+
+MoleculeNoneDescriptor_impl::MoleculeNoneDescriptor_impl()
+{}
+
+MoleculeNoneDescriptor_impl::~MoleculeNoneDescriptor_impl()
+{}
+
+bool MoleculeNoneDescriptor_impl::predicate(std::pair<moleculeId_t,molecule*>){
+  return false;
+}
+
+MoleculeDescriptor NoMolecules(){
+  return MoleculeDescriptor(MoleculeDescriptor::impl_ptr(new MoleculeNoneDescriptor_impl));
+}
+
+/************************** Operator stuff ************************/
+
+// AND
+MoleculeAndDescriptor_impl::MoleculeAndDescriptor_impl(MoleculeDescriptor::impl_ptr _lhs, MoleculeDescriptor::impl_ptr _rhs) :
+    lhs(_lhs), rhs(_rhs)
+{}
+
+MoleculeAndDescriptor_impl::~MoleculeAndDescriptor_impl()
+{}
+
+bool MoleculeAndDescriptor_impl::predicate(std::pair<moleculeId_t,molecule*> molecule){
+  return lhs->predicate(molecule) && rhs->predicate(molecule);
+}
+MoleculeDescriptor operator&&(const MoleculeDescriptor &lhs, const MoleculeDescriptor &rhs){
+  MoleculeDescriptor::impl_ptr newImpl = MoleculeDescriptor::impl_ptr(new MoleculeAndDescriptor_impl(lhs.get_impl(),rhs.get_impl()));
+  return MoleculeDescriptor(newImpl);
+}
+
+// OR
+MoleculeOrDescriptor_impl::MoleculeOrDescriptor_impl(MoleculeDescriptor::impl_ptr _lhs ,MoleculeDescriptor::impl_ptr _rhs) :
+    lhs(_lhs), rhs(_rhs)
+{}
+
+MoleculeOrDescriptor_impl::~MoleculeOrDescriptor_impl(){
+}
+
+bool MoleculeOrDescriptor_impl::predicate(std::pair<moleculeId_t,molecule*> molecule){
+  return lhs->predicate(molecule) || rhs->predicate(molecule);
+}
+
+MoleculeDescriptor  operator||(const MoleculeDescriptor &lhs, const MoleculeDescriptor &rhs){
+  MoleculeDescriptor::impl_ptr newImpl = MoleculeDescriptor::impl_ptr(new MoleculeOrDescriptor_impl(lhs.get_impl(),rhs.get_impl()));
+  return MoleculeDescriptor(newImpl);
+}
+
+// NOT
+
+MoleculeNotDescriptor_impl::MoleculeNotDescriptor_impl(MoleculeDescriptor::impl_ptr _arg) :
+    arg(_arg)
+{}
+
+
+MoleculeNotDescriptor_impl::~MoleculeNotDescriptor_impl()
+{
+}
+
+bool MoleculeNotDescriptor_impl::predicate(std::pair<moleculeId_t,molecule*> molecule){
+ return !(arg->predicate(molecule));
+}
+
+MoleculeDescriptor operator!(const MoleculeDescriptor &arg){
+  MoleculeDescriptor::impl_ptr newImpl = MoleculeDescriptor::impl_ptr(new MoleculeNotDescriptor_impl(arg.get_impl()));
+  return MoleculeDescriptor(newImpl);
+}
Index: src/Descriptors/MoleculeDescriptor.hpp
===================================================================
--- src/Descriptors/MoleculeDescriptor.hpp	(revision 1c51c861e1b8694410e5e4704ed0144951b98c2e)
+++ src/Descriptors/MoleculeDescriptor.hpp	(revision 1c51c861e1b8694410e5e4704ed0144951b98c2e)
@@ -0,0 +1,114 @@
+/*
+ * MoleculeDescriptor.hpp
+ *
+ *  Created on: Feb 5, 2010
+ *      Author: crueger
+ */
+
+#ifndef MOLECULEDESCRIPTOR_HPP_
+#define MOLECULEDESCRIPTOR_HPP_
+
+
+#include <vector>
+#include <map>
+#include <boost/shared_ptr.hpp>
+#include "World.hpp"
+
+class World;
+class molecule;
+
+// internal implementation, allows assignment, copying etc
+class MoleculeDescriptor_impl;
+
+/**
+ * An MoleculeDescriptor describes a Set of Molecules from the World. Can be used for any method that needs to work on
+ * a specific set of Molecules.
+ *
+ * This Class is implemented using the PIMPL-Idion, i.e. this class only contains an abstract structure
+ * that forwards any request to a wrapped pointer-to-implementation. This way operators and calculations
+ * on Descriptors are possible.
+ *
+ * Concrete Implementation Objects can be shared between multiple Wrappers, so make sure that
+ * any Implementation remainst constant during lifetime.
+ */
+class MoleculeDescriptor {
+  // close coupling to the world to allow access
+  friend molecule* World::getMolecule(MoleculeDescriptor descriptor);
+  friend std::vector<molecule*> World::getAllMolecules(MoleculeDescriptor descriptor);
+  friend class World::MoleculeIterator;
+
+  friend MoleculeDescriptor operator&&(const MoleculeDescriptor &lhs, const MoleculeDescriptor &rhs);
+  friend MoleculeDescriptor operator||(const MoleculeDescriptor &lhs, const MoleculeDescriptor &rhs);
+  friend MoleculeDescriptor operator!(const MoleculeDescriptor &arg);
+
+public:
+  typedef boost::shared_ptr<MoleculeDescriptor_impl> impl_ptr; //!< Allow easy changes of the pointer-to-implementation type
+
+  MoleculeDescriptor(impl_ptr);
+
+  /**
+   * Copy constructor.
+   * Takes the Implementation from the copied object and sets it's own pointer to link there.
+   * This way the actuall implementation object is shared between copy and original
+   */
+  MoleculeDescriptor(const MoleculeDescriptor&);
+  ~MoleculeDescriptor();
+
+  /**
+   * Assignment Operator.
+   *
+   * Implemented by setting the pointer to the new Implementation.
+   */
+  MoleculeDescriptor &operator=(MoleculeDescriptor &);
+
+protected:
+  /**
+   * forward Method to implementation
+   */
+  molecule* find();
+
+  /**
+   * forward Method to implementation
+   */
+  std::vector<molecule*> findAll();
+
+  /**
+   * Return the implementation this Wrapper currently points to.
+   * Used for copying, assignment and in Iterators over subsets of the World.
+   */
+  impl_ptr get_impl() const;
+
+private:
+  impl_ptr impl;
+};
+
+/**
+ * produce an Moleculedescriptor that at the point of construction contains an implementation that matches all Molecules
+ */
+MoleculeDescriptor AllMolecules();
+
+/**
+ * produce an Moleculedescriptor that at the point of construction contains an implementation that matches no Molecules
+ */
+MoleculeDescriptor NoMolecules();
+
+/**
+ * Set Intersection for two Moleculedescriptors. The resulting Moleculedescriptor will only match an Molecule if both
+ * given Moleculedescriptors also match. Uses short circuit inside, so the second predicate wont be called
+ * when the first one failed.
+ */
+MoleculeDescriptor operator&&(const MoleculeDescriptor &lhs, const MoleculeDescriptor &rhs);
+
+/**
+ * Set Union for two MoleculeDescriptors. The resulting MoleculeDescriptor will match an Molecule if at least one of
+ * the two given MoleculeDescriptors does match. Used short circuit inside, so the second predicate wont
+ * be called when the first one failed.
+ */
+MoleculeDescriptor operator||(const MoleculeDescriptor &lhs, const MoleculeDescriptor &rhs);
+
+/**
+ * Set inversion for an MoleculeDescriptor. Matches an Molecule if the given MoleculeDescriptor did not match.
+ */
+MoleculeDescriptor operator!(const MoleculeDescriptor &arg);
+
+#endif /* MOLECULEDESCRIPTOR_HPP_ */
Index: src/Descriptors/MoleculeDescriptor_impl.hpp
===================================================================
--- src/Descriptors/MoleculeDescriptor_impl.hpp	(revision 1c51c861e1b8694410e5e4704ed0144951b98c2e)
+++ src/Descriptors/MoleculeDescriptor_impl.hpp	(revision 1c51c861e1b8694410e5e4704ed0144951b98c2e)
@@ -0,0 +1,145 @@
+#ifndef MOLECULEDESCRIPTOR_IMPL_HPP
+#define MOLECULEDESCRIPTOR_IMPL_HPP
+
+#include "Descriptors/MoleculeDescriptor.hpp"
+
+/************************ Declarations of implementation Objects ************************/
+
+/**
+ * This class implements a general Base class for MoleculeDescriptors using the PIMPL-Idiom
+ *
+ * The predicate for this class is empty and should be implemented by derived classes.
+ * By the predicate it is described which molecules should be picked for a given descriptor.
+ */
+
+class MoleculeDescriptor_impl
+{
+  friend class MoleculeDescriptor;
+public:
+
+  MoleculeDescriptor_impl();
+  virtual ~MoleculeDescriptor_impl();
+
+  /**
+   * Implement this abstract Method to make a concrete MoleculeDescriptor pick certain Molecules
+   */
+  virtual bool predicate(std::pair<moleculeId_t,molecule*>)=0;
+
+protected:
+
+  /**
+   * This method is called when the Descriptor is used to find the first matching
+   * Molecule. Walks through all Molecules and stops on the first match. Can be implemented
+   * when the searched Molecule can be found in a more efficient way. Calculated
+   * Moleculedescriptors will always use this method, so no improvement there.
+   */
+  virtual molecule* find();
+
+  /**
+   * This method is called when the Descriptor is used to find all matching Molecules.
+   * Walks through all Molecules and tests the predicate on each one. A vector of all
+   * matching Molecules is returned.
+   */
+  virtual std::vector<molecule*> findAll();
+
+  /**
+   * This method is used internally to query the Set of Molecules from the world.
+   * By using this method derived classes can also access the Internal World Datastructre.
+   * Implemented in full in the Base Descriptor Implementation, so only this one method
+   * needs to be friend with the World class.
+   */
+  World::MoleculeSet& getMolecules();
+};
+
+/************************** Universe and Emptyset *****************/
+
+/**
+ * A simple MoleculeDescriptor that will always match all Molecules present in the World.
+ */
+class MoleculeAllDescriptor_impl : public MoleculeDescriptor_impl {
+public:
+  MoleculeAllDescriptor_impl();
+  virtual ~MoleculeAllDescriptor_impl();
+
+  /**
+   * Always returns true for any Molecule
+   */
+  virtual bool predicate(std::pair<moleculeId_t,molecule*>);
+};
+
+
+/**
+ * An MoleculeDescriptor that never matches any Molecule in the World.
+ */
+class MoleculeNoneDescriptor_impl : public MoleculeDescriptor_impl {
+public:
+  MoleculeNoneDescriptor_impl();
+  virtual ~MoleculeNoneDescriptor_impl();
+
+  /**
+   * Always returns false for any Molecule
+   */
+  virtual bool predicate(std::pair<moleculeId_t,molecule*>);
+};
+
+/************************** Operator stuff ************************/
+
+/**
+ * Intersection of two MoleculeDescriptors
+ */
+class MoleculeAndDescriptor_impl : public MoleculeDescriptor_impl
+{
+public:
+  MoleculeAndDescriptor_impl(MoleculeDescriptor::impl_ptr _lhs, MoleculeDescriptor::impl_ptr _rhs);
+  ~MoleculeAndDescriptor_impl();
+
+  /**
+   * This predicate uses the predicate from the first && the predicate from the
+   * second Descriptor to decide if an Molecule should be selected.
+   */
+  virtual bool predicate(std::pair<moleculeId_t,molecule*>);
+
+private:
+  MoleculeDescriptor::impl_ptr lhs;
+  MoleculeDescriptor::impl_ptr rhs;
+};
+
+/**
+ * Union of two MoleculeDescriptors
+ */
+class MoleculeOrDescriptor_impl : public MoleculeDescriptor_impl
+{
+public:
+  MoleculeOrDescriptor_impl(MoleculeDescriptor::impl_ptr _lhs, MoleculeDescriptor::impl_ptr _rhs);
+  virtual ~MoleculeOrDescriptor_impl();
+
+  /**
+   * This predicate uses the predicate form the first || the predicate from the
+   * second Descriptor to decide if an Molecule should be selected.
+   */
+  virtual bool predicate(std::pair<moleculeId_t,molecule*>);
+
+private:
+  MoleculeDescriptor::impl_ptr lhs;
+  MoleculeDescriptor::impl_ptr rhs;
+};
+
+/**
+ * Set Inversion of a Descriptor
+ */
+class MoleculeNotDescriptor_impl : public MoleculeDescriptor_impl
+{
+public:
+  MoleculeNotDescriptor_impl(MoleculeDescriptor::impl_ptr _arg);
+  virtual ~MoleculeNotDescriptor_impl();
+
+  /**
+   * Opposite of the given descriptor predicate.
+   */
+  virtual bool predicate(std::pair<moleculeId_t,molecule*>);
+
+private:
+  MoleculeDescriptor::impl_ptr arg;
+};
+
+#endif //MOLECULEDESCRIPTOR_IMPL_HPP
