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-2017 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 "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  for (int j = 0; j < n; j++)
45  idx[size() + j] = i[j];
46  add(n);
47 }
48 
49 void IdxSet::remove(int n, int m)
50 {
51  assert(n <= m && m < size() && n >= 0);
52  ++m;
53 
54  int cpy = m - n;
55  int newnum = num - cpy;
56  cpy = (size() - m >= cpy) ? cpy : size() - m;
57 
58  do
59  {
60  --num;
61  --cpy;
62  idx[n + cpy] = idx[num];
63  }
64  while (cpy > 0);
65  num = newnum;
66 }
67 
69 {
70  if (this != &rhs)
71  {
72  if (idx != 0 && max() < rhs.size())
73  {
74  if (freeArray)
75  spx_free(idx);
76  idx = 0;
77  }
78 
79  if (idx == 0)
80  {
81  len = rhs.size();
82  spx_alloc(idx, len);
83  freeArray = true;
84  }
85 
86  for (num = 0; num < rhs.size(); ++num)
87  idx[num] = rhs.idx[num];
88  }
89 
90  assert(size() == rhs.size());
91  assert(size() <= max());
92  assert(isConsistent());
93 
94  return *this;
95 }
96 
98  : len(old.len)
99  , idx(0)
100 {
101  spx_alloc(idx, len);
102 
103  for (num = 0; num < old.num; num++)
104  idx[num] = old.idx[num];
105 
106  freeArray = true;
107 
108  assert(size() == old.size());
109  assert(size() <= max());
110  assert(isConsistent());
111 }
112 
114 {
115 #ifdef ENABLE_CONSISTENCY_CHECKS
116  int i, j;
117 
118  if (len > 0 && idx == 0)
119  return MSGinconsistent("IdxSet");
120 
121  for (i = 0; i < size(); ++i)
122  {
123  if (index(i) < 0)
124  return MSGinconsistent("IdxSet");
125 
126  for (j = 0; j < i; j++)
127  if (index(i) == index(j))
128  return MSGinconsistent("IdxSet");
129  }
130 #endif
131 
132  return true;
133 }
134 } // 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:113
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:49
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:68
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:109