CoinModelUseful.hpp 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
/* $Id$ */
// Copyright (C) 2005, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

#ifndef CoinModelUseful_H
#define CoinModelUseful_H

#include <cstdlib>
#include <cmath>
#include <cassert>
#include <cfloat>
#include <cstring>
#include <cstdio>
#include <iostream>

#include "CoinTypes.hpp"
#include "CoinPragma.hpp"

/**
   This is for various structures/classes needed by CoinModel.

   CoinModelLink
   CoinModelLinkedList
   CoinModelHash
*/
/// for going through row or column

class CoinModelLink {

public:
  /**@name Constructors, destructor */
  //@{
  /** Default constructor. */
  CoinModelLink();
  /** Destructor */
  ~CoinModelLink();
  //@}

  /**@name Copy method */
  //@{
  /** The copy constructor. */
  CoinModelLink(const CoinModelLink &);
  /// =
  CoinModelLink &operator=(const CoinModelLink &);
  //@}

  /**@name Sets and gets method */
  //@{
  /// Get row
  inline int row() const
  {
    return row_;
  }
  /// Get column
  inline int column() const
  {
    return column_;
  }
  /// Get value
  inline double value() const
  {
    return value_;
  }
  /// Get value
  inline double element() const
  {
    return value_;
  }
  /// Get position
  inline CoinBigIndex position() const
  {
    return position_;
  }
  /// Get onRow
  inline bool onRow() const
  {
    return onRow_;
  }
  /// Set row
  inline void setRow(int row)
  {
    row_ = row;
  }
  /// Set column
  inline void setColumn(int column)
  {
    column_ = column;
  }
  /// Set value
  inline void setValue(double value)
  {
    value_ = value;
  }
  /// Set value
  inline void setElement(double value)
  {
    value_ = value;
  }
  /// Set position
  inline void setPosition(CoinBigIndex position)
  {
    position_ = position;
  }
  /// Set onRow
  inline void setOnRow(bool onRow)
  {
    onRow_ = onRow;
  }
  //@}

private:
  /**@name Data members */
  //@{
  /// Row
  int row_;
  /// Column
  int column_;
  /// Value as double
  double value_;
  /// Position in data
  CoinBigIndex position_;
  /// If on row chain
  bool onRow_;
  //@}
};

/// for linked lists
// for specifying triple
typedef struct {
  // top bit is nonzero if string
  // rest is row
  unsigned int row;
  //CoinModelRowIndex row;
  int column;
  double value; // If string then index into strings
} CoinModelTriple;
inline int rowInTriple(const CoinModelTriple &triple)
{
  return triple.row & 0x7fffffff;
}
inline void setRowInTriple(CoinModelTriple &triple, int iRow)
{
  triple.row = iRow | (triple.row & 0x80000000);
}
inline bool stringInTriple(const CoinModelTriple &triple)
{
  return (triple.row & 0x80000000) != 0;
}
inline void setStringInTriple(CoinModelTriple &triple, bool string)
{
  triple.row = (string ? 0x80000000 : 0) | (triple.row & 0x7fffffff);
}
inline void setRowAndStringInTriple(CoinModelTriple &triple,
  int iRow, bool string)
{
  triple.row = (string ? 0x80000000 : 0) | iRow;
}
/// for names and hashing
// for hashing
typedef struct {
  int index, next;
} CoinModelHashLink;
typedef struct {
  CoinBigIndex index, next;
} CoinModelHashLink2;

/* Function type.  */
typedef double (*func_t)(double);

/// For string evaluation
/* Data type for links in the chain of symbols.  */
struct symrec {
  char *name; /* name of symbol */
  int type; /* type of symbol: either VAR or FNCT */
  union {
    double var; /* value of a VAR */
    func_t fnctptr; /* value of a FNCT */
  } value;
  struct symrec *next; /* link field */
};

typedef struct symrec symrec;

class CoinYacc {
private:
  CoinYacc(const CoinYacc &rhs);
  CoinYacc &operator=(const CoinYacc &rhs);

public:
  CoinYacc()
    : symtable(NULL)
    , symbuf(NULL)
    , length(0)
    , unsetValue(0)
  {
  }
  ~CoinYacc()
  {
    if (length) {
      free(symbuf);
      symbuf = NULL;
    }
    symrec *s = symtable;
    while (s) {
      free(s->name);
      symtable = s;
      s = s->next;
      free(symtable);
    }
  }

public:
  symrec *symtable;
  char *symbuf;
  int length;
  double unsetValue;
};

