00001 #pragma once
00002 #ifndef OPENGM_EXPLICIT_FUNCTION_HXX
00003 #define OPENGM_EXPLICIT_FUNCTION_HXX
00004
00005 #include "opengm/datastructures/marray/marray.hxx"
00006 #include "opengm/functions/function_registration.hxx"
00007 #include "opengm/functions/function_properties_base.hxx"
00008
00009 namespace opengm {
00010
00014 template<class T, class I=size_t, class L=size_t>
00015 class ExplicitFunction
00016 : public marray::Marray<T>,
00017 public FunctionBase<ExplicitFunction<T, I, L>, T, I, L>
00018 {
00019 public:
00020 typedef T ValueType;
00021 typedef L LabelType;
00022 typedef I IndexType;
00023
00024 ExplicitFunction()
00025 : marray::Marray<T>()
00026 {}
00027
00029 ExplicitFunction(const T& value)
00030 : marray::Marray<T>(value)
00031 {}
00032
00033 ExplicitFunction(const ExplicitFunction& other)
00034 : marray::Marray<T>(other)
00035 {}
00036
00037 ExplicitFunction& operator=(const ExplicitFunction& other)
00038 {
00039 marray::Marray<T>::operator=(other);
00040 return *this;
00041 }
00042
00051 template <class SHAPE_ITERATOR>
00052 ExplicitFunction(SHAPE_ITERATOR shapeBegin, SHAPE_ITERATOR shapeEnd)
00053 : marray::Marray<T>(shapeBegin, shapeEnd)
00054 {}
00055
00057 template <class SHAPE_ITERATOR>
00058 ExplicitFunction(SHAPE_ITERATOR shapeBegin, SHAPE_ITERATOR shapeEnd, const T & value)
00059 : marray::Marray<T>(shapeBegin, shapeEnd, value)
00060 {}
00061 };
00062
00065 template<class T, class I, class L>
00066 struct FunctionRegistration< ExplicitFunction<T, I, L> >{
00067 enum ID {
00068 Id=opengm::FUNCTION_TYPE_ID_OFFSET
00069 };
00070 };
00071
00073 template<class T, class I, class L>
00074 class FunctionSerialization< ExplicitFunction<T, I, L> >{
00075 public:
00076 typedef typename ExplicitFunction<T, I, L>::value_type ValueType;
00077
00078 static size_t indexSequenceSize(const ExplicitFunction<T, I, L> &);
00079 static size_t valueSequenceSize(const ExplicitFunction<T, I, L> &);
00080 template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
00081 static void serialize(const ExplicitFunction<T, I, L> &, INDEX_OUTPUT_ITERATOR, VALUE_OUTPUT_ITERATOR );
00082 template<class INDEX_INPUT_ITERATOR , class VALUE_INPUT_ITERATOR>
00083 static void deserialize( INDEX_INPUT_ITERATOR, VALUE_INPUT_ITERATOR, ExplicitFunction<T, I, L> &);
00084 };
00086
00087 template<class T, class I, class L>
00088 inline size_t FunctionSerialization<ExplicitFunction<T, I, L> >::indexSequenceSize
00089 (
00090 const ExplicitFunction<T, I, L> & src
00091 ) {
00092 return src.dimension() +1;
00093 }
00094
00095 template<class T, class I, class L>
00096 inline size_t FunctionSerialization<ExplicitFunction<T, I, L> >::valueSequenceSize
00097 (
00098 const ExplicitFunction<T, I, L> & src
00099 ) {
00100 return src.size();
00101 }
00102
00103 template<class T, class I, class L>
00104 template<class INDEX_OUTPUT_ITERATOR, class VALUE_OUTPUT_ITERATOR >
00105 void FunctionSerialization< ExplicitFunction<T, I, L> >::serialize
00106 (
00107 const ExplicitFunction<T, I, L> & src,
00108 INDEX_OUTPUT_ITERATOR indexOutIterator,
00109 VALUE_OUTPUT_ITERATOR valueOutIterator
00110 ) {
00111 if(src.dimension()==0) {
00112 *indexOutIterator=0;
00113 *valueOutIterator=src(0);
00114 }
00115 else{
00116 *indexOutIterator=src.dimension();
00117 ++indexOutIterator;
00118 for(size_t i=0;i<src.dimension();++i) {
00119 *indexOutIterator=src.shape(i);
00120 ++indexOutIterator;
00121 }
00122 for(size_t i=0;i<src.size();++i) {
00123 *valueOutIterator=src(i);
00124 ++valueOutIterator;
00125 }
00126 }
00127 }
00128
00129 template<class T, class I, class L>
00130 template<class INDEX_INPUT_ITERATOR, class VALUE_INPUT_ITERATOR >
00131 void FunctionSerialization<ExplicitFunction<T, I, L> >::deserialize
00132 (
00133 INDEX_INPUT_ITERATOR indexOutIterator,
00134 VALUE_INPUT_ITERATOR valueOutIterator,
00135 ExplicitFunction<T, I, L> & dst
00136 ) {
00137 if(*indexOutIterator==0) {
00138 dst.assign();
00139 dst=ExplicitFunction<T, I, L>(*valueOutIterator);
00140 }
00141 else{
00142 const size_t dim=*indexOutIterator;
00143 std::vector<size_t> shape(dim);
00144 ++indexOutIterator;
00145 for(size_t i=0;i<dim;++i) {
00146 shape[i]=*indexOutIterator;
00147 ++indexOutIterator;
00148 }
00149 dst.assign();
00150 dst.resize(shape.begin(), shape.end() );
00151 for(size_t i=0;i<dst.size();++i) {
00152 dst(i)=*valueOutIterator;
00153 ++valueOutIterator;
00154 }
00155 }
00156 }
00157
00158 }
00159
00160 #endif // OPENGM_EXPLICIT_FUNCTION_HXX