[1253] | 1 | /* -*- C++ -*- |
---|
| 2 | * src/lemon/lin_expr.h - Part of LEMON, a generic C++ optimization library |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
| 6 | * |
---|
| 7 | * Permission to use, modify and distribute this software is granted |
---|
| 8 | * provided that this copyright notice appears in all copies. For |
---|
| 9 | * precise terms see the accompanying LICENSE file. |
---|
| 10 | * |
---|
| 11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 12 | * express or implied, and with no claim as to its suitability for any |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | #ifndef LEMON_LIN_EXPR_H |
---|
| 18 | #define LEMON_LIN_EXPR_H |
---|
| 19 | |
---|
| 20 | #include<vector> |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | #include<map> |
---|
| 24 | |
---|
| 25 | ///\file |
---|
| 26 | ///\brief Classes to handle linear expressions |
---|
| 27 | namespace lemon { |
---|
| 28 | |
---|
| 29 | /// Class to handle sparse linear expressions |
---|
| 30 | template <class _V,class _C> |
---|
| 31 | class SparseLinExpr : public std::map<_V, _C> |
---|
| 32 | { |
---|
| 33 | public: |
---|
| 34 | typedef _V Var; |
---|
| 35 | typedef _C Coeff; |
---|
| 36 | |
---|
| 37 | protected: |
---|
| 38 | typedef typename std::map<_V, _C> Base; |
---|
| 39 | |
---|
| 40 | Coeff const_comp; |
---|
| 41 | public: |
---|
| 42 | SparseLinExpr() { } |
---|
| 43 | SparseLinExpr(const Var &v) : const_comp(v) { |
---|
| 44 | Base::insert(std::make_pair(v, 1)); |
---|
| 45 | } |
---|
| 46 | SparseLinExpr(const Coeff &v) : const_comp(v) {} |
---|
| 47 | |
---|
| 48 | void set(const Var &v,const Coeff &c) { |
---|
| 49 | return Base::insert(std::make_pair(v, c)); |
---|
| 50 | } |
---|
| 51 | // Coeff &operator[](const Var &v) { return data[v]; } |
---|
| 52 | // const Coeff &operator[](const Var &v) const { return data[v]; } |
---|
| 53 | |
---|
| 54 | Coeff &constComp() { return const_comp; } |
---|
| 55 | const Coeff &constComp() const { return const_comp; } |
---|
| 56 | |
---|
| 57 | ///Removes the components with zero coefficient. |
---|
| 58 | void simplify() { |
---|
| 59 | for (typename Base::iterator i=Base::begin(); i!=Base::end();) { |
---|
| 60 | typename Base::iterator j=i; |
---|
| 61 | ++j; |
---|
| 62 | if ((*i).second==0) Base::erase(i); |
---|
| 63 | j=i; |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | }; |
---|
| 68 | |
---|
| 69 | } //namespace lemon |
---|
| 70 | |
---|
| 71 | #endif //LEMON_LIN_EXPR_H |
---|