CoinUtils  2.11.9
CoinModelUseful.hpp
Go to the documentation of this file.
1 /* $Id$ */
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 
29 class CoinModelLink {
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 {
165  CoinBigIndex index, next;
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:
214  symrec *symtable;
215  char *symbuf;
216  int length;
217  double unsetValue;
218 };
219 
220 class CoinModelHash {
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_;
293  int numberItems_;
295  int maximumItems_;
297  int lastSlot_;
299 };
301 class CoinModelHash2 {
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 };
366 class CoinModelLinkedList {
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:
503  int numberMajor_;
505  int maximumMajor_;
511  int type_;
513 };
514 
515 #endif
516 
517 /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
518 */
CoinModelHash
Definition: CoinModelUseful.hpp:220
CoinModelTriple
for linked lists
Definition: CoinModelUseful.hpp:130
func_t
double(* func_t)(double)
Definition: CoinModelUseful.hpp:169
CoinModelLinkedList::maximumElements
CoinBigIndex maximumElements() const
Maximum number of elements.
Definition: CoinModelUseful.hpp:413
CoinModelLinkedList::~CoinModelLinkedList
~CoinModelLinkedList()
Destructor.
CoinModelLinkedList::maximumMajor
int maximumMajor() const
Maximum number of major items i.e. rows if just row links.
Definition: CoinModelUseful.hpp:403
CoinModelHash::~CoinModelHash
~CoinModelHash()
Destructor.
CoinModelLinkedList::next_
CoinBigIndex * next_
Next - maximumElements long.
Definition: CoinModelUseful.hpp:497
CoinModelHash::maximumItems_
int maximumItems_
Maximum number of items.
Definition: CoinModelUseful.hpp:295
CoinModelLinkedList::lastFree
CoinBigIndex lastFree() const
Last on free chain.
Definition: CoinModelUseful.hpp:423
CoinModelHash::hash
int hash(const char *name) const
Returns index or -1.
CoinModelLinkedList
Definition: CoinModelUseful.hpp:366
CoinModelLinkedList::previous_
CoinBigIndex * previous_
Previous - maximumElements long.
Definition: CoinModelUseful.hpp:495
CoinModelLinkedList::previous
const CoinBigIndex * previous() const
Previous array.
Definition: CoinModelUseful.hpp:443
CoinModelLinkedList::maximumElements_
CoinBigIndex maximumElements_
Maximum number of elements.
Definition: CoinModelUseful.hpp:509
symrec::var
double var
Definition: CoinModelUseful.hpp:177
CoinModelLinkedList::deleteRowOne
void deleteRowOne(CoinBigIndex position, CoinModelTriple *triples, CoinModelHash2 &hash)
Deletes one element from Row list.
CoinModelHash2::numberItems_
CoinBigIndex numberItems_
Number of items.
Definition: CoinModelUseful.hpp:359
CoinModelHash::CoinModelHash
CoinModelHash()
Default constructor.
setRowInTriple
void setRowInTriple(CoinModelTriple &triple, int iRow)
Definition: CoinModelUseful.hpp:142
CoinModelLinkedList::numberMajor
int numberMajor() const
Number of major items i.e. rows if just row links.
Definition: CoinModelUseful.hpp:398
CoinModelLinkedList::updateDeletedOne
void updateDeletedOne(CoinBigIndex position, const CoinModelTriple *triples)
Update column list for one element when one element deleted from row copy.
CoinModelLinkedList::maximumMajor_
int maximumMajor_
Maximum number of major items i.e. rows if just row links.
Definition: CoinModelUseful.hpp:505
CoinModelHash::names
const char *const * names() const
Names.
Definition: CoinModelUseful.hpp:256
CoinModelHash2::hash_
CoinModelHashLink2 * hash_
hash
Definition: CoinModelUseful.hpp:357
CoinYacc::symbuf
char * symbuf
Definition: CoinModelUseful.hpp:215
CoinModelHash::setName
void setName(int which, char *name)
Sets name at position (does not create)
CoinModelHash2::addHash
void addHash(CoinBigIndex index, int row, int column, const CoinModelTriple *triples)
Adds to hash.
CoinYacc::operator=
CoinYacc & operator=(const CoinYacc &rhs)
symrec
For string evaluation.
Definition: CoinModelUseful.hpp:173
CoinModelLinkedList::operator=
CoinModelLinkedList & operator=(const CoinModelLinkedList &)
=
CoinModelLinkedList::numberElements_
CoinBigIndex numberElements_
Number of elements.
Definition: CoinModelUseful.hpp:507
CoinModelHash2::numberItems
CoinBigIndex numberItems() const
Number of items.
Definition: CoinModelUseful.hpp:325
CoinModelHash::name
const char * name(int which) const
Returns name at position (or NULL)
CoinModelHash::hashValue
int hashValue(const char *name) const
Returns a hash value.
CoinModelHash::setNumberItems
void setNumberItems(int number)
Set number of items.
CoinModelHash2::setNumberItems
void setNumberItems(CoinBigIndex number)
Set number of items.
symrec::value
union symrec::@0 value
CoinYacc::unsetValue
double unsetValue
Definition: CoinModelUseful.hpp:217
CoinModelHash2
For int,int hashing.
Definition: CoinModelUseful.hpp:301
setStringInTriple
void setStringInTriple(CoinModelTriple &triple, bool string)
Definition: CoinModelUseful.hpp:150
CoinModelHash2::hashValue
CoinBigIndex hashValue(int row, int column) const
Returns a hash value.
CoinYacc::symtable
symrec * symtable
Definition: CoinModelUseful.hpp:214
CoinModelLinkedList::firstFree
CoinBigIndex firstFree() const
First on free chain.
Definition: CoinModelUseful.hpp:418
CoinModelLinkedList::last
CoinBigIndex last(int which) const
Last on chain.
Definition: CoinModelUseful.hpp:433
CoinModelHash2::~CoinModelHash2
~CoinModelHash2()
Destructor.
CoinModelHash::validateHash
void validateHash() const
Validates.
CoinModelLinkedList::next
const CoinBigIndex * next() const
Next array.
Definition: CoinModelUseful.hpp:438
CoinModelLinkedList::addEasy
CoinBigIndex addEasy(int majorIndex, CoinBigIndex numberOfElements, const int *indices, const double *elements, CoinModelTriple *triples, CoinModelHash2 &hash)
Adds to list - easy case i.e.
symrec::name
char * name
Definition: CoinModelUseful.hpp:174
CoinModelHash::getName
char * getName(int which) const
Returns non const name at position (or NULL)
CoinModelHash2::resize
void resize(CoinBigIndex maxItems, const CoinModelTriple *triples, bool forceReHash=false)
Resize hash (also re-hashs)
CoinModelHash::resize
void resize(int maxItems, bool forceReHash=false)
Resize hash (also re-hashs)
CoinModelHash2::maximumItems_
CoinBigIndex maximumItems_
Maximum number of items.
Definition: CoinModelUseful.hpp:361
CoinModelHash::hash_
CoinModelHashLink * hash_
hash
Definition: CoinModelUseful.hpp:291
symrec::next
struct symrec * next
Definition: CoinModelUseful.hpp:180
CoinModelLinkedList::numberMajor_
int numberMajor_
Number of major items i.e. rows if just row links.
Definition: CoinModelUseful.hpp:503
CoinBigIndex
int CoinBigIndex
Definition: Coin_C_defines.h:136
CoinTypes.hpp
CoinModelLinkedList::updateDeleted
void updateDeleted(int which, CoinModelTriple *triples, CoinModelLinkedList &otherList)
Deletes from list - other case i.e.
CoinModelLinkedList::validateLinks
void validateLinks(const CoinModelTriple *triples) const
Checks that links are consistent.
CoinModelHashLink2
Definition: CoinModelUseful.hpp:164
CoinYacc::CoinYacc
CoinYacc()
Definition: CoinModelUseful.hpp:191
CoinModelHash::addHash
void addHash(int index, const char *name)
Adds to hash.
CoinModelLinkedList::CoinModelLinkedList
CoinModelLinkedList()
Default constructor.
CoinModelLinkedList::resize
void resize(int maxMajor, CoinBigIndex maxElements)
Resize list - for row list maxMajor is maximum rows.
CoinModelHash2::lastSlot_
CoinBigIndex lastSlot_
Last slot looked at.
Definition: CoinModelUseful.hpp:363
CoinModelLinkedList::fill
void fill(int first, int last)
Fills first,last with -1.
CoinModelHash2::CoinModelHash2
CoinModelHash2()
Default constructor.
CoinYacc
Definition: CoinModelUseful.hpp:185
CoinModelLinkedList::first_
CoinBigIndex * first_
First - maximumMajor+1 long (last free element chain)
Definition: CoinModelUseful.hpp:499
CoinModelLinkedList::create
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.
CoinModelLinkedList::addHard
void addHard(int minorIndex, CoinBigIndex numberOfElements, const int *indices, const double *elements, CoinModelTriple *triples, CoinModelHash2 &hash)
Adds to list - hard case i.e.
CoinModelLinkedList::first
CoinBigIndex first(int which) const
First on chain.
Definition: CoinModelUseful.hpp:428
CoinModelLinkedList::last_
CoinBigIndex * last_
Last - maximumMajor+1 long (last free element chain)
Definition: CoinModelUseful.hpp:501
CoinModelLinkedList::numberElements
CoinBigIndex numberElements() const
Number of elements.
Definition: CoinModelUseful.hpp:408
CoinModelHash2::deleteHash
void deleteHash(CoinBigIndex index, int row, int column)
Deletes from hash.
CoinYacc::~CoinYacc
~CoinYacc()
Definition: CoinModelUseful.hpp:198
CoinModelHash::operator=
CoinModelHash & operator=(const CoinModelHash &)
=
symrec::fnctptr
func_t fnctptr
Definition: CoinModelUseful.hpp:178
CoinModelHash2::maximumItems
CoinBigIndex maximumItems() const
Maximum number of items.
Definition: CoinModelUseful.hpp:332
CoinModelHash::deleteHash
void deleteHash(int index)
Deletes from hash.
CoinModelHash::numberItems_
int numberItems_
Number of items.
Definition: CoinModelUseful.hpp:293
setRowAndStringInTriple
void setRowAndStringInTriple(CoinModelTriple &triple, int iRow, bool string)
Definition: CoinModelUseful.hpp:154
stringInTriple
bool stringInTriple(const CoinModelTriple &triple)
Definition: CoinModelUseful.hpp:146
symrec::type
int type
Definition: CoinModelUseful.hpp:175
CoinModelHash::maximumItems
int maximumItems() const
Maximum number of items.
Definition: CoinModelUseful.hpp:251
CoinPragma.hpp
CoinModelLinkedList::synchronize
void synchronize(CoinModelLinkedList &other)
Puts in free list from other list.
CoinModelHash2::hash
CoinBigIndex hash(int row, int column, const CoinModelTriple *triples) const
Returns index or -1.
CoinYacc::length
int length
Definition: CoinModelUseful.hpp:216
rowInTriple
int rowInTriple(const CoinModelTriple &triple)
Definition: CoinModelUseful.hpp:138
CoinModelHash::lastSlot_
int lastSlot_
Last slot looked at.
Definition: CoinModelUseful.hpp:297
CoinModelHash::names_
char ** names_
Names.
Definition: CoinModelUseful.hpp:289
CoinModelHash::numberItems
int numberItems() const
Number of items i.e. rows if just row names.
Definition: CoinModelUseful.hpp:244
CoinModelLinkedList::deleteSame
void deleteSame(int which, CoinModelTriple *triples, CoinModelHash2 &hash, bool zapTriples)
Deletes from list - same case i.e.
CoinModelHash2::operator=
CoinModelHash2 & operator=(const CoinModelHash2 &)
=
CoinModelLinkedList::type_
int type_
0 row list, 1 column list
Definition: CoinModelUseful.hpp:511
CoinModelTriple::row
unsigned int row
Definition: CoinModelUseful.hpp:133