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