// File: TCollection_List.gxx // Created: Thu Dec 17 16:10:17 1992 // Author: Remi LEQUETTE // // Revised: Thu Jan 17 11:40:17 1995 // By: Mireille MERCIEN #include #include //======================================================================= //function : TCollection_List //purpose : //======================================================================= TCollection_List::TCollection_List() : myFirst(NULL), myLast(NULL) {} //======================================================================= //function : TCollection_List //purpose : //======================================================================= TCollection_List::TCollection_List(const TCollection_List& Other) : myFirst(NULL), myLast(NULL) { if (!Other.IsEmpty()) { // cout << "List copied : magic constructor"<Next(); delete q; } myFirst = myLast = NULL; } //======================================================================= //function : Extent //purpose : //======================================================================= Standard_Integer TCollection_List::Extent() const { Standard_Integer Size = 0; TCollection_ListNode* p = (TCollection_ListNode*) myFirst; while (p != NULL) { Size++; p = (TCollection_ListNode*)p->Next(); } return Size; } //======================================================================= //function : Prepend //purpose : //======================================================================= void TCollection_List::Prepend(const Item& I) { myFirst = new TCollection_ListNode(I,(TCollection_ListNode*)myFirst); if (myLast == NULL) myLast = myFirst; } //======================================================================= //function : Prepend //purpose : //======================================================================= void TCollection_List::Prepend(const Item& I, TCollection_ListIterator& theIt) { myFirst = new TCollection_ListNode(I,(TCollection_ListNode*)myFirst); theIt.current = myFirst; theIt.previous = NULL; if (myLast == NULL) myLast = myFirst; } //======================================================================= //function : Prepend //purpose : //======================================================================= void TCollection_List::Prepend(TCollection_List& Other) { if (!Other.IsEmpty()) { ((TCollection_ListNode*)Other.myLast)->Next() = (TCollection_ListNode*)myFirst; myFirst = Other.myFirst; Other.myFirst = Other.myLast = NULL; } } //======================================================================= //function : Append //purpose : //======================================================================= void TCollection_List::Append(const Item& I) { TCollection_ListNode* p = new TCollection_ListNode(I,(TCollection_MapNode*)0L); if (myFirst == NULL) { myFirst = myLast = p; } else { ((TCollection_ListNode*)myLast)->Next() = (TCollection_ListNode*)p; myLast = p; } } //======================================================================= //function : Append //purpose : //======================================================================= void TCollection_List::Append(const Item& I, TCollection_ListIterator& theIt) { TCollection_ListNode* p = new TCollection_ListNode(I,(TCollection_MapNode*)0L); theIt.current = p; theIt.previous = myLast; if (myFirst == NULL) { myFirst = myLast = p; } else { ((TCollection_ListNode*)myLast)->Next() = (TCollection_ListNode*)p; myLast = p; } } //======================================================================= //function : Append //purpose : //======================================================================= void TCollection_List::Append(TCollection_List& Other) { if (!Other.IsEmpty()) { if (IsEmpty()) { myFirst = Other.myFirst; myLast = Other.myLast; } else { ((TCollection_ListNode*)myLast)->Next() = (TCollection_ListNode*)Other.myFirst; myLast = Other.myLast; } Other.myLast = Other.myFirst = NULL; } } //======================================================================= //function : First //purpose : //======================================================================= Item& TCollection_List::First() const { Standard_NoSuchObject_Raise_if(myFirst == NULL,"List:First"); return ((TCollection_ListNode*) myFirst)->Value(); } //======================================================================= //function : Last //purpose : //======================================================================= Item& TCollection_List::Last() const { Standard_NoSuchObject_Raise_if(myLast == NULL,"List:Last"); return ((TCollection_ListNode*) myLast)->Value(); } //======================================================================= //function : RemoveFirst //purpose : //======================================================================= void TCollection_List::RemoveFirst() { if (myFirst == NULL) return; TCollection_ListNode* p = (TCollection_ListNode*) myFirst; myFirst = p->Next(); delete p; if (myFirst == NULL) myLast = NULL; } //======================================================================= //function : Remove //purpose : //======================================================================= void TCollection_List::Remove(TCollection_ListIterator& It) { Standard_NoSuchObject_Raise_if(!It.More(), "TCollection_List::Remove"); if (It.previous == NULL) { RemoveFirst(); It.current = myFirst; } else { TCollection_ListNode* p = (TCollection_ListNode*)((TCollection_ListNode*) It.current)->Next(); ((TCollection_ListNode*)It.previous)->Next() = (TCollection_ListNode*)p; delete ((TCollection_ListNode*) It.current); It.current = p; if (p == NULL) myLast = It.previous; } } //======================================================================= //function : InsertBefore //purpose : //======================================================================= void TCollection_List::InsertBefore(const Item& I, TCollection_ListIterator& It) { Standard_NoSuchObject_Raise_if(!It.More(), "TCollection_List::InsertBefore"); if (It.previous == NULL) { Prepend(I); It.previous = myFirst; } else { TCollection_ListNode* p = new TCollection_ListNode(I,(TCollection_ListNode*)It.current); ((TCollection_ListNode*)It.previous)->Next() = (TCollection_ListNode*)p; It.previous = p; } } //======================================================================= //function : InsertBefore //purpose : //======================================================================= void TCollection_List::InsertBefore(TCollection_List& Other, TCollection_ListIterator& It) { Standard_NoSuchObject_Raise_if(!It.More(), "TCollection_List::InsertBefore"); if (!Other.IsEmpty()) { if (It.previous == NULL) { It.previous = Other.myLast; Prepend(Other); } else { ((TCollection_ListNode*)It.previous)->Next() = (TCollection_ListNode*)Other.myFirst; ((TCollection_ListNode*)Other.myLast)->Next() = (TCollection_ListNode*)It.current; It.previous = Other.myLast; Other.myLast = Other.myFirst = NULL; } } } //======================================================================= //function : InsertAfter //purpose : //======================================================================= void TCollection_List::InsertAfter(const Item& I, TCollection_ListIterator& It) { Standard_NoSuchObject_Raise_if(!It.More(),"TCollection_List::InsertAfter"); if (It.current == myLast) Append(I); else { TCollection_ListNode* p = new TCollection_ListNode(I,((TCollection_ListNode*)It.current)->Next()); ((TCollection_ListNode*)It.current)->Next() = (TCollection_ListNode*)p; } } //======================================================================= //function : InsertAfter //purpose : //======================================================================= void TCollection_List::InsertAfter(TCollection_List& Other, TCollection_ListIterator& It) { Standard_NoSuchObject_Raise_if(!It.More(),"TCollection_List::InsertAfter"); if (It.current == myLast) Append(Other); else if (!Other.IsEmpty()) { ((TCollection_ListNode*)Other.myLast)->Next() = ((TCollection_ListNode*)It.current)->Next(); ((TCollection_ListNode*)It.current)->Next() = (TCollection_ListNode*)Other.myFirst; Other.myLast = Other.myFirst = NULL; } } //======================================================================= //function : TCollection_ListIterator //purpose : //======================================================================= void TCollection_ListIterator::Next() { Standard_NoMoreObject_Raise_if(current == NULL, "TCollection_ListIteratorOfList"); previous = current; current = ((TCollection_ListNode*)previous)->Next(); } //======================================================================= //function : Value //purpose : //======================================================================= Item& TCollection_ListIterator::Value() const { Standard_NoSuchObject_Raise_if(current == NULL, "TCollection_ListIteratorOfList"); return ((TCollection_ListNode*)current)->Value(); }