Cbc  2.10.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CoinModelUseful.hpp
Go to the documentation of this file.
1 /* $Id: CoinModelUseful.hpp 2083 2019-01-06 19:38:09Z unxusr $ */
2 // Copyright (C) 2005, International Business Machines
3 // Corporation and others. All Rights Reserved.
4 // This code is licensed under the terms of the Eclipse Public License (EPL).
5 
6 #ifndef CoinModelUseful_H
7 #define CoinModelUseful_H
8 
9 #include <cstdlib>
10 #include <cmath>
11 #include <cassert>
12 #include <cfloat>
13 #include <cstring>
14 #include <cstdio>
15 #include <iostream>
16 
17 #include "CoinTypes.hpp"
18 #include "CoinPragma.hpp"
19 
27 
30 
31 public:
35  CoinModelLink();
39 
47 
50  inline int row() const
52  {
53  return row_;
54  }
56  inline int column() const
57  {
58  return column_;
59  }
61  inline double value() const
62  {
63  return value_;
64  }
66  inline double element() const
67  {
68  return value_;
69  }
71  inline CoinBigIndex position() const
72  {
73  return position_;
74  }
76  inline bool onRow() const
77  {
78  return onRow_;
79  }
81  inline void setRow(int row)
82  {
83  row_ = row;
84  }
86  inline void setColumn(int column)
87  {
88  column_ = column;
89  }
91  inline void setValue(double value)
92  {
93  value_ = value;
94  }
96  inline void setElement(double value)
97  {
98  value_ = value;
99  }
102  {
104  }
106  inline void setOnRow(bool onRow)
107  {
108  onRow_ = onRow;
109  }
111 
112 private:
115  int row_;
118  int column_;
120  double value_;
124  bool onRow_;
126 };
127 
129 // for specifying triple
130 typedef struct {
131  // top bit is nonzero if string
132  // rest is row
133  unsigned int row;
134  //CoinModelRowIndex row;
135  int column;
136  double value; // If string then index into strings
138 inline int rowInTriple(const CoinModelTriple &triple)
139 {
140  return triple.row & 0x7fffffff;
141 }
142 inline void setRowInTriple(CoinModelTriple &triple, int iRow)
143 {
144  triple.row = iRow | (triple.row & 0x80000000);
145 }
146 inline bool stringInTriple(const CoinModelTriple &triple)
147 {
148  return (triple.row & 0x80000000) != 0;
149 }
150 inline void setStringInTriple(CoinModelTriple &triple, bool string)
151 {
152  triple.row = (string ? 0x80000000 : 0) | (triple.row & 0x7fffffff);
153 }
155  int iRow, bool string)
156 {
157  triple.row = (string ? 0x80000000 : 0) | iRow;
158 }
160 // for hashing
161 typedef struct {
162  int index, next;
164 typedef struct {
167 
168 /* Function type. */
169 typedef double (*func_t)(double);
170 
172 /* Data type for links in the chain of symbols. */
173 struct symrec {
174  char *name; /* name of symbol */
175  int type; /* type of symbol: either VAR or FNCT */
176  union {
177  double var; /* value of a VAR */
178  func_t fnctptr; /* value of a FNCT */
179  } value;
180  struct symrec *next; /* link field */
181 };
182 
183 typedef struct symrec symrec;
184 
185 class CoinYacc {
186 private:
187  CoinYacc(const CoinYacc &rhs);
188  CoinYacc &operator=(const CoinYacc &rhs);
189 
190 public:
192  : symtable(NULL)
193  , symbuf(NULL)
194  , length(0)
195  , unsetValue(0)
196  {
197  }
199  {
200  if (length) {
201  free(symbuf);
202  symbuf = NULL;
203  }
204  symrec *s = symtable;
205  while (s) {
206  free(s->name);
207  symtable = s;
208  s = s->next;
209  free(symtable);
210  }
211  }
212 
213 public:
215  char *symbuf;
216  int length;
217  double unsetValue;
218 };
219 
221 
222 public:
226  CoinModelHash();
228  ~CoinModelHash();
230 
234  CoinModelHash(const CoinModelHash &);
238 
241  void resize(int maxItems, bool forceReHash = false);
244  inline int numberItems() const
245  {
246  return numberItems_;
247  }
249  void setNumberItems(int number);
251  inline int maximumItems() const
252  {
253  return maximumItems_;
254  }
256  inline const char *const *names() const
257  {
258  return names_;
259  }
261 
264  int hash(const char *name) const;
267  void addHash(int index, const char *name);
269  void deleteHash(int index);
271  const char *name(int which) const;
273  char *getName(int which) const;
275  void setName(int which, char *name);
277  void validateHash() const;
278 
279 private:
281  int hashValue(const char *name) const;
282 
283 public:
285 private:
288  char **names_;
299 };
302 
303 public:
307  CoinModelHash2();
309  ~CoinModelHash2();
311 
319 
322  void resize(CoinBigIndex maxItems, const CoinModelTriple *triples, bool forceReHash = false);
325  inline CoinBigIndex numberItems() const
326  {
327  return numberItems_;
328  }
330  void setNumberItems(CoinBigIndex number);
332  inline CoinBigIndex maximumItems() const
333  {
334  return maximumItems_;
335  }
337 
340  CoinBigIndex hash(int row, int column, const CoinModelTriple *triples) const;
343  void addHash(CoinBigIndex index, int row, int column, const CoinModelTriple *triples);
345  void deleteHash(CoinBigIndex index, int row, int column);
346 
347 private:
349  CoinBigIndex hashValue(int row, int column) const;
350 
351 public:
353 private:
365 };
367 
368 public:
376 
384 
389  void resize(int maxMajor, CoinBigIndex maxElements);
393  void create(int maxMajor, CoinBigIndex maxElements,
394  int numberMajor, int numberMinor,
395  int type,
398  inline int numberMajor() const
399  {
400  return numberMajor_;
401  }
403  inline int maximumMajor() const
404  {
405  return maximumMajor_;
406  }
409  {
410  return numberElements_;
411  }
414  {
415  return maximumElements_;
416  }
418  inline CoinBigIndex firstFree() const
419  {
420  return first_[maximumMajor_];
421  }
423  inline CoinBigIndex lastFree() const
424  {
425  return last_[maximumMajor_];
426  }
428  inline CoinBigIndex first(int which) const
429  {
430  return first_[which];
431  }
433  inline CoinBigIndex last(int which) const
434  {
435  return last_[which];
436  }
438  inline const CoinBigIndex *next() const
439  {
440  return next_;
441  }
443  inline const CoinBigIndex *previous() const
444  {
445  return previous_;
446  }
448 
454  CoinBigIndex addEasy(int majorIndex, CoinBigIndex numberOfElements, const int *indices,
455  const double *elements, CoinModelTriple *triples,
456  CoinModelHash2 &hash);
459  void addHard(int minorIndex, CoinBigIndex numberOfElements, const int *indices,
460  const double *elements, CoinModelTriple *triples,
461  CoinModelHash2 &hash);
465  void addHard(CoinBigIndex first, const CoinModelTriple *triples,
469  void deleteSame(int which, CoinModelTriple *triples,
470  CoinModelHash2 &hash, bool zapTriples);
474  void updateDeleted(int which, CoinModelTriple *triples,
475  CoinModelLinkedList &otherList);
478  void deleteRowOne(CoinBigIndex position, CoinModelTriple *triples,
479  CoinModelHash2 &hash);
483  void updateDeletedOne(CoinBigIndex position, const CoinModelTriple *triples);
485  void fill(int first, int last);
487  void synchronize(CoinModelLinkedList &other);
489  void validateLinks(const CoinModelTriple *triples) const;
491 private:
511  int type_;
513 };
514 
515 #endif
516 
517 /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
518 */
void resize(CoinBigIndex maxItems, const CoinModelTriple *triples, bool forceReHash=false)
Resize hash (also re-hashs)
CoinBigIndex * previous_
Previous - maximumElements long.
int numberMajor_
Number of major items i.e. rows if just row links.
CoinBigIndex hashValue(int row, int column) const
Returns a hash value.
char ** names_
Names.
CoinBigIndex maximumItems_
Maximum number of items.
CoinBigIndex firstFree() const
First on free chain.
CoinModelHash & operator=(const CoinModelHash &)
=
int rowInTriple(const CoinModelTriple &triple)
int maximumMajor_
Maximum number of major items i.e. rows if just row links.
double(* func_t)(double)
void addHard(int minorIndex, CoinBigIndex numberOfElements, const int *indices, const double *elements, CoinModelTriple *triples, CoinModelHash2 &hash)
Adds to list - hard case i.e.
const CoinBigIndex * previous() const
Previous array.
CoinBigIndex maximumElements() const
Maximum number of elements.
void resize(int maxItems, bool forceReHash=false)
Resize hash (also re-hashs)
CoinBigIndex numberItems() const
Number of items.
void setRowAndStringInTriple(CoinModelTriple &triple, int iRow, bool string)
CoinBigIndex * last_
Last - maximumMajor+1 long (last free element chain)
int maximumItems() const
Maximum number of items.
void deleteHash(int index)
Deletes from hash.
int hashValue(const char *name) const
Returns a hash value.
CoinBigIndex * next_
Next - maximumElements long.
int numberMajor() const
Number of major items i.e. rows if just row links.
void updateDeleted(int which, CoinModelTriple *triples, CoinModelLinkedList &otherList)
Deletes from list - other case i.e.
CoinBigIndex addEasy(int majorIndex, CoinBigIndex numberOfElements, const int *indices, const double *elements, CoinModelTriple *triples, CoinModelHash2 &hash)
Adds to list - easy case i.e.
void deleteRowOne(CoinBigIndex position, CoinModelTriple *triples, CoinModelHash2 &hash)
Deletes one element from Row list.
CoinBigIndex lastSlot_
Last slot looked at.
void deleteHash(CoinBigIndex index, int row, int column)
Deletes from hash.
void fill(int first, int last)
Fills first,last with -1.
void synchronize(CoinModelLinkedList &other)
Puts in free list from other list.
For int,int hashing.
void updateDeletedOne(CoinBigIndex position, const CoinModelTriple *triples)
Update column list for one element when one element deleted from row copy.
CoinBigIndex maximumItems() const
Maximum number of items.
CoinBigIndex numberElements_
Number of elements.
CoinBigIndex lastFree() const
Last on free chain.
double unsetValue
int type_
0 row list, 1 column list
CoinModelHashLink2 * hash_
hash
~CoinModelLinkedList()
Destructor.
CoinModelHashLink * hash_
hash
void setStringInTriple(CoinModelTriple &triple, bool string)
const CoinBigIndex * next() const
Next array.
struct symrec * next
const char *const * names() const
Names.
CoinModelLinkedList()
Default constructor.
void setNumberItems(int number)
Set number of items.
CoinBigIndex first(int which) const
First on chain.
For string evaluation.
CoinModelHash()
Default constructor.
for linked lists
CoinBigIndex * first_
First - maximumMajor+1 long (last free element chain)
void validateLinks(const CoinModelTriple *triples) const
Checks that links are consistent.
int numberItems_
Number of items.
CoinBigIndex last(int which) const
Last on chain.
void create(int maxMajor, CoinBigIndex maxElements, int numberMajor, int numberMinor, int type, CoinBigIndex numberElements, const CoinModelTriple *triples)
Create list - for row list maxMajor is maximum rows.
CoinBigIndex numberElements() const
Number of elements.
const char * name(int which) const
Returns name at position (or NULL)
~CoinModelHash()
Destructor.
union symrec::@0 value
void addHash(CoinBigIndex index, int row, int column, const CoinModelTriple *triples)
Adds to hash.
bool stringInTriple(const CoinModelTriple &triple)
CoinBigIndex hash(int row, int column, const CoinModelTriple *triples) const
Returns index or -1.
int lastSlot_
Last slot looked at.
int maximumMajor() const
Maximum number of major items i.e. rows if just row links.
int hash(const char *name) const
Returns index or -1.
int CoinBigIndex
void resize(int maxMajor, CoinBigIndex maxElements)
Resize list - for row list maxMajor is maximum rows.
int maximumItems_
Maximum number of items.
int numberItems() const
Number of items i.e. rows if just row names.
CoinBigIndex maximumElements_
Maximum number of elements.
void setName(int which, char *name)
Sets name at position (does not create)
void setRowInTriple(CoinModelTriple &triple, int iRow)
CoinYacc & operator=(const CoinYacc &rhs)
void validateHash() const
Validates.
void deleteSame(int which, CoinModelTriple *triples, CoinModelHash2 &hash, bool zapTriples)
Deletes from list - same case i.e.
char * getName(int which) const
Returns non const name at position (or NULL)
symrec * symtable
CoinModelHash2 & operator=(const CoinModelHash2 &)
=
void setNumberItems(CoinBigIndex number)
Set number of items.
func_t fnctptr
CoinModelHash2()
Default constructor.
CoinModelLinkedList & operator=(const CoinModelLinkedList &)
=
void addHash(int index, const char *name)
Adds to hash.
CoinBigIndex numberItems_
Number of items.
~CoinModelHash2()
Destructor.