block.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * \brief A block is a group of variables that must be moved together to improve
  3. * the goal function without violating already active constraints.
  4. * The variables in a block are spanned by a tree of active constraints.
  5. *
  6. * Authors:
  7. * Tim Dwyer <[email protected]>
  8. *
  9. * Copyright (C) 2005 Authors
  10. *
  11. * This version is released under the CPL (Common Public License) with
  12. * the Graphviz distribution.
  13. * A version is also available under the LGPL as part of the Adaptagrams
  14. * project: https://github.com/mjwybrow/adaptagrams.
  15. * If you make improvements or bug fixes to this code it would be much
  16. * appreciated if you could also contribute those changes back to the
  17. * Adaptagrams repository.
  18. */
  19. #pragma once
  20. #include <vector>
  21. #include <iostream>
  22. struct Variable;
  23. struct Constraint;
  24. class Block
  25. {
  26. friend std::ostream& operator <<(std::ostream &os,const Block &b);
  27. public:
  28. std::vector<Variable*> vars;
  29. double posn;
  30. double weight;
  31. double wposn;
  32. Block(Variable *v=nullptr);
  33. Block(const Block &) = delete;
  34. Constraint* findMinLM();
  35. Constraint* findMinLMBetween(Variable* lv, Variable* rv);
  36. Constraint* findMinInConstraint();
  37. Constraint* findMinOutConstraint();
  38. void deleteMinInConstraint();
  39. void deleteMinOutConstraint();
  40. double desiredWeightedPosition();
  41. void merge(Block *b, Constraint *c, double dist);
  42. void merge(Block *b, Constraint *c);
  43. void mergeIn(Block *b);
  44. void mergeOut(Block *b);
  45. void split(Block *&l, Block *&r, Constraint *c);
  46. Constraint* splitBetween(Variable* vl, Variable* vr, Block* &lb, Block* &rb);
  47. void setUpInConstraints();
  48. void setUpOutConstraints();
  49. double cost();
  50. bool deleted;
  51. long timeStamp;
  52. std::vector<Constraint *> in;
  53. std::vector<Constraint *> out;
  54. private:
  55. typedef enum {NONE, LEFT, RIGHT} Direction;
  56. typedef std::pair<double, Constraint*> Pair;
  57. void reset_active_lm(Variable *v, Variable *u);
  58. double compute_dfdv(Variable *v, Variable *u, Constraint *&min_lm);
  59. Pair compute_dfdv_between(
  60. Variable*, Variable*, Variable*, Direction, bool);
  61. bool canFollowLeft(const Constraint *c, const Variable *last);
  62. bool canFollowRight(const Constraint *c, const Variable *last);
  63. void populateSplitBlock(Block *b, Variable *v, Variable *u);
  64. void addVariable(Variable *v);
  65. std::vector<Constraint *> setUpConstraintHeap(bool use_in);
  66. };