Scippy

SoPlex

Sequential object-oriented simPlex

nameset.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-2018 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 <string.h>
17 #include "soplex/spxdefines.h"
18 #include "soplex/nameset.h"
19 #include "soplex/spxalloc.h"
20 
21 namespace soplex
22 {
23 const char NameSet::Name::deflt = '\0';
24 
25 void NameSet::add(const char* str)
26 {
27  DataKey k;
28  add(k, str);
29 }
30 
31 void NameSet::add(DataKey& p_key, const char* str)
32 {
33  const Name nstr (str);
34 
35  if (!hashtab.has(nstr))
36  {
37  if (size() + 1 > max() * HASHTABLE_FILLFACTOR)
38  {
39  assert(factor >= 1);
40  reMax(int(factor*max() + 8));
41  }
42 
43  if (memSize() + int(strlen(str)) >= memMax())
44  {
45  memPack();
46  if (memSize() + int(strlen(str)) >= memMax())
47  {
48  assert(memFactor >= 1);
49  memRemax(int(memFactor*memMax()) + 9 + int(strlen(str)));
50  assert(memSize() + int(strlen(str)) < memMax());
51  }
52  }
53  int idx = memused;
54  char* tmp = &(mem[idx]);
55  memused += int(strlen(str)) + 1;
56 
57  spxSnprintf(tmp, SPX_MAXSTRLEN, "%s", str);
58  *(set.create(p_key)) = idx;
59  Name memname(tmp);
60  hashtab.add(memname, p_key);
61  }
62 }
63 
64 void NameSet::add(const NameSet& p_set)
65 {
66  for (int i = 0; i < p_set.num(); ++i)
67  {
68  Name iname(p_set[i]);
69  if (!hashtab.has(iname))
70  add(p_set[i]);
71  }
72 }
73 
74 void NameSet::add(DataKey p_key[], const NameSet& p_set)
75 {
76  for (int i = 0; i < p_set.num(); ++i)
77  {
78  Name iname = Name(p_set[i]);
79  if (!hashtab.has(iname))
80  add(p_key[i], p_set[i]);
81  }
82 }
83 
84 void NameSet::remove(const char *str)
85 {
86  const Name nam(str);
87  if (hashtab.has(nam))
88  {
89  const DataKey* hkey = hashtab.get(nam);
90  assert(hkey != 0);
91  hashtab.remove(nam);
92  set.remove(*hkey);
93  }
94 }
95 
96 void NameSet::remove(const DataKey& p_key)
97 {
98  assert(has(p_key));
99 
100  hashtab.remove(Name(&mem[set[p_key]]));
101  set.remove(p_key);
102 }
103 
104 void NameSet::remove(const DataKey keys[], int n)
105 {
106  for (int i = 0; i < n; ++i)
107  remove(keys[i]);
108 }
109 
110 void NameSet::remove(const int nums[], int n)
111 {
112  for (int i = 0; i < n; ++i)
113  remove(nums[i]);
114 }
115 
116 void NameSet::remove(int dstat[])
117 {
118  for(int i = 0; i < set.num(); i++)
119  {
120  if (dstat[i] < 0)
121  {
122  const Name nam = &mem[set[i]];
123  hashtab.remove(nam);
124  }
125  }
126  set.remove(dstat);
127 
128  assert(isConsistent());
129 }
130 
132 {
133  set.clear();
134  hashtab.clear();
135  memused = 0;
136 }
137 
138 void NameSet::reMax(int newmax)
139 {
140  hashtab.reMax(newmax);
141  set.reMax(newmax);
142 }
143 
144 void NameSet::memRemax(int newmax)
145 {
146  memmax = (newmax < memSize()) ? memSize() : newmax;
148 
149  hashtab.clear ();
150 
151  for (int i = num() - 1; i >= 0; --i)
152  hashtab.add(Name(&mem[set[key(i)]]), key(i));
153 }
154 
156 {
157  char* newmem = 0;
158  int newlast = 0;
159  int i;
160 
161  hashtab.clear();
162 
163  spx_alloc(newmem, memSize());
164 
165  for(i = 0; i < num(); i++)
166  {
167  const char* t = &mem[set[i]];
168  spxSnprintf(&newmem[newlast], SPX_MAXSTRLEN, "%s", t);
169  set[i] = newlast;
170  newlast += int(strlen(t)) + 1;
171  }
172  memcpy(mem, newmem, static_cast<size_t>(newlast));
173  memused = newlast;
174 
175  assert(memSize() <= memMax());
176 
177  spx_free(newmem);
178 
179  for (i = 0; i < num(); i++)
180  hashtab.add(Name(&mem[set[key(i)]]), key(i));
181 }
182 
183 /// returns the hash value of the name.
185 {
186  unsigned int res = 37;
187  const char* sptr = str->name;
188 
189  while(*sptr != '\0')
190  {
191  res *= 11;
192  res += (unsigned int) (*sptr++);
193 
194  }
195  res %= 0x0fffffff;
196  return ((int) res);
197 }
198 
199 NameSet::NameSet(int p_max, int mmax, Real fac, Real memFac)
200  : set(p_max)
201  , mem(0)
202  , hashtab(NameSetNameHashFunction, set.max(), 0, fac)
203  , factor(fac)
204  , memFactor(memFac)
205 {
206  memused = 0;
207  memmax = (mmax < 1) ? (8 * set.max() + 1) : mmax;
208  spx_alloc(mem, memmax);
209 }
210 
212 {
213  spx_free(mem);
214 }
215 
217 {
218 #ifdef ENABLE_CONSISTENCY_CHECKS
219  if (memused > memmax)
220  return MSGinconsistent("NameSet");
221 
222  int i;
223 
224  for(i = 0; i < num(); i++)
225  {
226  const char* t = &mem[set[i]];
227 
228  if (!has(t))
229  return MSGinconsistent("NameSet");
230 
231  if (strcmp(t, operator[](key(t))))
232  return MSGinconsistent("NameSet");
233  }
234  return set.isConsistent() && hashtab.isConsistent();
235 #else
236  return true;
237 #endif
238 }
239 
240 std::ostream& operator<<(std::ostream& s, const NameSet& nset)
241 {
242  for(int i = 0; i < nset.num(); i++)
243  {
244  s << i << " "
245  << nset.key(i).info << "."
246  << nset.key(i).idx << "= "
247  << nset[i]
248  << std::endl;
249  }
250  return s;
251 }
252 
253 
254 } // namespace soplex
static int NameSetNameHashFunction(const NameSet::Name *str)
returns the hash value of the name.
Definition: nameset.cpp:184
int memSize() const
returns used length of string memory.
Definition: nameset.h:192
const char * name
pointer to the name string.
Definition: nameset.h:86
int num() const
returns nr. of names in NameSet.
Definition: nameset.h:168
Memory allocation routines.
DataHashTable< Name, DataKey > hashtab
hashtable for names
Definition: nameset.h:147
void reMax(int newmax=0)
resets max() to newmax.
Definition: nameset.cpp:138
Set of strings.
#define HASHTABLE_FILLFACTOR
Definition: datahashtable.h:28
Entry identifier class for items of a DataSet.Every item in a DataSet is assigned a DataKey by which ...
Definition: datakey.h:46
static const char deflt
default zero string.
Definition: nameset.h:78
int memmax
size of string memory
Definition: nameset.h:141
Handles of names in a NameSet.Class Name provides the handles (i.e., char*s) of names in a NameSet...
Definition: nameset.h:71
NameSet(int max=10000, int mmax=-1, Real fac=2, Real memFac=2)
default constructor.
Definition: nameset.cpp:199
void spx_alloc(T &p, int n=1)
Allocate memory.
Definition: spxalloc.h:48
DataSet< int > set
name set.
Definition: nameset.h:139
double Real
Definition: spxdefines.h:218
std::ostream & operator<<(std::ostream &s, const VectorBase< R > &vec)
Output operator.
Definition: basevectors.h:1123
int spxSnprintf(char *t, size_t len, const char *s,...)
safe version of snprintf
Definition: spxdefines.h:460
int info
user information to store values -1, 0, +1
Definition: datakey.h:55
Real factor
memory extension factor for entries.
Definition: nameset.h:317
DataKey key(int pnum) const
returns DataKey of the pnum &#39;th name in NameSet.
Definition: nameset.h:198
void add(const char *str)
Definition: nameset.cpp:25
void clear()
removes all names from NameSet.
Definition: nameset.cpp:131
Debugging, floating point type and parameter definitions.
Set of strings.Class NameSet implements a symbol or name table. It allows to store or remove names (i...
Definition: nameset.h:61
char * mem
string memory
Definition: nameset.h:140
void spx_realloc(T &p, int n)
Change amount of allocated memory.
Definition: spxalloc.h:79
Everything should be within this namespace.
int memused
size of used string memory
Definition: nameset.h:142
void memRemax(int newmax=0)
resets memMax() to newmax.
Definition: nameset.cpp:144
void memPack()
garbage collection.
Definition: nameset.cpp:155
Real memFactor
memory extension factor for names.
Definition: nameset.h:325
bool isConsistent() const
consistency check.
Definition: nameset.cpp:216
#define MSGinconsistent(name)
Definition: spxdefines.h:126
int max() const
returns maximum nr. of names that fit into NameSet.
Definition: nameset.h:174
int size() const
returns maximum DataKey::idx used in NameSet.
Definition: nameset.h:180
void remove(const DataKey &key)
removes name with DataKey key from NameSet.
Definition: nameset.cpp:96
~NameSet()
destructor.
Definition: nameset.cpp:211
int idx
(locally) unique key index
Definition: datakey.h:56
void spx_free(T &p)
Release memory.
Definition: spxalloc.h:109
bool has(int pnum) const
does NameSet has a name with number pnum?
Definition: nameset.h:231
int memMax() const
returns maximum length of string memory.
Definition: nameset.h:186
#define SPX_MAXSTRLEN
Definition: spxdefines.h:249