Scippy

SoPlex

Sequential object-oriented simPlex

nameset.h
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 /**@file nameset.h
17  * @brief Set of strings.
18  */
19 #ifndef _NAMESET_H_
20 #define _NAMESET_H_
21 
22 #include <assert.h>
23 
24 #include "soplex/spxdefines.h"
25 #include "soplex/dataset.h"
26 #include "soplex/datahashtable.h"
27 #include "soplex/datakey.h"
28 
29 namespace soplex
30 {
31 /**@class NameSet
32  @brief Set of strings.
33  @ingroup Elementary
34 
35  Class NameSet implements a symbol or name table. It allows to store or
36  remove names (i.e., \c char*), but does not provide means for manipulating
37  stored names.
38 
39  Names in a NameSet may be accessed via numbers from 0 through num()-1
40  and via \ref soplex::DataKey "DataKeys". See DataSet for a description of
41  these concepts.
42 
43  At a time a NameSet can hold a maximum of max() entries. This can be
44  reset with method reMax(). If more than max() names are added to a
45  NameSet, it adjusts itself automatically to the required size. This
46  implies, that references to names within a NameSet may become invalid if
47  the NameSet is expanded.
48 
49  All names (i.e., the actual char strings) in a NameSet are stored in one
50  continuous memory block of size memMax(). At one time memSize() bytes of
51  it are used for actually saving names; the remaining memory is free to hold
52  additional names. memRemax() can be used to reset memMax() but not lower
53  than to memSize(). Method memPack() performs a garbage collection to
54  gain free memory resulting from removed names.
55 
56  @warning Since the keys the NameSet uses to reference the strings are
57  generated internally, it is extremly important that the calls
58  to DataSet from within NameSet are synchronous to any calls
59  outside to DataSet, such as in row or column adding.
60 */
61 class NameSet
62 {
63 public:
64 
65  /**@brief Handles of names in a NameSet.
66  * @ingroup Elementary
67  *
68  * Class Name provides the handles (i.e., char*s) of names in a
69  * NameSet.
70  */
71  class Name
72  {
73  private:
74 
75  //------------------------------
76  /**@name Private Data */
77  //@{
78  static const char deflt; ///< default zero string.
79  //@}
80 
81  public:
82 
83  //------------------------------
84  /**@name Public Data */
85  //@{
86  const char* name; ///< pointer to the name string.
87  //@}
88 
89  //------------------------------
90  /**@name Friends */
91  //@{
92  /// equality operator.
93  friend int operator==(const Name& n1, const Name& n2)
94  {
95  return (strcmp(n1.name, n2.name) == 0);
96  }
97  /// output operator.
98  friend std::ostream& operator<<(std::ostream& out, const Name& n)
99  {
100  return out << n.name;
101  }
102  //@}
103 
104  //------------------------------
105  /**@name Debugging */
106  //@{
107  /// consistency check.
108  bool isConsistent() const
109  {
110  return (name != 0);
111  }
112  //@}
113 
114  //------------------------------------
115  /**@name Constructors / destructors */
116  //@{
117  /// default constructor.
119  : name(&deflt)
120  {}
121  /// copy constructor.
122  /** Only the pointer to the name is copied, but not the name itself.
123  */
124  Name(const Name& str)
125  : name(str.name)
126  {}
127  /// implictly constructs a Name out of a C style character string.
128  Name(const char* str)
129  : name(str)
130  {}
131  //@}
132  };
133 
134 private:
135 
136  //------------------------------
137  /**@name Data */
138  //@{
139  DataSet < int > set; ///< name set.
140  char* mem; ///< string memory
141  int memmax; ///< size of string memory
142  int memused; ///< size of used string memory
143  /** Every name in a NameSet is assigned a DataKey by which it can be
144  accessed (see NameSet::operator[]()). See DataKey for a more
145  detailed description of the concept of Keys.
146  */
147  DataHashTable < Name, DataKey > hashtab; ///< hashtable for names
148  //@}
149 
150 public:
151 
152  //------------------------------
153  /**@name Inquiry */
154  //@{
155  /// returns \p num 'th name of NameSet.
156  const char* operator[](int pnum) const
157  {
158  return &mem[set[pnum]];
159  }
160 
161  /// returns name for DataKey \p pkey of NameSet.
162  const char* operator[](const DataKey& pkey) const
163  {
164  return &mem[set[pkey]];
165  }
166 
167  /// returns nr. of names in NameSet.
168  int num() const
169  {
170  return set.num();
171  }
172 
173  /// returns maximum nr. of names that fit into NameSet.
174  int max() const
175  {
176  return set.max();
177  }
178 
179  /// returns maximum DataKey::idx used in NameSet.
180  int size() const
181  {
182  return set.size();
183  }
184 
185  /// returns maximum length of string memory.
186  int memMax() const
187  {
188  return memmax;
189  }
190 
191  /// returns used length of string memory.
192  int memSize() const
193  {
194  return memused;
195  }
196 
197  /// returns DataKey of the \p pnum 'th name in NameSet.
198  DataKey key(int pnum) const
199  {
200  return set.key(pnum);
201  }
202 
203  /// returns DataKey of name \p str in NameSet.
204  DataKey key(const char* str) const
205  {
206  const Name nam(str);
207  const DataKey* result = hashtab.get(nam);
208  return result == 0 ? DataKey() : *hashtab.get(nam);
209  }
210 
211  /// returns number of name with DataKey \p pkey in NameSet.
212  int number(const DataKey& pkey) const
213  {
214  return set.number(pkey);
215  }
216 
217  /// returns number of name \p str in NameSet.
218  int number(const char* str) const
219  {
220  const Name nam(str);
221 
222  if(hashtab.has(nam))
223  {
224  assert(hashtab.get(nam) != 0);
225  return number(*hashtab.get(nam));
226  }
227  else
228  return -1;
229  }
230 
231  /// does NameSet has a name with number \p pnum?
232  bool has(int pnum) const
233  {
234  return set.has(pnum);
235  }
236 
237  /// does NameSet has a name \p str?
238  bool has(const char* str) const
239  {
240  const Name nam(str);
241  return hashtab.has(nam);
242  }
243 
244  /// does NameSet has a name with DataKey \p pkey?
245  bool has(const DataKey& pkey) const
246  {
247  return set.has(pkey);
248  }
249  //@}
250 
251  //----------------------------
252  /**@name Extension */
253  //@{
254  ///
255  void add(const char* str);
256  /// adds name \p str to NameSet.
257  void add(DataKey& key, const char* str);
258 
259  ///
260  void add(const NameSet& set);
261  /// adds all names in \p set to NameSet.
262  void add(DataKey key[], const NameSet& nset);
263  //@}
264 
265 
266  //----------------------------
267  /**@name Shrinking */
268  //@{
269  /// removes name with DataKey \p key from NameSet.
270  void remove(const DataKey& key);
271 
272  /// removes \p pnum 'th name from NameSet.
273  void remove(int pnum)
274  {
275  remove(key(pnum));
276  }
277 
278  /// removes name \p str from NameSet.
279  void remove(const char* str);
280 
281  /// removes \p n names with DataKeys \p keys from NameSet.
282  void remove(const DataKey keys[], int n);
283 
284  /// removes \p n names with numbers \p nums from NameSet.
285  void remove(const int nums[], int n);
286 
287  /// remove all entries where \p dstat is less than zero.
288  void remove(int dstat[]);
289 
290  /// removes all names from NameSet.
291  void clear();
292  //@}
293 
294 
295  //----------------------------
296  /**@name Memory Control */
297  //@{
298  /// resets max() to \p newmax.
299  void reMax(int newmax = 0);
300 
301  /// resets memMax() to \p newmax.
302  void memRemax(int newmax = 0);
303 
304  /// garbage collection.
305  void memPack();
306  //@}
307 
308 
309  //----------------------------
310  /**@name Control Parameters */
311  //@{
312  /// memory extension factor for entries.
313  /** When more than max() names are added to a NameSet, it is
314  automatically resized to fit the additional names. Parameter
315  \p factor is the factor by which the element memory is extended to do
316  so.
317  */
319 
320  /// memory extension factor for names.
321  /** When the names added to a NameSet do no longer fit into the name
322  memory (i.e. the memory for saving the strings), it is automatically
323  resized to fit the additional names. Parameter \p memFactor is the
324  factor by which this memory is extended to do so.
325  */
327  //@}
328 
329  //----------------------------
330  /**@name Miscellaneous */
331  //@{
332  /// consistency check.
333  bool isConsistent() const;
334  //@}
335 
336  //--------------------------------------
337  /**@name Constructors / Destructors */
338  //@{
339  /// default constructor.
340  /** @param max start value for max()
341  * @param mmax start value for memMax()
342  * @param fac start value for #factor
343  * @param memFac start value for #memFactor
344  */
345  explicit
346  NameSet(int max = 10000,
347  int mmax = -1,
348  Real fac = 2,
349  Real memFac = 2);
350 
351  /// destructor.
352  ~NameSet();
353  //@}
354 
355 private:
356 
357  //--------------------------------------
358  /**@name Blocked */
359  //@{
360  /// copy constructor.
361  NameSet(const NameSet& old);
362 
363  /// assignment operator.
364  NameSet& operator=(const NameSet& rhs);
365  //@}
366 };
367 
368 extern std::ostream& operator<<(std::ostream& s, const NameSet& nset);
369 
370 } // namespace soplex
371 #endif // _NAMESET_H_
int memSize() const
returns used length of string memory.
Definition: nameset.h:192
Generic hash table for data objects.
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
Entry identifier class for items of a DataSet.
DataHashTable< Name, DataKey > hashtab
hashtable for names
Definition: nameset.h:147
void reMax(int newmax=0)
resets max() to newmax.
Definition: nameset.cpp:144
bool isConsistent() const
consistency check.
Definition: nameset.h:108
DataKey key(const char *str) const
returns DataKey of name str in NameSet.
Definition: nameset.h:204
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:207
Name(const char *str)
implictly constructs a Name out of a C style character string.
Definition: nameset.h:128
Set of data objects.
friend int operator==(const Name &n1, const Name &n2)
equality operator.
Definition: nameset.h:93
const char * operator[](int pnum) const
returns num &#39;th name of NameSet.
Definition: nameset.h:156
double Real
Definition: spxdefines.h:218
Name()
default constructor.
Definition: nameset.h:118
bool has(const char *str) const
does NameSet has a name str?
Definition: nameset.h:238
Name(const Name &str)
copy constructor.
Definition: nameset.h:124
int number(const char *str) const
returns number of name str in NameSet.
Definition: nameset.h:218
Real factor
memory extension factor for entries.
Definition: nameset.h:318
DataKey key(int pnum) const
returns DataKey of the pnum &#39;th name in NameSet.
Definition: nameset.h:198
const char * operator[](const DataKey &pkey) const
returns name for DataKey pkey of NameSet.
Definition: nameset.h:162
Generic hash table for data objects.Class DataHashTable provides a generic hash table for Data Object...
Definition: datahashtable.h:76
void add(const char *str)
Definition: nameset.cpp:25
void clear()
removes all names from NameSet.
Definition: nameset.cpp:137
NameSet & operator=(const NameSet &rhs)
assignment operator.
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
Everything should be within this namespace.
int memused
size of used string memory
Definition: nameset.h:142
bool has(const DataKey &pkey) const
does NameSet has a name with DataKey pkey?
Definition: nameset.h:245
void memRemax(int newmax=0)
resets memMax() to newmax.
Definition: nameset.cpp:150
void memPack()
garbage collection.
Definition: nameset.cpp:161
Real memFactor
memory extension factor for names.
Definition: nameset.h:326
bool has(const HashItem &h) const
Is item h present in DataHashTable?
const Info * get(const HashItem &h) const
returns const pointer to Info of HashItem h or 0, if item is not found.
friend std::ostream & operator<<(std::ostream &out, const Name &n)
output operator.
Definition: nameset.h:98
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
int number(const DataKey &pkey) const
returns number of name with DataKey pkey in NameSet.
Definition: nameset.h:212
~NameSet()
destructor.
Definition: nameset.cpp:219
bool has(int pnum) const
does NameSet has a name with number pnum?
Definition: nameset.h:232
int memMax() const
returns maximum length of string memory.
Definition: nameset.h:186