source: src/Fragmentation/Summation/SetValues/IndexedValue_impl.hpp@ afc28a

Fix_ChargeSampling_PBC
Last change on this file since afc28a was bd8b57, checked in by Frederik Heber <heber@…>, 9 years ago

Extracted IndexedValue from IndexedVectors.

  • we may now sum up indexed values of arbitrary type, i.e. an arbitrary class that fulfills a certain interface, and each instance connected to a specific index (within index sets).
  • added detail::force where std::vector<double> is specialized for three components.
  • IndexedVectors is now a specialization of IndexedValue for detail::force.
  • adapated usage signatures in AnalyseFragmentationResultsAction, InterfaceVMGJob, and MPQCCommandJob.
  • slight changes in IndexedVectorsUnitTest because boost::assign is no longer used for detail::force.
  • Property mode set to 100644
File size: 3.0 KB
Line 
1/*
2 * IndexedValue_impl.hpp
3 *
4 * Created on: 29.07.2012
5 * Author: heber
6 */
7
8#ifndef INDEXEDVALUE_IMPL_HPP_
9#define INDEXEDVALUE_IMPL_HPP_
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16#include "CodePatterns/Assert.hpp"
17
18#include "IndexedValue.hpp"
19
20#include "CodePatterns/Log.hpp"
21
22#include <iostream>
23
24#include "Fragmentation/Summation/ZeroInstance.hpp"
25
26template <class value>
27const typename IndexedValue<value>::index_t IndexedValue<value>::DropIndex(-1);
28
29template <class value>
30IndexedValue<value>::IndexedValue(
31 const IndexedValue<value>::indices_t &_indices,
32 const IndexedValue<value>::values_t &_values)
33{
34 ASSERT(_indices.size() == _values.size(),
35 "IndexedVectors::IndexedVectors() - vector of indices and values don't match in size.");
36 typename indices_t::const_iterator indexiter = _indices.begin();
37 typename values_t::const_iterator vectoriter = _values.begin();
38 for (; vectoriter != _values.end(); ++indexiter, ++vectoriter) {
39 if (*indexiter != DropIndex) { // skip all force values associated to -1
40#ifndef NDEBUG
41 std::pair<typename indexedvalues_t::iterator, bool> inserter =
42#endif
43 values.insert( std::make_pair( *indexiter, *vectoriter) );
44 ASSERT( inserter.second,
45 "IndexedValue<>::IndexedValue() - index "
46 +toString(inserter.first->first)+" already present with vector "
47 +toString(inserter.first->second)+".");
48 }
49 }
50}
51
52template <class value>
53IndexedValue<value>& IndexedValue<value>::operator=(const IndexedValue<value> &other)
54{
55 // check for self-assignment
56 if (this != &other) {
57 values.clear();
58 values = other.values;
59 }
60 return *this;
61}
62
63template <class value>
64void IndexedValue<value>::superposeOtherIndexedVectors(const IndexedValue<value> &other, const double prefactor)
65{
66 for (typename indexedvalues_t::const_iterator otheriter = other.values.begin();
67 otheriter != other.values.end(); ++otheriter) {
68 typename indexedvalues_t::iterator iter = values.find(otheriter->first);
69 if (iter == values.end()) {
70 // index does not exist
71 std::pair<typename indexedvalues_t::iterator, bool> inserter =
72 values.insert( std::make_pair( otheriter->first, ZeroInstance<value>()) );
73 ASSERT( inserter.second,
74 "IndexedValue<>::superposeOtherIndexedVectors() - index is present though unfound before?");
75 iter = inserter.first;
76 }
77 iter->second.superposeOtherIndexedVectors(otheriter->second, prefactor);
78 }
79}
80
81template <class value>
82bool IndexedValue<value>::operator==(const IndexedValue<value>& other) const
83{
84 bool status = (values == other.values);
85 return status;
86}
87
88template <class value>
89std::ostream & operator<<(std::ostream &ost, const IndexedValue<value> &other)
90{
91 for (typename IndexedValue<value>::indexedvalues_t::const_iterator iter = other.values.begin();
92 iter != other.values.end(); ++iter)
93 ost << "(" << iter->first << "," << iter->second << ") ";
94 return ost;
95}
96
97
98#endif /* INDEXEDVALUE_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.