utilities2.hxx

Go to the documentation of this file.
00001 /*
00002  * smallobjects.h
00003  *
00004  *  Created on: Jul 28, 2010
00005  *      Author: bsavchyn
00006  */
00007 
00008 #ifndef UTILITIES2_HXX_
00009 #define UTILITIES2_HXX_
00010 #include <functional>
00011 #include <numeric>
00012 #include <string>
00013 #include <stdexcept>
00014 #include <cmath>
00015 #ifdef TRWS_DEBUG_OUTPUT
00016 #include "output_debug_utils.hxx"
00017 #endif
00018 
00019 namespace trws_base{
00020 
00021 template<class GM>
00022 class VariableToFactorMapping
00023 {
00024 public:
00025    typedef typename GM::IndexType IndexType;
00026    static const IndexType InvalidIndex;
00027    VariableToFactorMapping(const GM& gm);
00028    IndexType operator() (IndexType var)const{OPENGM_ASSERT(var < _mapping.size()); return _mapping[var];}
00029    bool EachVariableHasUnaryFactor()const{return (_numberOfUnaryFactors==_mapping.size());}
00030    IndexType numberOfUnaryFactors()const{return _numberOfUnaryFactors;}
00031 private:
00032    std::vector<IndexType> _mapping;
00033    IndexType _numberOfUnaryFactors;
00034 };
00035 
00036 template <class GM>
00037 const typename VariableToFactorMapping<GM>::IndexType VariableToFactorMapping<GM>::InvalidIndex=std::numeric_limits<IndexType>::max();
00038 
00039 template<class GM>
00040 VariableToFactorMapping<GM>::VariableToFactorMapping(const GM& gm):
00041 _mapping(gm.numberOfVariables(),InvalidIndex)//invalid index
00042 {
00043    for (IndexType i=0;i<gm.numberOfFactors();++i)
00044       if (gm[i].numberOfVariables() == 1)
00045       { if (_mapping[gm[i].variableIndex(0)]==InvalidIndex)
00046          _mapping[gm[i].variableIndex(0)]=i;
00047         else std::runtime_error("VariableToFactorMapping::VariableToFactorMapping() : duplicate unary factor!");
00048       }
00049 
00050    _numberOfUnaryFactors=std::count_if(_mapping.begin(),_mapping.end(),std::bind2nd(std::not_equal_to<IndexType>(),InvalidIndex));
00051 }
00052 
00053 template<class FACTOR>
00054 class FactorWrapper
00055 {
00056 public:
00057    typedef typename FACTOR::ValueType ValueType;
00058    typedef typename FACTOR::LabelType LabelType;
00059    FactorWrapper(const FACTOR& f):_f(f){};
00060    ValueType operator () (LabelType l1, LabelType l2)const
00061     {LabelType lab[]={l1,l2}; return _f(lab);}
00062 private:
00063    const FACTOR& _f;
00064 };
00065 
00066 
00067 template < class InputIterator, class UnaryOperator >
00068 InputIterator transform_inplace ( InputIterator first, InputIterator last, UnaryOperator op )
00069 {
00070   while (first != last)
00071   {
00072     *first= op(*first);
00073     ++first;
00074   }
00075 
00076   return first;
00077 }
00078 
00079 inline void exception_check (bool condition,const std::string& str)
00080 {
00081  if (!condition) throw std::runtime_error(str);
00082 }
00083 
00084 template < class Matrix,class OutputIterator, class Pseudo2DArray>
00085 OutputIterator copy_transpose( const Matrix* src,size_t totalsize,OutputIterator outBegin,size_t rowlength, Pseudo2DArray& arr2d)
00086 {
00087    exception_check(totalsize%rowlength == 0,"copy_transpose(): totalsize%rowlength != 0");
00088 
00089    arr2d.resize(rowlength,totalsize/rowlength);
00090    for (size_t i=0;i<rowlength;++i)
00091       outBegin=std::copy(arr2d.beginSrc(src,i),arr2d.endSrc(src,i),outBegin);
00092 
00093    return outBegin;
00094 }
00095 
00096 
00097 template <class T> struct plus2ndMul : std::binary_function <T,T,T> {
00098    plus2ndMul(T mul):_mul(mul){};
00099   T operator() (T x, T y) const
00100     {return x+y*_mul;}
00101 private:
00102   T _mul;
00103 };
00104 
00105 template <class T> struct mul2ndPlus : std::binary_function <T,T,T> {
00106    mul2ndPlus(T add):_add(add){};
00107   T operator() (T x, T y) const
00108     {return x*(y+_add);}
00109 private:
00110   T _add;
00111 };
00112 
00113 template <class T> struct mulAndExp : std::unary_function <T,T> {
00114    mulAndExp(T mul):_mul(mul){};
00115   T operator() (T x) const
00116     {return ::exp(_mul*x);}
00117 private:
00118   T _mul;
00119 };
00120 
00121 
00122 template <class T> struct make0ifless : std::unary_function <T,T> {
00123    make0ifless(T threshold):_threshold(threshold){};
00124   T operator() (T x) const
00125     {return (x < _threshold ? 0.0 : x);}
00126 private:
00127   T _threshold;
00128 };
00129 
00130 template <class T> struct minusminus : std::binary_function <T,T,T> {
00131   T operator() (const T& x, const T& y) const
00132     {return y-x;}
00133 };
00134 
00135 template <class T> struct plusplusConst : std::binary_function <T,T,T>
00136 {
00137    plusplusConst(const T& constant):_constant(constant) {};
00138    T operator() (const T& x, const T& y) const
00139     {return x+y+_constant;}
00140 private:
00141    T _constant;
00142 };
00143 
00144 
00145 template <class T> struct maximum : std::binary_function <T,T,T> {
00146   T operator() (const T& x, const T& y) const
00147     {return std::max(x,y);}
00148 };
00149 
00150 
00151  template<class T>
00152  class srcIterator
00153  {
00154    public:
00155     typedef T value_type;
00156     typedef std::forward_iterator_tag iterator_category;
00157     typedef void difference_type;
00158     typedef T* pointer;
00159     typedef T& reference;
00160     srcIterator():_pbin(0),_pindex(0){};
00161     srcIterator(const srcIterator<T>& it):_pbin(it._pbin),_pindex(it._pindex){};
00162     srcIterator(T* pbin, size_t* pindex):_pbin(pbin),_pindex(pindex){};
00163     //T& operator * (){return (*_pbin)[*_pindex];}
00164     T& operator * (){return *(_pbin+(*_pindex));}
00165     srcIterator& operator ++(){++_pindex; return *this;}
00166     srcIterator operator ++(int){srcIterator it(*this);++_pindex; return it;}
00167     bool operator != (const srcIterator& it)const{return ((it._pbin!=_pbin)||(it._pindex!=_pindex));};
00168     bool operator == (const srcIterator& it)const{return !(*this!=it);}
00169     srcIterator& operator +=(size_t offset){_pindex+=offset; return (*this);}
00170    private:
00171     T* _pbin;
00172     size_t* _pindex;
00173  };
00174 
00175  template<class T>
00176  srcIterator<T> operator + (const srcIterator<T>& it,size_t offset){srcIterator<T> result=it; return (result+=offset);}
00177 
00178  template<class T>
00179  class Pseudo2DArray
00180  {
00181  public:
00182     typedef srcIterator<const T> const_srciterator;
00183     typedef const T* const_trgiterator;
00184     typedef srcIterator<T> srciterator;
00185     typedef T* trgiterator;
00186 
00187     Pseudo2DArray(size_t srcsize=0,size_t trgsize=0):_srcsize(srcsize),_trgsize(trgsize){_setupindex();};
00188 
00189    inline void resize(size_t srcsize,size_t trgsize);
00190 
00191    const_srciterator beginSrc(const T* pbin,size_t src) {assert(src<_srcsize); return const_srciterator(pbin,&_index[src*_trgsize]);};
00192    const_srciterator endSrc(const T* pbin,size_t src){return beginSrc(pbin,src)+_trgsize;};
00193 
00194    const_trgiterator beginTrg(const T* pbin,size_t trg){assert(trg<_trgsize); return pbin+trg*_srcsize;};
00195    const_trgiterator endTrg(const T* pbin,size_t trg){return beginTrg(pbin,trg)+_srcsize;}
00196 
00197    srciterator beginSrcNC(T* pbin,size_t src) {assert(src<_srcsize); return srciterator(pbin,&_index[src*_trgsize]);};
00198    srciterator endSrcNC(T* pbin,size_t src){return beginSrcNC(pbin,src)+_trgsize;};
00199 
00200    trgiterator beginTrgNC(T* pbin,size_t trg){assert(trg<_trgsize); return pbin+trg*_srcsize;};
00201    trgiterator endTrgNC(T* pbin,size_t trg){return beginTrgNC(pbin,trg)+_srcsize;}
00202 
00203  private:
00204    void _setupindex();
00205    size_t _srcsize;
00206    size_t _trgsize;
00207    std::vector<size_t> _index;
00208  };
00209 
00210  template<class T>
00211  void Pseudo2DArray<T>::resize(size_t srcsize,size_t trgsize)
00212  {
00213   if ((srcsize==_srcsize) && (trgsize==_trgsize))
00214     return;
00215 
00216    _srcsize=srcsize;
00217    _trgsize=trgsize;
00218    _setupindex();
00219  };
00220 
00221  template<class T>
00222  void Pseudo2DArray<T>::_setupindex()
00223  {
00224   _index.assign(_srcsize*_trgsize,0);
00225   if (_index.empty())
00226       return;
00227   size_t* _pindex=&_index[0];
00228   for (size_t src=0;src<_srcsize;++src)
00229       for (size_t trg=0;trg<_trgsize;++trg)
00230        *(_pindex++)=trg*_srcsize+src;
00231  };
00232 
00233 
00234  template<class Object>
00235  void DeallocatePointer(Object* p)
00236  {
00237    if (p!=0) delete p;
00238  }
00239 
00240 
00241 };
00242 
00243 
00244 #endif /* SMALLOBJECTS_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Mon Jun 17 16:31:07 2013 for OpenGM by  doxygen 1.6.3