src/work/marci/lp/expression.h
author marci
Wed, 26 Jan 2005 15:54:06 +0000
changeset 1097 c91e765266d7
child 1099 91a8ee9d088d
permissions -rw-r--r--
A proposal or test implementation for linear expression`
     1 // -*- c++ -*-
     2 #ifndef LEMON_EXPRESSION_H
     3 #define LEMON_EXPRESSION_H
     4 
     5 #include <iostream>
     6 #include <map>
     7 
     8 namespace lemon {
     9 
    10   /*! \brief Linear expression
    11 
    12     \c Expr<_Col,_Value> implements a class of linear expressions with the 
    13     operations of addition and multiplication with scalar. 
    14 
    15     \author Marton Makai
    16    */
    17   template <typename _Col, typename _Value>
    18   class Expr;
    19 
    20   template <typename _Col, typename _Value>
    21   class Expr {
    22   protected:
    23     typedef 
    24     typename std::map<_Col, _Value> Data; 
    25     Data data;
    26   public:
    27     Expr() { }
    28     Expr(_Col _col) { 
    29       data.insert(std::make_pair(_col, 1));
    30     }
    31     Expr& operator*=(_Value _value) {
    32       for (typename Data::iterator i=data.begin(); 
    33 	   i!=data.end(); ++i) {
    34 	(*i).second *= _value;
    35       }
    36       return *this;
    37     }
    38     Expr& operator+=(const Expr<_Col, _Value>& expr) {
    39       for (typename Data::const_iterator j=expr.data.begin(); 
    40 	   j!=expr.data.end(); ++j) {
    41 	typename Data::iterator i=data.find((*j).first);
    42 	if (i==data.end()) {
    43 	  data.insert(std::make_pair((*j).first, (*j).second));
    44 	} else {
    45 	  (*i).second+=(*j).second;
    46 	}
    47       }
    48       return *this;
    49     }
    50     template <typename _C, typename _V> 
    51     friend std::ostream& operator<<(std::ostream& os, 
    52 				    const Expr<_C, _V>& expr);
    53   };
    54 
    55   template <typename _Col, typename _Value>
    56   Expr<_Col, _Value> operator*(_Value _value, _Col _col) {
    57     Expr<_Col, _Value> tmp(_col);
    58     tmp*=_value;
    59     return tmp;
    60   }
    61 
    62   template <typename _Col, typename _Value>
    63   Expr<_Col, _Value> operator*(_Value _value, 
    64 			       const Expr<_Col, _Value>& expr) {
    65     Expr<_Col, _Value> tmp(expr);
    66     tmp*=_value;
    67     return tmp;
    68   }
    69 
    70   template <typename _Col, typename _Value>
    71   Expr<_Col, _Value> operator+(const Expr<_Col, _Value>& expr1, 
    72 			       const Expr<_Col, _Value>& expr2) {
    73     Expr<_Col, _Value> tmp(expr1);
    74     tmp+=expr2;
    75     return tmp;
    76   }
    77 
    78   template <typename _Col, typename _Value>
    79   std::ostream& operator<<(std::ostream& os, 
    80 			   const Expr<_Col, _Value>& expr) {
    81     for (typename Expr<_Col, _Value>::Data::const_iterator i=
    82 	   expr.data.begin(); 
    83 	 i!=expr.data.end(); ++i) {
    84       os << (*i).second << "*" << (*i).first << " ";
    85     }
    86     return os;
    87   }
    88   
    89 } //namespace lemon
    90 
    91 #endif //LEMON_EXPRESSION_H