OpenGM  2.3.x
Discrete Graphical Model Library
indexing.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_INDEXING_HXX
3 #define OPENGM_INDEXING_HXX
4 
5 #include "opengm/opengm.hxx"
7 
8 namespace opengm {
10  template<class VECTOR>
11  bool isEqualValueVector(const VECTOR vector) {
12  for(size_t i=0;i<vector.size();++i) {
13  if(vector[0]!=vector[i]) {
14  return false;
15  }
16  }
17  return true;
18  }
19 
21  template<class Iterator,class FIXED_COORDINATE_INDEX_CONTAINER,class FIXED_COORDINATE_VALUE_CONTAINER>
22  class SubShapeWalker{
23  public:
29  SubShapeWalker
30  (
31  Iterator shapeBegin,
32  const size_t dimension,
33  const FIXED_COORDINATE_INDEX_CONTAINER & fixedCoordinateIndex,
34  const FIXED_COORDINATE_VALUE_CONTAINER & fixedCoordinateValue
35  )
36  : shapeBegin_(shapeBegin),
37  coordinateTuple_(dimension,0),
38  fixedCoordinateValue_(fixedCoordinateValue),
39  fixedCoordinateIndex_(fixedCoordinateIndex),
40  dimension_(dimension) {
41  for(size_t d = 0; d < fixedCoordinateIndex_.size(); ++d) {
42  coordinateTuple_[fixedCoordinateIndex_[d]] = fixedCoordinateValue_[d];
43  }
44  };
45 
47  void resetCoordinate() {
48  for(size_t i = 0; i < dimension_; ++i) {
49  coordinateTuple_[i] = static_cast<size_t>(0);
50  }
51  for(size_t i = 0; i < fixedCoordinateIndex_.size(); ++i) {
52  coordinateTuple_[fixedCoordinateIndex_[i]] = fixedCoordinateValue_[i];
53  }
54  };
55 
57  inline SubShapeWalker & operator++() {
58  size_t counter = 0;
59  for(size_t d = 0; d < dimension_; ++d) {
60  bool atFixedValue = false;
61  for(size_t i = counter; i < fixedCoordinateIndex_.size(); ++i) {
62  if(d == fixedCoordinateIndex_[i]) {
63  atFixedValue = true;
64  ++counter;
65  }
66  }
67  if(atFixedValue == false) {
68  if(coordinateTuple_[d] != shapeBegin_[d] - 1) {
69  coordinateTuple_[d]++;
70  break;
71  }
72  else {
73  if(d != dimension_ - 1) {
74  coordinateTuple_[d] = 0;
75  }
76  else {
77  coordinateTuple_[d]++;
78  break;
79  }
80  }
81  }
82  }
83  return *this;
84  };
85 
87  const opengm::FastSequence<size_t> & coordinateTuple()const {
88  return coordinateTuple_;
89  };
90 
92  size_t subSize() {
93  size_t result = 1;
94  size_t counter = 0;
95  for(size_t d = 0; d < dimension_; ++d) {
96  bool fixedVal = false;
97  for(size_t i = counter; i < fixedCoordinateIndex_.size(); ++i) {
98  if(d == fixedCoordinateIndex_[i])
99  {
100  fixedVal = true;
101  counter++;
102  break;
103  }
104  }
105  if(fixedVal == false) {
106  result *= shapeBegin_[d];
107  }
108  }
109  return result;
110  }
111 
112  private:
113  Iterator shapeBegin_;
114  opengm::FastSequence<size_t> coordinateTuple_;
115  const FIXED_COORDINATE_VALUE_CONTAINER & fixedCoordinateValue_;
116  const FIXED_COORDINATE_INDEX_CONTAINER & fixedCoordinateIndex_;
117  const size_t dimension_;
118  //size_t subSize_;
119  };
120 
121  template<class Iterator>
122  class ShapeWalker
123  {
124  public:
125  ShapeWalker(Iterator shapeBegin,size_t dimension)
126  : shapeBegin_(shapeBegin),
127  coordinateTuple_(dimension, 0),
128  dimension_(dimension) { }
129  ShapeWalker & operator++() {
130  for(size_t d = 0; d < dimension_; ++d) {
131  if( size_t(coordinateTuple_[d]) != (size_t(shapeBegin_[d]) - size_t(1)) ) {
132  ++coordinateTuple_[d];
133  OPENGM_ASSERT(coordinateTuple_[d]<shapeBegin_[d]);
134  break;
135  }
136  else {
137  if(d != dimension_ - 1) {
138  coordinateTuple_[d] = 0;
139  }
140  else {
141  coordinateTuple_[d]++;
142  break;
143  }
144  }
145  }
146  return *this;
147  };
148  const opengm::FastSequence<size_t> & coordinateTuple()const {
149  return coordinateTuple_;
150  };
151  void reset(){
152  std::fill(coordinateTuple_.begin(),coordinateTuple_.end(),0);
153  }
154  private:
155  Iterator shapeBegin_;
156  opengm::FastSequence<size_t> coordinateTuple_;
157  const size_t dimension_;
158  };
159 
160  template<class Iterator>
161  class ShapeWalkerSwitchedOrder
162  {
163  public:
164  ShapeWalkerSwitchedOrder(Iterator shapeBegin,size_t dimension)
165  : shapeBegin_(shapeBegin),
166  coordinateTuple_(dimension, 0),
167  dimension_(dimension) { }
168  ShapeWalkerSwitchedOrder & operator++() {
169  for(size_t d = dimension_-1; true; --d) {
170  if( size_t(coordinateTuple_[d]) != (size_t(shapeBegin_[d]) - size_t(1)) ) {
171  ++coordinateTuple_[d];
172  OPENGM_ASSERT(coordinateTuple_[d]<shapeBegin_[d]);
173  break;
174  }
175  else {
176  if(d != 0) {
177  coordinateTuple_[d] = 0;
178  }
179  else {
180  coordinateTuple_[d]++;
181  break;
182  }
183  }
184  //if(d==0){
185  // break;
186  //}
187  }
188  return *this;
189  };
190  const opengm::FastSequence<size_t> & coordinateTuple()const {
191  return coordinateTuple_;
192  };
193 
194  private:
195  Iterator shapeBegin_;
196  opengm::FastSequence<size_t> coordinateTuple_;
197  const size_t dimension_;
198  };
199 
200  template<class SHAPE_AB_ITERATOR>
201  class TripleShapeWalker{
202  public:
203  template<class VI_AB,class VI_A,class VI_B>
204  TripleShapeWalker
205  (
206  SHAPE_AB_ITERATOR shapeABBegin,
207  const size_t dimAB,
208  const VI_AB & viAB,
209  const VI_A & viA,
210  const VI_B & viB
211  ): shapeABBegin_(shapeABBegin),
212  dimensionAB_(dimAB),
213  coordinateTupleAB_(viAB.size(), 0),
214  coordinateTupleA_(viA.size(), 0),
215  coordinateTupleB_(viB.size(), 0),
216  viMatchA_(viAB.size(), false),
217  viMatchB_(viAB.size(), false),
218  viMatchIndexA_(viAB.size()),
219  viMatchIndexB_(viAB.size()) {
220  OPENGM_ASSERT(dimAB == viAB.size());
221  OPENGM_ASSERT( viA.size() != 0);
222  OPENGM_ASSERT( viB.size() != 0);
223  //vi matching:
224  size_t counterA = 0;
225  size_t counterB = 0;
226  for(size_t d = 0; d < dimensionAB_; ++d) {
227  if(counterA<viA.size()) {
228  if(viAB[d] == viA[counterA]) {
229  viMatchA_[d] = true;
230  viMatchIndexA_[d] = counterA;
231  counterA++;
232  }
233  }
234  if(counterB<viB.size()) {
235  if(viAB[d] == viB[counterB]) {
236  viMatchB_[d] = true;
237  viMatchIndexB_[d] = counterB;
238  counterB++;
239  }
240  }
241  }
242  }
243 
244  TripleShapeWalker & operator++() {
245  for(size_t d = 0; d < dimensionAB_; ++d) {
246  if( int (coordinateTupleAB_[d]) != int( int(shapeABBegin_[d]) - int(1))) {
247  coordinateTupleAB_[d]++;
248  if(viMatchA_[d]) {
249  coordinateTupleA_[viMatchIndexA_[d]]++;
250  }
251  if(viMatchB_[d]) {
252  coordinateTupleB_[viMatchIndexB_[d]]++;
253  }
254  break;
255  }
256  else {
257  coordinateTupleAB_[d] = 0;
258  if(viMatchA_[d]) {
259  coordinateTupleA_[viMatchIndexA_[d]] = 0;
260  }
261  if(viMatchB_[d]) {
262  coordinateTupleB_[viMatchIndexB_[d]] = 0;
263  }
264  }
265  }
266  return *this;
267  };
268 
269  const opengm::FastSequence<size_t> &coordinateTupleA()const {
270  return coordinateTupleA_;
271  };
272 
273  const opengm::FastSequence<size_t> & coordinateTupleB()const {
274  return coordinateTupleB_;
275  };
276 
277  const opengm::FastSequence<size_t> & coordinateTupleAB()const {
278  return coordinateTupleAB_;
279  };
280  private:
281  SHAPE_AB_ITERATOR shapeABBegin_;
282  const size_t dimensionAB_;
283  opengm::FastSequence<size_t> coordinateTupleAB_;
284  opengm::FastSequence<size_t> coordinateTupleA_;
285  opengm::FastSequence<size_t> coordinateTupleB_;
286  opengm::FastSequence<bool> viMatchA_;
287  opengm::FastSequence<bool> viMatchB_;
288  opengm::FastSequence<size_t> viMatchIndexA_;
289  opengm::FastSequence<size_t> viMatchIndexB_;
290  };
291 
292  template<class SHAPE_AB_ITERATOR>
293  class DoubleShapeWalker {
294  public:
295  template<class VI_A,class VI_B>
296  DoubleShapeWalker
297  (
298  SHAPE_AB_ITERATOR shapeABbegin,
299  const size_t dimAb,
300  const VI_A & viAB,
301  const VI_B & viA
302  )
303  :shapeABbegin_(shapeABbegin),
304  dimensionAB_(dimAb),
305  coordinateTupleAB_(dimensionAB_, 0),
306  coordinateTupleA_(viA.size(), 0),
307  viMatchA_(dimensionAB_, false),
308  viMatchIndexA_(dimensionAB_) {
309  //vi matching:
310  size_t counterA = 0;
311  for(size_t d = 0; d < dimensionAB_; ++d) {
312  for(size_t i = counterA; i < viA.size(); ++i) {
313  if(viAB[d] == viA[i]) {
314  viMatchA_[d] = true;
315  viMatchIndexA_[d] = i;
316  ++counterA;
317  }
318  }
319  }
320  }
321 
322  DoubleShapeWalker & operator++() {
323  for(size_t d = 0; d < dimensionAB_; ++d) {
324  if(coordinateTupleAB_[d] != shapeABbegin_[d] - 1) {
325  coordinateTupleAB_[d]++;
326  if(viMatchA_[d] == true) {
327  coordinateTupleA_[viMatchIndexA_[d]]++;
328  }
329  break;
330  }
331  else {
332  coordinateTupleAB_[d] = 0;
333  if(viMatchA_[d] == true) {
334  coordinateTupleA_[viMatchIndexA_[d]] = 0;
335  }
336  }
337  }
338  return *this;
339  };
340 
341  const opengm::FastSequence<size_t> & coordinateTupleA()const {
342  return coordinateTupleA_;
343  };
344 
345  const opengm::FastSequence<size_t> & coordinateTupleAB()const {
346  return coordinateTupleAB_;
347  };
348 
349  private:
350  SHAPE_AB_ITERATOR shapeABbegin_;
351  const size_t dimensionAB_;
352  opengm::FastSequence<size_t> coordinateTupleAB_;
353  opengm::FastSequence<size_t> coordinateTupleA_;
354  opengm::FastSequence<bool> viMatchA_;
355  opengm::FastSequence<size_t> viMatchIndexA_;
356  };
357 
358 } // namespace opengm
359 
360 #endif // #ifndef OPENGM_INDEXING_HXX
The OpenGM namespace.
Definition: config.hxx:43
Vector that stores values on the stack if size is smaller than MAX_STACK.
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
View< T, false, A > & operator++(View< T, false, A > &v)
Definition: marray.hxx:3082