alpar@1253: /* -*- C++ -*- alpar@1253: * src/lemon/lin_expr.h - Part of LEMON, a generic C++ optimization library alpar@1253: * alpar@1253: * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@1253: * (Egervary Combinatorial Optimization Research Group, EGRES). alpar@1253: * alpar@1253: * Permission to use, modify and distribute this software is granted alpar@1253: * provided that this copyright notice appears in all copies. For alpar@1253: * precise terms see the accompanying LICENSE file. alpar@1253: * alpar@1253: * This software is provided "AS IS" with no warranty of any kind, alpar@1253: * express or implied, and with no claim as to its suitability for any alpar@1253: * purpose. alpar@1253: * alpar@1253: */ alpar@1253: alpar@1253: #ifndef LEMON_LIN_EXPR_H alpar@1253: #define LEMON_LIN_EXPR_H alpar@1253: alpar@1253: #include alpar@1253: alpar@1253: alpar@1253: #include alpar@1253: alpar@1253: ///\file alpar@1253: ///\brief Classes to handle linear expressions alpar@1253: namespace lemon { alpar@1253: alpar@1253: /// Class to handle sparse linear expressions alpar@1253: template alpar@1253: class SparseLinExpr : public std::map<_V, _C> alpar@1253: { alpar@1253: public: alpar@1253: typedef _V Var; alpar@1253: typedef _C Coeff; alpar@1253: alpar@1253: protected: alpar@1253: typedef typename std::map<_V, _C> Base; alpar@1253: alpar@1253: Coeff const_comp; alpar@1253: public: alpar@1253: SparseLinExpr() { } alpar@1253: SparseLinExpr(const Var &v) : const_comp(v) { alpar@1253: Base::insert(std::make_pair(v, 1)); alpar@1253: } alpar@1253: SparseLinExpr(const Coeff &v) : const_comp(v) {} alpar@1253: alpar@1253: void set(const Var &v,const Coeff &c) { alpar@1253: return Base::insert(std::make_pair(v, c)); alpar@1253: } alpar@1253: // Coeff &operator[](const Var &v) { return data[v]; } alpar@1253: // const Coeff &operator[](const Var &v) const { return data[v]; } alpar@1253: alpar@1253: Coeff &constComp() { return const_comp; } alpar@1253: const Coeff &constComp() const { return const_comp; } alpar@1253: alpar@1253: ///Removes the components with zero coefficient. alpar@1253: void simplify() { alpar@1253: for (typename Base::iterator i=Base::begin(); i!=Base::end();) { alpar@1253: typename Base::iterator j=i; alpar@1253: ++j; alpar@1253: if ((*i).second==0) Base::erase(i); alpar@1253: j=i; alpar@1253: } alpar@1253: } alpar@1253: alpar@1253: }; alpar@1253: alpar@1253: } //namespace lemon alpar@1253: alpar@1253: #endif //LEMON_LIN_EXPR_H