constraint.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * \brief A constraint determines a minimum or exact spacing required between
  3. * two variables.
  4. *
  5. * Authors:
  6. * Tim Dwyer <[email protected]>
  7. *
  8. * Copyright (C) 2005 Authors
  9. *
  10. * This version is released under the CPL (Common Public License) with
  11. * the Graphviz distribution.
  12. * A version is also available under the LGPL as part of the Adaptagrams
  13. * project: https://github.com/mjwybrow/adaptagrams.
  14. * If you make improvements or bug fixes to this code it would be much
  15. * appreciated if you could also contribute those changes back to the
  16. * Adaptagrams repository.
  17. */
  18. #pragma once
  19. #include <iostream>
  20. #include <vpsc/variable.h>
  21. struct Constraint
  22. {
  23. friend std::ostream& operator <<(std::ostream &os,const Constraint &c);
  24. public:
  25. Variable *left;
  26. Variable *right;
  27. double gap;
  28. double lm;
  29. Constraint(Variable *left, Variable *right, double gap);
  30. ~Constraint();
  31. double slack() const { return right->position() - gap - left->position(); }
  32. long timeStamp;
  33. bool active;
  34. bool visited;
  35. };
  36. #include <float.h>
  37. #include <vpsc/block.h>
  38. static inline bool compareConstraints(const Constraint *const l,
  39. const Constraint *const r) {
  40. double const sl =
  41. l->left->block->timeStamp > l->timeStamp
  42. ||l->left->block==l->right->block
  43. ?-DBL_MAX:l->slack();
  44. double const sr =
  45. r->left->block->timeStamp > r->timeStamp
  46. ||r->left->block==r->right->block
  47. ?-DBL_MAX:r->slack();
  48. if(sl==sr) {
  49. // arbitrary choice based on id
  50. if(l->left->id==r->left->id) {
  51. if(l->right->id<r->right->id) return true;
  52. return false;
  53. }
  54. if(l->left->id<r->left->id) return true;
  55. return false;
  56. }
  57. return sl < sr;
  58. }