clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name ClpModel.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -mframe-pointer=none -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/11.0.0 -D HAVE_CONFIG_H -I . -I . -I /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/coinmp/CoinUtils/src -D COIN_HAS_CLP -D NDEBUG -D CLP_BUILD -D PIC -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10 -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/x86_64-redhat-linux -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/10/../../../../include/c++/10/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/11.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -O3 -Wwrite-strings -Wno-unknown-pragmas -Wno-long-long -fdeprecated-macro -fdebug-compilation-dir /home/maarten/src/libreoffice/core/workdir/UnpackedTarball/coinmp/Clp/src -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -vectorize-loops -vectorize-slp -analyzer-output=html -faddrsig -o /home/maarten/tmp/wis/scan-build-libreoffice/output/report/2020-10-07-141433-9725-1 -x c++ ClpModel.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | #include <cstdlib> |
7 | #include <cmath> |
8 | #include <cassert> |
9 | #include <cfloat> |
10 | #include <string> |
11 | #include <cstdio> |
12 | #include <iostream> |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | #include "CoinPragma.hpp" |
39 | |
40 | #include "CoinHelperFunctions.hpp" |
41 | #include "CoinTime.hpp" |
42 | #include "ClpModel.hpp" |
43 | #include "ClpEventHandler.hpp" |
44 | #include "ClpPackedMatrix.hpp" |
45 | #ifndef SLIM_CLP |
46 | #include "ClpPlusMinusOneMatrix.hpp" |
47 | #endif |
48 | #ifndef CLP_NO_VECTOR |
49 | #include "CoinPackedVector.hpp" |
50 | #endif |
51 | #include "CoinIndexedVector.hpp" |
52 | #if SLIM_CLP==2 |
53 | #define SLIM_NOIO |
54 | #endif |
55 | #ifndef SLIM_NOIO |
56 | #include "CoinMpsIO.hpp" |
57 | #include "CoinFileIO.hpp" |
58 | #include "CoinModel.hpp" |
59 | #endif |
60 | #include "ClpMessage.hpp" |
61 | #include "CoinMessage.hpp" |
62 | #include "ClpLinearObjective.hpp" |
63 | #ifndef SLIM_CLP |
64 | #include "ClpQuadraticObjective.hpp" |
65 | #include "CoinBuild.hpp" |
66 | #endif |
67 | |
68 | |
69 | ClpModel::ClpModel (bool emptyMessages) : |
70 | |
71 | optimizationDirection_(1), |
72 | objectiveValue_(0.0), |
73 | smallElement_(1.0e-20), |
74 | objectiveScale_(1.0), |
75 | rhsScale_(1.0), |
76 | numberRows_(0), |
77 | numberColumns_(0), |
78 | rowActivity_(NULL), |
79 | columnActivity_(NULL), |
80 | dual_(NULL), |
81 | reducedCost_(NULL), |
82 | rowLower_(NULL), |
83 | rowUpper_(NULL), |
84 | objective_(NULL), |
85 | rowObjective_(NULL), |
86 | columnLower_(NULL), |
87 | columnUpper_(NULL), |
88 | matrix_(NULL), |
89 | rowCopy_(NULL), |
90 | scaledMatrix_(NULL), |
91 | ray_(NULL), |
92 | rowScale_(NULL), |
93 | columnScale_(NULL), |
94 | inverseRowScale_(NULL), |
95 | inverseColumnScale_(NULL), |
96 | scalingFlag_(3), |
97 | status_(NULL), |
98 | integerType_(NULL), |
99 | userPointer_(NULL), |
100 | trustedUserPointer_(NULL), |
101 | numberIterations_(0), |
102 | solveType_(0), |
103 | whatsChanged_(0), |
104 | problemStatus_(-1), |
105 | secondaryStatus_(0), |
106 | lengthNames_(0), |
107 | numberThreads_(0), |
108 | specialOptions_(0), |
109 | #ifndef CLP_NO_STD |
110 | defaultHandler_(true), |
111 | rowNames_(), |
112 | columnNames_(), |
113 | #else |
114 | defaultHandler_(true), |
115 | #endif |
116 | maximumColumns_(-1), |
117 | maximumRows_(-1), |
118 | maximumInternalColumns_(-1), |
119 | maximumInternalRows_(-1), |
120 | savedRowScale_(NULL), |
121 | savedColumnScale_(NULL) |
122 | { |
123 | intParam_[ClpMaxNumIteration] = 2147483647; |
124 | intParam_[ClpMaxNumIterationHotStart] = 9999999; |
125 | intParam_[ClpNameDiscipline] = 1; |
126 | |
127 | dblParam_[ClpDualObjectiveLimit] = COIN_DBL_MAX; |
128 | dblParam_[ClpPrimalObjectiveLimit] = COIN_DBL_MAX; |
129 | dblParam_[ClpDualTolerance] = 1e-7; |
130 | dblParam_[ClpPrimalTolerance] = 1e-7; |
131 | dblParam_[ClpObjOffset] = 0.0; |
132 | dblParam_[ClpMaxSeconds] = -1.0; |
133 | dblParam_[ClpPresolveTolerance] = 1.0e-8; |
134 | |
135 | #ifndef CLP_NO_STD |
136 | strParam_[ClpProbName] = "ClpDefaultName"; |
137 | #endif |
138 | handler_ = new CoinMessageHandler(); |
139 | handler_->setLogLevel(1); |
140 | eventHandler_ = new ClpEventHandler(); |
141 | if (!emptyMessages) { |
142 | messages_ = ClpMessage(); |
143 | coinMessages_ = CoinMessage(); |
144 | } |
145 | randomNumberGenerator_.setSeed(1234567); |
146 | } |
147 | |
148 | |
149 | |
150 | ClpModel::~ClpModel () |
151 | { |
152 | if (defaultHandler_) { |
153 | delete handler_; |
154 | handler_ = NULL; |
155 | } |
156 | gutsOfDelete(0); |
157 | } |
158 | |
159 | void |
160 | ClpModel::gutsOfDelete(int type) |
161 | { |
162 | if (!type || !permanentArrays()) { |
163 | maximumRows_ = -1; |
164 | maximumColumns_ = -1; |
165 | delete [] rowActivity_; |
166 | rowActivity_ = NULL; |
167 | delete [] columnActivity_; |
168 | columnActivity_ = NULL; |
169 | delete [] dual_; |
170 | dual_ = NULL; |
171 | delete [] reducedCost_; |
172 | reducedCost_ = NULL; |
173 | delete [] rowLower_; |
174 | delete [] rowUpper_; |
175 | delete [] rowObjective_; |
176 | rowLower_ = NULL; |
177 | rowUpper_ = NULL; |
178 | rowObjective_ = NULL; |
179 | delete [] columnLower_; |
180 | delete [] columnUpper_; |
181 | delete objective_; |
182 | columnLower_ = NULL; |
183 | columnUpper_ = NULL; |
184 | objective_ = NULL; |
185 | delete [] savedRowScale_; |
186 | if (rowScale_ == savedRowScale_) |
187 | rowScale_ = NULL; |
188 | savedRowScale_ = NULL; |
189 | delete [] savedColumnScale_; |
190 | if (columnScale_ == savedColumnScale_) |
191 | columnScale_ = NULL; |
192 | savedColumnScale_ = NULL; |
193 | delete [] rowScale_; |
194 | rowScale_ = NULL; |
195 | delete [] columnScale_; |
196 | columnScale_ = NULL; |
197 | delete [] integerType_; |
198 | integerType_ = NULL; |
199 | delete [] status_; |
200 | status_ = NULL; |
201 | delete eventHandler_; |
202 | eventHandler_ = NULL; |
203 | } |
204 | whatsChanged_ = 0; |
205 | delete matrix_; |
206 | matrix_ = NULL; |
207 | delete rowCopy_; |
208 | rowCopy_ = NULL; |
209 | delete scaledMatrix_; |
210 | scaledMatrix_ = NULL, |
211 | delete [] ray_; |
212 | ray_ = NULL; |
213 | specialOptions_ = 0; |
214 | } |
215 | void |
216 | ClpModel::setRowScale(double * scale) |
217 | { |
218 | if (!savedRowScale_) { |
219 | delete [] reinterpret_cast<double *> (rowScale_); |
220 | rowScale_ = scale; |
221 | } else { |
222 | assert (!scale); |
223 | rowScale_ = NULL; |
224 | } |
225 | } |
226 | void |
227 | ClpModel::setColumnScale(double * scale) |
228 | { |
229 | if (!savedColumnScale_) { |
230 | delete [] reinterpret_cast<double *> (columnScale_); |
231 | columnScale_ = scale; |
232 | } else { |
233 | assert (!scale); |
234 | columnScale_ = NULL; |
235 | } |
236 | } |
237 | |
238 | void ClpModel::setPrimalTolerance( double value) |
239 | { |
240 | if (value > 0.0 && value < 1.0e10) |
241 | dblParam_[ClpPrimalTolerance] = value; |
242 | } |
243 | void ClpModel::setDualTolerance( double value) |
244 | { |
245 | if (value > 0.0 && value < 1.0e10) |
246 | dblParam_[ClpDualTolerance] = value; |
247 | } |
248 | void ClpModel::setOptimizationDirection( double value) |
249 | { |
250 | optimizationDirection_ = value; |
251 | } |
252 | void |
253 | ClpModel::gutsOfLoadModel (int numberRows, int numberColumns, |
254 | const double* collb, const double* colub, |
255 | const double* obj, |
256 | const double* rowlb, const double* rowub, |
257 | const double * rowObjective) |
258 | { |
259 | |
260 | ClpEventHandler * handler = eventHandler_->clone(); |
261 | |
262 | int saveOptions = specialOptions_; |
263 | gutsOfDelete(0); |
264 | specialOptions_ = saveOptions; |
265 | eventHandler_ = handler; |
266 | numberRows_ = numberRows; |
267 | numberColumns_ = numberColumns; |
268 | rowActivity_ = new double[numberRows_]; |
269 | columnActivity_ = new double[numberColumns_]; |
270 | dual_ = new double[numberRows_]; |
271 | reducedCost_ = new double[numberColumns_]; |
272 | |
273 | CoinZeroN(dual_, numberRows_); |
274 | CoinZeroN(reducedCost_, numberColumns_); |
275 | int iRow, iColumn; |
276 | |
277 | rowLower_ = ClpCopyOfArray(rowlb, numberRows_, -COIN_DBL_MAX); |
278 | rowUpper_ = ClpCopyOfArray(rowub, numberRows_, COIN_DBL_MAX); |
279 | double * objective = ClpCopyOfArray(obj, numberColumns_, 0.0); |
280 | objective_ = new ClpLinearObjective(objective, numberColumns_); |
281 | delete [] objective; |
282 | rowObjective_ = ClpCopyOfArray(rowObjective, numberRows_); |
283 | columnLower_ = ClpCopyOfArray(collb, numberColumns_, 0.0); |
284 | columnUpper_ = ClpCopyOfArray(colub, numberColumns_, COIN_DBL_MAX); |
285 | |
286 | for (iRow = 0; iRow < numberRows_; iRow++) { |
287 | if (rowLower_[iRow] > 0.0) { |
288 | rowActivity_[iRow] = rowLower_[iRow]; |
289 | } else if (rowUpper_[iRow] < 0.0) { |
290 | rowActivity_[iRow] = rowUpper_[iRow]; |
291 | } else { |
292 | rowActivity_[iRow] = 0.0; |
293 | } |
294 | if (rowLower_[iRow] < -1.0e27) |
295 | rowLower_[iRow] = -COIN_DBL_MAX; |
296 | if (rowUpper_[iRow] > 1.0e27) |
297 | rowUpper_[iRow] = COIN_DBL_MAX; |
298 | } |
299 | for (iColumn = 0; iColumn < numberColumns_; iColumn++) { |
300 | if (columnLower_[iColumn] > 0.0) { |
301 | columnActivity_[iColumn] = columnLower_[iColumn]; |
302 | } else if (columnUpper_[iColumn] < 0.0) { |
303 | columnActivity_[iColumn] = columnUpper_[iColumn]; |
304 | } else { |
305 | columnActivity_[iColumn] = 0.0; |
306 | } |
307 | if (columnLower_[iColumn] < -1.0e27) |
308 | columnLower_[iColumn] = -COIN_DBL_MAX; |
309 | if (columnUpper_[iColumn] > 1.0e27) |
310 | columnUpper_[iColumn] = COIN_DBL_MAX; |
311 | } |
312 | } |
313 | |
314 | void ClpModel::setRowObjective(const double * rowObjective) |
315 | { |
316 | delete [] rowObjective_; |
317 | rowObjective_ = ClpCopyOfArray(rowObjective, numberRows_); |
318 | whatsChanged_ = 0; |
319 | } |
320 | void |
321 | ClpModel::loadProblem ( const ClpMatrixBase& matrix, |
322 | const double* collb, const double* colub, |
323 | const double* obj, |
324 | const double* rowlb, const double* rowub, |
325 | const double * rowObjective) |
326 | { |
327 | gutsOfLoadModel(matrix.getNumRows(), matrix.getNumCols(), |
328 | collb, colub, obj, rowlb, rowub, rowObjective); |
329 | if (matrix.isColOrdered()) { |
330 | matrix_ = matrix.clone(); |
331 | } else { |
332 | |
333 | CoinPackedMatrix matrix2; |
334 | matrix2.setExtraGap(0.0); |
335 | matrix2.setExtraMajor(0.0); |
336 | matrix2.reverseOrderedCopyOf(*matrix.getPackedMatrix()); |
337 | matrix.releasePackedMatrix(); |
338 | matrix_ = new ClpPackedMatrix(matrix2); |
339 | } |
340 | matrix_->setDimensions(numberRows_, numberColumns_); |
341 | } |
342 | void |
343 | ClpModel::loadProblem ( const CoinPackedMatrix& matrix, |
344 | const double* collb, const double* colub, |
345 | const double* obj, |
346 | const double* rowlb, const double* rowub, |
347 | const double * rowObjective) |
348 | { |
349 | ClpPackedMatrix* clpMatrix = |
350 | dynamic_cast< ClpPackedMatrix*>(matrix_); |
351 | bool special = (clpMatrix) ? clpMatrix->wantsSpecialColumnCopy() : false; |
352 | gutsOfLoadModel(matrix.getNumRows(), matrix.getNumCols(), |
353 | collb, colub, obj, rowlb, rowub, rowObjective); |
354 | if (matrix.isColOrdered()) { |
355 | matrix_ = new ClpPackedMatrix(matrix); |
356 | if (special) { |
357 | clpMatrix = static_cast< ClpPackedMatrix*>(matrix_); |
358 | clpMatrix->makeSpecialColumnCopy(); |
359 | } |
360 | } else { |
361 | CoinPackedMatrix matrix2; |
362 | matrix2.setExtraGap(0.0); |
363 | matrix2.setExtraMajor(0.0); |
364 | matrix2.reverseOrderedCopyOf(matrix); |
365 | matrix_ = new ClpPackedMatrix(matrix2); |
366 | } |
367 | matrix_->setDimensions(numberRows_, numberColumns_); |
368 | } |
369 | void |
370 | ClpModel::loadProblem ( |
371 | const int numcols, const int numrows, |
372 | const CoinBigIndex* start, const int* index, |
373 | const double* value, |
374 | const double* collb, const double* colub, |
375 | const double* obj, |
376 | const double* rowlb, const double* rowub, |
377 | const double * rowObjective) |
378 | { |
379 | gutsOfLoadModel(numrows, numcols, |
380 | collb, colub, obj, rowlb, rowub, rowObjective); |
381 | int numberElements = start ? start[numcols] : 0; |
382 | CoinPackedMatrix matrix(true, numrows, numrows ? numcols : 0, numberElements, |
383 | value, index, start, NULL); |
384 | matrix_ = new ClpPackedMatrix(matrix); |
385 | matrix_->setDimensions(numberRows_, numberColumns_); |
386 | } |
387 | void |
388 | ClpModel::loadProblem ( |
389 | const int numcols, const int numrows, |
390 | const CoinBigIndex* start, const int* index, |
391 | const double* value, const int* length, |
392 | const double* collb, const double* colub, |
393 | const double* obj, |
394 | const double* rowlb, const double* rowub, |
395 | const double * rowObjective) |
396 | { |
397 | gutsOfLoadModel(numrows, numcols, |
398 | collb, colub, obj, rowlb, rowub, rowObjective); |
399 | |
400 | int numberElements = 0; |
401 | int i; |
402 | for (i = 0; i < numcols; i++) |
403 | numberElements += length[i]; |
404 | CoinPackedMatrix matrix(true, numrows, numcols, numberElements, |
405 | value, index, start, length); |
406 | matrix_ = new ClpPackedMatrix(matrix); |
407 | } |
408 | #ifndef SLIM_NOIO |
409 | |
410 | int |
411 | ClpModel::loadProblem ( CoinModel & modelObject, bool tryPlusMinusOne) |
412 | { |
413 | if (modelObject.numberColumns() == 0 && modelObject.numberRows() == 0) |
414 | return 0; |
415 | int numberErrors = 0; |
416 | |
417 | double * rowLower = modelObject.rowLowerArray(); |
418 | double * rowUpper = modelObject.rowUpperArray(); |
419 | double * columnLower = modelObject.columnLowerArray(); |
420 | double * columnUpper = modelObject.columnUpperArray(); |
421 | double * objective = modelObject.objectiveArray(); |
422 | int * integerType = modelObject.integerTypeArray(); |
423 | double * associated = modelObject.associatedArray(); |
424 | |
425 | if (modelObject.stringsExist()) { |
426 | numberErrors = modelObject.createArrays(rowLower, rowUpper, columnLower, columnUpper, |
427 | objective, integerType, associated); |
428 | } |
429 | int numberRows = modelObject.numberRows(); |
430 | int numberColumns = modelObject.numberColumns(); |
431 | gutsOfLoadModel(numberRows, numberColumns, |
432 | columnLower, columnUpper, objective, rowLower, rowUpper, NULL); |
433 | setObjectiveOffset(modelObject.objectiveOffset()); |
434 | CoinBigIndex * startPositive = NULL; |
435 | CoinBigIndex * startNegative = NULL; |
436 | delete matrix_; |
437 | if (tryPlusMinusOne) { |
438 | startPositive = new CoinBigIndex[numberColumns+1]; |
439 | startNegative = new CoinBigIndex[numberColumns]; |
440 | modelObject.countPlusMinusOne(startPositive, startNegative, associated); |
441 | if (startPositive[0] < 0) { |
442 | |
443 | tryPlusMinusOne = false; |
444 | delete [] startPositive; |
445 | delete [] startNegative; |
446 | } |
447 | } |
448 | #ifndef SLIM_CLP |
449 | if (!tryPlusMinusOne) { |
450 | #endif |
451 | CoinPackedMatrix matrix; |
452 | modelObject.createPackedMatrix(matrix, associated); |
453 | matrix_ = new ClpPackedMatrix(matrix); |
454 | #ifndef SLIM_CLP |
455 | } else { |
456 | |
457 | CoinBigIndex size = startPositive[numberColumns]; |
458 | int * indices = new int[size]; |
459 | modelObject.createPlusMinusOne(startPositive, startNegative, indices, |
460 | associated); |
461 | |
462 | ClpPlusMinusOneMatrix * matrix = new ClpPlusMinusOneMatrix(); |
463 | matrix->passInCopy(numberRows, numberColumns, |
464 | true, indices, startPositive, startNegative); |
465 | matrix_ = matrix; |
466 | } |
467 | #endif |
468 | #ifndef CLP_NO_STD |
469 | |
470 | int numberItems; |
471 | numberItems = modelObject.rowNames()->numberItems(); |
472 | if (numberItems) { |
473 | const char *const * rowNames = modelObject.rowNames()->names(); |
474 | copyRowNames(rowNames, 0, numberItems); |
475 | } |
476 | numberItems = modelObject.columnNames()->numberItems(); |
477 | if (numberItems) { |
478 | const char *const * columnNames = modelObject.columnNames()->names(); |
479 | copyColumnNames(columnNames, 0, numberItems); |
480 | } |
481 | #endif |
482 | |
483 | assert(integerType); |
484 | for (int iColumn = 0; iColumn < numberColumns; iColumn++) { |
485 | if (integerType[iColumn]) |
486 | setInteger(iColumn); |
487 | } |
488 | if (rowLower != modelObject.rowLowerArray() || |
489 | columnLower != modelObject.columnLowerArray()) { |
490 | delete [] rowLower; |
491 | delete [] rowUpper; |
492 | delete [] columnLower; |
493 | delete [] columnUpper; |
494 | delete [] objective; |
495 | delete [] integerType; |
496 | delete [] associated; |
497 | if (numberErrors) |
498 | handler_->message(CLP_BAD_STRING_VALUES, messages_) |
499 | << numberErrors |
500 | << CoinMessageEol; |
501 | } |
502 | matrix_->setDimensions(numberRows_, numberColumns_); |
503 | return numberErrors; |
504 | } |
505 | #endif |
506 | void |
507 | ClpModel::getRowBound(int iRow, double& lower, double& upper) const |
508 | { |
509 | lower = -COIN_DBL_MAX; |
510 | upper = COIN_DBL_MAX; |
511 | if (rowUpper_) |
512 | upper = rowUpper_[iRow]; |
513 | if (rowLower_) |
514 | lower = rowLower_[iRow]; |
515 | } |
516 | |
517 | #ifndef NDEBUG |
518 | |
519 | |
520 | static void indexError(int index, |
521 | std::string methodName) |
522 | { |
523 | std::cerr << "Illegal index " << index << " in ClpModel::" << methodName << std::endl; |
524 | throw CoinError("Illegal index", methodName, "ClpModel"); |
525 | } |
526 | #endif |
527 | |
528 | void |
529 | ClpModel::setObjectiveCoefficient( int elementIndex, double elementValue ) |
530 | { |
531 | #ifndef NDEBUG |
532 | if (elementIndex < 0 || elementIndex >= numberColumns_) { |
533 | indexError(elementIndex, "setObjectiveCoefficient"); |
534 | } |
535 | #endif |
536 | objective()[elementIndex] = elementValue; |
537 | whatsChanged_ = 0; |
538 | } |
539 | |
540 | |
541 | void |
542 | ClpModel::setRowLower( int elementIndex, double elementValue ) |
543 | { |
544 | if (elementValue < -1.0e27) |
545 | elementValue = -COIN_DBL_MAX; |
546 | rowLower_[elementIndex] = elementValue; |
547 | whatsChanged_ = 0; |
548 | } |
549 | |
550 | |
551 | |
552 | void |
553 | ClpModel::setRowUpper( int elementIndex, double elementValue ) |
554 | { |
555 | if (elementValue > 1.0e27) |
556 | elementValue = COIN_DBL_MAX; |
557 | rowUpper_[elementIndex] = elementValue; |
558 | whatsChanged_ = 0; |
559 | } |
560 | |
561 | |
562 | void |
563 | ClpModel::setRowBounds( int elementIndex, |
564 | double lower, double upper ) |
565 | { |
566 | if (lower < -1.0e27) |
567 | lower = -COIN_DBL_MAX; |
568 | if (upper > 1.0e27) |
569 | upper = COIN_DBL_MAX; |
570 | CoinAssert (upper >= lower); |
571 | rowLower_[elementIndex] = lower; |
572 | rowUpper_[elementIndex] = upper; |
573 | whatsChanged_ = 0; |
574 | } |
575 | void ClpModel::setRowSetBounds(const int* indexFirst, |
576 | const int* indexLast, |
577 | const double* boundList) |
578 | { |
579 | #ifndef NDEBUG |
580 | int n = numberRows_; |
581 | #endif |
582 | double * lower = rowLower_; |
583 | double * upper = rowUpper_; |
584 | whatsChanged_ = 0; |
585 | while (indexFirst != indexLast) { |
586 | const int iRow = *indexFirst++; |
587 | #ifndef NDEBUG |
588 | if (iRow < 0 || iRow >= n) { |
589 | indexError(iRow, "setRowSetBounds"); |
590 | } |
591 | #endif |
592 | lower[iRow] = *boundList++; |
593 | upper[iRow] = *boundList++; |
594 | if (lower[iRow] < -1.0e27) |
595 | lower[iRow] = -COIN_DBL_MAX; |
596 | if (upper[iRow] > 1.0e27) |
597 | upper[iRow] = COIN_DBL_MAX; |
598 | CoinAssert (upper[iRow] >= lower[iRow]); |
599 | } |
600 | } |
601 | |
602 | |
603 | |
604 | void |
605 | ClpModel::setColumnLower( int elementIndex, double elementValue ) |
606 | { |
607 | #ifndef NDEBUG |
608 | int n = numberColumns_; |
609 | if (elementIndex < 0 || elementIndex >= n) { |
610 | indexError(elementIndex, "setColumnLower"); |
611 | } |
612 | #endif |
613 | if (elementValue < -1.0e27) |
614 | elementValue = -COIN_DBL_MAX; |
615 | columnLower_[elementIndex] = elementValue; |
616 | whatsChanged_ = 0; |
617 | } |
618 | |
619 | |
620 | |
621 | void |
622 | ClpModel::setColumnUpper( int elementIndex, double elementValue ) |
623 | { |
624 | #ifndef NDEBUG |
625 | int n = numberColumns_; |
626 | if (elementIndex < 0 || elementIndex >= n) { |
627 | indexError(elementIndex, "setColumnUpper"); |
628 | } |
629 | #endif |
630 | if (elementValue > 1.0e27) |
631 | elementValue = COIN_DBL_MAX; |
632 | columnUpper_[elementIndex] = elementValue; |
633 | whatsChanged_ = 0; |
634 | } |
635 | |
636 | |
637 | void |
638 | ClpModel::setColumnBounds( int elementIndex, |
639 | double lower, double upper ) |
640 | { |
641 | #ifndef NDEBUG |
642 | int n = numberColumns_; |
643 | if (elementIndex < 0 || elementIndex >= n) { |
644 | indexError(elementIndex, "setColumnBounds"); |
645 | } |
646 | #endif |
647 | if (lower < -1.0e27) |
648 | lower = -COIN_DBL_MAX; |
649 | if (upper > 1.0e27) |
650 | upper = COIN_DBL_MAX; |
651 | columnLower_[elementIndex] = lower; |
652 | columnUpper_[elementIndex] = upper; |
653 | CoinAssert (upper >= lower); |
654 | whatsChanged_ = 0; |
655 | } |
656 | void ClpModel::setColumnSetBounds(const int* indexFirst, |
657 | const int* indexLast, |
658 | const double* boundList) |
659 | { |
660 | double * lower = columnLower_; |
661 | double * upper = columnUpper_; |
662 | whatsChanged_ = 0; |
663 | #ifndef NDEBUG |
664 | int n = numberColumns_; |
665 | #endif |
666 | while (indexFirst != indexLast) { |
667 | const int iColumn = *indexFirst++; |
668 | #ifndef NDEBUG |
669 | if (iColumn < 0 || iColumn >= n) { |
670 | indexError(iColumn, "setColumnSetBounds"); |
671 | } |
672 | #endif |
673 | lower[iColumn] = *boundList++; |
674 | upper[iColumn] = *boundList++; |
675 | CoinAssert (upper[iColumn] >= lower[iColumn]); |
676 | if (lower[iColumn] < -1.0e27) |
677 | lower[iColumn] = -COIN_DBL_MAX; |
678 | if (upper[iColumn] > 1.0e27) |
679 | upper[iColumn] = COIN_DBL_MAX; |
680 | } |
681 | } |
682 | |
683 | |
684 | |
685 | ClpModel::ClpModel(const ClpModel &rhs, int scalingMode) : |
686 | optimizationDirection_(rhs.optimizationDirection_), |
687 | numberRows_(rhs.numberRows_), |
688 | numberColumns_(rhs.numberColumns_), |
689 | specialOptions_(rhs.specialOptions_), |
690 | maximumColumns_(-1), |
691 | maximumRows_(-1), |
692 | maximumInternalColumns_(-1), |
693 | maximumInternalRows_(-1), |
694 | savedRowScale_(NULL), |
695 | savedColumnScale_(NULL) |
696 | { |
697 | gutsOfCopy(rhs); |
698 | if (scalingMode >= 0 && matrix_ && |
699 | matrix_->allElementsInRange(this, smallElement_, 1.0e20)) { |
700 | |
701 | scalingFlag_ = scalingMode; |
702 | setRowScale(NULL); |
703 | setColumnScale(NULL); |
704 | delete rowCopy_; |
705 | rowCopy_ = NULL; |
706 | delete scaledMatrix_; |
707 | scaledMatrix_ = NULL; |
708 | if (scalingMode && !matrix_->scale(this)) { |
709 | |
710 | inverseRowScale_ = rowScale_ + numberRows_; |
711 | inverseColumnScale_ = columnScale_ + numberColumns_; |
712 | gutsOfScaling(); |
713 | |
714 | scalingFlag_ = -scalingFlag_; |
715 | } else { |
716 | |
717 | scalingFlag_ = 0; |
718 | } |
719 | } |
720 | |
721 | } |
722 | |
723 | ClpModel & |
724 | ClpModel::operator=(const ClpModel & rhs) |
725 | { |
726 | if (this != &rhs) { |
727 | if (defaultHandler_) { |
728 | |
729 | |
730 | } |
731 | gutsOfDelete(1); |
732 | optimizationDirection_ = rhs.optimizationDirection_; |
733 | numberRows_ = rhs.numberRows_; |
734 | numberColumns_ = rhs.numberColumns_; |
735 | gutsOfCopy(rhs, -1); |
736 | } |
737 | return *this; |
738 | } |
739 | |
740 | |
741 | |
742 | void |
743 | ClpModel::gutsOfCopy(const ClpModel & rhs, int trueCopy) |
744 | { |
745 | defaultHandler_ = rhs.defaultHandler_; |
746 | randomNumberGenerator_ = rhs.randomNumberGenerator_; |
747 | if (trueCopy >= 0) { |
748 | if (defaultHandler_) |
749 | handler_ = new CoinMessageHandler(*rhs.handler_); |
750 | else |
751 | handler_ = rhs.handler_; |
752 | eventHandler_ = rhs.eventHandler_->clone(); |
753 | messages_ = rhs.messages_; |
754 | coinMessages_ = rhs.coinMessages_; |
755 | } else { |
756 | if (!eventHandler_ && rhs.eventHandler_) |
757 | eventHandler_ = rhs.eventHandler_->clone(); |
758 | } |
759 | intParam_[ClpMaxNumIteration] = rhs.intParam_[ClpMaxNumIteration]; |
760 | intParam_[ClpMaxNumIterationHotStart] = |
761 | rhs.intParam_[ClpMaxNumIterationHotStart]; |
762 | intParam_[ClpNameDiscipline] = rhs.intParam_[ClpNameDiscipline] ; |
763 | |
764 | dblParam_[ClpDualObjectiveLimit] = rhs.dblParam_[ClpDualObjectiveLimit]; |
765 | dblParam_[ClpPrimalObjectiveLimit] = rhs.dblParam_[ClpPrimalObjectiveLimit]; |
766 | dblParam_[ClpDualTolerance] = rhs.dblParam_[ClpDualTolerance]; |
767 | dblParam_[ClpPrimalTolerance] = rhs.dblParam_[ClpPrimalTolerance]; |
768 | dblParam_[ClpObjOffset] = rhs.dblParam_[ClpObjOffset]; |
769 | dblParam_[ClpMaxSeconds] = rhs.dblParam_[ClpMaxSeconds]; |
770 | dblParam_[ClpPresolveTolerance] = rhs.dblParam_[ClpPresolveTolerance]; |
771 | #ifndef CLP_NO_STD |
772 | |
773 | strParam_[ClpProbName] = rhs.strParam_[ClpProbName]; |
774 | #endif |
775 | |
776 | optimizationDirection_ = rhs.optimizationDirection_; |
777 | objectiveValue_ = rhs.objectiveValue_; |
778 | smallElement_ = rhs.smallElement_; |
779 | objectiveScale_ = rhs.objectiveScale_; |
780 | rhsScale_ = rhs.rhsScale_; |
781 | numberIterations_ = rhs.numberIterations_; |
782 | solveType_ = rhs.solveType_; |
783 | whatsChanged_ = rhs.whatsChanged_; |
784 | problemStatus_ = rhs.problemStatus_; |
785 | secondaryStatus_ = rhs.secondaryStatus_; |
786 | numberRows_ = rhs.numberRows_; |
787 | numberColumns_ = rhs.numberColumns_; |
788 | userPointer_ = rhs.userPointer_; |
789 | trustedUserPointer_ = rhs.trustedUserPointer_; |
790 | scalingFlag_ = rhs.scalingFlag_; |
791 | specialOptions_ = rhs.specialOptions_; |
792 | if (trueCopy) { |
793 | #ifndef CLP_NO_STD |
794 | lengthNames_ = rhs.lengthNames_; |
795 | if (lengthNames_) { |
796 | rowNames_ = rhs.rowNames_; |
797 | columnNames_ = rhs.columnNames_; |
798 | } |
799 | #endif |
800 | numberThreads_ = rhs.numberThreads_; |
801 | if (maximumRows_ < 0) { |
802 | specialOptions_ &= ~65536; |
803 | savedRowScale_ = NULL; |
804 | savedColumnScale_ = NULL; |
805 | integerType_ = CoinCopyOfArray(rhs.integerType_, numberColumns_); |
806 | rowActivity_ = ClpCopyOfArray(rhs.rowActivity_, numberRows_); |
807 | columnActivity_ = ClpCopyOfArray(rhs.columnActivity_, numberColumns_); |
808 | dual_ = ClpCopyOfArray(rhs.dual_, numberRows_); |
809 | reducedCost_ = ClpCopyOfArray(rhs.reducedCost_, numberColumns_); |
810 | rowLower_ = ClpCopyOfArray ( rhs.rowLower_, numberRows_ ); |
811 | rowUpper_ = ClpCopyOfArray ( rhs.rowUpper_, numberRows_ ); |
812 | columnLower_ = ClpCopyOfArray ( rhs.columnLower_, numberColumns_ ); |
813 | columnUpper_ = ClpCopyOfArray ( rhs.columnUpper_, numberColumns_ ); |
814 | rowScale_ = ClpCopyOfArray(rhs.rowScale_, numberRows_ * 2); |
815 | columnScale_ = ClpCopyOfArray(rhs.columnScale_, numberColumns_ * 2); |
816 | if (rhs.objective_) |
817 | objective_ = rhs.objective_->clone(); |
818 | else |
819 | objective_ = NULL; |
820 | rowObjective_ = ClpCopyOfArray ( rhs.rowObjective_, numberRows_ ); |
821 | status_ = ClpCopyOfArray( rhs.status_, numberColumns_ + numberRows_); |
822 | ray_ = NULL; |
823 | if (problemStatus_ == 1) |
824 | ray_ = ClpCopyOfArray (rhs.ray_, numberRows_); |
825 | else if (problemStatus_ == 2) |
826 | ray_ = ClpCopyOfArray (rhs.ray_, numberColumns_); |
827 | if (rhs.rowCopy_) { |
828 | rowCopy_ = rhs.rowCopy_->clone(); |
829 | } else { |
830 | rowCopy_ = NULL; |
831 | } |
832 | if (rhs.scaledMatrix_) { |
833 | scaledMatrix_ = new ClpPackedMatrix(*rhs.scaledMatrix_); |
834 | } else { |
835 | scaledMatrix_ = NULL; |
836 | } |
837 | matrix_ = NULL; |
838 | if (rhs.matrix_) { |
839 | matrix_ = rhs.matrix_->clone(); |
840 | } |
841 | } else { |
842 | |
843 | savedRowScale_ = NULL; |
844 | savedColumnScale_ = NULL; |
845 | startPermanentArrays(); |
846 | if (rhs.integerType_) { |
847 | assert (integerType_); |
848 | ClpDisjointCopyN(rhs.integerType_, numberColumns_, integerType_); |
849 | } else { |
850 | integerType_ = NULL; |
851 | } |
852 | if (rhs.rowActivity_) { |
853 | ClpDisjointCopyN ( rhs.rowActivity_, numberRows_ , |
854 | rowActivity_); |
855 | ClpDisjointCopyN ( rhs.columnActivity_, numberColumns_ , |
856 | columnActivity_); |
857 | ClpDisjointCopyN ( rhs.dual_, numberRows_ , |
858 | dual_); |
859 | ClpDisjointCopyN ( rhs.reducedCost_, numberColumns_ , |
860 | reducedCost_); |
861 | } else { |
862 | rowActivity_ = NULL; |
863 | columnActivity_ = NULL; |
864 | dual_ = NULL; |
865 | reducedCost_ = NULL; |
866 | } |
867 | ClpDisjointCopyN ( rhs.rowLower_, numberRows_, rowLower_ ); |
868 | ClpDisjointCopyN ( rhs.rowUpper_, numberRows_, rowUpper_ ); |
869 | ClpDisjointCopyN ( rhs.columnLower_, numberColumns_, columnLower_ ); |
870 | assert ((specialOptions_ & 131072) == 0); |
871 | abort(); |
872 | ClpDisjointCopyN ( rhs.columnUpper_, numberColumns_, columnUpper_ ); |
873 | if (rhs.objective_) { |
874 | abort(); |
875 | objective_ = rhs.objective_->clone(); |
876 | } else { |
877 | objective_ = NULL; |
878 | } |
879 | assert (!rhs.rowObjective_); |
880 | ClpDisjointCopyN( rhs.status_, numberColumns_ + numberRows_, status_); |
881 | ray_ = NULL; |
882 | if (problemStatus_ == 1) |
883 | ray_ = ClpCopyOfArray (rhs.ray_, numberRows_); |
884 | else if (problemStatus_ == 2) |
885 | ray_ = ClpCopyOfArray (rhs.ray_, numberColumns_); |
886 | assert (!ray_); |
887 | delete rowCopy_; |
888 | if (rhs.rowCopy_) { |
889 | rowCopy_ = rhs.rowCopy_->clone(); |
890 | } else { |
891 | rowCopy_ = NULL; |
892 | } |
893 | delete scaledMatrix_; |
894 | if (rhs.scaledMatrix_) { |
895 | scaledMatrix_ = new ClpPackedMatrix(*rhs.scaledMatrix_); |
896 | } else { |
897 | scaledMatrix_ = NULL; |
898 | } |
899 | delete matrix_; |
900 | matrix_ = NULL; |
901 | if (rhs.matrix_) { |
902 | matrix_ = rhs.matrix_->clone(); |
903 | } |
904 | if (rhs.savedRowScale_) { |
905 | assert (savedRowScale_); |
906 | assert (!rowScale_); |
907 | ClpDisjointCopyN(rhs.savedRowScale_, 4 * maximumInternalRows_, savedRowScale_); |
908 | ClpDisjointCopyN(rhs.savedColumnScale_, 4 * maximumInternalColumns_, savedColumnScale_); |
909 | } else { |
910 | assert (!savedRowScale_); |
911 | if (rowScale_) { |
912 | ClpDisjointCopyN(rhs.rowScale_, numberRows_, rowScale_); |
913 | ClpDisjointCopyN(rhs.columnScale_, numberColumns_, columnScale_); |
914 | } else { |
915 | rowScale_ = NULL; |
916 | columnScale_ = NULL; |
917 | } |
918 | } |
919 | abort(); |
920 | } |
921 | } else { |
922 | savedRowScale_ = rhs.savedRowScale_; |
923 | assert (!savedRowScale_); |
924 | savedColumnScale_ = rhs.savedColumnScale_; |
925 | rowActivity_ = rhs.rowActivity_; |
926 | columnActivity_ = rhs.columnActivity_; |
927 | dual_ = rhs.dual_; |
928 | reducedCost_ = rhs.reducedCost_; |
929 | rowLower_ = rhs.rowLower_; |
930 | rowUpper_ = rhs.rowUpper_; |
931 | objective_ = rhs.objective_; |
932 | rowObjective_ = rhs.rowObjective_; |
933 | columnLower_ = rhs.columnLower_; |
934 | columnUpper_ = rhs.columnUpper_; |
935 | matrix_ = rhs.matrix_; |
936 | rowCopy_ = NULL; |
937 | scaledMatrix_ = NULL; |
938 | ray_ = rhs.ray_; |
939 | |
940 | |
941 | lengthNames_ = 0; |
942 | numberThreads_ = rhs.numberThreads_; |
943 | #ifndef CLP_NO_STD |
944 | rowNames_ = std::vector<std::string> (); |
945 | columnNames_ = std::vector<std::string> (); |
946 | #endif |
947 | integerType_ = NULL; |
948 | status_ = rhs.status_; |
949 | } |
950 | inverseRowScale_ = NULL; |
951 | inverseColumnScale_ = NULL; |
952 | } |
953 | |
954 | void |
955 | ClpModel::copy(const ClpMatrixBase * from, ClpMatrixBase * & to) |
956 | { |
957 | assert (from); |
958 | const ClpPackedMatrix * matrixFrom = (dynamic_cast<const ClpPackedMatrix*>(from)); |
959 | ClpPackedMatrix * matrixTo = (dynamic_cast< ClpPackedMatrix*>(to)); |
960 | if (matrixFrom && matrixTo) { |
961 | matrixTo->copy(matrixFrom); |
962 | } else { |
963 | delete to; |
964 | to = from->clone(); |
965 | } |
966 | #if 0 |
967 | delete modelPtr_->matrix_; |
968 | if (continuousModel_->matrix_) { |
969 | modelPtr_->matrix_ = continuousModel_->matrix_->clone(); |
970 | } else { |
971 | modelPtr_->matrix_ = NULL; |
972 | } |
973 | #endif |
974 | } |
975 | |
976 | |
977 | |
978 | void |
979 | ClpModel::borrowModel(ClpModel & rhs) |
980 | { |
981 | if (defaultHandler_) { |
982 | delete handler_; |
983 | handler_ = NULL; |
984 | } |
985 | gutsOfDelete(1); |
986 | optimizationDirection_ = rhs.optimizationDirection_; |
987 | numberRows_ = rhs.numberRows_; |
988 | numberColumns_ = rhs.numberColumns_; |
989 | delete [] rhs.ray_; |
990 | rhs.ray_ = NULL; |
991 | |
992 | ClpPackedMatrix * save = rhs.scaledMatrix_; |
993 | rhs.scaledMatrix_ = NULL; |
994 | delete scaledMatrix_; |
995 | scaledMatrix_ = NULL; |
996 | gutsOfCopy(rhs, 0); |
997 | rhs.scaledMatrix_ = save; |
998 | specialOptions_ = rhs.specialOptions_ & ~65536; |
999 | savedRowScale_ = NULL; |
1000 | savedColumnScale_ = NULL; |
1001 | inverseRowScale_ = NULL; |
1002 | inverseColumnScale_ = NULL; |
1003 | } |
1004 | |
1005 | void |
1006 | ClpModel::returnModel(ClpModel & otherModel) |
1007 | { |
1008 | otherModel.objectiveValue_ = objectiveValue_; |
1009 | otherModel.numberIterations_ = numberIterations_; |
1010 | otherModel.problemStatus_ = problemStatus_; |
1011 | otherModel.secondaryStatus_ = secondaryStatus_; |
1012 | rowActivity_ = NULL; |
1013 | columnActivity_ = NULL; |
1014 | dual_ = NULL; |
1015 | reducedCost_ = NULL; |
1016 | rowLower_ = NULL; |
1017 | rowUpper_ = NULL; |
1018 | objective_ = NULL; |
1019 | rowObjective_ = NULL; |
1020 | columnLower_ = NULL; |
1021 | columnUpper_ = NULL; |
1022 | matrix_ = NULL; |
1023 | if (rowCopy_ != otherModel.rowCopy_) |
1024 | delete rowCopy_; |
1025 | rowCopy_ = NULL; |
1026 | delete scaledMatrix_; |
1027 | scaledMatrix_ = NULL; |
1028 | delete [] otherModel.ray_; |
1029 | otherModel.ray_ = ray_; |
1030 | ray_ = NULL; |
1031 | if (rowScale_ && otherModel.rowScale_ != rowScale_) { |
1032 | delete [] rowScale_; |
1033 | delete [] columnScale_; |
1034 | } |
1035 | rowScale_ = NULL; |
1036 | columnScale_ = NULL; |
1037 | |
1038 | |
1039 | |
1040 | if (otherModel.status_ != status_) { |
1041 | delete [] otherModel.status_; |
1042 | otherModel.status_ = status_; |
1043 | } |
1044 | status_ = NULL; |
1045 | if (defaultHandler_) { |
1046 | delete handler_; |
1047 | handler_ = NULL; |
1048 | } |
1049 | inverseRowScale_ = NULL; |
1050 | inverseColumnScale_ = NULL; |
1051 | } |
1052 | |
1053 | |
1054 | |
1055 | |
1056 | bool |
1057 | ClpModel::setIntParam(ClpIntParam key, int value) |
1058 | { |
1059 | switch (key) { |
1060 | case ClpMaxNumIteration: |
1061 | if (value < 0) |
1062 | return false; |
1063 | break; |
1064 | case ClpMaxNumIterationHotStart: |
1065 | if (value < 0) |
1066 | return false; |
1067 | break; |
1068 | case ClpNameDiscipline: |
1069 | if (value < 0) |
1070 | return false; |
1071 | break; |
1072 | default: |
1073 | return false; |
1074 | } |
1075 | intParam_[key] = value; |
1076 | return true; |
1077 | } |
1078 | |
1079 | |
1080 | |
1081 | bool |
1082 | ClpModel::setDblParam(ClpDblParam key, double value) |
1083 | { |
1084 | |
1085 | switch (key) { |
1086 | case ClpDualObjectiveLimit: |
1087 | break; |
1088 | |
1089 | case ClpPrimalObjectiveLimit: |
1090 | break; |
1091 | |
1092 | case ClpDualTolerance: |
1093 | if (value <= 0.0 || value > 1.0e10) |
1094 | return false; |
1095 | break; |
1096 | |
1097 | case ClpPrimalTolerance: |
1098 | if (value <= 0.0 || value > 1.0e10) |
1099 | return false; |
1100 | break; |
1101 | |
1102 | case ClpObjOffset: |
1103 | break; |
1104 | |
1105 | case ClpMaxSeconds: |
1106 | if(value >= 0) |
1107 | value += CoinCpuTime(); |
1108 | else |
1109 | value = -1.0; |
1110 | break; |
1111 | |
1112 | case ClpPresolveTolerance: |
1113 | if (value <= 0.0 || value > 1.0e10) |
1114 | return false; |
1115 | break; |
1116 | |
1117 | default: |
1118 | return false; |
1119 | } |
1120 | dblParam_[key] = value; |
1121 | return true; |
1122 | } |
1123 | |
1124 | |
1125 | #ifndef CLP_NO_STD |
1126 | |
1127 | bool |
1128 | ClpModel::setStrParam(ClpStrParam key, const std::string & value) |
1129 | { |
1130 | |
1131 | switch (key) { |
1132 | case ClpProbName: |
1133 | break; |
1134 | |
1135 | default: |
1136 | return false; |
1137 | } |
1138 | strParam_[key] = value; |
1139 | return true; |
1140 | } |
1141 | #endif |
1142 | |
1143 | |
1144 | double * resizeDouble(double * array , int size, int newSize, double fill, |
1145 | bool createArray) |
1146 | { |
1147 | if ((array || createArray) && size < newSize) { |
1148 | int i; |
1149 | double * newArray = new double[newSize]; |
1150 | if (array) |
1151 | CoinMemcpyN(array, CoinMin(newSize, size), newArray); |
1152 | delete [] array; |
1153 | array = newArray; |
1154 | for (i = size; i < newSize; i++) |
1155 | array[i] = fill; |
1156 | } |
1157 | return array; |
1158 | } |
1159 | |
1160 | double * deleteDouble(double * array , int size, |
1161 | int number, const int * which, int & newSize) |
1162 | { |
1163 | if (array) { |
1164 | int i ; |
1165 | char * deleted = new char[size]; |
1166 | int numberDeleted = 0; |
1167 | CoinZeroN(deleted, size); |
1168 | for (i = 0; i < number; i++) { |
1169 | int j = which[i]; |
1170 | if (j >= 0 && j < size && !deleted[j]) { |
1171 | numberDeleted++; |
1172 | deleted[j] = 1; |
1173 | } |
1174 | } |
1175 | newSize = size - numberDeleted; |
1176 | double * newArray = new double[newSize]; |
1177 | int put = 0; |
1178 | for (i = 0; i < size; i++) { |
1179 | if (!deleted[i]) { |
1180 | newArray[put++] = array[i]; |
1181 | } |
1182 | } |
1183 | delete [] array; |
1184 | array = newArray; |
1185 | delete [] deleted; |
1186 | } |
1187 | return array; |
1188 | } |
1189 | char * deleteChar(char * array , int size, |
1190 | int number, const int * which, int & newSize, |
1191 | bool ifDelete) |
1192 | { |
1193 | if (array) { |
1194 | int i ; |
1195 | char * deleted = new char[size]; |
1196 | int numberDeleted = 0; |
1197 | CoinZeroN(deleted, size); |
1198 | for (i = 0; i < number; i++) { |
1199 | int j = which[i]; |
1200 | if (j >= 0 && j < size && !deleted[j]) { |
1201 | numberDeleted++; |
1202 | deleted[j] = 1; |
1203 | } |
1204 | } |
1205 | newSize = size - numberDeleted; |
1206 | char * newArray = new char[newSize]; |
1207 | int put = 0; |
1208 | for (i = 0; i < size; i++) { |
1209 | if (!deleted[i]) { |
1210 | newArray[put++] = array[i]; |
1211 | } |
1212 | } |
1213 | if (ifDelete) |
1214 | delete [] array; |
1215 | array = newArray; |
1216 | delete [] deleted; |
1217 | } |
1218 | return array; |
1219 | } |
1220 | |
1221 | void |
1222 | ClpModel::createEmptyMatrix() |
1223 | { |
1224 | delete matrix_; |
1225 | whatsChanged_ = 0; |
1226 | CoinPackedMatrix matrix2; |
1227 | matrix_ = new ClpPackedMatrix(matrix2); |
1228 | } |
1229 | |
1230 | |
1231 | |
1232 | |
1233 | |
1234 | |
1235 | |
1236 | int |
1237 | ClpModel::cleanMatrix(double threshold) |
1238 | { |
1239 | ClpPackedMatrix * matrix = (dynamic_cast< ClpPackedMatrix*>(matrix_)); |
1240 | if (matrix) { |
1241 | return matrix->getPackedMatrix()->cleanMatrix(threshold); |
1242 | } else { |
1243 | return -1; |
1244 | } |
1245 | } |
1246 | |
1247 | void |
1248 | ClpModel::resize (int newNumberRows, int newNumberColumns) |
1249 | { |
1250 | if (newNumberRows == numberRows_ && |
1251 | newNumberColumns == numberColumns_) |
1252 | return; |
1253 | whatsChanged_ = 0; |
1254 | int numberRows2 = newNumberRows; |
1255 | int numberColumns2 = newNumberColumns; |
1256 | if (numberRows2 < maximumRows_) |
1257 | numberRows2 = maximumRows_; |
1258 | if (numberColumns2 < maximumColumns_) |
1259 | numberColumns2 = maximumColumns_; |
1260 | if (numberRows2 > maximumRows_) { |
1261 | rowActivity_ = resizeDouble(rowActivity_, numberRows_, |
1262 | newNumberRows, 0.0, true); |
1263 | dual_ = resizeDouble(dual_, numberRows_, |
1264 | newNumberRows, 0.0, true); |
1265 | rowObjective_ = resizeDouble(rowObjective_, numberRows_, |
1266 | newNumberRows, 0.0, false); |
1267 | rowLower_ = resizeDouble(rowLower_, numberRows_, |
1268 | newNumberRows, -COIN_DBL_MAX, true); |
1269 | rowUpper_ = resizeDouble(rowUpper_, numberRows_, |
1270 | newNumberRows, COIN_DBL_MAX, true); |
1271 | } |
1272 | if (numberColumns2 > maximumColumns_) { |
1273 | columnActivity_ = resizeDouble(columnActivity_, numberColumns_, |
1274 | newNumberColumns, 0.0, true); |
1275 | reducedCost_ = resizeDouble(reducedCost_, numberColumns_, |
1276 | newNumberColumns, 0.0, true); |
1277 | } |
1278 | if (savedRowScale_ && numberRows2 > maximumInternalRows_) { |
1279 | double * temp; |
1280 | temp = new double [4*newNumberRows]; |
1281 | CoinFillN(temp, 4 * newNumberRows, 1.0); |
1282 | CoinMemcpyN(savedRowScale_, numberRows_, temp); |
1283 | CoinMemcpyN(savedRowScale_ + maximumInternalRows_, numberRows_, temp + newNumberRows); |
1284 | CoinMemcpyN(savedRowScale_ + 2 * maximumInternalRows_, numberRows_, temp + 2 * newNumberRows); |
1285 | CoinMemcpyN(savedRowScale_ + 3 * maximumInternalRows_, numberRows_, temp + 3 * newNumberRows); |
1286 | delete [] savedRowScale_; |
1287 | savedRowScale_ = temp; |
1288 | } |
1289 | if (savedColumnScale_ && numberColumns2 > maximumInternalColumns_) { |
1290 | double * temp; |
1291 | temp = new double [4*newNumberColumns]; |
1292 | CoinFillN(temp, 4 * newNumberColumns, 1.0); |
1293 | CoinMemcpyN(savedColumnScale_, numberColumns_, temp); |
1294 | CoinMemcpyN(savedColumnScale_ + maximumInternalColumns_, numberColumns_, temp + newNumberColumns); |
1295 | CoinMemcpyN(savedColumnScale_ + 2 * maximumInternalColumns_, numberColumns_, temp + 2 * newNumberColumns); |
1296 | CoinMemcpyN(savedColumnScale_ + 3 * maximumInternalColumns_, numberColumns_, temp + 3 * newNumberColumns); |
1297 | delete [] savedColumnScale_; |
1298 | savedColumnScale_ = temp; |
1299 | } |
1300 | if (objective_ && numberColumns2 > maximumColumns_) |
1301 | objective_->resize(newNumberColumns); |
1302 | else if (!objective_) |
1303 | objective_ = new ClpLinearObjective(NULL, newNumberColumns); |
1304 | if (numberColumns2 > maximumColumns_) { |
1305 | columnLower_ = resizeDouble(columnLower_, numberColumns_, |
1306 | newNumberColumns, 0.0, true); |
1307 | columnUpper_ = resizeDouble(columnUpper_, numberColumns_, |
1308 | newNumberColumns, COIN_DBL_MAX, true); |
1309 | } |
1310 | if (newNumberRows < numberRows_) { |
1311 | int * which = new int[numberRows_-newNumberRows]; |
1312 | int i; |
1313 | for (i = newNumberRows; i < numberRows_; i++) |
1314 | which[i-newNumberRows] = i; |
1315 | matrix_->deleteRows(numberRows_ - newNumberRows, which); |
1316 | delete [] which; |
1317 | } |
1318 | if (numberRows_ != newNumberRows || numberColumns_ != newNumberColumns) { |
1319 | |
1320 | problemStatus_ = -1; |
1321 | secondaryStatus_ = 0; |
1322 | delete [] ray_; |
1323 | ray_ = NULL; |
1324 | } |
1325 | setRowScale(NULL); |
1326 | setColumnScale(NULL); |
1327 | if (status_) { |
1328 | if (newNumberColumns + newNumberRows) { |
1329 | if (newNumberColumns + newNumberRows > maximumRows_ + maximumColumns_) { |
1330 | unsigned char * tempC = new unsigned char [newNumberColumns+newNumberRows]; |
1331 | unsigned char * tempR = tempC + newNumberColumns; |
1332 | memset(tempC, 3, newNumberColumns * sizeof(unsigned char)); |
1333 | memset(tempR, 1, newNumberRows * sizeof(unsigned char)); |
1334 | CoinMemcpyN(status_, CoinMin(newNumberColumns, numberColumns_), tempC); |
1335 | CoinMemcpyN(status_ + numberColumns_, CoinMin(newNumberRows, numberRows_), tempR); |
1336 | delete [] status_; |
1337 | status_ = tempC; |
1338 | } else if (newNumberColumns < numberColumns_) { |
1339 | memmove(status_ + newNumberColumns, status_ + numberColumns_, |
1340 | newNumberRows); |
1341 | } else if (newNumberColumns > numberColumns_) { |
1342 | memset(status_ + numberColumns_, 3, newNumberColumns - numberColumns_); |
1343 | memmove(status_ + newNumberColumns, status_ + numberColumns_, |
1344 | newNumberRows); |
1345 | } |
1346 | } else { |
1347 | |
1348 | delete [] status_; |
1349 | status_ = NULL; |
1350 | } |
1351 | } |
1352 | #ifndef CLP_NO_STD |
1353 | if (lengthNames_) { |
1354 | |
1355 | if (numberRows_ < newNumberRows) { |
1356 | rowNames_.resize(newNumberRows); |
1357 | lengthNames_ = CoinMax(lengthNames_, 8); |
1358 | char name[9]; |
1359 | for (int iRow = numberRows_; iRow < newNumberRows; iRow++) { |
1360 | sprintf(name, "R%7.7d", iRow); |
1361 | rowNames_[iRow] = name; |
1362 | } |
1363 | } |
1364 | if (numberColumns_ < newNumberColumns) { |
1365 | columnNames_.resize(newNumberColumns); |
1366 | lengthNames_ = CoinMax(lengthNames_, 8); |
1367 | char name[9]; |
1368 | for (int iColumn = numberColumns_; iColumn < newNumberColumns; iColumn++) { |
1369 | sprintf(name, "C%7.7d", iColumn); |
1370 | columnNames_[iColumn] = name; |
1371 | } |
1372 | } |
1373 | } |
1374 | #endif |
1375 | numberRows_ = newNumberRows; |
1376 | if (newNumberColumns < numberColumns_ && matrix_->getNumCols()) { |
1377 | int * which = new int[numberColumns_-newNumberColumns]; |
1378 | int i; |
1379 | for (i = newNumberColumns; i < numberColumns_; i++) |
1380 | which[i-newNumberColumns] = i; |
1381 | matrix_->deleteCols(numberColumns_ - newNumberColumns, which); |
1382 | delete [] which; |
1383 | } |
1384 | if (integerType_ && numberColumns2 > maximumColumns_) { |
1385 | char * temp = new char [newNumberColumns]; |
1386 | CoinZeroN(temp, newNumberColumns); |
1387 | CoinMemcpyN(integerType_, |
1388 | CoinMin(newNumberColumns, numberColumns_), temp); |
1389 | delete [] integerType_; |
1390 | integerType_ = temp; |
1391 | } |
1392 | numberColumns_ = newNumberColumns; |
1393 | if ((specialOptions_ & 65536) != 0) { |
1394 | |
1395 | } |
1396 | if (maximumRows_ >= 0) { |
1397 | if (numberRows_ > maximumRows_) |
1398 | COIN_DETAIL_PRINT(printf("resize %d rows, %d old maximum rows\n", |
1399 | numberRows_, maximumRows_)); |
1400 | maximumRows_ = CoinMax(maximumRows_, numberRows_); |
1401 | maximumColumns_ = CoinMax(maximumColumns_, numberColumns_); |
1402 | } |
1403 | } |
1404 | |
1405 | void |
1406 | ClpModel::deleteRows(int number, const int * which) |
1407 | { |
1408 | if (!number) |
1409 | return; |
1410 | whatsChanged_ &= ~(1 + 2 + 4 + 8 + 16 + 32); |
1411 | int newSize = 0; |
1412 | if (maximumRows_ < 0) { |
1413 | rowActivity_ = deleteDouble(rowActivity_, numberRows_, |
1414 | number, which, newSize); |
1415 | dual_ = deleteDouble(dual_, numberRows_, |
1416 | number, which, newSize); |
1417 | rowObjective_ = deleteDouble(rowObjective_, numberRows_, |
1418 | number, which, newSize); |
1419 | rowLower_ = deleteDouble(rowLower_, numberRows_, |
1420 | number, which, newSize); |
1421 | rowUpper_ = deleteDouble(rowUpper_, numberRows_, |
1422 | number, which, newSize); |
1423 | if (matrix_->getNumRows()) |
1424 | matrix_->deleteRows(number, which); |
1425 | |
1426 | |
1427 | if (status_) { |
1428 | if (numberColumns_ + newSize) { |
1429 | unsigned char * tempR = reinterpret_cast<unsigned char *> |
1430 | (deleteChar(reinterpret_cast<char *>(status_) + numberColumns_, |
1431 | numberRows_, |
1432 | number, which, newSize, false)); |
1433 | unsigned char * tempC = new unsigned char [numberColumns_+newSize]; |
1434 | CoinMemcpyN(status_, numberColumns_, tempC); |
1435 | CoinMemcpyN(tempR, newSize, tempC + numberColumns_); |
1436 | delete [] tempR; |
1437 | delete [] status_; |
1438 | status_ = tempC; |
1439 | } else { |
1440 | |
1441 | delete [] status_; |
1442 | status_ = NULL; |
1443 | } |
1444 | } |
1445 | } else { |
1446 | char * deleted = new char [numberRows_]; |
1447 | int i; |
1448 | int numberDeleted = 0; |
1449 | CoinZeroN(deleted, numberRows_); |
1450 | for (i = 0; i < number; i++) { |
1451 | int j = which[i]; |
1452 | if (j >= 0 && j < numberRows_ && !deleted[j]) { |
1453 | numberDeleted++; |
1454 | deleted[j] = 1; |
1455 | } |
1456 | } |
1457 | assert (!rowObjective_); |
1458 | unsigned char * status2 = status_ + numberColumns_; |
1459 | for (i = 0; i < numberRows_; i++) { |
1460 | if (!deleted[i]) { |
1461 | rowActivity_[newSize] = rowActivity_[i]; |
1462 | dual_[newSize] = dual_[i]; |
1463 | rowLower_[newSize] = rowLower_[i]; |
1464 | rowUpper_[newSize] = rowUpper_[i]; |
1465 | status2[newSize] = status2[i]; |
1466 | newSize++; |
1467 | } |
1468 | } |
1469 | if (matrix_->getNumRows()) |
1470 | matrix_->deleteRows(number, which); |
1471 | |
1472 | delete [] deleted; |
1473 | } |
1474 | #ifndef CLP_NO_STD |
1475 | |
1476 | if (lengthNames_) { |
1477 | char * mark = new char [numberRows_]; |
1478 | CoinZeroN(mark, numberRows_); |
1479 | int i; |
1480 | for (i = 0; i < number; i++) |
1481 | mark[which[i]] = 1; |
1482 | int k = 0; |
1483 | for ( i = 0; i < numberRows_; ++i) { |
1484 | if (!mark[i]) |
1485 | rowNames_[k++] = rowNames_[i]; |
1486 | } |
1487 | rowNames_.erase(rowNames_.begin() + k, rowNames_.end()); |
1488 | delete [] mark; |
1489 | } |
1490 | #endif |
1491 | numberRows_ = newSize; |
1492 | |
1493 | problemStatus_ = -1; |
1494 | secondaryStatus_ = 0; |
1495 | delete [] ray_; |
1496 | ray_ = NULL; |
1497 | if (savedRowScale_ != rowScale_) { |
1498 | delete [] rowScale_; |
1499 | delete [] columnScale_; |
1500 | } |
1501 | rowScale_ = NULL; |
1502 | columnScale_ = NULL; |
1503 | delete scaledMatrix_; |
1504 | scaledMatrix_ = NULL; |
1505 | } |
1506 | |
1507 | void |
1508 | ClpModel::deleteColumns(int number, const int * which) |
1509 | { |
1510 | if (!number) |
1511 | return; |
1512 | assert (maximumColumns_ < 0); |
1513 | whatsChanged_ &= ~(1 + 2 + 4 + 8 + 64 + 128 + 256); |
1514 | int newSize = 0; |
1515 | columnActivity_ = deleteDouble(columnActivity_, numberColumns_, |
1516 | number, which, newSize); |
1517 | reducedCost_ = deleteDouble(reducedCost_, numberColumns_, |
1518 | number, which, newSize); |
1519 | objective_->deleteSome(number, which); |
1520 | columnLower_ = deleteDouble(columnLower_, numberColumns_, |
1521 | number, which, newSize); |
1522 | columnUpper_ = deleteDouble(columnUpper_, numberColumns_, |
1523 | number, which, newSize); |
1524 | |
1525 | if (matrix_->getNumCols() < numberColumns_) { |
1526 | int * which2 = new int [number]; |
1527 | int n = 0; |
1528 | int nMatrix = matrix_->getNumCols(); |
1529 | for (int i = 0; i < number; i++) { |
1530 | if (which[i] < nMatrix) |
1531 | which2[n++] = which[i]; |
1532 | } |
1533 | matrix_->deleteCols(n, which2); |
1534 | delete [] which2; |
1535 | } else { |
1536 | matrix_->deleteCols(number, which); |
1537 | } |
1538 | |
1539 | |
1540 | if (status_) { |
1541 | if (numberRows_ + newSize) { |
1542 | unsigned char * tempC = reinterpret_cast<unsigned char *> |
1543 | (deleteChar(reinterpret_cast<char *>(status_), |
1544 | numberColumns_, |
1545 | number, which, newSize, false)); |
1546 | unsigned char * temp = new unsigned char [numberRows_+newSize]; |
1547 | CoinMemcpyN(tempC, newSize, temp); |
1548 | CoinMemcpyN(status_ + numberColumns_, numberRows_, temp + newSize); |
1549 | delete [] tempC; |
1550 | delete [] status_; |
1551 | status_ = temp; |
1552 | } else { |
1553 | |
1554 | delete [] status_; |
1555 | status_ = NULL; |
1556 | } |
1557 | } |
1558 | integerType_ = deleteChar(integerType_, numberColumns_, |
1559 | number, which, newSize, true); |
1560 | #ifndef CLP_NO_STD |
1561 | |
1562 | if (lengthNames_) { |
1563 | char * mark = new char [numberColumns_]; |
1564 | CoinZeroN(mark, numberColumns_); |
1565 | int i; |
1566 | for (i = 0; i < number; i++) |
1567 | mark[which[i]] = 1; |
1568 | int k = 0; |
1569 | for ( i = 0; i < numberColumns_; ++i) { |
1570 | if (!mark[i]) |
1571 | columnNames_[k++] = columnNames_[i]; |
1572 | } |
1573 | columnNames_.erase(columnNames_.begin() + k, columnNames_.end()); |
1574 | delete [] mark; |
1575 | } |
1576 | #endif |
1577 | numberColumns_ = newSize; |
1578 | |
1579 | problemStatus_ = -1; |
1580 | secondaryStatus_ = 0; |
1581 | delete [] ray_; |
1582 | ray_ = NULL; |
1583 | setRowScale(NULL); |
1584 | setColumnScale(NULL); |
1585 | } |
1586 | |
1587 | void |
1588 | ClpModel::deleteRowsAndColumns(int numberRows, const int * whichRows, |
1589 | int numberColumns, const int * whichColumns) |
1590 | { |
1591 | if (!numberColumns) { |
1592 | deleteRows(numberRows,whichRows); |
1593 | } else if (!numberRows) { |
1594 | deleteColumns(numberColumns,whichColumns); |
1595 | } else { |
1596 | whatsChanged_ &= ~511; |
1597 | bool doStatus = status_!=NULL; |
1598 | int numberTotal=numberRows_+numberColumns_; |
1599 | int * backRows = new int [numberTotal]; |
1600 | int * backColumns = backRows+numberRows_; |
1601 | memset(backRows,0,numberTotal*sizeof(int)); |
1602 | int newNumberColumns=0; |
1603 | for (int i=0;i<numberColumns;i++) { |
1604 | int iColumn=whichColumns[i]; |
1605 | if (iColumn>=0&&iColumn<numberColumns_) |
1606 | backColumns[iColumn]=-1; |
1607 | } |
1608 | assert (objective_->type()==1); |
1609 | double * obj = objective(); |
1610 | for (int i=0;i<numberColumns_;i++) { |
1611 | if (!backColumns[i]) { |
1612 | columnActivity_[newNumberColumns] = columnActivity_[i]; |
1613 | reducedCost_[newNumberColumns] = reducedCost_[i]; |
1614 | obj[newNumberColumns] = obj[i]; |
1615 | columnLower_[newNumberColumns] = columnLower_[i]; |
1616 | columnUpper_[newNumberColumns] = columnUpper_[i]; |
1617 | if (doStatus) |
1618 | status_[newNumberColumns] = status_[i]; |
1619 | backColumns[i]=newNumberColumns++; |
1620 | } |
1621 | } |
1622 | integerType_ = deleteChar(integerType_, numberColumns_, |
1623 | numberColumns, whichColumns, newNumberColumns, true); |
1624 | #ifndef CLP_NO_STD |
1625 | |
1626 | if (lengthNames_) { |
1627 | for (int i=0;i<numberColumns_;i++) { |
1628 | int iColumn=backColumns[i]; |
1629 | if (iColumn) |
1630 | columnNames_[iColumn] = columnNames_[i]; |
1631 | } |
1632 | columnNames_.erase(columnNames_.begin() + newNumberColumns, columnNames_.end()); |
1633 | } |
1634 | #endif |
1635 | int newNumberRows=0; |
1636 | assert (!rowObjective_); |
1637 | unsigned char * status2 = status_ + numberColumns_; |
1638 | unsigned char * status2a = status_ + newNumberColumns; |
1639 | for (int i=0;i<numberRows;i++) { |
1640 | int iRow=whichRows[i]; |
1641 | if (iRow>=0&&iRow<numberRows_) |
1642 | backRows[iRow]=-1; |
1643 | } |
1644 | for (int i=0;i<numberRows_;i++) { |
1645 | if (!backRows[i]) { |
1646 | rowActivity_[newNumberRows] = rowActivity_[i]; |
1647 | dual_[newNumberRows] = dual_[i]; |
1648 | rowLower_[newNumberRows] = rowLower_[i]; |
1649 | rowUpper_[newNumberRows] = rowUpper_[i]; |
1650 | if (doStatus) |
1651 | status2a[newNumberRows] = status2[i]; |
1652 | backRows[i]=newNumberRows++; |
1653 | } |
1654 | } |
1655 | #ifndef CLP_NO_STD |
1656 | |
1657 | if (lengthNames_) { |
1658 | for (int i=0;i<numberRows_;i++) { |
1659 | int iRow=backRows[i]; |
1660 | if (iRow) |
1661 | rowNames_[iRow] = rowNames_[i]; |
1662 | } |
1663 | rowNames_.erase(rowNames_.begin() + newNumberRows, rowNames_.end()); |
1664 | } |
1665 | #endif |
1666 | |
1667 | ClpPackedMatrix * clpMatrix = dynamic_cast<ClpPackedMatrix *>(matrix_); |
1668 | CoinPackedMatrix * matrix = clpMatrix ? clpMatrix->matrix() : NULL; |
1669 | if (matrix_->getNumCols() < numberColumns_) { |
1670 | assert (matrix); |
1671 | CoinBigIndex nel=matrix->getNumElements(); |
1672 | int n=matrix->getNumCols(); |
1673 | matrix->reserve(numberColumns_,nel); |
1674 | CoinBigIndex * columnStart = matrix->getMutableVectorStarts(); |
1675 | int * columnLength = matrix->getMutableVectorLengths(); |
1676 | for (int i=n;i<numberColumns_;i++) { |
1677 | columnStart[i]=nel; |
1678 | columnLength[i]=0; |
1679 | } |
1680 | } |
1681 | if (matrix) { |
1682 | matrix->setExtraMajor(0.1); |
1683 | |
1684 | matrix->setExtraGap(0.0); |
1685 | matrix->setExtraMajor(0.0); |
1686 | int * row = matrix->getMutableIndices(); |
1687 | CoinBigIndex * columnStart = matrix->getMutableVectorStarts(); |
1688 | int * columnLength = matrix->getMutableVectorLengths(); |
1689 | double * element = matrix->getMutableElements(); |
1690 | newNumberColumns=0; |
1691 | CoinBigIndex n=0; |
1692 | for (int iColumn=0;iColumn<numberColumns_;iColumn++) { |
1693 | if (backColumns[iColumn]>=0) { |
1694 | CoinBigIndex start = columnStart[iColumn]; |
1695 | int nSave=n; |
1696 | columnStart[newNumberColumns]=n; |
1697 | for (CoinBigIndex j=start;j<start+columnLength[iColumn];j++) { |
1698 | int iRow=row[j]; |
1699 | iRow = backRows[iRow]; |
1700 | if (iRow>=0) { |
1701 | row[n]=iRow; |
1702 | element[n++]=element[j]; |
1703 | } |
1704 | } |
1705 | columnLength[newNumberColumns++]=n-nSave; |
1706 | } |
1707 | } |
1708 | columnStart[newNumberColumns]=n; |
1709 | matrix->setNumElements(n); |
1710 | matrix->setMajorDim(newNumberColumns); |
1711 | matrix->setMinorDim(newNumberRows); |
1712 | clpMatrix->setNumberActiveColumns(newNumberColumns); |
1713 | |
1714 | |
1715 | |
1716 | |
1717 | } else { |
1718 | matrix_->deleteRows(numberRows, whichRows); |
1719 | matrix_->deleteCols(numberColumns, whichColumns); |
1720 | } |
1721 | numberColumns_ = newNumberColumns; |
1722 | numberRows_ = newNumberRows; |
1723 | delete [] backRows; |
1724 | |
1725 | problemStatus_ = -1; |
1726 | secondaryStatus_ = 0; |
1727 | delete [] ray_; |
1728 | ray_ = NULL; |
1729 | if (savedRowScale_ != rowScale_) { |
1730 | delete [] rowScale_; |
1731 | delete [] columnScale_; |
1732 | } |
1733 | rowScale_ = NULL; |
1734 | columnScale_ = NULL; |
1735 | delete scaledMatrix_; |
1736 | scaledMatrix_ = NULL; |
1737 | delete rowCopy_; |
1738 | rowCopy_ = NULL; |
1739 | } |
1740 | } |
1741 | |
1742 | void |
1743 | ClpModel::addRow(int numberInRow, const int * columns, |
1744 | const double * elements, double rowLower, double rowUpper) |
1745 | { |
1746 | CoinBigIndex starts[2]; |
1747 | starts[0] = 0; |
1748 | starts[1] = numberInRow; |
1749 | addRows(1, &rowLower, &rowUpper, starts, columns, elements); |
1750 | } |
1751 | |
1752 | void |
1753 | ClpModel::addRows(int number, const double * rowLower, |
1754 | const double * rowUpper, |
1755 | const CoinBigIndex * rowStarts, const int * columns, |
1756 | const double * elements) |
1757 | { |
1758 | if (number) { |
1759 | whatsChanged_ &= ~(1 + 2 + 8 + 16 + 32); |
1760 | int numberRowsNow = numberRows_; |
1761 | resize(numberRowsNow + number, numberColumns_); |
1762 | double * lower = rowLower_ + numberRowsNow; |
1763 | double * upper = rowUpper_ + numberRowsNow; |
1764 | int iRow; |
1765 | if (rowLower) { |
1766 | for (iRow = 0; iRow < number; iRow++) { |
1767 | double value = rowLower[iRow]; |
1768 | if (value < -1.0e20) |
1769 | value = -COIN_DBL_MAX; |
1770 | lower[iRow] = value; |
1771 | } |
1772 | } else { |
1773 | for (iRow = 0; iRow < number; iRow++) { |
1774 | lower[iRow] = -COIN_DBL_MAX; |
1775 | } |
1776 | } |
1777 | if (rowUpper) { |
1778 | for (iRow = 0; iRow < number; iRow++) { |
1779 | double value = rowUpper[iRow]; |
1780 | if (value > 1.0e20) |
1781 | value = COIN_DBL_MAX; |
1782 | upper[iRow] = value; |
1783 | } |
1784 | } else { |
1785 | for (iRow = 0; iRow < number; iRow++) { |
1786 | upper[iRow] = COIN_DBL_MAX; |
1787 | } |
1788 | } |
1789 | |
1790 | |
1791 | delete rowCopy_; |
1792 | rowCopy_ = NULL; |
1793 | delete scaledMatrix_; |
1794 | scaledMatrix_ = NULL; |
1795 | if (!matrix_) |
1796 | createEmptyMatrix(); |
1797 | setRowScale(NULL); |
1798 | setColumnScale(NULL); |
1799 | #ifndef CLP_NO_STD |
1800 | if (lengthNames_) { |
1801 | rowNames_.resize(numberRows_); |
1802 | } |
1803 | #endif |
1804 | if (rowStarts) { |
1805 | |
1806 | matrix_->getPackedMatrix()->reserve(numberColumns_, 0, true); |
1807 | matrix_->appendMatrix(number, 0, rowStarts, columns, elements); |
1808 | } |
1809 | } |
1810 | } |
1811 | |
1812 | void |
1813 | ClpModel::addRows(int number, const double * rowLower, |
1814 | const double * rowUpper, |
1815 | const CoinBigIndex * rowStarts, |
1816 | const int * rowLengths, const int * columns, |
1817 | const double * elements) |
1818 | { |
1819 | if (number) { |
1820 | CoinBigIndex numberElements = 0; |
1821 | int iRow; |
1822 | for (iRow = 0; iRow < number; iRow++) |
1823 | numberElements += rowLengths[iRow]; |
1824 | int * newStarts = new int[number+1]; |
1825 | int * newIndex = new int[numberElements]; |
1826 | double * newElements = new double[numberElements]; |
1827 | numberElements = 0; |
1828 | newStarts[0] = 0; |
1829 | for (iRow = 0; iRow < number; iRow++) { |
1830 | int iStart = rowStarts[iRow]; |
1831 | int length = rowLengths[iRow]; |
1832 | CoinMemcpyN(columns + iStart, length, newIndex + numberElements); |
1833 | CoinMemcpyN(elements + iStart, length, newElements + numberElements); |
1834 | numberElements += length; |
1835 | newStarts[iRow+1] = numberElements; |
1836 | } |
1837 | addRows(number, rowLower, rowUpper, |
1838 | newStarts, newIndex, newElements); |
1839 | delete [] newStarts; |
1840 | delete [] newIndex; |
1841 | delete [] newElements; |
1842 | } |
1843 | } |
1844 | #ifndef CLP_NO_VECTOR |
1845 | void |
1846 | ClpModel::addRows(int number, const double * rowLower, |
1847 | const double * rowUpper, |
1848 | const CoinPackedVectorBase * const * rows) |
1849 | { |
1850 | if (!number) |
1851 | return; |
1852 | whatsChanged_ &= ~(1 + 2 + 8 + 16 + 32); |
1853 | int numberRowsNow = numberRows_; |
1854 | resize(numberRowsNow + number, numberColumns_); |
1855 | double * lower = rowLower_ + numberRowsNow; |
1856 | double * upper = rowUpper_ + numberRowsNow; |
1857 | int iRow; |
1858 | if (rowLower) { |
1859 | for (iRow = 0; iRow < number; iRow++) { |
1860 | double value = rowLower[iRow]; |
1861 | if (value < -1.0e20) |
1862 | value = -COIN_DBL_MAX; |
1863 | lower[iRow] = value; |
1864 | } |
1865 | } else { |
1866 | for (iRow = 0; iRow < number; iRow++) { |
1867 | lower[iRow] = -COIN_DBL_MAX; |
1868 | } |
1869 | } |
1870 | if (rowUpper) { |
1871 | for (iRow = 0; iRow < number; iRow++) { |
1872 | double value = rowUpper[iRow]; |
1873 | if (value > 1.0e20) |
1874 | value = COIN_DBL_MAX; |
1875 | upper[iRow] = value; |
1876 | } |
1877 | } else { |
1878 | for (iRow = 0; iRow < number; iRow++) { |
1879 | upper[iRow] = COIN_DBL_MAX; |
1880 | } |
1881 | } |
1882 | |
1883 | |
1884 | delete rowCopy_; |
1885 | rowCopy_ = NULL; |
1886 | delete scaledMatrix_; |
1887 | scaledMatrix_ = NULL; |
1888 | if (!matrix_) |
1889 | createEmptyMatrix(); |
1890 | if (rows) |
1891 | matrix_->appendRows(number, rows); |
1892 | setRowScale(NULL); |
1893 | setColumnScale(NULL); |
1894 | if (lengthNames_) { |
1895 | rowNames_.resize(numberRows_); |
1896 | } |
1897 | } |
1898 | #endif |
1899 | #ifndef SLIM_CLP |
1900 | |
1901 | int |
1902 | ClpModel::addRows(const CoinBuild & buildObject, bool tryPlusMinusOne, bool checkDuplicates) |
1903 | { |
1904 | CoinAssertHint (buildObject.type() == 0, "Looks as if both addRows and addCols being used"); |
1905 | int number = buildObject.numberRows(); |
1906 | int numberErrors = 0; |
1907 | if (number) { |
| 1 | Assuming 'number' is not equal to 0 | |
|
| |
1908 | CoinBigIndex size = 0; |
1909 | int iRow; |
1910 | double * lower = new double [number]; |
1911 | double * upper = new double [number]; |
1912 | if ((!matrix_ || !matrix_->getNumElements()) && tryPlusMinusOne) { |
| 3 | | Assuming field 'matrix_' is non-null | |
|
| 4 | | Assuming the condition is true | |
|
| 5 | | Assuming 'tryPlusMinusOne' is true | |
|
| |
1913 | |
1914 | for (iRow = 0; iRow < number; iRow++) { |
| 7 | | Assuming 'iRow' is >= 'number' | |
|
| 8 | | Loop condition is false. Execution continues on line 1938 | |
|
1915 | const int * columns; |
1916 | const double * elements; |
1917 | int numberElements = buildObject.row(iRow, lower[iRow], |
1918 | upper[iRow], |
1919 | columns, elements); |
1920 | for (int i = 0; i < numberElements; i++) { |
1921 | |
1922 | if (elements[i]) { |
1923 | if (fabs(elements[i]) == 1.0) { |
1924 | size++; |
1925 | } else { |
1926 | |
1927 | tryPlusMinusOne = false; |
1928 | } |
1929 | } |
1930 | } |
1931 | if (!tryPlusMinusOne) |
1932 | break; |
1933 | } |
1934 | } else { |
1935 | |
1936 | tryPlusMinusOne = false; |
1937 | } |
1938 | if (!tryPlusMinusOne) { |
| |
1939 | CoinBigIndex numberElements = buildObject.numberElements(); |
1940 | CoinBigIndex * starts = new CoinBigIndex [number+1]; |
1941 | int * column = new int[numberElements]; |
1942 | double * element = new double[numberElements]; |
1943 | starts[0] = 0; |
1944 | numberElements = 0; |
1945 | for (iRow = 0; iRow < number; iRow++) { |
1946 | const int * columns; |
1947 | const double * elements; |
1948 | int numberElementsThis = buildObject.row(iRow, lower[iRow], upper[iRow], |
1949 | columns, elements); |
1950 | CoinMemcpyN(columns, numberElementsThis, column + numberElements); |
1951 | CoinMemcpyN(elements, numberElementsThis, element + numberElements); |
1952 | numberElements += numberElementsThis; |
1953 | starts[iRow+1] = numberElements; |
1954 | } |
1955 | addRows(number, lower, upper, NULL); |
1956 | |
1957 | matrix_->setDimensions(-1, numberColumns_); |
1958 | numberErrors = matrix_->appendMatrix(number, 0, starts, column, element, |
1959 | checkDuplicates ? numberColumns_ : -1); |
1960 | delete [] starts; |
1961 | delete [] column; |
1962 | delete [] element; |
1963 | } else { |
1964 | char * which = NULL; |
1965 | if (checkDuplicates) { |
| 10 | | Assuming 'checkDuplicates' is false | |
|
| |
1966 | which = new char[numberColumns_]; |
1967 | CoinZeroN(which, numberColumns_); |
1968 | } |
1969 | |
1970 | |
1971 | addRows(number, lower, upper, NULL); |
1972 | CoinBigIndex * startPositive = new CoinBigIndex [numberColumns_+1]; |
1973 | CoinBigIndex * startNegative = new CoinBigIndex [numberColumns_]; |
1974 | int * indices = new int [size]; |
| |
1975 | CoinZeroN(startPositive, numberColumns_); |
1976 | CoinZeroN(startNegative, numberColumns_); |
1977 | int maxColumn = -1; |
1978 | |
1979 | for (iRow = 0; iRow < number; iRow++) { |
| 13 | | Loop condition is false. Execution continues on line 2014 | |
|
1980 | const int * columns; |
1981 | const double * elements; |
1982 | int numberElements = buildObject.row(iRow, lower[iRow], |
1983 | upper[iRow], |
1984 | columns, elements); |
1985 | for (int i = 0; i < numberElements; i++) { |
1986 | int iColumn = columns[i]; |
1987 | if (checkDuplicates) { |
1988 | if (iColumn >= numberColumns_) { |
1989 | if(which[iColumn]) |
1990 | numberErrors++; |
1991 | else |
1992 | which[iColumn] = 1; |
1993 | } else { |
1994 | numberErrors++; |
1995 | |
1996 | checkDuplicates = false; |
1997 | } |
1998 | } |
1999 | maxColumn = CoinMax(maxColumn, iColumn); |
2000 | if (elements[i] == 1.0) { |
2001 | startPositive[iColumn]++; |
2002 | } else if (elements[i] == -1.0) { |
2003 | startNegative[iColumn]++; |
2004 | } |
2005 | } |
2006 | if (checkDuplicates) { |
2007 | for (int i = 0; i < numberElements; i++) { |
2008 | int iColumn = columns[i]; |
2009 | which[iColumn] = 0; |
2010 | } |
2011 | } |
2012 | } |
2013 | |
2014 | int numberColumns = maxColumn + 1; |
2015 | CoinAssertHint (numberColumns <= numberColumns_, |
2016 | "rows having column indices >= numberColumns_"); |
2017 | size = 0; |
2018 | int iColumn; |
2019 | for (iColumn = 0; iColumn < numberColumns_; iColumn++) { |
| 14 | | Assuming 'iColumn' is < field 'numberColumns_' | |
|
| 15 | | Loop condition is true. Entering loop body | |
|
| 16 | | Assuming 'iColumn' is >= field 'numberColumns_' | |
|
| 17 | | Loop condition is false. Execution continues on line 2027 | |
|
2020 | CoinBigIndex n = startPositive[iColumn]; |
2021 | startPositive[iColumn] = size; |
2022 | size += n; |
2023 | n = startNegative[iColumn]; |
2024 | startNegative[iColumn] = size; |
2025 | size += n; |
2026 | } |
2027 | startPositive[numberColumns_] = size; |
2028 | for (iRow = 0; iRow < number; iRow++) { |
| 18 | | Loop condition is false. Execution continues on line 2049 | |
|
2029 | const int * columns; |
2030 | const double * elements; |
2031 | int numberElements = buildObject.row(iRow, lower[iRow], |
2032 | upper[iRow], |
2033 | columns, elements); |
2034 | for (int i = 0; i < numberElements; i++) { |
2035 | int iColumn = columns[i]; |
2036 | maxColumn = CoinMax(maxColumn, iColumn); |
2037 | if (elements[i] == 1.0) { |
2038 | CoinBigIndex position = startPositive[iColumn]; |
2039 | indices[position] = iRow; |
2040 | startPositive[iColumn]++; |
2041 | } else if (elements[i] == -1.0) { |
2042 | CoinBigIndex position = startNegative[iColumn]; |
2043 | indices[position] = iRow; |
2044 | startNegative[iColumn]++; |
2045 | } |
2046 | } |
2047 | } |
2048 | |
2049 | for (iColumn = numberColumns_ - 1; iColumn >= 0; iColumn--) { |
| 19 | | Loop condition is true. Entering loop body | |
|
| 20 | | Loop condition is false. Execution continues on line 2053 | |
|
2050 | startPositive[iColumn+1] = startNegative[iColumn]; |
2051 | startNegative[iColumn] = startPositive[iColumn]; |
2052 | } |
2053 | startPositive[0] = 0; |
2054 | for (iColumn = 0; iColumn < numberColumns_; iColumn++) { |
| 21 | | Loop condition is true. Entering loop body | |
|
2055 | CoinBigIndex start = startPositive[iColumn]; |
2056 | CoinBigIndex end = startNegative[iColumn]; |
2057 | std::sort(indices + start, indices + end); |
2058 | start = startNegative[iColumn]; |
2059 | end = startPositive[iColumn+1]; |
2060 | std::sort(indices + start, indices + end); |
| |
2061 | } |
2062 | |
2063 | delete matrix_; |
2064 | ClpPlusMinusOneMatrix * matrix = new ClpPlusMinusOneMatrix(); |
2065 | matrix->passInCopy(numberRows_, numberColumns, |
2066 | true, indices, startPositive, startNegative); |
2067 | matrix_ = matrix; |
2068 | delete [] which; |
2069 | } |
2070 | delete [] lower; |
2071 | delete [] upper; |
2072 | |
2073 | matrix_->setDimensions(numberRows_, numberColumns_); |
2074 | } |
2075 | return numberErrors; |
2076 | } |
2077 | #endif |
2078 | #ifndef SLIM_NOIO |
2079 | |
2080 | int |
2081 | ClpModel::addRows( CoinModel & modelObject, bool tryPlusMinusOne, bool checkDuplicates) |
2082 | { |
2083 | if (modelObject.numberElements() == 0) |
2084 | return 0; |
2085 | bool goodState = true; |
2086 | int numberErrors = 0; |
2087 | if (modelObject.columnLowerArray()) { |
2088 | |
2089 | int numberColumns2 = modelObject.numberColumns(); |
2090 | const double * columnLower = modelObject.columnLowerArray(); |
2091 | const double * columnUpper = modelObject.columnUpperArray(); |
2092 | const double * objective = modelObject.objectiveArray(); |
2093 | const int * integerType = modelObject.integerTypeArray(); |
2094 | for (int i = 0; i < numberColumns2; i++) { |
2095 | if (columnLower[i] != 0.0) |
2096 | goodState = false; |
2097 | if (columnUpper[i] != COIN_DBL_MAX) |
2098 | goodState = false; |
2099 | if (objective[i] != 0.0) |
2100 | goodState = false; |
2101 | if (integerType[i] != 0) |
2102 | goodState = false; |
2103 | } |
2104 | } |
2105 | if (goodState) { |
2106 | |
2107 | |
2108 | double * rowLower = modelObject.rowLowerArray(); |
2109 | double * rowUpper = modelObject.rowUpperArray(); |
2110 | double * columnLower = modelObject.columnLowerArray(); |
2111 | double * columnUpper = modelObject.columnUpperArray(); |
2112 | double * objective = modelObject.objectiveArray(); |
2113 | int * integerType = modelObject.integerTypeArray(); |
2114 | double * associated = modelObject.associatedArray(); |
2115 | |
2116 | if (modelObject.stringsExist()) { |
2117 | numberErrors = modelObject.createArrays(rowLower, rowUpper, columnLower, columnUpper, |
2118 | objective, integerType, associated); |
2119 | } |
2120 | int numberRows = numberRows_; |
2121 | int numberRows2 = modelObject.numberRows(); |
2122 | if (numberRows2 && !numberErrors) { |
2123 | CoinBigIndex * startPositive = NULL; |
2124 | CoinBigIndex * startNegative = NULL; |
2125 | int numberColumns = modelObject.numberColumns(); |
2126 | if ((!matrix_ || !matrix_->getNumElements()) && !numberRows && tryPlusMinusOne) { |
2127 | startPositive = new CoinBigIndex[numberColumns+1]; |
2128 | startNegative = new CoinBigIndex[numberColumns]; |
2129 | modelObject.countPlusMinusOne(startPositive, startNegative, associated); |
2130 | if (startPositive[0] < 0) { |
2131 | |
2132 | tryPlusMinusOne = false; |
2133 | delete [] startPositive; |
2134 | delete [] startNegative; |
2135 | } |
2136 | } else { |
2137 | |
2138 | tryPlusMinusOne = false; |
2139 | } |
2140 | assert (rowLower); |
2141 | addRows(numberRows2, rowLower, rowUpper, NULL, NULL, NULL); |
2142 | #ifndef SLIM_CLP |
2143 | if (!tryPlusMinusOne) { |
2144 | #endif |
2145 | CoinPackedMatrix matrix; |
2146 | modelObject.createPackedMatrix(matrix, associated); |
2147 | assert (!matrix.getExtraGap()); |
2148 | if (matrix_->getNumRows()) { |
2149 | |
2150 | matrix.reverseOrdering(); |
2151 | assert (!matrix.getExtraGap()); |
2152 | const int * column = matrix.getIndices(); |
2153 | |
2154 | const CoinBigIndex * rowStart = matrix.getVectorStarts(); |
2155 | const double * element = matrix.getElements(); |
2156 | |
2157 | matrix_->setDimensions(-1, numberColumns_); |
2158 | numberErrors += matrix_->appendMatrix(numberRows2, 0, rowStart, column, element, |
2159 | checkDuplicates ? numberColumns_ : -1); |
2160 | } else { |
2161 | delete matrix_; |
2162 | matrix_ = new ClpPackedMatrix(matrix); |
2163 | } |
2164 | #ifndef SLIM_CLP |
2165 | } else { |
2166 | |
2167 | CoinBigIndex size = startPositive[numberColumns]; |
2168 | int * indices = new int[size]; |
2169 | modelObject.createPlusMinusOne(startPositive, startNegative, indices, |
2170 | associated); |
2171 | |
2172 | ClpPlusMinusOneMatrix * matrix = new ClpPlusMinusOneMatrix(); |
2173 | matrix->passInCopy(numberRows2, numberColumns, |
2174 | true, indices, startPositive, startNegative); |
2175 | delete matrix_; |
2176 | matrix_ = matrix; |
2177 | } |
2178 | |
2179 | if (modelObject.rowNames()->numberItems()) { |
2180 | const char *const * rowNames = modelObject.rowNames()->names(); |
2181 | copyRowNames(rowNames, numberRows, numberRows_); |
2182 | } |
2183 | #endif |
2184 | } |
2185 | if (rowLower != modelObject.rowLowerArray()) { |
2186 | delete [] rowLower; |
2187 | delete [] rowUpper; |
2188 | delete [] columnLower; |
2189 | delete [] columnUpper; |
2190 | delete [] objective; |
2191 | delete [] integerType; |
2192 | delete [] associated; |
2193 | if (numberErrors) |
2194 | handler_->message(CLP_BAD_STRING_VALUES, messages_) |
2195 | << numberErrors |
2196 | << CoinMessageEol; |
2197 | } |
2198 | return numberErrors; |
2199 | } else { |
2200 | |
2201 | handler_->message(CLP_COMPLICATED_MODEL, messages_) |
2202 | << modelObject.numberRows() |
2203 | << modelObject.numberColumns() |
2204 | << CoinMessageEol; |
2205 | return -1; |
2206 | } |
2207 | } |
2208 | #endif |
2209 | |
2210 | void |
2211 | ClpModel::addColumn(int numberInColumn, |
2212 | const int * rows, |
2213 | const double * elements, |
2214 | double columnLower, |
2215 | double columnUpper, |
2216 | double objective) |
2217 | { |
2218 | CoinBigIndex starts[2]; |
2219 | starts[0] = 0; |
2220 | starts[1] = numberInColumn; |
2221 | addColumns(1, &columnLower, &columnUpper, &objective, starts, rows, elements); |
2222 | } |
2223 | |
2224 | void |
2225 | ClpModel::addColumns(int number, const double * columnLower, |
2226 | const double * columnUpper, |
2227 | const double * objIn, |
2228 | const int * columnStarts, const int * rows, |
2229 | const double * elements) |
2230 | { |
2231 | |
2232 | if (number) { |
2233 | whatsChanged_ &= ~(1 + 2 + 4 + 64 + 128 + 256); |
2234 | int numberColumnsNow = numberColumns_; |
2235 | resize(numberRows_, numberColumnsNow + number); |
2236 | double * lower = columnLower_ + numberColumnsNow; |
2237 | double * upper = columnUpper_ + numberColumnsNow; |
2238 | double * obj = objective() + numberColumnsNow; |
2239 | int iColumn; |
2240 | if (columnLower) { |
2241 | for (iColumn = 0; iColumn < number; iColumn++) { |
2242 | double value = columnLower[iColumn]; |
2243 | if (value < -1.0e20) |
2244 | value = -COIN_DBL_MAX; |
2245 | lower[iColumn] = value; |
2246 | } |
2247 | } else { |
2248 | for (iColumn = 0; iColumn < number; iColumn++) { |
2249 | lower[iColumn] = 0.0; |
2250 | } |
2251 | } |
2252 | if (columnUpper) { |
2253 | for (iColumn = 0; iColumn < number; iColumn++) { |
2254 | double value = columnUpper[iColumn]; |
2255 | if (value > 1.0e20) |
2256 | value = COIN_DBL_MAX; |
2257 | upper[iColumn] = value; |
2258 | } |
2259 | } else { |
2260 | for (iColumn = 0; iColumn < number; iColumn++) { |
2261 | upper[iColumn] = COIN_DBL_MAX; |
2262 | } |
2263 | } |
2264 | if (objIn) { |
2265 | for (iColumn = 0; iColumn < number; iColumn++) { |
2266 | obj[iColumn] = objIn[iColumn]; |
2267 | } |
2268 | } else { |
2269 | for (iColumn = 0; iColumn < number; iColumn++) { |
2270 | obj[iColumn] = 0.0; |
2271 | } |
2272 | } |
2273 | |
2274 | |
2275 | delete rowCopy_; |
2276 | rowCopy_ = NULL; |
2277 | delete scaledMatrix_; |
2278 | scaledMatrix_ = NULL; |
2279 | if (!matrix_) |
2280 | createEmptyMatrix(); |
2281 | setRowScale(NULL); |
2282 | setColumnScale(NULL); |
2283 | #ifndef CLP_NO_STD |
2284 | if (lengthNames_) { |
2285 | columnNames_.resize(numberColumns_); |
2286 | } |
2287 | #endif |
2288 | |
2289 | matrix_->appendMatrix(number, 1, columnStarts, rows, elements); |
2290 | } |
2291 | } |
2292 | |
2293 | void |
2294 | ClpModel::addColumns(int number, const double * columnLower, |
2295 | const double * columnUpper, |
2296 | const double * objIn, |
2297 | const int * columnStarts, |
2298 | const int * columnLengths, const int * rows, |
2299 | const double * elements) |
2300 | { |
2301 | if (number) { |
2302 | CoinBigIndex numberElements = 0; |
2303 | int iColumn; |
2304 | for (iColumn = 0; iColumn < number; iColumn++) |
2305 | numberElements += columnLengths[iColumn]; |
2306 | int * newStarts = new int[number+1]; |
2307 | int * newIndex = new int[numberElements]; |
2308 | double * newElements = new double[numberElements]; |
2309 | numberElements = 0; |
2310 | newStarts[0] = 0; |
2311 | for (iColumn = 0; iColumn < number; iColumn++) { |
2312 | int iStart = columnStarts[iColumn]; |
2313 | int length = columnLengths[iColumn]; |
2314 | CoinMemcpyN(rows + iStart, length, newIndex + numberElements); |
2315 | CoinMemcpyN(elements + iStart, length, newElements + numberElements); |
2316 | numberElements += length; |
2317 | newStarts[iColumn+1] = numberElements; |
2318 | } |
2319 | addColumns(number, columnLower, columnUpper, objIn, |
2320 | newStarts, newIndex, newElements); |
2321 | delete [] newStarts; |
2322 | delete [] newIndex; |
2323 | delete [] newElements; |
2324 | } |
2325 | } |
2326 | #ifndef CLP_NO_VECTOR |
2327 | void |
2328 | ClpModel::addColumns(int number, const double * columnLower, |
2329 | const double * columnUpper, |
2330 | const double * objIn, |
2331 | const CoinPackedVectorBase * const * columns) |
2332 | { |
2333 | if (!number) |
2334 | return; |
2335 | whatsChanged_ &= ~(1 + 2 + 4 + 64 + 128 + 256); |
2336 | int numberColumnsNow = numberColumns_; |
2337 | resize(numberRows_, numberColumnsNow + number); |
2338 | double * lower = columnLower_ + numberColumnsNow; |
2339 | double * upper = columnUpper_ + numberColumnsNow; |
2340 | double * obj = objective() + numberColumnsNow; |
2341 | int iColumn; |
2342 | if (columnLower) { |
2343 | for (iColumn = 0; iColumn < number; iColumn++) { |
2344 | double value = columnLower[iColumn]; |
2345 | if (value < -1.0e20) |
2346 | value = -COIN_DBL_MAX; |
2347 | lower[iColumn] = value; |
2348 | } |
2349 | } else { |
2350 | for (iColumn = 0; iColumn < number; iColumn++) { |
2351 | lower[iColumn] = 0.0; |
2352 | } |
2353 | } |
2354 | if (columnUpper) { |
2355 | for (iColumn = 0; iColumn < number; iColumn++) { |
2356 | double value = columnUpper[iColumn]; |
2357 | if (value > 1.0e20) |
2358 | value = COIN_DBL_MAX; |
2359 | upper[iColumn] = value; |
2360 | } |
2361 | } else { |
2362 | for (iColumn = 0; iColumn < number; iColumn++) { |
2363 | upper[iColumn] = COIN_DBL_MAX; |
2364 | } |
2365 | } |
2366 | if (objIn) { |
2367 | for (iColumn = 0; iColumn < number; iColumn++) { |
2368 | obj[iColumn] = objIn[iColumn]; |
2369 | } |
2370 | } else { |
2371 | for (iColumn = 0; iColumn < number; iColumn++) { |
2372 | obj[iColumn] = 0.0; |
2373 | } |
2374 | } |
2375 | |
2376 | |
2377 | delete rowCopy_; |
2378 | rowCopy_ = NULL; |
2379 | delete scaledMatrix_; |
2380 | scaledMatrix_ = NULL; |
2381 | if (!matrix_) |
2382 | createEmptyMatrix(); |
2383 | if (columns) |
2384 | matrix_->appendCols(number, columns); |
2385 | setRowScale(NULL); |
2386 | setColumnScale(NULL); |
2387 | if (lengthNames_) { |
2388 | columnNames_.resize(numberColumns_); |
2389 | } |
2390 | } |
2391 | #endif |
2392 | #ifndef SLIM_CLP |
2393 | |
2394 | int |
2395 | ClpModel::addColumns(const CoinBuild & buildObject, bool tryPlusMinusOne, bool checkDuplicates) |
2396 | { |
2397 | CoinAssertHint (buildObject.type() == 1, "Looks as if both addRows and addCols being used"); |
2398 | int number = buildObject.numberColumns(); |
2399 | int numberErrors = 0; |
2400 | if (number) { |
2401 | CoinBigIndex size = 0; |
2402 | int maximumLength = 0; |
2403 | double * lower = new double [number]; |
2404 | double * upper = new double [number]; |
2405 | int iColumn; |
2406 | double * objective = new double [number]; |
2407 | if ((!matrix_ || !matrix_->getNumElements()) && tryPlusMinusOne) { |
2408 | |
2409 | for (iColumn = 0; iColumn < number; iColumn++) { |
2410 | const int * rows; |
2411 | const double * elements; |
2412 | int numberElements = buildObject.column(iColumn, lower[iColumn], |
2413 | upper[iColumn], objective[iColumn], |
2414 | rows, elements); |
2415 | maximumLength = CoinMax(maximumLength, numberElements); |
2416 | for (int i = 0; i < numberElements; i++) { |
2417 | |
2418 | if (elements[i]) { |
2419 | if (fabs(elements[i]) == 1.0) { |
2420 | size++; |
2421 | } else { |
2422 | |
2423 | tryPlusMinusOne = false; |
2424 | } |
2425 | } |
2426 | } |
2427 | if (!tryPlusMinusOne) |
2428 | break; |
2429 | } |
2430 | } else { |
2431 | |
2432 | tryPlusMinusOne = false; |
2433 | } |
2434 | if (!tryPlusMinusOne) { |
2435 | CoinBigIndex numberElements = buildObject.numberElements(); |
2436 | CoinBigIndex * starts = new CoinBigIndex [number+1]; |
2437 | int * row = new int[numberElements]; |
2438 | double * element = new double[numberElements]; |
2439 | starts[0] = 0; |
2440 | numberElements = 0; |
2441 | for (iColumn = 0; iColumn < number; iColumn++) { |
2442 | const int * rows; |
2443 | const double * elements; |
2444 | int numberElementsThis = buildObject.column(iColumn, lower[iColumn], upper[iColumn], |
2445 | objective[iColumn], rows, elements); |
2446 | CoinMemcpyN(rows, numberElementsThis, row + numberElements); |
2447 | CoinMemcpyN(elements, numberElementsThis, element + numberElements); |
2448 | numberElements += numberElementsThis; |
2449 | starts[iColumn+1] = numberElements; |
2450 | } |
2451 | addColumns(number, lower, upper, objective, NULL); |
2452 | |
2453 | matrix_->setDimensions(numberRows_, -1); |
2454 | numberErrors = matrix_->appendMatrix(number, 1, starts, row, element, |
2455 | checkDuplicates ? numberRows_ : -1); |
2456 | delete [] starts; |
2457 | delete [] row; |
2458 | delete [] element; |
2459 | } else { |
2460 | |
2461 | addColumns(number, lower, upper, objective, NULL); |
2462 | char * which = NULL; |
2463 | if (checkDuplicates) { |
2464 | which = new char[numberRows_]; |
2465 | CoinZeroN(which, numberRows_); |
2466 | } |
2467 | |
2468 | CoinBigIndex * startPositive = new CoinBigIndex [number+1]; |
2469 | CoinBigIndex * startNegative = new CoinBigIndex [number]; |
2470 | int * indices = new int [size]; |
2471 | int * neg = new int[maximumLength]; |
2472 | startPositive[0] = 0; |
2473 | size = 0; |
2474 | int maxRow = -1; |
2475 | for (iColumn = 0; iColumn < number; iColumn++) { |
2476 | const int * rows; |
2477 | const double * elements; |
2478 | int numberElements = buildObject.column(iColumn, lower[iColumn], |
2479 | upper[iColumn], objective[iColumn], |
2480 | rows, elements); |
2481 | int nNeg = 0; |
2482 | CoinBigIndex start = size; |
2483 | for (int i = 0; i < numberElements; i++) { |
2484 | int iRow = rows[i]; |
2485 | if (checkDuplicates) { |
2486 | if (iRow >= numberRows_) { |
2487 | if(which[iRow]) |
2488 | numberErrors++; |
2489 | else |
2490 | which[iRow] = 1; |
2491 | } else { |
2492 | numberErrors++; |
2493 | |
2494 | checkDuplicates = false; |
2495 | } |
2496 | } |
2497 | maxRow = CoinMax(maxRow, iRow); |
2498 | if (elements[i] == 1.0) { |
2499 | indices[size++] = iRow; |
2500 | } else if (elements[i] == -1.0) { |
2501 | neg[nNeg++] = iRow; |
2502 | } |
2503 | } |
2504 | std::sort(indices + start, indices + size); |
2505 | std::sort(neg, neg + nNeg); |
2506 | startNegative[iColumn] = size; |
2507 | CoinMemcpyN(neg, nNeg, indices + size); |
2508 | size += nNeg; |
2509 | startPositive[iColumn+1] = size; |
2510 | } |
2511 | delete [] neg; |
2512 | |
2513 | assert (maxRow + 1 <= numberRows_); |
2514 | |
2515 | delete matrix_; |
2516 | ClpPlusMinusOneMatrix * matrix = new ClpPlusMinusOneMatrix(); |
2517 | matrix->passInCopy(numberRows_, number, true, indices, startPositive, startNegative); |
2518 | matrix_ = matrix; |
2519 | delete [] which; |
2520 | } |
2521 | delete [] objective; |
2522 | delete [] lower; |
2523 | delete [] upper; |
2524 | } |
2525 | return 0; |
2526 | } |
2527 | #endif |
2528 | #ifndef SLIM_NOIO |
2529 | |
2530 | int |
2531 | ClpModel::addColumns( CoinModel & modelObject, bool tryPlusMinusOne, bool checkDuplicates) |
2532 | { |
2533 | if (modelObject.numberElements() == 0) |
2534 | return 0; |
2535 | bool goodState = true; |
2536 | if (modelObject.rowLowerArray()) { |
2537 | |
2538 | int numberRows2 = modelObject.numberRows(); |
2539 | const double * rowLower = modelObject.rowLowerArray(); |
2540 | const double * rowUpper = modelObject.rowUpperArray(); |
2541 | for (int i = 0; i < numberRows2; i++) { |
2542 | if (rowLower[i] != -COIN_DBL_MAX) |
2543 | goodState = false; |
2544 | if (rowUpper[i] != COIN_DBL_MAX) |
2545 | goodState = false; |
2546 | } |
2547 | } |
2548 | if (goodState) { |
2549 | |
2550 | int numberErrors = 0; |
2551 | |
2552 | double * rowLower = modelObject.rowLowerArray(); |
2553 | double * rowUpper = modelObject.rowUpperArray(); |
2554 | double * columnLower = modelObject.columnLowerArray(); |
2555 | double * columnUpper = modelObject.columnUpperArray(); |
2556 | double * objective = modelObject.objectiveArray(); |
2557 | int * integerType = modelObject.integerTypeArray(); |
2558 | double * associated = modelObject.associatedArray(); |
2559 | |
2560 | if (modelObject.stringsExist()) { |
2561 | numberErrors = modelObject.createArrays(rowLower, rowUpper, columnLower, columnUpper, |
2562 | objective, integerType, associated); |
2563 | } |
2564 | int numberColumns = numberColumns_; |
2565 | int numberColumns2 = modelObject.numberColumns(); |
2566 | if (numberColumns2 && !numberErrors) { |
2567 | CoinBigIndex * startPositive = NULL; |
2568 | CoinBigIndex * startNegative = NULL; |
2569 | if ((!matrix_ || !matrix_->getNumElements()) && !numberColumns && tryPlusMinusOne) { |
2570 | startPositive = new CoinBigIndex[numberColumns2+1]; |
2571 | startNegative = new CoinBigIndex[numberColumns2]; |
2572 | modelObject.countPlusMinusOne(startPositive, startNegative, associated); |
2573 | if (startPositive[0] < 0) { |
2574 | |
2575 | tryPlusMinusOne = false; |
2576 | delete [] startPositive; |
2577 | delete [] startNegative; |
2578 | } |
2579 | } else { |
2580 | |
2581 | tryPlusMinusOne = false; |
2582 | } |
2583 | assert (columnLower); |
2584 | addColumns(numberColumns2, columnLower, columnUpper, objective, NULL, NULL, NULL); |
2585 | #ifndef SLIM_CLP |
2586 | if (!tryPlusMinusOne) { |
2587 | #endif |
2588 | CoinPackedMatrix matrix; |
2589 | modelObject.createPackedMatrix(matrix, associated); |
2590 | assert (!matrix.getExtraGap()); |
2591 | if (matrix_->getNumCols()) { |
2592 | const int * row = matrix.getIndices(); |
2593 | |
2594 | const CoinBigIndex * columnStart = matrix.getVectorStarts(); |
2595 | const double * element = matrix.getElements(); |
2596 | |
2597 | matrix_->setDimensions(numberRows_, -1); |
2598 | numberErrors += matrix_->appendMatrix(numberColumns2, 1, columnStart, row, element, |
2599 | checkDuplicates ? numberRows_ : -1); |
2600 | } else { |
2601 | delete matrix_; |
2602 | matrix_ = new ClpPackedMatrix(matrix); |
2603 | } |
2604 | #ifndef SLIM_CLP |
2605 | } else { |
2606 | |
2607 | CoinBigIndex size = startPositive[numberColumns2]; |
2608 | int * indices = new int[size]; |
2609 | modelObject.createPlusMinusOne(startPositive, startNegative, indices, |
2610 | associated); |
2611 | |
2612 | ClpPlusMinusOneMatrix * matrix = new ClpPlusMinusOneMatrix(); |
2613 | matrix->passInCopy(numberRows_, numberColumns2, |
2614 | true, indices, startPositive, startNegative); |
2615 | delete matrix_; |
2616 | matrix_ = matrix; |
2617 | } |
2618 | #endif |
2619 | #ifndef CLP_NO_STD |
2620 | |
2621 | if (modelObject.columnNames()->numberItems()) { |
2622 | const char *const * columnNames = modelObject.columnNames()->names(); |
2623 | copyColumnNames(columnNames, numberColumns, numberColumns_); |
2624 | } |
2625 | #endif |
2626 | |
2627 | assert(integerType); |
2628 | for (int iColumn = 0; iColumn < numberColumns2; iColumn++) { |
2629 | if (integerType[iColumn]) |
2630 | setInteger(iColumn + numberColumns); |
2631 | } |
2632 | } |
2633 | if (columnLower != modelObject.columnLowerArray()) { |
2634 | delete [] rowLower; |
2635 | delete [] rowUpper; |
2636 | delete [] columnLower; |
2637 | delete [] columnUpper; |
2638 | delete [] objective; |
2639 | delete [] integerType; |
2640 | delete [] associated; |
2641 | if (numberErrors) |
2642 | handler_->message(CLP_BAD_STRING_VALUES, messages_) |
2643 | << numberErrors |
2644 | << CoinMessageEol; |
2645 | } |
2646 | return numberErrors; |
2647 | } else { |
2648 | |
2649 | handler_->message(CLP_COMPLICATED_MODEL, messages_) |
2650 | << modelObject.numberRows() |
2651 | << modelObject.numberColumns() |
2652 | << CoinMessageEol; |
2653 | return -1; |
2654 | } |
2655 | } |
2656 | #endif |
2657 | |
2658 | void |
2659 | ClpModel::chgRowLower(const double * rowLower) |
2660 | { |
2661 | int numberRows = numberRows_; |
2662 | int iRow; |
2663 | whatsChanged_ = 0; |
2664 | if (rowLower) { |
2665 | for (iRow = 0; iRow < numberRows; iRow++) { |
2666 | double value = rowLower[iRow]; |
2667 | if (value < -1.0e20) |
2668 | value = -COIN_DBL_MAX; |
2669 | rowLower_[iRow] = value; |
2670 | } |
2671 | } else { |
2672 | for (iRow = 0; iRow < numberRows; iRow++) { |
2673 | rowLower_[iRow] = -COIN_DBL_MAX; |
2674 | } |
2675 | } |
2676 | } |
2677 | |
2678 | void |
2679 | ClpModel::chgRowUpper(const double * rowUpper) |
2680 | { |
2681 | whatsChanged_ = 0; |
2682 | int numberRows = numberRows_; |
2683 | int iRow; |
2684 | if (rowUpper) { |
2685 | for (iRow = 0; iRow < numberRows; iRow++) { |
2686 | double value = rowUpper[iRow]; |
2687 | if (value > 1.0e20) |
2688 | value = COIN_DBL_MAX; |
2689 | rowUpper_[iRow] = value; |
2690 | } |
2691 | } else { |
2692 | for (iRow = 0; iRow < numberRows; iRow++) { |
2693 | rowUpper_[iRow] = COIN_DBL_MAX;; |
2694 | } |
2695 | } |
2696 | } |
2697 | |
2698 | void |
2699 | ClpModel::chgColumnLower(const double * columnLower) |
2700 | { |
2701 | whatsChanged_ = 0; |
2702 | int numberColumns = numberColumns_; |
2703 | int iColumn; |
2704 | if (columnLower) { |
2705 | for (iColumn = 0; iColumn < numberColumns; iColumn++) { |
2706 | double value = columnLower[iColumn]; |
2707 | if (value < -1.0e20) |
2708 | value = -COIN_DBL_MAX; |
2709 | columnLower_[iColumn] = value; |
2710 | } |
2711 | } else { |
2712 | for (iColumn = 0; iColumn < numberColumns; iColumn++) { |
2713 | columnLower_[iColumn] = 0.0; |
2714 | } |
2715 | } |
2716 | } |
2717 | |
2718 | void |
2719 | ClpModel::chgColumnUpper(const double * columnUpper) |
2720 | { |
2721 | whatsChanged_ = 0; |
2722 | int numberColumns = numberColumns_; |
2723 | int iColumn; |
2724 | if (columnUpper) { |
2725 | for (iColumn = 0; iColumn < numberColumns; iColumn++) { |
2726 | double value = columnUpper[iColumn]; |
2727 | if (value > 1.0e20) |
2728 | value = COIN_DBL_MAX; |
2729 | columnUpper_[iColumn] = value; |
2730 | } |
2731 | } else { |
2732 | for (iColumn = 0; iColumn < numberColumns; iColumn++) { |
2733 | columnUpper_[iColumn] = COIN_DBL_MAX;; |
2734 | } |
2735 | } |
2736 | } |
2737 | |
2738 | void |
2739 | ClpModel::chgObjCoefficients(const double * objIn) |
2740 | { |
2741 | whatsChanged_ = 0; |
2742 | double * obj = objective(); |
2743 | int numberColumns = numberColumns_; |
2744 | int iColumn; |
2745 | if (objIn) { |
2746 | for (iColumn = 0; iColumn < numberColumns; iColumn++) { |
2747 | obj[iColumn] = objIn[iColumn]; |
2748 | } |
2749 | } else { |
2750 | for (iColumn = 0; iColumn < numberColumns; iColumn++) { |
2751 | obj[iColumn] = 0.0; |
2752 | } |
2753 | } |
2754 | } |
2755 | |
2756 | double * |
2757 | ClpModel::infeasibilityRay(bool fullRay) const |
2758 | { |
2759 | double * array = NULL; |
2760 | if (problemStatus_ == 1 && ray_) { |
2761 | if (!fullRay) { |
2762 | array = ClpCopyOfArray(ray_, numberRows_); |
2763 | } else { |
2764 | array = new double [numberRows_+numberColumns_]; |
2765 | memcpy(array,ray_,numberRows_*sizeof(double)); |
2766 | memset(array+numberRows_,0,numberColumns_*sizeof(double)); |
2767 | transposeTimes(-1.0,array,array+numberRows_); |
2768 | } |
2769 | } |
2770 | return array; |
2771 | } |
2772 | double * |
2773 | ClpModel::unboundedRay() const |
2774 | { |
2775 | double * array = NULL; |
2776 | if (problemStatus_ == 2) |
2777 | array = ClpCopyOfArray(ray_, numberColumns_); |
2778 | return array; |
2779 | } |
2780 | void |
2781 | ClpModel::setMaximumIterations(int value) |
2782 | { |
2783 | if(value >= 0) |
2784 | intParam_[ClpMaxNumIteration] = value; |
2785 | } |
2786 | void |
2787 | ClpModel::setMaximumSeconds(double value) |
2788 | { |
2789 | if(value >= 0) |
2790 | dblParam_[ClpMaxSeconds] = value + CoinCpuTime(); |
2791 | else |
2792 | dblParam_[ClpMaxSeconds] = -1.0; |
2793 | } |
2794 | |
2795 | bool |
2796 | ClpModel::hitMaximumIterations() const |
2797 | { |
2798 | |
2799 | bool hitMax = (numberIterations_ >= intParam_[ClpMaxNumIteration]); |
2800 | if (dblParam_[ClpMaxSeconds] >= 0.0 && !hitMax) { |
2801 | hitMax = (CoinCpuTime() >= dblParam_[ClpMaxSeconds]); |
2802 | } |
2803 | return hitMax; |
2804 | } |
2805 | |
2806 | void |
2807 | ClpModel::onStopped() |
2808 | { |
2809 | if (problemStatus_ == 3) { |
2810 | secondaryStatus_ = 0; |
2811 | if (CoinCpuTime() >= dblParam_[ClpMaxSeconds] && dblParam_[ClpMaxSeconds] >= 0.0) |
2812 | secondaryStatus_ = 9; |
2813 | } |
2814 | } |
2815 | |
2816 | void |
2817 | ClpModel::passInMessageHandler(CoinMessageHandler * handler) |
2818 | { |
2819 | if (defaultHandler_) |
2820 | delete handler_; |
2821 | defaultHandler_ = false; |
2822 | handler_ = handler; |
2823 | } |
2824 | |
2825 | CoinMessageHandler * |
2826 | ClpModel::pushMessageHandler(CoinMessageHandler * handler, |
2827 | bool & oldDefault) |
2828 | { |
2829 | CoinMessageHandler * returnValue = handler_; |
2830 | oldDefault = defaultHandler_; |
2831 | defaultHandler_ = false; |
2832 | handler_ = handler; |
2833 | return returnValue; |
2834 | } |
2835 | |
2836 | void |
2837 | ClpModel::popMessageHandler(CoinMessageHandler * oldHandler, bool oldDefault) |
2838 | { |
2839 | if (defaultHandler_) |
2840 | delete handler_; |
2841 | defaultHandler_ = oldDefault; |
2842 | handler_ = oldHandler; |
2843 | } |
2844 | |
2845 | void |
2846 | ClpModel::setDefaultMessageHandler() |
2847 | { |
2848 | int logLevel = handler_->logLevel(); |
2849 | if (defaultHandler_) |
2850 | delete handler_; |
2851 | defaultHandler_ = true; |
2852 | handler_ = new CoinMessageHandler(); |
2853 | handler_->setLogLevel(logLevel); |
2854 | } |
2855 | |
2856 | void |
2857 | ClpModel::newLanguage(CoinMessages::Language language) |
2858 | { |
2859 | messages_ = ClpMessage(language); |
2860 | } |
2861 | #ifndef SLIM_NOIO |
2862 | |
2863 | int |
2864 | ClpModel::readMps(const char *fileName, |
2865 | bool keepNames, |
2866 | bool ignoreErrors) |
2867 | { |
2868 | if (!strcmp(fileName, "-") || !strcmp(fileName, "stdin")) { |
2869 | |
2870 | } else { |
2871 | std::string name = fileName; |
2872 | bool readable = fileCoinReadable(name); |
2873 | if (!readable) { |
2874 | handler_->message(CLP_UNABLE_OPEN, messages_) |
2875 | << fileName << CoinMessageEol; |
2876 | return -1; |
2877 | } |
2878 | } |
2879 | CoinMpsIO m; |
2880 | m.passInMessageHandler(handler_); |
2881 | *m.messagesPointer() = coinMessages(); |
2882 | bool savePrefix = m.messageHandler()->prefix(); |
2883 | m.messageHandler()->setPrefix(handler_->prefix()); |
2884 | m.setSmallElementValue(CoinMax(smallElement_, m.getSmallElementValue())); |
2885 | double time1 = CoinCpuTime(), time2; |
2886 | int status = 0; |
2887 | try { |
2888 | status = m.readMps(fileName, ""); |
2889 | } catch (CoinError e) { |
2890 | e.print(); |
2891 | status = -1; |
2892 | } |
2893 | m.messageHandler()->setPrefix(savePrefix); |
2894 | if (!status || (ignoreErrors && (status > 0 && status < 100000))) { |
2895 | loadProblem(*m.getMatrixByCol(), |
2896 | m.getColLower(), m.getColUpper(), |
2897 | m.getObjCoefficients(), |
2898 | m.getRowLower(), m.getRowUpper()); |
2899 | if (m.integerColumns()) { |
2900 | integerType_ = new char[numberColumns_]; |
2901 | CoinMemcpyN(m.integerColumns(), numberColumns_, integerType_); |
2902 | } else { |
2903 | integerType_ = NULL; |
2904 | } |
2905 | #ifndef SLIM_CLP |
2906 | |
2907 | if (m.reader()->whichSection ( ) == COIN_QUAD_SECTION ) { |
2908 | int * start = NULL; |
2909 | int * column = NULL; |
2910 | double * element = NULL; |
2911 | status = m.readQuadraticMps(NULL, start, column, element, 2); |
2912 | if (!status || ignoreErrors) |
2913 | loadQuadraticObjective(numberColumns_, start, column, element); |
2914 | delete [] start; |
2915 | delete [] column; |
2916 | delete [] element; |
2917 | } |
2918 | #endif |
2919 | #ifndef CLP_NO_STD |
2920 | |
2921 | setStrParam(ClpProbName, m.getProblemName()); |
2922 | |
2923 | if (keepNames) { |
2924 | unsigned int maxLength = 0; |
2925 | int iRow; |
2926 | rowNames_ = std::vector<std::string> (); |
2927 | columnNames_ = std::vector<std::string> (); |
2928 | rowNames_.reserve(numberRows_); |
2929 | for (iRow = 0; iRow < numberRows_; iRow++) { |
2930 | const char * name = m.rowName(iRow); |
2931 | maxLength = CoinMax(maxLength, static_cast<unsigned int> (strlen(name))); |
2932 | rowNames_.push_back(name); |
2933 | } |
2934 | |
2935 | int iColumn; |
2936 | columnNames_.reserve(numberColumns_); |
2937 | for (iColumn = 0; iColumn < numberColumns_; iColumn++) { |
2938 | const char * name = m.columnName(iColumn); |
2939 | maxLength = CoinMax(maxLength, static_cast<unsigned int> (strlen(name))); |
2940 | columnNames_.push_back(name); |
2941 | } |
2942 | lengthNames_ = static_cast<int> (maxLength); |
2943 | } else { |
2944 | lengthNames_ = 0; |
2945 | } |
2946 | #endif |
2947 | setDblParam(ClpObjOffset, m.objectiveOffset()); |
2948 | time2 = CoinCpuTime(); |
2949 | handler_->message(CLP_IMPORT_RESULT, messages_) |
2950 | << fileName |
2951 | << time2 - time1 << CoinMessageEol; |
2952 | } else { |
2953 | |
2954 | handler_->message(CLP_IMPORT_ERRORS, messages_) |
2955 | << status << fileName << CoinMessageEol; |
2956 | } |
2957 | |
2958 | return status; |
2959 | } |
2960 | |
2961 | int |
2962 | ClpModel::readGMPL(const char *fileName, const char * dataName, |
2963 | bool keepNames) |
2964 | { |
2965 | FILE *fp = fopen(fileName, "r"); |
2966 | if (fp) { |
2967 | |
2968 | fclose(fp); |
2969 | if (dataName) { |
2970 | fp = fopen(dataName, "r"); |
2971 | if (fp) { |
2972 | fclose(fp); |
2973 | } else { |
2974 | handler_->message(CLP_UNABLE_OPEN, messages_) |
2975 | << dataName << CoinMessageEol; |
2976 | return -1; |
2977 | } |
2978 | } |
2979 | } else { |
2980 | handler_->message(CLP_UNABLE_OPEN, messages_) |
2981 | << fileName << CoinMessageEol; |
2982 | return -1; |
2983 | } |
2984 | CoinMpsIO m; |
2985 | m.passInMessageHandler(handler_); |
2986 | *m.messagesPointer() = coinMessages(); |
2987 | bool savePrefix = m.messageHandler()->prefix(); |
2988 | m.messageHandler()->setPrefix(handler_->prefix()); |
2989 | double time1 = CoinCpuTime(), time2; |
2990 | int status = m.readGMPL(fileName, dataName, keepNames); |
2991 | m.messageHandler()->setPrefix(savePrefix); |
2992 | if (!status) { |
2993 | loadProblem(*m.getMatrixByCol(), |
2994 | m.getColLower(), m.getColUpper(), |
2995 | m.getObjCoefficients(), |
2996 | m.getRowLower(), m.getRowUpper()); |
2997 | if (m.integerColumns()) { |
2998 | integerType_ = new char[numberColumns_]; |
2999 | CoinMemcpyN(m.integerColumns(), numberColumns_, integerType_); |
3000 | } else { |
3001 | integerType_ = NULL; |
3002 | } |
3003 | #ifndef CLP_NO_STD |
3004 | |
3005 | setStrParam(ClpProbName, m.getProblemName()); |
3006 | |
3007 | if (keepNames) { |
3008 | unsigned int maxLength = 0; |
3009 | int iRow; |
3010 | rowNames_ = std::vector<std::string> (); |
3011 | columnNames_ = std::vector<std::string> (); |
3012 | rowNames_.reserve(numberRows_); |
3013 | for (iRow = 0; iRow < numberRows_; iRow++) { |
3014 | const char * name = m.rowName(iRow); |
3015 | maxLength = CoinMax(maxLength, static_cast<unsigned int> (strlen(name))); |
3016 | rowNames_.push_back(name); |
3017 | } |
3018 | |
3019 | int iColumn; |
3020 | columnNames_.reserve(numberColumns_); |
3021 | for (iColumn = 0; iColumn < numberColumns_; iColumn++) { |
3022 | const char * name = m.columnName(iColumn); |
3023 | maxLength = CoinMax(maxLength, static_cast<unsigned int> (strlen(name))); |
3024 | columnNames_.push_back(name); |
3025 | } |
3026 | lengthNames_ = static_cast<int> (maxLength); |
3027 | } else { |
3028 | lengthNames_ = 0; |
3029 | } |
3030 | #endif |
3031 | setDblParam(ClpObjOffset, m.objectiveOffset()); |
3032 | time2 = CoinCpuTime(); |
3033 | handler_->message(CLP_IMPORT_RESULT, messages_) |
3034 | << fileName |
3035 | << time2 - time1 << CoinMessageEol; |
3036 | } else { |
3037 | |
3038 | handler_->message(CLP_IMPORT_ERRORS, messages_) |
3039 | << status << fileName << CoinMessageEol; |
3040 | } |
3041 | return status; |
3042 | } |
3043 | #endif |
3044 | bool ClpModel::isPrimalObjectiveLimitReached() const |
3045 | { |
3046 | double limit = 0.0; |
3047 | getDblParam(ClpPrimalObjectiveLimit, limit); |
3048 | if (limit > 1e30) { |
3049 | |
3050 | return false; |
3051 | } |
3052 | |
3053 | const double obj = objectiveValue(); |
3054 | const double maxmin = optimizationDirection(); |
3055 | |
3056 | if (problemStatus_ == 0) |
3057 | return maxmin > 0 ? (obj < limit) : (-obj < limit) ; |
3058 | else if (problemStatus_ == 2) |
3059 | return true; |
3060 | else |
3061 | return false; |
3062 | } |
3063 | |
3064 | bool ClpModel::isDualObjectiveLimitReached() const |
3065 | { |
3066 | |
3067 | double limit = 0.0; |
3068 | getDblParam(ClpDualObjectiveLimit, limit); |
3069 | if (limit > 1e30) { |
3070 | |
3071 | return false; |
3072 | } |
3073 | |
3074 | const double obj = objectiveValue(); |
3075 | const double maxmin = optimizationDirection(); |
3076 | |
3077 | if (problemStatus_ == 0) |
3078 | return maxmin > 0 ? (obj > limit) : (-obj > limit) ; |
3079 | else if (problemStatus_ == 1) |
3080 | return true; |
3081 | else |
3082 | return false; |
3083 | |
3084 | } |
3085 | void |
3086 | ClpModel::copyInIntegerInformation(const char * information) |
3087 | { |
3088 | delete [] integerType_; |
3089 | if (information) { |
3090 | integerType_ = new char[numberColumns_]; |
3091 | CoinMemcpyN(information, numberColumns_, integerType_); |
3092 | } else { |
3093 | integerType_ = NULL; |
3094 | } |
3095 | } |
3096 | void |
3097 | ClpModel::setContinuous(int index) |
3098 | { |
3099 | |
3100 | if (integerType_) { |
3101 | #ifndef NDEBUG |
3102 | if (index < 0 || index >= numberColumns_) { |
3103 | indexError(index, "setContinuous"); |
3104 | } |
3105 | #endif |
3106 | integerType_[index] = 0; |
3107 | } |
3108 | } |
3109 | |
3110 | void |
3111 | ClpModel::setInteger(int index) |
3112 | { |
3113 | if (!integerType_) { |
3114 | integerType_ = new char[numberColumns_]; |
3115 | CoinZeroN ( integerType_, numberColumns_); |
3116 | } |
3117 | #ifndef NDEBUG |
3118 | if (index < 0 || index >= numberColumns_) { |
3119 | indexError(index, "setInteger"); |
3120 | } |
3121 | #endif |
3122 | integerType_[index] = 1; |
3123 | } |
3124 | |
3125 | bool |
3126 | ClpModel::isInteger(int index) const |
3127 | { |
3128 | if (!integerType_) { |
3129 | return false; |
3130 | } else { |
3131 | #ifndef NDEBUG |
3132 | if (index < 0 || index >= numberColumns_) { |
3133 | indexError(index, "isInteger"); |
3134 | } |
3135 | #endif |
3136 | return (integerType_[index] != 0); |
3137 | } |
3138 | } |
3139 | #ifndef CLP_NO_STD |
3140 | |
3141 | void |
3142 | ClpModel::dropNames() |
3143 | { |
3144 | lengthNames_ = 0; |
3145 | rowNames_ = std::vector<std::string> (); |
3146 | columnNames_ = std::vector<std::string> (); |
3147 | } |
3148 | #endif |
3149 | |
3150 | void |
3151 | ClpModel::deleteIntegerInformation() |
3152 | { |
3153 | delete [] integerType_; |
3154 | integerType_ = NULL; |
3155 | } |
3156 | |
3157 | |
3158 | unsigned char * |
3159 | ClpModel::statusCopy() const |
3160 | { |
3161 | return ClpCopyOfArray(status_, numberRows_ + numberColumns_); |
3162 | } |
3163 | |
3164 | void |
3165 | ClpModel::copyinStatus(const unsigned char * statusArray) |
3166 | { |
3167 | delete [] status_; |
3168 | if (statusArray) { |
3169 | status_ = new unsigned char [numberRows_+numberColumns_]; |
3170 | CoinMemcpyN(statusArray, (numberRows_ + numberColumns_), status_); |
3171 | } else { |
3172 | status_ = NULL; |
3173 | } |
3174 | } |
3175 | #ifndef SLIM_CLP |
3176 | |
3177 | void |
3178 | ClpModel::loadQuadraticObjective(const int numberColumns, const CoinBigIndex * start, |
3179 | const int * column, const double * element) |
3180 | { |
3181 | whatsChanged_ = 0; |
3182 | CoinAssert (numberColumns == numberColumns_); |
3183 | assert ((dynamic_cast< ClpLinearObjective*>(objective_))); |
3184 | double offset; |
3185 | ClpObjective * obj = new ClpQuadraticObjective(objective_->gradient(NULL, NULL, offset, false), |
3186 | numberColumns, |
3187 | start, column, element); |
3188 | delete objective_; |
3189 | objective_ = obj; |
3190 | |
3191 | } |
3192 | void |
3193 | ClpModel::loadQuadraticObjective ( const CoinPackedMatrix& matrix) |
3194 | { |
3195 | whatsChanged_ = 0; |
3196 | CoinAssert (matrix.getNumCols() == numberColumns_); |
3197 | assert ((dynamic_cast< ClpLinearObjective*>(objective_))); |
3198 | double offset; |
3199 | ClpQuadraticObjective * obj = |
3200 | new ClpQuadraticObjective(objective_->gradient(NULL, NULL, offset, false), |
3201 | numberColumns_, |
3202 | NULL, NULL, NULL); |
3203 | delete objective_; |
3204 | objective_ = obj; |
3205 | obj->loadQuadraticObjective(matrix); |
3206 | } |
3207 | |
3208 | void |
3209 | ClpModel::deleteQuadraticObjective() |
3210 | { |
3211 | whatsChanged_ = 0; |
3212 | ClpQuadraticObjective * obj = (dynamic_cast< ClpQuadraticObjective*>(objective_)); |
3213 | if (obj) |
3214 | obj->deleteQuadraticObjective(); |
3215 | } |
3216 | #endif |
3217 | void |
3218 | ClpModel::setObjective(ClpObjective * objective) |
3219 | { |
3220 | whatsChanged_ = 0; |
3221 | delete objective_; |
3222 | objective_ = objective->clone(); |
3223 | } |
3224 | |
3225 | double * whichDouble(double * array , int number, const int * which) |
3226 | { |
3227 | double * newArray = NULL; |
3228 | if (array && number) { |
3229 | int i ; |
3230 | newArray = new double[number]; |
3231 | for (i = 0; i < number; i++) |
3232 | newArray[i] = array[which[i]]; |
3233 | } |
3234 | return newArray; |
3235 | } |
3236 | char * whichChar(char * array , int number, const int * which) |
3237 | { |
3238 | char * newArray = NULL; |
3239 | if (array && number) { |
3240 | int i ; |
3241 | newArray = new char[number]; |
3242 | for (i = 0; i < number; i++) |
3243 | newArray[i] = array[which[i]]; |
3244 | } |
3245 | return newArray; |
3246 | } |
3247 | unsigned char * whichUnsignedChar(unsigned char * array , |
3248 | int number, const int * which) |
3249 | { |
3250 | unsigned char * newArray = NULL; |
3251 | if (array && number) { |
3252 | int i ; |
3253 | newArray = new unsigned char[number]; |
3254 | for (i = 0; i < number; i++) |
3255 | newArray[i] = array[which[i]]; |
3256 | } |
3257 | return newArray; |
3258 | } |
3259 | |
3260 | void |
3261 | ClpModel::replaceMatrix( ClpMatrixBase * matrix, bool deleteCurrent) |
3262 | { |
3263 | if (deleteCurrent) |
3264 | delete matrix_; |
3265 | matrix_ = matrix; |
3266 | whatsChanged_ = 0; |
3267 | } |
3268 | |
3269 | ClpModel::ClpModel ( const ClpModel * rhs, |
3270 | int numberRows, const int * whichRow, |
3271 | int numberColumns, const int * whichColumn, |
3272 | bool dropNames, bool dropIntegers) |
3273 | : specialOptions_(rhs->specialOptions_), |
3274 | maximumColumns_(-1), |
3275 | maximumRows_(-1), |
3276 | maximumInternalColumns_(-1), |
3277 | maximumInternalRows_(-1), |
3278 | savedRowScale_(NULL), |
3279 | savedColumnScale_(NULL) |
3280 | { |
3281 | defaultHandler_ = rhs->defaultHandler_; |
3282 | if (defaultHandler_) |
3283 | handler_ = new CoinMessageHandler(*rhs->handler_); |
3284 | else |
3285 | handler_ = rhs->handler_; |
3286 | eventHandler_ = rhs->eventHandler_->clone(); |
3287 | randomNumberGenerator_ = rhs->randomNumberGenerator_; |
3288 | messages_ = rhs->messages_; |
3289 | coinMessages_ = rhs->coinMessages_; |
3290 | maximumColumns_ = -1; |
3291 | maximumRows_ = -1; |
3292 | maximumInternalColumns_ = -1; |
3293 | maximumInternalRows_ = -1; |
3294 | savedRowScale_ = NULL; |
3295 | savedColumnScale_ = NULL; |
3296 | intParam_[ClpMaxNumIteration] = rhs->intParam_[ClpMaxNumIteration]; |
3297 | intParam_[ClpMaxNumIterationHotStart] = |
3298 | rhs->intParam_[ClpMaxNumIterationHotStart]; |
3299 | intParam_[ClpNameDiscipline] = rhs->intParam_[ClpNameDiscipline] ; |
3300 | |
3301 | dblParam_[ClpDualObjectiveLimit] = rhs->dblParam_[ClpDualObjectiveLimit]; |
3302 | dblParam_[ClpPrimalObjectiveLimit] = rhs->dblParam_[ClpPrimalObjectiveLimit]; |
3303 | dblParam_[ClpDualTolerance] = rhs->dblParam_[ClpDualTolerance]; |
3304 | dblParam_[ClpPrimalTolerance] = rhs->dblParam_[ClpPrimalTolerance]; |
3305 | dblParam_[ClpObjOffset] = rhs->dblParam_[ClpObjOffset]; |
3306 | dblParam_[ClpMaxSeconds] = rhs->dblParam_[ClpMaxSeconds]; |
3307 | dblParam_[ClpPresolveTolerance] = rhs->dblParam_[ClpPresolveTolerance]; |
3308 | #ifndef CLP_NO_STD |
3309 | strParam_[ClpProbName] = rhs->strParam_[ClpProbName]; |
3310 | #endif |
3311 | specialOptions_ = rhs->specialOptions_; |
3312 | optimizationDirection_ = rhs->optimizationDirection_; |
3313 | objectiveValue_ = rhs->objectiveValue_; |
3314 | smallElement_ = rhs->smallElement_; |
3315 | objectiveScale_ = rhs->objectiveScale_; |
3316 | rhsScale_ = rhs->rhsScale_; |
3317 | numberIterations_ = rhs->numberIterations_; |
3318 | solveType_ = rhs->solveType_; |
3319 | whatsChanged_ = 0; |
3320 | problemStatus_ = rhs->problemStatus_; |
3321 | secondaryStatus_ = rhs->secondaryStatus_; |
3322 | |
3323 | int numberBad = 0; |
3324 | int i; |
3325 | for (i = 0; i < numberRows; i++) |
3326 | if (whichRow[i] < 0 || whichRow[i] >= rhs->numberRows_) |
3327 | numberBad++; |
3328 | CoinAssertHint(!numberBad, "Bad row list for subproblem constructor"); |
3329 | numberBad = 0; |
3330 | for (i = 0; i < numberColumns; i++) |
3331 | if (whichColumn[i] < 0 || whichColumn[i] >= rhs->numberColumns_) |
3332 | numberBad++; |
3333 | CoinAssertHint(!numberBad, "Bad Column list for subproblem constructor"); |
3334 | numberRows_ = numberRows; |
3335 | numberColumns_ = numberColumns; |
3336 | userPointer_ = rhs->userPointer_; |
3337 | trustedUserPointer_ = rhs->trustedUserPointer_; |
3338 | numberThreads_ = 0; |
3339 | #ifndef CLP_NO_STD |
3340 | if (!dropNames) { |
3341 | unsigned int maxLength = 0; |
3342 | int iRow; |
3343 | rowNames_ = std::vector<std::string> (); |
3344 | columnNames_ = std::vector<std::string> (); |
3345 | rowNames_.reserve(numberRows_); |
3346 | for (iRow = 0; iRow < numberRows_; iRow++) { |
3347 | rowNames_.push_back(rhs->rowNames_[whichRow[iRow]]); |
3348 | maxLength = CoinMax(maxLength, static_cast<unsigned int> (strlen(rowNames_[iRow].c_str()))); |
3349 | } |
3350 | int iColumn; |
3351 | columnNames_.reserve(numberColumns_); |
3352 | for (iColumn = 0; iColumn < numberColumns_; iColumn++) { |
3353 | columnNames_.push_back(rhs->columnNames_[whichColumn[iColumn]]); |
3354 | maxLength = CoinMax(maxLength, static_cast<unsigned int> (strlen(columnNames_[iColumn].c_str()))); |
3355 | } |
3356 | lengthNames_ = static_cast<int> (maxLength); |
3357 | } else { |
3358 | lengthNames_ = 0; |
3359 | rowNames_ = std::vector<std::string> (); |
3360 | columnNames_ = std::vector<std::string> (); |
3361 | } |
3362 | #endif |
3363 | if (rhs->integerType_ && !dropIntegers) { |
3364 | integerType_ = whichChar(rhs->integerType_, numberColumns, whichColumn); |
3365 | } else { |
3366 | integerType_ = NULL; |
3367 | } |
3368 | if (rhs->rowActivity_) { |
3369 | rowActivity_ = whichDouble(rhs->rowActivity_, numberRows, whichRow); |
3370 | dual_ = whichDouble(rhs->dual_, numberRows, whichRow); |
3371 | columnActivity_ = whichDouble(rhs->columnActivity_, numberColumns, |
3372 | whichColumn); |
3373 | reducedCost_ = whichDouble(rhs->reducedCost_, numberColumns, |
3374 | whichColumn); |
3375 | } else { |
3376 | rowActivity_ = NULL; |
3377 | columnActivity_ = NULL; |
3378 | dual_ = NULL; |
3379 | reducedCost_ = NULL; |
3380 | } |
3381 | rowLower_ = whichDouble(rhs->rowLower_, numberRows, whichRow); |
3382 | rowUpper_ = whichDouble(rhs->rowUpper_, numberRows, whichRow); |
3383 | columnLower_ = whichDouble(rhs->columnLower_, numberColumns, whichColumn); |
3384 | columnUpper_ = whichDouble(rhs->columnUpper_, numberColumns, whichColumn); |
3385 | if (rhs->objective_) |
3386 | objective_ = rhs->objective_->subsetClone(numberColumns, whichColumn); |
3387 | else |
3388 | objective_ = NULL; |
3389 | rowObjective_ = whichDouble(rhs->rowObjective_, numberRows, whichRow); |
3390 | |
3391 | if (rhs->status_) { |
3392 | status_ = new unsigned char[numberColumns_+numberRows_]; |
3393 | unsigned char * rowStatus = whichUnsignedChar(rhs->status_ + rhs->numberColumns_, |
3394 | numberRows_, whichRow); |
3395 | unsigned char * columnStatus = whichUnsignedChar(rhs->status_, |
3396 | numberColumns_, whichColumn); |
3397 | CoinMemcpyN(rowStatus, numberRows_, status_ + numberColumns_); |
3398 | delete [] rowStatus; |
3399 | CoinMemcpyN(columnStatus, numberColumns_, status_); |
3400 | delete [] columnStatus; |
3401 | } else { |
3402 | status_=NULL; |
3403 | } |
3404 | ray_ = NULL; |
3405 | if (problemStatus_ == 1) |
3406 | ray_ = whichDouble (rhs->ray_, numberRows, whichRow); |
3407 | else if (problemStatus_ == 2) |
3408 | ray_ = whichDouble (rhs->ray_, numberColumns, whichColumn); |
3409 | rowScale_ = NULL; |
3410 | columnScale_ = NULL; |
3411 | inverseRowScale_ = NULL; |
3412 | inverseColumnScale_ = NULL; |
3413 | scalingFlag_ = rhs->scalingFlag_; |
3414 | rowCopy_ = NULL; |
3415 | scaledMatrix_ = NULL; |
3416 | matrix_ = NULL; |
3417 | if (rhs->matrix_) { |
3418 | matrix_ = rhs->matrix_->subsetClone(numberRows, whichRow, |
3419 | numberColumns, whichColumn); |
3420 | } |
3421 | randomNumberGenerator_ = rhs->randomNumberGenerator_; |
3422 | } |
3423 | #ifndef CLP_NO_STD |
3424 | |
3425 | void |
3426 | ClpModel::copyNames(const std::vector<std::string> & rowNames, |
3427 | const std::vector<std::string> & columnNames) |
3428 | { |
3429 | unsigned int maxLength = 0; |
3430 | int iRow; |
3431 | rowNames_ = std::vector<std::string> (); |
3432 | columnNames_ = std::vector<std::string> (); |
3433 | rowNames_.reserve(numberRows_); |
3434 | for (iRow = 0; iRow < numberRows_; iRow++) { |
3435 | rowNames_.push_back(rowNames[iRow]); |
3436 | maxLength = CoinMax(maxLength, static_cast<unsigned int> (strlen(rowNames_[iRow].c_str()))); |
3437 | } |
3438 | int iColumn; |
3439 | columnNames_.reserve(numberColumns_); |
3440 | for (iColumn = 0; iColumn < numberColumns_; iColumn++) { |
3441 | columnNames_.push_back(columnNames[iColumn]); |
3442 | maxLength = CoinMax(maxLength, static_cast<unsigned int> (strlen(columnNames_[iColumn].c_str()))); |
3443 | } |
3444 | lengthNames_ = static_cast<int> (maxLength); |
3445 | } |
3446 | |
3447 | std::string |
3448 | ClpModel::getRowName(int iRow) const |
3449 | { |
3450 | #ifndef NDEBUG |
3451 | if (iRow < 0 || iRow >= numberRows_) { |
3452 | indexError(iRow, "getRowName"); |
3453 | } |
3454 | #endif |
3455 | int size = static_cast<int>(rowNames_.size()); |
3456 | if (size > iRow) { |
3457 | return rowNames_[iRow]; |
3458 | } else { |
3459 | char name[9]; |
3460 | sprintf(name, "R%7.7d", iRow); |
3461 | std::string rowName(name); |
3462 | return rowName; |
3463 | } |
3464 | } |
3465 | |
3466 | void |
3467 | ClpModel::setRowName(int iRow, std::string &name) |
3468 | { |
3469 | #ifndef NDEBUG |
3470 | if (iRow < 0 || iRow >= numberRows_) { |
3471 | indexError(iRow, "setRowName"); |
3472 | } |
3473 | #endif |
3474 | unsigned int maxLength = lengthNames_; |
3475 | int size = static_cast<int>(rowNames_.size()); |
3476 | if (size <= iRow) |
3477 | rowNames_.resize(iRow + 1); |
3478 | rowNames_[iRow] = name; |
3479 | maxLength = CoinMax(maxLength, static_cast<unsigned int> (strlen(name.c_str()))); |
3480 | |
3481 | lengthNames_ = static_cast<int> (maxLength); |
3482 | } |
3483 | |
3484 | std::string |
3485 | ClpModel::getColumnName(int iColumn) const |
3486 | { |
3487 | #ifndef NDEBUG |
3488 | if (iColumn < 0 || iColumn >= numberColumns_) { |
3489 | indexError(iColumn, "getColumnName"); |
3490 | } |
3491 | #endif |
3492 | int size = static_cast<int>(columnNames_.size()); |
3493 | if (size > iColumn) { |
3494 | return columnNames_[iColumn]; |
3495 | } else { |
3496 | char name[9]; |
3497 | sprintf(name, "C%7.7d", iColumn); |
3498 | std::string columnName(name); |
3499 | return columnName; |
3500 | } |
3501 | } |
3502 | |
3503 | void |
3504 | ClpModel::setColumnName(int iColumn, std::string &name) |
3505 | { |
3506 | #ifndef NDEBUG |
3507 | if (iColumn < 0 || iColumn >= numberColumns_) { |
3508 | indexError(iColumn, "setColumnName"); |
3509 | } |
3510 | #endif |
3511 | unsigned int maxLength = lengthNames_; |
3512 | int size = static_cast<int>(columnNames_.size()); |
3513 | if (size <= iColumn) |
3514 | columnNames_.resize(iColumn + 1); |
3515 | columnNames_[iColumn] = name; |
3516 | maxLength = CoinMax(maxLength, static_cast<unsigned int> (strlen(name.c_str()))); |
3517 | |
3518 | lengthNames_ = static_cast<int> (maxLength); |
3519 | } |
3520 | |
3521 | void |
3522 | ClpModel::copyRowNames(const std::vector<std::string> & rowNames, int first, int last) |
3523 | { |
3524 | |
3525 | if (!lengthNames_&&numberColumns_) { |
3526 | lengthNames_=8; |
3527 | copyColumnNames(NULL,0,numberColumns_); |
3528 | } |
3529 | unsigned int maxLength = lengthNames_; |
3530 | int size = static_cast<int>(rowNames_.size()); |
3531 | if (size != numberRows_) |
3532 | rowNames_.resize(numberRows_); |
3533 | int iRow; |
3534 | for (iRow = first; iRow < last; iRow++) { |
3535 | rowNames_[iRow] = rowNames[iRow-first]; |
3536 | maxLength = CoinMax(maxLength, static_cast<unsigned int> (strlen(rowNames_[iRow-first].c_str()))); |
3537 | } |
3538 | |
3539 | lengthNames_ = static_cast<int> (maxLength); |
3540 | } |
3541 | |
3542 | void |
3543 | ClpModel::copyColumnNames(const std::vector<std::string> & columnNames, int first, int last) |
3544 | { |
3545 | |
3546 | if (!lengthNames_&&numberRows_) { |
3547 | lengthNames_=8; |
3548 | copyRowNames(NULL,0,numberRows_); |
3549 | } |
3550 | unsigned int maxLength = lengthNames_; |
3551 | int size = static_cast<int>(columnNames_.size()); |
3552 | if (size != numberColumns_) |
3553 | columnNames_.resize(numberColumns_); |
3554 | int iColumn; |
3555 | for (iColumn = first; iColumn < last; iColumn++) { |
3556 | columnNames_[iColumn] = columnNames[iColumn-first]; |
3557 | maxLength = CoinMax(maxLength, static_cast<unsigned int> (strlen(columnNames_[iColumn-first].c_str()))); |
3558 | } |
3559 | |
3560 | lengthNames_ = static_cast<int> (maxLength); |
3561 | } |
3562 | |
3563 | void |
3564 | ClpModel::copyRowNames(const char * const * rowNames, int first, int last) |
3565 | { |
3566 | |
3567 | if (!lengthNames_&&numberColumns_) { |
3568 | lengthNames_=8; |
3569 | copyColumnNames(NULL,0,numberColumns_); |
3570 | } |
3571 | unsigned int maxLength = lengthNames_; |
3572 | int size = static_cast<int>(rowNames_.size()); |
3573 | if (size != numberRows_) |
3574 | rowNames_.resize(numberRows_); |
3575 | int iRow; |
3576 | for (iRow = first; iRow < last; iRow++) { |
3577 | if (rowNames && rowNames[iRow-first] && strlen(rowNames[iRow-first])) { |
3578 | rowNames_[iRow] = rowNames[iRow-first]; |
3579 | maxLength = CoinMax(maxLength, static_cast<unsigned int> (strlen(rowNames[iRow-first]))); |
3580 | } else { |
3581 | maxLength = CoinMax(maxLength, static_cast<unsigned int> (8)); |
3582 | char name[9]; |
3583 | sprintf(name, "R%7.7d", iRow); |
3584 | rowNames_[iRow] = name; |
3585 | } |
3586 | } |
3587 | |
3588 | lengthNames_ = static_cast<int> (maxLength); |
3589 | } |
3590 | |
3591 | void |
3592 | ClpModel::copyColumnNames(const char * const * columnNames, int first, int last) |
3593 | { |
3594 | |
3595 | if (!lengthNames_&&numberRows_) { |
3596 | lengthNames_=8; |
3597 | copyRowNames(NULL,0,numberRows_); |
3598 | } |
3599 | unsigned int maxLength = lengthNames_; |
3600 | int size = static_cast<int>(columnNames_.size()); |
3601 | if (size != numberColumns_) |
3602 | columnNames_.resize(numberColumns_); |
3603 | int iColumn; |
3604 | for (iColumn = first; iColumn < last; iColumn++) { |
3605 | if (columnNames && columnNames[iColumn-first] && strlen(columnNames[iColumn-first])) { |
3606 | columnNames_[iColumn] = columnNames[iColumn-first]; |
3607 | maxLength = CoinMax(maxLength, static_cast<unsigned int> (strlen(columnNames[iColumn-first]))); |
3608 | } else { |
3609 | maxLength = CoinMax(maxLength, static_cast<unsigned int> (8)); |
3610 | char name[9]; |
3611 | sprintf(name, "C%7.7d", iColumn); |
3612 | columnNames_[iColumn] = name; |
3613 | } |
3614 | } |
3615 | |
3616 | lengthNames_ = static_cast<int> (maxLength); |
3617 | } |
3618 | #endif |
3619 | |
3620 | void |
3621 | ClpModel::setPrimalObjectiveLimit(double value) |
3622 | { |
3623 | dblParam_[ClpPrimalObjectiveLimit] = value; |
3624 | } |
3625 | |
3626 | void |
3627 | ClpModel::setDualObjectiveLimit(double value) |
3628 | { |
3629 | dblParam_[ClpDualObjectiveLimit] = value; |
3630 | } |
3631 | |
3632 | void |
3633 | ClpModel::setObjectiveOffset(double value) |
3634 | { |
3635 | dblParam_[ClpObjOffset] = value; |
3636 | } |
3637 | |
3638 | int ClpModel::emptyProblem(int * infeasNumber, double * infeasSum, bool printMessage) |
3639 | { |
3640 | secondaryStatus_ = 6; |
3641 | if (printMessage) |
3642 | handler_->message(CLP_EMPTY_PROBLEM, messages_) |
3643 | << numberRows_ |
3644 | << numberColumns_ |
3645 | << 0 |
3646 | << CoinMessageEol; |
3647 | int returnCode = 0; |
3648 | if (numberRows_ || numberColumns_) { |
3649 | if (!status_) { |
3650 | status_ = new unsigned char[numberRows_+numberColumns_]; |
3651 | CoinZeroN(status_, numberRows_ + numberColumns_); |
3652 | } |
3653 | } |
3654 | |
3655 | |
3656 | int numberPrimalInfeasibilities = 0; |
3657 | double sumPrimalInfeasibilities = 0.0; |
3658 | int numberDualInfeasibilities = 0; |
3659 | double sumDualInfeasibilities = 0.0; |
3660 | if (numberRows_) { |
3661 | for (int i = 0; i < numberRows_; i++) { |
3662 | dual_[i] = 0.0; |
3663 | if (rowLower_[i] <= rowUpper_[i]) { |
3664 | if (rowLower_[i] > -1.0e30 || rowUpper_[i] < 1.0e30) { |
3665 | if (rowLower_[i] <= 0.0 && rowUpper_[i] >= 0.0) { |
3666 | if (fabs(rowLower_[i]) < fabs(rowUpper_[i])) |
3667 | rowActivity_[i] = rowLower_[i]; |
3668 | else |
3669 | rowActivity_[i] = rowUpper_[i]; |
3670 | } else { |
3671 | rowActivity_[i] = 0.0; |
3672 | numberPrimalInfeasibilities++; |
3673 | sumPrimalInfeasibilities += CoinMin(rowLower_[i], -rowUpper_[i]); |
3674 | returnCode = 1; |
3675 | } |
3676 | } else { |
3677 | rowActivity_[i] = 0.0; |
3678 | } |
3679 | } else { |
3680 | rowActivity_[i] = 0.0; |
3681 | numberPrimalInfeasibilities++; |
3682 | sumPrimalInfeasibilities += rowLower_[i] - rowUpper_[i]; |
3683 | returnCode = 1; |
3684 | } |
3685 | status_[i+numberColumns_] = 1; |
3686 | } |
3687 | } |
3688 | objectiveValue_ = 0.0; |
3689 | if (numberColumns_) { |
3690 | const double * cost = objective(); |
3691 | for (int i = 0; i < numberColumns_; i++) { |
3692 | reducedCost_[i] = cost[i]; |
3693 | double objValue = cost[i] * optimizationDirection_; |
3694 | if (columnLower_[i] <= columnUpper_[i]) { |
3695 | if (columnLower_[i] > -1.0e30 || columnUpper_[i] < 1.0e30) { |
3696 | if (!objValue) { |
3697 | if (fabs(columnLower_[i]) < fabs(columnUpper_[i])) { |
3698 | columnActivity_[i] = columnLower_[i]; |
3699 | status_[i] = 3; |
3700 | } else { |
3701 | columnActivity_[i] = columnUpper_[i]; |
3702 | status_[i] = 2; |
3703 | } |
3704 | } else if (objValue > 0.0) { |
3705 | if (columnLower_[i] > -1.0e30) { |
3706 | columnActivity_[i] = columnLower_[i]; |
3707 | status_[i] = 3; |
3708 | } else { |
3709 | columnActivity_[i] = columnUpper_[i]; |
3710 | status_[i] = 2; |
3711 | numberDualInfeasibilities++;; |
3712 | sumDualInfeasibilities += fabs(objValue); |
3713 | returnCode |= 2; |
3714 | } |
3715 | objectiveValue_ += columnActivity_[i] * objValue; |
3716 | } else { |
3717 | if (columnUpper_[i] < 1.0e30) { |
3718 | columnActivity_[i] = columnUpper_[i]; |
3719 | status_[i] = 2; |
3720 | } else { |
3721 | columnActivity_[i] = columnLower_[i]; |
3722 | status_[i] = 3; |
3723 | numberDualInfeasibilities++;; |
3724 | sumDualInfeasibilities += fabs(objValue); |
3725 | returnCode |= 2; |
3726 | } |
3727 | objectiveValue_ += columnActivity_[i] * objValue; |
3728 | } |
3729 | } else { |
3730 | columnActivity_[i] = 0.0; |
3731 | if (objValue) { |
3732 | numberDualInfeasibilities++;; |
3733 | sumDualInfeasibilities += fabs(objValue); |
3734 | returnCode |= 2; |
3735 | } |
3736 | status_[i] = 0; |
3737 | } |
3738 | } else { |
3739 | if (fabs(columnLower_[i]) < fabs(columnUpper_[i])) { |
3740 | columnActivity_[i] = columnLower_[i]; |
3741 | status_[i] = 3; |
3742 | } else { |
3743 | columnActivity_[i] = columnUpper_[i]; |
3744 | status_[i] = 2; |
3745 | } |
3746 | numberPrimalInfeasibilities++; |
3747 | sumPrimalInfeasibilities += columnLower_[i] - columnUpper_[i]; |
3748 | returnCode |= 1; |
3749 | } |
3750 | } |
3751 | } |
3752 | objectiveValue_ /= (objectiveScale_ * rhsScale_); |
3753 | if (infeasNumber) { |
3754 | infeasNumber[0] = numberDualInfeasibilities; |
3755 | infeasSum[0] = sumDualInfeasibilities; |
3756 | infeasNumber[1] = numberPrimalInfeasibilities; |
3757 | infeasSum[1] = sumPrimalInfeasibilities; |
3758 | } |
3759 | if (returnCode == 3) |
3760 | returnCode = 4; |
3761 | return returnCode; |
3762 | } |
3763 | #ifndef SLIM_NOIO |
3764 | |
3765 | |
3766 | |
3767 | |
3768 | |
3769 | |
3770 | |
3771 | |
3772 | |
3773 | |
3774 | |
3775 | |
3776 | int |
3777 | ClpModel::writeMps(const char *filename, |
3778 | int formatType, int numberAcross, |
3779 | double objSense) const |
3780 | { |
3781 | matrix_->setDimensions(numberRows_, numberColumns_); |
3782 | |
3783 | |
3784 | double * objective = new double[numberColumns_]; |
3785 | CoinMemcpyN(getObjCoefficients(), numberColumns_, objective); |
3786 | if (objSense * getObjSense() < 0.0) { |
3787 | for (int i = 0; i < numberColumns_; ++i) |
3788 | objective [i] = - objective[i]; |
3789 | } |
3790 | |
3791 | const char * const * const rowNames = rowNamesAsChar(); |
3792 | const char * const * const columnNames = columnNamesAsChar(); |
3793 | CoinMpsIO writer; |
3794 | writer.passInMessageHandler(handler_); |
3795 | *writer.messagesPointer() = coinMessages(); |
3796 | writer.setMpsData(*(matrix_->getPackedMatrix()), COIN_DBL_MAX, |
3797 | getColLower(), getColUpper(), |
3798 | objective, |
3799 | reinterpret_cast<const char*> (NULL) , |
3800 | getRowLower(), getRowUpper(), |
3801 | columnNames, rowNames); |
3802 | |
3803 | writer.copyInIntegerInformation(integerInformation()); |
3804 | writer.setObjectiveOffset(objectiveOffset()); |
3805 | delete [] objective; |
3806 | CoinPackedMatrix * quadratic = NULL; |
3807 | #ifndef SLIM_CLP |
3808 | |
3809 | #ifndef NO_RTTI |
3810 | ClpQuadraticObjective * quadraticObj = (dynamic_cast< ClpQuadraticObjective*>(objective_)); |
3811 | #else |
3812 | ClpQuadraticObjective * quadraticObj = NULL; |
3813 | if (objective_->type() == 2) |
3814 | quadraticObj = (static_cast< ClpQuadraticObjective*>(objective_)); |
3815 | #endif |
3816 | if (quadraticObj) |
3817 | quadratic = quadraticObj->quadraticObjective(); |
3818 | #endif |
3819 | int returnCode = writer.writeMps(filename, 0 , formatType, numberAcross, |
3820 | quadratic); |
3821 | if (rowNames) { |
3822 | deleteNamesAsChar(rowNames, numberRows_ + 1); |
3823 | deleteNamesAsChar(columnNames, numberColumns_); |
3824 | } |
3825 | return returnCode; |
3826 | } |
3827 | #ifndef CLP_NO_STD |
3828 | |
3829 | const char * const * |
3830 | ClpModel::rowNamesAsChar() const |
3831 | { |
3832 | char ** rowNames = NULL; |
3833 | if (lengthNames()) { |
3834 | rowNames = new char * [numberRows_+1]; |
3835 | int numberNames = static_cast<int>(rowNames_.size()); |
3836 | numberNames = CoinMin(numberRows_, numberNames); |
3837 | int iRow; |
3838 | for (iRow = 0; iRow < numberNames; iRow++) { |
3839 | if (rowName(iRow) != "") { |
3840 | rowNames[iRow] = |
3841 | CoinStrdup(rowName(iRow).c_str()); |
3842 | } else { |
3843 | char name[9]; |
3844 | sprintf(name, "R%7.7d", iRow); |
3845 | rowNames[iRow] = CoinStrdup(name); |
3846 | } |
3847 | #ifdef STRIPBLANKS |
3848 | char * xx = rowNames[iRow]; |
3849 | int i; |
3850 | int length = strlen(xx); |
3851 | int n = 0; |
3852 | for (i = 0; i < length; i++) { |
3853 | if (xx[i] != ' ') |
3854 | xx[n++] = xx[i]; |
3855 | } |
3856 | xx[n] = '\0'; |
3857 | #endif |
3858 | } |
3859 | char name[9]; |
3860 | for ( ; iRow < numberRows_; iRow++) { |
3861 | sprintf(name, "R%7.7d", iRow); |
3862 | rowNames[iRow] = CoinStrdup(name); |
3863 | } |
3864 | rowNames[numberRows_] = CoinStrdup("OBJROW"); |
3865 | } |
3866 | return reinterpret_cast<const char * const *>(rowNames); |
3867 | } |
3868 | |
3869 | const char * const * |
3870 | ClpModel::columnNamesAsChar() const |
3871 | { |
3872 | char ** columnNames = NULL; |
3873 | if (lengthNames()) { |
3874 | columnNames = new char * [numberColumns_]; |
3875 | int numberNames = static_cast<int>(columnNames_.size()); |
3876 | numberNames = CoinMin(numberColumns_, numberNames); |
3877 | int iColumn; |
3878 | for (iColumn = 0; iColumn < numberNames; iColumn++) { |
3879 | if (columnName(iColumn) != "") { |
3880 | columnNames[iColumn] = |
3881 | CoinStrdup(columnName(iColumn).c_str()); |
3882 | } else { |
3883 | char name[9]; |
3884 | sprintf(name, "C%7.7d", iColumn); |
3885 | columnNames[iColumn] = CoinStrdup(name); |
3886 | } |
3887 | #ifdef STRIPBLANKS |
3888 | char * xx = columnNames[iColumn]; |
3889 | int i; |
3890 | int length = strlen(xx); |
3891 | int n = 0; |
3892 | for (i = 0; i < length; i++) { |
3893 | if (xx[i] != ' ') |
3894 | xx[n++] = xx[i]; |
3895 | } |
3896 | xx[n] = '\0'; |
3897 | #endif |
3898 | } |
3899 | char name[9]; |
3900 | for ( ; iColumn < numberColumns_; iColumn++) { |
3901 | sprintf(name, "C%7.7d", iColumn); |
3902 | columnNames[iColumn] = CoinStrdup(name); |
3903 | } |
3904 | } |
3905 | return (columnNames); |
3906 | } |
3907 | |
3908 | void |
3909 | ClpModel::deleteNamesAsChar(const char * const * names, int number) const |
3910 | { |
3911 | for (int i = 0; i < number; i++) { |
3912 | free(const_cast<char *>(names[i])); |
3913 | } |
3914 | delete [] const_cast<char **>(names); |
3915 | } |
3916 | #endif |
3917 | #endif |
3918 | |
3919 | void |
3920 | ClpModel::passInEventHandler(const ClpEventHandler * eventHandler) |
3921 | { |
3922 | delete eventHandler_; |
3923 | eventHandler_ = eventHandler->clone(); |
3924 | } |
3925 | |
3926 | void |
3927 | ClpModel::scaling(int mode) |
3928 | { |
3929 | |
3930 | if (mode != scalingFlag_) { |
3931 | whatsChanged_ &= ~(2 + 4 + 8); |
3932 | |
3933 | setClpScaledMatrix(NULL); |
3934 | } |
3935 | if (mode > 0 && mode < 6) { |
3936 | scalingFlag_ = mode; |
3937 | } else if (!mode) { |
3938 | scalingFlag_ = 0; |
3939 | setRowScale(NULL); |
3940 | setColumnScale(NULL); |
3941 | } |
3942 | } |
3943 | void |
3944 | ClpModel::times(double scalar, |
3945 | const double * x, double * y) const |
3946 | { |
3947 | if (!scaledMatrix_ || !rowScale_) { |
3948 | if (rowScale_) |
3949 | matrix_->times(scalar, x, y, rowScale_, columnScale_); |
3950 | else |
3951 | matrix_->times(scalar, x, y); |
3952 | } else { |
3953 | scaledMatrix_->times(scalar, x, y); |
3954 | } |
3955 | } |
3956 | void |
3957 | ClpModel::transposeTimes(double scalar, |
3958 | const double * x, double * y) const |
3959 | { |
3960 | if (!scaledMatrix_ || !rowScale_) { |
3961 | if (rowScale_) |
3962 | matrix_->transposeTimes(scalar, x, y, rowScale_, columnScale_, NULL); |
3963 | else |
3964 | matrix_->transposeTimes(scalar, x, y); |
3965 | } else { |
3966 | scaledMatrix_->transposeTimes(scalar, x, y); |
3967 | } |
3968 | } |
3969 | |
3970 | void |
3971 | ClpModel::gutsOfScaling() |
3972 | { |
3973 | int i; |
3974 | if (rowObjective_) { |
3975 | for (i = 0; i < numberRows_; i++) |
3976 | rowObjective_[i] /= rowScale_[i]; |
3977 | } |
3978 | for (i = 0; i < numberRows_; i++) { |
3979 | double multiplier = rowScale_[i]; |
3980 | double inverseMultiplier = 1.0 / multiplier; |
3981 | rowActivity_[i] *= multiplier; |
3982 | dual_[i] *= inverseMultiplier; |
3983 | if (rowLower_[i] > -1.0e30) |
3984 | rowLower_[i] *= multiplier; |
3985 | else |
3986 | rowLower_[i] = -COIN_DBL_MAX; |
3987 | if (rowUpper_[i] < 1.0e30) |
3988 | rowUpper_[i] *= multiplier; |
3989 | else |
3990 | rowUpper_[i] = COIN_DBL_MAX; |
3991 | } |
3992 | for (i = 0; i < numberColumns_; i++) { |
3993 | double multiplier = 1.0 * inverseColumnScale_[i]; |
3994 | columnActivity_[i] *= multiplier; |
3995 | reducedCost_[i] *= columnScale_[i]; |
3996 | if (columnLower_[i] > -1.0e30) |
3997 | columnLower_[i] *= multiplier; |
3998 | else |
3999 | columnLower_[i] = -COIN_DBL_MAX; |
4000 | if (columnUpper_[i] < 1.0e30) |
4001 | columnUpper_[i] *= multiplier; |
4002 | else |
4003 | columnUpper_[i] = COIN_DBL_MAX; |
4004 | |
4005 | } |
4006 | |
4007 | |
4008 | matrix_->reallyScale(rowScale_, columnScale_); |
4009 | objective_->reallyScale(columnScale_); |
4010 | } |
4011 | |
4012 | |
4013 | void |
4014 | ClpModel::unscale() |
4015 | { |
4016 | if (rowScale_) { |
4017 | int i; |
4018 | |
4019 | for (i = 0; i < numberRows_; i++) |
4020 | rowScale_[i] = 1.0 * inverseRowScale_[i]; |
4021 | for (i = 0; i < numberColumns_; i++) |
4022 | columnScale_[i] = 1.0 * inverseColumnScale_[i]; |
4023 | gutsOfScaling(); |
4024 | } |
4025 | |
4026 | scalingFlag_ = 0; |
4027 | setRowScale(NULL); |
4028 | setColumnScale(NULL); |
4029 | } |
4030 | void |
4031 | ClpModel::setSpecialOptions(unsigned int value) |
4032 | { |
4033 | specialOptions_ = value; |
4034 | } |
4035 | |
4036 | |
4037 | CoinModel * |
4038 | ClpModel::createCoinModel() const |
4039 | { |
4040 | CoinModel * coinModel = new CoinModel(); |
4041 | CoinPackedMatrix matrixByRow; |
4042 | matrixByRow.setExtraGap(0.0); |
4043 | matrixByRow.setExtraMajor(0.0); |
4044 | matrixByRow.reverseOrderedCopyOf(*matrix()); |
4045 | coinModel->setObjectiveOffset(objectiveOffset()); |
4046 | coinModel->setProblemName(problemName().c_str()); |
4047 | |
4048 | |
4049 | const double * element = matrixByRow.getElements(); |
4050 | const int * column = matrixByRow.getIndices(); |
4051 | const CoinBigIndex * rowStart = matrixByRow.getVectorStarts(); |
4052 | const int * rowLength = matrixByRow.getVectorLengths(); |
4053 | int i; |
4054 | for (i = 0; i < numberRows_; i++) { |
4055 | coinModel->addRow(rowLength[i], column + rowStart[i], |
4056 | element + rowStart[i], rowLower_[i], rowUpper_[i]); |
4057 | } |
4058 | |
4059 | const double * objective = this->objective(); |
4060 | for (i = 0; i < numberColumns_; i++) { |
4061 | coinModel->setColumnBounds(i, columnLower_[i], columnUpper_[i]); |
4062 | coinModel->setColumnObjective(i, objective[i]); |
4063 | } |
4064 | for ( i = 0; i < numberColumns_; i++) { |
4065 | if (isInteger(i)) |
4066 | coinModel->setColumnIsInteger(i, true); |
4067 | } |
4068 | |
4069 | coinModel->zapRowNames(); |
4070 | coinModel->zapColumnNames(); |
4071 | for (i = 0; i < numberRows_; i++) { |
4072 | char temp[30]; |
4073 | strcpy(temp, rowName(i).c_str()); |
4074 | size_t length = strlen(temp); |
4075 | for (size_t j = 0; j < length; j++) { |
4076 | if (temp[j] == '-') |
4077 | temp[j] = '_'; |
4078 | } |
4079 | coinModel->setRowName(i, temp); |
4080 | } |
4081 | for (i = 0; i < numberColumns_; i++) { |
4082 | char temp[30]; |
4083 | strcpy(temp, columnName(i).c_str()); |
4084 | size_t length = strlen(temp); |
4085 | for (size_t j = 0; j < length; j++) { |
4086 | if (temp[j] == '-') |
4087 | temp[j] = '_'; |
4088 | } |
4089 | coinModel->setColumnName(i, temp); |
4090 | } |
4091 | ClpQuadraticObjective * obj = (dynamic_cast< ClpQuadraticObjective*>(objective_)); |
4092 | if (obj) { |
4093 | const CoinPackedMatrix * quadObj = obj->quadraticObjective(); |
4094 | |
4095 | const double * element = quadObj->getElements(); |
4096 | const int * row = quadObj->getIndices(); |
4097 | const CoinBigIndex * columnStart = quadObj->getVectorStarts(); |
4098 | const int * columnLength = quadObj->getVectorLengths(); |
4099 | for (i = 0; i < numberColumns_; i++) { |
4100 | int nels = columnLength[i]; |
4101 | if (nels) { |
4102 | CoinBigIndex start = columnStart[i]; |
4103 | double constant = coinModel->getColumnObjective(i); |
4104 | char temp[100000]; |
4105 | char temp2[30]; |
4106 | sprintf(temp, "%g", constant); |
4107 | for (CoinBigIndex k = start; k < start + nels; k++) { |
4108 | int kColumn = row[k]; |
4109 | double value = element[k]; |
4110 | #if 1 |
4111 | |
4112 | if (kColumn < i) |
4113 | continue; |
4114 | else if (kColumn == i) |
4115 | value *= 0.5; |
4116 | #endif |
4117 | if (value == 1.0) |
4118 | sprintf(temp2, "+%s", coinModel->getColumnName(kColumn)); |
4119 | else if (value == -1.0) |
4120 | sprintf(temp2, "-%s", coinModel->getColumnName(kColumn)); |
4121 | else if (value > 0.0) |
4122 | sprintf(temp2, "+%g*%s", value, coinModel->getColumnName(kColumn)); |
4123 | else |
4124 | sprintf(temp2, "%g*%s", value, coinModel->getColumnName(kColumn)); |
4125 | strcat(temp, temp2); |
4126 | assert (strlen(temp) < 100000); |
4127 | } |
4128 | coinModel->setObjective(i, temp); |
4129 | if (logLevel() > 2) |
4130 | printf("el for objective column %s is %s\n", coinModel->getColumnName(i), temp); |
4131 | } |
4132 | } |
4133 | } |
4134 | return coinModel; |
4135 | } |
4136 | |
4137 | void |
4138 | ClpModel::startPermanentArrays() |
4139 | { |
4140 | COIN_DETAIL_PRINT(printf("startperm a %d rows, %d maximum rows\n", |
4141 | numberRows_, maximumRows_)); |
4142 | if ((specialOptions_ & 65536) != 0) { |
4143 | if (numberRows_ > maximumRows_ || numberColumns_ > maximumColumns_) { |
4144 | if (numberRows_ > maximumRows_) { |
4145 | if (maximumRows_ > 0) |
4146 | maximumRows_ = numberRows_ + 10 + numberRows_ / 100; |
4147 | else |
4148 | maximumRows_ = numberRows_; |
4149 | } |
4150 | if (numberColumns_ > maximumColumns_) { |
4151 | if (maximumColumns_ > 0) |
4152 | maximumColumns_ = numberColumns_ + 10 + numberColumns_ / 100; |
4153 | else |
4154 | maximumColumns_ = numberColumns_; |
4155 | } |
4156 | |
4157 | resize(maximumRows_, maximumColumns_); |
4158 | COIN_DETAIL_PRINT(printf("startperm b %d rows, %d maximum rows\n", |
4159 | numberRows_, maximumRows_)); |
4160 | } else { |
4161 | return; |
4162 | } |
4163 | } else { |
4164 | specialOptions_ |= 65536; |
4165 | maximumRows_ = numberRows_; |
4166 | maximumColumns_ = numberColumns_; |
4167 | baseMatrix_ = *matrix(); |
4168 | baseMatrix_.cleanMatrix(); |
4169 | baseRowCopy_.setExtraGap(0.0); |
4170 | baseRowCopy_.setExtraMajor(0.0); |
4171 | baseRowCopy_.reverseOrderedCopyOf(baseMatrix_); |
4172 | COIN_DETAIL_PRINT(printf("startperm c %d rows, %d maximum rows\n", |
4173 | numberRows_, maximumRows_)); |
4174 | } |
4175 | } |
4176 | |
4177 | void |
4178 | ClpModel::stopPermanentArrays() |
4179 | { |
4180 | specialOptions_ &= ~65536; |
4181 | maximumRows_ = -1; |
4182 | maximumColumns_ = -1; |
4183 | if (rowScale_ != savedRowScale_) { |
4184 | delete [] savedRowScale_; |
4185 | delete [] savedColumnScale_; |
4186 | } |
4187 | savedRowScale_ = NULL; |
4188 | savedColumnScale_ = NULL; |
4189 | } |
4190 | |
4191 | void |
4192 | ClpModel::setNewRowCopy(ClpMatrixBase * newCopy) |
4193 | { |
4194 | delete rowCopy_; |
4195 | rowCopy_ = newCopy; |
4196 | } |
4197 | |
4198 | |
4199 | |
4200 | |
4201 | |
4202 | |
4203 | |
4204 | |
4205 | int |
4206 | ClpModel::findNetwork(char * rotate, double fractionNeeded) |
4207 | { |
4208 | int * mapping = new int [numberRows_]; |
4209 | |
4210 | CoinPackedMatrix * columnCopy = matrix(); |
4211 | |
4212 | CoinPackedMatrix * copy = new CoinPackedMatrix(); |
4213 | copy->setExtraGap(0.0); |
4214 | copy->setExtraMajor(0.0); |
4215 | copy->reverseOrderedCopyOf(*columnCopy); |
4216 | |
4217 | copy->cleanMatrix(); |
4218 | |
4219 | const int * columnIn = copy->getIndices(); |
4220 | const CoinBigIndex * rowStartIn = copy->getVectorStarts(); |
4221 | const int * rowLength = copy->getVectorLengths(); |
4222 | const double * elementByRowIn = copy->getElements(); |
4223 | int iRow, iColumn; |
4224 | int numberEligible = 0; |
4225 | int numberIn = 0; |
4226 | int numberElements = 0; |
4227 | for (iRow = 0; iRow < numberRows_; iRow++) { |
4228 | bool possible = true; |
4229 | mapping[iRow] = -1; |
4230 | rotate[iRow] = -1; |
4231 | for (CoinBigIndex j = rowStartIn[iRow]; j < rowStartIn[iRow] + rowLength[iRow]; j++) { |
4232 | |
4233 | double value = elementByRowIn[j]; |
4234 | if (fabs(value) != 1.0) { |
4235 | possible = false; |
4236 | break; |
4237 | } |
4238 | } |
4239 | if (rowLength[iRow] && possible) { |
4240 | mapping[iRow] = numberEligible; |
4241 | numberEligible++; |
4242 | numberElements += rowLength[iRow]; |
4243 | } |
4244 | } |
4245 | if (numberEligible < fractionNeeded * numberRows_) { |
4246 | delete [] mapping; |
4247 | delete copy; |
4248 | return 0; |
4249 | } |
4250 | |
4251 | int * eligible = new int [numberRows_]; |
4252 | int * column = new int [numberElements]; |
4253 | CoinBigIndex * rowStart = new CoinBigIndex [numberEligible+1]; |
4254 | char * elementByRow = new char [numberElements]; |
4255 | numberEligible = 0; |
4256 | numberElements = 0; |
4257 | rowStart[0] = 0; |
4258 | for (iRow = 0; iRow < numberRows_; iRow++) { |
4259 | if (mapping[iRow] < 0) |
4260 | continue; |
4261 | assert (numberEligible == mapping[iRow]); |
4262 | rotate[numberEligible] = 0; |
4263 | for (CoinBigIndex j = rowStartIn[iRow]; j < rowStartIn[iRow] + rowLength[iRow]; j++) { |
4264 | column[numberElements] = columnIn[j]; |
4265 | double value = elementByRowIn[j]; |
4266 | if (value == 1.0) |
4267 | elementByRow[numberElements++] = 1; |
4268 | else |
4269 | elementByRow[numberElements++] = -1; |
4270 | } |
4271 | numberEligible++; |
4272 | rowStart[numberEligible] = numberElements; |
4273 | } |
4274 | |
4275 | delete copy; |
4276 | const int * rowIn = columnCopy->getIndices(); |
4277 | const CoinBigIndex * columnStartIn = columnCopy->getVectorStarts(); |
4278 | const int * columnLengthIn = columnCopy->getVectorLengths(); |
4279 | const double * elementByColumnIn = columnCopy->getElements(); |
4280 | int * columnLength = new int [numberColumns_]; |
4281 | |
4282 | bool isNetworkAlready = true; |
4283 | bool trueNetwork = true; |
4284 | for (iColumn = 0; iColumn < numberColumns_; iColumn++) { |
4285 | double product = 1.0; |
4286 | int n = 0; |
4287 | for (CoinBigIndex j = columnStartIn[iColumn]; j < columnStartIn[iColumn] + columnLengthIn[iColumn]; j++) { |
4288 | iRow = mapping[rowIn[j]]; |
4289 | if (iRow >= 0) { |
4290 | n++; |
4291 | product *= elementByColumnIn[j]; |
4292 | } |
4293 | } |
4294 | if (n >= 2) { |
4295 | if (product != -1.0 || n > 2) |
4296 | isNetworkAlready = false; |
4297 | } else if (n == 1) { |
4298 | trueNetwork = false; |
4299 | } |
4300 | columnLength[iColumn] = n; |
4301 | } |
4302 | if (!isNetworkAlready) { |
4303 | |
4304 | double * count = new double [numberRows_]; |
4305 | int * which = new int [numberRows_]; |
4306 | int numberLast = -1; |
4307 | |
4308 | char * columnCount = new char[numberColumns_]; |
4309 | memset(columnCount, 0, numberColumns_); |
4310 | char * currentColumnCount = new char[numberColumns_]; |
4311 | |
4312 | while (numberIn > numberLast) { |
4313 | numberLast = numberIn; |
4314 | int numberLeft = 0; |
4315 | for (iRow = 0; iRow < numberEligible; iRow++) { |
4316 | if (rotate[iRow] == 0 && rowStart[iRow+1] > rowStart[iRow]) { |
4317 | which[numberLeft] = iRow; |
4318 | int merit = 0; |
4319 | bool OK = true; |
4320 | bool reflectionOK = true; |
4321 | for (CoinBigIndex j = rowStart[iRow]; j < rowStart[iRow+1]; j++) { |
4322 | iColumn = column[j]; |
4323 | int iCount = columnCount[iColumn]; |
4324 | int absCount = CoinAbs(iCount); |
4325 | if (absCount < 2) { |
4326 | merit = CoinMax(columnLength[iColumn] - absCount - 1, merit); |
4327 | if (elementByRow[j] == iCount) |
4328 | OK = false; |
4329 | else if (elementByRow[j] == -iCount) |
4330 | reflectionOK = false; |
4331 | } else { |
4332 | merit = -2; |
4333 | break; |
4334 | } |
4335 | } |
4336 | if (merit > -2 && (OK || reflectionOK) && |
4337 | (!OK || !reflectionOK || !numberIn)) { |
4338 | |
4339 | count[numberLeft++] = (rowStart[iRow+1] - rowStart[iRow] - 1) * |
4340 | (static_cast<double>(merit)); |
4341 | if (OK) |
4342 | rotate[iRow] = 0; |
4343 | else |
4344 | rotate[iRow] = 1; |
4345 | } else { |
4346 | |
4347 | rotate[iRow] = -1; |
4348 | } |
4349 | } |
4350 | } |
4351 | CoinSort_2(count, count + numberLeft, which); |
4352 | |
4353 | memset(currentColumnCount, 0, numberColumns_); |
4354 | for (iRow = 0; iRow < numberLeft; iRow++) { |
4355 | int jRow = which[iRow]; |
4356 | bool possible = true; |
4357 | for (int i = 0; i < numberIn; i++) { |
4358 | for (CoinBigIndex j = rowStart[jRow]; j < rowStart[jRow+1]; j++) { |
4359 | if (currentColumnCount[column[j]]) { |
4360 | possible = false; |
4361 | break; |
4362 | } |
4363 | } |
4364 | } |
4365 | if (possible) { |
4366 | rotate[jRow] = static_cast<char>(rotate[jRow] + 2); |
4367 | eligible[numberIn++] = jRow; |
4368 | char multiplier = static_cast<char>((rotate[jRow] == 2) ? 1 : -1); |
4369 | for (CoinBigIndex j = rowStart[jRow]; j < rowStart[jRow+1]; j++) { |
4370 | iColumn = column[j]; |
4371 | currentColumnCount[iColumn]++; |
4372 | int iCount = columnCount[iColumn]; |
4373 | int absCount = CoinAbs(iCount); |
4374 | if (!absCount) { |
4375 | columnCount[iColumn] = static_cast<char>(elementByRow[j] * multiplier); |
4376 | } else { |
4377 | columnCount[iColumn] = 2; |
4378 | } |
4379 | } |
4380 | } |
4381 | } |
4382 | } |
4383 | #ifndef NDEBUG |
4384 | for (iRow = 0; iRow < numberIn; iRow++) { |
4385 | int kRow = eligible[iRow]; |
4386 | assert (rotate[kRow] >= 2); |
4387 | } |
4388 | #endif |
4389 | trueNetwork = true; |
4390 | for (iColumn = 0; iColumn < numberColumns_; iColumn++) { |
4391 | if (CoinAbs(static_cast<int>(columnCount[iColumn])) == 1) { |
4392 | trueNetwork = false; |
4393 | break; |
4394 | } |
4395 | } |
4396 | delete [] currentColumnCount; |
4397 | delete [] columnCount; |
4398 | delete [] which; |
4399 | delete [] count; |
4400 | } else { |
4401 | numberIn = numberEligible; |
4402 | for (iRow = 0; iRow < numberRows_; iRow++) { |
4403 | int kRow = mapping[iRow]; |
4404 | if (kRow >= 0) { |
4405 | rotate[kRow] = 2; |
4406 | } |
4407 | } |
4408 | } |
4409 | if (!trueNetwork) |
4410 | numberIn = - numberIn; |
4411 | delete [] column; |
4412 | delete [] rowStart; |
4413 | delete [] elementByRow; |
4414 | delete [] columnLength; |
4415 | |
4416 | char * rotate2 = CoinCopyOfArray(rotate, numberEligible); |
4417 | for (iRow = 0; iRow < numberRows_; iRow++) { |
4418 | int kRow = mapping[iRow]; |
4419 | if (kRow >= 0) { |
4420 | int iState = rotate2[kRow]; |
4421 | if (iState > 1) |
4422 | iState -= 2; |
4423 | else |
4424 | iState = -1; |
4425 | rotate[iRow] = static_cast<char>(iState); |
4426 | } else { |
4427 | rotate[iRow] = -1; |
4428 | } |
4429 | } |
4430 | delete [] rotate2; |
4431 | delete [] eligible; |
4432 | delete [] mapping; |
4433 | return numberIn; |
4434 | } |
4435 | |
4436 | |
4437 | |
4438 | |
4439 | |
4440 | |
4441 | |
4442 | ClpDataSave::ClpDataSave () |
4443 | { |
4444 | dualBound_ = 0.0; |
4445 | infeasibilityCost_ = 0.0; |
4446 | sparseThreshold_ = 0; |
4447 | pivotTolerance_ = 0.0; |
4448 | zeroFactorizationTolerance_ = 1.0e13; |
4449 | zeroSimplexTolerance_ = 1.0e-13; |
4450 | acceptablePivot_ = 0.0; |
4451 | objectiveScale_ = 1.0; |
4452 | perturbation_ = 0; |
4453 | forceFactorization_ = -1; |
4454 | scalingFlag_ = 0; |
4455 | specialOptions_ = 0; |
4456 | } |
4457 | |
4458 | |
4459 | |
4460 | |
4461 | ClpDataSave::ClpDataSave (const ClpDataSave & rhs) |
4462 | { |
4463 | dualBound_ = rhs.dualBound_; |
4464 | infeasibilityCost_ = rhs.infeasibilityCost_; |
4465 | pivotTolerance_ = rhs.pivotTolerance_; |
4466 | zeroFactorizationTolerance_ = rhs.zeroFactorizationTolerance_; |
4467 | zeroSimplexTolerance_ = rhs.zeroSimplexTolerance_; |
4468 | acceptablePivot_ = rhs.acceptablePivot_; |
4469 | objectiveScale_ = rhs.objectiveScale_; |
4470 | sparseThreshold_ = rhs.sparseThreshold_; |
4471 | perturbation_ = rhs.perturbation_; |
4472 | forceFactorization_ = rhs.forceFactorization_; |
4473 | scalingFlag_ = rhs.scalingFlag_; |
4474 | specialOptions_ = rhs.specialOptions_; |
4475 | } |
4476 | |
4477 | |
4478 | |
4479 | |
4480 | ClpDataSave::~ClpDataSave () |
4481 | { |
4482 | } |
4483 | |
4484 | |
4485 | |
4486 | |
4487 | ClpDataSave & |
4488 | ClpDataSave::operator=(const ClpDataSave& rhs) |
4489 | { |
4490 | if (this != &rhs) { |
4491 | dualBound_ = rhs.dualBound_; |
4492 | infeasibilityCost_ = rhs.infeasibilityCost_; |
4493 | pivotTolerance_ = rhs.pivotTolerance_; |
4494 | zeroFactorizationTolerance_ = zeroFactorizationTolerance_; |
4495 | zeroSimplexTolerance_ = zeroSimplexTolerance_; |
4496 | acceptablePivot_ = rhs.acceptablePivot_; |
4497 | objectiveScale_ = rhs.objectiveScale_; |
4498 | sparseThreshold_ = rhs.sparseThreshold_; |
4499 | perturbation_ = rhs.perturbation_; |
4500 | forceFactorization_ = rhs.forceFactorization_; |
4501 | scalingFlag_ = rhs.scalingFlag_; |
4502 | specialOptions_ = rhs.specialOptions_; |
4503 | } |
4504 | return *this; |
4505 | } |
4506 | |
4507 | void |
4508 | ClpModel::generateCpp( FILE * fp) |
4509 | { |
4510 | |
4511 | if (!lengthNames_) { |
4512 | |
4513 | fprintf(fp, " clpModel->dropNames();\n"); |
4514 | } |
4515 | ClpModel defaultModel; |
4516 | ClpModel * other = &defaultModel; |
4517 | int iValue1, iValue2; |
4518 | double dValue1, dValue2; |
4519 | iValue1 = this->maximumIterations(); |
4520 | iValue2 = other->maximumIterations(); |
4521 | fprintf(fp, "%d int save_maximumIterations = clpModel->maximumIterations();\n", iValue1 == iValue2 ? 2 : 1); |
4522 | fprintf(fp, "%d clpModel->setMaximumIterations(%d);\n", iValue1 == iValue2 ? 4 : 3, iValue1); |
4523 | fprintf(fp, "%d clpModel->setMaximumIterations(save_maximumIterations);\n", iValue1 == iValue2 ? 7 : 6); |
4524 | dValue1 = this->primalTolerance(); |
4525 | dValue2 = other->primalTolerance(); |
4526 | fprintf(fp, "%d double save_primalTolerance = clpModel->primalTolerance();\n", dValue1 == dValue2 ? 2 : 1); |
4527 | fprintf(fp, "%d clpModel->setPrimalTolerance(%g);\n", dValue1 == dValue2 ? 4 : 3, dValue1); |
4528 | fprintf(fp, "%d clpModel->setPrimalTolerance(save_primalTolerance);\n", dValue1 == dValue2 ? 7 : 6); |
4529 | dValue1 = this->dualTolerance(); |
4530 | dValue2 = other->dualTolerance(); |
4531 | fprintf(fp, "%d double save_dualTolerance = clpModel->dualTolerance();\n", dValue1 == dValue2 ? 2 : 1); |
4532 | fprintf(fp, "%d clpModel->setDualTolerance(%g);\n", dValue1 == dValue2 ? 4 : 3, dValue1); |
4533 | fprintf(fp, "%d clpModel->setDualTolerance(save_dualTolerance);\n", dValue1 == dValue2 ? 7 : 6); |
4534 | iValue1 = this->numberIterations(); |
4535 | iValue2 = other->numberIterations(); |
4536 | fprintf(fp, "%d int save_numberIterations = clpModel->numberIterations();\n", iValue1 == iValue2 ? 2 : 1); |
4537 | fprintf(fp, "%d clpModel->setNumberIterations(%d);\n", iValue1 == iValue2 ? 4 : 3, iValue1); |
4538 | fprintf(fp, "%d clpModel->setNumberIterations(save_numberIterations);\n", iValue1 == iValue2 ? 7 : 6); |
4539 | dValue1 = this->maximumSeconds(); |
4540 | dValue2 = other->maximumSeconds(); |
4541 | fprintf(fp, "%d double save_maximumSeconds = clpModel->maximumSeconds();\n", dValue1 == dValue2 ? 2 : 1); |
4542 | fprintf(fp, "%d clpModel->setMaximumSeconds(%g);\n", dValue1 == dValue2 ? 4 : 3, dValue1); |
4543 | fprintf(fp, "%d clpModel->setMaximumSeconds(save_maximumSeconds);\n", dValue1 == dValue2 ? 7 : 6); |
4544 | dValue1 = this->optimizationDirection(); |
4545 | dValue2 = other->optimizationDirection(); |
4546 | fprintf(fp, "%d double save_optimizationDirection = clpModel->optimizationDirection();\n", dValue1 == dValue2 ? 2 : 1); |
4547 | fprintf(fp, "%d clpModel->setOptimizationDirection(%g);\n", dValue1 == dValue2 ? 4 : 3, dValue1); |
4548 | fprintf(fp, "%d clpModel->setOptimizationDirection(save_optimizationDirection);\n", dValue1 == dValue2 ? 7 : 6); |
4549 | dValue1 = this->objectiveScale(); |
4550 | dValue2 = other->objectiveScale(); |
4551 | fprintf(fp, "%d double save_objectiveScale = clpModel->objectiveScale();\n", dValue1 == dValue2 ? 2 : 1); |
4552 | fprintf(fp, "%d clpModel->setObjectiveScale(%g);\n", dValue1 == dValue2 ? 4 : 3, dValue1); |
4553 | fprintf(fp, "%d clpModel->setObjectiveScale(save_objectiveScale);\n", dValue1 == dValue2 ? 7 : 6); |
4554 | dValue1 = this->rhsScale(); |
4555 | dValue2 = other->rhsScale(); |
4556 | fprintf(fp, "%d double save_rhsScale = clpModel->rhsScale();\n", dValue1 == dValue2 ? 2 : 1); |
4557 | fprintf(fp, "%d clpModel->setRhsScale(%g);\n", dValue1 == dValue2 ? 4 : 3, dValue1); |
4558 | fprintf(fp, "%d clpModel->setRhsScale(save_rhsScale);\n", dValue1 == dValue2 ? 7 : 6); |
4559 | iValue1 = this->scalingFlag(); |
4560 | iValue2 = other->scalingFlag(); |
4561 | fprintf(fp, "%d int save_scalingFlag = clpModel->scalingFlag();\n", iValue1 == iValue2 ? 2 : 1); |
4562 | fprintf(fp, "%d clpModel->scaling(%d);\n", iValue1 == iValue2 ? 4 : 3, iValue1); |
4563 | fprintf(fp, "%d clpModel->scaling(save_scalingFlag);\n", iValue1 == iValue2 ? 7 : 6); |
4564 | dValue1 = this->getSmallElementValue(); |
4565 | dValue2 = other->getSmallElementValue(); |
4566 | fprintf(fp, "%d double save_getSmallElementValue = clpModel->getSmallElementValue();\n", dValue1 == dValue2 ? 2 : 1); |
4567 | fprintf(fp, "%d clpModel->setSmallElementValue(%g);\n", dValue1 == dValue2 ? 4 : 3, dValue1); |
4568 | fprintf(fp, "%d clpModel->setSmallElementValue(save_getSmallElementValue);\n", dValue1 == dValue2 ? 7 : 6); |
4569 | iValue1 = this->logLevel(); |
4570 | iValue2 = other->logLevel(); |
4571 | fprintf(fp, "%d int save_logLevel = clpModel->logLevel();\n", iValue1 == iValue2 ? 2 : 1); |
4572 | fprintf(fp, "%d clpModel->setLogLevel(%d);\n", iValue1 == iValue2 ? 4 : 3, iValue1); |
4573 | fprintf(fp, "%d clpModel->setLogLevel(save_logLevel);\n", iValue1 == iValue2 ? 7 : 6); |
4574 | } |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | #ifndef _STL_ALGO_H |
57 | #define _STL_ALGO_H 1 |
58 | |
59 | #include <cstdlib> // for rand |
60 | #include <bits/algorithmfwd.h> |
61 | #include <bits/stl_heap.h> |
62 | #include <bits/stl_tempbuf.h> // for _Temporary_buffer |
63 | #include <bits/predefined_ops.h> |
64 | |
65 | #if __cplusplus >= 201103L |
66 | #include <bits/uniform_int_dist.h> |
67 | #endif |
68 | |
69 | |
70 | |
71 | namespace std _GLIBCXX_VISIBILITY(default) |
72 | { |
73 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
74 | |
75 | |
76 | template<typename _Iterator, typename _Compare> |
77 | _GLIBCXX20_CONSTEXPR |
78 | void |
79 | __move_median_to_first(_Iterator __result,_Iterator __a, _Iterator __b, |
80 | _Iterator __c, _Compare __comp) |
81 | { |
82 | if (__comp(__a, __b)) |
| 33 | | Calling '_Iter_less_iter::operator()' | |
|
83 | { |
84 | if (__comp(__b, __c)) |
85 | std::iter_swap(__result, __b); |
86 | else if (__comp(__a, __c)) |
87 | std::iter_swap(__result, __c); |
88 | else |
89 | std::iter_swap(__result, __a); |
90 | } |
91 | else if (__comp(__a, __c)) |
92 | std::iter_swap(__result, __a); |
93 | else if (__comp(__b, __c)) |
94 | std::iter_swap(__result, __c); |
95 | else |
96 | std::iter_swap(__result, __b); |
97 | } |
98 | |
99 | |
100 | template<typename _InputIterator, typename _Predicate> |
101 | _GLIBCXX20_CONSTEXPR |
102 | inline _InputIterator |
103 | __find_if_not(_InputIterator __first, _InputIterator __last, |
104 | _Predicate __pred) |
105 | { |
106 | return std::__find_if(__first, __last, |
107 | __gnu_cxx::__ops::__negate(__pred), |
108 | std::__iterator_category(__first)); |
109 | } |
110 | |
111 | |
112 | |
113 | |
114 | template<typename _InputIterator, typename _Predicate, typename _Distance> |
115 | _GLIBCXX20_CONSTEXPR |
116 | _InputIterator |
117 | __find_if_not_n(_InputIterator __first, _Distance& __len, _Predicate __pred) |
118 | { |
119 | for (; __len; --__len, (void) ++__first) |
120 | if (!__pred(__first)) |
121 | break; |
122 | return __first; |
123 | } |
124 | |
125 | |
126 | |
127 | |
128 | |
129 | |
130 | |
131 | |
132 | |
133 | |
134 | |
135 | |
136 | |
137 | |
138 | template<typename _ForwardIterator1, typename _ForwardIterator2, |
139 | typename _BinaryPredicate> |
140 | _GLIBCXX20_CONSTEXPR |
141 | _ForwardIterator1 |
142 | __search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
143 | _ForwardIterator2 __first2, _ForwardIterator2 __last2, |
144 | _BinaryPredicate __predicate) |
145 | { |
146 | |
147 | if (__first1 == __last1 || __first2 == __last2) |
148 | return __first1; |
149 | |
150 | |
151 | _ForwardIterator2 __p1(__first2); |
152 | if (++__p1 == __last2) |
153 | return std::__find_if(__first1, __last1, |
154 | __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2)); |
155 | |
156 | |
157 | _ForwardIterator1 __current = __first1; |
158 | |
159 | for (;;) |
160 | { |
161 | __first1 = |
162 | std::__find_if(__first1, __last1, |
163 | __gnu_cxx::__ops::__iter_comp_iter(__predicate, __first2)); |
164 | |
165 | if (__first1 == __last1) |
166 | return __last1; |
167 | |
168 | _ForwardIterator2 __p = __p1; |
169 | __current = __first1; |
170 | if (++__current == __last1) |
171 | return __last1; |
172 | |
173 | while (__predicate(__current, __p)) |
174 | { |
175 | if (++__p == __last2) |
176 | return __first1; |
177 | if (++__current == __last1) |
178 | return __last1; |
179 | } |
180 | ++__first1; |
181 | } |
182 | return __first1; |
183 | } |
184 | |
185 | |
186 | |
187 | |
188 | |
189 | |
190 | template<typename _ForwardIterator, typename _Integer, |
191 | typename _UnaryPredicate> |
192 | _GLIBCXX20_CONSTEXPR |
193 | _ForwardIterator |
194 | __search_n_aux(_ForwardIterator __first, _ForwardIterator __last, |
195 | _Integer __count, _UnaryPredicate __unary_pred, |
196 | std::forward_iterator_tag) |
197 | { |
198 | __first = std::__find_if(__first, __last, __unary_pred); |
199 | while (__first != __last) |
200 | { |
201 | typename iterator_traits<_ForwardIterator>::difference_type |
202 | __n = __count; |
203 | _ForwardIterator __i = __first; |
204 | ++__i; |
205 | while (__i != __last && __n != 1 && __unary_pred(__i)) |
206 | { |
207 | ++__i; |
208 | --__n; |
209 | } |
210 | if (__n == 1) |
211 | return __first; |
212 | if (__i == __last) |
213 | return __last; |
214 | __first = std::__find_if(++__i, __last, __unary_pred); |
215 | } |
216 | return __last; |
217 | } |
218 | |
219 | |
220 | |
221 | |
222 | |
223 | template<typename _RandomAccessIter, typename _Integer, |
224 | typename _UnaryPredicate> |
225 | _GLIBCXX20_CONSTEXPR |
226 | _RandomAccessIter |
227 | __search_n_aux(_RandomAccessIter __first, _RandomAccessIter __last, |
228 | _Integer __count, _UnaryPredicate __unary_pred, |
229 | std::random_access_iterator_tag) |
230 | { |
231 | typedef typename std::iterator_traits<_RandomAccessIter>::difference_type |
232 | _DistanceType; |
233 | |
234 | _DistanceType __tailSize = __last - __first; |
235 | _DistanceType __remainder = __count; |
236 | |
237 | while (__remainder <= __tailSize) |
238 | { |
239 | __first += __remainder; |
240 | __tailSize -= __remainder; |
241 | |
242 | |
243 | _RandomAccessIter __backTrack = __first; |
244 | while (__unary_pred(--__backTrack)) |
245 | { |
246 | if (--__remainder == 0) |
247 | return (__first - __count); |
248 | } |
249 | __remainder = __count + 1 - (__first - __backTrack); |
250 | } |
251 | return __last; |
252 | } |
253 | |
254 | template<typename _ForwardIterator, typename _Integer, |
255 | typename _UnaryPredicate> |
256 | _GLIBCXX20_CONSTEXPR |
257 | _ForwardIterator |
258 | __search_n(_ForwardIterator __first, _ForwardIterator __last, |
259 | _Integer __count, |
260 | _UnaryPredicate __unary_pred) |
261 | { |
262 | if (__count <= 0) |
263 | return __first; |
264 | |
265 | if (__count == 1) |
266 | return std::__find_if(__first, __last, __unary_pred); |
267 | |
268 | return std::__search_n_aux(__first, __last, __count, __unary_pred, |
269 | std::__iterator_category(__first)); |
270 | } |
271 | |
272 | |
273 | template<typename _ForwardIterator1, typename _ForwardIterator2, |
274 | typename _BinaryPredicate> |
275 | _GLIBCXX20_CONSTEXPR |
276 | _ForwardIterator1 |
277 | __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
278 | _ForwardIterator2 __first2, _ForwardIterator2 __last2, |
279 | forward_iterator_tag, forward_iterator_tag, |
280 | _BinaryPredicate __comp) |
281 | { |
282 | if (__first2 == __last2) |
283 | return __last1; |
284 | |
285 | _ForwardIterator1 __result = __last1; |
286 | while (1) |
287 | { |
288 | _ForwardIterator1 __new_result |
289 | = std::__search(__first1, __last1, __first2, __last2, __comp); |
290 | if (__new_result == __last1) |
291 | return __result; |
292 | else |
293 | { |
294 | __result = __new_result; |
295 | __first1 = __new_result; |
296 | ++__first1; |
297 | } |
298 | } |
299 | } |
300 | |
301 | |
302 | template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, |
303 | typename _BinaryPredicate> |
304 | _GLIBCXX20_CONSTEXPR |
305 | _BidirectionalIterator1 |
306 | __find_end(_BidirectionalIterator1 __first1, |
307 | _BidirectionalIterator1 __last1, |
308 | _BidirectionalIterator2 __first2, |
309 | _BidirectionalIterator2 __last2, |
310 | bidirectional_iterator_tag, bidirectional_iterator_tag, |
311 | _BinaryPredicate __comp) |
312 | { |
313 | |
314 | __glibcxx_function_requires(_BidirectionalIteratorConcept< |
315 | _BidirectionalIterator1>) |
316 | __glibcxx_function_requires(_BidirectionalIteratorConcept< |
317 | _BidirectionalIterator2>) |
318 | |
319 | typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1; |
320 | typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2; |
321 | |
322 | _RevIterator1 __rlast1(__first1); |
323 | _RevIterator2 __rlast2(__first2); |
324 | _RevIterator1 __rresult = std::__search(_RevIterator1(__last1), __rlast1, |
325 | _RevIterator2(__last2), __rlast2, |
326 | __comp); |
327 | |
328 | if (__rresult == __rlast1) |
329 | return __last1; |
330 | else |
331 | { |
332 | _BidirectionalIterator1 __result = __rresult.base(); |
333 | std::advance(__result, -std::distance(__first2, __last2)); |
334 | return __result; |
335 | } |
336 | } |
337 | |
338 | |
339 | |
340 | |
341 | |
342 | |
343 | |
344 | |
345 | |
346 | |
347 | |
348 | |
349 | |
350 | |
351 | |
352 | |
353 | |
354 | |
355 | |
356 | |
357 | |
358 | |
359 | |
360 | |
361 | |
362 | |
363 | |
364 | template<typename _ForwardIterator1, typename _ForwardIterator2> |
365 | _GLIBCXX20_CONSTEXPR |
366 | inline _ForwardIterator1 |
367 | find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
368 | _ForwardIterator2 __first2, _ForwardIterator2 __last2) |
369 | { |
370 | |
371 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) |
372 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) |
373 | __glibcxx_function_requires(_EqualOpConcept< |
374 | typename iterator_traits<_ForwardIterator1>::value_type, |
375 | typename iterator_traits<_ForwardIterator2>::value_type>) |
376 | __glibcxx_requires_valid_range(__first1, __last1); |
377 | __glibcxx_requires_valid_range(__first2, __last2); |
378 | |
379 | return std::__find_end(__first1, __last1, __first2, __last2, |
380 | std::__iterator_category(__first1), |
381 | std::__iterator_category(__first2), |
382 | __gnu_cxx::__ops::__iter_equal_to_iter()); |
383 | } |
384 | |
385 | |
386 | |
387 | |
388 | |
389 | |
390 | |
391 | |
392 | |
393 | |
394 | |
395 | |
396 | |
397 | |
398 | |
399 | |
400 | |
401 | |
402 | |
403 | |
404 | |
405 | |
406 | |
407 | |
408 | |
409 | |
410 | |
411 | |
412 | |
413 | template<typename _ForwardIterator1, typename _ForwardIterator2, |
414 | typename _BinaryPredicate> |
415 | _GLIBCXX20_CONSTEXPR |
416 | inline _ForwardIterator1 |
417 | find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
418 | _ForwardIterator2 __first2, _ForwardIterator2 __last2, |
419 | _BinaryPredicate __comp) |
420 | { |
421 | |
422 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) |
423 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) |
424 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
425 | typename iterator_traits<_ForwardIterator1>::value_type, |
426 | typename iterator_traits<_ForwardIterator2>::value_type>) |
427 | __glibcxx_requires_valid_range(__first1, __last1); |
428 | __glibcxx_requires_valid_range(__first2, __last2); |
429 | |
430 | return std::__find_end(__first1, __last1, __first2, __last2, |
431 | std::__iterator_category(__first1), |
432 | std::__iterator_category(__first2), |
433 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
434 | } |
435 | |
436 | #if __cplusplus >= 201103L |
437 | |
438 | |
439 | |
440 | |
441 | |
442 | |
443 | |
444 | |
445 | |
446 | |
447 | |
448 | |
449 | template<typename _InputIterator, typename _Predicate> |
450 | _GLIBCXX20_CONSTEXPR |
451 | inline bool |
452 | all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) |
453 | { return __last == std::find_if_not(__first, __last, __pred); } |
454 | |
455 | |
456 | |
457 | |
458 | |
459 | |
460 | |
461 | |
462 | |
463 | |
464 | |
465 | |
466 | |
467 | template<typename _InputIterator, typename _Predicate> |
468 | _GLIBCXX20_CONSTEXPR |
469 | inline bool |
470 | none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) |
471 | { return __last == _GLIBCXX_STD_A::find_if(__first, __last, __pred); } |
472 | |
473 | |
474 | |
475 | |
476 | |
477 | |
478 | |
479 | |
480 | |
481 | |
482 | |
483 | |
484 | |
485 | |
486 | template<typename _InputIterator, typename _Predicate> |
487 | _GLIBCXX20_CONSTEXPR |
488 | inline bool |
489 | any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred) |
490 | { return !std::none_of(__first, __last, __pred); } |
491 | |
492 | |
493 | |
494 | |
495 | |
496 | |
497 | |
498 | |
499 | |
500 | |
501 | |
502 | template<typename _InputIterator, typename _Predicate> |
503 | _GLIBCXX20_CONSTEXPR |
504 | inline _InputIterator |
505 | find_if_not(_InputIterator __first, _InputIterator __last, |
506 | _Predicate __pred) |
507 | { |
508 | |
509 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
510 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
511 | typename iterator_traits<_InputIterator>::value_type>) |
512 | __glibcxx_requires_valid_range(__first, __last); |
513 | return std::__find_if_not(__first, __last, |
514 | __gnu_cxx::__ops::__pred_iter(__pred)); |
515 | } |
516 | |
517 | |
518 | |
519 | |
520 | |
521 | |
522 | |
523 | |
524 | |
525 | |
526 | |
527 | template<typename _InputIterator, typename _Predicate> |
528 | _GLIBCXX20_CONSTEXPR |
529 | inline bool |
530 | is_partitioned(_InputIterator __first, _InputIterator __last, |
531 | _Predicate __pred) |
532 | { |
533 | __first = std::find_if_not(__first, __last, __pred); |
534 | if (__first == __last) |
535 | return true; |
536 | ++__first; |
537 | return std::none_of(__first, __last, __pred); |
538 | } |
539 | |
540 | |
541 | |
542 | |
543 | |
544 | |
545 | |
546 | |
547 | |
548 | |
549 | template<typename _ForwardIterator, typename _Predicate> |
550 | _GLIBCXX20_CONSTEXPR |
551 | _ForwardIterator |
552 | partition_point(_ForwardIterator __first, _ForwardIterator __last, |
553 | _Predicate __pred) |
554 | { |
555 | |
556 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
557 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
558 | typename iterator_traits<_ForwardIterator>::value_type>) |
559 | |
560 | |
561 | __glibcxx_requires_valid_range(__first, __last); |
562 | |
563 | typedef typename iterator_traits<_ForwardIterator>::difference_type |
564 | _DistanceType; |
565 | |
566 | _DistanceType __len = std::distance(__first, __last); |
567 | |
568 | while (__len > 0) |
569 | { |
570 | _DistanceType __half = __len >> 1; |
571 | _ForwardIterator __middle = __first; |
572 | std::advance(__middle, __half); |
573 | if (__pred(*__middle)) |
574 | { |
575 | __first = __middle; |
576 | ++__first; |
577 | __len = __len - __half - 1; |
578 | } |
579 | else |
580 | __len = __half; |
581 | } |
582 | return __first; |
583 | } |
584 | #endif |
585 | |
586 | template<typename _InputIterator, typename _OutputIterator, |
587 | typename _Predicate> |
588 | _GLIBCXX20_CONSTEXPR |
589 | _OutputIterator |
590 | __remove_copy_if(_InputIterator __first, _InputIterator __last, |
591 | _OutputIterator __result, _Predicate __pred) |
592 | { |
593 | for (; __first != __last; ++__first) |
594 | if (!__pred(__first)) |
595 | { |
596 | *__result = *__first; |
597 | ++__result; |
598 | } |
599 | return __result; |
600 | } |
601 | |
602 | |
603 | |
604 | |
605 | |
606 | |
607 | |
608 | |
609 | |
610 | |
611 | |
612 | |
613 | |
614 | |
615 | |
616 | template<typename _InputIterator, typename _OutputIterator, typename _Tp> |
617 | _GLIBCXX20_CONSTEXPR |
618 | inline _OutputIterator |
619 | remove_copy(_InputIterator __first, _InputIterator __last, |
620 | _OutputIterator __result, const _Tp& __value) |
621 | { |
622 | |
623 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
624 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
625 | typename iterator_traits<_InputIterator>::value_type>) |
626 | __glibcxx_function_requires(_EqualOpConcept< |
627 | typename iterator_traits<_InputIterator>::value_type, _Tp>) |
628 | __glibcxx_requires_valid_range(__first, __last); |
629 | |
630 | return std::__remove_copy_if(__first, __last, __result, |
631 | __gnu_cxx::__ops::__iter_equals_val(__value)); |
632 | } |
633 | |
634 | |
635 | |
636 | |
637 | |
638 | |
639 | |
640 | |
641 | |
642 | |
643 | |
644 | |
645 | |
646 | |
647 | |
648 | |
649 | template<typename _InputIterator, typename _OutputIterator, |
650 | typename _Predicate> |
651 | _GLIBCXX20_CONSTEXPR |
652 | inline _OutputIterator |
653 | remove_copy_if(_InputIterator __first, _InputIterator __last, |
654 | _OutputIterator __result, _Predicate __pred) |
655 | { |
656 | |
657 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
658 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
659 | typename iterator_traits<_InputIterator>::value_type>) |
660 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
661 | typename iterator_traits<_InputIterator>::value_type>) |
662 | __glibcxx_requires_valid_range(__first, __last); |
663 | |
664 | return std::__remove_copy_if(__first, __last, __result, |
665 | __gnu_cxx::__ops::__pred_iter(__pred)); |
666 | } |
667 | |
668 | #if __cplusplus >= 201103L |
669 | |
670 | |
671 | |
672 | |
673 | |
674 | |
675 | |
676 | |
677 | |
678 | |
679 | |
680 | |
681 | |
682 | |
683 | |
684 | template<typename _InputIterator, typename _OutputIterator, |
685 | typename _Predicate> |
686 | _GLIBCXX20_CONSTEXPR |
687 | _OutputIterator |
688 | copy_if(_InputIterator __first, _InputIterator __last, |
689 | _OutputIterator __result, _Predicate __pred) |
690 | { |
691 | |
692 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
693 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
694 | typename iterator_traits<_InputIterator>::value_type>) |
695 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
696 | typename iterator_traits<_InputIterator>::value_type>) |
697 | __glibcxx_requires_valid_range(__first, __last); |
698 | |
699 | for (; __first != __last; ++__first) |
700 | if (__pred(*__first)) |
701 | { |
702 | *__result = *__first; |
703 | ++__result; |
704 | } |
705 | return __result; |
706 | } |
707 | |
708 | template<typename _InputIterator, typename _Size, typename _OutputIterator> |
709 | _GLIBCXX20_CONSTEXPR |
710 | _OutputIterator |
711 | __copy_n_a(_InputIterator __first, _Size __n, _OutputIterator __result) |
712 | { |
713 | if (__n > 0) |
714 | { |
715 | while (true) |
716 | { |
717 | *__result = *__first; |
718 | ++__result; |
719 | if (--__n > 0) |
720 | ++__first; |
721 | else |
722 | break; |
723 | } |
724 | } |
725 | return __result; |
726 | } |
727 | |
728 | template<typename _CharT, typename _Size> |
729 | __enable_if_t<__is_char<_CharT>::__value, _CharT*> |
730 | __copy_n_a(istreambuf_iterator<_CharT, char_traits<_CharT>>, |
731 | _Size, _CharT*); |
732 | |
733 | template<typename _InputIterator, typename _Size, typename _OutputIterator> |
734 | _GLIBCXX20_CONSTEXPR |
735 | _OutputIterator |
736 | __copy_n(_InputIterator __first, _Size __n, |
737 | _OutputIterator __result, input_iterator_tag) |
738 | { |
739 | return std::__niter_wrap(__result, |
740 | __copy_n_a(__first, __n, |
741 | std::__niter_base(__result))); |
742 | } |
743 | |
744 | template<typename _RandomAccessIterator, typename _Size, |
745 | typename _OutputIterator> |
746 | _GLIBCXX20_CONSTEXPR |
747 | inline _OutputIterator |
748 | __copy_n(_RandomAccessIterator __first, _Size __n, |
749 | _OutputIterator __result, random_access_iterator_tag) |
750 | { return std::copy(__first, __first + __n, __result); } |
751 | |
752 | |
753 | |
754 | |
755 | |
756 | |
757 | |
758 | |
759 | |
760 | |
761 | |
762 | |
763 | |
764 | |
765 | template<typename _InputIterator, typename _Size, typename _OutputIterator> |
766 | _GLIBCXX20_CONSTEXPR |
767 | inline _OutputIterator |
768 | copy_n(_InputIterator __first, _Size __n, _OutputIterator __result) |
769 | { |
770 | |
771 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
772 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
773 | typename iterator_traits<_InputIterator>::value_type>) |
774 | __glibcxx_requires_can_increment(__first, __n); |
775 | __glibcxx_requires_can_increment(__result, __n); |
776 | |
777 | return std::__copy_n(__first, __n, __result, |
778 | std::__iterator_category(__first)); |
779 | } |
780 | |
781 | |
782 | |
783 | |
784 | |
785 | |
786 | |
787 | |
788 | |
789 | |
790 | |
791 | |
792 | |
793 | |
794 | |
795 | |
796 | template<typename _InputIterator, typename _OutputIterator1, |
797 | typename _OutputIterator2, typename _Predicate> |
798 | _GLIBCXX20_CONSTEXPR |
799 | pair<_OutputIterator1, _OutputIterator2> |
800 | partition_copy(_InputIterator __first, _InputIterator __last, |
801 | _OutputIterator1 __out_true, _OutputIterator2 __out_false, |
802 | _Predicate __pred) |
803 | { |
804 | |
805 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
806 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1, |
807 | typename iterator_traits<_InputIterator>::value_type>) |
808 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2, |
809 | typename iterator_traits<_InputIterator>::value_type>) |
810 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
811 | typename iterator_traits<_InputIterator>::value_type>) |
812 | __glibcxx_requires_valid_range(__first, __last); |
813 | |
814 | for (; __first != __last; ++__first) |
815 | if (__pred(*__first)) |
816 | { |
817 | *__out_true = *__first; |
818 | ++__out_true; |
819 | } |
820 | else |
821 | { |
822 | *__out_false = *__first; |
823 | ++__out_false; |
824 | } |
825 | |
826 | return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false); |
827 | } |
828 | #endif // C++11 |
829 | |
830 | template<typename _ForwardIterator, typename _Predicate> |
831 | _GLIBCXX20_CONSTEXPR |
832 | _ForwardIterator |
833 | __remove_if(_ForwardIterator __first, _ForwardIterator __last, |
834 | _Predicate __pred) |
835 | { |
836 | __first = std::__find_if(__first, __last, __pred); |
837 | if (__first == __last) |
838 | return __first; |
839 | _ForwardIterator __result = __first; |
840 | ++__first; |
841 | for (; __first != __last; ++__first) |
842 | if (!__pred(__first)) |
843 | { |
844 | *__result = _GLIBCXX_MOVE(*__first); |
845 | ++__result; |
846 | } |
847 | return __result; |
848 | } |
849 | |
850 | |
851 | |
852 | |
853 | |
854 | |
855 | |
856 | |
857 | |
858 | |
859 | |
860 | |
861 | |
862 | |
863 | |
864 | |
865 | |
866 | |
867 | template<typename _ForwardIterator, typename _Tp> |
868 | _GLIBCXX20_CONSTEXPR |
869 | inline _ForwardIterator |
870 | remove(_ForwardIterator __first, _ForwardIterator __last, |
871 | const _Tp& __value) |
872 | { |
873 | |
874 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
875 | _ForwardIterator>) |
876 | __glibcxx_function_requires(_EqualOpConcept< |
877 | typename iterator_traits<_ForwardIterator>::value_type, _Tp>) |
878 | __glibcxx_requires_valid_range(__first, __last); |
879 | |
880 | return std::__remove_if(__first, __last, |
881 | __gnu_cxx::__ops::__iter_equals_val(__value)); |
882 | } |
883 | |
884 | |
885 | |
886 | |
887 | |
888 | |
889 | |
890 | |
891 | |
892 | |
893 | |
894 | |
895 | |
896 | |
897 | |
898 | |
899 | |
900 | |
901 | template<typename _ForwardIterator, typename _Predicate> |
902 | _GLIBCXX20_CONSTEXPR |
903 | inline _ForwardIterator |
904 | remove_if(_ForwardIterator __first, _ForwardIterator __last, |
905 | _Predicate __pred) |
906 | { |
907 | |
908 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
909 | _ForwardIterator>) |
910 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
911 | typename iterator_traits<_ForwardIterator>::value_type>) |
912 | __glibcxx_requires_valid_range(__first, __last); |
913 | |
914 | return std::__remove_if(__first, __last, |
915 | __gnu_cxx::__ops::__pred_iter(__pred)); |
916 | } |
917 | |
918 | template<typename _ForwardIterator, typename _BinaryPredicate> |
919 | _GLIBCXX20_CONSTEXPR |
920 | _ForwardIterator |
921 | __adjacent_find(_ForwardIterator __first, _ForwardIterator __last, |
922 | _BinaryPredicate __binary_pred) |
923 | { |
924 | if (__first == __last) |
925 | return __last; |
926 | _ForwardIterator __next = __first; |
927 | while (++__next != __last) |
928 | { |
929 | if (__binary_pred(__first, __next)) |
930 | return __first; |
931 | __first = __next; |
932 | } |
933 | return __last; |
934 | } |
935 | |
936 | template<typename _ForwardIterator, typename _BinaryPredicate> |
937 | _GLIBCXX20_CONSTEXPR |
938 | _ForwardIterator |
939 | __unique(_ForwardIterator __first, _ForwardIterator __last, |
940 | _BinaryPredicate __binary_pred) |
941 | { |
942 | |
943 | __first = std::__adjacent_find(__first, __last, __binary_pred); |
944 | if (__first == __last) |
945 | return __last; |
946 | |
947 | |
948 | _ForwardIterator __dest = __first; |
949 | ++__first; |
950 | while (++__first != __last) |
951 | if (!__binary_pred(__dest, __first)) |
952 | *++__dest = _GLIBCXX_MOVE(*__first); |
953 | return ++__dest; |
954 | } |
955 | |
956 | |
957 | |
958 | |
959 | |
960 | |
961 | |
962 | |
963 | |
964 | |
965 | |
966 | |
967 | |
968 | |
969 | |
970 | template<typename _ForwardIterator> |
971 | _GLIBCXX20_CONSTEXPR |
972 | inline _ForwardIterator |
973 | unique(_ForwardIterator __first, _ForwardIterator __last) |
974 | { |
975 | |
976 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
977 | _ForwardIterator>) |
978 | __glibcxx_function_requires(_EqualityComparableConcept< |
979 | typename iterator_traits<_ForwardIterator>::value_type>) |
980 | __glibcxx_requires_valid_range(__first, __last); |
981 | |
982 | return std::__unique(__first, __last, |
983 | __gnu_cxx::__ops::__iter_equal_to_iter()); |
984 | } |
985 | |
986 | |
987 | |
988 | |
989 | |
990 | |
991 | |
992 | |
993 | |
994 | |
995 | |
996 | |
997 | |
998 | |
999 | |
1000 | |
1001 | template<typename _ForwardIterator, typename _BinaryPredicate> |
1002 | _GLIBCXX20_CONSTEXPR |
1003 | inline _ForwardIterator |
1004 | unique(_ForwardIterator __first, _ForwardIterator __last, |
1005 | _BinaryPredicate __binary_pred) |
1006 | { |
1007 | |
1008 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
1009 | _ForwardIterator>) |
1010 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
1011 | typename iterator_traits<_ForwardIterator>::value_type, |
1012 | typename iterator_traits<_ForwardIterator>::value_type>) |
1013 | __glibcxx_requires_valid_range(__first, __last); |
1014 | |
1015 | return std::__unique(__first, __last, |
1016 | __gnu_cxx::__ops::__iter_comp_iter(__binary_pred)); |
1017 | } |
1018 | |
1019 | |
1020 | |
1021 | |
1022 | |
1023 | |
1024 | |
1025 | template<typename _ForwardIterator, typename _OutputIterator, |
1026 | typename _BinaryPredicate> |
1027 | _GLIBCXX20_CONSTEXPR |
1028 | _OutputIterator |
1029 | __unique_copy(_ForwardIterator __first, _ForwardIterator __last, |
1030 | _OutputIterator __result, _BinaryPredicate __binary_pred, |
1031 | forward_iterator_tag, output_iterator_tag) |
1032 | { |
1033 | |
1034 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
1035 | typename iterator_traits<_ForwardIterator>::value_type, |
1036 | typename iterator_traits<_ForwardIterator>::value_type>) |
1037 | |
1038 | _ForwardIterator __next = __first; |
1039 | *__result = *__first; |
1040 | while (++__next != __last) |
1041 | if (!__binary_pred(__first, __next)) |
1042 | { |
1043 | __first = __next; |
1044 | *++__result = *__first; |
1045 | } |
1046 | return ++__result; |
1047 | } |
1048 | |
1049 | |
1050 | |
1051 | |
1052 | |
1053 | |
1054 | |
1055 | template<typename _InputIterator, typename _OutputIterator, |
1056 | typename _BinaryPredicate> |
1057 | _GLIBCXX20_CONSTEXPR |
1058 | _OutputIterator |
1059 | __unique_copy(_InputIterator __first, _InputIterator __last, |
1060 | _OutputIterator __result, _BinaryPredicate __binary_pred, |
1061 | input_iterator_tag, output_iterator_tag) |
1062 | { |
1063 | |
1064 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
1065 | typename iterator_traits<_InputIterator>::value_type, |
1066 | typename iterator_traits<_InputIterator>::value_type>) |
1067 | |
1068 | typename iterator_traits<_InputIterator>::value_type __value = *__first; |
1069 | __decltype(__gnu_cxx::__ops::__iter_comp_val(__binary_pred)) |
1070 | __rebound_pred |
1071 | = __gnu_cxx::__ops::__iter_comp_val(__binary_pred); |
1072 | *__result = __value; |
1073 | while (++__first != __last) |
1074 | if (!__rebound_pred(__first, __value)) |
1075 | { |
1076 | __value = *__first; |
1077 | *++__result = __value; |
1078 | } |
1079 | return ++__result; |
1080 | } |
1081 | |
1082 | |
1083 | |
1084 | |
1085 | |
1086 | |
1087 | |
1088 | template<typename _InputIterator, typename _ForwardIterator, |
1089 | typename _BinaryPredicate> |
1090 | _GLIBCXX20_CONSTEXPR |
1091 | _ForwardIterator |
1092 | __unique_copy(_InputIterator __first, _InputIterator __last, |
1093 | _ForwardIterator __result, _BinaryPredicate __binary_pred, |
1094 | input_iterator_tag, forward_iterator_tag) |
1095 | { |
1096 | |
1097 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
1098 | typename iterator_traits<_ForwardIterator>::value_type, |
1099 | typename iterator_traits<_InputIterator>::value_type>) |
1100 | *__result = *__first; |
1101 | while (++__first != __last) |
1102 | if (!__binary_pred(__result, __first)) |
1103 | *++__result = *__first; |
1104 | return ++__result; |
1105 | } |
1106 | |
1107 | |
1108 | |
1109 | |
1110 | |
1111 | |
1112 | template<typename _BidirectionalIterator> |
1113 | _GLIBCXX20_CONSTEXPR |
1114 | void |
1115 | __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, |
1116 | bidirectional_iterator_tag) |
1117 | { |
1118 | while (true) |
1119 | if (__first == __last || __first == --__last) |
1120 | return; |
1121 | else |
1122 | { |
1123 | std::iter_swap(__first, __last); |
1124 | ++__first; |
1125 | } |
1126 | } |
1127 | |
1128 | |
1129 | |
1130 | |
1131 | |
1132 | |
1133 | template<typename _RandomAccessIterator> |
1134 | _GLIBCXX20_CONSTEXPR |
1135 | void |
1136 | __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, |
1137 | random_access_iterator_tag) |
1138 | { |
1139 | if (__first == __last) |
1140 | return; |
1141 | --__last; |
1142 | while (__first < __last) |
1143 | { |
1144 | std::iter_swap(__first, __last); |
1145 | ++__first; |
1146 | --__last; |
1147 | } |
1148 | } |
1149 | |
1150 | |
1151 | |
1152 | |
1153 | |
1154 | |
1155 | |
1156 | |
1157 | |
1158 | |
1159 | |
1160 | |
1161 | |
1162 | template<typename _BidirectionalIterator> |
1163 | _GLIBCXX20_CONSTEXPR |
1164 | inline void |
1165 | reverse(_BidirectionalIterator __first, _BidirectionalIterator __last) |
1166 | { |
1167 | |
1168 | __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< |
1169 | _BidirectionalIterator>) |
1170 | __glibcxx_requires_valid_range(__first, __last); |
1171 | std::__reverse(__first, __last, std::__iterator_category(__first)); |
1172 | } |
1173 | |
1174 | |
1175 | |
1176 | |
1177 | |
1178 | |
1179 | |
1180 | |
1181 | |
1182 | |
1183 | |
1184 | |
1185 | |
1186 | |
1187 | |
1188 | |
1189 | |
1190 | template<typename _BidirectionalIterator, typename _OutputIterator> |
1191 | _GLIBCXX20_CONSTEXPR |
1192 | _OutputIterator |
1193 | reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last, |
1194 | _OutputIterator __result) |
1195 | { |
1196 | |
1197 | __glibcxx_function_requires(_BidirectionalIteratorConcept< |
1198 | _BidirectionalIterator>) |
1199 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
1200 | typename iterator_traits<_BidirectionalIterator>::value_type>) |
1201 | __glibcxx_requires_valid_range(__first, __last); |
1202 | |
1203 | while (__first != __last) |
1204 | { |
1205 | --__last; |
1206 | *__result = *__last; |
1207 | ++__result; |
1208 | } |
1209 | return __result; |
1210 | } |
1211 | |
1212 | |
1213 | |
1214 | |
1215 | |
1216 | template<typename _EuclideanRingElement> |
1217 | _GLIBCXX20_CONSTEXPR |
1218 | _EuclideanRingElement |
1219 | __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n) |
1220 | { |
1221 | while (__n != 0) |
1222 | { |
1223 | _EuclideanRingElement __t = __m % __n; |
1224 | __m = __n; |
1225 | __n = __t; |
1226 | } |
1227 | return __m; |
1228 | } |
1229 | |
1230 | inline namespace _V2 |
1231 | { |
1232 | |
1233 | |
1234 | template<typename _ForwardIterator> |
1235 | _GLIBCXX20_CONSTEXPR |
1236 | _ForwardIterator |
1237 | __rotate(_ForwardIterator __first, |
1238 | _ForwardIterator __middle, |
1239 | _ForwardIterator __last, |
1240 | forward_iterator_tag) |
1241 | { |
1242 | if (__first == __middle) |
1243 | return __last; |
1244 | else if (__last == __middle) |
1245 | return __first; |
1246 | |
1247 | _ForwardIterator __first2 = __middle; |
1248 | do |
1249 | { |
1250 | std::iter_swap(__first, __first2); |
1251 | ++__first; |
1252 | ++__first2; |
1253 | if (__first == __middle) |
1254 | __middle = __first2; |
1255 | } |
1256 | while (__first2 != __last); |
1257 | |
1258 | _ForwardIterator __ret = __first; |
1259 | |
1260 | __first2 = __middle; |
1261 | |
1262 | while (__first2 != __last) |
1263 | { |
1264 | std::iter_swap(__first, __first2); |
1265 | ++__first; |
1266 | ++__first2; |
1267 | if (__first == __middle) |
1268 | __middle = __first2; |
1269 | else if (__first2 == __last) |
1270 | __first2 = __middle; |
1271 | } |
1272 | return __ret; |
1273 | } |
1274 | |
1275 | |
1276 | template<typename _BidirectionalIterator> |
1277 | _GLIBCXX20_CONSTEXPR |
1278 | _BidirectionalIterator |
1279 | __rotate(_BidirectionalIterator __first, |
1280 | _BidirectionalIterator __middle, |
1281 | _BidirectionalIterator __last, |
1282 | bidirectional_iterator_tag) |
1283 | { |
1284 | |
1285 | __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< |
1286 | _BidirectionalIterator>) |
1287 | |
1288 | if (__first == __middle) |
1289 | return __last; |
1290 | else if (__last == __middle) |
1291 | return __first; |
1292 | |
1293 | std::__reverse(__first, __middle, bidirectional_iterator_tag()); |
1294 | std::__reverse(__middle, __last, bidirectional_iterator_tag()); |
1295 | |
1296 | while (__first != __middle && __middle != __last) |
1297 | { |
1298 | std::iter_swap(__first, --__last); |
1299 | ++__first; |
1300 | } |
1301 | |
1302 | if (__first == __middle) |
1303 | { |
1304 | std::__reverse(__middle, __last, bidirectional_iterator_tag()); |
1305 | return __last; |
1306 | } |
1307 | else |
1308 | { |
1309 | std::__reverse(__first, __middle, bidirectional_iterator_tag()); |
1310 | return __first; |
1311 | } |
1312 | } |
1313 | |
1314 | |
1315 | template<typename _RandomAccessIterator> |
1316 | _GLIBCXX20_CONSTEXPR |
1317 | _RandomAccessIterator |
1318 | __rotate(_RandomAccessIterator __first, |
1319 | _RandomAccessIterator __middle, |
1320 | _RandomAccessIterator __last, |
1321 | random_access_iterator_tag) |
1322 | { |
1323 | |
1324 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
1325 | _RandomAccessIterator>) |
1326 | |
1327 | if (__first == __middle) |
1328 | return __last; |
1329 | else if (__last == __middle) |
1330 | return __first; |
1331 | |
1332 | typedef typename iterator_traits<_RandomAccessIterator>::difference_type |
1333 | _Distance; |
1334 | typedef typename iterator_traits<_RandomAccessIterator>::value_type |
1335 | _ValueType; |
1336 | |
1337 | _Distance __n = __last - __first; |
1338 | _Distance __k = __middle - __first; |
1339 | |
1340 | if (__k == __n - __k) |
1341 | { |
1342 | std::swap_ranges(__first, __middle, __middle); |
1343 | return __middle; |
1344 | } |
1345 | |
1346 | _RandomAccessIterator __p = __first; |
1347 | _RandomAccessIterator __ret = __first + (__last - __middle); |
1348 | |
1349 | for (;;) |
1350 | { |
1351 | if (__k < __n - __k) |
1352 | { |
1353 | if (__is_pod(_ValueType) && __k == 1) |
1354 | { |
1355 | _ValueType __t = _GLIBCXX_MOVE(*__p); |
1356 | _GLIBCXX_MOVE3(__p + 1, __p + __n, __p); |
1357 | *(__p + __n - 1) = _GLIBCXX_MOVE(__t); |
1358 | return __ret; |
1359 | } |
1360 | _RandomAccessIterator __q = __p + __k; |
1361 | for (_Distance __i = 0; __i < __n - __k; ++ __i) |
1362 | { |
1363 | std::iter_swap(__p, __q); |
1364 | ++__p; |
1365 | ++__q; |
1366 | } |
1367 | __n %= __k; |
1368 | if (__n == 0) |
1369 | return __ret; |
1370 | std::swap(__n, __k); |
1371 | __k = __n - __k; |
1372 | } |
1373 | else |
1374 | { |
1375 | __k = __n - __k; |
1376 | if (__is_pod(_ValueType) && __k == 1) |
1377 | { |
1378 | _ValueType __t = _GLIBCXX_MOVE(*(__p + __n - 1)); |
1379 | _GLIBCXX_MOVE_BACKWARD3(__p, __p + __n - 1, __p + __n); |
1380 | *__p = _GLIBCXX_MOVE(__t); |
1381 | return __ret; |
1382 | } |
1383 | _RandomAccessIterator __q = __p + __n; |
1384 | __p = __q - __k; |
1385 | for (_Distance __i = 0; __i < __n - __k; ++ __i) |
1386 | { |
1387 | --__p; |
1388 | --__q; |
1389 | std::iter_swap(__p, __q); |
1390 | } |
1391 | __n %= __k; |
1392 | if (__n == 0) |
1393 | return __ret; |
1394 | std::swap(__n, __k); |
1395 | } |
1396 | } |
1397 | } |
1398 | |
1399 | |
1400 | |
1401 | |
1402 | |
1403 | |
1404 | |
1405 | |
1406 | |
1407 | |
1408 | |
1409 | |
1410 | |
1411 | |
1412 | |
1413 | |
1414 | |
1415 | |
1416 | |
1417 | |
1418 | |
1419 | |
1420 | |
1421 | |
1422 | template<typename _ForwardIterator> |
1423 | _GLIBCXX20_CONSTEXPR |
1424 | inline _ForwardIterator |
1425 | rotate(_ForwardIterator __first, _ForwardIterator __middle, |
1426 | _ForwardIterator __last) |
1427 | { |
1428 | |
1429 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
1430 | _ForwardIterator>) |
1431 | __glibcxx_requires_valid_range(__first, __middle); |
1432 | __glibcxx_requires_valid_range(__middle, __last); |
1433 | |
1434 | return std::__rotate(__first, __middle, __last, |
1435 | std::__iterator_category(__first)); |
1436 | } |
1437 | |
1438 | } |
1439 | |
1440 | |
1441 | |
1442 | |
1443 | |
1444 | |
1445 | |
1446 | |
1447 | |
1448 | |
1449 | |
1450 | |
1451 | |
1452 | |
1453 | |
1454 | |
1455 | |
1456 | |
1457 | |
1458 | |
1459 | |
1460 | template<typename _ForwardIterator, typename _OutputIterator> |
1461 | _GLIBCXX20_CONSTEXPR |
1462 | inline _OutputIterator |
1463 | rotate_copy(_ForwardIterator __first, _ForwardIterator __middle, |
1464 | _ForwardIterator __last, _OutputIterator __result) |
1465 | { |
1466 | |
1467 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
1468 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
1469 | typename iterator_traits<_ForwardIterator>::value_type>) |
1470 | __glibcxx_requires_valid_range(__first, __middle); |
1471 | __glibcxx_requires_valid_range(__middle, __last); |
1472 | |
1473 | return std::copy(__first, __middle, |
1474 | std::copy(__middle, __last, __result)); |
1475 | } |
1476 | |
1477 | |
1478 | template<typename _ForwardIterator, typename _Predicate> |
1479 | _GLIBCXX20_CONSTEXPR |
1480 | _ForwardIterator |
1481 | __partition(_ForwardIterator __first, _ForwardIterator __last, |
1482 | _Predicate __pred, forward_iterator_tag) |
1483 | { |
1484 | if (__first == __last) |
1485 | return __first; |
1486 | |
1487 | while (__pred(*__first)) |
1488 | if (++__first == __last) |
1489 | return __first; |
1490 | |
1491 | _ForwardIterator __next = __first; |
1492 | |
1493 | while (++__next != __last) |
1494 | if (__pred(*__next)) |
1495 | { |
1496 | std::iter_swap(__first, __next); |
1497 | ++__first; |
1498 | } |
1499 | |
1500 | return __first; |
1501 | } |
1502 | |
1503 | |
1504 | template<typename _BidirectionalIterator, typename _Predicate> |
1505 | _GLIBCXX20_CONSTEXPR |
1506 | _BidirectionalIterator |
1507 | __partition(_BidirectionalIterator __first, _BidirectionalIterator __last, |
1508 | _Predicate __pred, bidirectional_iterator_tag) |
1509 | { |
1510 | while (true) |
1511 | { |
1512 | while (true) |
1513 | if (__first == __last) |
1514 | return __first; |
1515 | else if (__pred(*__first)) |
1516 | ++__first; |
1517 | else |
1518 | break; |
1519 | --__last; |
1520 | while (true) |
1521 | if (__first == __last) |
1522 | return __first; |
1523 | else if (!bool(__pred(*__last))) |
1524 | --__last; |
1525 | else |
1526 | break; |
1527 | std::iter_swap(__first, __last); |
1528 | ++__first; |
1529 | } |
1530 | } |
1531 | |
1532 | |
1533 | |
1534 | |
1535 | |
1536 | |
1537 | |
1538 | |
1539 | |
1540 | template<typename _ForwardIterator, typename _Pointer, typename _Predicate, |
1541 | typename _Distance> |
1542 | _ForwardIterator |
1543 | __stable_partition_adaptive(_ForwardIterator __first, |
1544 | _ForwardIterator __last, |
1545 | _Predicate __pred, _Distance __len, |
1546 | _Pointer __buffer, |
1547 | _Distance __buffer_size) |
1548 | { |
1549 | if (__len == 1) |
1550 | return __first; |
1551 | |
1552 | if (__len <= __buffer_size) |
1553 | { |
1554 | _ForwardIterator __result1 = __first; |
1555 | _Pointer __result2 = __buffer; |
1556 | |
1557 | |
1558 | |
1559 | |
1560 | *__result2 = _GLIBCXX_MOVE(*__first); |
1561 | ++__result2; |
1562 | ++__first; |
1563 | for (; __first != __last; ++__first) |
1564 | if (__pred(__first)) |
1565 | { |
1566 | *__result1 = _GLIBCXX_MOVE(*__first); |
1567 | ++__result1; |
1568 | } |
1569 | else |
1570 | { |
1571 | *__result2 = _GLIBCXX_MOVE(*__first); |
1572 | ++__result2; |
1573 | } |
1574 | |
1575 | _GLIBCXX_MOVE3(__buffer, __result2, __result1); |
1576 | return __result1; |
1577 | } |
1578 | |
1579 | _ForwardIterator __middle = __first; |
1580 | std::advance(__middle, __len / 2); |
1581 | _ForwardIterator __left_split = |
1582 | std::__stable_partition_adaptive(__first, __middle, __pred, |
1583 | __len / 2, __buffer, |
1584 | __buffer_size); |
1585 | |
1586 | |
1587 | |
1588 | _Distance __right_len = __len - __len / 2; |
1589 | _ForwardIterator __right_split = |
1590 | std::__find_if_not_n(__middle, __right_len, __pred); |
1591 | |
1592 | if (__right_len) |
1593 | __right_split = |
1594 | std::__stable_partition_adaptive(__right_split, __last, __pred, |
1595 | __right_len, |
1596 | __buffer, __buffer_size); |
1597 | |
1598 | return std::rotate(__left_split, __middle, __right_split); |
1599 | } |
1600 | |
1601 | template<typename _ForwardIterator, typename _Predicate> |
1602 | _ForwardIterator |
1603 | __stable_partition(_ForwardIterator __first, _ForwardIterator __last, |
1604 | _Predicate __pred) |
1605 | { |
1606 | __first = std::__find_if_not(__first, __last, __pred); |
1607 | |
1608 | if (__first == __last) |
1609 | return __first; |
1610 | |
1611 | typedef typename iterator_traits<_ForwardIterator>::value_type |
1612 | _ValueType; |
1613 | typedef typename iterator_traits<_ForwardIterator>::difference_type |
1614 | _DistanceType; |
1615 | |
1616 | _Temporary_buffer<_ForwardIterator, _ValueType> |
1617 | __buf(__first, std::distance(__first, __last)); |
1618 | return |
1619 | std::__stable_partition_adaptive(__first, __last, __pred, |
1620 | _DistanceType(__buf.requested_size()), |
1621 | __buf.begin(), |
1622 | _DistanceType(__buf.size())); |
1623 | } |
1624 | |
1625 | |
1626 | |
1627 | |
1628 | |
1629 | |
1630 | |
1631 | |
1632 | |
1633 | |
1634 | |
1635 | |
1636 | |
1637 | |
1638 | |
1639 | |
1640 | |
1641 | |
1642 | template<typename _ForwardIterator, typename _Predicate> |
1643 | inline _ForwardIterator |
1644 | stable_partition(_ForwardIterator __first, _ForwardIterator __last, |
1645 | _Predicate __pred) |
1646 | { |
1647 | |
1648 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
1649 | _ForwardIterator>) |
1650 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
1651 | typename iterator_traits<_ForwardIterator>::value_type>) |
1652 | __glibcxx_requires_valid_range(__first, __last); |
1653 | |
1654 | return std::__stable_partition(__first, __last, |
1655 | __gnu_cxx::__ops::__pred_iter(__pred)); |
1656 | } |
1657 | |
1658 | |
1659 | template<typename _RandomAccessIterator, typename _Compare> |
1660 | _GLIBCXX20_CONSTEXPR |
1661 | void |
1662 | __heap_select(_RandomAccessIterator __first, |
1663 | _RandomAccessIterator __middle, |
1664 | _RandomAccessIterator __last, _Compare __comp) |
1665 | { |
1666 | std::__make_heap(__first, __middle, __comp); |
1667 | for (_RandomAccessIterator __i = __middle; __i < __last; ++__i) |
1668 | if (__comp(__i, __first)) |
1669 | std::__pop_heap(__first, __middle, __i, __comp); |
1670 | } |
1671 | |
1672 | |
1673 | |
1674 | template<typename _InputIterator, typename _RandomAccessIterator, |
1675 | typename _Compare> |
1676 | _GLIBCXX20_CONSTEXPR |
1677 | _RandomAccessIterator |
1678 | __partial_sort_copy(_InputIterator __first, _InputIterator __last, |
1679 | _RandomAccessIterator __result_first, |
1680 | _RandomAccessIterator __result_last, |
1681 | _Compare __comp) |
1682 | { |
1683 | typedef typename iterator_traits<_InputIterator>::value_type |
1684 | _InputValueType; |
1685 | typedef iterator_traits<_RandomAccessIterator> _RItTraits; |
1686 | typedef typename _RItTraits::difference_type _DistanceType; |
1687 | |
1688 | if (__result_first == __result_last) |
1689 | return __result_last; |
1690 | _RandomAccessIterator __result_real_last = __result_first; |
1691 | while (__first != __last && __result_real_last != __result_last) |
1692 | { |
1693 | *__result_real_last = *__first; |
1694 | ++__result_real_last; |
1695 | ++__first; |
1696 | } |
1697 | |
1698 | std::__make_heap(__result_first, __result_real_last, __comp); |
1699 | while (__first != __last) |
1700 | { |
1701 | if (__comp(__first, __result_first)) |
1702 | std::__adjust_heap(__result_first, _DistanceType(0), |
1703 | _DistanceType(__result_real_last |
1704 | - __result_first), |
1705 | _InputValueType(*__first), __comp); |
1706 | ++__first; |
1707 | } |
1708 | std::__sort_heap(__result_first, __result_real_last, __comp); |
1709 | return __result_real_last; |
1710 | } |
1711 | |
1712 | |
1713 | |
1714 | |
1715 | |
1716 | |
1717 | |
1718 | |
1719 | |
1720 | |
1721 | |
1722 | |
1723 | |
1724 | |
1725 | |
1726 | |
1727 | |
1728 | |
1729 | |
1730 | template<typename _InputIterator, typename _RandomAccessIterator> |
1731 | _GLIBCXX20_CONSTEXPR |
1732 | inline _RandomAccessIterator |
1733 | partial_sort_copy(_InputIterator __first, _InputIterator __last, |
1734 | _RandomAccessIterator __result_first, |
1735 | _RandomAccessIterator __result_last) |
1736 | { |
1737 | #ifdef _GLIBCXX_CONCEPT_CHECKS |
1738 | typedef typename iterator_traits<_InputIterator>::value_type |
1739 | _InputValueType; |
1740 | typedef typename iterator_traits<_RandomAccessIterator>::value_type |
1741 | _OutputValueType; |
1742 | #endif |
1743 | |
1744 | |
1745 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
1746 | __glibcxx_function_requires(_ConvertibleConcept<_InputValueType, |
1747 | _OutputValueType>) |
1748 | __glibcxx_function_requires(_LessThanOpConcept<_InputValueType, |
1749 | _OutputValueType>) |
1750 | __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>) |
1751 | __glibcxx_requires_valid_range(__first, __last); |
1752 | __glibcxx_requires_irreflexive(__first, __last); |
1753 | __glibcxx_requires_valid_range(__result_first, __result_last); |
1754 | |
1755 | return std::__partial_sort_copy(__first, __last, |
1756 | __result_first, __result_last, |
1757 | __gnu_cxx::__ops::__iter_less_iter()); |
1758 | } |
1759 | |
1760 | |
1761 | |
1762 | |
1763 | |
1764 | |
1765 | |
1766 | |
1767 | |
1768 | |
1769 | |
1770 | |
1771 | |
1772 | |
1773 | |
1774 | |
1775 | |
1776 | |
1777 | |
1778 | |
1779 | |
1780 | template<typename _InputIterator, typename _RandomAccessIterator, |
1781 | typename _Compare> |
1782 | _GLIBCXX20_CONSTEXPR |
1783 | inline _RandomAccessIterator |
1784 | partial_sort_copy(_InputIterator __first, _InputIterator __last, |
1785 | _RandomAccessIterator __result_first, |
1786 | _RandomAccessIterator __result_last, |
1787 | _Compare __comp) |
1788 | { |
1789 | #ifdef _GLIBCXX_CONCEPT_CHECKS |
1790 | typedef typename iterator_traits<_InputIterator>::value_type |
1791 | _InputValueType; |
1792 | typedef typename iterator_traits<_RandomAccessIterator>::value_type |
1793 | _OutputValueType; |
1794 | #endif |
1795 | |
1796 | |
1797 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
1798 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
1799 | _RandomAccessIterator>) |
1800 | __glibcxx_function_requires(_ConvertibleConcept<_InputValueType, |
1801 | _OutputValueType>) |
1802 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
1803 | _InputValueType, _OutputValueType>) |
1804 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
1805 | _OutputValueType, _OutputValueType>) |
1806 | __glibcxx_requires_valid_range(__first, __last); |
1807 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
1808 | __glibcxx_requires_valid_range(__result_first, __result_last); |
1809 | |
1810 | return std::__partial_sort_copy(__first, __last, |
1811 | __result_first, __result_last, |
1812 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
1813 | } |
1814 | |
1815 | |
1816 | template<typename _RandomAccessIterator, typename _Compare> |
1817 | _GLIBCXX20_CONSTEXPR |
1818 | void |
1819 | __unguarded_linear_insert(_RandomAccessIterator __last, |
1820 | _Compare __comp) |
1821 | { |
1822 | typename iterator_traits<_RandomAccessIterator>::value_type |
1823 | __val = _GLIBCXX_MOVE(*__last); |
1824 | _RandomAccessIterator __next = __last; |
1825 | --__next; |
1826 | while (__comp(__val, __next)) |
1827 | { |
1828 | *__last = _GLIBCXX_MOVE(*__next); |
1829 | __last = __next; |
1830 | --__next; |
1831 | } |
1832 | *__last = _GLIBCXX_MOVE(__val); |
1833 | } |
1834 | |
1835 | |
1836 | template<typename _RandomAccessIterator, typename _Compare> |
1837 | _GLIBCXX20_CONSTEXPR |
1838 | void |
1839 | __insertion_sort(_RandomAccessIterator __first, |
1840 | _RandomAccessIterator __last, _Compare __comp) |
1841 | { |
1842 | if (__first == __last) return; |
1843 | |
1844 | for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) |
1845 | { |
1846 | if (__comp(__i, __first)) |
1847 | { |
1848 | typename iterator_traits<_RandomAccessIterator>::value_type |
1849 | __val = _GLIBCXX_MOVE(*__i); |
1850 | _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1); |
1851 | *__first = _GLIBCXX_MOVE(__val); |
1852 | } |
1853 | else |
1854 | std::__unguarded_linear_insert(__i, |
1855 | __gnu_cxx::__ops::__val_comp_iter(__comp)); |
1856 | } |
1857 | } |
1858 | |
1859 | |
1860 | template<typename _RandomAccessIterator, typename _Compare> |
1861 | _GLIBCXX20_CONSTEXPR |
1862 | inline void |
1863 | __unguarded_insertion_sort(_RandomAccessIterator __first, |
1864 | _RandomAccessIterator __last, _Compare __comp) |
1865 | { |
1866 | for (_RandomAccessIterator __i = __first; __i != __last; ++__i) |
1867 | std::__unguarded_linear_insert(__i, |
1868 | __gnu_cxx::__ops::__val_comp_iter(__comp)); |
1869 | } |
1870 | |
1871 | |
1872 | |
1873 | |
1874 | |
1875 | enum { _S_threshold = 16 }; |
1876 | |
1877 | |
1878 | template<typename _RandomAccessIterator, typename _Compare> |
1879 | _GLIBCXX20_CONSTEXPR |
1880 | void |
1881 | __final_insertion_sort(_RandomAccessIterator __first, |
1882 | _RandomAccessIterator __last, _Compare __comp) |
1883 | { |
1884 | if (__last - __first > int(_S_threshold)) |
1885 | { |
1886 | std::__insertion_sort(__first, __first + int(_S_threshold), __comp); |
1887 | std::__unguarded_insertion_sort(__first + int(_S_threshold), __last, |
1888 | __comp); |
1889 | } |
1890 | else |
1891 | std::__insertion_sort(__first, __last, __comp); |
1892 | } |
1893 | |
1894 | |
1895 | template<typename _RandomAccessIterator, typename _Compare> |
1896 | _GLIBCXX20_CONSTEXPR |
1897 | _RandomAccessIterator |
1898 | __unguarded_partition(_RandomAccessIterator __first, |
1899 | _RandomAccessIterator __last, |
1900 | _RandomAccessIterator __pivot, _Compare __comp) |
1901 | { |
1902 | while (true) |
1903 | { |
1904 | while (__comp(__first, __pivot)) |
1905 | ++__first; |
1906 | --__last; |
1907 | while (__comp(__pivot, __last)) |
1908 | --__last; |
1909 | if (!(__first < __last)) |
1910 | return __first; |
1911 | std::iter_swap(__first, __last); |
1912 | ++__first; |
1913 | } |
1914 | } |
1915 | |
1916 | |
1917 | template<typename _RandomAccessIterator, typename _Compare> |
1918 | _GLIBCXX20_CONSTEXPR |
1919 | inline _RandomAccessIterator |
1920 | __unguarded_partition_pivot(_RandomAccessIterator __first, |
1921 | _RandomAccessIterator __last, _Compare __comp) |
1922 | { |
1923 | _RandomAccessIterator __mid = __first + (__last - __first) / 2; |
1924 | std::__move_median_to_first(__first, __first + 1, __mid, __last - 1, |
| 32 | | Calling '__move_median_to_first<int *, __gnu_cxx::__ops::_Iter_less_iter>' | |
|
1925 | __comp); |
1926 | return std::__unguarded_partition(__first + 1, __last, __first, __comp); |
1927 | } |
1928 | |
1929 | template<typename _RandomAccessIterator, typename _Compare> |
1930 | _GLIBCXX20_CONSTEXPR |
1931 | inline void |
1932 | __partial_sort(_RandomAccessIterator __first, |
1933 | _RandomAccessIterator __middle, |
1934 | _RandomAccessIterator __last, |
1935 | _Compare __comp) |
1936 | { |
1937 | std::__heap_select(__first, __middle, __last, __comp); |
1938 | std::__sort_heap(__first, __middle, __comp); |
1939 | } |
1940 | |
1941 | |
1942 | template<typename _RandomAccessIterator, typename _Size, typename _Compare> |
1943 | _GLIBCXX20_CONSTEXPR |
1944 | void |
1945 | __introsort_loop(_RandomAccessIterator __first, |
1946 | _RandomAccessIterator __last, |
1947 | _Size __depth_limit, _Compare __comp) |
1948 | { |
1949 | while (__last - __first > int(_S_threshold)) |
| 27 | | Assuming the condition is true | |
|
| 28 | | Loop condition is true. Entering loop body | |
|
1950 | { |
1951 | if (__depth_limit == 0) |
| 29 | | Assuming '__depth_limit' is not equal to 0 | |
|
| |
1952 | { |
1953 | std::__partial_sort(__first, __last, __last, __comp); |
1954 | return; |
1955 | } |
1956 | --__depth_limit; |
1957 | _RandomAccessIterator __cut = |
1958 | std::__unguarded_partition_pivot(__first, __last, __comp); |
| 31 | | Calling '__unguarded_partition_pivot<int *, __gnu_cxx::__ops::_Iter_less_iter>' | |
|
1959 | std::__introsort_loop(__cut, __last, __depth_limit, __comp); |
1960 | __last = __cut; |
1961 | } |
1962 | } |
1963 | |
1964 | |
1965 | |
1966 | template<typename _RandomAccessIterator, typename _Compare> |
1967 | _GLIBCXX20_CONSTEXPR |
1968 | inline void |
1969 | __sort(_RandomAccessIterator __first, _RandomAccessIterator __last, |
1970 | _Compare __comp) |
1971 | { |
1972 | if (__first != __last) |
| 24 | | Assuming '__first' is not equal to '__last' | |
|
| |
1973 | { |
1974 | std::__introsort_loop(__first, __last, |
| 26 | | Calling '__introsort_loop<int *, long, __gnu_cxx::__ops::_Iter_less_iter>' | |
|
1975 | std::__lg(__last - __first) * 2, |
1976 | __comp); |
1977 | std::__final_insertion_sort(__first, __last, __comp); |
1978 | } |
1979 | } |
1980 | |
1981 | template<typename _RandomAccessIterator, typename _Size, typename _Compare> |
1982 | _GLIBCXX20_CONSTEXPR |
1983 | void |
1984 | __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth, |
1985 | _RandomAccessIterator __last, _Size __depth_limit, |
1986 | _Compare __comp) |
1987 | { |
1988 | while (__last - __first > 3) |
1989 | { |
1990 | if (__depth_limit == 0) |
1991 | { |
1992 | std::__heap_select(__first, __nth + 1, __last, __comp); |
1993 | |
1994 | std::iter_swap(__first, __nth); |
1995 | return; |
1996 | } |
1997 | --__depth_limit; |
1998 | _RandomAccessIterator __cut = |
1999 | std::__unguarded_partition_pivot(__first, __last, __comp); |
2000 | if (__cut <= __nth) |
2001 | __first = __cut; |
2002 | else |
2003 | __last = __cut; |
2004 | } |
2005 | std::__insertion_sort(__first, __last, __comp); |
2006 | } |
2007 | |
2008 | |
2009 | |
2010 | |
2011 | |
2012 | |
2013 | |
2014 | |
2015 | |
2016 | |
2017 | |
2018 | |
2019 | |
2020 | |
2021 | |
2022 | |
2023 | |
2024 | |
2025 | |
2026 | |
2027 | |
2028 | template<typename _ForwardIterator, typename _Tp, typename _Compare> |
2029 | _GLIBCXX20_CONSTEXPR |
2030 | inline _ForwardIterator |
2031 | lower_bound(_ForwardIterator __first, _ForwardIterator __last, |
2032 | const _Tp& __val, _Compare __comp) |
2033 | { |
2034 | |
2035 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
2036 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
2037 | typename iterator_traits<_ForwardIterator>::value_type, _Tp>) |
2038 | __glibcxx_requires_partitioned_lower_pred(__first, __last, |
2039 | __val, __comp); |
2040 | |
2041 | return std::__lower_bound(__first, __last, __val, |
2042 | __gnu_cxx::__ops::__iter_comp_val(__comp)); |
2043 | } |
2044 | |
2045 | template<typename _ForwardIterator, typename _Tp, typename _Compare> |
2046 | _GLIBCXX20_CONSTEXPR |
2047 | _ForwardIterator |
2048 | __upper_bound(_ForwardIterator __first, _ForwardIterator __last, |
2049 | const _Tp& __val, _Compare __comp) |
2050 | { |
2051 | typedef typename iterator_traits<_ForwardIterator>::difference_type |
2052 | _DistanceType; |
2053 | |
2054 | _DistanceType __len = std::distance(__first, __last); |
2055 | |
2056 | while (__len > 0) |
2057 | { |
2058 | _DistanceType __half = __len >> 1; |
2059 | _ForwardIterator __middle = __first; |
2060 | std::advance(__middle, __half); |
2061 | if (__comp(__val, __middle)) |
2062 | __len = __half; |
2063 | else |
2064 | { |
2065 | __first = __middle; |
2066 | ++__first; |
2067 | __len = __len - __half - 1; |
2068 | } |
2069 | } |
2070 | return __first; |
2071 | } |
2072 | |
2073 | |
2074 | |
2075 | |
2076 | |
2077 | |
2078 | |
2079 | |
2080 | |
2081 | |
2082 | |
2083 | |
2084 | template<typename _ForwardIterator, typename _Tp> |
2085 | _GLIBCXX20_CONSTEXPR |
2086 | inline _ForwardIterator |
2087 | upper_bound(_ForwardIterator __first, _ForwardIterator __last, |
2088 | const _Tp& __val) |
2089 | { |
2090 | |
2091 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
2092 | __glibcxx_function_requires(_LessThanOpConcept< |
2093 | _Tp, typename iterator_traits<_ForwardIterator>::value_type>) |
2094 | __glibcxx_requires_partitioned_upper(__first, __last, __val); |
2095 | |
2096 | return std::__upper_bound(__first, __last, __val, |
2097 | __gnu_cxx::__ops::__val_less_iter()); |
2098 | } |
2099 | |
2100 | |
2101 | |
2102 | |
2103 | |
2104 | |
2105 | |
2106 | |
2107 | |
2108 | |
2109 | |
2110 | |
2111 | |
2112 | |
2113 | |
2114 | |
2115 | template<typename _ForwardIterator, typename _Tp, typename _Compare> |
2116 | _GLIBCXX20_CONSTEXPR |
2117 | inline _ForwardIterator |
2118 | upper_bound(_ForwardIterator __first, _ForwardIterator __last, |
2119 | const _Tp& __val, _Compare __comp) |
2120 | { |
2121 | |
2122 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
2123 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
2124 | _Tp, typename iterator_traits<_ForwardIterator>::value_type>) |
2125 | __glibcxx_requires_partitioned_upper_pred(__first, __last, |
2126 | __val, __comp); |
2127 | |
2128 | return std::__upper_bound(__first, __last, __val, |
2129 | __gnu_cxx::__ops::__val_comp_iter(__comp)); |
2130 | } |
2131 | |
2132 | template<typename _ForwardIterator, typename _Tp, |
2133 | typename _CompareItTp, typename _CompareTpIt> |
2134 | _GLIBCXX20_CONSTEXPR |
2135 | pair<_ForwardIterator, _ForwardIterator> |
2136 | __equal_range(_ForwardIterator __first, _ForwardIterator __last, |
2137 | const _Tp& __val, |
2138 | _CompareItTp __comp_it_val, _CompareTpIt __comp_val_it) |
2139 | { |
2140 | typedef typename iterator_traits<_ForwardIterator>::difference_type |
2141 | _DistanceType; |
2142 | |
2143 | _DistanceType __len = std::distance(__first, __last); |
2144 | |
2145 | while (__len > 0) |
2146 | { |
2147 | _DistanceType __half = __len >> 1; |
2148 | _ForwardIterator __middle = __first; |
2149 | std::advance(__middle, __half); |
2150 | if (__comp_it_val(__middle, __val)) |
2151 | { |
2152 | __first = __middle; |
2153 | ++__first; |
2154 | __len = __len - __half - 1; |
2155 | } |
2156 | else if (__comp_val_it(__val, __middle)) |
2157 | __len = __half; |
2158 | else |
2159 | { |
2160 | _ForwardIterator __left |
2161 | = std::__lower_bound(__first, __middle, __val, __comp_it_val); |
2162 | std::advance(__first, __len); |
2163 | _ForwardIterator __right |
2164 | = std::__upper_bound(++__middle, __first, __val, __comp_val_it); |
2165 | return pair<_ForwardIterator, _ForwardIterator>(__left, __right); |
2166 | } |
2167 | } |
2168 | return pair<_ForwardIterator, _ForwardIterator>(__first, __first); |
2169 | } |
2170 | |
2171 | |
2172 | |
2173 | |
2174 | |
2175 | |
2176 | |
2177 | |
2178 | |
2179 | |
2180 | |
2181 | |
2182 | |
2183 | |
2184 | |
2185 | |
2186 | |
2187 | |
2188 | template<typename _ForwardIterator, typename _Tp> |
2189 | _GLIBCXX20_CONSTEXPR |
2190 | inline pair<_ForwardIterator, _ForwardIterator> |
2191 | equal_range(_ForwardIterator __first, _ForwardIterator __last, |
2192 | const _Tp& __val) |
2193 | { |
2194 | |
2195 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
2196 | __glibcxx_function_requires(_LessThanOpConcept< |
2197 | typename iterator_traits<_ForwardIterator>::value_type, _Tp>) |
2198 | __glibcxx_function_requires(_LessThanOpConcept< |
2199 | _Tp, typename iterator_traits<_ForwardIterator>::value_type>) |
2200 | __glibcxx_requires_partitioned_lower(__first, __last, __val); |
2201 | __glibcxx_requires_partitioned_upper(__first, __last, __val); |
2202 | |
2203 | return std::__equal_range(__first, __last, __val, |
2204 | __gnu_cxx::__ops::__iter_less_val(), |
2205 | __gnu_cxx::__ops::__val_less_iter()); |
2206 | } |
2207 | |
2208 | |
2209 | |
2210 | |
2211 | |
2212 | |
2213 | |
2214 | |
2215 | |
2216 | |
2217 | |
2218 | |
2219 | |
2220 | |
2221 | |
2222 | |
2223 | |
2224 | |
2225 | template<typename _ForwardIterator, typename _Tp, typename _Compare> |
2226 | _GLIBCXX20_CONSTEXPR |
2227 | inline pair<_ForwardIterator, _ForwardIterator> |
2228 | equal_range(_ForwardIterator __first, _ForwardIterator __last, |
2229 | const _Tp& __val, _Compare __comp) |
2230 | { |
2231 | |
2232 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
2233 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
2234 | typename iterator_traits<_ForwardIterator>::value_type, _Tp>) |
2235 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
2236 | _Tp, typename iterator_traits<_ForwardIterator>::value_type>) |
2237 | __glibcxx_requires_partitioned_lower_pred(__first, __last, |
2238 | __val, __comp); |
2239 | __glibcxx_requires_partitioned_upper_pred(__first, __last, |
2240 | __val, __comp); |
2241 | |
2242 | return std::__equal_range(__first, __last, __val, |
2243 | __gnu_cxx::__ops::__iter_comp_val(__comp), |
2244 | __gnu_cxx::__ops::__val_comp_iter(__comp)); |
2245 | } |
2246 | |
2247 | |
2248 | |
2249 | |
2250 | |
2251 | |
2252 | |
2253 | |
2254 | |
2255 | |
2256 | |
2257 | |
2258 | |
2259 | template<typename _ForwardIterator, typename _Tp> |
2260 | _GLIBCXX20_CONSTEXPR |
2261 | bool |
2262 | binary_search(_ForwardIterator __first, _ForwardIterator __last, |
2263 | const _Tp& __val) |
2264 | { |
2265 | |
2266 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
2267 | __glibcxx_function_requires(_LessThanOpConcept< |
2268 | _Tp, typename iterator_traits<_ForwardIterator>::value_type>) |
2269 | __glibcxx_requires_partitioned_lower(__first, __last, __val); |
2270 | __glibcxx_requires_partitioned_upper(__first, __last, __val); |
2271 | |
2272 | _ForwardIterator __i |
2273 | = std::__lower_bound(__first, __last, __val, |
2274 | __gnu_cxx::__ops::__iter_less_val()); |
2275 | return __i != __last && !(__val < *__i); |
2276 | } |
2277 | |
2278 | |
2279 | |
2280 | |
2281 | |
2282 | |
2283 | |
2284 | |
2285 | |
2286 | |
2287 | |
2288 | |
2289 | |
2290 | |
2291 | |
2292 | |
2293 | template<typename _ForwardIterator, typename _Tp, typename _Compare> |
2294 | _GLIBCXX20_CONSTEXPR |
2295 | bool |
2296 | binary_search(_ForwardIterator __first, _ForwardIterator __last, |
2297 | const _Tp& __val, _Compare __comp) |
2298 | { |
2299 | |
2300 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
2301 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
2302 | _Tp, typename iterator_traits<_ForwardIterator>::value_type>) |
2303 | __glibcxx_requires_partitioned_lower_pred(__first, __last, |
2304 | __val, __comp); |
2305 | __glibcxx_requires_partitioned_upper_pred(__first, __last, |
2306 | __val, __comp); |
2307 | |
2308 | _ForwardIterator __i |
2309 | = std::__lower_bound(__first, __last, __val, |
2310 | __gnu_cxx::__ops::__iter_comp_val(__comp)); |
2311 | return __i != __last && !bool(__comp(__val, *__i)); |
2312 | } |
2313 | |
2314 | |
2315 | |
2316 | |
2317 | template<typename _InputIterator1, typename _InputIterator2, |
2318 | typename _OutputIterator, typename _Compare> |
2319 | void |
2320 | __move_merge_adaptive(_InputIterator1 __first1, _InputIterator1 __last1, |
2321 | _InputIterator2 __first2, _InputIterator2 __last2, |
2322 | _OutputIterator __result, _Compare __comp) |
2323 | { |
2324 | while (__first1 != __last1 && __first2 != __last2) |
2325 | { |
2326 | if (__comp(__first2, __first1)) |
2327 | { |
2328 | *__result = _GLIBCXX_MOVE(*__first2); |
2329 | ++__first2; |
2330 | } |
2331 | else |
2332 | { |
2333 | *__result = _GLIBCXX_MOVE(*__first1); |
2334 | ++__first1; |
2335 | } |
2336 | ++__result; |
2337 | } |
2338 | if (__first1 != __last1) |
2339 | _GLIBCXX_MOVE3(__first1, __last1, __result); |
2340 | } |
2341 | |
2342 | |
2343 | template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, |
2344 | typename _BidirectionalIterator3, typename _Compare> |
2345 | void |
2346 | __move_merge_adaptive_backward(_BidirectionalIterator1 __first1, |
2347 | _BidirectionalIterator1 __last1, |
2348 | _BidirectionalIterator2 __first2, |
2349 | _BidirectionalIterator2 __last2, |
2350 | _BidirectionalIterator3 __result, |
2351 | _Compare __comp) |
2352 | { |
2353 | if (__first1 == __last1) |
2354 | { |
2355 | _GLIBCXX_MOVE_BACKWARD3(__first2, __last2, __result); |
2356 | return; |
2357 | } |
2358 | else if (__first2 == __last2) |
2359 | return; |
2360 | |
2361 | --__last1; |
2362 | --__last2; |
2363 | while (true) |
2364 | { |
2365 | if (__comp(__last2, __last1)) |
2366 | { |
2367 | *--__result = _GLIBCXX_MOVE(*__last1); |
2368 | if (__first1 == __last1) |
2369 | { |
2370 | _GLIBCXX_MOVE_BACKWARD3(__first2, ++__last2, __result); |
2371 | return; |
2372 | } |
2373 | --__last1; |
2374 | } |
2375 | else |
2376 | { |
2377 | *--__result = _GLIBCXX_MOVE(*__last2); |
2378 | if (__first2 == __last2) |
2379 | return; |
2380 | --__last2; |
2381 | } |
2382 | } |
2383 | } |
2384 | |
2385 | |
2386 | template<typename _BidirectionalIterator1, typename _BidirectionalIterator2, |
2387 | typename _Distance> |
2388 | _BidirectionalIterator1 |
2389 | __rotate_adaptive(_BidirectionalIterator1 __first, |
2390 | _BidirectionalIterator1 __middle, |
2391 | _BidirectionalIterator1 __last, |
2392 | _Distance __len1, _Distance __len2, |
2393 | _BidirectionalIterator2 __buffer, |
2394 | _Distance __buffer_size) |
2395 | { |
2396 | _BidirectionalIterator2 __buffer_end; |
2397 | if (__len1 > __len2 && __len2 <= __buffer_size) |
2398 | { |
2399 | if (__len2) |
2400 | { |
2401 | __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer); |
2402 | _GLIBCXX_MOVE_BACKWARD3(__first, __middle, __last); |
2403 | return _GLIBCXX_MOVE3(__buffer, __buffer_end, __first); |
2404 | } |
2405 | else |
2406 | return __first; |
2407 | } |
2408 | else if (__len1 <= __buffer_size) |
2409 | { |
2410 | if (__len1) |
2411 | { |
2412 | __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer); |
2413 | _GLIBCXX_MOVE3(__middle, __last, __first); |
2414 | return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last); |
2415 | } |
2416 | else |
2417 | return __last; |
2418 | } |
2419 | else |
2420 | return std::rotate(__first, __middle, __last); |
2421 | } |
2422 | |
2423 | |
2424 | template<typename _BidirectionalIterator, typename _Distance, |
2425 | typename _Pointer, typename _Compare> |
2426 | void |
2427 | __merge_adaptive(_BidirectionalIterator __first, |
2428 | _BidirectionalIterator __middle, |
2429 | _BidirectionalIterator __last, |
2430 | _Distance __len1, _Distance __len2, |
2431 | _Pointer __buffer, _Distance __buffer_size, |
2432 | _Compare __comp) |
2433 | { |
2434 | if (__len1 <= __len2 && __len1 <= __buffer_size) |
2435 | { |
2436 | _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer); |
2437 | std::__move_merge_adaptive(__buffer, __buffer_end, __middle, __last, |
2438 | __first, __comp); |
2439 | } |
2440 | else if (__len2 <= __buffer_size) |
2441 | { |
2442 | _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer); |
2443 | std::__move_merge_adaptive_backward(__first, __middle, __buffer, |
2444 | __buffer_end, __last, __comp); |
2445 | } |
2446 | else |
2447 | { |
2448 | _BidirectionalIterator __first_cut = __first; |
2449 | _BidirectionalIterator __second_cut = __middle; |
2450 | _Distance __len11 = 0; |
2451 | _Distance __len22 = 0; |
2452 | if (__len1 > __len2) |
2453 | { |
2454 | __len11 = __len1 / 2; |
2455 | std::advance(__first_cut, __len11); |
2456 | __second_cut |
2457 | = std::__lower_bound(__middle, __last, *__first_cut, |
2458 | __gnu_cxx::__ops::__iter_comp_val(__comp)); |
2459 | __len22 = std::distance(__middle, __second_cut); |
2460 | } |
2461 | else |
2462 | { |
2463 | __len22 = __len2 / 2; |
2464 | std::advance(__second_cut, __len22); |
2465 | __first_cut |
2466 | = std::__upper_bound(__first, __middle, *__second_cut, |
2467 | __gnu_cxx::__ops::__val_comp_iter(__comp)); |
2468 | __len11 = std::distance(__first, __first_cut); |
2469 | } |
2470 | |
2471 | _BidirectionalIterator __new_middle |
2472 | = std::__rotate_adaptive(__first_cut, __middle, __second_cut, |
2473 | __len1 - __len11, __len22, __buffer, |
2474 | __buffer_size); |
2475 | std::__merge_adaptive(__first, __first_cut, __new_middle, __len11, |
2476 | __len22, __buffer, __buffer_size, __comp); |
2477 | std::__merge_adaptive(__new_middle, __second_cut, __last, |
2478 | __len1 - __len11, |
2479 | __len2 - __len22, __buffer, |
2480 | __buffer_size, __comp); |
2481 | } |
2482 | } |
2483 | |
2484 | |
2485 | template<typename _BidirectionalIterator, typename _Distance, |
2486 | typename _Compare> |
2487 | void |
2488 | __merge_without_buffer(_BidirectionalIterator __first, |
2489 | _BidirectionalIterator __middle, |
2490 | _BidirectionalIterator __last, |
2491 | _Distance __len1, _Distance __len2, |
2492 | _Compare __comp) |
2493 | { |
2494 | if (__len1 == 0 || __len2 == 0) |
2495 | return; |
2496 | |
2497 | if (__len1 + __len2 == 2) |
2498 | { |
2499 | if (__comp(__middle, __first)) |
2500 | std::iter_swap(__first, __middle); |
2501 | return; |
2502 | } |
2503 | |
2504 | _BidirectionalIterator __first_cut = __first; |
2505 | _BidirectionalIterator __second_cut = __middle; |
2506 | _Distance __len11 = 0; |
2507 | _Distance __len22 = 0; |
2508 | if (__len1 > __len2) |
2509 | { |
2510 | __len11 = __len1 / 2; |
2511 | std::advance(__first_cut, __len11); |
2512 | __second_cut |
2513 | = std::__lower_bound(__middle, __last, *__first_cut, |
2514 | __gnu_cxx::__ops::__iter_comp_val(__comp)); |
2515 | __len22 = std::distance(__middle, __second_cut); |
2516 | } |
2517 | else |
2518 | { |
2519 | __len22 = __len2 / 2; |
2520 | std::advance(__second_cut, __len22); |
2521 | __first_cut |
2522 | = std::__upper_bound(__first, __middle, *__second_cut, |
2523 | __gnu_cxx::__ops::__val_comp_iter(__comp)); |
2524 | __len11 = std::distance(__first, __first_cut); |
2525 | } |
2526 | |
2527 | _BidirectionalIterator __new_middle |
2528 | = std::rotate(__first_cut, __middle, __second_cut); |
2529 | std::__merge_without_buffer(__first, __first_cut, __new_middle, |
2530 | __len11, __len22, __comp); |
2531 | std::__merge_without_buffer(__new_middle, __second_cut, __last, |
2532 | __len1 - __len11, __len2 - __len22, __comp); |
2533 | } |
2534 | |
2535 | template<typename _BidirectionalIterator, typename _Compare> |
2536 | void |
2537 | __inplace_merge(_BidirectionalIterator __first, |
2538 | _BidirectionalIterator __middle, |
2539 | _BidirectionalIterator __last, |
2540 | _Compare __comp) |
2541 | { |
2542 | typedef typename iterator_traits<_BidirectionalIterator>::value_type |
2543 | _ValueType; |
2544 | typedef typename iterator_traits<_BidirectionalIterator>::difference_type |
2545 | _DistanceType; |
2546 | |
2547 | if (__first == __middle || __middle == __last) |
2548 | return; |
2549 | |
2550 | const _DistanceType __len1 = std::distance(__first, __middle); |
2551 | const _DistanceType __len2 = std::distance(__middle, __last); |
2552 | |
2553 | typedef _Temporary_buffer<_BidirectionalIterator, _ValueType> _TmpBuf; |
2554 | _TmpBuf __buf(__first, __len1 + __len2); |
2555 | |
2556 | if (__buf.begin() == 0) |
2557 | std::__merge_without_buffer |
2558 | (__first, __middle, __last, __len1, __len2, __comp); |
2559 | else |
2560 | std::__merge_adaptive |
2561 | (__first, __middle, __last, __len1, __len2, __buf.begin(), |
2562 | _DistanceType(__buf.size()), __comp); |
2563 | } |
2564 | |
2565 | |
2566 | |
2567 | |
2568 | |
2569 | |
2570 | |
2571 | |
2572 | |
2573 | |
2574 | |
2575 | |
2576 | |
2577 | |
2578 | |
2579 | |
2580 | |
2581 | |
2582 | |
2583 | template<typename _BidirectionalIterator> |
2584 | inline void |
2585 | inplace_merge(_BidirectionalIterator __first, |
2586 | _BidirectionalIterator __middle, |
2587 | _BidirectionalIterator __last) |
2588 | { |
2589 | |
2590 | __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< |
2591 | _BidirectionalIterator>) |
2592 | __glibcxx_function_requires(_LessThanComparableConcept< |
2593 | typename iterator_traits<_BidirectionalIterator>::value_type>) |
2594 | __glibcxx_requires_sorted(__first, __middle); |
2595 | __glibcxx_requires_sorted(__middle, __last); |
2596 | __glibcxx_requires_irreflexive(__first, __last); |
2597 | |
2598 | std::__inplace_merge(__first, __middle, __last, |
2599 | __gnu_cxx::__ops::__iter_less_iter()); |
2600 | } |
2601 | |
2602 | |
2603 | |
2604 | |
2605 | |
2606 | |
2607 | |
2608 | |
2609 | |
2610 | |
2611 | |
2612 | |
2613 | |
2614 | |
2615 | |
2616 | |
2617 | |
2618 | |
2619 | |
2620 | |
2621 | |
2622 | |
2623 | |
2624 | template<typename _BidirectionalIterator, typename _Compare> |
2625 | inline void |
2626 | inplace_merge(_BidirectionalIterator __first, |
2627 | _BidirectionalIterator __middle, |
2628 | _BidirectionalIterator __last, |
2629 | _Compare __comp) |
2630 | { |
2631 | |
2632 | __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept< |
2633 | _BidirectionalIterator>) |
2634 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
2635 | typename iterator_traits<_BidirectionalIterator>::value_type, |
2636 | typename iterator_traits<_BidirectionalIterator>::value_type>) |
2637 | __glibcxx_requires_sorted_pred(__first, __middle, __comp); |
2638 | __glibcxx_requires_sorted_pred(__middle, __last, __comp); |
2639 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
2640 | |
2641 | std::__inplace_merge(__first, __middle, __last, |
2642 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
2643 | } |
2644 | |
2645 | |
2646 | |
2647 | template<typename _InputIterator, typename _OutputIterator, |
2648 | typename _Compare> |
2649 | _OutputIterator |
2650 | __move_merge(_InputIterator __first1, _InputIterator __last1, |
2651 | _InputIterator __first2, _InputIterator __last2, |
2652 | _OutputIterator __result, _Compare __comp) |
2653 | { |
2654 | while (__first1 != __last1 && __first2 != __last2) |
2655 | { |
2656 | if (__comp(__first2, __first1)) |
2657 | { |
2658 | *__result = _GLIBCXX_MOVE(*__first2); |
2659 | ++__first2; |
2660 | } |
2661 | else |
2662 | { |
2663 | *__result = _GLIBCXX_MOVE(*__first1); |
2664 | ++__first1; |
2665 | } |
2666 | ++__result; |
2667 | } |
2668 | return _GLIBCXX_MOVE3(__first2, __last2, |
2669 | _GLIBCXX_MOVE3(__first1, __last1, |
2670 | __result)); |
2671 | } |
2672 | |
2673 | template<typename _RandomAccessIterator1, typename _RandomAccessIterator2, |
2674 | typename _Distance, typename _Compare> |
2675 | void |
2676 | __merge_sort_loop(_RandomAccessIterator1 __first, |
2677 | _RandomAccessIterator1 __last, |
2678 | _RandomAccessIterator2 __result, _Distance __step_size, |
2679 | _Compare __comp) |
2680 | { |
2681 | const _Distance __two_step = 2 * __step_size; |
2682 | |
2683 | while (__last - __first >= __two_step) |
2684 | { |
2685 | __result = std::__move_merge(__first, __first + __step_size, |
2686 | __first + __step_size, |
2687 | __first + __two_step, |
2688 | __result, __comp); |
2689 | __first += __two_step; |
2690 | } |
2691 | __step_size = std::min(_Distance(__last - __first), __step_size); |
2692 | |
2693 | std::__move_merge(__first, __first + __step_size, |
2694 | __first + __step_size, __last, __result, __comp); |
2695 | } |
2696 | |
2697 | template<typename _RandomAccessIterator, typename _Distance, |
2698 | typename _Compare> |
2699 | _GLIBCXX20_CONSTEXPR |
2700 | void |
2701 | __chunk_insertion_sort(_RandomAccessIterator __first, |
2702 | _RandomAccessIterator __last, |
2703 | _Distance __chunk_size, _Compare __comp) |
2704 | { |
2705 | while (__last - __first >= __chunk_size) |
2706 | { |
2707 | std::__insertion_sort(__first, __first + __chunk_size, __comp); |
2708 | __first += __chunk_size; |
2709 | } |
2710 | std::__insertion_sort(__first, __last, __comp); |
2711 | } |
2712 | |
2713 | enum { _S_chunk_size = 7 }; |
2714 | |
2715 | template<typename _RandomAccessIterator, typename _Pointer, typename _Compare> |
2716 | void |
2717 | __merge_sort_with_buffer(_RandomAccessIterator __first, |
2718 | _RandomAccessIterator __last, |
2719 | _Pointer __buffer, _Compare __comp) |
2720 | { |
2721 | typedef typename iterator_traits<_RandomAccessIterator>::difference_type |
2722 | _Distance; |
2723 | |
2724 | const _Distance __len = __last - __first; |
2725 | const _Pointer __buffer_last = __buffer + __len; |
2726 | |
2727 | _Distance __step_size = _S_chunk_size; |
2728 | std::__chunk_insertion_sort(__first, __last, __step_size, __comp); |
2729 | |
2730 | while (__step_size < __len) |
2731 | { |
2732 | std::__merge_sort_loop(__first, __last, __buffer, |
2733 | __step_size, __comp); |
2734 | __step_size *= 2; |
2735 | std::__merge_sort_loop(__buffer, __buffer_last, __first, |
2736 | __step_size, __comp); |
2737 | __step_size *= 2; |
2738 | } |
2739 | } |
2740 | |
2741 | template<typename _RandomAccessIterator, typename _Pointer, |
2742 | typename _Distance, typename _Compare> |
2743 | void |
2744 | __stable_sort_adaptive(_RandomAccessIterator __first, |
2745 | _RandomAccessIterator __last, |
2746 | _Pointer __buffer, _Distance __buffer_size, |
2747 | _Compare __comp) |
2748 | { |
2749 | const _Distance __len = (__last - __first + 1) / 2; |
2750 | const _RandomAccessIterator __middle = __first + __len; |
2751 | if (__len > __buffer_size) |
2752 | { |
2753 | std::__stable_sort_adaptive(__first, __middle, __buffer, |
2754 | __buffer_size, __comp); |
2755 | std::__stable_sort_adaptive(__middle, __last, __buffer, |
2756 | __buffer_size, __comp); |
2757 | } |
2758 | else |
2759 | { |
2760 | std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp); |
2761 | std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp); |
2762 | } |
2763 | std::__merge_adaptive(__first, __middle, __last, |
2764 | _Distance(__middle - __first), |
2765 | _Distance(__last - __middle), |
2766 | __buffer, __buffer_size, |
2767 | __comp); |
2768 | } |
2769 | |
2770 | |
2771 | template<typename _RandomAccessIterator, typename _Compare> |
2772 | void |
2773 | __inplace_stable_sort(_RandomAccessIterator __first, |
2774 | _RandomAccessIterator __last, _Compare __comp) |
2775 | { |
2776 | if (__last - __first < 15) |
2777 | { |
2778 | std::__insertion_sort(__first, __last, __comp); |
2779 | return; |
2780 | } |
2781 | _RandomAccessIterator __middle = __first + (__last - __first) / 2; |
2782 | std::__inplace_stable_sort(__first, __middle, __comp); |
2783 | std::__inplace_stable_sort(__middle, __last, __comp); |
2784 | std::__merge_without_buffer(__first, __middle, __last, |
2785 | __middle - __first, |
2786 | __last - __middle, |
2787 | __comp); |
2788 | } |
2789 | |
2790 | |
2791 | |
2792 | |
2793 | |
2794 | |
2795 | |
2796 | |
2797 | template<typename _InputIterator1, typename _InputIterator2, |
2798 | typename _Compare> |
2799 | _GLIBCXX20_CONSTEXPR |
2800 | bool |
2801 | __includes(_InputIterator1 __first1, _InputIterator1 __last1, |
2802 | _InputIterator2 __first2, _InputIterator2 __last2, |
2803 | _Compare __comp) |
2804 | { |
2805 | while (__first1 != __last1 && __first2 != __last2) |
2806 | if (__comp(__first2, __first1)) |
2807 | return false; |
2808 | else if (__comp(__first1, __first2)) |
2809 | ++__first1; |
2810 | else |
2811 | { |
2812 | ++__first1; |
2813 | ++__first2; |
2814 | } |
2815 | |
2816 | return __first2 == __last2; |
2817 | } |
2818 | |
2819 | |
2820 | |
2821 | |
2822 | |
2823 | |
2824 | |
2825 | |
2826 | |
2827 | |
2828 | |
2829 | |
2830 | |
2831 | |
2832 | |
2833 | |
2834 | |
2835 | |
2836 | |
2837 | template<typename _InputIterator1, typename _InputIterator2> |
2838 | _GLIBCXX20_CONSTEXPR |
2839 | inline bool |
2840 | includes(_InputIterator1 __first1, _InputIterator1 __last1, |
2841 | _InputIterator2 __first2, _InputIterator2 __last2) |
2842 | { |
2843 | |
2844 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
2845 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
2846 | __glibcxx_function_requires(_LessThanOpConcept< |
2847 | typename iterator_traits<_InputIterator1>::value_type, |
2848 | typename iterator_traits<_InputIterator2>::value_type>) |
2849 | __glibcxx_function_requires(_LessThanOpConcept< |
2850 | typename iterator_traits<_InputIterator2>::value_type, |
2851 | typename iterator_traits<_InputIterator1>::value_type>) |
2852 | __glibcxx_requires_sorted_set(__first1, __last1, __first2); |
2853 | __glibcxx_requires_sorted_set(__first2, __last2, __first1); |
2854 | __glibcxx_requires_irreflexive2(__first1, __last1); |
2855 | __glibcxx_requires_irreflexive2(__first2, __last2); |
2856 | |
2857 | return std::__includes(__first1, __last1, __first2, __last2, |
2858 | __gnu_cxx::__ops::__iter_less_iter()); |
2859 | } |
2860 | |
2861 | |
2862 | |
2863 | |
2864 | |
2865 | |
2866 | |
2867 | |
2868 | |
2869 | |
2870 | |
2871 | |
2872 | |
2873 | |
2874 | |
2875 | |
2876 | |
2877 | |
2878 | |
2879 | |
2880 | |
2881 | |
2882 | template<typename _InputIterator1, typename _InputIterator2, |
2883 | typename _Compare> |
2884 | _GLIBCXX20_CONSTEXPR |
2885 | inline bool |
2886 | includes(_InputIterator1 __first1, _InputIterator1 __last1, |
2887 | _InputIterator2 __first2, _InputIterator2 __last2, |
2888 | _Compare __comp) |
2889 | { |
2890 | |
2891 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
2892 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
2893 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
2894 | typename iterator_traits<_InputIterator1>::value_type, |
2895 | typename iterator_traits<_InputIterator2>::value_type>) |
2896 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
2897 | typename iterator_traits<_InputIterator2>::value_type, |
2898 | typename iterator_traits<_InputIterator1>::value_type>) |
2899 | __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
2900 | __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); |
2901 | __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); |
2902 | __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); |
2903 | |
2904 | return std::__includes(__first1, __last1, __first2, __last2, |
2905 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
2906 | } |
2907 | |
2908 | |
2909 | |
2910 | |
2911 | |
2912 | |
2913 | |
2914 | |
2915 | |
2916 | |
2917 | |
2918 | template<typename _BidirectionalIterator, typename _Compare> |
2919 | _GLIBCXX20_CONSTEXPR |
2920 | bool |
2921 | __next_permutation(_BidirectionalIterator __first, |
2922 | _BidirectionalIterator __last, _Compare __comp) |
2923 | { |
2924 | if (__first == __last) |
2925 | return false; |
2926 | _BidirectionalIterator __i = __first; |
2927 | ++__i; |
2928 | if (__i == __last) |
2929 | return false; |
2930 | __i = __last; |
2931 | --__i; |
2932 | |
2933 | for(;;) |
2934 | { |
2935 | _BidirectionalIterator __ii = __i; |
2936 | --__i; |
2937 | if (__comp(__i, __ii)) |
2938 | { |
2939 | _BidirectionalIterator __j = __last; |
2940 | while (!__comp(__i, --__j)) |
2941 | {} |
2942 | std::iter_swap(__i, __j); |
2943 | std::__reverse(__ii, __last, |
2944 | std::__iterator_category(__first)); |
2945 | return true; |
2946 | } |
2947 | if (__i == __first) |
2948 | { |
2949 | std::__reverse(__first, __last, |
2950 | std::__iterator_category(__first)); |
2951 | return false; |
2952 | } |
2953 | } |
2954 | } |
2955 | |
2956 | |
2957 | |
2958 | |
2959 | |
2960 | |
2961 | |
2962 | |
2963 | |
2964 | |
2965 | |
2966 | |
2967 | |
2968 | template<typename _BidirectionalIterator> |
2969 | _GLIBCXX20_CONSTEXPR |
2970 | inline bool |
2971 | next_permutation(_BidirectionalIterator __first, |
2972 | _BidirectionalIterator __last) |
2973 | { |
2974 | |
2975 | __glibcxx_function_requires(_BidirectionalIteratorConcept< |
2976 | _BidirectionalIterator>) |
2977 | __glibcxx_function_requires(_LessThanComparableConcept< |
2978 | typename iterator_traits<_BidirectionalIterator>::value_type>) |
2979 | __glibcxx_requires_valid_range(__first, __last); |
2980 | __glibcxx_requires_irreflexive(__first, __last); |
2981 | |
2982 | return std::__next_permutation |
2983 | (__first, __last, __gnu_cxx::__ops::__iter_less_iter()); |
2984 | } |
2985 | |
2986 | |
2987 | |
2988 | |
2989 | |
2990 | |
2991 | |
2992 | |
2993 | |
2994 | |
2995 | |
2996 | |
2997 | |
2998 | |
2999 | |
3000 | |
3001 | template<typename _BidirectionalIterator, typename _Compare> |
3002 | _GLIBCXX20_CONSTEXPR |
3003 | inline bool |
3004 | next_permutation(_BidirectionalIterator __first, |
3005 | _BidirectionalIterator __last, _Compare __comp) |
3006 | { |
3007 | |
3008 | __glibcxx_function_requires(_BidirectionalIteratorConcept< |
3009 | _BidirectionalIterator>) |
3010 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
3011 | typename iterator_traits<_BidirectionalIterator>::value_type, |
3012 | typename iterator_traits<_BidirectionalIterator>::value_type>) |
3013 | __glibcxx_requires_valid_range(__first, __last); |
3014 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
3015 | |
3016 | return std::__next_permutation |
3017 | (__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
3018 | } |
3019 | |
3020 | template<typename _BidirectionalIterator, typename _Compare> |
3021 | _GLIBCXX20_CONSTEXPR |
3022 | bool |
3023 | __prev_permutation(_BidirectionalIterator __first, |
3024 | _BidirectionalIterator __last, _Compare __comp) |
3025 | { |
3026 | if (__first == __last) |
3027 | return false; |
3028 | _BidirectionalIterator __i = __first; |
3029 | ++__i; |
3030 | if (__i == __last) |
3031 | return false; |
3032 | __i = __last; |
3033 | --__i; |
3034 | |
3035 | for(;;) |
3036 | { |
3037 | _BidirectionalIterator __ii = __i; |
3038 | --__i; |
3039 | if (__comp(__ii, __i)) |
3040 | { |
3041 | _BidirectionalIterator __j = __last; |
3042 | while (!__comp(--__j, __i)) |
3043 | {} |
3044 | std::iter_swap(__i, __j); |
3045 | std::__reverse(__ii, __last, |
3046 | std::__iterator_category(__first)); |
3047 | return true; |
3048 | } |
3049 | if (__i == __first) |
3050 | { |
3051 | std::__reverse(__first, __last, |
3052 | std::__iterator_category(__first)); |
3053 | return false; |
3054 | } |
3055 | } |
3056 | } |
3057 | |
3058 | |
3059 | |
3060 | |
3061 | |
3062 | |
3063 | |
3064 | |
3065 | |
3066 | |
3067 | |
3068 | |
3069 | |
3070 | |
3071 | template<typename _BidirectionalIterator> |
3072 | _GLIBCXX20_CONSTEXPR |
3073 | inline bool |
3074 | prev_permutation(_BidirectionalIterator __first, |
3075 | _BidirectionalIterator __last) |
3076 | { |
3077 | |
3078 | __glibcxx_function_requires(_BidirectionalIteratorConcept< |
3079 | _BidirectionalIterator>) |
3080 | __glibcxx_function_requires(_LessThanComparableConcept< |
3081 | typename iterator_traits<_BidirectionalIterator>::value_type>) |
3082 | __glibcxx_requires_valid_range(__first, __last); |
3083 | __glibcxx_requires_irreflexive(__first, __last); |
3084 | |
3085 | return std::__prev_permutation(__first, __last, |
3086 | __gnu_cxx::__ops::__iter_less_iter()); |
3087 | } |
3088 | |
3089 | |
3090 | |
3091 | |
3092 | |
3093 | |
3094 | |
3095 | |
3096 | |
3097 | |
3098 | |
3099 | |
3100 | |
3101 | |
3102 | |
3103 | |
3104 | template<typename _BidirectionalIterator, typename _Compare> |
3105 | _GLIBCXX20_CONSTEXPR |
3106 | inline bool |
3107 | prev_permutation(_BidirectionalIterator __first, |
3108 | _BidirectionalIterator __last, _Compare __comp) |
3109 | { |
3110 | |
3111 | __glibcxx_function_requires(_BidirectionalIteratorConcept< |
3112 | _BidirectionalIterator>) |
3113 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
3114 | typename iterator_traits<_BidirectionalIterator>::value_type, |
3115 | typename iterator_traits<_BidirectionalIterator>::value_type>) |
3116 | __glibcxx_requires_valid_range(__first, __last); |
3117 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
3118 | |
3119 | return std::__prev_permutation(__first, __last, |
3120 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
3121 | } |
3122 | |
3123 | |
3124 | |
3125 | |
3126 | template<typename _InputIterator, typename _OutputIterator, |
3127 | typename _Predicate, typename _Tp> |
3128 | _GLIBCXX20_CONSTEXPR |
3129 | _OutputIterator |
3130 | __replace_copy_if(_InputIterator __first, _InputIterator __last, |
3131 | _OutputIterator __result, |
3132 | _Predicate __pred, const _Tp& __new_value) |
3133 | { |
3134 | for (; __first != __last; ++__first, (void)++__result) |
3135 | if (__pred(__first)) |
3136 | *__result = __new_value; |
3137 | else |
3138 | *__result = *__first; |
3139 | return __result; |
3140 | } |
3141 | |
3142 | |
3143 | |
3144 | |
3145 | |
3146 | |
3147 | |
3148 | |
3149 | |
3150 | |
3151 | |
3152 | |
3153 | |
3154 | |
3155 | |
3156 | template<typename _InputIterator, typename _OutputIterator, typename _Tp> |
3157 | _GLIBCXX20_CONSTEXPR |
3158 | inline _OutputIterator |
3159 | replace_copy(_InputIterator __first, _InputIterator __last, |
3160 | _OutputIterator __result, |
3161 | const _Tp& __old_value, const _Tp& __new_value) |
3162 | { |
3163 | |
3164 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
3165 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
3166 | typename iterator_traits<_InputIterator>::value_type>) |
3167 | __glibcxx_function_requires(_EqualOpConcept< |
3168 | typename iterator_traits<_InputIterator>::value_type, _Tp>) |
3169 | __glibcxx_requires_valid_range(__first, __last); |
3170 | |
3171 | return std::__replace_copy_if(__first, __last, __result, |
3172 | __gnu_cxx::__ops::__iter_equals_val(__old_value), |
3173 | __new_value); |
3174 | } |
3175 | |
3176 | |
3177 | |
3178 | |
3179 | |
3180 | |
3181 | |
3182 | |
3183 | |
3184 | |
3185 | |
3186 | |
3187 | |
3188 | |
3189 | |
3190 | |
3191 | template<typename _InputIterator, typename _OutputIterator, |
3192 | typename _Predicate, typename _Tp> |
3193 | _GLIBCXX20_CONSTEXPR |
3194 | inline _OutputIterator |
3195 | replace_copy_if(_InputIterator __first, _InputIterator __last, |
3196 | _OutputIterator __result, |
3197 | _Predicate __pred, const _Tp& __new_value) |
3198 | { |
3199 | |
3200 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
3201 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
3202 | typename iterator_traits<_InputIterator>::value_type>) |
3203 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
3204 | typename iterator_traits<_InputIterator>::value_type>) |
3205 | __glibcxx_requires_valid_range(__first, __last); |
3206 | |
3207 | return std::__replace_copy_if(__first, __last, __result, |
3208 | __gnu_cxx::__ops::__pred_iter(__pred), |
3209 | __new_value); |
3210 | } |
3211 | |
3212 | #if __cplusplus >= 201103L |
3213 | |
3214 | |
3215 | |
3216 | |
3217 | |
3218 | |
3219 | |
3220 | template<typename _ForwardIterator> |
3221 | _GLIBCXX20_CONSTEXPR |
3222 | inline bool |
3223 | is_sorted(_ForwardIterator __first, _ForwardIterator __last) |
3224 | { return std::is_sorted_until(__first, __last) == __last; } |
3225 | |
3226 | |
3227 | |
3228 | |
3229 | |
3230 | |
3231 | |
3232 | |
3233 | |
3234 | |
3235 | template<typename _ForwardIterator, typename _Compare> |
3236 | _GLIBCXX20_CONSTEXPR |
3237 | inline bool |
3238 | is_sorted(_ForwardIterator __first, _ForwardIterator __last, |
3239 | _Compare __comp) |
3240 | { return std::is_sorted_until(__first, __last, __comp) == __last; } |
3241 | |
3242 | template<typename _ForwardIterator, typename _Compare> |
3243 | _GLIBCXX20_CONSTEXPR |
3244 | _ForwardIterator |
3245 | __is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, |
3246 | _Compare __comp) |
3247 | { |
3248 | if (__first == __last) |
3249 | return __last; |
3250 | |
3251 | _ForwardIterator __next = __first; |
3252 | for (++__next; __next != __last; __first = __next, (void)++__next) |
3253 | if (__comp(__next, __first)) |
3254 | return __next; |
3255 | return __next; |
3256 | } |
3257 | |
3258 | |
3259 | |
3260 | |
3261 | |
3262 | |
3263 | |
3264 | |
3265 | |
3266 | template<typename _ForwardIterator> |
3267 | _GLIBCXX20_CONSTEXPR |
3268 | inline _ForwardIterator |
3269 | is_sorted_until(_ForwardIterator __first, _ForwardIterator __last) |
3270 | { |
3271 | |
3272 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
3273 | __glibcxx_function_requires(_LessThanComparableConcept< |
3274 | typename iterator_traits<_ForwardIterator>::value_type>) |
3275 | __glibcxx_requires_valid_range(__first, __last); |
3276 | __glibcxx_requires_irreflexive(__first, __last); |
3277 | |
3278 | return std::__is_sorted_until(__first, __last, |
3279 | __gnu_cxx::__ops::__iter_less_iter()); |
3280 | } |
3281 | |
3282 | |
3283 | |
3284 | |
3285 | |
3286 | |
3287 | |
3288 | |
3289 | |
3290 | |
3291 | template<typename _ForwardIterator, typename _Compare> |
3292 | _GLIBCXX20_CONSTEXPR |
3293 | inline _ForwardIterator |
3294 | is_sorted_until(_ForwardIterator __first, _ForwardIterator __last, |
3295 | _Compare __comp) |
3296 | { |
3297 | |
3298 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
3299 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
3300 | typename iterator_traits<_ForwardIterator>::value_type, |
3301 | typename iterator_traits<_ForwardIterator>::value_type>) |
3302 | __glibcxx_requires_valid_range(__first, __last); |
3303 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
3304 | |
3305 | return std::__is_sorted_until(__first, __last, |
3306 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
3307 | } |
3308 | |
3309 | |
3310 | |
3311 | |
3312 | |
3313 | |
3314 | |
3315 | |
3316 | |
3317 | template<typename _Tp> |
3318 | _GLIBCXX14_CONSTEXPR |
3319 | inline pair<const _Tp&, const _Tp&> |
3320 | minmax(const _Tp& __a, const _Tp& __b) |
3321 | { |
3322 | |
3323 | __glibcxx_function_requires(_LessThanComparableConcept<_Tp>) |
3324 | |
3325 | return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a) |
3326 | : pair<const _Tp&, const _Tp&>(__a, __b); |
3327 | } |
3328 | |
3329 | |
3330 | |
3331 | |
3332 | |
3333 | |
3334 | |
3335 | |
3336 | |
3337 | |
3338 | template<typename _Tp, typename _Compare> |
3339 | _GLIBCXX14_CONSTEXPR |
3340 | inline pair<const _Tp&, const _Tp&> |
3341 | minmax(const _Tp& __a, const _Tp& __b, _Compare __comp) |
3342 | { |
3343 | return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a) |
3344 | : pair<const _Tp&, const _Tp&>(__a, __b); |
3345 | } |
3346 | |
3347 | template<typename _ForwardIterator, typename _Compare> |
3348 | _GLIBCXX14_CONSTEXPR |
3349 | pair<_ForwardIterator, _ForwardIterator> |
3350 | __minmax_element(_ForwardIterator __first, _ForwardIterator __last, |
3351 | _Compare __comp) |
3352 | { |
3353 | _ForwardIterator __next = __first; |
3354 | if (__first == __last |
3355 | || ++__next == __last) |
3356 | return std::make_pair(__first, __first); |
3357 | |
3358 | _ForwardIterator __min{}, __max{}; |
3359 | if (__comp(__next, __first)) |
3360 | { |
3361 | __min = __next; |
3362 | __max = __first; |
3363 | } |
3364 | else |
3365 | { |
3366 | __min = __first; |
3367 | __max = __next; |
3368 | } |
3369 | |
3370 | __first = __next; |
3371 | ++__first; |
3372 | |
3373 | while (__first != __last) |
3374 | { |
3375 | __next = __first; |
3376 | if (++__next == __last) |
3377 | { |
3378 | if (__comp(__first, __min)) |
3379 | __min = __first; |
3380 | else if (!__comp(__first, __max)) |
3381 | __max = __first; |
3382 | break; |
3383 | } |
3384 | |
3385 | if (__comp(__next, __first)) |
3386 | { |
3387 | if (__comp(__next, __min)) |
3388 | __min = __next; |
3389 | if (!__comp(__first, __max)) |
3390 | __max = __first; |
3391 | } |
3392 | else |
3393 | { |
3394 | if (__comp(__first, __min)) |
3395 | __min = __first; |
3396 | if (!__comp(__next, __max)) |
3397 | __max = __next; |
3398 | } |
3399 | |
3400 | __first = __next; |
3401 | ++__first; |
3402 | } |
3403 | |
3404 | return std::make_pair(__min, __max); |
3405 | } |
3406 | |
3407 | |
3408 | |
3409 | |
3410 | |
3411 | |
3412 | |
3413 | |
3414 | |
3415 | |
3416 | |
3417 | |
3418 | template<typename _ForwardIterator> |
3419 | _GLIBCXX14_CONSTEXPR |
3420 | inline pair<_ForwardIterator, _ForwardIterator> |
3421 | minmax_element(_ForwardIterator __first, _ForwardIterator __last) |
3422 | { |
3423 | |
3424 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
3425 | __glibcxx_function_requires(_LessThanComparableConcept< |
3426 | typename iterator_traits<_ForwardIterator>::value_type>) |
3427 | __glibcxx_requires_valid_range(__first, __last); |
3428 | __glibcxx_requires_irreflexive(__first, __last); |
3429 | |
3430 | return std::__minmax_element(__first, __last, |
3431 | __gnu_cxx::__ops::__iter_less_iter()); |
3432 | } |
3433 | |
3434 | |
3435 | |
3436 | |
3437 | |
3438 | |
3439 | |
3440 | |
3441 | |
3442 | |
3443 | |
3444 | |
3445 | |
3446 | template<typename _ForwardIterator, typename _Compare> |
3447 | _GLIBCXX14_CONSTEXPR |
3448 | inline pair<_ForwardIterator, _ForwardIterator> |
3449 | minmax_element(_ForwardIterator __first, _ForwardIterator __last, |
3450 | _Compare __comp) |
3451 | { |
3452 | |
3453 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
3454 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
3455 | typename iterator_traits<_ForwardIterator>::value_type, |
3456 | typename iterator_traits<_ForwardIterator>::value_type>) |
3457 | __glibcxx_requires_valid_range(__first, __last); |
3458 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
3459 | |
3460 | return std::__minmax_element(__first, __last, |
3461 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
3462 | } |
3463 | |
3464 | |
3465 | template<typename _Tp> |
3466 | _GLIBCXX14_CONSTEXPR |
3467 | inline _Tp |
3468 | min(initializer_list<_Tp> __l) |
3469 | { return *std::min_element(__l.begin(), __l.end()); } |
3470 | |
3471 | template<typename _Tp, typename _Compare> |
3472 | _GLIBCXX14_CONSTEXPR |
3473 | inline _Tp |
3474 | min(initializer_list<_Tp> __l, _Compare __comp) |
3475 | { return *std::min_element(__l.begin(), __l.end(), __comp); } |
3476 | |
3477 | template<typename _Tp> |
3478 | _GLIBCXX14_CONSTEXPR |
3479 | inline _Tp |
3480 | max(initializer_list<_Tp> __l) |
3481 | { return *std::max_element(__l.begin(), __l.end()); } |
3482 | |
3483 | template<typename _Tp, typename _Compare> |
3484 | _GLIBCXX14_CONSTEXPR |
3485 | inline _Tp |
3486 | max(initializer_list<_Tp> __l, _Compare __comp) |
3487 | { return *std::max_element(__l.begin(), __l.end(), __comp); } |
3488 | |
3489 | template<typename _Tp> |
3490 | _GLIBCXX14_CONSTEXPR |
3491 | inline pair<_Tp, _Tp> |
3492 | minmax(initializer_list<_Tp> __l) |
3493 | { |
3494 | pair<const _Tp*, const _Tp*> __p = |
3495 | std::minmax_element(__l.begin(), __l.end()); |
3496 | return std::make_pair(*__p.first, *__p.second); |
3497 | } |
3498 | |
3499 | template<typename _Tp, typename _Compare> |
3500 | _GLIBCXX14_CONSTEXPR |
3501 | inline pair<_Tp, _Tp> |
3502 | minmax(initializer_list<_Tp> __l, _Compare __comp) |
3503 | { |
3504 | pair<const _Tp*, const _Tp*> __p = |
3505 | std::minmax_element(__l.begin(), __l.end(), __comp); |
3506 | return std::make_pair(*__p.first, *__p.second); |
3507 | } |
3508 | |
3509 | |
3510 | |
3511 | |
3512 | |
3513 | |
3514 | |
3515 | |
3516 | |
3517 | |
3518 | |
3519 | |
3520 | |
3521 | |
3522 | |
3523 | template<typename _ForwardIterator1, typename _ForwardIterator2, |
3524 | typename _BinaryPredicate> |
3525 | _GLIBCXX20_CONSTEXPR |
3526 | inline bool |
3527 | is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
3528 | _ForwardIterator2 __first2, _BinaryPredicate __pred) |
3529 | { |
3530 | |
3531 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) |
3532 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) |
3533 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
3534 | typename iterator_traits<_ForwardIterator1>::value_type, |
3535 | typename iterator_traits<_ForwardIterator2>::value_type>) |
3536 | __glibcxx_requires_valid_range(__first1, __last1); |
3537 | |
3538 | return std::__is_permutation(__first1, __last1, __first2, |
3539 | __gnu_cxx::__ops::__iter_comp_iter(__pred)); |
3540 | } |
3541 | |
3542 | #if __cplusplus > 201103L |
3543 | template<typename _ForwardIterator1, typename _ForwardIterator2, |
3544 | typename _BinaryPredicate> |
3545 | _GLIBCXX20_CONSTEXPR |
3546 | bool |
3547 | __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
3548 | _ForwardIterator2 __first2, _ForwardIterator2 __last2, |
3549 | _BinaryPredicate __pred) |
3550 | { |
3551 | using _Cat1 |
3552 | = typename iterator_traits<_ForwardIterator1>::iterator_category; |
3553 | using _Cat2 |
3554 | = typename iterator_traits<_ForwardIterator2>::iterator_category; |
3555 | using _It1_is_RA = is_same<_Cat1, random_access_iterator_tag>; |
3556 | using _It2_is_RA = is_same<_Cat2, random_access_iterator_tag>; |
3557 | constexpr bool __ra_iters = _It1_is_RA() && _It2_is_RA(); |
3558 | if (__ra_iters) |
3559 | { |
3560 | auto __d1 = std::distance(__first1, __last1); |
3561 | auto __d2 = std::distance(__first2, __last2); |
3562 | if (__d1 != __d2) |
3563 | return false; |
3564 | } |
3565 | |
3566 | |
3567 | |
3568 | for (; __first1 != __last1 && __first2 != __last2; |
3569 | ++__first1, (void)++__first2) |
3570 | if (!__pred(__first1, __first2)) |
3571 | break; |
3572 | |
3573 | if (__ra_iters) |
3574 | { |
3575 | if (__first1 == __last1) |
3576 | return true; |
3577 | } |
3578 | else |
3579 | { |
3580 | auto __d1 = std::distance(__first1, __last1); |
3581 | auto __d2 = std::distance(__first2, __last2); |
3582 | if (__d1 == 0 && __d2 == 0) |
3583 | return true; |
3584 | if (__d1 != __d2) |
3585 | return false; |
3586 | } |
3587 | |
3588 | for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan) |
3589 | { |
3590 | if (__scan != std::__find_if(__first1, __scan, |
3591 | __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))) |
3592 | continue; |
3593 | |
3594 | auto __matches = std::__count_if(__first2, __last2, |
3595 | __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)); |
3596 | if (0 == __matches |
3597 | || std::__count_if(__scan, __last1, |
3598 | __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)) |
3599 | != __matches) |
3600 | return false; |
3601 | } |
3602 | return true; |
3603 | } |
3604 | |
3605 | |
3606 | |
3607 | |
3608 | |
3609 | |
3610 | |
3611 | |
3612 | |
3613 | |
3614 | |
3615 | |
3616 | |
3617 | |
3618 | template<typename _ForwardIterator1, typename _ForwardIterator2> |
3619 | _GLIBCXX20_CONSTEXPR |
3620 | inline bool |
3621 | is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
3622 | _ForwardIterator2 __first2, _ForwardIterator2 __last2) |
3623 | { |
3624 | __glibcxx_requires_valid_range(__first1, __last1); |
3625 | __glibcxx_requires_valid_range(__first2, __last2); |
3626 | |
3627 | return |
3628 | std::__is_permutation(__first1, __last1, __first2, __last2, |
3629 | __gnu_cxx::__ops::__iter_equal_to_iter()); |
3630 | } |
3631 | |
3632 | |
3633 | |
3634 | |
3635 | |
3636 | |
3637 | |
3638 | |
3639 | |
3640 | |
3641 | |
3642 | |
3643 | |
3644 | |
3645 | |
3646 | template<typename _ForwardIterator1, typename _ForwardIterator2, |
3647 | typename _BinaryPredicate> |
3648 | _GLIBCXX20_CONSTEXPR |
3649 | inline bool |
3650 | is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
3651 | _ForwardIterator2 __first2, _ForwardIterator2 __last2, |
3652 | _BinaryPredicate __pred) |
3653 | { |
3654 | __glibcxx_requires_valid_range(__first1, __last1); |
3655 | __glibcxx_requires_valid_range(__first2, __last2); |
3656 | |
3657 | return std::__is_permutation(__first1, __last1, __first2, __last2, |
3658 | __gnu_cxx::__ops::__iter_comp_iter(__pred)); |
3659 | } |
3660 | |
3661 | #if __cplusplus > 201402L |
3662 | |
3663 | #define __cpp_lib_clamp 201603 |
3664 | |
3665 | |
3666 | |
3667 | |
3668 | |
3669 | |
3670 | |
3671 | |
3672 | |
3673 | template<typename _Tp> |
3674 | constexpr const _Tp& |
3675 | clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi) |
3676 | { |
3677 | __glibcxx_assert(!(__hi < __lo)); |
3678 | return (__val < __lo) ? __lo : (__hi < __val) ? __hi : __val; |
3679 | } |
3680 | |
3681 | |
3682 | |
3683 | |
3684 | |
3685 | |
3686 | |
3687 | |
3688 | |
3689 | |
3690 | |
3691 | template<typename _Tp, typename _Compare> |
3692 | constexpr const _Tp& |
3693 | clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi, _Compare __comp) |
3694 | { |
3695 | __glibcxx_assert(!__comp(__hi, __lo)); |
3696 | return __comp(__val, __lo) ? __lo : __comp(__hi, __val) ? __hi : __val; |
3697 | } |
3698 | #endif // C++17 |
3699 | #endif // C++14 |
3700 | |
3701 | #ifdef _GLIBCXX_USE_C99_STDINT_TR1 |
3702 | |
3703 | |
3704 | |
3705 | |
3706 | |
3707 | |
3708 | |
3709 | |
3710 | |
3711 | |
3712 | |
3713 | |
3714 | |
3715 | |
3716 | |
3717 | |
3718 | |
3719 | |
3720 | |
3721 | |
3722 | |
3723 | template<typename _IntType, typename _UniformRandomBitGenerator> |
3724 | pair<_IntType, _IntType> |
3725 | __gen_two_uniform_ints(_IntType __b0, _IntType __b1, |
3726 | _UniformRandomBitGenerator&& __g) |
3727 | { |
3728 | _IntType __x |
3729 | = uniform_int_distribution<_IntType>{0, (__b0 * __b1) - 1}(__g); |
3730 | return std::make_pair(__x / __b1, __x % __b1); |
3731 | } |
3732 | |
3733 | |
3734 | |
3735 | |
3736 | |
3737 | |
3738 | |
3739 | |
3740 | |
3741 | |
3742 | |
3743 | |
3744 | |
3745 | template<typename _RandomAccessIterator, |
3746 | typename _UniformRandomNumberGenerator> |
3747 | void |
3748 | shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, |
3749 | _UniformRandomNumberGenerator&& __g) |
3750 | { |
3751 | |
3752 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
3753 | _RandomAccessIterator>) |
3754 | __glibcxx_requires_valid_range(__first, __last); |
3755 | |
3756 | if (__first == __last) |
3757 | return; |
3758 | |
3759 | typedef typename iterator_traits<_RandomAccessIterator>::difference_type |
3760 | _DistanceType; |
3761 | |
3762 | typedef typename std::make_unsigned<_DistanceType>::type __ud_type; |
3763 | typedef typename std::uniform_int_distribution<__ud_type> __distr_type; |
3764 | typedef typename __distr_type::param_type __p_type; |
3765 | |
3766 | typedef typename remove_reference<_UniformRandomNumberGenerator>::type |
3767 | _Gen; |
3768 | typedef typename common_type<typename _Gen::result_type, __ud_type>::type |
3769 | __uc_type; |
3770 | |
3771 | const __uc_type __urngrange = __g.max() - __g.min(); |
3772 | const __uc_type __urange = __uc_type(__last - __first); |
3773 | |
3774 | if (__urngrange / __urange >= __urange) |
3775 | |
3776 | { |
3777 | _RandomAccessIterator __i = __first + 1; |
3778 | |
3779 | |
3780 | |
3781 | |
3782 | |
3783 | if ((__urange % 2) == 0) |
3784 | { |
3785 | __distr_type __d{0, 1}; |
3786 | std::iter_swap(__i++, __first + __d(__g)); |
3787 | } |
3788 | |
3789 | |
3790 | |
3791 | |
3792 | |
3793 | while (__i != __last) |
3794 | { |
3795 | const __uc_type __swap_range = __uc_type(__i - __first) + 1; |
3796 | |
3797 | const pair<__uc_type, __uc_type> __pospos = |
3798 | __gen_two_uniform_ints(__swap_range, __swap_range + 1, __g); |
3799 | |
3800 | std::iter_swap(__i++, __first + __pospos.first); |
3801 | std::iter_swap(__i++, __first + __pospos.second); |
3802 | } |
3803 | |
3804 | return; |
3805 | } |
3806 | |
3807 | __distr_type __d; |
3808 | |
3809 | for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) |
3810 | std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first))); |
3811 | } |
3812 | #endif |
3813 | |
3814 | #endif // C++11 |
3815 | |
3816 | _GLIBCXX_BEGIN_NAMESPACE_ALGO |
3817 | |
3818 | |
3819 | |
3820 | |
3821 | |
3822 | |
3823 | |
3824 | |
3825 | |
3826 | |
3827 | |
3828 | |
3829 | |
3830 | template<typename _InputIterator, typename _Function> |
3831 | _GLIBCXX20_CONSTEXPR |
3832 | _Function |
3833 | for_each(_InputIterator __first, _InputIterator __last, _Function __f) |
3834 | { |
3835 | |
3836 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
3837 | __glibcxx_requires_valid_range(__first, __last); |
3838 | for (; __first != __last; ++__first) |
3839 | __f(*__first); |
3840 | return __f; |
3841 | } |
3842 | |
3843 | #if __cplusplus >= 201703L |
3844 | |
3845 | |
3846 | |
3847 | |
3848 | |
3849 | |
3850 | |
3851 | |
3852 | |
3853 | |
3854 | |
3855 | |
3856 | template<typename _InputIterator, typename _Size, typename _Function> |
3857 | _InputIterator |
3858 | for_each_n(_InputIterator __first, _Size __n, _Function __f) |
3859 | { |
3860 | auto __n2 = std::__size_to_integer(__n); |
3861 | using _Cat = typename iterator_traits<_InputIterator>::iterator_category; |
3862 | if constexpr (is_base_of_v<random_access_iterator_tag, _Cat>) |
3863 | { |
3864 | if (__n2 <= 0) |
3865 | return __first; |
3866 | auto __last = __first + __n2; |
3867 | std::for_each(__first, __last, std::move(__f)); |
3868 | return __last; |
3869 | } |
3870 | else |
3871 | { |
3872 | while (__n2-->0) |
3873 | { |
3874 | __f(*__first); |
3875 | ++__first; |
3876 | } |
3877 | return __first; |
3878 | } |
3879 | } |
3880 | #endif // C++17 |
3881 | |
3882 | |
3883 | |
3884 | |
3885 | |
3886 | |
3887 | |
3888 | |
3889 | |
3890 | |
3891 | template<typename _InputIterator, typename _Tp> |
3892 | _GLIBCXX20_CONSTEXPR |
3893 | inline _InputIterator |
3894 | find(_InputIterator __first, _InputIterator __last, |
3895 | const _Tp& __val) |
3896 | { |
3897 | |
3898 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
3899 | __glibcxx_function_requires(_EqualOpConcept< |
3900 | typename iterator_traits<_InputIterator>::value_type, _Tp>) |
3901 | __glibcxx_requires_valid_range(__first, __last); |
3902 | return std::__find_if(__first, __last, |
3903 | __gnu_cxx::__ops::__iter_equals_val(__val)); |
3904 | } |
3905 | |
3906 | |
3907 | |
3908 | |
3909 | |
3910 | |
3911 | |
3912 | |
3913 | |
3914 | |
3915 | |
3916 | template<typename _InputIterator, typename _Predicate> |
3917 | _GLIBCXX20_CONSTEXPR |
3918 | inline _InputIterator |
3919 | find_if(_InputIterator __first, _InputIterator __last, |
3920 | _Predicate __pred) |
3921 | { |
3922 | |
3923 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
3924 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
3925 | typename iterator_traits<_InputIterator>::value_type>) |
3926 | __glibcxx_requires_valid_range(__first, __last); |
3927 | |
3928 | return std::__find_if(__first, __last, |
3929 | __gnu_cxx::__ops::__pred_iter(__pred)); |
3930 | } |
3931 | |
3932 | |
3933 | |
3934 | |
3935 | |
3936 | |
3937 | |
3938 | |
3939 | |
3940 | |
3941 | |
3942 | |
3943 | |
3944 | |
3945 | |
3946 | |
3947 | |
3948 | template<typename _InputIterator, typename _ForwardIterator> |
3949 | _GLIBCXX20_CONSTEXPR |
3950 | _InputIterator |
3951 | find_first_of(_InputIterator __first1, _InputIterator __last1, |
3952 | _ForwardIterator __first2, _ForwardIterator __last2) |
3953 | { |
3954 | |
3955 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
3956 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
3957 | __glibcxx_function_requires(_EqualOpConcept< |
3958 | typename iterator_traits<_InputIterator>::value_type, |
3959 | typename iterator_traits<_ForwardIterator>::value_type>) |
3960 | __glibcxx_requires_valid_range(__first1, __last1); |
3961 | __glibcxx_requires_valid_range(__first2, __last2); |
3962 | |
3963 | for (; __first1 != __last1; ++__first1) |
3964 | for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter) |
3965 | if (*__first1 == *__iter) |
3966 | return __first1; |
3967 | return __last1; |
3968 | } |
3969 | |
3970 | |
3971 | |
3972 | |
3973 | |
3974 | |
3975 | |
3976 | |
3977 | |
3978 | |
3979 | |
3980 | |
3981 | |
3982 | |
3983 | |
3984 | |
3985 | |
3986 | |
3987 | |
3988 | |
3989 | template<typename _InputIterator, typename _ForwardIterator, |
3990 | typename _BinaryPredicate> |
3991 | _GLIBCXX20_CONSTEXPR |
3992 | _InputIterator |
3993 | find_first_of(_InputIterator __first1, _InputIterator __last1, |
3994 | _ForwardIterator __first2, _ForwardIterator __last2, |
3995 | _BinaryPredicate __comp) |
3996 | { |
3997 | |
3998 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
3999 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
4000 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
4001 | typename iterator_traits<_InputIterator>::value_type, |
4002 | typename iterator_traits<_ForwardIterator>::value_type>) |
4003 | __glibcxx_requires_valid_range(__first1, __last1); |
4004 | __glibcxx_requires_valid_range(__first2, __last2); |
4005 | |
4006 | for (; __first1 != __last1; ++__first1) |
4007 | for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter) |
4008 | if (__comp(*__first1, *__iter)) |
4009 | return __first1; |
4010 | return __last1; |
4011 | } |
4012 | |
4013 | |
4014 | |
4015 | |
4016 | |
4017 | |
4018 | |
4019 | |
4020 | |
4021 | |
4022 | template<typename _ForwardIterator> |
4023 | _GLIBCXX20_CONSTEXPR |
4024 | inline _ForwardIterator |
4025 | adjacent_find(_ForwardIterator __first, _ForwardIterator __last) |
4026 | { |
4027 | |
4028 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
4029 | __glibcxx_function_requires(_EqualityComparableConcept< |
4030 | typename iterator_traits<_ForwardIterator>::value_type>) |
4031 | __glibcxx_requires_valid_range(__first, __last); |
4032 | |
4033 | return std::__adjacent_find(__first, __last, |
4034 | __gnu_cxx::__ops::__iter_equal_to_iter()); |
4035 | } |
4036 | |
4037 | |
4038 | |
4039 | |
4040 | |
4041 | |
4042 | |
4043 | |
4044 | |
4045 | |
4046 | |
4047 | |
4048 | template<typename _ForwardIterator, typename _BinaryPredicate> |
4049 | _GLIBCXX20_CONSTEXPR |
4050 | inline _ForwardIterator |
4051 | adjacent_find(_ForwardIterator __first, _ForwardIterator __last, |
4052 | _BinaryPredicate __binary_pred) |
4053 | { |
4054 | |
4055 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
4056 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
4057 | typename iterator_traits<_ForwardIterator>::value_type, |
4058 | typename iterator_traits<_ForwardIterator>::value_type>) |
4059 | __glibcxx_requires_valid_range(__first, __last); |
4060 | |
4061 | return std::__adjacent_find(__first, __last, |
4062 | __gnu_cxx::__ops::__iter_comp_iter(__binary_pred)); |
4063 | } |
4064 | |
4065 | |
4066 | |
4067 | |
4068 | |
4069 | |
4070 | |
4071 | |
4072 | |
4073 | |
4074 | template<typename _InputIterator, typename _Tp> |
4075 | _GLIBCXX20_CONSTEXPR |
4076 | inline typename iterator_traits<_InputIterator>::difference_type |
4077 | count(_InputIterator __first, _InputIterator __last, const _Tp& __value) |
4078 | { |
4079 | |
4080 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
4081 | __glibcxx_function_requires(_EqualOpConcept< |
4082 | typename iterator_traits<_InputIterator>::value_type, _Tp>) |
4083 | __glibcxx_requires_valid_range(__first, __last); |
4084 | |
4085 | return std::__count_if(__first, __last, |
4086 | __gnu_cxx::__ops::__iter_equals_val(__value)); |
4087 | } |
4088 | |
4089 | |
4090 | |
4091 | |
4092 | |
4093 | |
4094 | |
4095 | |
4096 | |
4097 | |
4098 | template<typename _InputIterator, typename _Predicate> |
4099 | _GLIBCXX20_CONSTEXPR |
4100 | inline typename iterator_traits<_InputIterator>::difference_type |
4101 | count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred) |
4102 | { |
4103 | |
4104 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
4105 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
4106 | typename iterator_traits<_InputIterator>::value_type>) |
4107 | __glibcxx_requires_valid_range(__first, __last); |
4108 | |
4109 | return std::__count_if(__first, __last, |
4110 | __gnu_cxx::__ops::__pred_iter(__pred)); |
4111 | } |
4112 | |
4113 | |
4114 | |
4115 | |
4116 | |
4117 | |
4118 | |
4119 | |
4120 | |
4121 | |
4122 | |
4123 | |
4124 | |
4125 | |
4126 | |
4127 | |
4128 | |
4129 | |
4130 | |
4131 | |
4132 | |
4133 | |
4134 | |
4135 | |
4136 | |
4137 | |
4138 | |
4139 | template<typename _ForwardIterator1, typename _ForwardIterator2> |
4140 | _GLIBCXX20_CONSTEXPR |
4141 | inline _ForwardIterator1 |
4142 | search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
4143 | _ForwardIterator2 __first2, _ForwardIterator2 __last2) |
4144 | { |
4145 | |
4146 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) |
4147 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) |
4148 | __glibcxx_function_requires(_EqualOpConcept< |
4149 | typename iterator_traits<_ForwardIterator1>::value_type, |
4150 | typename iterator_traits<_ForwardIterator2>::value_type>) |
4151 | __glibcxx_requires_valid_range(__first1, __last1); |
4152 | __glibcxx_requires_valid_range(__first2, __last2); |
4153 | |
4154 | return std::__search(__first1, __last1, __first2, __last2, |
4155 | __gnu_cxx::__ops::__iter_equal_to_iter()); |
4156 | } |
4157 | |
4158 | |
4159 | |
4160 | |
4161 | |
4162 | |
4163 | |
4164 | |
4165 | |
4166 | |
4167 | |
4168 | |
4169 | |
4170 | |
4171 | |
4172 | |
4173 | |
4174 | |
4175 | |
4176 | |
4177 | |
4178 | |
4179 | template<typename _ForwardIterator1, typename _ForwardIterator2, |
4180 | typename _BinaryPredicate> |
4181 | _GLIBCXX20_CONSTEXPR |
4182 | inline _ForwardIterator1 |
4183 | search(_ForwardIterator1 __first1, _ForwardIterator1 __last1, |
4184 | _ForwardIterator2 __first2, _ForwardIterator2 __last2, |
4185 | _BinaryPredicate __predicate) |
4186 | { |
4187 | |
4188 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>) |
4189 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>) |
4190 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
4191 | typename iterator_traits<_ForwardIterator1>::value_type, |
4192 | typename iterator_traits<_ForwardIterator2>::value_type>) |
4193 | __glibcxx_requires_valid_range(__first1, __last1); |
4194 | __glibcxx_requires_valid_range(__first2, __last2); |
4195 | |
4196 | return std::__search(__first1, __last1, __first2, __last2, |
4197 | __gnu_cxx::__ops::__iter_comp_iter(__predicate)); |
4198 | } |
4199 | |
4200 | |
4201 | |
4202 | |
4203 | |
4204 | |
4205 | |
4206 | |
4207 | |
4208 | |
4209 | |
4210 | |
4211 | |
4212 | |
4213 | |
4214 | |
4215 | template<typename _ForwardIterator, typename _Integer, typename _Tp> |
4216 | _GLIBCXX20_CONSTEXPR |
4217 | inline _ForwardIterator |
4218 | search_n(_ForwardIterator __first, _ForwardIterator __last, |
4219 | _Integer __count, const _Tp& __val) |
4220 | { |
4221 | |
4222 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
4223 | __glibcxx_function_requires(_EqualOpConcept< |
4224 | typename iterator_traits<_ForwardIterator>::value_type, _Tp>) |
4225 | __glibcxx_requires_valid_range(__first, __last); |
4226 | |
4227 | return std::__search_n(__first, __last, __count, |
4228 | __gnu_cxx::__ops::__iter_equals_val(__val)); |
4229 | } |
4230 | |
4231 | |
4232 | |
4233 | |
4234 | |
4235 | |
4236 | |
4237 | |
4238 | |
4239 | |
4240 | |
4241 | |
4242 | |
4243 | |
4244 | |
4245 | |
4246 | |
4247 | |
4248 | |
4249 | template<typename _ForwardIterator, typename _Integer, typename _Tp, |
4250 | typename _BinaryPredicate> |
4251 | _GLIBCXX20_CONSTEXPR |
4252 | inline _ForwardIterator |
4253 | search_n(_ForwardIterator __first, _ForwardIterator __last, |
4254 | _Integer __count, const _Tp& __val, |
4255 | _BinaryPredicate __binary_pred) |
4256 | { |
4257 | |
4258 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
4259 | __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate, |
4260 | typename iterator_traits<_ForwardIterator>::value_type, _Tp>) |
4261 | __glibcxx_requires_valid_range(__first, __last); |
4262 | |
4263 | return std::__search_n(__first, __last, __count, |
4264 | __gnu_cxx::__ops::__iter_comp_val(__binary_pred, __val)); |
4265 | } |
4266 | |
4267 | #if __cplusplus > 201402L |
4268 | |
4269 | |
4270 | |
4271 | |
4272 | |
4273 | |
4274 | |
4275 | template<typename _ForwardIterator, typename _Searcher> |
4276 | inline _ForwardIterator |
4277 | search(_ForwardIterator __first, _ForwardIterator __last, |
4278 | const _Searcher& __searcher) |
4279 | { return __searcher(__first, __last).first; } |
4280 | #endif |
4281 | |
4282 | |
4283 | |
4284 | |
4285 | |
4286 | |
4287 | |
4288 | |
4289 | |
4290 | |
4291 | |
4292 | |
4293 | |
4294 | |
4295 | |
4296 | |
4297 | |
4298 | template<typename _InputIterator, typename _OutputIterator, |
4299 | typename _UnaryOperation> |
4300 | _GLIBCXX20_CONSTEXPR |
4301 | _OutputIterator |
4302 | transform(_InputIterator __first, _InputIterator __last, |
4303 | _OutputIterator __result, _UnaryOperation __unary_op) |
4304 | { |
4305 | |
4306 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
4307 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
4308 | |
4309 | __typeof__(__unary_op(*__first))>) |
4310 | __glibcxx_requires_valid_range(__first, __last); |
4311 | |
4312 | for (; __first != __last; ++__first, (void)++__result) |
4313 | *__result = __unary_op(*__first); |
4314 | return __result; |
4315 | } |
4316 | |
4317 | |
4318 | |
4319 | |
4320 | |
4321 | |
4322 | |
4323 | |
4324 | |
4325 | |
4326 | |
4327 | |
4328 | |
4329 | |
4330 | |
4331 | |
4332 | |
4333 | |
4334 | |
4335 | |
4336 | template<typename _InputIterator1, typename _InputIterator2, |
4337 | typename _OutputIterator, typename _BinaryOperation> |
4338 | _GLIBCXX20_CONSTEXPR |
4339 | _OutputIterator |
4340 | transform(_InputIterator1 __first1, _InputIterator1 __last1, |
4341 | _InputIterator2 __first2, _OutputIterator __result, |
4342 | _BinaryOperation __binary_op) |
4343 | { |
4344 | |
4345 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
4346 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
4347 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
4348 | |
4349 | __typeof__(__binary_op(*__first1,*__first2))>) |
4350 | __glibcxx_requires_valid_range(__first1, __last1); |
4351 | |
4352 | for (; __first1 != __last1; ++__first1, (void)++__first2, ++__result) |
4353 | *__result = __binary_op(*__first1, *__first2); |
4354 | return __result; |
4355 | } |
4356 | |
4357 | |
4358 | |
4359 | |
4360 | |
4361 | |
4362 | |
4363 | |
4364 | |
4365 | |
4366 | |
4367 | |
4368 | |
4369 | |
4370 | template<typename _ForwardIterator, typename _Tp> |
4371 | _GLIBCXX20_CONSTEXPR |
4372 | void |
4373 | replace(_ForwardIterator __first, _ForwardIterator __last, |
4374 | const _Tp& __old_value, const _Tp& __new_value) |
4375 | { |
4376 | |
4377 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
4378 | _ForwardIterator>) |
4379 | __glibcxx_function_requires(_EqualOpConcept< |
4380 | typename iterator_traits<_ForwardIterator>::value_type, _Tp>) |
4381 | __glibcxx_function_requires(_ConvertibleConcept<_Tp, |
4382 | typename iterator_traits<_ForwardIterator>::value_type>) |
4383 | __glibcxx_requires_valid_range(__first, __last); |
4384 | |
4385 | for (; __first != __last; ++__first) |
4386 | if (*__first == __old_value) |
4387 | *__first = __new_value; |
4388 | } |
4389 | |
4390 | |
4391 | |
4392 | |
4393 | |
4394 | |
4395 | |
4396 | |
4397 | |
4398 | |
4399 | |
4400 | |
4401 | |
4402 | |
4403 | template<typename _ForwardIterator, typename _Predicate, typename _Tp> |
4404 | _GLIBCXX20_CONSTEXPR |
4405 | void |
4406 | replace_if(_ForwardIterator __first, _ForwardIterator __last, |
4407 | _Predicate __pred, const _Tp& __new_value) |
4408 | { |
4409 | |
4410 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
4411 | _ForwardIterator>) |
4412 | __glibcxx_function_requires(_ConvertibleConcept<_Tp, |
4413 | typename iterator_traits<_ForwardIterator>::value_type>) |
4414 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
4415 | typename iterator_traits<_ForwardIterator>::value_type>) |
4416 | __glibcxx_requires_valid_range(__first, __last); |
4417 | |
4418 | for (; __first != __last; ++__first) |
4419 | if (__pred(*__first)) |
4420 | *__first = __new_value; |
4421 | } |
4422 | |
4423 | |
4424 | |
4425 | |
4426 | |
4427 | |
4428 | |
4429 | |
4430 | |
4431 | |
4432 | |
4433 | |
4434 | |
4435 | |
4436 | template<typename _ForwardIterator, typename _Generator> |
4437 | _GLIBCXX20_CONSTEXPR |
4438 | void |
4439 | generate(_ForwardIterator __first, _ForwardIterator __last, |
4440 | _Generator __gen) |
4441 | { |
4442 | |
4443 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
4444 | __glibcxx_function_requires(_GeneratorConcept<_Generator, |
4445 | typename iterator_traits<_ForwardIterator>::value_type>) |
4446 | __glibcxx_requires_valid_range(__first, __last); |
4447 | |
4448 | for (; __first != __last; ++__first) |
4449 | *__first = __gen(); |
4450 | } |
4451 | |
4452 | |
4453 | |
4454 | |
4455 | |
4456 | |
4457 | |
4458 | |
4459 | |
4460 | |
4461 | |
4462 | |
4463 | |
4464 | |
4465 | |
4466 | |
4467 | |
4468 | |
4469 | |
4470 | template<typename _OutputIterator, typename _Size, typename _Generator> |
4471 | _GLIBCXX20_CONSTEXPR |
4472 | _OutputIterator |
4473 | generate_n(_OutputIterator __first, _Size __n, _Generator __gen) |
4474 | { |
4475 | |
4476 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
4477 | |
4478 | __typeof__(__gen())>) |
4479 | |
4480 | typedef __decltype(std::__size_to_integer(__n)) _IntSize; |
4481 | for (_IntSize __niter = std::__size_to_integer(__n); |
4482 | __niter > 0; --__niter, (void) ++__first) |
4483 | *__first = __gen(); |
4484 | return __first; |
4485 | } |
4486 | |
4487 | |
4488 | |
4489 | |
4490 | |
4491 | |
4492 | |
4493 | |
4494 | |
4495 | |
4496 | |
4497 | |
4498 | |
4499 | |
4500 | |
4501 | |
4502 | |
4503 | |
4504 | |
4505 | |
4506 | |
4507 | |
4508 | template<typename _InputIterator, typename _OutputIterator> |
4509 | _GLIBCXX20_CONSTEXPR |
4510 | inline _OutputIterator |
4511 | unique_copy(_InputIterator __first, _InputIterator __last, |
4512 | _OutputIterator __result) |
4513 | { |
4514 | |
4515 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
4516 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
4517 | typename iterator_traits<_InputIterator>::value_type>) |
4518 | __glibcxx_function_requires(_EqualityComparableConcept< |
4519 | typename iterator_traits<_InputIterator>::value_type>) |
4520 | __glibcxx_requires_valid_range(__first, __last); |
4521 | |
4522 | if (__first == __last) |
4523 | return __result; |
4524 | return std::__unique_copy(__first, __last, __result, |
4525 | __gnu_cxx::__ops::__iter_equal_to_iter(), |
4526 | std::__iterator_category(__first), |
4527 | std::__iterator_category(__result)); |
4528 | } |
4529 | |
4530 | |
4531 | |
4532 | |
4533 | |
4534 | |
4535 | |
4536 | |
4537 | |
4538 | |
4539 | |
4540 | |
4541 | |
4542 | |
4543 | |
4544 | |
4545 | |
4546 | |
4547 | |
4548 | |
4549 | template<typename _InputIterator, typename _OutputIterator, |
4550 | typename _BinaryPredicate> |
4551 | _GLIBCXX20_CONSTEXPR |
4552 | inline _OutputIterator |
4553 | unique_copy(_InputIterator __first, _InputIterator __last, |
4554 | _OutputIterator __result, |
4555 | _BinaryPredicate __binary_pred) |
4556 | { |
4557 | |
4558 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>) |
4559 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
4560 | typename iterator_traits<_InputIterator>::value_type>) |
4561 | __glibcxx_requires_valid_range(__first, __last); |
4562 | |
4563 | if (__first == __last) |
4564 | return __result; |
4565 | return std::__unique_copy(__first, __last, __result, |
4566 | __gnu_cxx::__ops::__iter_comp_iter(__binary_pred), |
4567 | std::__iterator_category(__first), |
4568 | std::__iterator_category(__result)); |
4569 | } |
4570 | |
4571 | #if _GLIBCXX_HOSTED |
4572 | |
4573 | |
4574 | |
4575 | |
4576 | |
4577 | |
4578 | |
4579 | |
4580 | |
4581 | |
4582 | |
4583 | template<typename _RandomAccessIterator> |
4584 | inline void |
4585 | random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last) |
4586 | { |
4587 | |
4588 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
4589 | _RandomAccessIterator>) |
4590 | __glibcxx_requires_valid_range(__first, __last); |
4591 | |
4592 | if (__first != __last) |
4593 | for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) |
4594 | { |
4595 | |
4596 | _RandomAccessIterator __j = __first |
4597 | + std::rand() % ((__i - __first) + 1); |
4598 | if (__i != __j) |
4599 | std::iter_swap(__i, __j); |
4600 | } |
4601 | } |
4602 | #endif |
4603 | |
4604 | |
4605 | |
4606 | |
4607 | |
4608 | |
4609 | |
4610 | |
4611 | |
4612 | |
4613 | |
4614 | |
4615 | |
4616 | |
4617 | |
4618 | template<typename _RandomAccessIterator, typename _RandomNumberGenerator> |
4619 | void |
4620 | random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last, |
4621 | #if __cplusplus >= 201103L |
4622 | _RandomNumberGenerator&& __rand) |
4623 | #else |
4624 | _RandomNumberGenerator& __rand) |
4625 | #endif |
4626 | { |
4627 | |
4628 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
4629 | _RandomAccessIterator>) |
4630 | __glibcxx_requires_valid_range(__first, __last); |
4631 | |
4632 | if (__first == __last) |
4633 | return; |
4634 | for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) |
4635 | { |
4636 | _RandomAccessIterator __j = __first + __rand((__i - __first) + 1); |
4637 | if (__i != __j) |
4638 | std::iter_swap(__i, __j); |
4639 | } |
4640 | } |
4641 | |
4642 | |
4643 | |
4644 | |
4645 | |
4646 | |
4647 | |
4648 | |
4649 | |
4650 | |
4651 | |
4652 | |
4653 | |
4654 | |
4655 | |
4656 | |
4657 | |
4658 | template<typename _ForwardIterator, typename _Predicate> |
4659 | _GLIBCXX20_CONSTEXPR |
4660 | inline _ForwardIterator |
4661 | partition(_ForwardIterator __first, _ForwardIterator __last, |
4662 | _Predicate __pred) |
4663 | { |
4664 | |
4665 | __glibcxx_function_requires(_Mutable_ForwardIteratorConcept< |
4666 | _ForwardIterator>) |
4667 | __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate, |
4668 | typename iterator_traits<_ForwardIterator>::value_type>) |
4669 | __glibcxx_requires_valid_range(__first, __last); |
4670 | |
4671 | return std::__partition(__first, __last, __pred, |
4672 | std::__iterator_category(__first)); |
4673 | } |
4674 | |
4675 | |
4676 | |
4677 | |
4678 | |
4679 | |
4680 | |
4681 | |
4682 | |
4683 | |
4684 | |
4685 | |
4686 | |
4687 | |
4688 | |
4689 | |
4690 | |
4691 | |
4692 | template<typename _RandomAccessIterator> |
4693 | _GLIBCXX20_CONSTEXPR |
4694 | inline void |
4695 | partial_sort(_RandomAccessIterator __first, |
4696 | _RandomAccessIterator __middle, |
4697 | _RandomAccessIterator __last) |
4698 | { |
4699 | |
4700 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
4701 | _RandomAccessIterator>) |
4702 | __glibcxx_function_requires(_LessThanComparableConcept< |
4703 | typename iterator_traits<_RandomAccessIterator>::value_type>) |
4704 | __glibcxx_requires_valid_range(__first, __middle); |
4705 | __glibcxx_requires_valid_range(__middle, __last); |
4706 | __glibcxx_requires_irreflexive(__first, __last); |
4707 | |
4708 | std::__partial_sort(__first, __middle, __last, |
4709 | __gnu_cxx::__ops::__iter_less_iter()); |
4710 | } |
4711 | |
4712 | |
4713 | |
4714 | |
4715 | |
4716 | |
4717 | |
4718 | |
4719 | |
4720 | |
4721 | |
4722 | |
4723 | |
4724 | |
4725 | |
4726 | |
4727 | |
4728 | |
4729 | |
4730 | |
4731 | template<typename _RandomAccessIterator, typename _Compare> |
4732 | _GLIBCXX20_CONSTEXPR |
4733 | inline void |
4734 | partial_sort(_RandomAccessIterator __first, |
4735 | _RandomAccessIterator __middle, |
4736 | _RandomAccessIterator __last, |
4737 | _Compare __comp) |
4738 | { |
4739 | |
4740 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
4741 | _RandomAccessIterator>) |
4742 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
4743 | typename iterator_traits<_RandomAccessIterator>::value_type, |
4744 | typename iterator_traits<_RandomAccessIterator>::value_type>) |
4745 | __glibcxx_requires_valid_range(__first, __middle); |
4746 | __glibcxx_requires_valid_range(__middle, __last); |
4747 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
4748 | |
4749 | std::__partial_sort(__first, __middle, __last, |
4750 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
4751 | } |
4752 | |
4753 | |
4754 | |
4755 | |
4756 | |
4757 | |
4758 | |
4759 | |
4760 | |
4761 | |
4762 | |
4763 | |
4764 | |
4765 | |
4766 | |
4767 | |
4768 | template<typename _RandomAccessIterator> |
4769 | _GLIBCXX20_CONSTEXPR |
4770 | inline void |
4771 | nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, |
4772 | _RandomAccessIterator __last) |
4773 | { |
4774 | |
4775 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
4776 | _RandomAccessIterator>) |
4777 | __glibcxx_function_requires(_LessThanComparableConcept< |
4778 | typename iterator_traits<_RandomAccessIterator>::value_type>) |
4779 | __glibcxx_requires_valid_range(__first, __nth); |
4780 | __glibcxx_requires_valid_range(__nth, __last); |
4781 | __glibcxx_requires_irreflexive(__first, __last); |
4782 | |
4783 | if (__first == __last || __nth == __last) |
4784 | return; |
4785 | |
4786 | std::__introselect(__first, __nth, __last, |
4787 | std::__lg(__last - __first) * 2, |
4788 | __gnu_cxx::__ops::__iter_less_iter()); |
4789 | } |
4790 | |
4791 | |
4792 | |
4793 | |
4794 | |
4795 | |
4796 | |
4797 | |
4798 | |
4799 | |
4800 | |
4801 | |
4802 | |
4803 | |
4804 | |
4805 | |
4806 | |
4807 | |
4808 | template<typename _RandomAccessIterator, typename _Compare> |
4809 | _GLIBCXX20_CONSTEXPR |
4810 | inline void |
4811 | nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth, |
4812 | _RandomAccessIterator __last, _Compare __comp) |
4813 | { |
4814 | |
4815 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
4816 | _RandomAccessIterator>) |
4817 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
4818 | typename iterator_traits<_RandomAccessIterator>::value_type, |
4819 | typename iterator_traits<_RandomAccessIterator>::value_type>) |
4820 | __glibcxx_requires_valid_range(__first, __nth); |
4821 | __glibcxx_requires_valid_range(__nth, __last); |
4822 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
4823 | |
4824 | if (__first == __last || __nth == __last) |
4825 | return; |
4826 | |
4827 | std::__introselect(__first, __nth, __last, |
4828 | std::__lg(__last - __first) * 2, |
4829 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
4830 | } |
4831 | |
4832 | |
4833 | |
4834 | |
4835 | |
4836 | |
4837 | |
4838 | |
4839 | |
4840 | |
4841 | |
4842 | |
4843 | |
4844 | |
4845 | |
4846 | template<typename _RandomAccessIterator> |
4847 | _GLIBCXX20_CONSTEXPR |
4848 | inline void |
4849 | sort(_RandomAccessIterator __first, _RandomAccessIterator __last) |
4850 | { |
4851 | |
4852 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
4853 | _RandomAccessIterator>) |
4854 | __glibcxx_function_requires(_LessThanComparableConcept< |
4855 | typename iterator_traits<_RandomAccessIterator>::value_type>) |
4856 | __glibcxx_requires_valid_range(__first, __last); |
4857 | __glibcxx_requires_irreflexive(__first, __last); |
4858 | |
4859 | std::__sort(__first, __last, __gnu_cxx::__ops::__iter_less_iter()); |
| 23 | | Calling '__sort<int *, __gnu_cxx::__ops::_Iter_less_iter>' | |
|
4860 | } |
4861 | |
4862 | |
4863 | |
4864 | |
4865 | |
4866 | |
4867 | |
4868 | |
4869 | |
4870 | |
4871 | |
4872 | |
4873 | |
4874 | |
4875 | |
4876 | |
4877 | template<typename _RandomAccessIterator, typename _Compare> |
4878 | _GLIBCXX20_CONSTEXPR |
4879 | inline void |
4880 | sort(_RandomAccessIterator __first, _RandomAccessIterator __last, |
4881 | _Compare __comp) |
4882 | { |
4883 | |
4884 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
4885 | _RandomAccessIterator>) |
4886 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
4887 | typename iterator_traits<_RandomAccessIterator>::value_type, |
4888 | typename iterator_traits<_RandomAccessIterator>::value_type>) |
4889 | __glibcxx_requires_valid_range(__first, __last); |
4890 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
4891 | |
4892 | std::__sort(__first, __last, __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
4893 | } |
4894 | |
4895 | template<typename _InputIterator1, typename _InputIterator2, |
4896 | typename _OutputIterator, typename _Compare> |
4897 | _GLIBCXX20_CONSTEXPR |
4898 | _OutputIterator |
4899 | __merge(_InputIterator1 __first1, _InputIterator1 __last1, |
4900 | _InputIterator2 __first2, _InputIterator2 __last2, |
4901 | _OutputIterator __result, _Compare __comp) |
4902 | { |
4903 | while (__first1 != __last1 && __first2 != __last2) |
4904 | { |
4905 | if (__comp(__first2, __first1)) |
4906 | { |
4907 | *__result = *__first2; |
4908 | ++__first2; |
4909 | } |
4910 | else |
4911 | { |
4912 | *__result = *__first1; |
4913 | ++__first1; |
4914 | } |
4915 | ++__result; |
4916 | } |
4917 | return std::copy(__first2, __last2, |
4918 | std::copy(__first1, __last1, __result)); |
4919 | } |
4920 | |
4921 | |
4922 | |
4923 | |
4924 | |
4925 | |
4926 | |
4927 | |
4928 | |
4929 | |
4930 | |
4931 | |
4932 | |
4933 | |
4934 | |
4935 | |
4936 | |
4937 | |
4938 | |
4939 | |
4940 | template<typename _InputIterator1, typename _InputIterator2, |
4941 | typename _OutputIterator> |
4942 | _GLIBCXX20_CONSTEXPR |
4943 | inline _OutputIterator |
4944 | merge(_InputIterator1 __first1, _InputIterator1 __last1, |
4945 | _InputIterator2 __first2, _InputIterator2 __last2, |
4946 | _OutputIterator __result) |
4947 | { |
4948 | |
4949 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
4950 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
4951 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
4952 | typename iterator_traits<_InputIterator1>::value_type>) |
4953 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
4954 | typename iterator_traits<_InputIterator2>::value_type>) |
4955 | __glibcxx_function_requires(_LessThanOpConcept< |
4956 | typename iterator_traits<_InputIterator2>::value_type, |
4957 | typename iterator_traits<_InputIterator1>::value_type>) |
4958 | __glibcxx_requires_sorted_set(__first1, __last1, __first2); |
4959 | __glibcxx_requires_sorted_set(__first2, __last2, __first1); |
4960 | __glibcxx_requires_irreflexive2(__first1, __last1); |
4961 | __glibcxx_requires_irreflexive2(__first2, __last2); |
4962 | |
4963 | return _GLIBCXX_STD_A::__merge(__first1, __last1, |
4964 | __first2, __last2, __result, |
4965 | __gnu_cxx::__ops::__iter_less_iter()); |
4966 | } |
4967 | |
4968 | |
4969 | |
4970 | |
4971 | |
4972 | |
4973 | |
4974 | |
4975 | |
4976 | |
4977 | |
4978 | |
4979 | |
4980 | |
4981 | |
4982 | |
4983 | |
4984 | |
4985 | |
4986 | |
4987 | |
4988 | |
4989 | |
4990 | |
4991 | template<typename _InputIterator1, typename _InputIterator2, |
4992 | typename _OutputIterator, typename _Compare> |
4993 | _GLIBCXX20_CONSTEXPR |
4994 | inline _OutputIterator |
4995 | merge(_InputIterator1 __first1, _InputIterator1 __last1, |
4996 | _InputIterator2 __first2, _InputIterator2 __last2, |
4997 | _OutputIterator __result, _Compare __comp) |
4998 | { |
4999 | |
5000 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5001 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
5002 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
5003 | typename iterator_traits<_InputIterator1>::value_type>) |
5004 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
5005 | typename iterator_traits<_InputIterator2>::value_type>) |
5006 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
5007 | typename iterator_traits<_InputIterator2>::value_type, |
5008 | typename iterator_traits<_InputIterator1>::value_type>) |
5009 | __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
5010 | __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); |
5011 | __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); |
5012 | __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); |
5013 | |
5014 | return _GLIBCXX_STD_A::__merge(__first1, __last1, |
5015 | __first2, __last2, __result, |
5016 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
5017 | } |
5018 | |
5019 | template<typename _RandomAccessIterator, typename _Compare> |
5020 | inline void |
5021 | __stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, |
5022 | _Compare __comp) |
5023 | { |
5024 | typedef typename iterator_traits<_RandomAccessIterator>::value_type |
5025 | _ValueType; |
5026 | typedef typename iterator_traits<_RandomAccessIterator>::difference_type |
5027 | _DistanceType; |
5028 | |
5029 | typedef _Temporary_buffer<_RandomAccessIterator, _ValueType> _TmpBuf; |
5030 | _TmpBuf __buf(__first, std::distance(__first, __last)); |
5031 | |
5032 | if (__buf.begin() == 0) |
5033 | std::__inplace_stable_sort(__first, __last, __comp); |
5034 | else |
5035 | std::__stable_sort_adaptive(__first, __last, __buf.begin(), |
5036 | _DistanceType(__buf.size()), __comp); |
5037 | } |
5038 | |
5039 | |
5040 | |
5041 | |
5042 | |
5043 | |
5044 | |
5045 | |
5046 | |
5047 | |
5048 | |
5049 | |
5050 | |
5051 | |
5052 | |
5053 | |
5054 | |
5055 | |
5056 | template<typename _RandomAccessIterator> |
5057 | inline void |
5058 | stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last) |
5059 | { |
5060 | |
5061 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
5062 | _RandomAccessIterator>) |
5063 | __glibcxx_function_requires(_LessThanComparableConcept< |
5064 | typename iterator_traits<_RandomAccessIterator>::value_type>) |
5065 | __glibcxx_requires_valid_range(__first, __last); |
5066 | __glibcxx_requires_irreflexive(__first, __last); |
5067 | |
5068 | _GLIBCXX_STD_A::__stable_sort(__first, __last, |
5069 | __gnu_cxx::__ops::__iter_less_iter()); |
5070 | } |
5071 | |
5072 | |
5073 | |
5074 | |
5075 | |
5076 | |
5077 | |
5078 | |
5079 | |
5080 | |
5081 | |
5082 | |
5083 | |
5084 | |
5085 | |
5086 | |
5087 | |
5088 | |
5089 | |
5090 | template<typename _RandomAccessIterator, typename _Compare> |
5091 | inline void |
5092 | stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, |
5093 | _Compare __comp) |
5094 | { |
5095 | |
5096 | __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept< |
5097 | _RandomAccessIterator>) |
5098 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
5099 | typename iterator_traits<_RandomAccessIterator>::value_type, |
5100 | typename iterator_traits<_RandomAccessIterator>::value_type>) |
5101 | __glibcxx_requires_valid_range(__first, __last); |
5102 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
5103 | |
5104 | _GLIBCXX_STD_A::__stable_sort(__first, __last, |
5105 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
5106 | } |
5107 | |
5108 | template<typename _InputIterator1, typename _InputIterator2, |
5109 | typename _OutputIterator, |
5110 | typename _Compare> |
5111 | _GLIBCXX20_CONSTEXPR |
5112 | _OutputIterator |
5113 | __set_union(_InputIterator1 __first1, _InputIterator1 __last1, |
5114 | _InputIterator2 __first2, _InputIterator2 __last2, |
5115 | _OutputIterator __result, _Compare __comp) |
5116 | { |
5117 | while (__first1 != __last1 && __first2 != __last2) |
5118 | { |
5119 | if (__comp(__first1, __first2)) |
5120 | { |
5121 | *__result = *__first1; |
5122 | ++__first1; |
5123 | } |
5124 | else if (__comp(__first2, __first1)) |
5125 | { |
5126 | *__result = *__first2; |
5127 | ++__first2; |
5128 | } |
5129 | else |
5130 | { |
5131 | *__result = *__first1; |
5132 | ++__first1; |
5133 | ++__first2; |
5134 | } |
5135 | ++__result; |
5136 | } |
5137 | return std::copy(__first2, __last2, |
5138 | std::copy(__first1, __last1, __result)); |
5139 | } |
5140 | |
5141 | |
5142 | |
5143 | |
5144 | |
5145 | |
5146 | |
5147 | |
5148 | |
5149 | |
5150 | |
5151 | |
5152 | |
5153 | |
5154 | |
5155 | |
5156 | |
5157 | |
5158 | |
5159 | |
5160 | template<typename _InputIterator1, typename _InputIterator2, |
5161 | typename _OutputIterator> |
5162 | _GLIBCXX20_CONSTEXPR |
5163 | inline _OutputIterator |
5164 | set_union(_InputIterator1 __first1, _InputIterator1 __last1, |
5165 | _InputIterator2 __first2, _InputIterator2 __last2, |
5166 | _OutputIterator __result) |
5167 | { |
5168 | |
5169 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5170 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
5171 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
5172 | typename iterator_traits<_InputIterator1>::value_type>) |
5173 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
5174 | typename iterator_traits<_InputIterator2>::value_type>) |
5175 | __glibcxx_function_requires(_LessThanOpConcept< |
5176 | typename iterator_traits<_InputIterator1>::value_type, |
5177 | typename iterator_traits<_InputIterator2>::value_type>) |
5178 | __glibcxx_function_requires(_LessThanOpConcept< |
5179 | typename iterator_traits<_InputIterator2>::value_type, |
5180 | typename iterator_traits<_InputIterator1>::value_type>) |
5181 | __glibcxx_requires_sorted_set(__first1, __last1, __first2); |
5182 | __glibcxx_requires_sorted_set(__first2, __last2, __first1); |
5183 | __glibcxx_requires_irreflexive2(__first1, __last1); |
5184 | __glibcxx_requires_irreflexive2(__first2, __last2); |
5185 | |
5186 | return _GLIBCXX_STD_A::__set_union(__first1, __last1, |
5187 | __first2, __last2, __result, |
5188 | __gnu_cxx::__ops::__iter_less_iter()); |
5189 | } |
5190 | |
5191 | |
5192 | |
5193 | |
5194 | |
5195 | |
5196 | |
5197 | |
5198 | |
5199 | |
5200 | |
5201 | |
5202 | |
5203 | |
5204 | |
5205 | |
5206 | |
5207 | |
5208 | |
5209 | |
5210 | |
5211 | template<typename _InputIterator1, typename _InputIterator2, |
5212 | typename _OutputIterator, typename _Compare> |
5213 | _GLIBCXX20_CONSTEXPR |
5214 | inline _OutputIterator |
5215 | set_union(_InputIterator1 __first1, _InputIterator1 __last1, |
5216 | _InputIterator2 __first2, _InputIterator2 __last2, |
5217 | _OutputIterator __result, _Compare __comp) |
5218 | { |
5219 | |
5220 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5221 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
5222 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
5223 | typename iterator_traits<_InputIterator1>::value_type>) |
5224 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
5225 | typename iterator_traits<_InputIterator2>::value_type>) |
5226 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
5227 | typename iterator_traits<_InputIterator1>::value_type, |
5228 | typename iterator_traits<_InputIterator2>::value_type>) |
5229 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
5230 | typename iterator_traits<_InputIterator2>::value_type, |
5231 | typename iterator_traits<_InputIterator1>::value_type>) |
5232 | __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
5233 | __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); |
5234 | __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); |
5235 | __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); |
5236 | |
5237 | return _GLIBCXX_STD_A::__set_union(__first1, __last1, |
5238 | __first2, __last2, __result, |
5239 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
5240 | } |
5241 | |
5242 | template<typename _InputIterator1, typename _InputIterator2, |
5243 | typename _OutputIterator, |
5244 | typename _Compare> |
5245 | _GLIBCXX20_CONSTEXPR |
5246 | _OutputIterator |
5247 | __set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, |
5248 | _InputIterator2 __first2, _InputIterator2 __last2, |
5249 | _OutputIterator __result, _Compare __comp) |
5250 | { |
5251 | while (__first1 != __last1 && __first2 != __last2) |
5252 | if (__comp(__first1, __first2)) |
5253 | ++__first1; |
5254 | else if (__comp(__first2, __first1)) |
5255 | ++__first2; |
5256 | else |
5257 | { |
5258 | *__result = *__first1; |
5259 | ++__first1; |
5260 | ++__first2; |
5261 | ++__result; |
5262 | } |
5263 | return __result; |
5264 | } |
5265 | |
5266 | |
5267 | |
5268 | |
5269 | |
5270 | |
5271 | |
5272 | |
5273 | |
5274 | |
5275 | |
5276 | |
5277 | |
5278 | |
5279 | |
5280 | |
5281 | |
5282 | |
5283 | |
5284 | template<typename _InputIterator1, typename _InputIterator2, |
5285 | typename _OutputIterator> |
5286 | _GLIBCXX20_CONSTEXPR |
5287 | inline _OutputIterator |
5288 | set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, |
5289 | _InputIterator2 __first2, _InputIterator2 __last2, |
5290 | _OutputIterator __result) |
5291 | { |
5292 | |
5293 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5294 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
5295 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
5296 | typename iterator_traits<_InputIterator1>::value_type>) |
5297 | __glibcxx_function_requires(_LessThanOpConcept< |
5298 | typename iterator_traits<_InputIterator1>::value_type, |
5299 | typename iterator_traits<_InputIterator2>::value_type>) |
5300 | __glibcxx_function_requires(_LessThanOpConcept< |
5301 | typename iterator_traits<_InputIterator2>::value_type, |
5302 | typename iterator_traits<_InputIterator1>::value_type>) |
5303 | __glibcxx_requires_sorted_set(__first1, __last1, __first2); |
5304 | __glibcxx_requires_sorted_set(__first2, __last2, __first1); |
5305 | __glibcxx_requires_irreflexive2(__first1, __last1); |
5306 | __glibcxx_requires_irreflexive2(__first2, __last2); |
5307 | |
5308 | return _GLIBCXX_STD_A::__set_intersection(__first1, __last1, |
5309 | __first2, __last2, __result, |
5310 | __gnu_cxx::__ops::__iter_less_iter()); |
5311 | } |
5312 | |
5313 | |
5314 | |
5315 | |
5316 | |
5317 | |
5318 | |
5319 | |
5320 | |
5321 | |
5322 | |
5323 | |
5324 | |
5325 | |
5326 | |
5327 | |
5328 | |
5329 | |
5330 | |
5331 | |
5332 | |
5333 | |
5334 | template<typename _InputIterator1, typename _InputIterator2, |
5335 | typename _OutputIterator, typename _Compare> |
5336 | _GLIBCXX20_CONSTEXPR |
5337 | inline _OutputIterator |
5338 | set_intersection(_InputIterator1 __first1, _InputIterator1 __last1, |
5339 | _InputIterator2 __first2, _InputIterator2 __last2, |
5340 | _OutputIterator __result, _Compare __comp) |
5341 | { |
5342 | |
5343 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5344 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
5345 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
5346 | typename iterator_traits<_InputIterator1>::value_type>) |
5347 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
5348 | typename iterator_traits<_InputIterator1>::value_type, |
5349 | typename iterator_traits<_InputIterator2>::value_type>) |
5350 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
5351 | typename iterator_traits<_InputIterator2>::value_type, |
5352 | typename iterator_traits<_InputIterator1>::value_type>) |
5353 | __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
5354 | __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); |
5355 | __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); |
5356 | __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); |
5357 | |
5358 | return _GLIBCXX_STD_A::__set_intersection(__first1, __last1, |
5359 | __first2, __last2, __result, |
5360 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
5361 | } |
5362 | |
5363 | template<typename _InputIterator1, typename _InputIterator2, |
5364 | typename _OutputIterator, |
5365 | typename _Compare> |
5366 | _GLIBCXX20_CONSTEXPR |
5367 | _OutputIterator |
5368 | __set_difference(_InputIterator1 __first1, _InputIterator1 __last1, |
5369 | _InputIterator2 __first2, _InputIterator2 __last2, |
5370 | _OutputIterator __result, _Compare __comp) |
5371 | { |
5372 | while (__first1 != __last1 && __first2 != __last2) |
5373 | if (__comp(__first1, __first2)) |
5374 | { |
5375 | *__result = *__first1; |
5376 | ++__first1; |
5377 | ++__result; |
5378 | } |
5379 | else if (__comp(__first2, __first1)) |
5380 | ++__first2; |
5381 | else |
5382 | { |
5383 | ++__first1; |
5384 | ++__first2; |
5385 | } |
5386 | return std::copy(__first1, __last1, __result); |
5387 | } |
5388 | |
5389 | |
5390 | |
5391 | |
5392 | |
5393 | |
5394 | |
5395 | |
5396 | |
5397 | |
5398 | |
5399 | |
5400 | |
5401 | |
5402 | |
5403 | |
5404 | |
5405 | |
5406 | |
5407 | |
5408 | |
5409 | template<typename _InputIterator1, typename _InputIterator2, |
5410 | typename _OutputIterator> |
5411 | _GLIBCXX20_CONSTEXPR |
5412 | inline _OutputIterator |
5413 | set_difference(_InputIterator1 __first1, _InputIterator1 __last1, |
5414 | _InputIterator2 __first2, _InputIterator2 __last2, |
5415 | _OutputIterator __result) |
5416 | { |
5417 | |
5418 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5419 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
5420 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
5421 | typename iterator_traits<_InputIterator1>::value_type>) |
5422 | __glibcxx_function_requires(_LessThanOpConcept< |
5423 | typename iterator_traits<_InputIterator1>::value_type, |
5424 | typename iterator_traits<_InputIterator2>::value_type>) |
5425 | __glibcxx_function_requires(_LessThanOpConcept< |
5426 | typename iterator_traits<_InputIterator2>::value_type, |
5427 | typename iterator_traits<_InputIterator1>::value_type>) |
5428 | __glibcxx_requires_sorted_set(__first1, __last1, __first2); |
5429 | __glibcxx_requires_sorted_set(__first2, __last2, __first1); |
5430 | __glibcxx_requires_irreflexive2(__first1, __last1); |
5431 | __glibcxx_requires_irreflexive2(__first2, __last2); |
5432 | |
5433 | return _GLIBCXX_STD_A::__set_difference(__first1, __last1, |
5434 | __first2, __last2, __result, |
5435 | __gnu_cxx::__ops::__iter_less_iter()); |
5436 | } |
5437 | |
5438 | |
5439 | |
5440 | |
5441 | |
5442 | |
5443 | |
5444 | |
5445 | |
5446 | |
5447 | |
5448 | |
5449 | |
5450 | |
5451 | |
5452 | |
5453 | |
5454 | |
5455 | |
5456 | |
5457 | |
5458 | |
5459 | |
5460 | |
5461 | template<typename _InputIterator1, typename _InputIterator2, |
5462 | typename _OutputIterator, typename _Compare> |
5463 | _GLIBCXX20_CONSTEXPR |
5464 | inline _OutputIterator |
5465 | set_difference(_InputIterator1 __first1, _InputIterator1 __last1, |
5466 | _InputIterator2 __first2, _InputIterator2 __last2, |
5467 | _OutputIterator __result, _Compare __comp) |
5468 | { |
5469 | |
5470 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5471 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
5472 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
5473 | typename iterator_traits<_InputIterator1>::value_type>) |
5474 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
5475 | typename iterator_traits<_InputIterator1>::value_type, |
5476 | typename iterator_traits<_InputIterator2>::value_type>) |
5477 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
5478 | typename iterator_traits<_InputIterator2>::value_type, |
5479 | typename iterator_traits<_InputIterator1>::value_type>) |
5480 | __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
5481 | __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); |
5482 | __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); |
5483 | __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); |
5484 | |
5485 | return _GLIBCXX_STD_A::__set_difference(__first1, __last1, |
5486 | __first2, __last2, __result, |
5487 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
5488 | } |
5489 | |
5490 | template<typename _InputIterator1, typename _InputIterator2, |
5491 | typename _OutputIterator, |
5492 | typename _Compare> |
5493 | _GLIBCXX20_CONSTEXPR |
5494 | _OutputIterator |
5495 | __set_symmetric_difference(_InputIterator1 __first1, |
5496 | _InputIterator1 __last1, |
5497 | _InputIterator2 __first2, |
5498 | _InputIterator2 __last2, |
5499 | _OutputIterator __result, |
5500 | _Compare __comp) |
5501 | { |
5502 | while (__first1 != __last1 && __first2 != __last2) |
5503 | if (__comp(__first1, __first2)) |
5504 | { |
5505 | *__result = *__first1; |
5506 | ++__first1; |
5507 | ++__result; |
5508 | } |
5509 | else if (__comp(__first2, __first1)) |
5510 | { |
5511 | *__result = *__first2; |
5512 | ++__first2; |
5513 | ++__result; |
5514 | } |
5515 | else |
5516 | { |
5517 | ++__first1; |
5518 | ++__first2; |
5519 | } |
5520 | return std::copy(__first2, __last2, |
5521 | std::copy(__first1, __last1, __result)); |
5522 | } |
5523 | |
5524 | |
5525 | |
5526 | |
5527 | |
5528 | |
5529 | |
5530 | |
5531 | |
5532 | |
5533 | |
5534 | |
5535 | |
5536 | |
5537 | |
5538 | |
5539 | |
5540 | |
5541 | |
5542 | template<typename _InputIterator1, typename _InputIterator2, |
5543 | typename _OutputIterator> |
5544 | _GLIBCXX20_CONSTEXPR |
5545 | inline _OutputIterator |
5546 | set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1, |
5547 | _InputIterator2 __first2, _InputIterator2 __last2, |
5548 | _OutputIterator __result) |
5549 | { |
5550 | |
5551 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5552 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
5553 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
5554 | typename iterator_traits<_InputIterator1>::value_type>) |
5555 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
5556 | typename iterator_traits<_InputIterator2>::value_type>) |
5557 | __glibcxx_function_requires(_LessThanOpConcept< |
5558 | typename iterator_traits<_InputIterator1>::value_type, |
5559 | typename iterator_traits<_InputIterator2>::value_type>) |
5560 | __glibcxx_function_requires(_LessThanOpConcept< |
5561 | typename iterator_traits<_InputIterator2>::value_type, |
5562 | typename iterator_traits<_InputIterator1>::value_type>) |
5563 | __glibcxx_requires_sorted_set(__first1, __last1, __first2); |
5564 | __glibcxx_requires_sorted_set(__first2, __last2, __first1); |
5565 | __glibcxx_requires_irreflexive2(__first1, __last1); |
5566 | __glibcxx_requires_irreflexive2(__first2, __last2); |
5567 | |
5568 | return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1, |
5569 | __first2, __last2, __result, |
5570 | __gnu_cxx::__ops::__iter_less_iter()); |
5571 | } |
5572 | |
5573 | |
5574 | |
5575 | |
5576 | |
5577 | |
5578 | |
5579 | |
5580 | |
5581 | |
5582 | |
5583 | |
5584 | |
5585 | |
5586 | |
5587 | |
5588 | |
5589 | |
5590 | |
5591 | |
5592 | |
5593 | |
5594 | template<typename _InputIterator1, typename _InputIterator2, |
5595 | typename _OutputIterator, typename _Compare> |
5596 | _GLIBCXX20_CONSTEXPR |
5597 | inline _OutputIterator |
5598 | set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1, |
5599 | _InputIterator2 __first2, _InputIterator2 __last2, |
5600 | _OutputIterator __result, |
5601 | _Compare __comp) |
5602 | { |
5603 | |
5604 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>) |
5605 | __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>) |
5606 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
5607 | typename iterator_traits<_InputIterator1>::value_type>) |
5608 | __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator, |
5609 | typename iterator_traits<_InputIterator2>::value_type>) |
5610 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
5611 | typename iterator_traits<_InputIterator1>::value_type, |
5612 | typename iterator_traits<_InputIterator2>::value_type>) |
5613 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
5614 | typename iterator_traits<_InputIterator2>::value_type, |
5615 | typename iterator_traits<_InputIterator1>::value_type>) |
5616 | __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp); |
5617 | __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp); |
5618 | __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp); |
5619 | __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp); |
5620 | |
5621 | return _GLIBCXX_STD_A::__set_symmetric_difference(__first1, __last1, |
5622 | __first2, __last2, __result, |
5623 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
5624 | } |
5625 | |
5626 | template<typename _ForwardIterator, typename _Compare> |
5627 | _GLIBCXX14_CONSTEXPR |
5628 | _ForwardIterator |
5629 | __min_element(_ForwardIterator __first, _ForwardIterator __last, |
5630 | _Compare __comp) |
5631 | { |
5632 | if (__first == __last) |
5633 | return __first; |
5634 | _ForwardIterator __result = __first; |
5635 | while (++__first != __last) |
5636 | if (__comp(__first, __result)) |
5637 | __result = __first; |
5638 | return __result; |
5639 | } |
5640 | |
5641 | |
5642 | |
5643 | |
5644 | |
5645 | |
5646 | |
5647 | |
5648 | template<typename _ForwardIterator> |
5649 | _GLIBCXX14_CONSTEXPR |
5650 | _ForwardIterator |
5651 | inline min_element(_ForwardIterator __first, _ForwardIterator __last) |
5652 | { |
5653 | |
5654 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
5655 | __glibcxx_function_requires(_LessThanComparableConcept< |
5656 | typename iterator_traits<_ForwardIterator>::value_type>) |
5657 | __glibcxx_requires_valid_range(__first, __last); |
5658 | __glibcxx_requires_irreflexive(__first, __last); |
5659 | |
5660 | return _GLIBCXX_STD_A::__min_element(__first, __last, |
5661 | __gnu_cxx::__ops::__iter_less_iter()); |
5662 | } |
5663 | |
5664 | |
5665 | |
5666 | |
5667 | |
5668 | |
5669 | |
5670 | |
5671 | |
5672 | |
5673 | template<typename _ForwardIterator, typename _Compare> |
5674 | _GLIBCXX14_CONSTEXPR |
5675 | inline _ForwardIterator |
5676 | min_element(_ForwardIterator __first, _ForwardIterator __last, |
5677 | _Compare __comp) |
5678 | { |
5679 | |
5680 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
5681 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
5682 | typename iterator_traits<_ForwardIterator>::value_type, |
5683 | typename iterator_traits<_ForwardIterator>::value_type>) |
5684 | __glibcxx_requires_valid_range(__first, __last); |
5685 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
5686 | |
5687 | return _GLIBCXX_STD_A::__min_element(__first, __last, |
5688 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
5689 | } |
5690 | |
5691 | template<typename _ForwardIterator, typename _Compare> |
5692 | _GLIBCXX14_CONSTEXPR |
5693 | _ForwardIterator |
5694 | __max_element(_ForwardIterator __first, _ForwardIterator __last, |
5695 | _Compare __comp) |
5696 | { |
5697 | if (__first == __last) return __first; |
5698 | _ForwardIterator __result = __first; |
5699 | while (++__first != __last) |
5700 | if (__comp(__result, __first)) |
5701 | __result = __first; |
5702 | return __result; |
5703 | } |
5704 | |
5705 | |
5706 | |
5707 | |
5708 | |
5709 | |
5710 | |
5711 | |
5712 | template<typename _ForwardIterator> |
5713 | _GLIBCXX14_CONSTEXPR |
5714 | inline _ForwardIterator |
5715 | max_element(_ForwardIterator __first, _ForwardIterator __last) |
5716 | { |
5717 | |
5718 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
5719 | __glibcxx_function_requires(_LessThanComparableConcept< |
5720 | typename iterator_traits<_ForwardIterator>::value_type>) |
5721 | __glibcxx_requires_valid_range(__first, __last); |
5722 | __glibcxx_requires_irreflexive(__first, __last); |
5723 | |
5724 | return _GLIBCXX_STD_A::__max_element(__first, __last, |
5725 | __gnu_cxx::__ops::__iter_less_iter()); |
5726 | } |
5727 | |
5728 | |
5729 | |
5730 | |
5731 | |
5732 | |
5733 | |
5734 | |
5735 | |
5736 | |
5737 | template<typename _ForwardIterator, typename _Compare> |
5738 | _GLIBCXX14_CONSTEXPR |
5739 | inline _ForwardIterator |
5740 | max_element(_ForwardIterator __first, _ForwardIterator __last, |
5741 | _Compare __comp) |
5742 | { |
5743 | |
5744 | __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>) |
5745 | __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, |
5746 | typename iterator_traits<_ForwardIterator>::value_type, |
5747 | typename iterator_traits<_ForwardIterator>::value_type>) |
5748 | __glibcxx_requires_valid_range(__first, __last); |
5749 | __glibcxx_requires_irreflexive_pred(__first, __last, __comp); |
5750 | |
5751 | return _GLIBCXX_STD_A::__max_element(__first, __last, |
5752 | __gnu_cxx::__ops::__iter_comp_iter(__comp)); |
5753 | } |
5754 | |
5755 | #if __cplusplus >= 201402L |
5756 | |
5757 | template<typename _InputIterator, typename _RandomAccessIterator, |
5758 | typename _Size, typename _UniformRandomBitGenerator> |
5759 | _RandomAccessIterator |
5760 | __sample(_InputIterator __first, _InputIterator __last, input_iterator_tag, |
5761 | _RandomAccessIterator __out, random_access_iterator_tag, |
5762 | _Size __n, _UniformRandomBitGenerator&& __g) |
5763 | { |
5764 | using __distrib_type = uniform_int_distribution<_Size>; |
5765 | using __param_type = typename __distrib_type::param_type; |
5766 | __distrib_type __d{}; |
5767 | _Size __sample_sz = 0; |
5768 | while (__first != __last && __sample_sz != __n) |
5769 | { |
5770 | __out[__sample_sz++] = *__first; |
5771 | ++__first; |
5772 | } |
5773 | for (auto __pop_sz = __sample_sz; __first != __last; |
5774 | ++__first, (void) ++__pop_sz) |
5775 | { |
5776 | const auto __k = __d(__g, __param_type{0, __pop_sz}); |
5777 | if (__k < __n) |
5778 | __out[__k] = *__first; |
5779 | } |
5780 | return __out + __sample_sz; |
5781 | } |
5782 | |
5783 | |
5784 | template<typename _ForwardIterator, typename _OutputIterator, typename _Cat, |
5785 | typename _Size, typename _UniformRandomBitGenerator> |
5786 | _OutputIterator |
5787 | __sample(_ForwardIterator __first, _ForwardIterator __last, |
5788 | forward_iterator_tag, |
5789 | _OutputIterator __out, _Cat, |
5790 | _Size __n, _UniformRandomBitGenerator&& __g) |
5791 | { |
5792 | using __distrib_type = uniform_int_distribution<_Size>; |
5793 | using __param_type = typename __distrib_type::param_type; |
5794 | using _USize = make_unsigned_t<_Size>; |
5795 | using _Gen = remove_reference_t<_UniformRandomBitGenerator>; |
5796 | using __uc_type = common_type_t<typename _Gen::result_type, _USize>; |
5797 | |
5798 | __distrib_type __d{}; |
5799 | _Size __unsampled_sz = std::distance(__first, __last); |
5800 | __n = std::min(__n, __unsampled_sz); |
5801 | |
5802 | |
5803 | |
5804 | |
5805 | const __uc_type __urngrange = __g.max() - __g.min(); |
5806 | if (__urngrange / __uc_type(__unsampled_sz) >= __uc_type(__unsampled_sz)) |
5807 | |
5808 | |
5809 | { |
5810 | while (__n != 0 && __unsampled_sz >= 2) |
5811 | { |
5812 | const pair<_Size, _Size> __p = |
5813 | __gen_two_uniform_ints(__unsampled_sz, __unsampled_sz - 1, __g); |
5814 | |
5815 | --__unsampled_sz; |
5816 | if (__p.first < __n) |
5817 | { |
5818 | *__out++ = *__first; |
5819 | --__n; |
5820 | } |
5821 | |
5822 | ++__first; |
5823 | |
5824 | if (__n == 0) break; |
5825 | |
5826 | --__unsampled_sz; |
5827 | if (__p.second < __n) |
5828 | { |
5829 | *__out++ = *__first; |
5830 | --__n; |
5831 | } |
5832 | |
5833 | ++__first; |
5834 | } |
5835 | } |
5836 | |
5837 | |
5838 | |
5839 | for (; __n != 0; ++__first) |
5840 | if (__d(__g, __param_type{0, --__unsampled_sz}) < __n) |
5841 | { |
5842 | *__out++ = *__first; |
5843 | --__n; |
5844 | } |
5845 | return __out; |
5846 | } |
5847 | |
5848 | #if __cplusplus > 201402L |
5849 | #define __cpp_lib_sample 201603 |
5850 | |
5851 | template<typename _PopulationIterator, typename _SampleIterator, |
5852 | typename _Distance, typename _UniformRandomBitGenerator> |
5853 | _SampleIterator |
5854 | sample(_PopulationIterator __first, _PopulationIterator __last, |
5855 | _SampleIterator __out, _Distance __n, |
5856 | _UniformRandomBitGenerator&& __g) |
5857 | { |
5858 | using __pop_cat = typename |
5859 | std::iterator_traits<_PopulationIterator>::iterator_category; |
5860 | using __samp_cat = typename |
5861 | std::iterator_traits<_SampleIterator>::iterator_category; |
5862 | |
5863 | static_assert( |
5864 | __or_<is_convertible<__pop_cat, forward_iterator_tag>, |
5865 | is_convertible<__samp_cat, random_access_iterator_tag>>::value, |
5866 | "output range must use a RandomAccessIterator when input range" |
5867 | " does not meet the ForwardIterator requirements"); |
5868 | |
5869 | static_assert(is_integral<_Distance>::value, |
5870 | "sample size must be an integer type"); |
5871 | |
5872 | typename iterator_traits<_PopulationIterator>::difference_type __d = __n; |
5873 | return _GLIBCXX_STD_A:: |
5874 | __sample(__first, __last, __pop_cat{}, __out, __samp_cat{}, __d, |
5875 | std::forward<_UniformRandomBitGenerator>(__g)); |
5876 | } |
5877 | #endif // C++17 |
5878 | #endif // C++14 |
5879 | |
5880 | _GLIBCXX_END_NAMESPACE_ALGO |
5881 | _GLIBCXX_END_NAMESPACE_VERSION |
5882 | } |
5883 | |
5884 | #endif /* _STL_ALGO_H */ |