| 1 | /*
 | 
|---|
| 2 |  * IndexedVectors.hpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: 29.07.2012
 | 
|---|
| 5 |  *      Author: heber
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | #ifndef INDEXEDVECTORS_HPP_
 | 
|---|
| 9 | #define INDEXEDVECTORS_HPP_
 | 
|---|
| 10 | 
 | 
|---|
| 11 | // include config.h
 | 
|---|
| 12 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 13 | #include <config.h>
 | 
|---|
| 14 | #endif
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include <iosfwd>
 | 
|---|
| 17 | #include <map>
 | 
|---|
| 18 | #include <vector>
 | 
|---|
| 19 | 
 | 
|---|
| 20 | class IndexedVectorsTest;
 | 
|---|
| 21 | 
 | 
|---|
| 22 | /** IndexedVectors represents a class that contains a a set of vectors,
 | 
|---|
| 23 |  * each associated to a specific index. When adding or subtracting only
 | 
|---|
| 24 |  * the ones are combined that have matching indices.
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  * This is needed for summing up force vectors per nuclei obtained from
 | 
|---|
| 27 |  * fragment calculations.
 | 
|---|
| 28 |  *
 | 
|---|
| 29 |  */
 | 
|---|
| 30 | class IndexedVectors
 | 
|---|
| 31 | {
 | 
|---|
| 32 |   //!> grant unit test access to private parts
 | 
|---|
| 33 |   friend class IndexedVectorsTest;
 | 
|---|
| 34 | public:
 | 
|---|
| 35 |   //!> typedef for a single vector
 | 
|---|
| 36 |   typedef std::vector<double> vector_t;
 | 
|---|
| 37 |   //!> typedef for the index type
 | 
|---|
| 38 |   typedef size_t index_t;
 | 
|---|
| 39 |   //!> typedef for the indices matching the bunch of vectors
 | 
|---|
| 40 |   typedef std::vector<vector_t> vectors_t;
 | 
|---|
| 41 |   //!> typedef for the ordered indices matching the bunch of vectors
 | 
|---|
| 42 |   typedef std::vector<index_t> indices_t;
 | 
|---|
| 43 |   //!> typedef for a bunch of indexed vectors
 | 
|---|
| 44 |   typedef std::map<index_t, vector_t> indexedvectors_t;
 | 
|---|
| 45 | 
 | 
|---|
| 46 |   enum SpecificIndices_t {
 | 
|---|
| 47 |     DropIndex = -1
 | 
|---|
| 48 |   };
 | 
|---|
| 49 | 
 | 
|---|
| 50 |   /** Default constructor for class IndexedVectors.
 | 
|---|
| 51 |    *
 | 
|---|
| 52 |    */
 | 
|---|
| 53 |   IndexedVectors() {}
 | 
|---|
| 54 | 
 | 
|---|
| 55 |   /** Constructor for class IndexedVectors.
 | 
|---|
| 56 |    *
 | 
|---|
| 57 |    * We construct the internal map from \a _indices and \a _vectors. For
 | 
|---|
| 58 |    * every index -1 contained in \a _indices the respective vector in
 | 
|---|
| 59 |    * \a _vectors is \b not added but silently dropped.
 | 
|---|
| 60 |    *
 | 
|---|
| 61 |    * \param _indices index to each vector
 | 
|---|
| 62 |    * \param _vectors vectors
 | 
|---|
| 63 |    */
 | 
|---|
| 64 |   IndexedVectors(const indices_t &_indices, const vectors_t &_vectors);
 | 
|---|
| 65 | 
 | 
|---|
| 66 |   /** Assignment operator.
 | 
|---|
| 67 |    *
 | 
|---|
| 68 |    * \note This is required to place IndexedVectors in STL containers.
 | 
|---|
| 69 |    *
 | 
|---|
| 70 |    * \param other other instance to assign this one to
 | 
|---|
| 71 |    * \return ref to this instance
 | 
|---|
| 72 |    */
 | 
|---|
| 73 |   IndexedVectors& operator=(const IndexedVectors &other);
 | 
|---|
| 74 | 
 | 
|---|
| 75 |   /** Addition operator with another IndexedVector instance \a other.
 | 
|---|
| 76 |    *
 | 
|---|
| 77 |    * \param other other instance to sum onto this one.
 | 
|---|
| 78 |    * \return ref to this instance
 | 
|---|
| 79 |    */
 | 
|---|
| 80 |   IndexedVectors& operator+=(const IndexedVectors &other)
 | 
|---|
| 81 |   {
 | 
|---|
| 82 |     superposeOtherIndexedVectors(other, +1.);
 | 
|---|
| 83 |     return *this;
 | 
|---|
| 84 |   }
 | 
|---|
| 85 | 
 | 
|---|
| 86 |   /** Subtraction operator with another IndexedVector instance \a other.
 | 
|---|
| 87 |    *
 | 
|---|
| 88 |    * \param other other instance to subtract from this one.
 | 
|---|
| 89 |    * \return ref to this instance
 | 
|---|
| 90 |    */
 | 
|---|
| 91 |   IndexedVectors& operator-=(const IndexedVectors &other)
 | 
|---|
| 92 |   {
 | 
|---|
| 93 |     superposeOtherIndexedVectors(other, -1.);
 | 
|---|
| 94 |     return *this;
 | 
|---|
| 95 |   }
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   /** Const getter to index vectors.
 | 
|---|
| 98 |    *
 | 
|---|
| 99 |    * \return const reference to indexed vectors
 | 
|---|
| 100 |    */
 | 
|---|
| 101 |   const indexedvectors_t& getVectors() const
 | 
|---|
| 102 |   { return vectors; }
 | 
|---|
| 103 | 
 | 
|---|
| 104 |   /** Equality operator.
 | 
|---|
| 105 |    *
 | 
|---|
| 106 |    * @param other other instance to check against
 | 
|---|
| 107 |    * @return true - both are equal, false - some IndexedVectors differ
 | 
|---|
| 108 |    */
 | 
|---|
| 109 |   bool operator==(const IndexedVectors& other) const;
 | 
|---|
| 110 | 
 | 
|---|
| 111 |   bool operator!=(const IndexedVectors& other) const
 | 
|---|
| 112 |   {
 | 
|---|
| 113 |     return (!(*this == other));
 | 
|---|
| 114 |   }
 | 
|---|
| 115 | 
 | 
|---|
| 116 | private:
 | 
|---|
| 117 |   /** Helper function that contains all the logic of how to superpose two
 | 
|---|
| 118 |    * indexed vectors.
 | 
|---|
| 119 |    *
 | 
|---|
| 120 |    * Is called by IndexedVectors::operator+=() and IndexedVectors::operator-=()
 | 
|---|
| 121 |    *
 | 
|---|
| 122 |    * @param other other histogram
 | 
|---|
| 123 |    * @param prefactor +1. is then addition, -1. is subtraction.
 | 
|---|
| 124 |    */
 | 
|---|
| 125 |   void superposeOtherIndexedVectors(const IndexedVectors &other, const double prefactor);
 | 
|---|
| 126 | 
 | 
|---|
| 127 | private:
 | 
|---|
| 128 |   //!> internal map with all indexed vectors
 | 
|---|
| 129 |   indexedvectors_t vectors;
 | 
|---|
| 130 |   //!> fixed size of all vector_t
 | 
|---|
| 131 |   static const size_t FixedSize;
 | 
|---|
| 132 |   //!> static instance representing a null vector
 | 
|---|
| 133 |   static const vector_t nullvector;
 | 
|---|
| 134 | 
 | 
|---|
| 135 |   //!> grant access to output operator
 | 
|---|
| 136 |   friend std::ostream & operator<<(std::ostream &ost, const IndexedVectors &other);
 | 
|---|
| 137 | };
 | 
|---|
| 138 | 
 | 
|---|
| 139 | /** Output operator for IndexedVector.
 | 
|---|
| 140 |  *
 | 
|---|
| 141 |  *  Prints a space-separated list of all members as "(index, vector)".
 | 
|---|
| 142 |  *
 | 
|---|
| 143 |  * \param ost output stream to print to
 | 
|---|
| 144 |  * \param other instance to print
 | 
|---|
| 145 |  * \return ref to ost for concatenation
 | 
|---|
| 146 |  */
 | 
|---|
| 147 | std::ostream & operator<<(std::ostream &ost, const IndexedVectors &other);
 | 
|---|
| 148 | 
 | 
|---|
| 149 | template<typename T> T ZeroInstance();
 | 
|---|
| 150 | template<> IndexedVectors ZeroInstance<IndexedVectors>();
 | 
|---|
| 151 | 
 | 
|---|
| 152 | 
 | 
|---|
| 153 | #endif /* INDEXEDVECTORS_HPP_ */
 | 
|---|