OpenGM  2.3.x
Discrete Graphical Model Library
view_fix_variables_function.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_VIEW_FIX_VARIABLES_FUNCTION_HXX
3 #define OPENGM_VIEW_FIX_VARIABLES_FUNCTION_HXX
4 
6 
7 namespace opengm {
8 
10 template<class I, class L>
11 struct PositionAndLabel {
12  PositionAndLabel(const I = 0, const L = 0);
13  I position_;
14  L label_;
15 };
17 
21 template<class GM>
23 : public FunctionBase<ViewFixVariablesFunction<GM>, typename GM::ValueType, typename GM::IndexType, typename GM::LabelType> {
24 public:
25  typedef typename GM::ValueType ValueType;
26  typedef ValueType value_type;
27  typedef typename GM::FactorType FactorType;
28  typedef typename GM::OperatorType OperatorType;
29  typedef typename GM::IndexType IndexType;
30  typedef typename GM::LabelType LabelType;
31 
33  ViewFixVariablesFunction(const FactorType &, const std::vector<PositionAndLabel<IndexType, LabelType> > &);
34  template<class POSITION_AND_TYPE_CONTAINER>
35  ViewFixVariablesFunction(const FactorType &, const POSITION_AND_TYPE_CONTAINER &);
36  template<class Iterator>
37  ValueType operator()(Iterator begin)const;
38  IndexType shape(const IndexType)const;
39  IndexType dimension()const;
40  IndexType size()const;
41 
42 private:
43  FactorType const* factor_;
44  std::vector<PositionAndLabel<IndexType, LabelType> > positionAndLabels_;
45  mutable std::vector<LabelType> iteratorBuffer_;
46  mutable bool computedSize_;
47  mutable size_t size_;
48  std::vector<size_t> lookUp_;
49 };
50 
51 template<class I, class L>
52 PositionAndLabel<I, L>::PositionAndLabel
53 (
54  const I position,
55  const L label
56 )
57 : position_(position),
58  label_(label)
59 {}
60 
61 template<class GM>
62 inline
64 : factor_(NULL),
65  iteratorBuffer_(),
66  computedSize_(false),
67  size_(1)
68 {}
69 
70 template<class GM>
71 inline
73 (
74  const typename ViewFixVariablesFunction<GM>::FactorType& factor,
75  const std::vector<PositionAndLabel<typename GM::IndexType, typename GM::LabelType> >& positionAndLabels
76 )
77 : factor_(&factor),
78  positionAndLabels_(positionAndLabels),
79  iteratorBuffer_(factor.numberOfVariables()),
80  computedSize_(false),
81  size_(1),
82  lookUp_(factor.numberOfVariables()-positionAndLabels.size())
83 {
84  if(opengm::NO_DEBUG==false) {
85  for(size_t i=0; i<positionAndLabels_.size(); ++i) {
86  OPENGM_ASSERT(positionAndLabels_[i].label_ < factor_->numberOfLabels(positionAndLabels_[i].position_));
87  }
88  }
89  for(size_t ind=0; ind<lookUp_.size(); ++ind) {
90  size_t add=0;
91  for(IndexType i=0; i<positionAndLabels_.size(); ++i) {
92  if( positionAndLabels_[i].position_ <= ind+add) {
93  ++add;
94  }
95  }
96  lookUp_[ind]=ind+add;
97  }
98 }
99 
104 template<class GM>
105 template<class POSITION_AND_TYPE_CONTAINER>
106 inline
108 (
109  const typename ViewFixVariablesFunction<GM>::FactorType& factor,
110  const POSITION_AND_TYPE_CONTAINER& positionAndLabels
111 )
112 : factor_(&factor),
113  positionAndLabels_(positionAndLabels.begin(), positionAndLabels.end()),
114  iteratorBuffer_(factor.numberOfVariables()),
115  computedSize_(false),
116  size_(1),
117  lookUp_(factor.numberOfVariables()-positionAndLabels.size())
118 {
119  if(!(opengm::NO_DEBUG)) {
120  for(size_t i=0; i<positionAndLabels_.size(); ++i) {
121  OPENGM_ASSERT(positionAndLabels_[i].label_ < factor_->numberOfLabels(positionAndLabels_[i].position_));
122  }
123  }
124  for(size_t ind=0; ind<lookUp_.size(); ++ind) {
125  size_t add = 0;
126  for(IndexType i=0; i<positionAndLabels_.size(); ++i) {
127  if( positionAndLabels_[i].position_ <= ind+add) {
128  ++add;
129  }
130  }
131  lookUp_[ind]=ind+add;
132  }
133 }
134 
135 template<class GM>
136 template<class Iterator>
139 (
140  Iterator begin
141 ) const
142 {
143  OPENGM_ASSERT(factor_ != NULL);
144  for(size_t ind=0; ind<lookUp_.size(); ++ind) {
145  iteratorBuffer_[lookUp_[ind]]=*begin;
146  ++begin;
147  }
148  for(size_t i=0; i<positionAndLabels_.size(); ++i) {
149  iteratorBuffer_[positionAndLabels_[i].position_]
150  = positionAndLabels_[i].label_;
151  }
152  return factor_->operator()(iteratorBuffer_.begin());
153 }
154 
155 template<class GM>
158 (
159  const typename ViewFixVariablesFunction<GM>::IndexType index
160 ) const
161 {
162  OPENGM_ASSERT(factor_ != NULL);
163  size_t add = 0;
164  for(IndexType i=0; i<positionAndLabels_.size(); ++i) {
165  if( positionAndLabels_[i].position_ <= index+add) {
166  ++add;
167  }
168  }
169  OPENGM_ASSERT(index + add < factor_->numberOfVariables());
170  return factor_->numberOfLabels(index + add);
171 }
172 
173 template<class GM>
176 {
177  OPENGM_ASSERT(factor_!=NULL);
178  return factor_->numberOfVariables() - positionAndLabels_.size();
179 }
180 
181 template<class GM>
184 {
185  OPENGM_ASSERT(factor_!=NULL);
186  if(computedSize_== false) {
187  size_ = factor_->size();
188  for(IndexType j=0; j<positionAndLabels_.size(); ++j) {
189  size_ /= (factor_->numberOfLabels(positionAndLabels_[j].position_));
190  }
191  computedSize_ = true;
192  }
193  return size_;
194 }
195 
196 } // namespace opengm
197 
198 #endif // #ifndef OPENGM_VIEW_FIX_VARIABLES_FUNCTION_HXX
The OpenGM namespace.
Definition: config.hxx:43
Fallback implementation of member functions of OpenGM functions.
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
const bool NO_DEBUG
Definition: config.hxx:64
Funcion that refers to a factor of another GraphicalModel in which some variables are fixed...
ValueType operator()(Iterator begin) const
IndexType shape(const IndexType) const