functions/sparsemarray.hxx

Go to the documentation of this file.
00001 #pragma once
00002 #ifndef OPENGM_SPARSE_FUNCTION
00003 #define OPENGM_SPARSE_FUNCTION
00004 
00005 #include "opengm/datastructures/sparsemarray/sparsemarray.hxx"
00006 #include "opengm/functions/function_registration.hxx"
00007 #include "opengm/functions/function_properties_base.hxx"
00008 
00009 namespace opengm {
00010 
00013 template<class T,class I,class L,class C>
00014 struct FunctionRegistration<opengm::SparseFunction<T,I,L,C> > {
00015    enum ID { Id = opengm::FUNCTION_TYPE_ID_OFFSET + 1 };
00016 };
00017 
00021 template<class T,class I,class L,class C>
00022 class FunctionSerialization< opengm::SparseFunction<T,I,L,C> > {
00023 public:
00024    typedef typename opengm::SparseFunction<T,I,L,C>::ValueType ValueType;
00025 
00026    static size_t indexSequenceSize(const opengm::SparseFunction<T,I,L,C>&);
00027    static size_t valueSequenceSize(const opengm::SparseFunction<T,I,L,C>&);
00028    template<class INDEX_OUTPUT_ITERATOR,class VALUE_OUTPUT_ITERATOR >
00029       static void serialize(const opengm::SparseFunction<T,I,L,C>&, INDEX_OUTPUT_ITERATOR,VALUE_OUTPUT_ITERATOR );
00030    template<class INDEX_INPUT_ITERATOR ,class VALUE_INPUT_ITERATOR>
00031       static void deserialize( INDEX_INPUT_ITERATOR,VALUE_INPUT_ITERATOR, opengm::SparseFunction<T,I,L,C>&);
00032 };
00034 
00035 template<class T,class I,class L,class C>
00036 inline size_t
00037 FunctionSerialization<opengm::SparseFunction<T,I,L,C> >::indexSequenceSize
00038 (
00039    const opengm::SparseFunction<T,I,L,C>& src
00040 ) {
00041    return 1 + src.dimension()+1+src.container().size();
00042 }
00043 
00044 template<class T,class I,class L,class C>
00045 inline size_t
00046 FunctionSerialization<opengm::SparseFunction<T,I,L,C> >::valueSequenceSize
00047 (
00048    const opengm::SparseFunction<T,I,L,C>& src
00049 ) {
00050 
00051    return src.container().size()+1;
00052 }
00053 
00054 template<class T,class I,class L,class C>
00055 template<class INDEX_OUTPUT_ITERATOR,class VALUE_OUTPUT_ITERATOR >
00056 void
00057 FunctionSerialization<opengm::SparseFunction<T,I,L,C> >::serialize
00058 (
00059    const opengm::SparseFunction<T,I,L,C>& src,
00060    INDEX_OUTPUT_ITERATOR indexOutIterator,
00061    VALUE_OUTPUT_ITERATOR valueOutIterator
00062 ) {
00063    *indexOutIterator=src.dimension();
00064    ++indexOutIterator;
00065    for(size_t i=0;i<src.dimension();++i) {
00066       *indexOutIterator=src.shape(i);
00067       ++indexOutIterator;
00068    }
00069    //save the default value
00070    *valueOutIterator=src.defaultValue();
00071    ++valueOutIterator;
00072    //save how many not default values are in the SparseFunction
00073    *indexOutIterator=src.container().size();
00074    ++indexOutIterator;
00075    typedef typename opengm::SparseFunction<T,I,L,C>::ContainerType::const_iterator IterType;
00076    IterType srcIter=src.container().begin();
00077    for(size_t i=0;i<src.container().size();++i) {
00078       *indexOutIterator=srcIter->first;
00079       *valueOutIterator=srcIter->second;
00080       ++valueOutIterator;
00081       ++indexOutIterator;
00082       ++srcIter;
00083    }
00084 }
00085 
00086 template<class T,class I,class L,class C>
00087 template<class INDEX_INPUT_ITERATOR,class VALUE_INPUT_ITERATOR >
00088 void
00089 FunctionSerialization<opengm::SparseFunction<T,I,L,C> >::deserialize
00090 (
00091    INDEX_INPUT_ITERATOR indexOutIterator,
00092    VALUE_INPUT_ITERATOR valueOutIterator,
00093    opengm::SparseFunction<T,I,L,C>& dst
00094 ) {
00095 
00096    const size_t dim=*indexOutIterator;
00097    std::vector<size_t> shape(dim);
00098    ++indexOutIterator;
00099    for(size_t i=0;i<dim;++i) {
00100       shape[i]=*indexOutIterator;
00101       ++indexOutIterator;
00102    }
00103    dst = opengm::SparseFunction<T,I,L,C>(shape.begin(),shape.end(),*valueOutIterator);
00104    ++valueOutIterator;
00105    const size_t nNonDefault=*indexOutIterator;
00106    ++indexOutIterator;
00107    typedef typename opengm::SparseFunction<T,I,L,C>::KeyValPairType KeyValPairType;
00108    for(size_t i=0;i<nNonDefault;++i) {
00109 
00110       dst.container().insert(KeyValPairType(*indexOutIterator,*valueOutIterator));
00111       ++valueOutIterator;
00112       ++indexOutIterator;
00113    }
00114    
00115 }
00116 
00117 } // namepsace opengm
00118 
00119 #endif // OPENGM_SPARSE_FUNCTION
00120 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Mon Jun 17 16:31:06 2013 for OpenGM by  doxygen 1.6.3