OpenGM  2.3.x
Discrete Graphical Model Library
accumulation.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_ACCUMULATION_HXX
3 #define OPENGM_ACCUMULATION_HXX
4 
6 
8 
9 namespace opengm {
10 
11 template<class Value, class State, class Accumulator>
12 class Accumulation {
13 public:
15  typedef Value ValueType;
17  typedef State LabelType;
18  Accumulation();
19  ValueType value() const;
20  size_t size() const;
21  Value neutral() const;
22  void state(FastSequence<size_t>&) const;
23  template<class OUT_CONTAINER>
24  void state(OUT_CONTAINER &) const;
25  LabelType state(size_t) const;
26  void clear();
27  void operator()(const ValueType&, const opengm::FastSequence<size_t>&);
28  template<class CONTAINER>
29  void operator()(const ValueType&, const CONTAINER&);
30  void operator()(const ValueType&);
31 private:
32  ValueType value_;
34 };
35 
36 template<class Value, class State, class Accumulator>
37 inline
38 Accumulation<Value, State, Accumulator>::Accumulation()
39 : value_(Accumulator::template neutral<ValueType>()),
41 {}
42 
43 template<class Value, class State, class Accumulator>
44 inline typename Accumulation<Value, State, Accumulator>::ValueType
45 Accumulation<Value, State, Accumulator>::value() const
46 {
47  return(value_);
48 }
49 
50 template<class Value, class State, class Accumulator>
51 inline size_t
52 Accumulation<Value, State, Accumulator>::size() const
53 {
54  return state_.size();
55 }
56 
57 template<class Value, class State, class Accumulator>
58 inline typename Accumulation<Value, State, Accumulator>::ValueType
59 Accumulation<Value, State, Accumulator>::neutral() const
60 {
61  return state_.size();
62 }
63 
64 template<class Value, class State, class Accumulator>
65 inline void
66 Accumulation<Value, State, Accumulator>::state
67 (
69 ) const
70 {
71  out = state_;
72 }
73 
74 template<class Value, class State, class Accumulator>
75 template<class OUT_CONTAINER>
76 inline void
77 Accumulation<Value, State, Accumulator>::state
78 (
79  OUT_CONTAINER& out
80 ) const
81 {
82  out.resize(state_.size());
83  for(size_t i=0;i<state_.size();++i) {
84  out[i]=state_[i];
85  }
86 }
87 
88 template<class Value, class State, class Accumulator>
89 inline typename Accumulation<Value, State, Accumulator>::LabelType
90 Accumulation<Value, State, Accumulator>::state
91 (
92  size_t index
93 ) const
94 {
95  return state_[index];
96 }
97 
98 template<class Value, class State, class Accumulator>
99 inline void
100 Accumulation<Value, State, Accumulator>::clear()
101 {
102  Accumulator::neutral(value_);
103  state_.resize(0);
104 }
105 
106 template<class Value, class State, class Accumulator>
107 inline void
108 Accumulation<Value, State, Accumulator>::operator()
109 (
110  const ValueType& value,
111  const opengm::FastSequence<size_t>& state
112 ) {
113  if(Accumulator::bop(value, value_)) {
114  state_ = state;
115  OPENGM_ASSERT(state_.size()==state.size());
116  }
117  Accumulator::op(value, value_, value_);
118  //OPENGM_ASSERT(state_.size()==state.size());
119 }
120 
121 template<class Value, class State, class Accumulator>
122 template<class CONTAINER>
123 inline void
124 Accumulation<Value, State, Accumulator>::operator()
125 (
126  const ValueType& value,
127  const CONTAINER & state
128 )
129 {
130  if(Accumulator::bop(value, value_)) {
131  state_.resize(state.size());
132  for(size_t i=0;i<state.size();++i) {
133  state_[i] = state[i];
134  }
135  }
136  Accumulator::op(value, value_, value_);
137  OPENGM_ASSERT(state_.size()==state.size());
138 }
139 
140 template<class Value, class State, class Accumulator>
141 inline void
142 Accumulation<Value, State, Accumulator>::operator()
143 (
144  const ValueType& value
145 )
146 {
147  Accumulator::op(value, value_, value_);
148 }
149 
150 } // namespace opengm
151 
153 
154 #endif // #ifndef OPENGM_ACCUMULATION_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
void resize(const size_t)
resize the sequence