function_properties.hxx
Go to the documentation of this file.00001 #pragma once
00002 #ifndef OPENGM_FUNCTION_PROPERTIES_HXX
00003 #define OPENGM_FUNCTION_PROPERTIES_HXX
00004
00005 #include <cmath>
00006
00007 #include "opengm/opengm.hxx"
00008 #include "opengm/utilities/shape_accessor.hxx"
00009 #include "opengm/utilities/accessor_iterator.hxx"
00010 #include "opengm/utilities/accumulation.hxx"
00011 #include "opengm/utilities/indexing.hxx"
00012 #include "opengm/utilities/functors.hxx"
00013 #include "opengm/operations/adder.hxx"
00014 #include "opengm/operations/and.hxx"
00015 #include "opengm/operations/or.hxx"
00016 #include "opengm/operations/minimizer.hxx"
00017 #include "opengm/operations/maximizer.hxx"
00018 #include "opengm/operations/adder.hxx"
00019 #include "opengm/operations/integrator.hxx"
00020 #include "opengm/operations/multiplier.hxx"
00021
00022 #define OPENGM_FUNCTION_TYPEDEF_MACRO typedef typename FunctionType::ValueType ValueType;\
00023 typedef typename FunctionType::IndexType IndexType;\
00024 typedef typename FunctionType::LabelType LabelType;\
00025 typedef typename FunctionType::FunctionShapeIteratorType FunctionShapeIteratorType
00026
00027 namespace opengm {
00028
00029 struct BinaryProperties{
00030 enum Values{
00031 IsPotts=0,
00032 IsSubmodular1=1,
00033 IsPositive=2
00034 };
00035 };
00036
00037 struct ValueProperties{
00038 enum Values{
00039 Sum=0,
00040 Product=1,
00041 Minimum=2,
00042 Maximum=3
00043 };
00044 };
00045
00046 template<int PROPERTY_ID,class FUNCTION>
00047 class BinaryFunctionProperties;
00048
00049 template<int PROPERTY_ID,class FUNCTION>
00050 class ValueFunctionProperties;
00051
00052 namespace detail_properties{
00053 template<class FUNCTION,class ACCUMULATOR>
00054 class AllValuesInAnyOrderFunctionProperties;
00055 template<class FUNCTION,class ACCUMULATOR>
00056 class AtLeastAllUniqueValuesFunctionProperties;
00057 }
00058
00059
00060 template<class FUNCTION>
00061 class BinaryFunctionProperties<BinaryProperties::IsPotts, FUNCTION> {
00062 typedef FUNCTION FunctionType;
00063 OPENGM_FUNCTION_TYPEDEF_MACRO;
00064 public:
00065 static bool op(const FunctionType & f) {
00066 ShapeWalker<FunctionShapeIteratorType> shapeWalker(f.functionShapeBegin(), f.dimension());
00067 ValueType vEqual = f(shapeWalker.coordinateTuple().begin());
00068 ++shapeWalker;
00069 ValueType vNotEqual = f(shapeWalker.coordinateTuple().begin());
00070 ++shapeWalker;
00071 for (IndexType i = 2; i < f.size(); ++i, ++shapeWalker) {
00072
00073 if (isEqualValueVector(shapeWalker.coordinateTuple())) {
00074 if (vEqual != f(shapeWalker.coordinateTuple().begin()))
00075 return false;
00076 }
00077 else {
00078 if (vNotEqual != f(shapeWalker.coordinateTuple().begin()))
00079 return false;
00080 }
00081 }
00082 return true;
00083 }
00084 };
00085
00086
00087
00088
00089
00090
00091 template<class FUNCTION>
00092 class ValueFunctionProperties<ValueProperties::Product, FUNCTION>
00093 : public detail_properties::AllValuesInAnyOrderFunctionProperties<FUNCTION,Multiplier>{
00094 };
00095
00096 template<class FUNCTION>
00097 class ValueFunctionProperties<ValueProperties::Sum, FUNCTION>
00098 : public detail_properties::AllValuesInAnyOrderFunctionProperties<FUNCTION,Adder>{
00099 };
00100
00101 template<class FUNCTION>
00102 class ValueFunctionProperties<ValueProperties::Minimum, FUNCTION>
00103 : public detail_properties::AtLeastAllUniqueValuesFunctionProperties<FUNCTION,Minimizer>{
00104 };
00105
00106 template<class FUNCTION>
00107 class ValueFunctionProperties<ValueProperties::Maximum, FUNCTION>
00108 : public detail_properties::AtLeastAllUniqueValuesFunctionProperties<FUNCTION,Maximizer>{
00109 };
00110
00111
00112 namespace detail_properties{
00113 template<class FUNCTION,class ACCUMULATOR>
00114 class AllValuesInAnyOrderFunctionProperties{
00115 typedef FUNCTION FunctionType;
00116 OPENGM_FUNCTION_TYPEDEF_MACRO;
00117 public:
00118 static ValueType op(const FunctionType & f) {
00119 opengm::AccumulationFunctor<ACCUMULATOR,ValueType> functor;
00120 f.forAllValuesInAnyOrder(functor);
00121 return functor.value();
00122 }
00123 };
00124 template<class FUNCTION,class ACCUMULATOR>
00125 class AtLeastAllUniqueValuesFunctionProperties{
00126 typedef FUNCTION FunctionType;
00127 OPENGM_FUNCTION_TYPEDEF_MACRO;
00128 public:
00129 static ValueType op(const FunctionType & f) {
00130 opengm::AccumulationFunctor<ACCUMULATOR,ValueType> functor;
00131 f.forAllValuesInAnyOrder(functor);
00132 return functor.value();
00133 }
00134 };
00135 }
00136
00137 }
00138
00139 #endif //OPENGM_FUNCTION_PROPERTIES_HXX