📄 _dl_itr.cpp
字号:
DL_Iter<int>* a_listiter=new DL_Iter<int>(IntegerList); DL_Iter<int>* a_secondlistiter=new DL_Iter<int>(a_listiter);\endcode\param otheriter other iterator on same list*/template <class Dtype>DL_Iter<Dtype>:: DL_Iter(DL_Iter* otheriter){ if (otheriter->_current==0) Error("DL_Iter(otheriter)",NO_LIST_OTHER); _list=otheriter->_list; _list->_iterlevel++; // add 1 to DL_Iters on List _current=otheriter->_current;}/*!This constructs an iterator for a list of a given type, the list does not have to exist.Later on when a list is constructed,the iterator can be attached to it.This way an iterator to a specific list can be made static to a class, and can be usedfor several lists at the same time. \ntcarg: class | Dtype | list item object\par Example How to construct an iterator, without having a list first. This constructs an iterator for a list of the given type, but the list thus not yet exist.\code DL_Iter<int>* a_iter=new DL_Iter<int>(); DL_List<int>* IntegerList; IntegerList = new DL_List<int>(); a_iter.Attach(IntegerList); a_iter.insend(123); a_iter.Detach();\endcode*/template <class Dtype>DL_Iter<Dtype>:: DL_Iter():_list(0), _current(0){}/*!destruct an iterator for a list of a given type.*/template <class Dtype>DL_Iter<Dtype>::~DL_Iter(){ if (_current==0) return; _list->_iterlevel--; // decrease iterators if (_list->_iterlevel < 0) Error("~DL_Iter()",ITER_NEG);}/*!This attaches an iterator to a list of a given type, the list must exist.This way an iterator to a specific list can be madestatic to a class, and can be used for several lists at the same time.\n!tcarg: class | Dtype | list item object\par Example How to construct an iterator, without having a list first, and attach an iterator later:|\codeDL_Iter<int>* a_iter=new DL_Iter<int>();DL_List<int>* IntegerList;IntegerList = new DL_List<int>();a_iter.Attach(IntegerList);a_iter.insend(123);a_iter.Detach();\endcode\param newlist the list to attached the iterator to*/template <class Dtype>void DL_Iter<Dtype>::Attach(DL_List<Dtype>* newlist){ if (_current!=0) Error("Attach(list)",NOT_ALLOW); _list=newlist; _current=HD; _list->_iterlevel++; // add 1 to DL_Iters on list}/*!This detaches an iterator from a list of a given type, the list must exist.This way an iterator to a specific list can be made static to a class,and can be used for several lists at the same time. \n!tcarg: class | Dtype | list item object\par Example:How to construct an iterator, without having a list first, and attach an iterator later:\codeDL_Iter<int>* a_iter=new DL_Iter<int>();DL_List<int>* IntegerList;IntegerList = new DL_List<int>();a_iter.Attach(IntegerList);a_iter.insend(123);a_iter.Detach();\endcode\param newlist: the list to attached the iterator to*/template <class Dtype>void DL_Iter<Dtype>::Detach(){ if (_current==0) Error("Attach()",NO_LIST); _list->_iterlevel--; // subtract 1 from DL_Iters on list _list=0; _current=0;}/*// copy pointers to items from other listtemplate <class Dtype> void DL_Iter<Dtype>::merge(DL_List<Dtype>* otherlist){ DL_Node* node=otherlist->HD; //can be 0 if empty for(int i=0; i<otherlist->NB; i++) { insend(node->new_item); // insert item at end node=node->_next; // next item of otherlist }}*//*//call Dtype::mfp for each itemtemplate <class Dtype>void DL_Iter<Dtype>::foreach_mf(void (Dtype::*mfp)()){ DL_Node<Dtype>* node=HD; //can be 0 if empty for(int i=0; i< NB; i++) { ((node->_item).*mfp)(); node=node->_next; }}*//*! call given function for each item*/template <class Dtype>void DL_Iter<Dtype>::foreach_f(void (*fp) (Dtype n) ){ DL_Node<Dtype>* node=HD; //can be 0 if empty for(int i=0; i< NB; i++) { fp (node->_item); node=node->_next; }}/*!to move all objects in a list to the list of the iterator.\note The iterator level must be one to be able to use this function, else an error will be generated\note The list may not be the same list as the iterator list\par Example to take over all items in _intlist|\codeDL_List<int> _intlist; #create a list of integersDL_List<int> _intlist2; #create a list of integersint a=123;DL_Iter<int>* a_listiter2=new DL_Iter<int>(&_intlist2);_intlist->insend(a) // insert at enda_listiter2->takeover(_intlist)\endcode\param otherlist the list to take the items from*/template <class Dtype>void DL_Iter<Dtype>::takeover(DL_List<Dtype>* otherlist){ if (_current==0) Error("takeover(DL_List*)",NO_LIST); // no iterators allowed on otherlist if (otherlist->_iterlevel > 0) Error("takeover(DL_List*)",AC_ITER_LIST_OTHER); // otherlist not this list else if (otherlist == _list) Error("takeover(DL_List*)",SAME_LIST); if (otherlist->_nbitems == 0) return; //link other list into this list at the end TL->_next=otherlist->_root->_next; otherlist->_root->_next->_prev=TL; otherlist->_root->_prev->_next=RT; TL=otherlist->_root->_prev; //empty other list NB+=otherlist->_nbitems; otherlist->_nbitems=0; otherlist->_root->_next=otherlist->_root; otherlist->_root->_prev=otherlist->_root;}/*!to move all objects in a list (using iterator of that list) to the list of the iterator.\note The iterator level for both iterators must be one to be able to use this function,\note else an error will be generated\note The list may not be the same list as the iterator list\par Example to take over all items in a_listiter1 it's list|\codeDL_List<int> _intlist; #create a list of integersDL_List<int> _intlist2; #create a list of integersint a=123;DL_Iter<int>* a_listiter1=new DL_Iter<int>(&_intlist);DL_Iter<int>* a_listiter2=new DL_Iter<int>(&_intlist2);a_listiter1->insend(a) // insert at enda_listiter2->takeover(a_listiter1)\\!to move all objects in a list (using iterator of that list) to the list of the iterator\endcode\param otheriter: the iterator to take the items from*/template <class Dtype>void DL_Iter<Dtype>::takeover(DL_Iter* otheriter){ if (otheriter->_current==0) Error(" DL_Iter",NO_LIST_OTHER); if (_current==0) Error(" DL_Iter",NO_LIST); // only one iterator allowed on other list? if (otheriter->_list->_iterlevel > 1) Error("takeover(DL_Iter*)",AC_ITER_LIST_OTHER); // otherlist not this list? else if (otheriter->_list == _list) Error("takeover(DL_Iter*)",SAME_LIST); if (otheriter->NB == 0) return; //link other list into this list at the end TL->_next=otheriter->HD; otheriter->HD->_prev=TL; otheriter->TL->_next=RT; TL=otheriter->TL; //empty other iter & list NB+=otheriter->NB; otheriter->NB=0; otheriter->HD=otheriter->RT; otheriter->TL=otheriter->RT; otheriter->_current=otheriter->RT;}/*!to move maxcount objects in a list (using iterator of that list)to the list of the iterator.\note The iterator level for both iterators must be one to be able to use this function, else an error will be generated\note The list may not be the same list as the iterator list\note If less then maxcount objects are available in the source iterator, all of them are taken and no error will accur\par Example to take over 1 item from a_listiter1 it's list\codeDL_List<int> _intlist; #create a list of integersDL_List<int> _intlist2; #create a list of integersint a=123;DL_Iter<int>* a_listiter1=new DL_Iter<int>(&_intlist);DL_Iter<int>* a_listiter2=new DL_Iter<int>(&_intlist2);a_listiter1->insend(a) // insert at enda_listiter2->takeover(a_listiter1,1);//! to move maxcount objects in a list (using iterator of that list) to the list of the iterator\endcode\param otheriter the iterator to take the items from\param maxcount maximum number of objects to take over*/template <class Dtype>void DL_Iter<Dtype>::takeover(DL_Iter* otheriter, int maxcount){ if (otheriter->_current==0) Error("takeover(DL_Iter*,int)",NO_LIST_OTHER); if (_current==0) Error("takeover(DL_Iter*,int)",NO_LIST); if (otheriter->_list->_iterlevel > 1) Error("takeover(DL_Iter*,int)",AC_ITER_LIST_OTHER); else if (otheriter->_list == _list) Error("takeover(DL_Iter*,int)",SAME_LIST); if (maxcount<0) Error("takeover(DL_Iter*,int), maxcount < 0",NO_MES); if (otheriter->NB == 0) return; if (otheriter->NB <= maxcount) { //take it all //link other list into this list at the end TL->_next=otheriter->HD; otheriter->HD->_prev=TL; otheriter->TL->_next=RT; TL=otheriter->TL; //empty other iter & list NB+=otheriter->NB; otheriter->NB=0; otheriter->HD=otheriter->RT; otheriter->TL=otheriter->RT; otheriter->_current=otheriter->RT; } else { //take maxcount elements from otheriter //set cursor in otherlist to element maxcount DL_Node<Dtype>* node; if (NB/2 < maxcount) { // this is faster (1st half) node=otheriter->HD; for(int i=1; i<maxcount; i++) node=node->_next; } else { // no, this is faster (2nd half) node=otheriter->TL; for(int i=NB; i>maxcount+1; i--) node=node->_prev; } // link this->tail to other->head if (NB>0) { TL->_next=otheriter->HD; otheriter->HD->_prev=TL; } else // target is empty { HD=otheriter->HD; otheriter->HD->_prev=RT; } // set other root to node-> next (after last to copy) otheriter->HD=node->_next; otheriter->HD->_prev=otheriter->RT; // set this->tail to other->item()->prev (last element to be copied) TL=node; node->_next=RT; // still need to update element counter NB+=maxcount; // update other list otheriter->NB-=maxcount; otheriter->_current=otheriter->HD; // other->current is moved to this! }}/*!put the iterator root object before the current iterator position in the list.The current object will become the new head of the list.\note The iterator level must be one to be able to use this function,else an error will be generated\par Example move the root object to make the new head the old tail object|\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->totail();a_listiter->reset_head();a_listiter->tohead(); //the new head will be at object 3456\endcode*/template <class Dtype>void DL_Iter<Dtype>::reset_head(){ if (_current==0) Error("reset_head()",NO_LIST); if (_list->_iterlevel > 1 ) Error("reset_head()",ITER_GT_1); if(_current==RT) Error("reset head()",ITER_HITROOT); //link out RT HD->_prev=TL; TL->_next=HD; //link in RT before current HD=_current; TL=_current->_prev; TL->_next=RT; HD->_prev=RT;}/*!put the iterator root object after the current iterator position in the list.The current object will become the new tail of the list.\note The iterator level must be one to be able to use this function, else an error will be generated\par Example move the root object to make the new tail the old head object\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->tohead();a_listiter->reset_tail();a_listiter->totail(); //the new tail will be at object 1234\endcode*/template <class Dtype>void DL_Iter<Dtype>::reset_tail(){ if (_current==0) Error("reset_tail()",NO_LIST); if (_list->_iterlevel > 1 ) Error("reset_tail()",ITER_GT_1); if(_current==RT) Error("reset head()",ITER_HITROOT); //link out RT HD->_prev=TL; TL->_next=HD; //link in RT after current TL=_current; HD=_current->_next; HD->_prev=RT; TL->_next=RT;}/*!is list empty (contains items or not)?\return returns true is list is empty else false\par exmaple: too see if list is empty\codeDL_List<int> _intlist; #create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);if (a_listiter->Empty()) cout << "empty"\endcode*/template <class Dtype>bool DL_Iter<Dtype>::empty(){ if (_current==0) Error("empty()",NO_LIST); return(bool)(NB==0);}/*!is the iterator at the root of the list.\note Traversing the list in a certain direction using a while loop,the end can be tested with this function.\return returns true if the iterator is at the root of the list (after the last/tail/head object), else false.\par example: to traverse in both directions|\codeDL_List <int> _intlist; #create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->tohead();//traverse forwardswhile ( ! a_listiter->hitroot()){ cout << "The item =" << a_listiter->item(); a_listiter++; //goto next object}a_listiter->totail();//traverse backwardswhile ( ! a_listiter->hitroot()){ cout << "The item =" << a_listiter->item(); a_listiter--; //goto next object}\endcode*/template <class Dtype>bool DL_Iter<Dtype>::hitroot(){ if (_current==0) Error("hitroot()",NO_LIST); return(bool)(_current == RT);}/*!is the iterator at the head of the list.\return returns true if the iterator is at the head object of the list, else false.\par exmaple: too see the object at the head\codeDL_List <int> _intlist; #create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->tohead();if (a_listiter->athead()) cout << "at the head The item =" << a_listiter->item();\endcode*/template <class Dtype>bool DL_Iter<Dtype>::athead(){ if (_current==0) Error("athead()",NO_LIST); return(bool)(_current == HD);}/*!is the iterator at the tail of the list.\return returns true if the iterator is at the tail object of the list, else false.\par Example too see the object at the tail|\codeDL_List <int> _intlist; #create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);a_listiter->totail();if (a_listiter->attail()) cout << "at the tail The item =" << a_listiter->item();\endcode*/template <class Dtype>bool DL_Iter<Dtype>::attail(){ if (_current==0) Error("attail()",NO_LIST); return(bool)(_current == TL);}/*!does the iterator/list contain the given object\return returns true if the iterator/list contains the given object in the list, else false.\par Example too see if the object is already in the list\codeDL_List <int> _intlist; #create a list of integersDL_Iter<int>* a_listiter=new DL_Iter<int>(&_intlist);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -