| 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 | 
 | 
|---|
| 26 | template <class value>
 | 
|---|
| 27 | const typename IndexedValue<value>::index_t IndexedValue<value>::DropIndex(-1);
 | 
|---|
| 28 | 
 | 
|---|
| 29 | template <class value>
 | 
|---|
| 30 | IndexedValue<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 | 
 | 
|---|
| 52 | template <class value>
 | 
|---|
| 53 | IndexedValue<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 | 
 | 
|---|
| 63 | template <class value>
 | 
|---|
| 64 | void 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 | 
 | 
|---|
| 81 | template <class value>
 | 
|---|
| 82 | bool IndexedValue<value>::operator==(const IndexedValue<value>& other) const
 | 
|---|
| 83 | {
 | 
|---|
| 84 |   bool status = (values == other.values);
 | 
|---|
| 85 |   return status;
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | template <class value>
 | 
|---|
| 89 | std::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_ */
 | 
|---|