📄 bigraph.h
字号:
// first write the flags // is_weighted_d.writeData(sof_a, PARAM_WEIGHTED); // first write the vertices data (this includes subgraph indexing) // writeVertexDataText(sof_a, data_a); // now write the arcs // writeArcDataText(sof_a, arcs_a); // put an end string if necessary // sof_a.writeLabelSuffix(pname_a); // exit gracefully // return true;}// method: writeDataBinary//// arguments:// Sof& sof: (input) sof file object// const String& pname: (input) parameter name// DoubleLinkedList<TObject>& dlist: (input) graph vertex elements// DoubleLinkedList<TopoTriple>& alist: (input) graph topology//// return: a boolean value indicating status//// this method writes the object to the Sof file. it assumes that the// Sof file is already positioned correctly.//template<class TObject>boolean BiGraph<TObject>::writeDataBinary(Sof& sof_a, const String& pname_a, DoubleLinkedList<TObject>& data_a, DoubleLinkedList<TopoTriple>& arcs_a) const { // write the graph vertex elements // data_a.writeData(sof_a, pname_a); // write the graph arcs and weights // arcs_a.writeData(sof_a, pname_a); // exit gracefully // return true;}//-------------------------------------------------------------------------//// required equality method////-------------------------------------------------------------------------// method: eq//// arguments:// const BiGraph<TObject>& graph: (input) the graph to compare//// return: boolean value indicating test of equivalence//// this method compares two graph to see if they are equal. it can't// just call list-equality since all vertex pointers will be// different.//template<class TObject>boolean BiGraph<TObject>::eq(const BiGraph<TObject>& graph_a) const { // create lists to extract the graph structures // DoubleLinkedList<TObject> this_dlist; DoubleLinkedList<TopoTriple> this_alist; DoubleLinkedList<TObject> input_dlist; DoubleLinkedList<TopoTriple> input_alist; // extract the structure of the current list // this->get(this_dlist, this_alist); // extract the structure of the input list // graph_a.get(input_dlist, input_alist); // compare the lists for quality // if (!this_dlist.eq(input_dlist)) { return false; } if (!this_alist.eq(input_alist)) { return false; } // we have reached this far so the graphs have to be equal // return true;}//-------------------------------------------------------------------------//// required clear methods////-------------------------------------------------------------------------// method: clear//// arguments:// Integral::CMODE cmode_a: (input) clear mode//// return: a boolean value indicating status//// this method clears the reference to the internal data.// template<class TObject>boolean BiGraph<TObject>::clear(Integral::CMODE cmode_a) { // for RETAIN mode, call clear on every object but do not change // topology. for FREE mode we also need to call clear(FREE) on all // TObjects, but we will also delete the topology below. // if ((cmode_a == Integral::RETAIN) || (cmode_a == Integral::FREE)) { for (boolean more = gotoFirst(); more; more = gotoNext()) { ((TObject*)(getCurr()->getItem()))->clear(cmode_a); } } // for RETAIN mode we are done, make no changes to topology // if (cmode_a == Integral::RETAIN) { return true; } // remove all of the vertices from the start and term vertices // start_vertex_d->removeAllArcsChild(); term_vertex_d->removeAllArcsParent(); // when the graph is in SYSTEM-allocation mode we need to iterate over // all vertices in the graph and delete the objects // if ((getAllocationMode() == SYSTEM) || (cmode_a == Integral::FREE)) { for (boolean more = gotoFirst(); more; more = gotoNext()) { // delete the TObject // delete getCurr()->getItem(); // set the pointer to null // ((BiGraphVertex<TObject>*)getCurr())->setItem((TObject*)NULL); } } // clear the vertex list // DoubleLinkedList< BiGraphVertex<TObject> >::setAllocationMode(SYSTEM); DoubleLinkedList< BiGraphVertex<TObject> >::clear(Integral::RESET); DoubleLinkedList< BiGraphVertex<TObject> >::setAllocationMode(USER); // exit gracefully // return true;}//---------------------------------------------------------------------------//// class-specific public methods:// insert/remove arc methods////---------------------------------------------------------------------------// method: insertArc//// arguments:// BiGraphVertex<TObject>* start_vertex: (input) the start vertex// BiGraphVertex<TObject>* end_vertex: (input) the ending vertex// boolean is_epsilon: (input) is the arc an epsilon transition// float weight: (input) the weight on the arc (if any)// // return: a boolean value indicating status//// this method inserts an arc between two vertices in the graph provided// that the two vertices are already present in the graph//template<class TObject>boolean BiGraph<TObject>::insertArc(BiGraphVertex<TObject>* start_vertex_a, BiGraphVertex<TObject>* end_vertex_a, boolean is_epsilon_a, float weight_a) { // make sure neither vertex is NULL // if ((start_vertex_a == (BiGraphVertex<TObject>*)NULL) || (end_vertex_a == (BiGraphVertex<TObject>*)NULL)) { return Error::handle(name(), L"insertArc", Error::NULL_ARG, __FILE__, __LINE__); } // we don't allow connections between graphs so make sure the parent graph // for both vertices is either already this graph or is null // if (((start_vertex_a->getParentGraph() != (BiGraph<TObject>*)NULL) && (start_vertex_a->getParentGraph() != this)) || ((end_vertex_a->getParentGraph() != (BiGraph<TObject>*)NULL) && (end_vertex_a->getParentGraph() != this))) { return Error::handle(name(), L"insertArc", ERR_MULPAR, __FILE__, __LINE__); } // make sure that the start vertex and end vertex are already in the graph // if (start_vertex_a->getParentGraph() != this) { if ((start_vertex_a != start_vertex_d) && (start_vertex_a != term_vertex_d)) { return Error::handle(name(), L"insertArc", Error::ARG, __FILE__, __LINE__); } } if (end_vertex_a->getParentGraph() != this) { if ((end_vertex_a != start_vertex_d) && (end_vertex_a != term_vertex_d)) { return Error::handle(name(), L"insertArc", Error::ARG, __FILE__, __LINE__); } } // set the parent pointers regardless - this may be redundant but it is // probably no less efficient than putting an if statement around it // start_vertex_a->setParentGraph(this); end_vertex_a->setParentGraph(this); // add an arc from the start to the end in the child list // boolean return_val = start_vertex_a->insertArcChild(end_vertex_a, weight_a, is_epsilon_a); // add an arc from the end to the start in the parent list // return_val &= end_vertex_a->insertArcParent(start_vertex_a, weight_a, is_epsilon_a); // exit gracefully // return return_val;}// method: removeArc//// arguments:// BiGraphVertex<TObject>* start_vertex: (input) the start vertex// BiGraphVertex<TObject>* end_vertex: (input) the ending vertex// // return: a boolean value indicating status//// this method removes an arc between two vertices from the graph structure//template<class TObject>boolean BiGraph<TObject>::removeArc(BiGraphVertex<TObject>* start_vertex_a, BiGraphVertex<TObject>* end_vertex_a) { // make sure neither vertex is NULL // if ((start_vertex_a == (BiGraphVertex<TObject>*)NULL) || (end_vertex_a == (BiGraphVertex<TObject>*)NULL)) { return Error::handle(name(), L"removeArc", Error::NULL_ARG, __FILE__, __LINE__); } // we don't allow connections between graphs so make sure the parent graph // for both vertices is either already this graph or is null // if (((start_vertex_a->getParentGraph() != (BiGraph<TObject>*)NULL) && (start_vertex_a->getParentGraph() != this)) || ((end_vertex_a->getParentGraph() != (BiGraph<TObject>*)NULL) && (end_vertex_a->getParentGraph() != this))) { return Error::handle(name(), L"removeArc", ERR_MULPAR, __FILE__, __LINE__); } // make sure that the start vertex and end vertex are already in the graph // if (start_vertex_a->getParentGraph() != this) { if ((start_vertex_a != start_vertex_d) && (start_vertex_a != term_vertex_d)) { return Error::handle(name(), L"removeArc", Error::ARG, __FILE__, __LINE__); } } if (end_vertex_a->getParentGraph() != this) { if ((end_vertex_a != start_vertex_d) && (end_vertex_a != term_vertex_d)) { return Error::handle(name(), L"removeArc", Error::ARG, __FILE__, __LINE__); } } // remove the arc from the start to the end in the child list // boolean return_val = start_vertex_a->removeArcChild(end_vertex_a); // remove the arc from the end to the start in the parent list // return_val &= end_vertex_a->removeArcParent(start_vertex_a); // exit gracefully // return return_val; }//---------------------------------------------------------------------------//// class-specific public methods:// graph property methods////---------------------------------------------------------------------------// method: get//// arguments:// DoubleLinkedList<TObject>& dlist: (input) graph vertex elements// DoubleLinkedList<TopoTriple>& alist: (input) graph topology// // return: a boolean value indicating status//// this method reads the current graph structure into two lists and a// hash table. The first list contains all data elements in the graph// vertices and the second list contains information regarding the// graph arcs.//template<class TObject>boolean BiGraph<TObject>::get(DoubleLinkedList<TObject>& data_a, DoubleLinkedList<TopoTriple>& arcs_a) const { // declare local variables and structures // Long index; Long src_pos; Long dst_pos; Float weight; Boolean epsilon(false); BiGraphArc<TObject>* arc; BiGraphVertex<TObject>* svert; BiGraphVertex<TObject>* dvert; HashTable<BiGVKey<TObject>, Long> khash; // initialize the index // index = 0; // iterate over all vertices in the graph // for (boolean more = const_cast<BiGraph<TObject>* >(this)->gotoFirst(); more; more = const_cast<BiGraph<TObject>* >(this)->gotoNext()) { // add the vertex to the dlist // data_a.insert((TObject*)(const_cast<BiGraph<TObject>* > (this)->getCurr()->getItem())); // associate a unique number with the vertex // BiGVKey<TObject> hashkey(this->getCurr()); khash.insert(hashkey, &index); // increment the vertex index // index = (long)index + 1; } // iterate over all out going arcs from the start vertex // for (boolean arcs = const_cast<BiGraphVertex<TObject>* > (start_vertex_d)->gotoFirstChild(); arcs; arcs = const_cast<BiGraphVertex<TObject>* > (start_vertex_d)->gotoNextChild()) { // retrieve the current arc // arc = start_vertex_d->getCurrChild(); // get the arc weight // weight = arc->getWeight(); // get the epsilon flag // epsilon = arc->getEpsilon(); // get the destination vertex // dvert = arc->getVertex(); // get the position of the source vertex // Long index(this->START_INDEX); src_pos = index; // get the position of the destination vertex // if (dvert == this->getStart()) { Long index(this->START_INDEX); dst_pos = index; } else if (dvert == this->getTerm()) { Long index(this->TERM_INDEX); dst_pos = index; } else { BiGVKey<TObject> dst_key(dvert); dst_pos = *(khash.get(dst_key)); } // add the parameters to the alist // Pair<Long, Long> pair(src_pos, dst_pos); TopoTriple triple(pair, weight, epsilon); arcs_a.insert(&triple); } // iterate over all vertices in the graph // for (boolean more = const_cast<BiGraph<TObject>* >(this)->gotoFirst(); more; more = const_cast<BiGraph<TObject>* >(this)->gotoNext()) { // retrieve the current source vertex // svert = (BiGraphVertex<TObject>*)const_cast<BiGraph<TObject>* > (this)->getCurr(); // iterate over all out going arcs in the current vertex // for (boolean arcs = svert->gotoFirstChild(); arcs; arcs = svert->gotoNextChild()) { // retrieve the current arc // arc = svert->getCurrChild(); // get the arc weight // weight = arc->getWeight(); // get the epsilon flag // epsilon = arc->getEpsilon(); // get the destination vertex // dvert = arc->getVertex(); // get the position of the source vertex // if (svert == this->getStart()) { Long index(this->START_INDEX); src_pos = index; } else if (svert == this->getTerm()) { Long index(this->TERM_INDEX); src_pos = index; } else { BiGVKey<TObject> src_key(svert); src_pos = *(khash.get(src_key)); } // get the position of the destination vertex // if (dvert == this->getStart()) { Long index(this->START_INDEX); dst_pos = index; } else if (dvert == this->getTerm()) { Long index(this->TERM_INDEX); dst_pos = index; } else { BiGVKey<TObject> dst_key(dvert); dst_pos = *(khash.get(dst_key)); } // add the parameters to the arcs_a // Pair<Long, Long> pair(src_pos, dst_pos);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -