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