cppDelVertex

UseCase3



// COM1205 Propagation Pattern for DemDraw Project
//
// Created by Young Soo Yang (yyang@ccs.neu.edu)
//
// This propagation pattern will traverse all the vertices in a Graph object
// to delete a vertex with the given name.  It calls cppCompVertName() with
// argument Labelname to check each vertex name  and, if it finds one, deletes
// the vertex and all the edges attached to the vertex by calling
// delAttachedEdges(), and returns 1 to the caller, otherwise, returns 0.
// Caution: It should delete attached edges first before removing a vertex
//          because selecting edges to be removed is done by comparing source
//          and destination vertex name of an edge.
//
// Modification History:
//
// 11/26/95 - Young Soo Yang (yyang@ccs.neu.edu)
//	Modified to make it more structure shy
//


*operation* int cppDelVertex( char* Labelname )
   *init* (@ 0 @)
   *traverse*
      *from* Graph
         *through* -> *,vertices,*
      *to*   { AltVertex, ConstVertex }

   *carry*
      *in* Graph* iGraph = (@ this @),
      *in* Vertex_List* new_VList = (@ new Vertex_List() @)
      *along* *from* Graph *through* -> *,vertices,* *to* { AltVertex, ConstVertex }

   *wrapper* Graph
      *suffix*
      (@
         delete(this -> get_vertices());
         this -> set_vertices(new_VList);

         this -> cppDrawVertices();
         this -> cppDrawEdges();
      @)

   *wrapper* AltVertex
      (@
         if (this -> cppCompVertName(Labelname)) {

            iGraph -> delAttachedEdges(Labelname);
            this -> g_delete();
            return_val = 1;

         } else {

            new_VList -> append(this);

         }
      @)

   *wrapper* ConstVertex
      (@
         if (this -> cppCompVertName(Labelname)) {

            iGraph -> delAttachedEdges(Labelname);
            this -> g_delete();
            return_val = 1;

         } else {

            new_VList -> append(this);

         }
      @)



// This will deletes all the edges attached to the vertex of which label name
// is same with the argument Labelname.  So, this prop-pattern will check
// source & destination vertices of an edge to find edges to be deleted.
// Note: can't use g_delete() to delete an edge since each edge also points
//       to two vertices, source & destination, that should not be removed.

*operation* void delAttachedEdges(char* Labelname)
   *traverse*
      *from* Graph
         *through* -> *,edges,*
      *to*   { ConstEdge, AltEdge }

   *carry*
      *in* Edge_List* new_EList = (@ new Edge_List() @)
      *along* *from* Graph *through* -> *,edges,* *to* { ConstEdge, AltEdge }

   *wrapper* Graph
      *suffix*
      (@
         delete(this -> get_edges());
         this -> set_edges(new_EList);
      @)

   *wrapper* AltEdge
      (@
         if (this -> get_from() -> cppCompVertName(Labelname) ||
             this -> get_to() -> cppCompVertName(Labelname)) {

            if (this -> get_middlepoints() != NULL)
               this -> get_middlepoints() -> g_delete();
            delete(this);

         } else {

            new_EList -> append(this);

         }
      @)

   *wrapper* ConstEdge
      (@
         if (this -> get_from() -> cppCompVertName(Labelname) ||
             this -> get_to() -> cppCompVertName(Labelname)) {

            this -> get_edgename() -> g_delete();
            if (this -> get_middlepoints() != NULL)
               this -> get_middlepoints() -> g_delete();
            delete (this);

         } else {

            new_EList -> append(this);

         }
      @)