]> granicus.if.org Git - graphviz/commitdiff
avoid manual memory management of PairingHeap pointers in Block
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 12 Jun 2021 20:34:29 +0000 (13:34 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 19 Jun 2021 19:24:07 +0000 (12:24 -0700)
Not only does this free us from having to think about allocation tracking and
memory leaks, because Block does not participate in any inheritance hierarchy we
can also remove its explicit destructor.

lib/vpsc/block.cpp
lib/vpsc/block.h

index aac8c3fb9059c2e598e5219e5198898603adac28..8bb58a77beb88b5797f1ddc7e4c68746d5633704 100644 (file)
@@ -17,6 +17,7 @@
  * Adaptagrams repository.
  */
 #include <cassert>
+#include <memory>
 #include <vpsc/pairingheap/PairingHeap.h>
 #include <vpsc/constraint.h>
 #include <vpsc/block.h>
@@ -42,8 +43,6 @@ void Block::addVariable(Variable *v) {
 Block::Block(Variable *v) {
        timeStamp=0;
        posn=weight=wposn=0;
-       in=nullptr;
-       out=nullptr;
        deleted=false;
        if(v!=nullptr) {
                v->offset=0;
@@ -58,20 +57,16 @@ double Block::desiredWeightedPosition() {
        }
        return wp;
 }
-Block::~Block()
-{
-       delete in;
-       delete out;
-}
 void Block::setUpInConstraints() {
        setUpConstraintHeap(in,true);
 }
 void Block::setUpOutConstraints() {
        setUpConstraintHeap(out,false);
 }
-void Block::setUpConstraintHeap(PairingHeap<Constraint*>* &h,bool in) {
-       delete h;
-       h = new PairingHeap<Constraint*>(&compareConstraints);
+void Block::setUpConstraintHeap(std::unique_ptr<PairingHeap<Constraint*>> &h,
+         bool in) {
+       h = std::unique_ptr<PairingHeap<Constraint*>>(
+         new PairingHeap<Constraint*>(&compareConstraints));
        for (Variable *v : vars) {
                vector<Constraint*> *cs=in?&(v->in):&(v->out);
                for (vector<Constraint*>::iterator j=cs->begin();j!=cs->end();j++) {
@@ -133,7 +128,7 @@ void Block::mergeIn(Block *b) {
        // We check the top of the heaps to remove possible internal constraints
        findMinInConstraint();
        b->findMinInConstraint();
-       in->merge(b->in);
+       in->merge(b->in.get());
        if (RECTANGLE_OVERLAP_LOGGING) {
                ofstream f(LOGFILE,ios::app);
                f<<"  merged heap: "<<*in<<"\n";
@@ -142,7 +137,7 @@ void Block::mergeIn(Block *b) {
 void Block::mergeOut(Block *b) {       
        findMinOutConstraint();
        b->findMinOutConstraint();
-       out->merge(b->out);
+       out->merge(b->out.get());
 }
 Constraint *Block::findMinInConstraint() {
        Constraint *v = nullptr;
index 3a5129b7b0ea3702d2aba8100237ff7e30676aad..e15a87758865a408dd93ce4f3096f8a7ed585ca6 100644 (file)
@@ -19,6 +19,7 @@
 
 #pragma once
 
+#include <memory>
 #include <vector>
 #include <iostream>
 #include <vpsc/pairingheap/PairingHeap.h>
@@ -35,7 +36,6 @@ public:
        double wposn;
        Block(Variable *v=nullptr);
        Block(const Block &) = delete;
-       ~Block();
        Constraint* findMinLM();
        Constraint* findMinLMBetween(Variable* lv, Variable* rv);
        Constraint* findMinInConstraint();
@@ -54,8 +54,8 @@ public:
        double cost();
        bool deleted;
        long timeStamp;
-       PairingHeap<Constraint*> *in;
-       PairingHeap<Constraint*> *out;
+       std::unique_ptr<PairingHeap<Constraint*>> in;
+       std::unique_ptr<PairingHeap<Constraint*>> out;
 private:
        typedef enum {NONE, LEFT, RIGHT} Direction;
        typedef std::pair<double, Constraint*> Pair;
@@ -67,5 +67,5 @@ private:
        bool canFollowRight(Constraint *c, Variable *last);
        void populateSplitBlock(Block *b, Variable *v, Variable *u);
        void addVariable(Variable *v);
-       void setUpConstraintHeap(PairingHeap<Constraint*>* &h,bool in);
+       void setUpConstraintHeap(std::unique_ptr<PairingHeap<Constraint*>> &h,bool in);
 };