OpenGM  2.3.x
Discrete Graphical Model Library
accumulated_view.hxx
Go to the documentation of this file.
1 //
2 // File: accumulated_view.hxx
3 //
4 // This file is part of OpenGM.
5 //
6 // Copyright (C) 2015 Stefan Haller
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 //
26 
27 #pragma once
28 #ifndef OPENGM_FUNCTIONS_ACCUMULATED_VIEW_HXX
29 #define OPENGM_FUNCTIONS_ACCUMULATED_VIEW_HXX
30 
33 
34 namespace opengm {
35 
36 template<class GM>
37 class AccumulatedViewFunction : public FunctionBase< AccumulatedViewFunction<GM>,
38  typename GM::ValueType,
39  typename GM::IndexType,
40  typename GM::LabelType > {
41 public:
42  typedef typename GM::IndexType IndexType;
43  typedef typename GM::IndexType LabelType;
44  typedef typename GM::ValueType ValueType;
45  typedef typename GM::FactorType FactorType;
46  typedef typename GM::OperatorType OperatorType;
47 
49  AccumulatedViewFunction(const FactorType &);
50  template<class ITERATOR> AccumulatedViewFunction(ITERATOR begin, ITERATOR end);
51 
52  template<class ITERATOR> ValueType operator()(ITERATOR begin) const;
53  LabelType shape(const IndexType) const;
54  IndexType dimension() const;
55  IndexType size() const;
56 
57 private:
58  void check() const;
59 
61 };
62 
63 template<class GM>
65 {
66 }
67 
68 template<class GM>
70 (
71  const FactorType &factor
72 )
73 {
74  factors_.push_back(&factor);
75 }
76 
77 template<class GM>
78 template<class ITERATOR>
80 (
81  ITERATOR begin,
82  ITERATOR end
83 )
84 {
85  factors_.resize(end - begin);
86  std::copy(begin, end, factors_.begin());
87 
88  check();
89 }
90 
91 template<class GM>
92 template<class Iterator>
95 (
96  Iterator begin
97 ) const
98 {
99  check();
100 
101  ValueType result = GM::OperatorType::template neutral<ValueType>();
102  for (size_t i = 0; i < factors_.size(); ++i)
103  GM::OperatorType::op(factors_[i]->operator()(begin), result);
104 
105  return result;
106 }
107 
108 template<class GM>
111 (
112  const IndexType index
113 ) const
114 {
115  check();
116  return factors_[0]->numberOfLabels(index);
117 }
118 
119 template<class GM>
122 {
123  check();
124  return factors_[0]->numberOfVariables();
125 }
126 
127 template<class GM>
130 {
131  check();
132  return factors_[0]->size();
133 }
134 
135 template<class GM>
136 void
138 {
139 #ifndef NDEBUG
140  OPENGM_ASSERT_OP(factors_.size(), >, 0);
141 
142  for (size_t i = 0; i < factors_.size(); ++i)
143  OPENGM_ASSERT(factors_[i] != NULL);
144 
145  for (size_t i = 1; i < factors_.size(); ++i) {
146  OPENGM_ASSERT_OP(factors_[0]->size(), ==, factors_[i]->size());
147  OPENGM_ASSERT_OP(factors_[0]->numberOfVariables(), ==, factors_[i]->numberOfVariables());
148 
149  for (IndexType j = 0; j < factors_[0]->numberOfVariables(); ++j) {
150  OPENGM_ASSERT_OP(factors_[0]->numberOfLabels(j), ==, factors_[i]->numberOfLabels(j));
151  OPENGM_ASSERT_OP(factors_[0]->variableIndex(j), ==, factors_[i]->variableIndex(j));
152  }
153  }
154 #endif
155 }
156 
157 } // namespace opengm
158 
159 #endif
The OpenGM namespace.
Definition: config.hxx:43
Fallback implementation of member functions of OpenGM functions.
LabelType shape(const IndexType) const
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
ValueType operator()(ITERATOR begin) const
#define OPENGM_ASSERT_OP(a, op, b)
runtime assertion
Definition: opengm.hxx:53