/* * HomologyContainer.hpp * * Created on: Sep 22, 2012 * Author: heber */ #ifndef HOMOLOGYCONTAINER_HPP_ #define HOMOLOGYCONTAINER_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #include #include "CodePatterns/IteratorAdaptors.hpp" #include "CodePatterns/Observer/Observable.hpp" #include "Fragmentation/Homology/HomologyGraph.hpp" #include "Fragmentation/Summation/SetValues/Fragment.hpp" #include "Fragmentation/Summation/SetValues/SamplingGrid.hpp" class HomologyContainerTest; /** This class takes all KeySets in a Graph, checks for those that homologues * of one another and places them together. * * This is meant as a storage for key, value pairs, where the key is the KeySet * and the value is the energy associated to the fragment this keyset * represents. * Afterwards this can then be used as training data for a high-dimensional * approximation to the Born-Oppenheimer-surface decomposed into lower- * dimensional terms in an ANOVA-like fashion. * */ class HomologyContainer : public Observable { //!> grant access to output operator friend std::ostream& operator<<(std::ostream &out, const HomologyContainer &container); //!> grant unit test access friend class HomologyContainerTest; public: /** This structure represents all values associated to a specific homology * that we wish to store in this container for later reference. */ struct value_t { Fragment fragment; double energy; bool containsGrids; SamplingGrid charge_distribution; SamplingGrid potential_distribution; value_t() : energy(0.), containsGrids(false) {} bool operator==(const value_t &othervalue) const; private: friend class boost::serialization::access; // serialization template void serialize(Archive& ar, const unsigned int version) { ar & fragment; ar & energy; if (version > 0) { ar & containsGrids; if (containsGrids) { ar & charge_distribution; ar & potential_distribution; } } } }; public: typedef std::multimap< HomologyGraph, value_t> container_t; typedef container_t::const_iterator const_iterator; typedef MapKeyConstIterator const_key_iterator; typedef std::pair< const_iterator, const_iterator> range_t; public: /** Default Constructor of class HomologyContainer. * */ HomologyContainer(); /** Constructor of class HomologyContainer. * * @param values values with with to initially fill the container */ HomologyContainer(const container_t &values); /** Destructor of class HomologyContainer. * */ ~HomologyContainer() {} /** Equality comparator. * * Sadly, the insertion order of a std::multimap's values is not guaranteed * by the standard and boost::serialization does not heed the ordering of * the values associated to the same key. Hence, we implement a weaker * comparator for this class in order for the unit test to pass as we don't * actuallty care about the order of the homologous fragments. * * @param other instance to compare to * @return true - each container contains all elements of the other */ bool operator==(const HomologyContainer &other) const { return ((*this >= other) && (other >= *this)); } bool operator!=(const HomologyContainer& other) const { return !(*this == other); } /** Greater equal comparator, i.e. subset comparator * * @param other container to check if it's subset * @return true - \a other is a subset of this */ bool operator>=(const HomologyContainer &other) const; /** Inserter for more graphs along with values. * * @param values graph and values to insert */ void insert(const container_t &values); /** Returns iterator range with all contained graphs homologous to the given \a graph. * * @param graph graph to match * @return iterator range with all matches */ range_t getHomologousGraphs(const HomologyGraph &graph) const { return container.equal_range(graph); } /** Getter for constant iterator to begin of homologous graph container. * * @return begin constant iterator */ const_iterator begin() const { return container.begin(); } /** Getter for constant iterator to past end of homologous graph container. * * @return past end constant iterator */ const_iterator end() const { return container.end(); } const_key_iterator key_begin() const { return const_key_iterator(container.begin()); } const_key_iterator key_end() const { return const_key_iterator(container.end()); } const_key_iterator getNextKey(const_key_iterator &iter) const { return const_key_iterator(container.upper_bound(*iter)); } /** Clears all homologies from container. * */ void clear(); /** Returns the number of keys in the container. * * \return size of internal container */ const size_t size() { return container.size(); } private: //!> multimap containing all homologous graph under same key but each with its value container_t container; private: friend class boost::serialization::access; // serialization template void load(Archive& ar, const unsigned int version) { OBSERVE; ar & container; } template void save(Archive& ar, const unsigned int version) const { ar & container; } BOOST_SERIALIZATION_SPLIT_MEMBER() }; /** Output operator for HomologyContainer. * * \param out output stream * \param container container to print * \return output stream for concatenation */ std::ostream& operator<<(std::ostream &out, const HomologyContainer &container); // we need to give this class a unique key for serialization BOOST_CLASS_EXPORT_KEY(HomologyContainer) // version for serialized information associated to HomologyGraph BOOST_CLASS_VERSION(HomologyContainer::value_t, 1) #endif /* HOMOLOGYCONTAINER_HPP_ */