/* * IndexedValue.hpp * * Created on: 29.07.2012 * Author: heber */ #ifndef INDEXEDVALUE_HPP_ #define INDEXEDVALUE_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include #include #include /** IndexedVectors represents a class that contains a a set of vectors, * each associated to a specific index. When adding or subtracting only * the ones are combined that have matching indices. * * This is needed for summing up force vectors per nuclei obtained from * fragment calculations. * */ template class IndexedValue { public: //!> exposing the template type typedef value value_t; //!> typedef for the index type typedef unsigned int index_t; //!> typedef for the indices matching the bunch of vectors typedef std::vector values_t; //!> typedef for the ordered indices matching the bunch of vectors typedef std::vector indices_t; //!> typedef for a bunch of indexed vectors typedef typename std::map indexedvalues_t; //!> index that is dropped from the chargemap static const index_t DropIndex; /** Default constructor for class IndexedVectors. * */ IndexedValue() {} /** Constructor for class IndexedVectors. * * We construct the internal map from \a _indices and \a _vectors. For * every index -1 contained in \a _indices the respective vector in * \a _vectors is \b not added but silently dropped. * * \param _indices index to each vector * \param _vectors vectors */ IndexedValue(const indices_t &_indices, const values_t &_vectors); /** Assignment operator. * * \note This is required to place IndexedVectors in STL containers. * * \param other other instance to assign this one to * \return ref to this instance */ IndexedValue& operator=(const IndexedValue &other); /** Addition operator with another IndexedVector instance \a other. * * \param other other instance to sum onto this one. * \return ref to this instance */ IndexedValue& operator+=(const IndexedValue &other) { superposeOtherIndexedVectors(other, +1.); return *this; } /** Subtraction operator with another IndexedVector instance \a other. * * \param other other instance to subtract from this one. * \return ref to this instance */ IndexedValue& operator-=(const IndexedValue &other) { superposeOtherIndexedVectors(other, -1.); return *this; } /** Const getter to index vectors. * * \return const reference to indexed vectors */ const indexedvalues_t& getValues() const { return values; } /** Equality operator. * * @param other other instance to check against * @return true - both are equal, false - some IndexedVectors differ */ bool operator==(const IndexedValue& other) const; bool operator!=(const IndexedValue& other) const { return (!(*this == other)); } private: /** Helper function that contains all the logic of how to superpose two * indexed vectors. * * Is called by IndexedVectors::operator+=() and IndexedVectors::operator-=() * * @param other other histogram * @param prefactor +1. is then addition, -1. is subtraction. */ void superposeOtherIndexedVectors(const IndexedValue &other, const double prefactor); private: //!> internal map with all indexed vectors indexedvalues_t values; //!> grant access to output operator template friend std::ostream & operator<<(std::ostream &ost, const IndexedValue &other); }; #include "IndexedValue_impl.hpp" /** Output operator for IndexedVector. * * Prints a space-separated list of all members as "(index, vector)". * * \param ost output stream to print to * \param other instance to print * \return ref to ost for concatenation */ template std::ostream & operator<<(std::ostream &ost, const IndexedValue &other); #endif /* INDEXEDVALUE_HPP_ */