CoinUtils
trunk
|
00001 /* $Id$ */ 00002 // Copyright (C) 2005, International Business Machines 00003 // Corporation and others. All Rights Reserved. 00004 // This code is licensed under the terms of the Eclipse Public License (EPL). 00005 00006 #ifndef CoinModelUseful_H 00007 #define CoinModelUseful_H 00008 00009 00010 #include <cstdlib> 00011 #include <cmath> 00012 #include <cassert> 00013 #include <cfloat> 00014 #include <cstring> 00015 #include <cstdio> 00016 #include <iostream> 00017 00018 00019 #include "CoinPragma.hpp" 00020 00028 00029 00030 class CoinModelLink { 00031 00032 public: 00036 CoinModelLink(); 00038 ~CoinModelLink(); 00040 00044 CoinModelLink(const CoinModelLink&); 00046 CoinModelLink& operator=(const CoinModelLink&); 00048 00051 00052 inline int row() const 00053 { return row_;} 00055 inline int column() const 00056 { return column_;} 00058 inline double value() const 00059 { return value_;} 00061 inline double element() const 00062 { return value_;} 00064 inline int position() const 00065 { return position_;} 00067 inline bool onRow() const 00068 { return onRow_;} 00070 inline void setRow(int row) 00071 { row_=row;} 00073 inline void setColumn(int column) 00074 { column_=column;} 00076 inline void setValue(double value) 00077 { value_=value;} 00079 inline void setElement(double value) 00080 { value_=value;} 00082 inline void setPosition(int position) 00083 { position_=position;} 00085 inline void setOnRow(bool onRow) 00086 { onRow_=onRow;} 00088 00089 private: 00092 00093 int row_; 00095 int column_; 00097 double value_; 00099 int position_; 00101 bool onRow_; 00103 }; 00104 00106 // for specifying triple 00107 typedef struct { 00108 // top bit is nonzero if string 00109 // rest is row 00110 unsigned int row; 00111 //CoinModelRowIndex row; 00112 int column; 00113 double value; // If string then index into strings 00114 } CoinModelTriple; 00115 inline int rowInTriple(const CoinModelTriple & triple) 00116 { return triple.row&0x7fffffff;} 00117 inline void setRowInTriple(CoinModelTriple & triple,int iRow) 00118 { triple.row = iRow|(triple.row&0x80000000);} 00119 inline bool stringInTriple(const CoinModelTriple & triple) 00120 { return (triple.row&0x80000000)!=0;} 00121 inline void setStringInTriple(CoinModelTriple & triple,bool string) 00122 { triple.row = (string ? 0x80000000 : 0)|(triple.row&0x7fffffff);} 00123 inline void setRowAndStringInTriple(CoinModelTriple & triple, 00124 int iRow,bool string) 00125 { triple.row = (string ? 0x80000000 : 0)|iRow;} 00127 // for hashing 00128 typedef struct { 00129 int index, next; 00130 } CoinModelHashLink; 00131 00132 /* Function type. */ 00133 typedef double (*func_t) (double); 00134 00136 /* Data type for links in the chain of symbols. */ 00137 struct symrec 00138 { 00139 char *name; /* name of symbol */ 00140 int type; /* type of symbol: either VAR or FNCT */ 00141 union 00142 { 00143 double var; /* value of a VAR */ 00144 func_t fnctptr; /* value of a FNCT */ 00145 } value; 00146 struct symrec *next; /* link field */ 00147 }; 00148 00149 typedef struct symrec symrec; 00150 00151 class CoinYacc { 00152 private: 00153 CoinYacc(const CoinYacc& rhs); 00154 CoinYacc& operator=(const CoinYacc& rhs); 00155 00156 public: 00157 CoinYacc() : symtable(NULL), symbuf(NULL), length(0), unsetValue(0) {} 00158 ~CoinYacc() 00159 { 00160 if (length) { 00161 free(symbuf); 00162 symbuf = NULL; 00163 } 00164 symrec* s = symtable; 00165 while (s) { 00166 free(s->name); 00167 symtable = s; 00168 s = s->next; 00169 free(symtable); 00170 } 00171 } 00172 00173 public: 00174 symrec * symtable; 00175 char * symbuf; 00176 int length; 00177 double unsetValue; 00178 }; 00179 00180 class CoinModelHash { 00181 00182 public: 00186 CoinModelHash(); 00188 ~CoinModelHash(); 00190 00194 CoinModelHash(const CoinModelHash&); 00196 CoinModelHash& operator=(const CoinModelHash&); 00198 00201 00202 void resize(int maxItems,bool forceReHash=false); 00204 inline int numberItems() const 00205 { return numberItems_;} 00207 void setNumberItems(int number); 00209 inline int maximumItems() const 00210 { return maximumItems_;} 00212 inline const char *const * names() const 00213 { return names_;} 00215 00218 00219 int hash(const char * name) const; 00221 void addHash(int index, const char * name); 00223 void deleteHash(int index); 00225 const char * name(int which) const; 00227 char * getName(int which) const; 00229 void setName(int which,char * name ) ; 00231 void validateHash() const; 00232 private: 00234 int hashValue(const char * name) const; 00235 public: 00237 private: 00240 00241 char ** names_; 00243 CoinModelHashLink * hash_; 00245 int numberItems_; 00247 int maximumItems_; 00249 int lastSlot_; 00251 }; 00253 class CoinModelHash2 { 00254 00255 public: 00259 CoinModelHash2(); 00261 ~CoinModelHash2(); 00263 00267 CoinModelHash2(const CoinModelHash2&); 00269 CoinModelHash2& operator=(const CoinModelHash2&); 00271 00274 00275 void resize(int maxItems, const CoinModelTriple * triples,bool forceReHash=false); 00277 inline int numberItems() const 00278 { return numberItems_;} 00280 void setNumberItems(int number); 00282 inline int maximumItems() const 00283 { return maximumItems_;} 00285 00288 00289 int hash(int row, int column, const CoinModelTriple * triples) const; 00291 void addHash(int index, int row, int column, const CoinModelTriple * triples); 00293 void deleteHash(int index, int row, int column); 00294 private: 00296 int hashValue(int row, int column) const; 00297 public: 00299 private: 00302 00303 CoinModelHashLink * hash_; 00305 int numberItems_; 00307 int maximumItems_; 00309 int lastSlot_; 00311 }; 00312 class CoinModelLinkedList { 00313 00314 public: 00318 CoinModelLinkedList(); 00320 ~CoinModelLinkedList(); 00322 00326 CoinModelLinkedList(const CoinModelLinkedList&); 00328 CoinModelLinkedList& operator=(const CoinModelLinkedList&); 00330 00335 void resize(int maxMajor,int maxElements); 00339 void create(int maxMajor,int maxElements, 00340 int numberMajor, int numberMinor, 00341 int type, 00342 int numberElements, const CoinModelTriple * triples); 00344 inline int numberMajor() const 00345 { return numberMajor_;} 00347 inline int maximumMajor() const 00348 { return maximumMajor_;} 00350 inline int numberElements() const 00351 { return numberElements_;} 00353 inline int maximumElements() const 00354 { return maximumElements_;} 00356 inline int firstFree() const 00357 { return first_[maximumMajor_];} 00359 inline int lastFree() const 00360 { return last_[maximumMajor_];} 00362 inline int first(int which) const 00363 { return first_[which];} 00365 inline int last(int which) const 00366 { return last_[which];} 00368 inline const int * next() const 00369 { return next_;} 00371 inline const int * previous() const 00372 { return previous_;} 00374 00380 int addEasy(int majorIndex, int numberOfElements, const int * indices, 00381 const double * elements, CoinModelTriple * triples, 00382 CoinModelHash2 & hash); 00385 void addHard(int minorIndex, int numberOfElements, const int * indices, 00386 const double * elements, CoinModelTriple * triples, 00387 CoinModelHash2 & hash); 00391 void addHard(int first, const CoinModelTriple * triples, 00392 int firstFree, int lastFree,const int * nextOther); 00395 void deleteSame(int which, CoinModelTriple * triples, 00396 CoinModelHash2 & hash, bool zapTriples); 00400 void updateDeleted(int which, CoinModelTriple * triples, 00401 CoinModelLinkedList & otherList); 00404 void deleteRowOne(int position, CoinModelTriple * triples, 00405 CoinModelHash2 & hash); 00409 void updateDeletedOne(int position, const CoinModelTriple * triples); 00411 void fill(int first,int last); 00413 void synchronize(CoinModelLinkedList & other); 00415 void validateLinks(const CoinModelTriple * triples) const; 00417 private: 00420 00421 int * previous_; 00423 int * next_; 00425 int * first_; 00427 int * last_; 00429 int numberMajor_; 00431 int maximumMajor_; 00433 int numberElements_; 00435 int maximumElements_; 00437 int type_; 00439 }; 00440 00441 #endif