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 <set>
|
---|
19 | #include <vector>
|
---|
20 |
|
---|
21 | class IndexedVectorsTest;
|
---|
22 |
|
---|
23 | /** IndexedVectors represents a class that contains a a set of vectors,
|
---|
24 | * each associated to a specific index. When adding or subtracting only
|
---|
25 | * the ones are combined that have matching indices.
|
---|
26 | *
|
---|
27 | * This is needed for summing up force vectors per nuclei obtained from
|
---|
28 | * fragment calculations.
|
---|
29 | *
|
---|
30 | */
|
---|
31 | class IndexedVectors
|
---|
32 | {
|
---|
33 | //!> grant unit test access to private parts
|
---|
34 | friend class IndexedVectorsTest;
|
---|
35 | public:
|
---|
36 | //!> typedef for a single vector
|
---|
37 | typedef std::vector<double> vector_t;
|
---|
38 | //!> typedef for the index type
|
---|
39 | typedef size_t index_t;
|
---|
40 | //!> typedef for the indices matching the bunch of vectors
|
---|
41 | typedef std::vector<vector_t> vectors_t;
|
---|
42 | //!> typedef for the ordered indices matching the bunch of vectors
|
---|
43 | typedef std::set<index_t> indices_t;
|
---|
44 | //!> typedef for a bunch of indexed vectors
|
---|
45 | typedef std::map<index_t, vector_t> indexedvectors_t;
|
---|
46 |
|
---|
47 | /** Default constructor for class IndexedVectors.
|
---|
48 | *
|
---|
49 | */
|
---|
50 | IndexedVectors() {}
|
---|
51 |
|
---|
52 | /** Constructor for class IndexedVectors.
|
---|
53 | *
|
---|
54 | * We construct the internal map from \a _indices and \a _vectors.
|
---|
55 | *
|
---|
56 | * \param _indices index to each vector
|
---|
57 | * \param _vectors vectors
|
---|
58 | */
|
---|
59 | IndexedVectors(const indices_t &_indices, const vectors_t &_vectors);
|
---|
60 |
|
---|
61 | /** Assignment operator.
|
---|
62 | *
|
---|
63 | * \note This is required to place IndexedVectors in STL containers.
|
---|
64 | *
|
---|
65 | * \param other other instance to assign this one to
|
---|
66 | * \return ref to this instance
|
---|
67 | */
|
---|
68 | IndexedVectors& operator=(const IndexedVectors &other);
|
---|
69 |
|
---|
70 | /** Addition operator with another IndexedVector instance \a other.
|
---|
71 | *
|
---|
72 | * \param other other instance to sum onto this one.
|
---|
73 | * \return ref to this instance
|
---|
74 | */
|
---|
75 | IndexedVectors& operator+=(const IndexedVectors &other)
|
---|
76 | {
|
---|
77 | superposeOtherIndexedVectors(other, +1.);
|
---|
78 | return *this;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /** Subtraction operator with another IndexedVector instance \a other.
|
---|
82 | *
|
---|
83 | * \param other other instance to subtract from this one.
|
---|
84 | * \return ref to this instance
|
---|
85 | */
|
---|
86 | IndexedVectors& operator-=(const IndexedVectors &other)
|
---|
87 | {
|
---|
88 | superposeOtherIndexedVectors(other, -1.);
|
---|
89 | return *this;
|
---|
90 | }
|
---|
91 |
|
---|
92 | private:
|
---|
93 | /** Helper function that contains all the logic of how to superpose two
|
---|
94 | * indexed vectors.
|
---|
95 | *
|
---|
96 | * Is called by IndexedVectors::operator+=() and IndexedVectors::operator-=()
|
---|
97 | *
|
---|
98 | * @param other other histogram
|
---|
99 | * @param prefactor +1. is then addition, -1. is subtraction.
|
---|
100 | */
|
---|
101 | void superposeOtherIndexedVectors(const IndexedVectors &other, const double prefactor);
|
---|
102 |
|
---|
103 | private:
|
---|
104 | //!> internal map with all indexed vectors
|
---|
105 | indexedvectors_t vectors;
|
---|
106 | //!> fixed size of all vector_t
|
---|
107 | static const size_t FixedSize;
|
---|
108 | //!> static instance representing a null vector
|
---|
109 | static const vector_t nullvector;
|
---|
110 |
|
---|
111 | //!> grant access to output operator
|
---|
112 | friend std::ostream & operator<<(std::ostream &ost, const IndexedVectors &other);
|
---|
113 | };
|
---|
114 |
|
---|
115 | /** Output operator for IndexedVector.
|
---|
116 | *
|
---|
117 | * Prints a space-separated list of all members as "(index, vector)".
|
---|
118 | *
|
---|
119 | * \param ost output stream to print to
|
---|
120 | * \param other instance to print
|
---|
121 | * \return ref to ost for concatenation
|
---|
122 | */
|
---|
123 | std::ostream & operator<<(std::ostream &ost, const IndexedVectors &other);
|
---|
124 |
|
---|
125 |
|
---|
126 | #endif /* INDEXEDVECTORS_HPP_ */
|
---|