CoinModelUseful.hpp
Go to the documentation of this file.
1 /* $Id: CoinModelUseful.hpp 1215 2009-11-05 11:03:04Z forrest $ */
2 // Copyright (C) 2005, International Business Machines
3 // Corporation and others. All Rights Reserved.
4 #ifndef CoinModelUseful_H
5 #define CoinModelUseful_H
6 
7 
8 #include <cmath>
9 #include <cassert>
10 #include <cfloat>
11 #include <cstring>
12 #include <cstdio>
13 #include <iostream>
14 
15 
16 #include "CoinPragma.hpp"
17 #include "CoinFinite.hpp"
18 
26 
29 
30 public:
34  CoinModelLink();
38 
46 
49  inline int row() const
51  { return row_;}
53  inline int column() const
54  { return column_;}
56  inline double value() const
57  { return value_;}
59  inline double element() const
60  { return value_;}
62  inline int position() const
63  { return position_;}
65  inline bool onRow() const
66  { return onRow_;}
68  inline void setRow(int row)
69  { row_=row;}
71  inline void setColumn(int column)
72  { column_=column;}
74  inline void setValue(double value)
75  { value_=value;}
77  inline void setElement(double value)
78  { value_=value;}
80  inline void setPosition(int position)
83  inline void setOnRow(bool onRow)
84  { onRow_=onRow;}
86 
87 private:
90  int row_;
93  int column_;
95  double value_;
97  int position_;
99  bool onRow_;
101 };
102 
104 // for specifying triple
105 typedef struct {
106  // top bit is nonzero if string
107  // rest is row
108  unsigned int row;
109  //CoinModelRowIndex row;
110  int column;
111  double value; // If string then index into strings
113 inline int rowInTriple(const CoinModelTriple & triple)
114 { return triple.row&0x7fffffff;}
115 inline void setRowInTriple(CoinModelTriple & triple,int iRow)
116 { triple.row = iRow|(triple.row&0x80000000);}
117 inline bool stringInTriple(const CoinModelTriple & triple)
118 { return (triple.row&0x80000000)!=0;}
119 inline void setStringInTriple(CoinModelTriple & triple,bool string)
120 { triple.row = (string ? 0x80000000 : 0)|(triple.row&0x7fffffff);}
122  int iRow,bool string)
123 { triple.row = (string ? 0x80000000 : 0)|iRow;}
125 // for hashing
126 typedef struct {
127  int index, next;
129 
130 /* Function type. */
131 typedef double (*func_t) (double);
132 
134 /* Data type for links in the chain of symbols. */
135 struct symrec
136 {
137  char *name; /* name of symbol */
138  int type; /* type of symbol: either VAR or FNCT */
139  union
140  {
141  double var; /* value of a VAR */
142  func_t fnctptr; /* value of a FNCT */
143  } value;
144  struct symrec *next; /* link field */
145 };
146 
147 typedef struct symrec symrec;
148 
149 class CoinYacc {
150 private:
151  CoinYacc(const CoinYacc& rhs);
152  CoinYacc& operator=(const CoinYacc& rhs);
153 
154 public:
155  CoinYacc() : symtable(NULL), symbuf(NULL), length(0), unsetValue(0) {}
157  {
158  if (length) {
159  free(symbuf);
160  symbuf = NULL;
161  }
162  symrec* s = symtable;
163  while (s) {
164  free(s->name);
165  symtable = s;
166  s = s->next;
167  free(symtable);
168  }
169  }
170 
171 public:
173  char * symbuf;
174  int length;
175  double unsetValue;
176 };
177 
179 
180 public:
184  CoinModelHash();
186  ~CoinModelHash();
188 
196 
199  void resize(int maxItems,bool forceReHash=false);
202  inline int numberItems() const
203  { return numberItems_;}
205  void setNumberItems(int number);
207  inline int maximumItems() const
208  { return maximumItems_;}
210  inline const char *const * names() const
211  { return names_;}
213 
216  int hash(const char * name) const;
219  void addHash(int index, const char * name);
221  void deleteHash(int index);
223  const char * name(int which) const;
225  char * getName(int which) const;
227  void setName(int which,char * name ) ;
229  void validateHash() const;
230 private:
232  int hashValue(const char * name) const;
233 public:
235 private:
238  char ** names_;
249 };
252 
253 public:
257  CoinModelHash2();
259  ~CoinModelHash2();
261 
269 
272  void resize(int maxItems, const CoinModelTriple * triples,bool forceReHash=false);
275  inline int numberItems() const
276  { return numberItems_;}
278  void setNumberItems(int number);
280  inline int maximumItems() const
281  { return maximumItems_;}
283 
286  int hash(int row, int column, const CoinModelTriple * triples) const;
289  void addHash(int index, int row, int column, const CoinModelTriple * triples);
291  void deleteHash(int index, int row, int column);
292 private:
294  int hashValue(int row, int column) const;
295 public:
297 private:
309 };
311 
312 public:
320 
328 
333  void resize(int maxMajor,int maxElements);
337  void create(int maxMajor,int maxElements,
338  int numberMajor, int numberMinor,
339  int type,
340  int numberElements, const CoinModelTriple * triples);
342  inline int numberMajor() const
343  { return numberMajor_;}
345  inline int maximumMajor() const
346  { return maximumMajor_;}
348  inline int numberElements() const
349  { return numberElements_;}
351  inline int maximumElements() const
352  { return maximumElements_;}
354  inline int firstFree() const
355  { return first_[maximumMajor_];}
357  inline int lastFree() const
358  { return last_[maximumMajor_];}
360  inline int first(int which) const
361  { return first_[which];}
363  inline int last(int which) const
364  { return last_[which];}
366  inline const int * next() const
367  { return next_;}
369  inline const int * previous() const
370  { return previous_;}
372 
378  int addEasy(int majorIndex, int numberOfElements, const int * indices,
379  const double * elements, CoinModelTriple * triples,
380  CoinModelHash2 & hash);
383  void addHard(int minorIndex, int numberOfElements, const int * indices,
384  const double * elements, CoinModelTriple * triples,
385  CoinModelHash2 & hash);
389  void addHard(int first, const CoinModelTriple * triples,
390  int firstFree, int lastFree,const int * nextOther);
393  void deleteSame(int which, CoinModelTriple * triples,
394  CoinModelHash2 & hash, bool zapTriples);
398  void updateDeleted(int which, CoinModelTriple * triples,
399  CoinModelLinkedList & otherList);
402  void deleteRowOne(int position, CoinModelTriple * triples,
403  CoinModelHash2 & hash);
407  void updateDeletedOne(int position, const CoinModelTriple * triples);
409  void fill(int first,int last);
411  void synchronize(CoinModelLinkedList & other);
413  void validateLinks(const CoinModelTriple * triples) const;
415 private:
418  int * previous_;
421  int * next_;
423  int * first_;
425  int * last_;
435  int type_;
437 };
438 
439 #endif
void setStringInTriple(CoinModelTriple &triple, bool string)
void validateHash() const
Validates.
int numberElements_
Number of elements.
int * previous_
Previous - maximumElements long.
int numberMajor() const
Number of major items i.e. rows if just row links.
int maximumElements_
Maximum number of elements.
int maximumItems_
Maximum number of items.
int hash(int row, int column, const CoinModelTriple *triples) const
Returns index or -1.
void addHash(int index, const char *name)
Adds to hash.
int numberItems() const
Number of items i.e. rows if just row names.
CoinModelLinkedList()
Default constructor.
char * getName(int which) const
Returns non const name at position (or NULL)
const int * next() const
Next array.
void updateDeleted(int which, CoinModelTriple *triples, CoinModelLinkedList &otherList)
Deletes from list - other case i.e.
int lastSlot_
Last slot looked at.
void resize(int maxItems, const CoinModelTriple *triples, bool forceReHash=false)
Resize hash (also re-hashs)
int * next_
Next - maximumElements long.
CoinModelLinkedList & operator=(const CoinModelLinkedList &)
=
int numberElements() const
Number of elements.
int maximumElements() const
Maximum number of elements.
void deleteRowOne(int position, CoinModelTriple *triples, CoinModelHash2 &hash)
Deletes one element from Row list.
CoinModelHashLink * hash_
hash
bool stringInTriple(const CoinModelTriple &triple)
int maximumItems() const
Maximum number of items.
int maximumMajor_
Maximum number of major items i.e. rows if just row links.
void resize(int maxItems, bool forceReHash=false)
Resize hash (also re-hashs)
void setRowAndStringInTriple(CoinModelTriple &triple, int iRow, bool string)
CoinModelHash & operator=(const CoinModelHash &)
=
char ** names_
Names.
int firstFree() const
First on free chain.
void deleteSame(int which, CoinModelTriple *triples, CoinModelHash2 &hash, bool zapTriples)
Deletes from list - same case i.e.
double(* func_t)(double)
int type_
0 row list, 1 column list
const char * name(int which) const
Returns name at position (or NULL)
int hash(const char *name) const
Returns index or -1.
struct symrec * next
~CoinModelHash2()
Destructor.
~CoinModelHash()
Destructor.
void resize(int maxMajor, int maxElements)
Resize list - for row list maxMajor is maximum rows.
void validateLinks(const CoinModelTriple *triples) const
Checks that links are consistent.
int addEasy(int majorIndex, int numberOfElements, const int *indices, const double *elements, CoinModelTriple *triples, CoinModelHash2 &hash)
Adds to list - easy case i.e.
void deleteHash(int index, int row, int column)
Deletes from hash.
int * last_
Last - maximumMajor+1 long (last free element chain)
For string evaluation.
void setNumberItems(int number)
Set number of items.
int * first_
First - maximumMajor+1 long (last free element chain)
~CoinModelLinkedList()
Destructor.
int first(int which) const
First on chain.
void synchronize(CoinModelLinkedList &other)
Puts in free list from other list.
int numberItems() const
Number of items.
void setRowInTriple(CoinModelTriple &triple, int iRow)
int hashValue(const char *name) const
Returns a hash value.
double unsetValue
void deleteHash(int index)
Deletes from hash.
int numberItems_
Number of items.
CoinYacc & operator=(const CoinYacc &rhs)
int maximumItems() const
Maximum number of items.
union symrec::@0 value
int numberMajor_
Number of major items i.e. rows if just row links.
void updateDeletedOne(int position, const CoinModelTriple *triples)
Update column list for one element when one element deleted from row copy.
CoinModelHash()
Default constructor.
int numberItems_
Number of items.
CoinModelHashLink * hash_
hash
void addHash(int index, int row, int column, const CoinModelTriple *triples)
Adds to hash.
CoinModelHash2()
Default constructor.
const int * previous() const
Previous array.
int lastSlot_
Last slot looked at.
void create(int maxMajor, int maxElements, int numberMajor, int numberMinor, int type, int numberElements, const CoinModelTriple *triples)
Create list - for row list maxMajor is maximum rows.
int maximumMajor() const
Maximum number of major items i.e. rows if just row links.
CoinModelHash2 & operator=(const CoinModelHash2 &)
=
int hashValue(int row, int column) const
Returns a hash value.
int last(int which) const
Last on chain.
void fill(int first, int last)
Fills first,last with -1.
void setNumberItems(int number)
Set number of items.
symrec * symtable
const char *const * names() const
Names.
int lastFree() const
Last on free chain.
void addHard(int minorIndex, int numberOfElements, const int *indices, const double *elements, CoinModelTriple *triples, CoinModelHash2 &hash)
Adds to list - hard case i.e.
For int,int hashing.
func_t fnctptr
int rowInTriple(const CoinModelTriple &triple)
for linked lists
void setName(int which, char *name)
Sets name at position (does not create)
int maximumItems_
Maximum number of items.