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-2015 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 "spxdefines.h"
18 #include "nameset.h"
19 #include "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  strcpy(tmp, 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  strcpy(&newmem[newlast], 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 #if 0
200 NameSet& NameSet::operator=(const NameSet& rhs)
201 {
202  if (this != &rhs)
203  {
204  if (max() < rhs.size())
205  reMax(rhs.size());
206  if (memMax() < rhs.memSize())
207  memRemax(rhs.memSize());
208 
209  set = rhs.set;
210 
211  hashtab.clear();
212  for (int i = 0; i < set.num(); ++i)
213  {
214  Name iname(set[i].name);
215  DataKey ikey = DataKey(set.key(i));
216  hashtab.add(iname, ikey);
217  }
218  memPack();
219  }
220  return *this;
221 }
222 
223 NameSet::NameSet(const NameSet& org)
224  : set(org.set)
225  , mem(0)
226  , hashtab(org.hashtab)
227  , factor(org.factor)
228  , memFactor(org.memFactor)
229 {
230  memused = 0;
231  memmax = org.memSize();
232  spx_alloc(mem, memmax);
233 
234  list.clear();
235  hashtab.clear();
236  for (int i = 0; i < set.num(); ++i)
237  {
238  list.append(&(set[i]));
239  Name iname = set[i];
240  DataKey k = DataKey(set.key(i));
241  hashtab.add(iname, k);
242  }
243  memPack();
244 }
245 #endif
246 
247 NameSet::NameSet(int p_max, int mmax, Real fac, Real memFac)
248  : set(p_max)
249  , mem(0)
250  , hashtab(NameSetNameHashFunction, set.max(), 0, fac)
251  , factor(fac)
252  , memFactor(memFac)
253 {
254  memused = 0;
255  memmax = (mmax < 1) ? (8 * set.max() + 1) : mmax;
256  spx_alloc(mem, memmax);
257 }
258 
260 {
261  spx_free(mem);
262 }
263 
265 {
266 #ifdef ENABLE_CONSISTENCY_CHECKS
267  if (memused > memmax)
268  return MSGinconsistent("NameSet");
269 
270  int i;
271 
272  for(i = 0; i < num(); i++)
273  {
274  const char* t = &mem[set[i]];
275 
276  if (!has(t))
277  return MSGinconsistent("NameSet");
278 
279  if (strcmp(t, operator[](key(t))))
280  return MSGinconsistent("NameSet");
281  }
282  return set.isConsistent() && hashtab.isConsistent();
283 #else
284  return true;
285 #endif
286 }
287 
288 std::ostream& operator<<(std::ostream& s, const NameSet& nset)
289 {
290  for(int i = 0; i < nset.num(); i++)
291  {
292  s << i << " "
293  << nset.key(i).info << "."
294  << nset.key(i).idx << "= "
295  << nset[i]
296  << std::endl;
297  }
298  return s;
299 }
300 
301 
302 } // namespace soplex