1 | /* $Id: CoinBuild.hpp 1448 2011-06-19 15:34:41Z stefan $ */ |
2 | // Copyright (C) 2005, International Business Machines |
3 | // Corporation and others. All Rights Reserved. |
4 | // This code is licensed under the terms of the Eclipse Public License (EPL). |
5 | |
6 | #ifndef CoinBuild_H |
7 | #define CoinBuild_H |
8 | |
9 | |
10 | #include "CoinPragma.hpp" |
11 | #include "CoinTypes.hpp" |
12 | #include "CoinFinite.hpp" |
13 | |
14 | |
15 | /** |
16 | In many cases it is natural to build a model by adding one row at a time. In Coin this |
17 | is inefficient so this class gives some help. An instance of CoinBuild can be built up |
18 | more efficiently and then added to the Clp/OsiModel in one go. |
19 | |
20 | It may be more efficient to have fewer arrays and re-allocate them but this should |
21 | give a large gain over addRow. |
22 | |
23 | I have now extended it to columns. |
24 | |
25 | */ |
26 | |
27 | class CoinBuild { |
28 | |
29 | public: |
30 | /**@name Useful methods */ |
31 | //@{ |
32 | /// add a row |
33 | void addRow(int numberInRow, const int * columns, |
34 | const double * elements, double rowLower=-COIN_DBL_MAX, |
35 | double rowUpper=COIN_DBL_MAX); |
36 | /// add a column |
37 | void addColumn(int numberInColumn, const int * rows, |
38 | const double * elements, |
39 | double columnLower=0.0, |
40 | double columnUpper=COIN_DBL_MAX, double objectiveValue=0.0); |
41 | /// add a column |
42 | inline void addCol(int numberInColumn, const int * rows, |
43 | const double * elements, |
44 | double columnLower=0.0, |
45 | double columnUpper=COIN_DBL_MAX, double objectiveValue=0.0) |
46 | { addColumn(numberInColumn, rows, elements, columnLower, columnUpper, objectiveValue);} |
47 | /// Return number of rows or maximum found so far |
48 | inline int numberRows() const |
49 | { return (type_==0) ? numberItems_ : numberOther_;} |
50 | /// Return number of columns or maximum found so far |
51 | inline int numberColumns() const |
52 | { return (type_==1) ? numberItems_ : numberOther_;} |
53 | /// Return number of elements |
54 | inline CoinBigIndex numberElements() const |
55 | { return numberElements_;} |
56 | /** Returns number of elements in a row and information in row |
57 | */ |
58 | int row(int whichRow, double & rowLower, double & rowUpper, |
59 | const int * & indices, const double * & elements) const; |
60 | /** Returns number of elements in current row and information in row |
61 | Used as rows may be stored in a chain |
62 | */ |
63 | int currentRow(double & rowLower, double & rowUpper, |
64 | const int * & indices, const double * & elements) const; |
65 | /// Set current row |
66 | void setCurrentRow(int whichRow); |
67 | /// Returns current row number |
68 | int currentRow() const; |
69 | /** Returns number of elements in a column and information in column |
70 | */ |
71 | int column(int whichColumn, |
72 | double & columnLower, double & columnUpper,double & objectiveValue, |
73 | const int * & indices, const double * & elements) const; |
74 | /** Returns number of elements in current column and information in column |
75 | Used as columns may be stored in a chain |
76 | */ |
77 | int currentColumn( double & columnLower, double & columnUpper,double & objectiveValue, |
78 | const int * & indices, const double * & elements) const; |
79 | /// Set current column |
80 | void setCurrentColumn(int whichColumn); |
81 | /// Returns current column number |
82 | int currentColumn() const; |
83 | /// Returns type |
84 | inline int type() const |
85 | { return type_;} |
86 | //@} |
87 | |
88 | |
89 | /**@name Constructors, destructor */ |
90 | //@{ |
91 | /** Default constructor. */ |
92 | CoinBuild(); |
93 | /** Constructor with type 0==for addRow, 1== for addColumn. */ |
94 | CoinBuild(int type); |
95 | /** Destructor */ |
96 | ~CoinBuild(); |
97 | //@} |
98 | |
99 | /**@name Copy method */ |
100 | //@{ |
101 | /** The copy constructor. */ |
102 | CoinBuild(const CoinBuild&); |
103 | /// = |
104 | CoinBuild& operator=(const CoinBuild&); |
105 | //@} |
106 | private: |
107 | /// Set current |
108 | void setMutableCurrent(int which) const; |
109 | /// add a item |
110 | void addItem(int numberInItem, const int * indices, |
111 | const double * elements, |
112 | double itemLower, |
113 | double itemUpper, double objectiveValue); |
114 | /** Returns number of elements in a item and information in item |
115 | */ |
116 | int item(int whichItem, |
117 | double & itemLower, double & itemUpper,double & objectiveValue, |
118 | const int * & indices, const double * & elements) const; |
119 | /** Returns number of elements in current item and information in item |
120 | Used as items may be stored in a chain |
121 | */ |
122 | int currentItem( double & itemLower, double & itemUpper,double & objectiveValue, |
123 | const int * & indices, const double * & elements) const; |
124 | /// Set current item |
125 | void setCurrentItem(int whichItem); |
126 | /// Returns current item number |
127 | int currentItem() const; |
128 | |
129 | private: |
130 | /**@name Data members */ |
131 | //@{ |
132 | /// Current number of items |
133 | int numberItems_; |
134 | /// Current number of other dimension i.e. Columns if addRow (i.e. max) |
135 | int numberOther_; |
136 | /// Current number of elements |
137 | CoinBigIndex numberElements_; |
138 | /// Current item pointer |
139 | mutable double * currentItem_; |
140 | /// First item pointer |
141 | double * firstItem_; |
142 | /// Last item pointer |
143 | double * lastItem_; |
144 | /// Type of build - 0 for row, 1 for column, -1 unset |
145 | int type_; |
146 | //@} |
147 | }; |
148 | |
149 | #endif |
150 | |