📄 graphvertex.h
字号:
template<class TObject>boolean GraphVertex<TObject>::clear(Integral::CMODE cmode_a) { // switch on cmode // if (cmode_a == Integral::RETAIN) { if (item_d != (TObject*)NULL) { item_d->clear(cmode_a); } } else if ((cmode_a == Integral::RESET) || (cmode_a == Integral::RELEASE)) { // clear the references to the data // item_d = (TObject*)NULL; // clear the reference to the parent graph // parent_graph_d = (DiGraph<TObject>*)NULL; } else { // deallocate the allocated memory if necessary // if (item_d != (TObject*)NULL) { item_d->clear(Integral::FREE); delete item_d; item_d = (TObject*)NULL; } // clear the reference to the parent graph // parent_graph_d = (DiGraph<TObject>*)NULL; } // clear the arc list // return removeAllArcs();}//---------------------------------------------------------------------------//// class-specific public methods:// vertex manipulation methods////--------------------------------------------------------------------------- // method: insertArc//// arguments:// GraphVertex<TObject>* vertex: (input) the vertex to connect to// double weight: (input) the weight on the generated arc// boolean is_epsilon: (input) is this an epsilon transition?//// return: a boolean value indicating status//// this method inserts a weighted arc connecting this vertex to the input// vertex.//template<class TObject>boolean GraphVertex<TObject>::insertArc(GraphVertex<TObject>* vertex_a, double weight_a, boolean is_epsilon_a) { // check that the input vertex is not null // if (vertex_a == (GraphVertex<TObject>*)NULL) { return Error::handle(name(), L"insertArc", Error::ARG, __FILE__, __LINE__); } // create a new Arc // GraphArc<TObject>* new_arc = new GraphArc<TObject>(vertex_a, weight_a); new_arc->setEpsilon(is_epsilon_a); // insert the arc to the list // insert(new_arc); // exit gracefully // return true;}// method: insertArc//// arguments:// GraphVertex<TObject>* vertex: (input) the vertex to connect to// boolean is_epsilon: (input) is this an epsilon transition?//// return: a boolean value indicating status//// this method inserts an arc connecting this vertex to the input// vertex.//template<class TObject>boolean GraphVertex<TObject>::insertArc(GraphVertex<TObject>* vertex_a, boolean is_epsilon_a) { // check that the input vertex is not null // if (vertex_a == (GraphVertex<TObject>*)NULL) { return Error::handle(name(), L"insertArc", Error::ARG, __FILE__, __LINE__); } // create a new Arc // GraphArc<TObject>* new_arc = new GraphArc<TObject>(vertex_a); new_arc->setEpsilon(is_epsilon_a); // insert the arc to the list // insert(new_arc); // exit gracefully // return true;}// method: removeArc//// arguments:// GraphVertex<TObject>* vertex: (input) the vertex to unlink from//// return: a boolean value indicating status//// this method unlinks this vertex from the input vertex. if the graph is// not directed, then an arc from the input vertex is removed as well//template<class TObject>boolean GraphVertex<TObject>::removeArc(GraphVertex<TObject>* vertex_a) { // make sure the list is not empty // if (isEmpty()) { return false; } // find the input vertex in the adjacency list by looping over all arcs // SingleLinkedNode< GraphArc<TObject> >* tmp_node = first_d; // declare temporary objects // GraphVertex<TObject>* tmp_vert = (GraphVertex<TObject>*) NULL; // loop over each element and look for the input vertex // boolean vertex_found = false; while ((tmp_node != (SingleLinkedNode< GraphArc<TObject> >*)NULL) && !vertex_found) { // get the vertex out of the arc // tmp_vert = tmp_node->getItem()->getVertex(); // check to see if the found vertex is the one we are looking for // if (tmp_vert == vertex_a) { vertex_found = true; } else { tmp_node = tmp_node->getNext(); } } // if we found the vertex then break the arcs, else return an error. // if (vertex_found) { // if the found vertex was at the current node position then remove the // node // if (curr_d == tmp_node) { // remove the arc from the graph // boolean status = false; GraphArc<TObject>* graph_arc; status = remove(graph_arc); delete graph_arc; if (!status) { return false; } } else { // put the current node pointer in place so we can call a // SingleLinkedList remove method // SingleLinkedNode< GraphArc<TObject> >* tmp_curr = curr_d; curr_d = tmp_node; // remove the node // boolean status = false; GraphArc<TObject>* graph_arc; status = remove(graph_arc); delete graph_arc; if (!status) { return false; } // put the current pointer back in place // curr_d = tmp_curr; } } else { return false; } // exit gracefully // return true;}// method: removeArc//// arguments: none//// return: a boolean value indicating status//// this method unlinks this vertex from the vertex linked by the current arc// in the list. if the graph is not directed, then an arc from the found// vertex is removed as well//template<class TObject>boolean GraphVertex<TObject>::removeArc() { // if the adjacency list is empty then return false // if (isEmpty()) { return false; } // remove this arc // boolean status = false; GraphArc<TObject>* graph_arc; status = remove(graph_arc); delete graph_arc; // exit gracefully // return status;}// method: removeAllArcs//// arguments: none//// return: a boolean value indicating status//// this method removes all arcs extending from this vertex//template<class TObject>boolean GraphVertex<TObject>::removeAllArcs() { // declare a return value // boolean return_val = true; // loop until no arcs remain // while (!isEmpty()) { // remove the forward link // boolean status = false; GraphArc<TObject>* graph_arc; status = remove(graph_arc); delete graph_arc; // exit gracefully // return_val &= status; } // exit gracefully // return return_val;}// method: isAdjacent//// arguments:// GraphVertex<TObject>* vertex: (input) the vertex to find//// return: boolean flag indicating whether or not the vertex was found//// this method finds the input vertex in the adjacency list for this// vertex//template<class TObject>boolean GraphVertex<TObject>::isAdjacent(GraphVertex<TObject>* vertex_a) const { // make sure the list is not empty // if (isEmpty()) { return false; } // find the input vertex in the adjacency list by looping over all arcs // SingleLinkedNode< GraphArc<TObject> >* tmp_node = first_d; // declare temporary objects // GraphVertex<TObject>* tmp_vert = (GraphVertex<TObject>*) NULL; // loop over each element and look for the input vertex // boolean vertex_found = false; while ((tmp_node != (SingleLinkedNode< GraphArc<TObject> >*)NULL) && !vertex_found) { // get the vertex out of the node // tmp_vert = tmp_node->getItem()->getVertex(); // check to see if the found vertex is the one we are looking for // if (tmp_vert == vertex_a) { vertex_found = true; } else { tmp_node = tmp_node->getNext(); } } // exit gracefully // return vertex_found;}//---------------------------------------------------------------------------//// class-specific public methods:// vertex comparison methods////---------------------------------------------------------------------------// method: compareVertices//// arguments:// const GraphVertex<TObject>& compare_vertex: (input) the vertex to compare//// return: boolean value indicating test of equivalence//// this method compares two vertices for equivalent structure//template<class TObject>boolean GraphVertex<TObject>::compareVertices(const GraphVertex<TObject>& vertex_a) const { // declare the output variable // boolean arc_equal = false; // check for null items in either vertex // if (item_d == (TObject*)NULL) { if (vertex_a.item_d != (TObject*)NULL) { return false; } } else if (vertex_a.item_d == (TObject*)NULL) { return false; } // make sure that the vertices have the same number of arcs // if (this->length() != vertex_a.length()) { // if the lengths are different then the number of arcs in the // vertex are different // return false; } // save the current states // SingleLinkedNode< GraphArc<TObject> >* temp_curr1 = this->curr_d; SingleLinkedNode< GraphArc<TObject> >* temp_curr2 = vertex_a.curr_d; // iterate over all arcs contained in the current vertex // boolean more_nodes = const_cast< GraphVertex<TObject>* >(this)->gotoFirst(); while (more_nodes) { // get the current arc associated with the vertex // arc_equal = false; GraphArc<TObject>* this_arc = const_cast< GraphVertex<TObject>* > (this)->getCurr(); // iterate over all arcs associated with the input vertex // boolean more_nodes1 = const_cast< GraphVertex<TObject>& >(vertex_a).gotoFirst(); while (more_nodes1) { // get the current arc associated with the vertex // GraphArc<TObject>* input_arc = const_cast< GraphVertex<TObject>& > (vertex_a).getCurr(); // check if the arcs are similar // if (this_arc->eq(*input_arc)) { arc_equal = true; } // get the next arc in the vertex // more_nodes1 = const_cast< GraphVertex<TObject>& >(vertex_a).gotoNext(); } // if none of the arcs in the input vector are similar exit // if (!arc_equal) { const_cast< GraphVertex<TObject>* >(this)->curr_d = temp_curr1; const_cast< GraphVertex<TObject>& >(vertex_a).curr_d = temp_curr2; return false; } // get the next arc in the vertex // more_nodes = const_cast< GraphVertex<TObject>* >(this)->gotoNext(); } // reset the current pointer and return true // const_cast< GraphVertex<TObject>* >(this)->curr_d = temp_curr1; const_cast< GraphVertex<TObject>& >(vertex_a).curr_d = temp_curr2; return true;}// end of include file//#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -