]> granicus.if.org Git - graphviz/commitdiff
manage NodeSets by value instead of pointer
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 18 Apr 2021 20:03:36 +0000 (13:03 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 24 Apr 2021 21:15:10 +0000 (14:15 -0700)
There is no need to manually allocate memory for these sets. We can simply let
the compiler take care of this. Note that this is also unlikely to affect the
performance characteristics of getLeftNeighbours and getRightNeighbours because
Named Return Value Optimization (NRVO) is applicable to these functions.

lib/vpsc/generate-constraints.cpp

index f0901214b7861cf8a5945cf4b5c595b5bbdfe7d9..be49918be078dcfe35a4e7357f7e25a952e12eea 100644 (file)
@@ -47,30 +47,25 @@ struct Node {
        Rectangle *r;
        double pos;
        Node *firstAbove, *firstBelow;
-       NodeSet *leftNeighbours, *rightNeighbours;
+       NodeSet leftNeighbours, rightNeighbours;
        Node(Variable *v, Rectangle *r, double p) : v(v),r(r),pos(p) {
                firstAbove=firstBelow=NULL;
-               leftNeighbours=rightNeighbours=NULL;
                assert(r->width()<1e40);
        }
-       ~Node() {
-               delete leftNeighbours;
-               delete rightNeighbours;
-       }
        void addLeftNeighbour(Node *u) {
-               leftNeighbours->insert(u);
+               leftNeighbours.insert(u);
        }
        void addRightNeighbour(Node *u) {
-               rightNeighbours->insert(u);
+               rightNeighbours.insert(u);
        }
-       void setNeighbours(NodeSet *left, NodeSet *right) {
+       void setNeighbours(const NodeSet &left, const NodeSet &right) {
                leftNeighbours=left;
                rightNeighbours=right;
-               for(NodeSet::iterator i=left->begin();i!=left->end();i++) {
+               for(NodeSet::iterator i=left.begin();i!=left.end();i++) {
                        Node *v=*(i);
                        v->addRightNeighbour(this);
                }
-               for(NodeSet::iterator i=right->begin();i!=right->end();i++) {
+               for(NodeSet::iterator i=right.begin();i!=right.end();i++) {
                        Node *v=*(i);
                        v->addLeftNeighbour(this);
                }
@@ -86,32 +81,32 @@ bool CmpNodePos::operator() (const Node* u, const Node* v) const {
        return u < v;
 }
 
-static NodeSet* getLeftNeighbours(NodeSet &scanline,Node *v) {
-       NodeSet *leftv = new NodeSet;
+static NodeSet getLeftNeighbours(NodeSet &scanline,Node *v) {
+       NodeSet leftv;
        NodeSet::iterator i=scanline.find(v);
        while(i!=scanline.begin()) {
                Node *u=*(--i);
                if(u->r->overlapX(v->r)<=0) {
-                       leftv->insert(u);
+                       leftv.insert(u);
                        return leftv;
                }
                if(u->r->overlapX(v->r)<=u->r->overlapY(v->r)) {
-                       leftv->insert(u);
+                       leftv.insert(u);
                }
        }
        return leftv;
 }
-static NodeSet* getRightNeighbours(NodeSet &scanline,Node *v) {
-       NodeSet *rightv = new NodeSet;
+static NodeSet getRightNeighbours(NodeSet &scanline,Node *v) {
+       NodeSet rightv;
        NodeSet::iterator i=scanline.find(v);
        for(i++;i!=scanline.end(); i++) {
                Node *u=*(i);
                if(u->r->overlapX(v->r)<=0) {
-                       rightv->insert(u);
+                       rightv.insert(u);
                        return rightv;
                }
                if(u->r->overlapX(v->r)<=u->r->overlapY(v->r)) {
-                       rightv->insert(u);
+                       rightv.insert(u);
                }
        }
        return rightv;
@@ -183,16 +178,16 @@ int generateXConstraints(const int n, Rectangle** rs, Variable** vars, Constrain
                } else {
                        // Close event
                        if(useNeighbourLists) {
-                               for(Node *u : *v->leftNeighbours) {
+                               for(Node *u : v->leftNeighbours) {
                                        double sep = (v->r->width()+u->r->width())/2.0;
                                        constraints.push_back(new Constraint(u->v,v->v,sep));
-                                       u->rightNeighbours->erase(v);
+                                       u->rightNeighbours.erase(v);
                                }
                                
-                               for(Node *u : *v->rightNeighbours) {
+                               for(Node *u : v->rightNeighbours) {
                                        double sep = (v->r->width()+u->r->width())/2.0;
                                        constraints.push_back(new Constraint(v->v,u->v,sep));
-                                       u->leftNeighbours->erase(v);
+                                       u->leftNeighbours.erase(v);
                                }
                        } else {
                                Node *l=v->firstAbove, *r=v->firstBelow;