Block::Block(Variable *v) {
timeStamp=0;
posn=weight=wposn=0;
- in=NULL;
- out=NULL;
+ in=nullptr;
+ out=nullptr;
deleted=false;
vars=new vector<Variable*>;
- if(v!=NULL) {
+ if(v!=nullptr) {
v->offset=0;
addVariable(v);
}
out->merge(b->out);
}
Constraint *Block::findMinInConstraint() {
- Constraint *v = NULL;
+ Constraint *v = nullptr;
vector<Constraint*> outOfDate;
while (!in->isEmpty()) {
v = in->findMin();
in->insert(v);
}
if(in->isEmpty()) {
- v=NULL;
+ v=nullptr;
} else {
v=in->findMin();
}
return v;
}
Constraint *Block::findMinOutConstraint() {
- if(out->isEmpty()) return NULL;
+ if(out->isEmpty()) return nullptr;
Constraint *v = out->findMin();
while (v->left->block == v->right->block) {
out->deleteMin();
- if(out->isEmpty()) return NULL;
+ if(out->isEmpty()) return nullptr;
v = out->findMin();
}
return v;
Constraint *c=*it;
if(canFollowRight(c,u)) {
dfdv+=c->lm=compute_dfdv(c->right,v,min_lm);
- if(min_lm==NULL||c->lm<min_lm->lm) min_lm=c;
+ if(min_lm==nullptr||c->lm<min_lm->lm) min_lm=c;
}
}
for(vector<Constraint*>::iterator it=v->in.begin();it!=v->in.end();it++) {
Constraint *c=*it;
if(canFollowLeft(c,u)) {
dfdv-=c->lm=-compute_dfdv(c->left,v,min_lm);
- if(min_lm==NULL||c->lm<min_lm->lm) min_lm=c;
+ if(min_lm==nullptr||c->lm<min_lm->lm) min_lm=c;
}
}
return dfdv;
// the constraint c to compute the lagrangian multiplier lm_c.
// The top level v and r are variables between which we want to find the
// constraint with the smallest lm.
-// When we find r we pass NULL to subsequent recursive calls,
-// thus r=NULL indicates constraints are not on the shortest path.
-// Similarly, m is initially NULL and is only assigned a value if the next
+// When we find r we pass nullptr to subsequent recursive calls,
+// thus r=nullptr indicates constraints are not on the shortest path.
+// Similarly, m is initially nullptr and is only assigned a value if the next
// variable to be visited is r or if a possible min constraint is returned from
-// a nested call (rather than NULL).
+// a nested call (rather than nullptr).
// Then, the search for the m with minimum lm occurs as we return from
// the recursion (checking only constraints traversed left-to-right
// in order to avoid creating any new violations).
Block::Pair Block::compute_dfdv_between(Variable* r, Variable* v, Variable* u,
Direction dir = NONE, bool changedDirection = false) {
double dfdv=v->weight*(v->position() - v->desiredPosition);
- Constraint *m=NULL;
+ Constraint *m=nullptr;
for(Cit it(v->in.begin());it!=v->in.end();it++) {
Constraint *c=*it;
if(canFollowLeft(c,u)) {
changedDirection = true;
}
if(c->left==r) {
- r=NULL; m=c;
+ r=nullptr; m=c;
}
Pair p=compute_dfdv_between(r,c->left,v,
LEFT,changedDirection);
changedDirection = true;
}
if(c->right==r) {
- r=NULL; m=c;
+ r=nullptr; m=c;
}
Pair p=compute_dfdv_between(r,c->right,v,
RIGHT,changedDirection);
* that most wants to split
*/
Constraint *Block::findMinLM() {
- Constraint *min_lm=NULL;
- reset_active_lm(vars->front(),NULL);
- compute_dfdv(vars->front(),NULL,min_lm);
+ Constraint *min_lm=nullptr;
+ reset_active_lm(vars->front(),nullptr);
+ compute_dfdv(vars->front(),nullptr,min_lm);
return min_lm;
}
Constraint *Block::findMinLMBetween(Variable* lv, Variable* rv) {
- Constraint *min_lm=NULL;
- reset_active_lm(vars->front(),NULL);
- min_lm=compute_dfdv_between(rv,lv,NULL).second;
+ Constraint *min_lm=nullptr;
+ reset_active_lm(vars->front(),nullptr);
+ min_lm=compute_dfdv_between(rv,lv,nullptr).second;
return min_lm;
}
double posn;
double weight;
double wposn;
- Block(Variable *v=NULL);
+ Block(Variable *v=nullptr);
~Block();
Constraint* findMinLM();
Constraint* findMinLMBetween(Variable* lv, Variable* rv);
r->timeStamp=++blockTimeCtr;
r->setUpInConstraints();
Constraint *c=r->findMinInConstraint();
- while (c != NULL && c->slack()<0) {
+ while (c != nullptr && c->slack()<0) {
if (RECTANGLE_OVERLAP_LOGGING) {
ofstream f(LOGFILE,ios::app);
f<<"mergeLeft on constraint: "<<*c<<"\n";
}
r->deleteMinInConstraint();
Block *l = c->left->block;
- if (l->in==NULL) l->setUpInConstraints();
+ if (l->in==nullptr) l->setUpInConstraints();
double dist = c->right->offset - c->left->offset - c->gap;
if (r->vars->size() < l->vars->size()) {
dist=-dist;
}
l->setUpOutConstraints();
Constraint *c = l->findMinOutConstraint();
- while (c != NULL && c->slack()<0) {
+ while (c != nullptr && c->slack()<0) {
if (RECTANGLE_OVERLAP_LOGGING) {
ofstream f(LOGFILE,ios::app);
f<<"mergeRight on constraint: "<<*c<<"\n";
return vpsc->splitCnt;
}
void deleteVPSC(VPSC *vpsc) {
- assert(vpsc!=NULL);
+ assert(vpsc!=nullptr);
delete vpsc;
}
void solveVPSC(VPSC* vpsc) {
Node *firstAbove, *firstBelow;
NodeSet leftNeighbours, rightNeighbours;
Node(Variable *v, Rectangle *r, double p) : v(v),r(r),pos(p) {
- firstAbove=firstBelow=NULL;
+ firstAbove=firstBelow=nullptr;
assert(r->width()<1e40);
}
void addLeftNeighbour(Node *u) {
}
} else {
Node *l=v->firstAbove, *r=v->firstBelow;
- if(l!=NULL) {
+ if(l!=nullptr) {
double sep = (v->r->width()+l->r->width())/2.0;
constraints.push_back(new Constraint(l->v,v->v,sep));
l->firstBelow=v->firstBelow;
}
- if(r!=NULL) {
+ if(r!=nullptr) {
double sep = (v->r->width()+r->r->width())/2.0;
constraints.push_back(new Constraint(v->v,r->v,sep));
r->firstAbove=v->firstAbove;
} else {
// Close event
Node *l=v->firstAbove, *r=v->firstBelow;
- if(l!=NULL) {
+ if(l!=nullptr) {
double sep = (v->r->height()+l->r->height())/2.0;
constraints.push_back(new Constraint(l->v,v->v,sep));
l->firstBelow=v->firstBelow;
}
- if(r!=NULL) {
+ if(r!=nullptr) {
double sep = (v->r->height()+r->r->height())/2.0;
constraints.push_back(new Constraint(v->v,r->v,sep));
r->firstAbove=v->firstAbove;
template <class T>
PairingHeap<T>::PairingHeap( bool (*lessThan)(T const &lhs, T const &rhs) )
{
- root = NULL;
+ root = nullptr;
counter=0;
this->lessThan=lessThan;
}
template <class T>
PairingHeap<T>::PairingHeap( const PairingHeap<T> & rhs )
{
- root = NULL;
+ root = nullptr;
counter=rhs->size();
*this = rhs;
}
{
PairNode<T> *newNode = new PairNode<T>( x );
- if( root == NULL )
+ if( root == nullptr )
root = newNode;
else
compareAndLink( root, newNode );
PairNode<T> *oldRoot = root;
- if( root->leftChild == NULL )
- root = NULL;
+ if( root->leftChild == nullptr )
+ root = nullptr;
else
root = combineSiblings( root->leftChild );
counter--;
template <class T>
bool PairingHeap<T>::isEmpty( ) const
{
- return root == NULL;
+ return root == nullptr;
}
/**
void PairingHeap<T>::makeEmpty( )
{
reclaimMemory( root );
- root = NULL;
+ root = nullptr;
}
/**
template <class T>
void PairingHeap<T>::reclaimMemory( PairNode<T> * t ) const
{
- if( t != NULL )
+ if( t != nullptr )
{
reclaimMemory( t->leftChild );
reclaimMemory( t->nextSibling );
p->element = newVal;
if( p != root )
{
- if( p->nextSibling != NULL )
+ if( p->nextSibling != nullptr )
p->nextSibling->prev = p->prev;
if( p->prev->leftChild == p )
p->prev->leftChild = p->nextSibling;
else
p->prev->nextSibling = p->nextSibling;
- p->nextSibling = NULL;
+ p->nextSibling = nullptr;
compareAndLink( root, p );
}
}
/**
* Internal method that is the basic operation to maintain order.
* Links first and second together to satisfy heap order.
-* first is root of tree 1, which may not be NULL.
-* first->nextSibling MUST be NULL on entry.
-* second is root of tree 2, which may be NULL.
+* first is root of tree 1, which may not be nullptr.
+* first->nextSibling MUST be nullptr on entry.
+* second is root of tree 2, which may be nullptr.
* first becomes the result of the tree merge.
*/
template <class T>
compareAndLink( PairNode<T> * & first,
PairNode<T> *second ) const
{
- if( second == NULL )
+ if( second == nullptr )
return;
if( lessThan(second->element,first->element) )
{
second->prev = first->prev;
first->prev = second;
first->nextSibling = second->leftChild;
- if( first->nextSibling != NULL )
+ if( first->nextSibling != nullptr )
first->nextSibling->prev = first;
second->leftChild = first;
first = second;
// Attach second as leftmost child of first
second->prev = first;
first->nextSibling = second->nextSibling;
- if( first->nextSibling != NULL )
+ if( first->nextSibling != nullptr )
first->nextSibling->prev = first;
second->nextSibling = first->leftChild;
- if( second->nextSibling != NULL )
+ if( second->nextSibling != nullptr )
second->nextSibling->prev = second;
first->leftChild = second;
}
/**
* Internal method that implements two-pass merging.
* firstSibling the root of the conglomerate;
-* assumed not NULL.
+* assumed not nullptr.
*/
template <class T>
PairNode<T> *
PairingHeap<T>::combineSiblings( PairNode<T> *firstSibling ) const
{
- if( firstSibling->nextSibling == NULL )
+ if( firstSibling->nextSibling == nullptr )
return firstSibling;
// Allocate the array
// Store the subtrees in an array
int numSiblings = 0;
- for( ; firstSibling != NULL; numSiblings++ )
+ for( ; firstSibling != nullptr; numSiblings++ )
{
if( numSiblings == (int)treeArray.size( ) )
treeArray.resize( numSiblings * 2 );
treeArray[ numSiblings ] = firstSibling;
- firstSibling->prev->nextSibling = NULL; // break links
+ firstSibling->prev->nextSibling = nullptr; // break links
firstSibling = firstSibling->nextSibling;
}
if( numSiblings == (int)treeArray.size( ) )
treeArray.resize( numSiblings + 1 );
- treeArray[ numSiblings ] = NULL;
+ treeArray[ numSiblings ] = nullptr;
// Combine subtrees two at a time, going left to right
int i = 0;
PairNode<T> *
PairingHeap<T>::clone( PairNode<T> * t ) const
{
- if( t == NULL )
- return NULL;
+ if( t == nullptr )
+ return nullptr;
else
{
PairNode<T> *p = new PairNode<T>( t->element );
- if( ( p->leftChild = clone( t->leftChild ) ) != NULL )
+ if( ( p->leftChild = clone( t->leftChild ) ) != nullptr )
p->leftChild->prev = p;
- if( ( p->nextSibling = clone( t->nextSibling ) ) != NULL )
+ if( ( p->nextSibling = clone( t->nextSibling ) ) != nullptr )
p->nextSibling->prev = p;
return p;
}
ostream& operator <<(ostream &os, const PairingHeap<T> &b)
{
os<<"Heap:";
- if (b.root != NULL) {
+ if (b.root != nullptr) {
PairNode<T> *r = b.root;
list<PairNode<T>*> q;
q.push_back(r);
while (!q.empty()) {
r = q.front();
q.pop_front();
- if (r->leftChild != NULL) {
+ if (r->leftChild != nullptr) {
os << *r->element << ">";
PairNode<T> *c = r->leftChild;
- while (c != NULL) {
+ while (c != nullptr) {
q.push_back(c);
os << "," << *c->element;
c = c->nextSibling;
PairNode( const T & theElement ) :
element( theElement ),
- leftChild(NULL), nextSibling(NULL), prev(NULL)
+ leftChild(nullptr), nextSibling(nullptr), prev(nullptr)
{ }
friend class PairingHeap<T>;
};
void merge( PairingHeap<T> *rhs )
{
PairNode<T> *broot=rhs->getRoot();
- if (root == NULL) {
- if(broot != NULL) {
+ if (root == nullptr) {
+ if(broot != nullptr) {
root = broot;
}
} else {
protected:
PairNode<T> * getRoot() {
PairNode<T> *r=root;
- root=NULL;
+ root=nullptr;
return r;
}
private:
for(set<Block*>::const_iterator i=bs->begin();i!=bs->end();i++) {
Block *b=*i;
Constraint *c=b->findMinLM();
- if(c!=NULL && c->lm<0) {
+ if(c!=nullptr && c->lm<0) {
if (RECTANGLE_OVERLAP_LOGGING) {
ofstream f(LOGFILE,ios::app);
f<<"Split on constraint: "<<*c<<"\n";
}
// Split on c
- Block *l=NULL, *r=NULL;
+ Block *l=nullptr, *r=nullptr;
bs->split(b,l,r,c);
bs->cleanup();
// split alters the block set so we have to restart
}
splitBlocks();
long splitCtr = 0;
- Constraint* v = NULL;
+ Constraint* v = nullptr;
while(mostViolated(inactive,v)<-0.0000001) {
assert(!v->active);
Block *lb = v->left->block, *rb = v->right->block;
for(set<Block*>::const_iterator i(bs->begin());i!=bs->end();i++) {
Block* b = *i;
Constraint* v=b->findMinLM();
- if(v!=NULL && v->lm < -0.0000001) {
+ if(v!=nullptr && v->lm < -0.0000001) {
if (RECTANGLE_OVERLAP_LOGGING) {
ofstream f(LOGFILE,ios::app);
f<<" found split point: "<<*v<<" lm="<<v->lm<<"\n";
}
splitCnt++;
- Block *b = v->left->block, *l=NULL, *r=NULL;
+ Block *b = v->left->block, *l=nullptr, *r=nullptr;
assert(v->left->block == v->right->block);
double pos = b->posn;
b->split(l,r,v);
}
}
while(!graph.empty()) {
- node *u=NULL;
+ node *u=nullptr;
vector<node*>::iterator i=graph.begin();
for(;i!=graph.end();i++) {
u=*i;
Block *b=*i;
b->setUpInConstraints();
Constraint *c=b->findMinInConstraint();
- while(c!=NULL) {
+ while(c!=nullptr) {
Block *l=c->left->block;
bmap[b]->in.insert(bmap[l]);
b->deleteMinInConstraint();
b->setUpOutConstraints();
c=b->findMinOutConstraint();
- while(c!=NULL) {
+ while(c!=nullptr) {
Block *r=c->right->block;
bmap[b]->out.insert(bmap[r]);
b->deleteMinOutConstraint();
}
}
while(!graph.empty()) {
- node *u=NULL;
+ node *u=nullptr;
vector<node*>::iterator i=graph.begin();
for(;i!=graph.end();i++) {
u=*i;