src/work/marci/lp/expression.h
changeset 1097 c91e765266d7
child 1099 91a8ee9d088d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/marci/lp/expression.h	Wed Jan 26 15:54:06 2005 +0000
     1.3 @@ -0,0 +1,91 @@
     1.4 +// -*- c++ -*-
     1.5 +#ifndef LEMON_EXPRESSION_H
     1.6 +#define LEMON_EXPRESSION_H
     1.7 +
     1.8 +#include <iostream>
     1.9 +#include <map>
    1.10 +
    1.11 +namespace lemon {
    1.12 +
    1.13 +  /*! \brief Linear expression
    1.14 +
    1.15 +    \c Expr<_Col,_Value> implements a class of linear expressions with the 
    1.16 +    operations of addition and multiplication with scalar. 
    1.17 +
    1.18 +    \author Marton Makai
    1.19 +   */
    1.20 +  template <typename _Col, typename _Value>
    1.21 +  class Expr;
    1.22 +
    1.23 +  template <typename _Col, typename _Value>
    1.24 +  class Expr {
    1.25 +  protected:
    1.26 +    typedef 
    1.27 +    typename std::map<_Col, _Value> Data; 
    1.28 +    Data data;
    1.29 +  public:
    1.30 +    Expr() { }
    1.31 +    Expr(_Col _col) { 
    1.32 +      data.insert(std::make_pair(_col, 1));
    1.33 +    }
    1.34 +    Expr& operator*=(_Value _value) {
    1.35 +      for (typename Data::iterator i=data.begin(); 
    1.36 +	   i!=data.end(); ++i) {
    1.37 +	(*i).second *= _value;
    1.38 +      }
    1.39 +      return *this;
    1.40 +    }
    1.41 +    Expr& operator+=(const Expr<_Col, _Value>& expr) {
    1.42 +      for (typename Data::const_iterator j=expr.data.begin(); 
    1.43 +	   j!=expr.data.end(); ++j) {
    1.44 +	typename Data::iterator i=data.find((*j).first);
    1.45 +	if (i==data.end()) {
    1.46 +	  data.insert(std::make_pair((*j).first, (*j).second));
    1.47 +	} else {
    1.48 +	  (*i).second+=(*j).second;
    1.49 +	}
    1.50 +      }
    1.51 +      return *this;
    1.52 +    }
    1.53 +    template <typename _C, typename _V> 
    1.54 +    friend std::ostream& operator<<(std::ostream& os, 
    1.55 +				    const Expr<_C, _V>& expr);
    1.56 +  };
    1.57 +
    1.58 +  template <typename _Col, typename _Value>
    1.59 +  Expr<_Col, _Value> operator*(_Value _value, _Col _col) {
    1.60 +    Expr<_Col, _Value> tmp(_col);
    1.61 +    tmp*=_value;
    1.62 +    return tmp;
    1.63 +  }
    1.64 +
    1.65 +  template <typename _Col, typename _Value>
    1.66 +  Expr<_Col, _Value> operator*(_Value _value, 
    1.67 +			       const Expr<_Col, _Value>& expr) {
    1.68 +    Expr<_Col, _Value> tmp(expr);
    1.69 +    tmp*=_value;
    1.70 +    return tmp;
    1.71 +  }
    1.72 +
    1.73 +  template <typename _Col, typename _Value>
    1.74 +  Expr<_Col, _Value> operator+(const Expr<_Col, _Value>& expr1, 
    1.75 +			       const Expr<_Col, _Value>& expr2) {
    1.76 +    Expr<_Col, _Value> tmp(expr1);
    1.77 +    tmp+=expr2;
    1.78 +    return tmp;
    1.79 +  }
    1.80 +
    1.81 +  template <typename _Col, typename _Value>
    1.82 +  std::ostream& operator<<(std::ostream& os, 
    1.83 +			   const Expr<_Col, _Value>& expr) {
    1.84 +    for (typename Expr<_Col, _Value>::Data::const_iterator i=
    1.85 +	   expr.data.begin(); 
    1.86 +	 i!=expr.data.end(); ++i) {
    1.87 +      os << (*i).second << "*" << (*i).first << " ";
    1.88 +    }
    1.89 +    return os;
    1.90 +  }
    1.91 +  
    1.92 +} //namespace lemon
    1.93 +
    1.94 +#endif //LEMON_EXPRESSION_H