Graphviz 13.0.0~dev.20241222.1117
Loading...
Searching...
No Matches
blocks.cpp
Go to the documentation of this file.
1
22#include <vpsc/blocks.h>
23#include <vpsc/block.h>
24#include <vpsc/constraint.h>
25#include <fstream>
26using std::ios;
27using std::ofstream;
28using std::set;
29using std::list;
30
31#ifndef RECTANGLE_OVERLAP_LOGGING
32 #define RECTANGLE_OVERLAP_LOGGING 0
33#endif
34
36
37Blocks::Blocks(const int n, Variable *vs_[]) : vs(vs_), nvs(n) {
39 for(int i=0;i<nvs;i++) {
40 insert(new Block(vs[i]));
41 }
42}
44{
46 for (Block *b : *this) {
47 delete b;
48 }
49}
50
55list<Variable*> Blocks::totalOrder() {
56 list<Variable*> order;
57 for(int i=0;i<nvs;i++) {
58 vs[i]->visited=false;
59 }
60 for(int i=0;i<nvs;i++) {
61 if(vs[i]->in.empty()) {
62 dfsVisit(vs[i],order);
63 }
64 }
65 return order;
66}
67// Recursive depth first search giving total order by pushing nodes in the DAG
68// onto the front of the list when we finish searching them
69void Blocks::dfsVisit(Variable *v, list<Variable*> &order) {
70 v->visited=true;
71 for (Constraint *c : v->out) {
72 if(!c->right->visited) {
73 dfsVisit(c->right, order);
74 }
75 }
77 ofstream f(LOGFILE,ios::app);
78 f<<" order="<<*v<<"\n";
79 }
80 order.push_front(v);
81}
88 ofstream f(LOGFILE,ios::app);
89 f<<"mergeLeft called on "<<*r<<"\n";
90 }
94 while (c != nullptr && c->slack()<0) {
96 ofstream f(LOGFILE,ios::app);
97 f<<"mergeLeft on constraint: "<<*c<<"\n";
98 }
100 Block *l = c->left->block;
101 if (l->in.empty()) l->setUpInConstraints();
102 double dist = c->right->offset - c->left->offset - c->gap;
103 if (r->vars.size() < l->vars.size()) {
104 dist=-dist;
105 std::swap(l, r);
106 }
107 blockTimeCtr++;
108 r->merge(l, c, dist);
109 r->mergeIn(l);
111 removeBlock(l);
112 c=r->findMinInConstraint();
113 }
115 ofstream f(LOGFILE,ios::app);
116 f<<"merged "<<*r<<"\n";
117 }
118}
124 ofstream f(LOGFILE,ios::app);
125 f<<"mergeRight called on "<<*l<<"\n";
126 }
129 while (c != nullptr && c->slack()<0) {
131 ofstream f(LOGFILE,ios::app);
132 f<<"mergeRight on constraint: "<<*c<<"\n";
133 }
135 Block *r = c->right->block;
137 double dist = c->left->offset + c->gap - c->right->offset;
138 if (l->vars.size() > r->vars.size()) {
139 dist=-dist;
140 std::swap(l, r);
141 }
142 l->merge(r, c, dist);
143 l->mergeOut(r);
144 removeBlock(r);
146 }
148 ofstream f(LOGFILE,ios::app);
149 f<<"merged "<<*l<<"\n";
150 }
151}
152void Blocks::removeBlock(Block *doomed) {
153 doomed->deleted=true;
154 //erase(doomed);
155}
157 for (auto i = begin(); i != end();) {
158 Block *b=*i;
159 if(b->deleted) {
160 i = erase(i);
161 delete b;
162 } else {
163 ++i;
164 }
165 }
166}
171void Blocks::split(Block *b, Block *&l, Block *&r, Constraint *c) {
172 b->split(l,r,c);
174 ofstream f(LOGFILE,ios::app);
175 f<<"Split left: "<<*l<<"\n";
176 f<<"Split right: "<<*r<<"\n";
177 }
178 r->posn = b->posn;
179 r->wposn = r->posn * r->weight;
180 mergeLeft(l);
181 // r may have been merged!
182 r = c->right->block;
184 r->posn = r->wposn / r->weight;
185 mergeRight(r);
186 removeBlock(b);
187
188 insert(l);
189 insert(r);
190}
195double Blocks::cost() {
196 double c = 0;
197 for (Block *b : *this) {
198 c += b->cost();
199 }
200 return c;
201}
static void out(agerrlevel_t level, const char *fmt, va_list args)
Report messages using a user-supplied or default write function.
Definition agerror.c:84
#define RECTANGLE_OVERLAP_LOGGING
Definition block.cpp:33
long blockTimeCtr
Definition blocks.cpp:35
long blockTimeCtr
Definition blocks.cpp:35
#define LOGFILE
A block structure defined over the variables.
Definition blocks.h:24
Definition block.h:28
bool deleted
Definition block.h:53
Constraint * findMinInConstraint()
Definition block.cpp:178
std::vector< Variable * > vars
Definition block.h:31
void split(Block *&l, Block *&r, Constraint *c)
Definition block.cpp:396
double posn
Definition block.h:32
void deleteMinOutConstraint()
Definition block.cpp:240
void mergeIn(Block *b)
Definition block.cpp:163
void mergeOut(Block *b)
Definition block.cpp:173
std::vector< Constraint * > in
Definition block.h:55
void setUpOutConstraints()
Definition block.cpp:103
double wposn
Definition block.h:34
void setUpInConstraints()
Definition block.cpp:100
double desiredWeightedPosition()
Definition block.cpp:93
double weight
Definition block.h:33
Constraint * findMinOutConstraint()
Definition block.cpp:227
void merge(Block *b, Constraint *c, double dist)
Definition block.cpp:146
void deleteMinInConstraint()
Definition block.cpp:237
long timeStamp
Definition block.h:54
void cleanup()
Definition blocks.cpp:156
double cost()
Definition blocks.cpp:195
void split(Block *b, Block *&l, Block *&r, Constraint *c)
Definition blocks.cpp:171
std::list< Variable * > totalOrder()
Definition blocks.cpp:55
~Blocks()
Definition blocks.cpp:43
Blocks(const int n, Variable *vs[])
Definition blocks.cpp:37
void mergeRight(Block *l)
Definition blocks.cpp:122
void mergeLeft(Block *r)
Definition blocks.cpp:86
static void insert(PairHeap *h, Pair edge)
Definition closest.c:145
static double dist(int dim, double *x, double *y)
A constraint determines a minimum or exact spacing required between two variables.
Definition constraint.h:25
Variable * right
Definition constraint.h:29
double gap
Definition constraint.h:30
Variable * left
Definition constraint.h:28
double slack() const
Definition constraint.h:34
Block * block
Definition variable.h:33
double offset
Definition variable.h:32
bool visited
Definition variable.h:34