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