Scippy

SoPlex

Sequential object-oriented simPlex

idxset.cpp
Go to the documentation of this file.
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 
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 
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 
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
void add(int n)
appends n uninitialized indices.
Definition: idxset.h:158
int * idx
array of indices
Definition: idxset.h:74
int max() const
returns the maximal number of indices which can be stored in IdxSet.
Definition: idxset.h:138
Set of indices.
#define SPX_MSG_INCONSISTENT(name)
Definition: spxdefines.h:175
IdxSet()
default constructor.
Definition: idxset.h:100
bool isConsistent() const
consistency check.
Definition: idxset.cpp:126
void spx_alloc(T &p, int n=1)
Allocate memory.
Definition: spxalloc.h:58
void remove(int n, int m)
removes indices at position numbers n through m.
Definition: idxset.cpp:60
bool freeArray
true iff idx should be freed inside of this object
Definition: idxset.h:75
int pos(int i) const
returns the position of index i.
Definition: idxset.cpp:41
int size() const
returns the number of used indices.
Definition: idxset.h:133
int len
length of array idx
Definition: idxset.h:73
IdxSet & operator=(const IdxSet &set)
assignment operator.
Definition: idxset.cpp:80
Everything should be within this namespace.
int dim() const
returns the maximal index.
Definition: idxset.cpp:30
int index(int n) const
access n &#39;th index.
Definition: idxset.h:127
int num
number of used indices
Definition: idxset.h:72
Set of indices.Class IdxSet provides a set of indices. At construction it must be given an array of i...
Definition: idxset.h:65
void spx_free(T &p)
Release memory.
Definition: spxalloc.h:121