OpenGM  2.3.x
Discrete Graphical Model Library
singlesitefunction.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_SINGLESITEFUNCTION_HXX
3 #define OPENGM_SINGLESITEFUNCTION_HXX
4 
5 #include <vector>
6 #include <algorithm>
7 
8 #include "opengm/opengm.hxx"
11 
12 namespace opengm {
13 
15 
16 template<class T, size_t SIZE>
17 class StackStorage {
18 public:
19  StackStorage(){}
20  ~StackStorage(){}
21  template<class ITERATOR>
22  const T & operator()(ITERATOR iter) const {
23  OPENGM_ASSERT(*iter<SIZE);
24  return this->dataPointer_[*iter];
25  }
26  template<class ITERATOR>
27  T & operator()(ITERATOR iter) {
28  OPENGM_ASSERT(*iter<SIZE);
29  return this->dataPointer_[*iter];
30  }
31  T & operator[](const size_t index) {
32  OPENGM_ASSERT(index<SIZE);
33  return this->dataPointer_[index];
34  }
35  const T & operator[](const size_t index) const {
36  OPENGM_ASSERT(index<SIZE);
37  return this->dataPointer_[index];
38  }
39 
40 private:
41  T dataPointer_[SIZE];
42 };
43 
44 template<class T, size_t SIZE>
45 class HeapStorage {
46 public:
47  HeapStorage() {
48  dataPointer_ = new T[SIZE];
49  }
50  ~HeapStorage(){
51  delete[] dataPointer_;
52  }
53  T& operator[](const size_t index) {
54  OPENGM_ASSERT(index<SIZE);
55  return this->dataPointer_[index];
56  }
57  const T& operator[](const size_t index) const {
58  OPENGM_ASSERT(index<SIZE);
59  return this->dataPointer_[index];
60  }
61  template<class ITERATOR>
62  const T& operator()(ITERATOR iter) const {
63  OPENGM_ASSERT(*iter<SIZE);
64  return this->dataPointer_[*iter];
65  }
66  template<class ITERATOR>
67  T& operator()(ITERATOR iter) {
68  OPENGM_ASSERT(*iter<SIZE);
69  return this->dataPointer_[*iter];
70  }
71 
72 private:
73  T* dataPointer_;
74 };
75 
77 
81 template<class T, size_t SIZE, template<typename, size_t> class STORAGE>
83 : STORAGE<T, SIZE>,
84  public FunctionBase<StaticSingleSiteFunction<T, SIZE, STORAGE>, T, size_t, size_t>
85 {
86 public:
87  typedef T ValueType;
88  typedef T value_type;
89 
92  : STORAGE<T, SIZE>()
93  {
94  for(size_t i=0;i<SIZE;++i){
95  (*this).operator [](i)=other[i];
96  }
97  }
98 
100  {
101  if(this != &other) {
102  for(size_t i=0;i<SIZE;++i) {
103  (*this).operator [](i)=other[i];
104  }
105  }
106  return *this;
107  }
108 
109  template<class ITERATOR> const T& operator()(ITERATOR iter) const
110  {
111  OPENGM_ASSERT(*iter < SIZE);
112  return (static_cast<const STORAGE<T, SIZE>&>(*this)).operator()(iter);
113  }
114 
115  template<class ITERATOR> T& operator()(ITERATOR iter)
116  {
117  OPENGM_ASSERT(*iter<SIZE);
118  return (static_cast<STORAGE<T, SIZE>&>(*this)).operator()(iter);
119  }
120 
121  size_t size() const
122  { return SIZE; }
123 
124  size_t dimension() const
125  { return 1; }
126 
127  size_t shape(const size_t index) const
128  {
129  OPENGM_ASSERT(index == 0);
130  return SIZE;
131  }
132 };
133 
137 template<class T>
139 public:
140  typedef T ValueType;
141  typedef T value_type;
142 
143  DynamicSingleSiteFunction(const size_t size = 0)
144  : size_(size)
145  {
146  if(size_ != 0){
147  dataPointer_ = new T[size];
148  }
149  }
150 
151  DynamicSingleSiteFunction(const size_t size, ValueType value)
152  : size_(size)
153  {
154  if(size_ != 0) {
155  dataPointer_ = new T[size];
156  std::fill(dataPointer_, dataPointer_+size_, value);
157  }
158  }
159 
161  {
162  if(size_ != 0) {
163  delete[] dataPointer_;
164  }
165  }
166 
168  {
169  if(other.size_ != 0) {
170  dataPointer_ = new T[other.size_];
171  size_ = other.size_;
172  std::copy(other.dataPointer_, other.dataPointer_+size_, dataPointer_);
173  }
174  else {
175  size_=0;
176  }
177  }
178 
180  {
181  if(this != &other) {
182  if(other.size_ > size_) {
183  delete[] dataPointer_;
184  dataPointer_ = new T[other.size_];
185  size_ = other.size_;
186  std::copy(other.dataPointer_, other.dataPointer_ + size_, dataPointer_);
187  }
188  else if(other.size_ < size_) {
189  delete[] dataPointer_;
190  if(other.size_!= 0) {
191  dataPointer_= new T[other.size_];
192  size_ = other.size_;
193  std::copy(other.dataPointer_, other.dataPointer_+size_, dataPointer_);
194  }
195  else {
196  size_ = 0;
197  }
198  }
199  if(other.size_ == size_) {
200  std::copy(other.dataPointer_, other.dataPointer_+size_, dataPointer_);
201  }
202  }
203  return *this;
204  }
205 
206  void assign(const size_t size)
207  {
208  if(size_ != size){
209  delete[] dataPointer_;
210  if(size != 0){
211  dataPointer_ = new T[size];
212  size_=size;
213  }
214  else {
215  size_ = 0;
216  }
217  }
218  }
219 
220  void assign(const size_t size, const T value)
221  {
222  if(size_ != size){
223  delete[] dataPointer_;
224  if(size != 0) {
225  dataPointer_ = new T[size];
226  size_ = size;
227  std::fill(dataPointer_, dataPointer_+size_, value);
228  }
229  else {
230  size_=0;
231  }
232  }
233  else {
234  std::fill(dataPointer_, dataPointer_+size_, value);
235  }
236  }
237 
238  size_t size() const
239  { return size_; }
240 
241  size_t dimension() const
242  { return 1; }
243 
244  size_t shape(const size_t index) const
245  {
246  OPENGM_ASSERT(index==0);
247  return size_;
248  }
249 
250  T& operator[](const size_t index)
251  {
252  OPENGM_ASSERT(index < size_);
253  return dataPointer_[index];
254  }
255 
256  const T & operator[](const size_t index) const
257  {
258  OPENGM_ASSERT(index < size_);
259  return dataPointer_[index];
260  }
261 
262  template<class ITERATOR>
263  const T& operator()(ITERATOR iter) const
264  {
265  OPENGM_ASSERT(*iter < size_);
266  return dataPointer_[*iter];
267  }
268 
269  template<class ITERATOR>
270  T& operator()(ITERATOR iter)
271  {
272  OPENGM_ASSERT(*iter < size_);
273  return dataPointer_[*iter];
274  }
275 
276 private:
277  T* dataPointer_;
278  size_t size_;
279 };
280 
283 template<class T, size_t SIZE, template < typename , size_t > class STORAGE>
284 struct FunctionRegistration< StaticSingleSiteFunction<T, SIZE, STORAGE> > {
285  enum ID { Id = opengm::FUNCTION_TYPE_ID_OFFSET + 9 };
286 };
287 
289 template<class T>
290 struct FunctionRegistration< DynamicSingleSiteFunction<T> > {
291  enum ID { Id = opengm::FUNCTION_TYPE_ID_OFFSET + 10 };
292 };
293 
295 template<class T, size_t SIZE, template < typename , size_t > class STORAGE>
296 class FunctionSerialization< StaticSingleSiteFunction<T, SIZE, STORAGE> > {
297 public:
298  typedef typename StaticSingleSiteFunction<T, SIZE, STORAGE> ::ValueType ValueType;
299 
300  static size_t indexSequenceSize(const StaticSingleSiteFunction<T, SIZE, STORAGE> & f){
301  return 0;
302  }
303 
304  static size_t valueSequenceSize(const StaticSingleSiteFunction<T, SIZE, STORAGE> & f){
305  return SIZE;
306  }
307 
308  template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
309  static void serialize(const StaticSingleSiteFunction<T, SIZE, STORAGE> & f, INDEX_OUTPUT_ITERATOR ii, VALUE_OUTPUT_ITERATOR vi){
310  for(size_t i=0;i<SIZE;++i){
311  size_t c[]={i};
312  *vi=f(c);
313  ++vi;
314  }
315  }
316 
317  template<class INDEX_INPUT_ITERATOR , class VALUE_INPUT_ITERATOR>
318  static void deserialize( INDEX_INPUT_ITERATOR ii, VALUE_INPUT_ITERATOR vi, StaticSingleSiteFunction<T, SIZE, STORAGE> & f){
319  for(size_t i=0;i<SIZE;++i){
320  size_t c[]={i};
321  f(c)=*vi;
322  ++vi;
323  }
324  }
325 };
326 
328 template<class T>
329 class FunctionSerialization< DynamicSingleSiteFunction<T> > {
330 public:
331  typedef typename DynamicSingleSiteFunction<T>::ValueType ValueType;
332 
333  static size_t indexSequenceSize(const DynamicSingleSiteFunction<T> & f){
334  return 1;
335  }
336 
337  static size_t valueSequenceSize(const DynamicSingleSiteFunction<T> & f){
338  return f.size();
339  }
340 
341  template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
342  static void serialize(const DynamicSingleSiteFunction<T> & f, INDEX_OUTPUT_ITERATOR ii, VALUE_OUTPUT_ITERATOR vi){
343  for(size_t i=0;i<f.size();++i){
344  size_t c[]={i};
345  *vi=f(c);
346  ++vi;
347  }
348  }
349 
350  template<class INDEX_INPUT_ITERATOR , class VALUE_INPUT_ITERATOR>
351  static void deserialize( INDEX_INPUT_ITERATOR ii, VALUE_INPUT_ITERATOR vi, DynamicSingleSiteFunction<T> & f){
352  const size_t size=*ii;
353  f.assign(size);
354  for(size_t i=0;i<size;++i){
355  size_t c[]={i};
356  f(c)=*vi;
357  ++vi;
358  }
359  }
360 };
362 
363 } // namespace opengm
364 
365 #endif // #ifndef OPENGM_SINGLESITEFUNCTION_HXX
size_t shape(const size_t index) const
void assign(const size_t size, const T value)
The OpenGM namespace.
Definition: config.hxx:43
Fallback implementation of member functions of OpenGM functions.
DynamicSingleSiteFunction(const size_t size, ValueType value)
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
Single site function with dynamic size.
const T & operator()(ITERATOR iter) const
Single site function whose size is fixed at compile time.
StaticSingleSiteFunction & operator=(const StaticSingleSiteFunction &other)
DynamicSingleSiteFunction(const DynamicSingleSiteFunction &other)
StaticSingleSiteFunction(const StaticSingleSiteFunction &other)
DynamicSingleSiteFunction(const size_t size=0)
const size_t FUNCTION_TYPE_ID_OFFSET
User-defined function have ids smaller than FUNCTION_TYPE_ID_OFFSET.
const T & operator()(ITERATOR iter) const
const T & operator[](const size_t index) const
DynamicSingleSiteFunction & operator=(const DynamicSingleSiteFunction &other)
size_t shape(const size_t index) const