CoinUtils  2.10.14
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:
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);
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:
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:
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);
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
CoinModelHash
Definition: CoinModelUseful.hpp:180
setRowAndStringInTriple
void setRowAndStringInTriple(CoinModelTriple &triple, int iRow, bool string)
Definition: CoinModelUseful.hpp:123
CoinModelLinkedList::updateDeletedOne
void updateDeletedOne(int position, const CoinModelTriple *triples)
Update column list for one element when one element deleted from row copy.
CoinModelTriple
for linked lists
Definition: CoinModelUseful.hpp:107
CoinModelHash2::maximumItems_
int maximumItems_
Maximum number of items.
Definition: CoinModelUseful.hpp:307
CoinModelLinkedList::first_
int * first_
First - maximumMajor+1 long (last free element chain)
Definition: CoinModelUseful.hpp:425
func_t
double(* func_t)(double)
Definition: CoinModelUseful.hpp:133
CoinModelLinkedList::~CoinModelLinkedList
~CoinModelLinkedList()
Destructor.
CoinModelLinkedList::maximumMajor
int maximumMajor() const
Maximum number of major items i.e. rows if just row links.
Definition: CoinModelUseful.hpp:347
CoinModelHash::hash_
CoinModelHashLink * hash_
hash
Definition: CoinModelUseful.hpp:243
CoinModelHash::~CoinModelHash
~CoinModelHash()
Destructor.
setStringInTriple
void setStringInTriple(CoinModelTriple &triple, bool string)
Definition: CoinModelUseful.hpp:121
CoinModelHash::maximumItems_
int maximumItems_
Maximum number of items.
Definition: CoinModelUseful.hpp:247
CoinModelHash::hash
int hash(const char *name) const
Returns index or -1.
CoinModelLinkedList::CoinModelLinkedList
CoinModelLinkedList(const CoinModelLinkedList &)
The copy constructor.
CoinModelLinkedList
Definition: CoinModelUseful.hpp:312
CoinModelHash2::hash_
CoinModelHashLink * hash_
hash
Definition: CoinModelUseful.hpp:303
symrec::var
double var
Definition: CoinModelUseful.hpp:143
CoinModelLinkedList::deleteRowOne
void deleteRowOne(int position, CoinModelTriple *triples, CoinModelHash2 &hash)
Deletes one element from Row list.
CoinModelLinkedList::numberElements
int numberElements() const
Number of elements.
Definition: CoinModelUseful.hpp:350
CoinModelHash2::numberItems
int numberItems() const
Number of items.
Definition: CoinModelUseful.hpp:277
CoinModelHash::CoinModelHash
CoinModelHash(const CoinModelHash &)
The copy constructor.
CoinModelHash::CoinModelHash
CoinModelHash()
Default constructor.
CoinModelLinkedList::addHard
void addHard(int minorIndex, int numberOfElements, const int *indices, const double *elements, CoinModelTriple *triples, CoinModelHash2 &hash)
Adds to list - hard case i.e.
CoinModelLinkedList::numberMajor
int numberMajor() const
Number of major items i.e. rows if just row links.
Definition: CoinModelUseful.hpp:344
CoinModelLinkedList::maximumMajor_
int maximumMajor_
Maximum number of major items i.e. rows if just row links.
Definition: CoinModelUseful.hpp:431
CoinModelHash::setName
void setName(int which, char *name)
Sets name at position (does not create)
CoinYacc::symtable
symrec * symtable
Definition: CoinModelUseful.hpp:174
CoinYacc::operator=
CoinYacc & operator=(const CoinYacc &rhs)
CoinModelHash2::numberItems_
int numberItems_
Number of items.
Definition: CoinModelUseful.hpp:305
symrec
For string evaluation.
Definition: CoinModelUseful.hpp:138
CoinModelHash2::resize
void resize(int maxItems, const CoinModelTriple *triples, bool forceReHash=false)
Resize hash (also re-hashs)
CoinYacc::CoinYacc
CoinYacc(const CoinYacc &rhs)
stringInTriple
bool stringInTriple(const CoinModelTriple &triple)
Definition: CoinModelUseful.hpp:119
CoinModelLinkedList::operator=
CoinModelLinkedList & operator=(const CoinModelLinkedList &)
=
CoinModelHash::names_
char ** names_
Names.
Definition: CoinModelUseful.hpp:241
setRowInTriple
void setRowInTriple(CoinModelTriple &triple, int iRow)
Definition: CoinModelUseful.hpp:117
CoinPragma.hpp
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.
CoinModelLinkedList::next_
int * next_
Next - maximumElements long.
Definition: CoinModelUseful.hpp:423
CoinModelTriple::column
int column
Definition: CoinModelUseful.hpp:112
CoinModelHash2::maximumItems
int maximumItems() const
Maximum number of items.
Definition: CoinModelUseful.hpp:282
CoinModelHash::names
const char *const * names() const
Names.
Definition: CoinModelUseful.hpp:212
CoinModelLinkedList::next
const int * next() const
Next array.
Definition: CoinModelUseful.hpp:368
symrec::value
union symrec::@0 value
CoinModelLinkedList::maximumElements
int maximumElements() const
Maximum number of elements.
Definition: CoinModelUseful.hpp:353
CoinModelLinkedList::addHard
void addHard(int first, const CoinModelTriple *triples, int firstFree, int lastFree, const int *nextOther)
Adds to list - hard case i.e.
CoinYacc::unsetValue
double unsetValue
Definition: CoinModelUseful.hpp:177
CoinModelHash2
For int,int hashing.
Definition: CoinModelUseful.hpp:253
CoinModelLinkedList::maximumElements_
int maximumElements_
Maximum number of elements.
Definition: CoinModelUseful.hpp:435
symrec::next
struct symrec * next
Definition: CoinModelUseful.hpp:146
CoinModelHash2::~CoinModelHash2
~CoinModelHash2()
Destructor.
CoinModelLinkedList::previous_
int * previous_
Previous - maximumElements long.
Definition: CoinModelUseful.hpp:421
CoinModelHash::validateHash
void validateHash() const
Validates.
CoinYacc::symbuf
char * symbuf
Definition: CoinModelUseful.hpp:175
CoinModelTriple::value
double value
Definition: CoinModelUseful.hpp:113
CoinModelHash::getName
char * getName(int which) const
Returns non const name at position (or NULL)
CoinModelHash::resize
void resize(int maxItems, bool forceReHash=false)
Resize hash (also re-hashs)
CoinModelHash2::setNumberItems
void setNumberItems(int number)
Set number of items.
CoinModelLinkedList::numberMajor_
int numberMajor_
Number of major items i.e. rows if just row links.
Definition: CoinModelUseful.hpp:429
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.
CoinYacc::CoinYacc
CoinYacc()
Definition: CoinModelUseful.hpp:157
CoinModelHash::addHash
void addHash(int index, const char *name)
Adds to hash.
CoinModelLinkedList::previous
const int * previous() const
Previous array.
Definition: CoinModelUseful.hpp:371
CoinModelHash2::deleteHash
void deleteHash(int index, int row, int column)
Deletes from hash.
CoinModelLinkedList::CoinModelLinkedList
CoinModelLinkedList()
Default constructor.
CoinModelHash2::addHash
void addHash(int index, int row, int column, const CoinModelTriple *triples)
Adds to hash.
CoinModelLinkedList::fill
void fill(int first, int last)
Fills first,last with -1.
CoinModelHash2::CoinModelHash2
CoinModelHash2()
Default constructor.
CoinYacc
Definition: CoinModelUseful.hpp:151
CoinYacc::~CoinYacc
~CoinYacc()
Definition: CoinModelUseful.hpp:158
CoinModelHash2::lastSlot_
int lastSlot_
Last slot looked at.
Definition: CoinModelUseful.hpp:309
CoinModelHash::operator=
CoinModelHash & operator=(const CoinModelHash &)
=
symrec::fnctptr
func_t fnctptr
Definition: CoinModelUseful.hpp:144
CoinModelLinkedList::last
int last(int which) const
Last on chain.
Definition: CoinModelUseful.hpp:365
CoinModelLinkedList::lastFree
int lastFree() const
Last on free chain.
Definition: CoinModelUseful.hpp:359
CoinModelHash::deleteHash
void deleteHash(int index)
Deletes from hash.
CoinModelLinkedList::first
int first(int which) const
First on chain.
Definition: CoinModelUseful.hpp:362
CoinModelHash::numberItems_
int numberItems_
Number of items.
Definition: CoinModelUseful.hpp:245
CoinModelLinkedList::numberElements_
int numberElements_
Number of elements.
Definition: CoinModelUseful.hpp:433
symrec::type
int type
Definition: CoinModelUseful.hpp:140
CoinModelLinkedList::create
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.
CoinModelLinkedList::firstFree
int firstFree() const
First on free chain.
Definition: CoinModelUseful.hpp:356
CoinModelHash::maximumItems
int maximumItems() const
Maximum number of items.
Definition: CoinModelUseful.hpp:209
symrec::name
char * name
Definition: CoinModelUseful.hpp:139
CoinModelLinkedList::synchronize
void synchronize(CoinModelLinkedList &other)
Puts in free list from other list.
CoinYacc::length
int length
Definition: CoinModelUseful.hpp:176
rowInTriple
int rowInTriple(const CoinModelTriple &triple)
Definition: CoinModelUseful.hpp:115
CoinModelHash::lastSlot_
int lastSlot_
Last slot looked at.
Definition: CoinModelUseful.hpp:249
CoinModelLinkedList::resize
void resize(int maxMajor, int maxElements)
Resize list - for row list maxMajor is maximum rows.
CoinModelHash2::hash
int hash(int row, int column, const CoinModelTriple *triples) const
Returns index or -1.
CoinModelHash2::CoinModelHash2
CoinModelHash2(const CoinModelHash2 &)
The copy constructor.
CoinModelLinkedList::last_
int * last_
Last - maximumMajor+1 long (last free element chain)
Definition: CoinModelUseful.hpp:427
CoinModelHash::numberItems
int numberItems() const
Number of items i.e. rows if just row names.
Definition: CoinModelUseful.hpp:204
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:437
CoinModelLinkedList::addEasy
int addEasy(int majorIndex, int numberOfElements, const int *indices, const double *elements, CoinModelTriple *triples, CoinModelHash2 &hash)
Adds to list - easy case i.e.
CoinModelHash2::hashValue
int hashValue(int row, int column) const
Returns a hash value.
CoinModelTriple::row
unsigned int row
Definition: CoinModelUseful.hpp:110