📄 doublelinkedlist.h
字号:
// method: setAllocationMode // boolean setAllocationMode(ALLOCATION alloc) { alloc_d = alloc; return true; } //--------------------------------------------------------------------------- // // private methods // //---------------------------------------------------------------------------private: // sort methods // boolean randQuickSort(Integral::ORDER sort_order); boolean insertionSort(Integral::ORDER sort_order); // swap methods: // exchange two nodes in the list, this method assume the i_node // and j_node are in the list and i_node is in front of j_node // the avoids the overhead of checking and is used inside sort // boolean swap(NODE*& i_node, NODE*& j_node); // node addressing methods // NODE* getNode(long index) const; // friend class // template <class TObject_diagnose> friend class DoubleLinkedListDiagnose; };//-----------------------------------------------------------------------------//// we define non-integral constants at the end of class definition for// templates (for non-templates these are defined in the default constructor)// //-----------------------------------------------------------------------------// constants: required constants such as the class name//template <class TObject>const String DoubleLinkedList<TObject>::CLASS_NAME(L"DoubleLinkedList");template <class TObject>const String DoubleLinkedList<TObject>::DEF_PARAM(L"values");// static instantiations: debug level and memory manager//template <class TObject>Integral::DEBUG DoubleLinkedList<TObject>::debug_level_d = Integral::NONE;template <class TObject>MemoryManager DoubleLinkedList<TObject>::mgr_d(sizeof(DoubleLinkedList<TObject>), CLASS_NAME);// below are all the methods for the DoubleLinkedList template class////----------------------------------------------------------------------//// required static methods////----------------------------------------------------------------------// method: name//// arguments: none//// return: a static String& containing the class name//// this method returns the class name//template<class TObject>const String& DoubleLinkedList<TObject>::name() { // create the static name string for this class and return it // static String cname(CLASS_NAME); cname.clear(); cname.concat(CLASS_NAME); cname.concat(L"<"); cname.concat(TObject::name()); cname.concat(L">"); // return the name // return cname;}//----------------------------------------------------------------------//// required debug methods////----------------------------------------------------------------------// method: debug//// arguments:// const unichar* message: (input) information message//// return: a boolean value indicating status//// this method dumps the contents of an object to the console// template<class TObject>boolean DoubleLinkedList<TObject>::debug(const unichar* message_a) const { // declare local variables // String output; String value; String extras; // print out the length // value.assign((long)length_d); output.debugStr(name(), message_a, L"length_d", value); Console::put(output); // print out the memory allocation mode // output.debugStr(name(), message_a, L"alloc_d", NameMap::ALLOCATION_MAP((long)alloc_d)); Console::put(output); // print out the debug level // value.assign(debug_level_d); output.debugStr(name(), message_a, L"debug_level_d", value); Console::put(output); NODE* temp_curr = curr_d; // loop over each node and print its value // const_cast< DoubleLinkedList<TObject>* >(this)->gotoFirst(); for (long i = 0; i < (long)length_d; i++) { extras.clear(); if (curr_d == first_d) { extras.concat(L", "); extras.concat(L"first"); } if (curr_d == last_d) { extras.concat(L", "); extras.concat(L"last"); } if (curr_d == mark_d) { extras.concat(L", "); extras.concat(L"mark"); } if (curr_d == temp_curr) { extras.concat(L", "); extras.concat(L"current"); } // trim extra , character and clear the string // extras.trimLeft(L", "); output.clear(); if (extras.length() > 0) { output.debugStr(name(), message_a, L"node", extras); } else { output.debugStr(name(), message_a, L"node"); } Console::put(output); // call the debug method of the element // Console::increaseIndention(); curr_d->debug(message_a); Console::decreaseIndention(); // go to next node // const_cast< DoubleLinkedList<TObject>* >(this)->gotoNext(); } // restore the current node // const_cast< DoubleLinkedList<TObject>* >(this)->curr_d = temp_curr; // exit gracefully // return true;}//------------------------------------------------------------------------//// required destructor/constructor(s)////-----------------------------------------------------------------------// method: default constructor//// arguments:// ALLOCATION alloc: (input) allocation mode for the list//// return: none//template<class TObject>DoubleLinkedList<TObject>::DoubleLinkedList(ALLOCATION alloc_a) { // initialize the pointers to the first, last, current and the marked nodes // first_d = (NODE*)NULL; last_d = (NODE*)NULL; curr_d = (NODE*)NULL; mark_d = (NODE*)NULL; // initialize memory allocation flag // alloc_d = alloc_a; // initialize the length // length_d = (long)0; // exit gracefully //}// method: copy constructor//// arguments:// DoubleLinkedList<TObject>& copy_list: (input) the list to copy//// return: none//template<class TObject>DoubleLinkedList<TObject>::DoubleLinkedList(const DoubleLinkedList<TObject>& copy_list_a) { // initialize the pointers to the first, last, current and the marked nodes // first_d = (NODE*)NULL; last_d = (NODE*)NULL; curr_d = (NODE*)NULL; mark_d = (NODE*)NULL; // copy the memory allocation flag // alloc_d = copy_list_a.alloc_d; // initialize the length // length_d = (long)0; // call the assign method to copy the list // assign(copy_list_a);}//------------------------------------------------------------------------//// required assign methods////-------------------------------------------------------------------------// method: assign//// arguments:// const DoubleLinkedList<TObject>& copy_list: (input) the list to copy//// return: a boolean value indicating status//// this method copies the contents of the input to this list//template<class TObject>booleanDoubleLinkedList<TObject>::assign(const DoubleLinkedList<TObject>& copy_list_a) { // clear this list // clear(); // store the state of the copy list // NODE* tmp_node = copy_list_a.first_d; // create a variable to store the current node pointer // NODE* tmp_curr = (NODE*)NULL; // loop over each element and insert each element onto the new // list. note that we don't use the standard gotoFirst, gotoNext // methods so that we can easily copy over the exact state of the // given list (including the current pointer) // while (tmp_node != (NODE*)NULL) { // add the next element to the list // insert(tmp_node->getItem()); // see if this is the marked element // if (tmp_node == copy_list_a.mark_d) { // set the marked element for this list // setMark(); } // see if this is the current element // if (tmp_node == copy_list_a.curr_d) { // set the marked element for this list // tmp_curr = curr_d; } // move to the next node // tmp_node = tmp_node->getNext(); } // set curr_d // curr_d = tmp_curr; // the length should be set by the inserts // if (length_d != copy_list_a.length_d) { return Error::handle(name(), L"assign", ERR, __FILE__, __LINE__); } // 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//template<class TObject>long DoubleLinkedList<TObject>::sofSize() const { // declare temporary variables // NODE* tmp_node = first_d; long tmp_size = 0; // count the size of length_d // tmp_size = length_d.sofSize(); // loop over each node and add the size of that node. this is the // total size of the list // while (tmp_node != (NODE*)NULL) { // add the size of this node // tmp_size += tmp_node->sofSize(); // move to the next node // tmp_node = tmp_node->getNext(); } // return the size // return tmp_size;}// 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 DoubleLinkedList<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 DoubleLinkedList<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(); } // put 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: 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 DoubleLinkedList<TObject>::readData(Sof& sof_a, const String& pname_a, long size_a, boolean param_a, boolean nested_a) { // first clear the list // if (!clear()) { return Error::handle(name(), L"readData", Error::READ, __FILE__, __LINE__, Error::WARNING); } // local variables // SofParser parser; String pname; // set the parser debug level // parser.setDebug(debug_level_d); // if param is false, this means implicit parameter // if (!param_a) { parser.setImplicitParam(); pname.assign(parser.implicitPname()); } else { pname.assign(pname_a); } // are we nested? // if (nested_a) { parser.setNest(); } // load the parse // if (!parser.load(sof_a, size_a)) { return Error::handle(name(), L"readData", Error::READ, __FILE__, __LINE__, Error::WARNING); } Long new_size((long)0); // read the length first: this differs for text or binary // if (sof_a.isText()) { new_size = parser.countTokens(pname); } // binary mode // else { if (!new_size.readData(sof_a, pname)) { return false; } } // change to USER allocation mode, but save the old state // ALLOCATION alloc = alloc_d; alloc_d = USER; for (long i = 0; i < new_size; i++) { // we need to new an element // TObject* new_obj = new TObject(); // read the node // if (!new_obj->readData(sof_a, pname, parser.getEntry(sof_a, pname, i, 1), false, true)) { alloc_d = alloc; return Error::handle(name(), L"readData", Error::READ, __FILE__, __LINE__, Error::WARNING); } // insert the node // if (!insert(new_obj)) { alloc_d = alloc; return Error::handle(name(), L"readData", Error::READ, __FILE__, __LINE__, Error::WARNING); } } // restore the allocation mode // alloc_d = alloc; // exit gracefully //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -