📄 _dl_itr.cpp
字号:
a_listiter->insend(1234);if (a_listiter->has(1234)) cout << "yes it does";\endcode\param otheritem item to search for*/template <class Dtype>bool DL_Iter<Dtype>::has(Dtype otheritem){ if (_current==0) Error("has()",NO_LIST); DL_Node<Dtype>* node=HD; //can be 0 if empty for(int i=0; i<NB; i++) { if (node->_item == otheritem) return true; node=node->_next; } return false;}/*!number of items in list\return number of items in the list\par Example: to see if a list contains only one object\codeDL_List <int> _intlist; #create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);if (a_listiter->count() == 1) cout << "one object in list";\endcode*/template <class Dtype>int DL_Iter<Dtype>::count(){ if (_current==0) Error("count()",NO_LIST); return NB;}/*!go to first item, if list is empty goto hite\par Example set iterator to head of list\codeDL_List <int> _intlist; #create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->insend(1234);a_listiter->tohead();\endcode*/template <class Dtype>void DL_Iter<Dtype>::tohead(){ if (_current==0) Error("tohead()",NO_LIST); _current=HD;}/*!go to last item, if list is empty goto hite\par Example set iterator to tail of list\codeDL_List <int> _intlist; #create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->insend(1234);a_listiter->totail();\endcode*/template <class Dtype>void DL_Iter<Dtype>::totail(){ if (_current==0) Error("totail()",NO_LIST); _current=TL;}/*!set the iterator position to the root (empty dummy) object in the list.\par Example set iterator to root of list and iterate\codeDL_List <int> _intlist; #create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->insend(1234);a_listiter->toroot();while (a_listiter->iterate()) cout << a_listiter->item();\endcode*/template <class Dtype>void DL_Iter<Dtype>::toroot(){ if (_current==0) Error("toroot()",NO_LIST); _current=RT;}/*!set the iterator position to next object in the list ( can be the root also)(prefix).\par Examplehow to iterate backwards\codeDL_List <int> _intlist; //create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->insend(1234);a_listiter->tohead();while (!a_listiter->hitroot()){ cout << a_listiter->item(); _listiter++;}\endcode*/template <class Dtype>void DL_Iter<Dtype>::operator++(void){ if (_current==0) Error("operator++()",NO_LIST); _current=_current->_next;}/*!set the iterator position to next object in the list ( can be the root also)(prefix).\par Examplehow to iterate backwards\codeDL_List <int> _intlist; //create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->insend(1234);a_listiter->tohead();while (!a_listiter->hitroot()){ cout << a_listiter->item(); ++_listiter;}\endcode*/template <class Dtype>void DL_Iter<Dtype>::operator++(int){ if (_current==0) Error("operator++(int)",NO_LIST); _current=_current->_next;}/*!set the iterator position to previous object in the list ( can be the root also)(prefix).\par Examplehow to iterate backwards\codeDL_List <int> _intlist; //create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->insend(1234);a_listiter->totail();while (!a_listiter->hitroot()){ cout << a_listiter->item(); _listiter--;}\endcode*/template <class Dtype>void DL_Iter<Dtype>::operator--(void){ if (_current==0) Error("operator++()",NO_LIST); _current=_current->_prev;}/*!set the iterator position to previous object in the list ( can be the root also)(prefix).\par Examplehow to iterate backwards\codeDL_List <int> _intlist; //create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->insend(1234);a_listiter->totail();while (!a_listiter->hitroot()){ cout << a_listiter->item(); --_listiter;}\endcode*/template <class Dtype>void DL_Iter<Dtype>::operator--(int){ if (_current==0) Error("operator++(int)",NO_LIST); _current=_current->_prev;}/*!set the iterator position n objects in the next direction ( can be the root also).\par Example:how to set iterator 2 items forward\codeDL_List <int> _intlist; #create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->insend(1234);a_listiter->tohead();a_listiter>>2;//at root now\endcode\param n go n places forward*/template <class Dtype>void DL_Iter<Dtype>::operator>>(int n){ if (_current==0) Error("operator>>()",NO_LIST); for(int i=0; i<n; i++) _current=_current->_next;}/*!set the iterator position n objects in the previous direction ( can be the root also).\par Example: how to set iterator 2 items backwards\codeDL_List <int> _intlist; #create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->insend(1234);a_listiter->totail();a_listiter<<2;//at root now\endcode\param n go n places back*/template <class Dtype>void DL_Iter<Dtype>::operator<<(int n){ if (_current==0) Error("operator<<()",NO_LIST); for(int i=0; i<n; i++) _current=_current->_prev;}/*!put the iterator at the position of the given object in the list.\return returns true if the object was in the list, else false\par Example: goto element 2345\codeDL_List <int> _intlist; #create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->insend(1234);a_listiter->insend(2345);a_listiter->insend(3456);a_listiter->toitem(2345); template <class Dtype>\endcode*/template <class Dtype>bool DL_Iter<Dtype>::toitem(Dtype item){ if (_current==0) Error("toitem(item)",NO_LIST); DL_Node<Dtype>* node=HD; //can be 0 if empty for(int i=0; i<NB; i++) { if (node->_item == item) { _current = node; return true; } node=node->_next; } return false;}/*!put the iterator at the same position as the given iterator in the list.\par Example: goto element 2345 and let a_listiter2 point to the same position\codeDL_List <int> _intlist; #create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);DL_Iter<int>* a_listiter2=new DL_Iter<int>(&_intlist);a_listiter->insend(1234);a_listiter->insend(2345);a_listiter->insend(3456);a_listiter->toitem(2345);a_listiter2->toiter(a_listiter2);\endcode\param otheriter other iterator to let this iterator point to.*/template <class Dtype>void DL_Iter<Dtype>::toiter(DL_Iter *otheriter){ if (otheriter->_current==0) Error("toiter(otheriter)",NO_LIST); // both iters must have the same list if (_list != otheriter->_list) Error("toiter(otheriter)",NOT_SAME_LIST); _current = otheriter->_current;}/*!put the iterator at the position of the given object in the list.\note DO NOT use this function. Normally you will not be able to address the nodes in a list.\return returns true if the node was in the list, else false\param othernode a node to let this iterator point to.*/template <class Dtype>bool DL_Iter<Dtype>::tonode(DL_Node<Dtype> *othernode){ DL_Node<Dtype>* node=HD; //can be 0 if empty //node is a temporary cursor for(int i=0; i<NB; i++) { if (node == othernode) { _current = othernode; return true; } node=node->_next; } return false;}/*!advance the iterator one position in the next direction in the list.\return returns true if not at the end/root of the list else false.\note This function combines iteration and testing for the end ofthe list in one.\note Therefore we do not have to advance the iterator ourselves.\noteThe iterator is first put to the next object, before testing for the end of the list. |This is why we need to start at the root element in general usage.\par Example iterate through all the items in a list\codeDL_List <int> _intlist; #create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->insend(1234);a_listiter->insend(2345);a_listiter->insend(3456);a_listiter->tobegin(2345);while (a_listiter->iterate()){ cout << a_listiter->item(); }\endcode*/template <class Dtype>bool DL_Iter<Dtype>::iterate(void){ if (_current==0) Error("iterate()",NO_LIST); _current=_current->_next; if (_current==RT) return false; return true;}/*!To get the item at the current iterator position\return returns the object where the iterator is pointing to at the moment.\noteIf the iterator is at the root of the list an error will be generated,since there is no item there.\par Example: get the element at the head of the list|\codeDL_List <int> _intlist; //create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->insend(1234);a_listiter->tohead();int theitem=a_listiter->item();\endcode*/template <class Dtype>Dtype DL_Iter<Dtype>::item(){ if (_current==0) Error("item()",NO_LIST); if (_current==RT) Error("item()",NO_ITEM); return _current->_item;}//! get the node at iterater positiontemplate <class Dtype>DL_Node<Dtype>* DL_Iter<Dtype>::node(){ if (_current==0) Error("item()",NO_LIST); if (_current==RT) Error("item()",NO_ITEM); return _current;}/*!set the iterator position to next object in the list ( can be the root also).\note Use this function inside a new class derived from DL_Iter.*/template <class Dtype>void DL_Iter<Dtype>::next(){ if (_current==0) Error("item()",NO_LIST); _current=_current->_next;}/*!set the iterator position to next object in the list, if this would be the root object,then set the iterator at the head object\par Examplecycle the list twice\codeDL_List <int> _intlist; #create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->insend(1234);a_listiter->insend(2345);a_listiter->tohead();int count=2*a_listiter->count();while (count){ cout << a_listiter->item(); next_wrap(); count--;}\endcode*/template <class Dtype>void DL_Iter<Dtype>::next_wrap(){ if (_current==0) Error("item()",NO_LIST); _current=_current->_next; if (_current==RT) _current=_current->_next;}/*!set the iterator position to previous object in the list ( can be the root also).\note Use this function inside a new class derived from DL_Iter.*/template <class Dtype>void DL_Iter<Dtype>::prev(){ if (_current==0) Error("item()",NO_LIST); _current=_current->_prev;}/*!set the iterator position to previous object in the list, if this would be the root object,then set the iterator at the tail object\par Examplecycle the list twice\codeDL_List <int> _intlist; #create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->insend(1234);a_listiter->insend(2345);a_listiter->totail();int count=2*a_listiter->count();while (count){ cout << a_listiter->item(); prev_wrap(); count--;}\endcode*/template <class Dtype>void DL_Iter<Dtype>::prev_wrap(){ if (_current==0) Error("item()",NO_LIST); _current=_current->_prev; if (_current==RT) _current=_current->_prev;}template <class Dtype>void DL_Iter<Dtype>::remove_all(){ if (_current==0) Error("remove_all()",NO_LIST); if (_list->_iterlevel > 1 ) Error("remove_all()",ITER_GT_1); _list->_iterlevel--; _list->remove_all(); _list->_iterlevel++; _current=RT;}/*!remove object at current iterator position from the list.\note The object itself is not deleted, only removed from the list. The user is responsible for memory management.\note The iterator level must be one to be able to use this function, else an error will be generated\note The list must contain an object at the current iterator position, else an error will be generated.\par Example: to insert integer a at begin of list and remove it directly\codeDL_List<int> _intlist; #create a list of integersint a=123;DL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->insbegin(a);a_listiter->tohead();a_listiter->remove();\endcode*/template <class Dtype>void DL_Iter<Dtype>::remove(){ if (_current==0) Error("remove()",NO_LIST); if (_list->_iterlevel > 1 ) Error("remove()",ITER_GT_1); if (_current==RT) Error("remove()",ITER_HITROOT); DL_Node<Dtype>* node=_current; _current=_current->_next; node->_prev->_next = node->_next; // update forward link node->_next->_prev = node->_prev; // update backward link NB--; delete node; // delete list node}/*!remove the object at the begin of the list using an iterator\note The object itself is not deleted, only removed from the list. The user is responsible for memory management.\note The iterator level must be one to be able to use this function, else an error will be generated\note The list must contain objects, else an error will be generated.\note Use this function if an iterator is needed to do more complex things. Else use the list member functions directly.\par Example: to insert integer a at begin of list and remove it directly|\codeDL_List<int> _intlist; #create a list of integersint a=123;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -