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