Miind
AlgorithmGrid.cpp
Go to the documentation of this file.
1 // Copyright (c) 2005 - 2012 Marc de Kamps
2 // 2012 David-Matthias Sichau
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation
9 // and/or other materials provided with the distribution.
10 // * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software
11 // without specific prior written permission.
12 //
13 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
15 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
16 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
17 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18 //
19 
20 #include "AlgorithmGrid.hpp"
21 #include <cassert>
22 #include <functional>
23 
24 using namespace MPILib;
25 
26 AlgorithmGrid::AlgorithmGrid(Number number_of_elements) :
27  _numberState(number_of_elements), _arrayState(0.0,
28  number_of_elements), _arrayInterpretation(0.0,
29  number_of_elements) {
30 }
31 
32 AlgorithmGrid::AlgorithmGrid(const std::vector<double>& array_state) :
33  _numberState(static_cast<Number>(array_state.size())), _arrayState(
34  toValarray<double>(array_state)), _arrayInterpretation(
35  std::valarray<double>(0.0, array_state.size())) {
36 }
37 
38 AlgorithmGrid::AlgorithmGrid(const std::vector<double>& array_state,
39  const std::vector<double>& array_interpretation) :
40  _numberState(static_cast<Number>(array_state.size())), _arrayState(
41  toValarray<double>(array_state)), _arrayInterpretation(
42  toValarray<double>(array_interpretation)) {
43  assert( _arrayState.size() == _arrayInterpretation.size());
44 }
45 
47  if (&rhs == this)
48  return *this;
49  else {
50  // resize, because copying valarrays of different length is undefined
51  _arrayState.resize(rhs._arrayState.size());
52  _arrayInterpretation.resize(rhs._arrayInterpretation.size());
53 
57  return *this;
58  }
59 }
60 
61 std::vector<double> AlgorithmGrid::toStateVector() const {
63 }
64 
65 
66 std::vector<double> AlgorithmGrid::toInterpretationVector() const {
68 }
69 
70 template<class Value>
71 std::valarray<Value> AlgorithmGrid::toValarray(
72  const std::vector<double>& vector) const {
73  return std::valarray<Value>(&vector[0], vector.size());
74 }
75 
76 template<class Value>
77 std::vector<Value> AlgorithmGrid::toVector(const std::valarray<Value>& array,
78  Number number_to_be_copied) const {
79  auto& array_test = const_cast<std::valarray<Value>&>(array);
80  auto p_begin = &array_test[0];
81  auto p_end = p_begin + number_to_be_copied;
82 
83  std::vector<Value> vector_return(0);
84  std::copy(p_begin, p_end, back_inserter(vector_return));
85 
86  return vector_return;
87 }
88 
89 std::valarray<double>& AlgorithmGrid::getArrayState() {
90  return _arrayState;
91 }
92 
93 std::valarray<double>& AlgorithmGrid::getArrayInterpretation() {
94  return _arrayInterpretation;
95 }
96 
98  return _numberState;
99 }
100 
102  return _numberState;
103 }
104 
105 
106 void AlgorithmGrid::resize(Number number_of_new_bins) {
107  _arrayState.resize(number_of_new_bins);
108  _arrayInterpretation.resize(number_of_new_bins);
109 }
110 
111 
112 const double* AlgorithmGrid::begin_state() const {
113 
114  auto& ref_state = const_cast<std::valarray<double>&>(_arrayState);
115  const double* p_begin = &ref_state[0];
116  return p_begin;
117 }
118 
119 const double* AlgorithmGrid::end_state() const {
120  auto& ref_state = const_cast<std::valarray<double>&>(_arrayState);
121  const double* p_end = &ref_state[_numberState];
122  return p_end;
123 }
124 
125 const double* AlgorithmGrid::begin_interpretation() const {
126 
127  auto& ref_state = const_cast<std::valarray<double>&>(_arrayInterpretation);
128  const double* p_begin = &ref_state[0];
129  return p_begin;
130 }
131 
132 const double* AlgorithmGrid::end_interpretation() const {
133  auto& ref_state = const_cast<std::valarray<double>&>(_arrayInterpretation);
134  const double* p_end = &ref_state[_numberState];
135  return p_end;
136 }
const double * end_interpretation() const
std::valarray< double > _arrayState
const double * begin_interpretation() const
std::valarray< double > & getArrayInterpretation()
unsigned int Number
STL namespace.
std::valarray< Value > toValarray(const std::vector< double > &vector) const
std::vector< double > toInterpretationVector() const
AlgorithmGrid & operator=(const AlgorithmGrid &rhs)
void resize(Number number_of_new_bins)
std::valarray< double > _arrayInterpretation
std::vector< double > toStateVector() const
const double * begin_state() const
allow iteration over internal values of the state
const double * end_state() const
std::valarray< double > & getArrayState()
std::vector< Value > toVector(const std::valarray< Value > &array, Number number_to_be_copied) const