OpenGM  2.3.x
Discrete Graphical Model Library
messagepassing_buffer.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_MESSAGE_PASSING_BUFFER_HXX
3 #define OPENGM_MESSAGE_PASSING_BUFFER_HXX
4 
6 
7 namespace opengm {
8 
9  template<class ARRAY>
10  class MessageBuffer {
11  public:
12  typedef ARRAY ArrayType;
13  typedef typename ARRAY::ValueType ValueType;
14 
15  // construction and assignment
16  MessageBuffer();
17  template<class SHAPE_ITERATOR>
18  MessageBuffer(SHAPE_ITERATOR, SHAPE_ITERATOR, const ValueType& = ValueType());
19  template<class SHAPE_ITERATOR>
20  void assign(SHAPE_ITERATOR, SHAPE_ITERATOR, const ValueType& = ValueType());
21  template<class SHAPE>
22  void assign(SHAPE, const ValueType& = ValueType());
23 /*
24  template<class GRAPHICAL_MODEL, class INDEX_ITERATOR>
25  MessageBuffer(const GRAPHICAL_MODEL& , INDEX_ITERATOR, INDEX_ITERATOR, const ValueType& = ValueType());
26  template<class GRAPHICAL_MODEL, class INDEX_ITERATOR>
27  void assign(const GRAPHICAL_MODEL& , INDEX_ITERATOR, INDEX_ITERATOR, const ValueType& = ValueType());
28 */
29  // query
30  const ARRAY& current() const;
31  const ARRAY& old() const;
32  template<class DIST>
33  ValueType dist() const; // distance between current and old
34 
35  ARRAY& current();
36  ARRAY& old();
37 
38  // manipulation
39  void toggle();
40 
41  private:
42  bool flag_;
43  ARRAY buffer1_;
44  ARRAY buffer2_;
45  };
46 
47 
48  //**********************
49  // IMPLEMENTATION
50  //**********************
51 
52  template<class ARRAY>
53  inline MessageBuffer<ARRAY>::MessageBuffer()
54  {}
55 
56  template<class ARRAY>
57  template<class SHAPE_ITERATOR>
58  inline MessageBuffer<ARRAY>::MessageBuffer
59  (
60  SHAPE_ITERATOR begin,
61  SHAPE_ITERATOR end,
62  const typename ARRAY::ValueType& constant
63  ) {
64  assign(begin, end, constant);
65  }
66 /*
67  template<class ARRAY>
68  template<class GRAPHICAL_MODEL, class INDEX_ITERATOR>
69  inline
70  MessageBuffer<ARRAY>::MessageBuffer
71  (
72  const GRAPHICAL_MODEL& gm,
73  INDEX_ITERATOR begin,
74  INDEX_ITERATOR end,
75  const typename ARRAY::ValueType& constant
76  ) {
77  assign(gm, begin, end, constant);
78  }
79 */
80  template<class ARRAY>
81  template<class SHAPE_ITERATOR>
82  inline void MessageBuffer<ARRAY>::assign
83  (
84  SHAPE_ITERATOR begin,
85  SHAPE_ITERATOR end,
86  const typename ARRAY::ValueType& constant
87  )
88  {
89  if(begin == end) {
90  buffer1_ = constant;
91  buffer2_ = constant;
92  }
93  else {
94  buffer1_.assign(begin, end, constant);
95  buffer2_.assign(begin, end, constant);
96  }
97  flag_ = false;
98  }
99 
100  template<class ARRAY>
101  template<class SHAPE>
102  inline void MessageBuffer<ARRAY>::assign
103  (
104  SHAPE shape,
105  const typename ARRAY::ValueType& constant
106  )
107  {
108  if(shape == 0) {
109  buffer1_ = constant;
110  buffer2_ = constant;
111  }
112  else {
113  buffer1_.resize(&shape, &shape+1, constant);
114  buffer2_.resize(&shape, &shape+1, constant);
115  }
116  flag_ = false;
117  }
118 /*
119  template<class ARRAY>
120  template<class GRAPHICAL_MODEL, class INDEX_ITERATOR>
121  inline void MessageBuffer<ARRAY>::assign
122  (
123  const GRAPHICAL_MODEL& gm,
124  INDEX_ITERATOR begin,
125  INDEX_ITERATOR end,
126  const typename ARRAY::ValueType& constant
127  )
128  {
129  if(begin == end) {
130  buffer1_.assign(constant);
131  buffer2_.assign(constant);
132  }
133  else {
134  buffer1_.assign(gm, begin, end, constant);
135  buffer2_.assign(gm, begin, end, constant);
136  }
137  flag_ = false;
138  }
139 */
140 
141  template<class ARRAY>
142  inline ARRAY& MessageBuffer<ARRAY>::current() {
143  return flag_ ? buffer1_ : buffer2_;
144  }
145 
146  template<class ARRAY>
147  inline const ARRAY& MessageBuffer<ARRAY>::current() const {
148  return flag_ ? buffer1_ : buffer2_;
149  }
150 
151  template<class ARRAY>
152  inline ARRAY& MessageBuffer<ARRAY>::old() {
153  return flag_ ? buffer2_ : buffer1_;
154  }
155 
156  template<class ARRAY>
157  inline const ARRAY& MessageBuffer<ARRAY>::old() const {
158  return flag_ ? buffer2_ : buffer1_;
159  }
160 
161  template<class ARRAY>
162  inline void MessageBuffer<ARRAY>::toggle() {
163  flag_ = flag_ ? false : true;
164  }
165 
166  template<class ARRAY>
167  template<class DIST>
168  inline typename ARRAY::ValueType MessageBuffer<ARRAY>::dist() const {
169  return DIST::op(buffer1_, buffer2_);
170  }
171 }
172 
174 
175 #endif // #ifndef OPENGM_MESSAGE_PASSING_BUFFER_HXX
The OpenGM namespace.
Definition: config.hxx:43