1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 2 /* */ 3 /* This file is part of the class library */ 4 /* SoPlex --- the Sequential object-oriented simPlex. */ 5 /* */ 6 /* Copyright (c) 1996-2023 Zuse Institute Berlin (ZIB) */ 7 /* */ 8 /* Licensed under the Apache License, Version 2.0 (the "License"); */ 9 /* you may not use this file except in compliance with the License. */ 10 /* You may obtain a copy of the License at */ 11 /* */ 12 /* http://www.apache.org/licenses/LICENSE-2.0 */ 13 /* */ 14 /* Unless required by applicable law or agreed to in writing, software */ 15 /* distributed under the License is distributed on an "AS IS" BASIS, */ 16 /* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */ 17 /* See the License for the specific language governing permissions and */ 18 /* limitations under the License. */ 19 /* */ 20 /* You should have received a copy of the Apache-2.0 license */ 21 /* along with SoPlex; see the file LICENSE. If not email to soplex@zib.de. */ 22 /* */ 23 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 24 25 #include "soplex/idxset.h" 26 27 namespace soplex 28 { 29 30 int IdxSet::dim() const 31 { 32 int ddim = -1; 33 34 for(int i = 0; i < size(); i++) 35 if(ddim < idx[i]) 36 ddim = idx[i]; 37 38 return ddim; 39 } 40 41 int IdxSet::pos(int i) const 42 { 43 for(int n = 0; n < size(); n++) 44 if(idx[n] == i) 45 return n; 46 47 return -1; 48 } 49 50 void IdxSet::add(int n, const int i[]) 51 { 52 assert(n >= 0 && size() + n <= max()); 53 54 for(int j = 0; j < n; j++) 55 idx[size() + j] = i[j]; 56 57 add(n); 58 } 59 60 void IdxSet::remove(int n, int m) 61 { 62 assert(n <= m && m < size() && n >= 0); 63 ++m; 64 65 int cpy = m - n; 66 int newnum = num - cpy; 67 cpy = (size() - m >= cpy) ? cpy : size() - m; 68 69 do 70 { 71 --num; 72 --cpy; 73 idx[n + cpy] = idx[num]; 74 } 75 while(cpy > 0); 76 77 num = newnum; 78 } 79 80 IdxSet& IdxSet::operator=(const IdxSet& rhs) 81 { 82 if(this != &rhs) 83 { 84 if(idx != 0 && max() < rhs.size()) 85 { 86 if(freeArray) 87 spx_free(idx); 88 89 idx = 0; 90 } 91 92 if(idx == 0) 93 { 94 len = rhs.size(); 95 spx_alloc(idx, len); 96 freeArray = true; 97 } 98 99 for(num = 0; num < rhs.size(); ++num) 100 idx[num] = rhs.idx[num]; 101 } 102 103 assert(size() == rhs.size()); 104 assert(size() <= max()); 105 assert(isConsistent()); 106 107 return *this; 108 } 109 110 IdxSet::IdxSet(const IdxSet& old) 111 : len(old.len) 112 , idx(0) 113 { 114 spx_alloc(idx, len); 115 116 for(num = 0; num < old.num; num++) 117 idx[num] = old.idx[num]; 118 119 freeArray = true; 120 121 assert(size() == old.size()); 122 assert(size() <= max()); 123 assert(isConsistent()); 124 } 125 126 bool IdxSet::isConsistent() const 127 { 128 #ifdef ENABLE_CONSISTENCY_CHECKS 129 int i, j; 130 131 if(len > 0 && idx == 0) 132 return SPX_MSG_INCONSISTENT("IdxSet"); 133 134 for(i = 0; i < size(); ++i) 135 { 136 if(index(i) < 0) 137 return SPX_MSG_INCONSISTENT("IdxSet"); 138 139 for(j = 0; j < i; j++) 140 if(index(i) == index(j)) 141 return SPX_MSG_INCONSISTENT("IdxSet"); 142 } 143 144 #endif 145 146 return true; 147 } 148 } // namespace soplex 149