class CoinModelHash {

public:
  /**@name Constructors, destructor */
  //@{
  /** Default constructor. */
  CoinModelHash();
  /** Destructor */
  ~CoinModelHash();
  //@}

  /**@name Copy method */
  //@{
  /** The copy constructor. */
  CoinModelHash(const CoinModelHash &);
  /// =
  CoinModelHash &operator=(const CoinModelHash &);
  //@}

  /**@name sizing (just increases) */
  //@{
  /// Resize hash (also re-hashs)
  void resize(int maxItems, bool forceReHash = false);
  /// Number of items i.e. rows if just row names
  inline int numberItems() const
  {
    return numberItems_;
  }
  /// Set number of items
  void setNumberItems(int number);
  /// Maximum number of items
  inline int maximumItems() const
  {
    return maximumItems_;
  }
  /// Names
  inline const char *const *names() const
  {
    return names_;
  }
  //@}

  /**@name hashing */
  //@{
  /// Returns index or -1
  int hash(const char *name) const;
  /// Adds to hash
  void addHash(int index, const char *name);
  /// Deletes from hash
  void deleteHash(int index);
  /// Returns name at position (or NULL)
  const char *name(int which) const;
  /// Returns non const name at position (or NULL)
  char *getName(int which) const;
  /// Sets name at position (does not create)
  void setName(int which, char *name);
  /// Validates
  void validateHash() const;

private:
  /// Returns a hash value
  int hashValue(const char *name) const;

public:
  //@}
private:
  /**@name Data members */
  //@{
  /// Names
  char **names_;
  /// hash
  CoinModelHashLink *hash_;
  /// Number of items
  int numberItems_;
  /// Maximum number of items
  int maximumItems_;
  /// Last slot looked at
  int lastSlot_;
  //@}
};
/// For int,int hashing
class CoinModelHash2 {

public:
  /**@name Constructors, destructor */
  //@{
  /** Default constructor. */
  CoinModelHash2();
  /** Destructor */
  ~CoinModelHash2();
  //@}

  /**@name Copy method */
  //@{
  /** The copy constructor. */
  CoinModelHash2(const CoinModelHash2 &);
  /// =
  CoinModelHash2 &operator=(const CoinModelHash2 &);
  //@}

  /**@name sizing (just increases) */
  //@{
  /// Resize hash (also re-hashs)
  void resize(CoinBigIndex maxItems, const CoinModelTriple *triples, bool forceReHash = false);
  /// Number of items
  inline CoinBigIndex numberItems() const
  {
    return numberItems_;
  }
  /// Set number of items
  void setNumberItems(CoinBigIndex number);
  /// Maximum number of items
  inline CoinBigIndex maximumItems() const
  {
    return maximumItems_;
  }
  //@}

  /**@name hashing */
  //@{
  /// Returns index or -1
  CoinBigIndex hash(int row, int column, const CoinModelTriple *triples) const;
  /// Adds to hash
  void addHash(CoinBigIndex index, int row, int column, const CoinModelTriple *triples);
  /// Deletes from hash
  void deleteHash(CoinBigIndex index, int row, int column);

private:
  /// Returns a hash value
  CoinBigIndex hashValue(int row, int column) const;

public:
  //@}
private:
  /**@name Data members */
  //@{
  /// hash
  CoinModelHashLink2 *hash_;
  /// Number of items
  CoinBigIndex numberItems_;
  /// Maximum number of items
  CoinBigIndex maximumItems_;
  /// Last slot looked at
  CoinBigIndex lastSlot_;
  //@}
};
class CoinModelLinkedList {

public:
  /**@name Constructors, destructor */
  //@{
  /** Default constructor. */
  CoinModelLinkedList();
  /** Destructor */
  ~CoinModelLinkedList();
  //@}

  /**@name Copy method */
  //@{
  /** The copy constructor. */
  CoinModelLinkedList(const CoinModelLinkedList &);
  /// =
  CoinModelLinkedList &operator=(const CoinModelLinkedList &);
  //@}

