view_fix_variables_function.hxx
Go to the documentation of this file.00001 #pragma once
00002 #ifndef OPENGM_VIEW_FIX_VARIABLES_FUNCTION_HXX
00003 #define OPENGM_VIEW_FIX_VARIABLES_FUNCTION_HXX
00004
00005 #include "opengm/functions/function_properties_base.hxx"
00006
00007 namespace opengm {
00008
00010 template<class I, class L>
00011 struct PositionAndLabel {
00012 PositionAndLabel(const I = 0, const L = 0);
00013 I position_;
00014 L label_;
00015 };
00017
00021 template<class GM>
00022 class ViewFixVariablesFunction
00023 : public FunctionBase<ViewFixVariablesFunction<GM>, typename GM::ValueType, typename GM::IndexType, typename GM::LabelType> {
00024 public:
00025 typedef typename GM::ValueType ValueType;
00026 typedef ValueType value_type;
00027 typedef typename GM::FactorType FactorType;
00028 typedef typename GM::OperatorType OperatorType;
00029 typedef typename GM::IndexType IndexType;
00030 typedef typename GM::LabelType LabelType;
00031
00032 ViewFixVariablesFunction();
00033 ViewFixVariablesFunction(const FactorType &, const std::vector<PositionAndLabel<IndexType, LabelType> > &);
00034 template<class POSITION_AND_TYPE_CONTAINER>
00035 ViewFixVariablesFunction(const FactorType &, const POSITION_AND_TYPE_CONTAINER &);
00036 template<class Iterator>
00037 ValueType operator()(Iterator begin)const;
00038 IndexType shape(const IndexType)const;
00039 IndexType dimension()const;
00040 IndexType size()const;
00041
00042 private:
00043 FactorType const* factor_;
00044 std::vector<PositionAndLabel<IndexType, LabelType> > positionAndLabels_;
00045 mutable std::vector<LabelType> iteratorBuffer_;
00046 mutable bool computedSize_;
00047 mutable size_t size_;
00048 std::vector<size_t> lookUp_;
00049 };
00050
00051 template<class I, class L>
00052 PositionAndLabel<I, L>::PositionAndLabel
00053 (
00054 const I position,
00055 const L label
00056 )
00057 : position_(position),
00058 label_(label)
00059 {}
00060
00061 template<class GM>
00062 inline
00063 ViewFixVariablesFunction<GM>::ViewFixVariablesFunction()
00064 : factor_(NULL),
00065 iteratorBuffer_(),
00066 computedSize_(false),
00067 size_(1)
00068 {}
00069
00070 template<class GM>
00071 inline
00072 ViewFixVariablesFunction<GM>::ViewFixVariablesFunction
00073 (
00074 const typename ViewFixVariablesFunction<GM>::FactorType& factor,
00075 const std::vector<PositionAndLabel<typename GM::IndexType, typename GM::LabelType> >& positionAndLabels
00076 )
00077 : factor_(&factor),
00078 positionAndLabels_(positionAndLabels),
00079 iteratorBuffer_(factor.numberOfVariables()),
00080 computedSize_(false),
00081 size_(1),
00082 lookUp_(factor.numberOfVariables()-positionAndLabels.size())
00083 {
00084 if(opengm::NO_DEBUG==false) {
00085 for(size_t i=0; i<positionAndLabels_.size(); ++i) {
00086 OPENGM_ASSERT(positionAndLabels_[i].label_ < factor_->numberOfLabels(positionAndLabels_[i].position_));
00087 }
00088 }
00089 for(size_t ind=0; ind<lookUp_.size(); ++ind) {
00090 size_t add=0;
00091 for(IndexType i=0; i<positionAndLabels_.size(); ++i) {
00092 if( positionAndLabels_[i].position_ <= ind+add) {
00093 ++add;
00094 }
00095 }
00096 lookUp_[ind]=ind+add;
00097 }
00098 }
00099
00104 template<class GM>
00105 template<class POSITION_AND_TYPE_CONTAINER>
00106 inline
00107 ViewFixVariablesFunction<GM>::ViewFixVariablesFunction
00108 (
00109 const typename ViewFixVariablesFunction<GM>::FactorType& factor,
00110 const POSITION_AND_TYPE_CONTAINER& positionAndLabels
00111 )
00112 : factor_(&factor),
00113 positionAndLabels_(positionAndLabels.begin(), positionAndLabels.end()),
00114 iteratorBuffer_(factor.numberOfVariables()),
00115 computedSize_(false),
00116 size_(1),
00117 lookUp_(factor.numberOfVariables()-positionAndLabels.size())
00118 {
00119 if(!(opengm::NO_DEBUG)) {
00120 for(size_t i=0; i<positionAndLabels_.size(); ++i) {
00121 OPENGM_ASSERT(positionAndLabels_[i].label_ < factor_->numberOfLabels(positionAndLabels_[i].position_));
00122 }
00123 }
00124 for(size_t ind=0; ind<lookUp_.size(); ++ind) {
00125 size_t add = 0;
00126 for(IndexType i=0; i<positionAndLabels_.size(); ++i) {
00127 if( positionAndLabels_[i].position_ <= ind+add) {
00128 ++add;
00129 }
00130 }
00131 lookUp_[ind]=ind+add;
00132 }
00133 }
00134
00135 template<class GM>
00136 template<class Iterator>
00137 inline typename ViewFixVariablesFunction<GM>::ValueType
00138 ViewFixVariablesFunction<GM>::operator()
00139 (
00140 Iterator begin
00141 ) const
00142 {
00143 OPENGM_ASSERT(factor_ != NULL);
00144 for(size_t ind=0; ind<lookUp_.size(); ++ind) {
00145 iteratorBuffer_[lookUp_[ind]]=*begin;
00146 ++begin;
00147 }
00148 for(size_t i=0; i<positionAndLabels_.size(); ++i) {
00149 iteratorBuffer_[positionAndLabels_[i].position_]
00150 = positionAndLabels_[i].label_;
00151 }
00152 return factor_->operator()(iteratorBuffer_.begin());
00153 }
00154
00155 template<class GM>
00156 inline typename ViewFixVariablesFunction<GM>::IndexType
00157 ViewFixVariablesFunction<GM>::shape
00158 (
00159 const typename ViewFixVariablesFunction<GM>::IndexType index
00160 ) const
00161 {
00162 OPENGM_ASSERT(factor_ != NULL);
00163 size_t add = 0;
00164 for(IndexType i=0; i<positionAndLabels_.size(); ++i) {
00165 if( positionAndLabels_[i].position_ <= index+add) {
00166 ++add;
00167 }
00168 }
00169 OPENGM_ASSERT(index + add < factor_->numberOfVariables());
00170 return factor_->numberOfLabels(index + add);
00171 }
00172
00173 template<class GM>
00174 inline typename ViewFixVariablesFunction<GM>::IndexType
00175 ViewFixVariablesFunction<GM>::dimension() const
00176 {
00177 OPENGM_ASSERT(factor_!=NULL);
00178 return factor_->numberOfVariables() - positionAndLabels_.size();
00179 }
00180
00181 template<class GM>
00182 inline typename ViewFixVariablesFunction<GM>::IndexType
00183 ViewFixVariablesFunction<GM>::size() const
00184 {
00185 OPENGM_ASSERT(factor_!=NULL);
00186 if(computedSize_== false) {
00187 size_ = factor_->size();
00188 for(IndexType j=0; j<positionAndLabels_.size(); ++j) {
00189 size_ /= (factor_->numberOfLabels(positionAndLabels_[j].position_));
00190 }
00191 computedSize_ = true;
00192 }
00193 return size_;
00194 }
00195
00196 }
00197
00198 #endif // #ifndef OPENGM_VIEW_FIX_VARIABLES_FUNCTION_HXX