OpenGM  2.3.x
Discrete Graphical Model Library
datastructures/sparsemarray/sparsemarray.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_SPARSEMARRAY
3 #define OPENGM_SPARSEMARRAY
4 
5 #include <algorithm>
6 #include <iostream>
7 #include <map>
9 
10 
11 namespace opengm {
12 
13 template<class T,class I,class L,class CONTAINER=std::map<I,T> >
14 class SparseFunction : public FunctionBase<SparseFunction<T, I, L,CONTAINER>, T, I, L> {
15 public:
16  typedef CONTAINER ContainerType;
17  typedef typename ContainerType::key_type KeyType;
18  typedef typename ContainerType::mapped_type MappedType;
19  typedef std::pair<KeyType,MappedType> KeyValPairType;
20  typedef T ValueType;
21  typedef I IndexType;
22  typedef L LabelType;
23 
24 
25  typedef typename ContainerType::const_iterator ConstContainerIteratorType;
26  typedef typename ContainerType::iterator ContainerIteratorType;
27 
28 
30  dimension_(0),
31  defaultValue_(0),
32  container_(),
33  shape_(),
34  strides_(){
35  }
36 
37 
38  //constructors
39  template<class SHAPE_ITERATOR>
40  SparseFunction(SHAPE_ITERATOR shapeBegin,SHAPE_ITERATOR shapeEnd ,const ValueType defaultValue):
41  dimension_(std::distance(shapeBegin,shapeEnd)),
42  defaultValue_(defaultValue),
43  container_(){
44  shape_.resize(dimension_);
45  strides_.resize(dimension_);
46  // compute strides
47  LabelType strideVal=1;
48  for(unsigned short dim=0;dim<dimension_;++dim){
49  shape_[dim]=*shapeBegin;
50  strides_[dim]=strideVal;
51  strideVal*=shape_[dim];
52  ++shapeBegin;
53  }
54  }
55 
56  size_t size()const{
57  size_t size =1;
58  for(unsigned short dim=0;dim<dimension_;++dim){
59  size*=static_cast<size_t>(shape_[dim]);
60  }
61  return size;
62  }
63 
64  const ContainerType & container()const{
65  return container_;
66  }
67  ContainerType & container(){
68  return container_;
69  }
70  const size_t dimension()const{
71  return dimension_;
72  }
73  const LabelType shape(const IndexType i)const{
74  return shape_[i];
75  }
76 
77 
78 
79  template<class COORDINATE_ITERATOR>
80  void keyToCoordinate(const KeyType key, COORDINATE_ITERATOR coordinate )const{
81  typedef typename std::iterator_traits<COORDINATE_ITERATOR>::value_type CoordType;
82  KeyType keyRest=key;
83  if(dimension_!=1){
84  for(unsigned short d=0;d<dimension_;++d){
85  const unsigned short dim=(dimension_-1)-d;
86  const KeyType c=keyRest/static_cast<KeyType>( strides_[dim] );
87  keyRest=keyRest-c*static_cast<KeyType>( strides_[dim] );
88  coordinate[dim]=static_cast<CoordType>(c);
89  }
90  }
91  else{
92  *coordinate=static_cast<CoordType>(key);
93  }
94  }
95 
96 
97  template<class COORDINATE_ITERATOR>
98  KeyType coordinateToKey(COORDINATE_ITERATOR coordinate)const{
99  KeyType key=static_cast<KeyType>(0);
100  for(unsigned short dim=0;dim<dimension_;++dim){
101  key+=strides_[dim]*static_cast<KeyType>(*coordinate);
102  ++coordinate;
103  }
104  return key;
105  }
106 
107  template<class COORDINATE_ITERATOR,size_t DIM>
108  KeyType coordinateToKeyWithDim(COORDINATE_ITERATOR coordinate )const{
109  KeyType key=static_cast<KeyType>(0);
110  for(unsigned short dim=0;dim<DIM;++dim){
111  key+=strides_[dim]*static_cast<KeyType>(coordinate[dim]);
112  }
113  return key;
114  }
115 
116  template<class COORDINATE_ITERATOR>
117  ValueType operator()(COORDINATE_ITERATOR coordinate)const{
118  typedef COORDINATE_ITERATOR CoordType;
119  KeyType key;//=coordinateToKey(coordinate);
120 
121  switch (dimension_)
122  {
123  case 1:
124  return valueFromKey(coordinateToKeyWithDim<CoordType,1>(coordinate));
125  case 2:
126  return valueFromKey(coordinateToKeyWithDim<CoordType,2>(coordinate));
127  case 3:
128  return valueFromKey(coordinateToKeyWithDim<CoordType,3>(coordinate));
129  case 4:
130  return valueFromKey(coordinateToKeyWithDim<CoordType,4>(coordinate));
131  case 5:
132  return valueFromKey(coordinateToKeyWithDim<CoordType,5>(coordinate));
133  case 6:
134  return valueFromKey(coordinateToKeyWithDim<CoordType,6>(coordinate));
135  case 7:
136  return valueFromKey(coordinateToKeyWithDim<CoordType,7>(coordinate));
137  case 8:
138  return valueFromKey(coordinateToKeyWithDim<CoordType,8>(coordinate));
139  case 9:
140  return valueFromKey(coordinateToKeyWithDim<CoordType,9>(coordinate));
141  case 10:
142  return valueFromKey(coordinateToKeyWithDim<CoordType,10>(coordinate));
143  case 11:
144  return valueFromKey(coordinateToKeyWithDim<CoordType,11>(coordinate));
145  case 12:
146  return valueFromKey(coordinateToKeyWithDim<CoordType,12>(coordinate));
147  case 13:
148  return valueFromKey(coordinateToKeyWithDim<CoordType,13>(coordinate));
149  case 14:
150  return valueFromKey(coordinateToKeyWithDim<CoordType,14>(coordinate));
151  case 15:
152  return valueFromKey(coordinateToKeyWithDim<CoordType,15>(coordinate));
153  case 16:
154  return valueFromKey(coordinateToKeyWithDim<CoordType,16>(coordinate));
155  default:
156  return valueFromKey(coordinateToKey(coordinate));
157  }
158  }
159 
160  ValueType defaultValue()const{
161  return defaultValue_;
162  }
163 
164  ValueType valueFromKey(const KeyType key)const{
165  ConstContainerIteratorType iter=container_.find(key);
166  if(iter!=container_.end()){
167  return iter->second;
168  }
169  else{
170  return defaultValue_;
171  }
172  }
173 
174  template<class COORDINATE_ITERATOR>
175  void insert(COORDINATE_ITERATOR coordinate,const ValueType value){
176  container_.insert(KeyValPairType(coordinateToKey(coordinate),value));
177  }
178 
179 private:
180  unsigned short dimension_;
181  ValueType defaultValue_;
182  ContainerType container_;
183  std::vector<LabelType> shape_;
184  std::vector<size_t> strides_;
185 
186 };
187 }
188 
189 #endif
void insert(COORDINATE_ITERATOR coordinate, const ValueType value)
The OpenGM namespace.
Definition: config.hxx:43
Fallback implementation of member functions of OpenGM functions.
KeyType coordinateToKeyWithDim(COORDINATE_ITERATOR coordinate) const
SparseFunction(SHAPE_ITERATOR shapeBegin, SHAPE_ITERATOR shapeEnd, const ValueType defaultValue)
STL namespace.
const LabelType shape(const IndexType i) const
ValueType operator()(COORDINATE_ITERATOR coordinate) const
ContainerType::const_iterator ConstContainerIteratorType
ValueType valueFromKey(const KeyType key) const
void keyToCoordinate(const KeyType key, COORDINATE_ITERATOR coordinate) const
KeyType coordinateToKey(COORDINATE_ITERATOR coordinate) const
std::pair< KeyType, MappedType > KeyValPairType