  /**@name sizing (just increases) */
  //@{
  /** Resize list - for row list maxMajor is maximum rows.
  */
  void resize(int maxMajor, CoinBigIndex maxElements);
  /** Create list - for row list maxMajor is maximum rows.
      type 0 row list, 1 column list
  */
  void create(int maxMajor, CoinBigIndex maxElements,
    int numberMajor, int numberMinor,
    int type,
    CoinBigIndex numberElements, const CoinModelTriple *triples);
  /// Number of major items i.e. rows if just row links
  inline int numberMajor() const
  {
    return numberMajor_;
  }
  /// Maximum number of major items i.e. rows if just row links
  inline int maximumMajor() const
  {
    return maximumMajor_;
  }
  /// Number of elements
  inline CoinBigIndex numberElements() const
  {
    return numberElements_;
  }
  /// Maximum number of elements
  inline CoinBigIndex maximumElements() const
  {
    return maximumElements_;
  }
  /// First on free chain
  inline CoinBigIndex firstFree() const
  {
    return first_[maximumMajor_];
  }
  /// Last on free chain
  inline CoinBigIndex lastFree() const
  {
    return last_[maximumMajor_];
  }
  /// First on  chain
  inline CoinBigIndex first(int which) const
  {
    return first_[which];
  }
  /// Last on  chain
  inline CoinBigIndex last(int which) const
  {
    return last_[which];
  }
  /// Next array
  inline const CoinBigIndex *next() const
  {
    return next_;
  }
  /// Previous array
  inline const CoinBigIndex *previous() const
  {
    return previous_;
  }
  //@}

  /**@name does work */
  //@{
  /** Adds to list - easy case i.e. add row to row list
      Returns where chain starts
  */
  CoinBigIndex addEasy(int majorIndex, CoinBigIndex numberOfElements, const int *indices,
    const double *elements, CoinModelTriple *triples,
    CoinModelHash2 &hash);
  /** Adds to list - hard case i.e. add row to column list
  */
  void addHard(int minorIndex, CoinBigIndex numberOfElements, const int *indices,
    const double *elements, CoinModelTriple *triples,
    CoinModelHash2 &hash);
  /** Adds to list - hard case i.e. add row to column list
      This is when elements have been added to other copy
  */
  void addHard(CoinBigIndex first, const CoinModelTriple *triples,
    CoinBigIndex firstFree, CoinBigIndex lastFree, const CoinBigIndex *nextOther);
  /** Deletes from list - same case i.e. delete row from row list
  */
  void deleteSame(int which, CoinModelTriple *triples,
    CoinModelHash2 &hash, bool zapTriples);
  /** Deletes from list - other case i.e. delete row from column list
      This is when elements have been deleted from other copy
  */
  void updateDeleted(int which, CoinModelTriple *triples,
    CoinModelLinkedList &otherList);
  /** Deletes one element from Row list
  */
  void deleteRowOne(CoinBigIndex position, CoinModelTriple *triples,
    CoinModelHash2 &hash);
  /** Update column list for one element when
      one element deleted from row copy
  */
  void updateDeletedOne(CoinBigIndex position, const CoinModelTriple *triples);
  /// Fills first,last with -1
  void fill(int first, int last);
  /** Puts in free list from other list */
  void synchronize(CoinModelLinkedList &other);
  /// Checks that links are consistent
  void validateLinks(const CoinModelTriple *triples) const;
  //@}
private:
  /**@name Data members */
  //@{
  /// Previous - maximumElements long
  CoinBigIndex *previous_;
  /// Next - maximumElements long
  CoinBigIndex *next_;
  /// First - maximumMajor+1 long (last free element chain)
  CoinBigIndex *first_;
  /// Last - maximumMajor+1 long (last free element chain)
  CoinBigIndex *last_;
  /// Number of major items i.e. rows if just row links
  int numberMajor_;
  /// Maximum number of major items i.e. rows if just row links
  int maximumMajor_;
  /// Number of elements
  CoinBigIndex numberElements_;
  /// Maximum number of elements
  CoinBigIndex maximumElements_;
  /// 0 row list, 1 column list
  int type_;
  //@}
};

#endif

/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
*/