OpenGM  2.3.x
Discrete Graphical Model Library
explicit_function.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_EXPLICIT_FUNCTION_HXX
3 #define OPENGM_EXPLICIT_FUNCTION_HXX
4 
8 
9 namespace opengm {
10 
14 template<class T, class I=size_t, class L=size_t>
16 : public marray::Marray<T>,
17  public FunctionBase<ExplicitFunction<T, I, L>, T, I, L>
18 {
19 public:
20  typedef T ValueType;
21  typedef L LabelType;
22  typedef I IndexType;
23 
25  : marray::Marray<T>()
26  {}
27 
29  ExplicitFunction(const T& value)
30  : marray::Marray<T>(value)
31  {}
32 
34  : marray::Marray<T>(other)
35  {}
36 
38  {
40  return *this;
41  }
42 
51  template <class SHAPE_ITERATOR>
52  ExplicitFunction(SHAPE_ITERATOR shapeBegin, SHAPE_ITERATOR shapeEnd)
53  : marray::Marray<T>(shapeBegin, shapeEnd)
54  {}
55 
57  template <class SHAPE_ITERATOR>
58  ExplicitFunction(SHAPE_ITERATOR shapeBegin, SHAPE_ITERATOR shapeEnd, const T & value)
59  : marray::Marray<T>(shapeBegin, shapeEnd, value)
60  {}
61 };
62 
65 template<class T, class I, class L>
66 struct FunctionRegistration< ExplicitFunction<T, I, L> >{
67  enum ID {
69  };
70 };
71 
73 template<class T, class I, class L>
74 class FunctionSerialization< ExplicitFunction<T, I, L> >{
75 public:
76  typedef typename ExplicitFunction<T, I, L>::value_type ValueType;
77 
78  static size_t indexSequenceSize(const ExplicitFunction<T, I, L> &);
79  static size_t valueSequenceSize(const ExplicitFunction<T, I, L> &);
80  template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
81  static void serialize(const ExplicitFunction<T, I, L> &, INDEX_OUTPUT_ITERATOR, VALUE_OUTPUT_ITERATOR );
82  template<class INDEX_INPUT_ITERATOR , class VALUE_INPUT_ITERATOR>
83  static void deserialize( INDEX_INPUT_ITERATOR, VALUE_INPUT_ITERATOR, ExplicitFunction<T, I, L> &);
84 };
86 
87 template<class T, class I, class L>
88 inline size_t FunctionSerialization<ExplicitFunction<T, I, L> >::indexSequenceSize
89 (
90  const ExplicitFunction<T, I, L> & src
91 ) {
92  return src.dimension() +1;
93 }
94 
95 template<class T, class I, class L>
96 inline size_t FunctionSerialization<ExplicitFunction<T, I, L> >::valueSequenceSize
97 (
98  const ExplicitFunction<T, I, L> & src
99 ) {
100  return src.size();
101 }
102 
103 template<class T, class I, class L>
104 template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
105 void FunctionSerialization< ExplicitFunction<T, I, L> >::serialize
106 (
107  const ExplicitFunction<T, I, L> & src,
108  INDEX_OUTPUT_ITERATOR indexOutIterator,
109  VALUE_OUTPUT_ITERATOR valueOutIterator
110 ) {
111  if(src.dimension()==0) {
112  *indexOutIterator=0;
113  *valueOutIterator=src(0);
114  }
115  else{
116  *indexOutIterator=src.dimension();
117  ++indexOutIterator;
118  for(size_t i=0;i<src.dimension();++i) {
119  *indexOutIterator=src.shape(i);
120  ++indexOutIterator;
121  }
122  for(size_t i=0;i<src.size();++i) {
123  *valueOutIterator=src(i);
124  ++valueOutIterator;
125  }
126  }
127 }
128 
129 template<class T, class I, class L>
130 template<class INDEX_INPUT_ITERATOR, class VALUE_INPUT_ITERATOR >
131 void FunctionSerialization<ExplicitFunction<T, I, L> >::deserialize
132 (
133  INDEX_INPUT_ITERATOR indexOutIterator,
134  VALUE_INPUT_ITERATOR valueOutIterator,
135  ExplicitFunction<T, I, L> & dst
136 ) {
137  if(*indexOutIterator==0) {
138  dst.assign();
139  dst=ExplicitFunction<T, I, L>(*valueOutIterator);
140  }
141  else{
142  const size_t dim=*indexOutIterator;
143  std::vector<size_t> shape(dim);
144  ++indexOutIterator;
145  for(size_t i=0;i<dim;++i) {
146  shape[i]=*indexOutIterator;
147  ++indexOutIterator;
148  }
149  dst.assign();
150  dst.resize(shape.begin(), shape.end() );
151  for(size_t i=0;i<dst.size();++i) {
152  dst(i)=*valueOutIterator;
153  ++valueOutIterator;
154  }
155  }
156 }
157 
158 template<class FUNC, class T, class I, class L>
159 ExplicitFunction<T, I, L>
161 (
162  const FUNC &function
163 )
164 {
166  cloneAsExplicitFunction(function, result);
167  return result;
168 }
169 
170 template<class FUNC, class T, class I, class L>
171 void
173 (
174  const FUNC &function,
176 )
177 {
178  typedef ShapeWalker<typename FUNC::FunctionShapeIteratorType> Walker;
179  out.resize(function.functionShapeBegin(), function.functionShapeEnd());
180  Walker walker(function.functionShapeBegin(), function.dimension());
181  for (I i = 0; i < function.size(); ++i, ++walker) {
182  out(walker.coordinateTuple().begin()) =
183  function(walker.coordinateTuple().begin());
184  }
185 }
186 
187 } // namespace opengm
188 
189 #endif // OPENGM_EXPLICIT_FUNCTION_HXX
The OpenGM namespace.
Definition: config.hxx:43
ExplicitFunction< T, I, L > cloneAsExplicitFunction(const FUNC &function)
Fallback implementation of member functions of OpenGM functions.
Marray(const allocator_type &=allocator_type())
Runtime-flexible multi-dimensional views and arrays.
Definition: marray.hxx:20
Marray< T, A > & operator=(const T &)
Assignment.
Definition: marray.hxx:3764
ExplicitFunction(const T &value)
construct a constant explicit function of order 0
Function encoded as a dense multi-dimensional array, marray::Marray.
const size_t FUNCTION_TYPE_ID_OFFSET
User-defined function have ids smaller than FUNCTION_TYPE_ID_OFFSET.
ExplicitFunction & operator=(const ExplicitFunction &other)
Runtime-flexible multi-dimensional array.
Definition: marray.hxx:52
ExplicitFunction(SHAPE_ITERATOR shapeBegin, SHAPE_ITERATOR shapeEnd, const T &value)
construct a function encoded by a value table (whose entries are initialized with the same value) ...
ExplicitFunction(SHAPE_ITERATOR shapeBegin, SHAPE_ITERATOR shapeEnd)
construct a function encoded by a value table (whose entries are initialized as 0) ...
const size_t * shapeEnd() const
const size_t * shapeBegin() const
void resize(ShapeIterator, ShapeIterator, const T &=T())
ExplicitFunction(const ExplicitFunction &other)