Cbc  2.9.9
 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 1416 2011-04-17 09:57:29Z stefan $ */
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 
10 #include <cstdlib>
11 #include <cmath>
12 #include <cassert>
13 #include <cfloat>
14 #include <cstring>
15 #include <cstdio>
16 #include <iostream>
17 
18 
19 #include "CoinPragma.hpp"
20 
28 
31 
32 public:
36  CoinModelLink();
40 
48 
51  inline int row() const
53  { return row_;}
55  inline int column() const
56  { return column_;}
58  inline double value() const
59  { return value_;}
61  inline double element() const
62  { return value_;}
64  inline int position() const
65  { return position_;}
67  inline bool onRow() const
68  { return onRow_;}
70  inline void setRow(int row)
71  { row_=row;}
73  inline void setColumn(int column)
74  { column_=column;}
76  inline void setValue(double value)
77  { value_=value;}
79  inline void setElement(double value)
80  { value_=value;}
82  inline void setPosition(int position)
85  inline void setOnRow(bool onRow)
86  { onRow_=onRow;}
88 
89 private:
92  int row_;
95  int column_;
97  double value_;
99  int position_;
101  bool onRow_;
103 };
104 
106 // for specifying triple
107 typedef struct {
108  // top bit is nonzero if string
109  // rest is row
110  unsigned int row;
111  //CoinModelRowIndex row;
112  int column;
113  double value; // If string then index into strings
115 inline int rowInTriple(const CoinModelTriple & triple)
116 { return triple.row&0x7fffffff;}
117 inline void setRowInTriple(CoinModelTriple & triple,int iRow)
118 { triple.row = iRow|(triple.row&0x80000000);}
119 inline bool stringInTriple(const CoinModelTriple & triple)
120 { return (triple.row&0x80000000)!=0;}
121 inline void setStringInTriple(CoinModelTriple & triple,bool string)
122 { triple.row = (string ? 0x80000000 : 0)|(triple.row&0x7fffffff);}
124  int iRow,bool string)
125 { triple.row = (string ? 0x80000000 : 0)|iRow;}
127 // for hashing
128 typedef struct {
129  int index, next;
131 
132 /* Function type. */
133 typedef double (*func_t) (double);
134 
136 /* Data type for links in the chain of symbols. */
137 struct symrec
138 {
139  char *name; /* name of symbol */
140  int type; /* type of symbol: either VAR or FNCT */
141  union
142  {
143  double var; /* value of a VAR */
144  func_t fnctptr; /* value of a FNCT */
145  } value;
146  struct symrec *next; /* link field */
147 };
148 
149 typedef struct symrec symrec;
150 
151 class CoinYacc {
152 private:
153  CoinYacc(const CoinYacc& rhs);
154  CoinYacc& operator=(const CoinYacc& rhs);
155 
156 public:
157  CoinYacc() : symtable(NULL), symbuf(NULL), length(0), unsetValue(0) {}
159  {
160  if (length) {
161  free(symbuf);
162  symbuf = NULL;
163  }
164  symrec* s = symtable;
165  while (s) {
166  free(s->name);
167  symtable = s;
168  s = s->next;
169  free(symtable);
170  }
171  }
172 
173 public:
175  char * symbuf;
176  int length;
177  double unsetValue;
178 };
179 
181 
182 public:
186  CoinModelHash();
188  ~CoinModelHash();
190 
198 
201  void resize(int maxItems,bool forceReHash=false);
204  inline int numberItems() const
205  { return numberItems_;}
207  void setNumberItems(int number);
209  inline int maximumItems() const
210  { return maximumItems_;}
212  inline const char *const * names() const
213  { return names_;}
215 
218  int hash(const char * name) const;
221  void addHash(int index, const char * name);
223  void deleteHash(int index);
225  const char * name(int which) const;
227  char * getName(int which) const;
229  void setName(int which,char * name ) ;
231  void validateHash() const;
232 private:
234  int hashValue(const char * name) const;
235 public:
237 private:
240  char ** names_;
251 };
254 
255 public:
259  CoinModelHash2();
261  ~CoinModelHash2();
263 
271 
274  void resize(int maxItems, const CoinModelTriple * triples,bool forceReHash=false);
277  inline int numberItems() const
278  { return numberItems_;}
280  void setNumberItems(int number);
282  inline int maximumItems() const
283  { return maximumItems_;}
285 
288  int hash(int row, int column, const CoinModelTriple * triples) const;
291  void addHash(int index, int row, int column, const CoinModelTriple * triples);
293  void deleteHash(int index, int row, int column);
294 private:
296  int hashValue(int row, int column) const;
297 public:
299 private:
311 };
313 
314 public:
322 
330 
335  void resize(int maxMajor,int maxElements);
339  void create(int maxMajor,int maxElements,
340  int numberMajor, int numberMinor,
341  int type,
342  int numberElements, const CoinModelTriple * triples);
344  inline int numberMajor() const
345  { return numberMajor_;}
347  inline int maximumMajor() const
348  { return maximumMajor_;}
350  inline int numberElements() const
351  { return numberElements_;}
353  inline int maximumElements() const
354  { return maximumElements_;}
356  inline int firstFree() const
357  { return first_[maximumMajor_];}
359  inline int lastFree() const
360  { return last_[maximumMajor_];}
362  inline int first(int which) const
363  { return first_[which];}
365  inline int last(int which) const
366  { return last_[which];}
368  inline const int * next() const
369  { return next_;}
371  inline const int * previous() const
372  { return previous_;}
374 
380  int addEasy(int majorIndex, int numberOfElements, const int * indices,
381  const double * elements, CoinModelTriple * triples,
382  CoinModelHash2 & hash);
385  void addHard(int minorIndex, int numberOfElements, const int * indices,
386  const double * elements, CoinModelTriple * triples,
387  CoinModelHash2 & hash);
391  void addHard(int first, const CoinModelTriple * triples,
392  int firstFree, int lastFree,const int * nextOther);
395  void deleteSame(int which, CoinModelTriple * triples,
396  CoinModelHash2 & hash, bool zapTriples);
400  void updateDeleted(int which, CoinModelTriple * triples,
401  CoinModelLinkedList & otherList);
404  void deleteRowOne(int position, CoinModelTriple * triples,
405  CoinModelHash2 & hash);
409  void updateDeletedOne(int position, const CoinModelTriple * triples);
411  void fill(int first,int last);
413  void synchronize(CoinModelLinkedList & other);
415  void validateLinks(const CoinModelTriple * triples) const;
417 private:
420  int * previous_;
423  int * next_;
425  int * first_;
427  int * last_;
437  int type_;
439 };
440 
441 #endif
void setNumberItems(int number)
Set number of items.
int numberMajor_
Number of major items i.e. rows if just row links.
char ** names_
Names.
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 resize(int maxItems, bool forceReHash=false)
Resize hash (also re-hashs)
int lastSlot_
Last slot looked at.
void setRowAndStringInTriple(CoinModelTriple &triple, int iRow, bool string)
int numberItems() const
Number of items.
void resize(int maxItems, const CoinModelTriple *triples, bool forceReHash=false)
Resize hash (also re-hashs)
int maximumItems() const
Maximum number of items.
void deleteHash(int index)
Deletes from hash.
int hashValue(const char *name) const
Returns a hash value.
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.
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.
int * next_
Next - maximumElements long.
int addEasy(int majorIndex, int numberOfElements, const int *indices, const double *elements, CoinModelTriple *triples, CoinModelHash2 &hash)
Adds to list - easy case i.e.
void deleteRowOne(int position, CoinModelTriple *triples, CoinModelHash2 &hash)
Deletes one element from Row list.
double unsetValue
int type_
0 row list, 1 column list
void addHash(int index, int row, int column, const CoinModelTriple *triples)
Adds to hash.
int numberElements_
Number of elements.
~CoinModelLinkedList()
Destructor.
CoinModelHashLink * hash_
hash
int hash(int row, int column, const CoinModelTriple *triples) const
Returns index or -1.
void setStringInTriple(CoinModelTriple &triple, bool string)
struct symrec * next
int maximumElements() const
Maximum number of elements.
int first(int which) const
First on chain.
const char *const * names() const
Names.
CoinModelLinkedList()
Default constructor.
int last(int which) const
Last on chain.
void updateDeletedOne(int position, const CoinModelTriple *triples)
Update column list for one element when one element deleted from row copy.
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 lastFree() const
Last on free chain.
void setNumberItems(int number)
Set number of items.
For string evaluation.
CoinModelHash()
Default constructor.
for linked lists
int maximumElements_
Maximum number of elements.
void validateLinks(const CoinModelTriple *triples) const
Checks that links are consistent.
int numberItems_
Number of items.
int hashValue(int row, int column) const
Returns a hash value.
CoinModelHashLink * hash_
hash
const char * name(int which) const
Returns name at position (or NULL)
const int * next() const
Next array.
~CoinModelHash()
Destructor.
union symrec::@0 value
int numberElements() const
Number of elements.
bool stringInTriple(const CoinModelTriple &triple)
int maximumItems_
Maximum number of items.
int lastSlot_
Last slot looked at.
int * last_
Last - maximumMajor+1 long (last free element chain)
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 maximumItems_
Maximum number of items.
int numberItems() const
Number of items i.e. rows if just row names.
int * first_
First - maximumMajor+1 long (last free element chain)
const int * previous() const
Previous array.
void setName(int which, char *name)
Sets name at position (does not create)
void setRowInTriple(CoinModelTriple &triple, int iRow)
CoinYacc & operator=(const CoinYacc &rhs)
int * previous_
Previous - maximumElements long.
void validateHash() const
Validates.
void addHard(int minorIndex, int numberOfElements, const int *indices, const double *elements, CoinModelTriple *triples, CoinModelHash2 &hash)
Adds to list - hard case i.e.
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 &)
=
func_t fnctptr
int numberItems_
Number of items.
CoinModelHash2()
Default constructor.
CoinModelLinkedList & operator=(const CoinModelLinkedList &)
=
void addHash(int index, const char *name)
Adds to hash.
int maximumItems() const
Maximum number of items.
void deleteHash(int index, int row, int column)
Deletes from hash.
int firstFree() const
First on free chain.
~CoinModelHash2()
Destructor.
void resize(int maxMajor, int maxElements)
Resize list - for row list maxMajor is maximum rows.