OpenGM  2.3.x
Discrete Graphical Model Library
shape_accessor.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_SHAPE_ACCESSOR_HXX
3 #define OPENGM_SHAPE_ACCESSOR_HXX
4 
5 namespace opengm {
6 
8 
9  template<class FUNCTION>
10  class FunctionShapeAccessor {
11  public:
12  typedef size_t value_type;
13  typedef const value_type reference;
14  typedef const value_type* pointer;
15  typedef const FUNCTION& factor_reference;
16  typedef const FUNCTION* factor_pointer;
17 
18  FunctionShapeAccessor(factor_pointer f = NULL)
19  : factor_(f)
20  {}
21  FunctionShapeAccessor(factor_reference f)
22  : factor_(&f)
23  {}
24  size_t size() const {
25  return factor_ == NULL ? 0 : factor_->dimension();
26  }
27  value_type operator[](const size_t j) {
28  OPENGM_ASSERT(j<factor_->dimension());
29  return factor_->shape(j);
30  }
31  const value_type operator[](const size_t j) const {
32  OPENGM_ASSERT(j<factor_->dimension());
33  return factor_->shape(j);
34  }
35  bool operator==(const FunctionShapeAccessor<FUNCTION> & other) const
36  { return factor_ == other.factor_; }
37 
38  private:
39  factor_pointer factor_;
40  };
41 
42  template<class FACTOR>
43  class FactorShapeAccessor {
44  public:
45  typedef size_t value_type;
46  typedef const value_type reference;
47  typedef const value_type* pointer;
48  typedef const FACTOR& factor_reference;
49  typedef const FACTOR* factor_pointer;
50 
51  FactorShapeAccessor(factor_pointer f = 0)
52  : factor_(f)
53  {}
54  FactorShapeAccessor(factor_reference f)
55  : factor_(&f)
56  {}
57  size_t size() const
58  { return factor_ == 0 ? 0 : factor_->numberOfVariables(); }
59  reference operator[](const size_t j)
60  { return factor_->numberOfLabels(j); }
61  const value_type operator[](const size_t j) const
62  { return factor_->numberOfLabels(j); }
63  bool operator==(const FactorShapeAccessor<FACTOR> & other) const
64  { return factor_ == other.factor_; }
65 
66  private:
67  factor_pointer factor_;
68  };
69 
70 
71 
72  template<class SUBSET_ITERATOR, class GM_LABEL_ITER>
73  class SubsetAccessor {
74  public:
75  typedef typename std::iterator_traits<GM_LABEL_ITER>::value_type value_type;
76 
77  typedef const value_type reference;
78  typedef const value_type* pointer;
79 
80  SubsetAccessor()
81  : sBegin_(),
82  sEnd_(),
83  gmLabelIter_()
84  {}
85  SubsetAccessor(SUBSET_ITERATOR sBegin, SUBSET_ITERATOR sEnd , GM_LABEL_ITER iter)
86  : sBegin_(sBegin),
87  sEnd_(sEnd),
88  gmLabelIter_(iter)
89  {}
90  size_t size() const
91  { return std::distance(sBegin_, sEnd_); }
92  reference operator[](const size_t j)
93  { return gmLabelIter_[sBegin_[j]]; }
94  const value_type operator[](const size_t j) const
95  { return gmLabelIter_[sBegin_[j]]; }
96  bool operator==(const SubsetAccessor & other) const
97  {
98  return sBegin_ == other.sBegin_ &&
99  sEnd_ == other.sEnd_ &&
100  gmLabelIter_==other.gmLabelIter_;
101  }
102 
103  private:
104  SUBSET_ITERATOR sBegin_;
105  SUBSET_ITERATOR sEnd_;
106  GM_LABEL_ITER gmLabelIter_;
107  };
108 
109 
110 
111  template<class FACTOR, class GM_LABEL_ITER>
112  class GmLabelFactorLabelAccessor {
113  public:
114  typedef typename std::iterator_traits<GM_LABEL_ITER>::value_type value_type;
115 
116  typedef const value_type reference;
117  typedef const value_type* pointer;
118  typedef const FACTOR& factor_reference;
119  typedef const FACTOR* factor_pointer;
120 
121  GmLabelFactorLabelAccessor()
122  : factor_(NULL),
123  gmLabelIter_()
124  {}
125  GmLabelFactorLabelAccessor(factor_reference f , GM_LABEL_ITER iter)
126  : factor_(&f),
127  gmLabelIter_(iter)
128  {}
129  size_t size() const
130  { return factor_ == 0 ? 0 : factor_->numberOfVariables(); }
131  reference operator[](const size_t j)
132  { return gmLabelIter_[factor_->variableIndex(j)]; }
133  const value_type operator[](const size_t j) const
134  { return gmLabelIter_[factor_->variableIndex(j)]; }
135  bool operator==(const FactorShapeAccessor<FACTOR> & other) const
136  { return factor_ == other.factor_ && gmLabelIter_==other.gmLabelIter_;
137  }
138 
139  private:
140  factor_pointer factor_;
141  GM_LABEL_ITER gmLabelIter_;
142  };
143 
144 
145 
146  template<class FACTOR>
147  class FactorVariablesAccessor {
148  public:
149  typedef typename FACTOR::IndexType IndexType;
150  typedef IndexType value_type;
151  typedef const value_type reference;
152  typedef const value_type* pointer;
153  typedef const FACTOR& factor_reference;
154  typedef const FACTOR* factor_pointer;
155 
156  FactorVariablesAccessor(factor_pointer f = 0)
157  : factor_(f)
158  {}
159  FactorVariablesAccessor(factor_reference f)
160  : factor_(&f)
161  {}
162  IndexType size() const
163  { return factor_ == 0 ? 0 : factor_->numberOfVariables(); }
164  reference operator[](const size_t j)
165  { return factor_->numberOfLabels(j); }
166  const value_type operator[](const size_t j) const
167  { return factor_->variableIndex(j); }
168  bool operator==(const FactorVariablesAccessor<FACTOR> & other) const
169  { return factor_ == other.factor_; }
170 
171  private:
172  factor_pointer factor_;
173  };
174 
176 
177 } // namespace opengm
178 
179 #endif // #ifndef OPENGM_SHAPE_ACCESSOR_HXX
180 
The OpenGM namespace.
Definition: config.hxx:43
bool operator==(const IndicatorVariable< INDEX1_TYPE, LABEL1_TYPE > &indicatorVar1, const IndicatorVariable< INDEX2_TYPE, LABEL2_TYPE > &indicatorVar2)
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77