📄 bigraph.h
字号:
for (long i = 1; i < num_vertices - 1; i++) { // create a new graph vertex // this_sym_ptrs[i] = this->insertVertex(copy_sym_ptrs[i]->getItem()); } // now build the arclists for each node // for (long i = 0; i < num_vertices; i++) { // get the vertex in question // GraphVertex<TObject>* tmp_copy_vert = copy_sym_ptrs[i]; // loop over the copy graph's arclist // boolean arcs_remain = tmp_copy_vert->gotoFirst(); while (arcs_remain) { // setup temporary variables // long dest_index = -1; GraphArc<TObject>* tmp_arc = tmp_copy_vert->getCurr(); GraphVertex<TObject>* dest_vertex = tmp_arc->getVertex(); // find the destination vertex index // for (long j = 0; j < num_vertices; j++) { if (dest_vertex == copy_sym_ptrs[j]) { dest_index = j; break; } } if (dest_index == -1) { return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__); } // link the vertices in the new graph // insertArc(this_sym_ptrs[i], this_sym_ptrs[dest_index], tmp_arc->getEpsilon(), tmp_arc->getWeight()); // goto the next arc // arcs_remain = tmp_copy_vert->gotoNext(); } } // put the copy graph back in its original state // const_cast<DiGraph<TObject>& >(arg_a).gotoMark(); // exit gracefully // return true;}// method: assign//// arguments:// DoubleLinkedList<TObject>& data: (input) list of vertex data elements// DoubleLinkedList<TopoTriple> arcs: (input) list of arcs between vertices//// return: a boolean value indicating status//// this is a quick assign method for the BiGraph class. this method takes// a ordered list of data elements for the graph vertices and a list of// of arcs that connects the vertices in the graphs.//template<class TObject>boolean BiGraph<TObject>::assign(DoubleLinkedList<TObject>& data_a, DoubleLinkedList<TopoTriple>& arcs_a) { // declare local variables // long src = 0; long dst = 0; float weight = 0.0; boolean epsilon = false; // determine if we need to do any work // if (data_a.length() == 0) { return Error::handle(name(), L"assign", Error::ARG, __FILE__, __LINE__); } // before we assign we need to clear the current graph // this->clear(Integral::RESET); // create vertices for the input data // BiGraphVertex<TObject>* vertices[data_a.length()]; for (int i = 0; i < data_a.length(); i++) { data_a.gotoPosition(i); vertices[i] = insertVertex(data_a.getCurr()); } // loop over all elements in the arc list // for (boolean more = arcs_a.gotoFirst(); more; more = arcs_a.gotoNext()) { // define the source and destination objects // BiGraphVertex<TObject>* src_vertex; BiGraphVertex<TObject>* dst_vertex; // determine the parameters of the arc // weight = (float)arcs_a.getCurr()->second(); epsilon = (boolean)arcs_a.getCurr()->third(); src = (long)arcs_a.getCurr()->first().first(); dst = (long)arcs_a.getCurr()->first().second(); // check if the source is the start vertex // if (src == START_INDEX) { src_vertex = getStart(); } // check if the source is the term vertex // else if (src == TERM_INDEX) { src_vertex = getTerm(); } else { src_vertex = vertices[src]; } // check if the destination is the term vertex // if (dst == TERM_INDEX) { dst_vertex = getTerm(); } // check if the destination is the start vertex // else if (dst == START_INDEX) { dst_vertex = getStart(); } else { dst_vertex = vertices[dst]; } // insert an arc from the source to the destination vertices // insertArc(src_vertex, dst_vertex, epsilon, weight); } // exit gracefully // return true;}//------------------------------------------------------------------------//// required i/o methods////------------------------------------------------------------------------// method: sofSize//// arguments: none//// return: size of object as written to disk via the i/o methods//// this method determines the size of the object on disk. it has to// nearly go through as much trouble as writing the object to// determine the exact binary size.//template<class TObject>long BiGraph<TObject>::sofSize() const { // declare lists to read the graph structure // DoubleLinkedList<TObject> dlist(USER); DoubleLinkedList<TopoTriple> alist; // start with the length // long bytes = is_weighted_d.sofSize(); // get the structure of the graph // this->get(dlist, alist); // add the lengths of the lists // bytes += dlist.sofSize(); bytes += alist.sofSize(); // return the size // return bytes;}// method: read//// arguments:// Sof& sof: (input) sof file object// long tag: (input) sof object instance tag// const String& name: (input) sof object instance name//// return: a boolean value indicating status//// this method has the object read itself from an Sof file//template<class TObject>boolean BiGraph<TObject>::read(Sof& sof_a, long tag_a, const String& name_a) { // get the instance of the object from the Sof file // if (!sof_a.find(name_a, tag_a)) { return false; } // read the actual data from the sof file // if (!readData(sof_a)) { return false; } // exit gracefully // return true;}// method: write//// arguments:// Sof& sof: (input) sof file object// long tag: (input) sof object instance tag// const String& name: (input) sof object instance name//// return: a boolean value indicating status//// this method has the object write itself to an Sof file//template<class TObject>boolean BiGraph<TObject>::write(Sof& sof_a, long tag_a, const String& name_a) const { // declare a temporary size variable // long obj_size = 0; // switch on ascii or binary mode // if (sof_a.isText()) { // set the size to be dynamic // obj_size = Sof::ANY_SIZE; } else { // the size of the binary data to write // obj_size = sofSize(); } // write the object into the sof file's index // if (!sof_a.put(name_a, tag_a, obj_size)) { return false; } // exit gracefully // return writeData(sof_a);}// method: readDataText//// arguments:// Sof& sof: (input) sof file object// const String& pname: (input) parameter name// long size: (input) size of the object// boolean param: (input) is the parameter specified?// boolean nested: (input) is this nested?//// return: a boolean value indicating status//// this method has the object read itself from an Sof file. it assumes// that the Sof file is already positioned correctly.//template<class TObject>boolean BiGraph<TObject>::readDataText(Sof& sof_a, const String& pname_a, long size_a, boolean param_a, boolean nested_a) { // declare local variables // String index_str; // first cleanup the list // if (!clear(Integral::RESET)) { return Error::handle(name(), L"readDataText", Error::READ, __FILE__, __LINE__, Error::WARNING); } // local variables // SofParser parser; parser.setDebug(debug_level_d); // are we nested? // if (nested_a) { parser.setNest(); } // load the parse // if (!parser.load(sof_a, size_a)) { return Error::handle(name(), L"readDataText", Error::READ, __FILE__, __LINE__, Error::WARNING); } // read the two flags // if (!is_weighted_d.readData(sof_a, PARAM_WEIGHTED, parser.getEntry(sof_a, PARAM_WEIGHTED))) { return Error::handle(name(), L"readDataText", Error::READ, __FILE__, __LINE__, Error::WARNING); } if (debug_level_d >= Integral::ALL) { is_weighted_d.debug(L"is this weighted?"); } // create a hash table so that we can access the graph vertices // via their indeces i.e., their positions in the graph // ReadHash hash_table(USER); // read in the vertex data // if (!readVertexDataText(sof_a, parser, hash_table)) { return Error::handle(name(), L"readDataText", Error::ARG, __FILE__, __LINE__); } // read in the arc data // if (!readArcDataText(sof_a, parser, hash_table)) { return Error::handle(name(), L"readDataText", Error::ARG, __FILE__, __LINE__); } // exit gracefully // return true;}// method: readData//// arguments:// Sof& sof: (input) sof file object// const String& pname: (input) parameter name// long size: (input) size of the object// boolean param: (input) is the parameter specified?// boolean nested: (input) is this nested?//// return: a boolean value indicating status//// this method has the object read itself from an Sof file. it assumes// that the Sof file is already positioned correctly.//template<class TObject>boolean BiGraph<TObject>::readData(Sof& sof_a, const String& pname_a, long size_a, boolean param_a, boolean nested_a) { // when we are reading text data // if (sof_a.isText()) { return readDataText(sof_a, pname_a, size_a, param_a, nested_a); } // when we are reading binary data // else { return readDataBinary(sof_a); }}// method: writeData//// arguments:// Sof& sof: (input) sof file object// const String& pname: (input) parameter name//// 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>::writeData(Sof& sof_a, const String& pname_a) const { // declare local variables and structures to read the graph // boolean status = false; DoubleLinkedList<TObject> dlist(USER); DoubleLinkedList<TopoTriple> alist; // read the current structure of the graph // this->get(dlist, alist); // when we are writing text data // if (sof_a.isText()) { status = writeDataText(sof_a, pname_a, dlist, alist); } // when we are writing binary data // else { status = writeDataBinary(sof_a, pname_a, dlist, alist); } // exit gracefully // return status;}// method: readDataBinary//// arguments:// Sof& sof: (input) sof file object//// return: a boolean value indicating status//// this method has the object read itself from an Sof file. it assumes// that the Sof file is already positioned correctly.//template<class TObject>boolean BiGraph<TObject>::readDataBinary(Sof& sof_a) { // declare some lists to read the data from // DoubleLinkedList<TObject> dlist; DoubleLinkedList<TopoTriple> alist; // first cleanup the list // if (!clear(Integral::RESET)) { return Error::handle(name(), L"readDataBinary", Error::ARG, __FILE__, __LINE__); } // read the list graph vertex elements // dlist.readData(sof_a); // read the list of graph arcs and weights // alist.readData(sof_a); // create a graph using the list of vertex elements and arcs // this->assign(dlist, alist); // exit gracefully // return true;}// method: writeDataText//// 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>::writeDataText(Sof& sof_a, const String& pname_a, DoubleLinkedList<TObject>& data_a, DoubleLinkedList<TopoTriple>& arcs_a) const { // write a start string if necessary // sof_a.writeLabelPrefix(pname_a);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -