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-2019 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SoPlex is distributed under the terms of the ZIB Academic Licence. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SoPlex; see the file COPYING. If not email to soplex@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 #include "soplex/idxset.h"
17 
18 namespace soplex
19 {
20 
21 int IdxSet::dim() const
22 {
23  int ddim = -1;
24 
25  for(int i = 0; i < size(); i++)
26  if(ddim < idx[i])
27  ddim = idx[i];
28 
29  return ddim;
30 }
31 
32 int IdxSet::pos(int i) const
33 {
34  for(int n = 0; n < size(); n++)
35  if(idx[n] == i)
36  return n;
37 
38  return -1;
39 }
40 
41 void IdxSet::add(int n, const int i[])
42 {
43  assert(n >= 0 && size() + n <= max());
44 
45  for(int j = 0; j < n; j++)
46  idx[size() + j] = i[j];
47 
48  add(n);
49 }
50 
51 void IdxSet::remove(int n, int m)
52 {
53  assert(n <= m && m < size() && n >= 0);
54  ++m;
55 
56  int cpy = m - n;
57  int newnum = num - cpy;
58  cpy = (size() - m >= cpy) ? cpy : size() - m;
59 
60  do
61  {
62  --num;
63  --cpy;
64  idx[n + cpy] = idx[num];
65  }
66  while(cpy > 0);
67 
68  num = newnum;
69 }
70 
72 {
73  if(this != &rhs)
74  {
75  if(idx != 0 && max() < rhs.size())
76  {
77  if(freeArray)
78  spx_free(idx);
79 
80  idx = 0;
81  }
82 
83  if(idx == 0)
84  {
85  len = rhs.size();
86  spx_alloc(idx, len);
87  freeArray = true;
88  }
89 
90  for(num = 0; num < rhs.size(); ++num)
91  idx[num] = rhs.idx[num];
92  }
93 
94  assert(size() == rhs.size());
95  assert(size() <= max());
96  assert(isConsistent());
97 
98  return *this;
99 }
100 
102  : len(old.len)
103  , idx(0)
104 {
105  spx_alloc(idx, len);
106 
107  for(num = 0; num < old.num; num++)
108  idx[num] = old.idx[num];
109 
110  freeArray = true;
111 
112  assert(size() == old.size());
113  assert(size() <= max());
114  assert(isConsistent());
115 }
116 
118 {
119 #ifdef ENABLE_CONSISTENCY_CHECKS
120  int i, j;
121 
122  if(len > 0 && idx == 0)
123  return MSGinconsistent("IdxSet");
124 
125  for(i = 0; i < size(); ++i)
126  {
127  if(index(i) < 0)
128  return MSGinconsistent("IdxSet");
129 
130  for(j = 0; j < i; j++)
131  if(index(i) == index(j))
132  return MSGinconsistent("IdxSet");
133  }
134 
135 #endif
136 
137  return true;
138 }
139 } // namespace soplex
void add(int n)
appends n uninitialized indices.
Definition: idxset.h:149
int * idx
array of indices
Definition: idxset.h:65
int max() const
returns the maximal number of indices which can be stored in IdxSet.
Definition: idxset.h:129
Set of indices.
IdxSet()
default constructor.
Definition: idxset.h:91
bool isConsistent() const
consistency check.
Definition: idxset.cpp:117
void spx_alloc(T &p, int n=1)
Allocate memory.
Definition: spxalloc.h:48
void remove(int n, int m)
removes indices at position numbers n through m.
Definition: idxset.cpp:51
bool freeArray
true iff idx should be freed inside of this object
Definition: idxset.h:66
int pos(int i) const
returns the position of index i.
Definition: idxset.cpp:32
int size() const
returns the number of used indices.
Definition: idxset.h:124
int len
length of array idx
Definition: idxset.h:64
IdxSet & operator=(const IdxSet &set)
assignment operator.
Definition: idxset.cpp:71
Everything should be within this namespace.
int dim() const
returns the maximal index.
Definition: idxset.cpp:21
#define MSGinconsistent(name)
Definition: spxdefines.h:126
int index(int n) const
access n &#39;th index.
Definition: idxset.h:118
int num
number of used indices
Definition: idxset.h:63
Set of indices.Class IdxSet provides a set of indices. At construction it must be given an array of i...
Definition: idxset.h:56
void spx_free(T &p)
Release memory.
Definition: spxalloc.h:110