| [4694df] | 1 | /* | 
|---|
|  | 2 | * HomologyContainer.hpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: Sep 22, 2012 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 | #ifndef HOMOLOGYCONTAINER_HPP_ | 
|---|
|  | 9 | #define HOMOLOGYCONTAINER_HPP_ | 
|---|
|  | 10 |  | 
|---|
|  | 11 |  | 
|---|
|  | 12 | // include config.h | 
|---|
|  | 13 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 14 | #include <config.h> | 
|---|
|  | 15 | #endif | 
|---|
|  | 16 |  | 
|---|
| [12a24c] | 17 | #include <boost/serialization/export.hpp> | 
|---|
|  | 18 | #include <boost/serialization/map.hpp> | 
|---|
| [bcc24f3] | 19 | #include <boost/serialization/split_member.hpp> | 
|---|
| [12a24c] | 20 | #include <boost/serialization/vector.hpp> | 
|---|
| [a2a2f7] | 21 | #include <boost/serialization/version.hpp> | 
|---|
| [12a24c] | 22 |  | 
|---|
| [e15ffe] | 23 | #include <iosfwd> | 
|---|
| [4694df] | 24 | #include <map> | 
|---|
| [77b350] | 25 | #include <vector> | 
|---|
| [4694df] | 26 |  | 
|---|
| [39a07a] | 27 | #include "CodePatterns/IteratorAdaptors.hpp" | 
|---|
| [bcc24f3] | 28 | #include "CodePatterns/Observer/Observable.hpp" | 
|---|
| [39a07a] | 29 |  | 
|---|
| [77b350] | 30 | #include "Fragmentation/Homology/HomologyGraph.hpp" | 
|---|
| [fbf143] | 31 | #include "Fragmentation/Summation/SetValues/Fragment.hpp" | 
|---|
| [bf1d1b] | 32 | #include "Fragmentation/Summation/SetValues/SamplingGrid.hpp" | 
|---|
| [12a24c] | 33 |  | 
|---|
|  | 34 | class HomologyContainerTest; | 
|---|
|  | 35 |  | 
|---|
| [4694df] | 36 | /** This class takes all KeySets in a Graph, checks for those that homologues | 
|---|
|  | 37 | * of one another and places them together. | 
|---|
|  | 38 | * | 
|---|
|  | 39 | * This is meant as a storage for key, value pairs, where the key is the KeySet | 
|---|
|  | 40 | * and the value is the energy associated to the fragment this keyset | 
|---|
|  | 41 | * represents. | 
|---|
|  | 42 | * Afterwards this can then be used as training data for a high-dimensional | 
|---|
|  | 43 | * approximation to the Born-Oppenheimer-surface decomposed into lower- | 
|---|
|  | 44 | * dimensional terms in an ANOVA-like fashion. | 
|---|
|  | 45 | * | 
|---|
|  | 46 | */ | 
|---|
| [bcc24f3] | 47 | class HomologyContainer : public Observable | 
|---|
| [4694df] | 48 | { | 
|---|
| [e15ffe] | 49 | //!> grant access to output operator | 
|---|
|  | 50 | friend std::ostream& operator<<(std::ostream &out, const HomologyContainer &container); | 
|---|
| [12a24c] | 51 | //!> grant unit test access | 
|---|
|  | 52 | friend class HomologyContainerTest; | 
|---|
| [bf1d1b] | 53 |  | 
|---|
|  | 54 | public: | 
|---|
|  | 55 | /** This structure represents all values associated to a specific homology | 
|---|
|  | 56 | * that we wish to store in this container for later reference. | 
|---|
|  | 57 | */ | 
|---|
|  | 58 | struct value_t { | 
|---|
|  | 59 | Fragment fragment; | 
|---|
|  | 60 | double energy; | 
|---|
|  | 61 | bool containsGrids; | 
|---|
|  | 62 | SamplingGrid charge_distribution; | 
|---|
|  | 63 | SamplingGrid potential_distribution; | 
|---|
|  | 64 |  | 
|---|
|  | 65 | value_t() : | 
|---|
|  | 66 | energy(0.), | 
|---|
|  | 67 | containsGrids(false) | 
|---|
|  | 68 | {} | 
|---|
|  | 69 |  | 
|---|
|  | 70 | bool operator==(const value_t &othervalue) const; | 
|---|
|  | 71 |  | 
|---|
|  | 72 | private: | 
|---|
|  | 73 | friend class boost::serialization::access; | 
|---|
|  | 74 | // serialization | 
|---|
|  | 75 | template <typename Archive> | 
|---|
|  | 76 | void serialize(Archive& ar, const unsigned int version) | 
|---|
|  | 77 | { | 
|---|
|  | 78 | ar & fragment; | 
|---|
|  | 79 | ar & energy; | 
|---|
|  | 80 | if (version > 0) { | 
|---|
|  | 81 | ar & containsGrids; | 
|---|
|  | 82 | if (containsGrids) { | 
|---|
|  | 83 | ar & charge_distribution; | 
|---|
|  | 84 | ar & potential_distribution; | 
|---|
|  | 85 | } | 
|---|
|  | 86 | } | 
|---|
|  | 87 | } | 
|---|
|  | 88 | }; | 
|---|
|  | 89 |  | 
|---|
| [4694df] | 90 | public: | 
|---|
| [77b350] | 91 | typedef std::multimap< HomologyGraph, value_t> container_t; | 
|---|
| [d058e6] | 92 | typedef container_t::const_iterator const_iterator; | 
|---|
| [39a07a] | 93 | typedef MapKeyConstIterator<container_t::const_iterator> const_key_iterator; | 
|---|
| [d058e6] | 94 | typedef std::pair< const_iterator, const_iterator> range_t; | 
|---|
| [bcc24f3] | 95 |  | 
|---|
| [77b350] | 96 | public: | 
|---|
|  | 97 | /** Default Constructor of class HomologyContainer. | 
|---|
|  | 98 | * | 
|---|
|  | 99 | */ | 
|---|
| [bcc24f3] | 100 | HomologyContainer(); | 
|---|
| [77b350] | 101 |  | 
|---|
|  | 102 | /** Constructor of class HomologyContainer. | 
|---|
|  | 103 | * | 
|---|
|  | 104 | * @param values values with with to initially fill the container | 
|---|
|  | 105 | */ | 
|---|
| [bcc24f3] | 106 | HomologyContainer(const container_t &values); | 
|---|
|  | 107 |  | 
|---|
| [77b350] | 108 | /** Destructor of class HomologyContainer. | 
|---|
|  | 109 | * | 
|---|
|  | 110 | */ | 
|---|
| [4694df] | 111 | ~HomologyContainer() {} | 
|---|
|  | 112 |  | 
|---|
| [12a24c] | 113 | /** Equality comparator. | 
|---|
|  | 114 | * | 
|---|
|  | 115 | * Sadly, the insertion order of a std::multimap's values is not guaranteed | 
|---|
|  | 116 | * by the standard and boost::serialization does not heed the ordering of | 
|---|
|  | 117 | * the values associated to the same key. Hence, we implement a weaker | 
|---|
|  | 118 | * comparator for this class in order for the unit test to pass as we don't | 
|---|
|  | 119 | * actuallty care about the order of the homologous fragments. | 
|---|
|  | 120 | * | 
|---|
|  | 121 | * @param other instance to compare to | 
|---|
|  | 122 | * @return true - each container contains all elements of the other | 
|---|
|  | 123 | */ | 
|---|
|  | 124 | bool operator==(const HomologyContainer &other) const { | 
|---|
|  | 125 | return ((*this >= other) && (other >= *this)); | 
|---|
|  | 126 | } | 
|---|
|  | 127 | bool operator!=(const HomologyContainer& other) const { | 
|---|
|  | 128 | return !(*this == other); | 
|---|
|  | 129 | } | 
|---|
|  | 130 |  | 
|---|
|  | 131 | /** Greater equal comparator, i.e. subset comparator | 
|---|
|  | 132 | * | 
|---|
|  | 133 | * @param other container to check if it's subset | 
|---|
|  | 134 | * @return true - \a other is a subset of this | 
|---|
|  | 135 | */ | 
|---|
|  | 136 | bool operator>=(const HomologyContainer &other) const; | 
|---|
|  | 137 |  | 
|---|
| [77b350] | 138 | /** Inserter for more graphs along with values. | 
|---|
|  | 139 | * | 
|---|
|  | 140 | * @param values graph and values to insert | 
|---|
|  | 141 | */ | 
|---|
| [bcc24f3] | 142 | void insert(const container_t &values); | 
|---|
| [77b350] | 143 |  | 
|---|
|  | 144 | /** Returns iterator range with all contained graphs homologous to the given \a graph. | 
|---|
|  | 145 | * | 
|---|
|  | 146 | * @param graph graph to match | 
|---|
|  | 147 | * @return iterator range with all matches | 
|---|
|  | 148 | */ | 
|---|
| [a8bc27a] | 149 | range_t getHomologousGraphs(const HomologyGraph &graph) const { | 
|---|
| [77b350] | 150 | return container.equal_range(graph); | 
|---|
|  | 151 | } | 
|---|
|  | 152 |  | 
|---|
| [d058e6] | 153 | /** Getter for constant iterator to begin of homologous graph container. | 
|---|
|  | 154 | * | 
|---|
|  | 155 | * @return begin constant iterator | 
|---|
|  | 156 | */ | 
|---|
|  | 157 | const_iterator begin() const { | 
|---|
|  | 158 | return container.begin(); | 
|---|
|  | 159 | } | 
|---|
|  | 160 |  | 
|---|
|  | 161 | /** Getter for constant iterator to past end of homologous graph container. | 
|---|
|  | 162 | * | 
|---|
|  | 163 | * @return past end constant iterator | 
|---|
|  | 164 | */ | 
|---|
|  | 165 | const_iterator end() const { | 
|---|
|  | 166 | return container.end(); | 
|---|
|  | 167 | } | 
|---|
|  | 168 |  | 
|---|
| [39a07a] | 169 | const_key_iterator key_begin() const | 
|---|
|  | 170 | { return const_key_iterator(container.begin()); } | 
|---|
|  | 171 |  | 
|---|
|  | 172 | const_key_iterator key_end() const | 
|---|
|  | 173 | { return const_key_iterator(container.end()); } | 
|---|
|  | 174 |  | 
|---|
| [bcc24f3] | 175 | const_key_iterator getNextKey(const_key_iterator &iter) const | 
|---|
|  | 176 | { return const_key_iterator(container.upper_bound(*iter)); } | 
|---|
|  | 177 |  | 
|---|
|  | 178 | /** Clears all homologies from container. | 
|---|
|  | 179 | * | 
|---|
|  | 180 | */ | 
|---|
|  | 181 | void clear(); | 
|---|
|  | 182 |  | 
|---|
| [707a2b] | 183 | /** Returns the number of keys in the container. | 
|---|
|  | 184 | * | 
|---|
|  | 185 | * \return size of internal container | 
|---|
|  | 186 | */ | 
|---|
|  | 187 | const size_t size() | 
|---|
|  | 188 | { return container.size(); } | 
|---|
|  | 189 |  | 
|---|
| [4694df] | 190 | private: | 
|---|
| [77b350] | 191 | //!> multimap containing all homologous graph under same key but each with its value | 
|---|
|  | 192 | container_t container; | 
|---|
| [12a24c] | 193 |  | 
|---|
|  | 194 | private: | 
|---|
|  | 195 | friend class boost::serialization::access; | 
|---|
|  | 196 | // serialization | 
|---|
|  | 197 | template <typename Archive> | 
|---|
| [bcc24f3] | 198 | void load(Archive& ar, const unsigned int version) | 
|---|
|  | 199 | { | 
|---|
|  | 200 | OBSERVE; | 
|---|
|  | 201 | ar & container; | 
|---|
|  | 202 | } | 
|---|
|  | 203 | template <typename Archive> | 
|---|
|  | 204 | void save(Archive& ar, const unsigned int version) const | 
|---|
| [12a24c] | 205 | { | 
|---|
|  | 206 | ar & container; | 
|---|
|  | 207 | } | 
|---|
| [bcc24f3] | 208 | BOOST_SERIALIZATION_SPLIT_MEMBER() | 
|---|
| [4694df] | 209 | }; | 
|---|
|  | 210 |  | 
|---|
| [e15ffe] | 211 | /** Output operator for HomologyContainer. | 
|---|
|  | 212 | * | 
|---|
|  | 213 | * \param out output stream | 
|---|
|  | 214 | * \param container container to print | 
|---|
|  | 215 | * \return output stream for concatenation | 
|---|
|  | 216 | */ | 
|---|
|  | 217 | std::ostream& operator<<(std::ostream &out, const HomologyContainer &container); | 
|---|
| [4694df] | 218 |  | 
|---|
| [12a24c] | 219 | // we need to give this class a unique key for serialization | 
|---|
|  | 220 | BOOST_CLASS_EXPORT_KEY(HomologyContainer) | 
|---|
|  | 221 |  | 
|---|
| [bf1d1b] | 222 | // version for serialized information associated to HomologyGraph | 
|---|
|  | 223 | BOOST_CLASS_VERSION(HomologyContainer::value_t, 1) | 
|---|
| [12a24c] | 224 |  | 
|---|
| [4694df] | 225 | #endif /* HOMOLOGYCONTAINER_HPP_ */ | 
|---|