-=, - operators in expressions
authormarci
Thu, 27 Jan 2005 17:44:04 +0000
changeset 109991a8ee9d088d
parent 1098 e3b3667c6857
child 1100 299d1127a846
-=, - operators in expressions
src/work/marci/lp/expression.h
src/work/marci/lp/lp_solver_wrapper_3.h
     1.1 --- a/src/work/marci/lp/expression.h	Thu Jan 27 16:11:54 2005 +0000
     1.2 +++ b/src/work/marci/lp/expression.h	Thu Jan 27 17:44:04 2005 +0000
     1.3 @@ -19,11 +19,18 @@
     1.4  
     1.5    template <typename _Col, typename _Value>
     1.6    class Expr {
     1.7 -  protected:
     1.8 +//  protected:
     1.9 +  public:
    1.10      typedef 
    1.11      typename std::map<_Col, _Value> Data; 
    1.12      Data data;
    1.13    public:
    1.14 +    void simplify() {
    1.15 +      for (typename Data::iterator i=data.begin(); 
    1.16 +	   i!=data.end(); ++i) {
    1.17 +	if ((*i).second==0) data.erase(i);
    1.18 +      }
    1.19 +    }
    1.20      Expr() { }
    1.21      Expr(_Col _col) { 
    1.22        data.insert(std::make_pair(_col, 1));
    1.23 @@ -33,6 +40,7 @@
    1.24  	   i!=data.end(); ++i) {
    1.25  	(*i).second *= _value;
    1.26        }
    1.27 +      simplify();
    1.28        return *this;
    1.29      }
    1.30      Expr& operator+=(const Expr<_Col, _Value>& expr) {
    1.31 @@ -45,6 +53,20 @@
    1.32  	  (*i).second+=(*j).second;
    1.33  	}
    1.34        }
    1.35 +      simplify();
    1.36 +      return *this;
    1.37 +    }
    1.38 +    Expr& operator-=(const Expr<_Col, _Value>& expr) {
    1.39 +      for (typename Data::const_iterator j=expr.data.begin(); 
    1.40 +	   j!=expr.data.end(); ++j) {
    1.41 +	typename Data::iterator i=data.find((*j).first);
    1.42 +	if (i==data.end()) {
    1.43 +	  data.insert(std::make_pair((*j).first, -(*j).second));
    1.44 +	} else {
    1.45 +	  (*i).second+=-(*j).second;
    1.46 +	}
    1.47 +      }
    1.48 +      simplify();
    1.49        return *this;
    1.50      }
    1.51      template <typename _C, typename _V> 
    1.52 @@ -56,6 +78,7 @@
    1.53    Expr<_Col, _Value> operator*(_Value _value, _Col _col) {
    1.54      Expr<_Col, _Value> tmp(_col);
    1.55      tmp*=_value;
    1.56 +    tmp.simplify();
    1.57      return tmp;
    1.58    }
    1.59  
    1.60 @@ -64,6 +87,7 @@
    1.61  			       const Expr<_Col, _Value>& expr) {
    1.62      Expr<_Col, _Value> tmp(expr);
    1.63      tmp*=_value;
    1.64 +    tmp.simplify();
    1.65      return tmp;
    1.66    }
    1.67  
    1.68 @@ -72,6 +96,16 @@
    1.69  			       const Expr<_Col, _Value>& expr2) {
    1.70      Expr<_Col, _Value> tmp(expr1);
    1.71      tmp+=expr2;
    1.72 +    tmp.simplify();
    1.73 +    return tmp;
    1.74 +  }
    1.75 +
    1.76 +  template <typename _Col, typename _Value>
    1.77 +  Expr<_Col, _Value> operator-(const Expr<_Col, _Value>& expr1, 
    1.78 +			       const Expr<_Col, _Value>& expr2) {
    1.79 +    Expr<_Col, _Value> tmp(expr1);
    1.80 +    tmp-=expr2;
    1.81 +    tmp.simplify();
    1.82      return tmp;
    1.83    }
    1.84  
     2.1 --- a/src/work/marci/lp/lp_solver_wrapper_3.h	Thu Jan 27 16:11:54 2005 +0000
     2.2 +++ b/src/work/marci/lp/lp_solver_wrapper_3.h	Thu Jan 27 17:44:04 2005 +0000
     2.3 @@ -27,6 +27,7 @@
     2.4  //#include <lemon/list_graph.h>
     2.5  //#include <lemon/graph_wrapper.h>
     2.6  #include <lemon/invalid.h>
     2.7 +#include <expression.h>
     2.8  //#include <bfs_dfs.h>
     2.9  //#include <stp.h>
    2.10  //#include <lemon/max_flow.h>
    2.11 @@ -143,7 +144,18 @@
    2.12        ClassIt(const int& _i) : i(_i) { }
    2.13        /// Invalid constructor.
    2.14        ClassIt(const Invalid&) : i(-1) { }
    2.15 +      friend bool operator<(const ClassIt& x, const ClassIt& y);
    2.16 +      friend std::ostream& operator<<(std::ostream& os, 
    2.17 +				      const ClassIt& it);
    2.18      };
    2.19 +    friend bool operator<(const ClassIt& x, const ClassIt& y) {
    2.20 +      return (x.i < y.i);
    2.21 +    }
    2.22 +    friend std::ostream& operator<<(std::ostream& os, 
    2.23 +				    const ClassIt& it) {
    2.24 +      os << it.i;
    2.25 +      return os;
    2.26 +    }
    2.27      /// First member of class \c class_id.
    2.28      ClassIt& first(ClassIt& it, int class_id) const {
    2.29        it.i=tips[class_id].first;
    2.30 @@ -158,104 +170,6 @@
    2.31      bool valid(const ClassIt& it) const { return it.i!=-1; }
    2.32    };
    2.33  
    2.34 -  template <typename _Col, typename _Value>
    2.35 -  class Expr;
    2.36 -
    2.37 -  template <typename _Col, typename _Value>
    2.38 -  class SmallExpr {
    2.39 -    template <typename _C, typename _V> 
    2.40 -    friend class Expr;
    2.41 -  protected:
    2.42 -    _Col col;
    2.43 -    _Value value;
    2.44 -  public:
    2.45 -    SmallExpr(_Col _col) : col(_col), value(1) { 
    2.46 -    }
    2.47 -    SmallExpr& operator *= (_Value _value) {
    2.48 -      value*=_value;
    2.49 -      return *this;
    2.50 -    }
    2.51 -    //    template <typename _C, typename _V>
    2.52 -    //    friend SmallExpr<_C, _V> operator* (_V _value, 
    2.53 -    //					const SmallExpr<_C, _V>& expr);
    2.54 -    template <typename _C, typename _V>
    2.55 -    friend std::ostream& operator<<(std::ostream& os, 
    2.56 -				    const SmallExpr<_C, _V>& expr);
    2.57 -  };
    2.58 -
    2.59 -  template <typename _Col, typename _Value>
    2.60 -  SmallExpr<_Col, _Value> 
    2.61 -  operator* (_Value value, 
    2.62 -	     const SmallExpr<_Col, _Value>& expr) {
    2.63 -    SmallExpr<_Col, _Value> tmp;
    2.64 -    tmp=expr;
    2.65 -    tmp*=value;
    2.66 -    return tmp;
    2.67 -  }
    2.68 -
    2.69 -  template <typename _Col, typename _Value>
    2.70 -  std::ostream& operator<<(std::ostream& os, 
    2.71 -			   const SmallExpr<_Col, _Value>& expr) {
    2.72 -    os << expr.value << "*" << expr.col;
    2.73 -    return os;
    2.74 -  }
    2.75 -
    2.76 -  template <typename _Col, typename _Value>
    2.77 -  class Expr {
    2.78 -  protected:
    2.79 -    typedef 
    2.80 -    typename std::map<_Col, _Value> Data; 
    2.81 -    Data data;
    2.82 -  public:
    2.83 -    Expr() { }
    2.84 -    Expr(SmallExpr<_Col, _Value> expr) { 
    2.85 -      data.insert(std::make_pair(expr.col, expr.value));
    2.86 -    }
    2.87 -//     Expr(_Col col) { 
    2.88 -//       data.insert(std::make_pair(col, 1));
    2.89 -//     }
    2.90 -    Expr& operator*=(_Value _value) {
    2.91 -      for (typename Data::iterator i=data.begin(); 
    2.92 -	   i!=data.end(); ++i) {
    2.93 -	(*i).second *= _value;
    2.94 -      }
    2.95 -      return *this;
    2.96 -    }
    2.97 -    Expr& operator+=(SmallExpr<_Col, _Value> expr) {
    2.98 -      typename Data::iterator i=data.find(expr.col);
    2.99 -      if (i==data.end()) {
   2.100 -	data.insert(std::make_pair(expr.col, expr.value));
   2.101 -      } else {
   2.102 -	(*i).second+=expr.value;
   2.103 -      }
   2.104 -      return *this;
   2.105 -    }
   2.106 -    //    template <typename _C, typename _V> 
   2.107 -    //    friend Expr<_C, _V> operator*(_V _value, const Expr<_C, _V>& expr);
   2.108 -    template <typename _C, typename _V> 
   2.109 -    friend std::ostream& operator<<(std::ostream& os, 
   2.110 -				    const Expr<_C, _V>& expr);
   2.111 -  };
   2.112 -
   2.113 -  template <typename _Col, typename _Value>
   2.114 -  Expr<_Col, _Value> operator*(_Value _value, 
   2.115 -			       const Expr<_Col, _Value>& expr) {
   2.116 -    Expr<_Col, _Value> tmp;
   2.117 -    tmp=expr;
   2.118 -    tmp*=_value;
   2.119 -    return tmp;
   2.120 -  }
   2.121 -
   2.122 -  template <typename _Col, typename _Value>
   2.123 -  std::ostream& operator<<(std::ostream& os, 
   2.124 -			   const Expr<_Col, _Value>& expr) {
   2.125 -    for (typename Expr<_Col, _Value>::Data::const_iterator i=
   2.126 -	   expr.data.begin(); 
   2.127 -	 i!=expr.data.end(); ++i) {
   2.128 -      os << (*i).second << "*" << (*i).first << " ";
   2.129 -    }
   2.130 -    return os;
   2.131 -  }
   2.132  
   2.133    /*! \e
   2.134     */
   2.135 @@ -424,6 +338,39 @@
   2.136        return _getObjCoef(col_iter_map[col_it]);
   2.137      }
   2.138  
   2.139 +    //MOST HIGH LEVEL, USER FRIEND FUNCTIONS
   2.140 +
   2.141 +    /// \e
   2.142 +    typedef Expr<ColIt, _Value> Expression;
   2.143 +    /// \e
   2.144 +    typedef Expr<RowIt, _Value> DualExpression;
   2.145 +    /// \e
   2.146 +    void setRowCoeffs(RowIt row_it, const Expression& expr) {
   2.147 +      std::vector<std::pair<int, _Value> > row_coeffs;
   2.148 +      for(typename Expression::Data::const_iterator i=expr.data.begin(); 
   2.149 +	  i!=expr.data.end(); ++i) {
   2.150 +	row_coeffs.push_back(std::make_pair
   2.151 +			     (col_iter_map[(*i).first], (*i).second));
   2.152 +      }
   2.153 +      _setRowCoeffs(row_iter_map[row_it], row_coeffs);
   2.154 +    }
   2.155 +    /// \e
   2.156 +    void setColCoeffs(ColIt col_it, const DualExpression& expr) {
   2.157 +      std::vector<std::pair<int, _Value> > col_coeffs;
   2.158 +      for(typename DualExpression::Data::const_iterator i=expr.data.begin(); 
   2.159 +	  i!=expr.data.end(); ++i) {
   2.160 +	col_coeffs.push_back(std::make_pair
   2.161 +			     (row_iter_map[(*i).first], (*i).second));
   2.162 +      }
   2.163 +      _setColCoeffs(col_iter_map[col_it], col_coeffs);
   2.164 +    }
   2.165 +    /// \e
   2.166 +    void setObjCoeffs(const Expression& expr) {
   2.167 +      for(typename Expression::Data::const_iterator i=expr.data.begin(); 
   2.168 +	  i!=expr.data.end(); ++i) {
   2.169 +	setObjCoef((*i).first, (*i).second);
   2.170 +      }
   2.171 +    }
   2.172      //SOLVER FUNCTIONS
   2.173  
   2.174      /// \e