0027960: Configuration - fix compilation of OSD_Directory with MinGW-w64
[occt.git] / src / NCollection / NCollection_BaseList.cxx
1 // Created on: 2002-04-23
2 // Created by: Alexander KARTOMIN (akm)
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 // Purpose:     Implementation of the BaseList class
17
18 #include <NCollection_BaseList.hxx>
19
20 //=======================================================================
21 //function : PClear
22 //purpose  : Deletes all nodes from the list
23 //=======================================================================
24
25 void NCollection_BaseList::PClear (NCollection_DelListNode fDel)
26
27   NCollection_ListNode* pCur  = myFirst;
28   NCollection_ListNode* pNext = NULL;
29   while(pCur) 
30   {
31     pNext = pCur->Next();
32     fDel (pCur, myAllocator);
33     pCur = pNext;
34   }
35   myLength = 0;
36   myFirst = myLast = NULL;
37 }
38
39 //=======================================================================
40 //function : PAppend
41 //purpose  : Appends one item at the end
42 //=======================================================================
43
44 void NCollection_BaseList::PAppend (NCollection_ListNode* theNode)
45
46   if (myLength) 
47     myLast->Next() = theNode;
48   else 
49     myFirst = theNode;
50   theNode->Next() = NULL;
51   myLast = theNode;
52   myLength++;
53 }
54
55 //=======================================================================
56 //function : PAppend
57 //purpose  : Appends another list at the end
58 //=======================================================================
59
60 void NCollection_BaseList::PAppend (NCollection_BaseList& theOther)
61
62   if (this == &theOther || theOther.IsEmpty()) 
63     return;
64   
65   if (IsEmpty())
66     myFirst = theOther.myFirst;
67   else
68     myLast->Next() = theOther.myFirst;
69   myLast = theOther.myLast;
70   theOther.myFirst = theOther.myLast = NULL;
71
72   myLength += theOther.myLength;
73   theOther.myLength = 0;
74 }
75
76 //=======================================================================
77 //function : PPrepend
78 //purpose  : Prepends one item at the beginning
79 //=======================================================================
80
81 void NCollection_BaseList::PPrepend (NCollection_ListNode* theNode)
82
83   theNode->Next() = myFirst;
84   myFirst = theNode;
85   if (myLast==NULL)
86     myLast = myFirst;
87   myLength++;
88 }
89
90 //=======================================================================
91 //function : PPrepend
92 //purpose  : 
93 //=======================================================================
94
95 void NCollection_BaseList::PPrepend (NCollection_BaseList& theOther)
96
97   if (this == &theOther || theOther.IsEmpty()) 
98     return;
99
100   if (IsEmpty())
101     myLast = theOther.myLast;
102   else
103     theOther.myLast->Next() = myFirst;
104   myFirst = theOther.myFirst;
105   theOther.myFirst = theOther.myLast = NULL;
106
107   myLength += theOther.myLength;
108   theOther.myLength = 0;
109 }
110
111 //=======================================================================
112 //function : PRemoveFirst
113 //purpose  : 
114 //=======================================================================
115
116 void NCollection_BaseList::PRemoveFirst (NCollection_DelListNode fDel) 
117 {
118   Standard_NoSuchObject_Raise_if(IsEmpty(),
119                                  "NCollection_BaseList::PRemoveFirst");
120   NCollection_ListNode* pItem = myFirst;
121   myFirst = pItem->Next();
122   fDel (pItem, myAllocator);
123   myLength--;
124   if (myLength == 0) 
125     myLast = NULL;
126 }
127
128 //=======================================================================
129 //function : PRemove
130 //purpose  : 
131 //=======================================================================
132
133 void NCollection_BaseList::PRemove (Iterator& theIter, NCollection_DelListNode fDel) 
134 {
135   Standard_NoSuchObject_Raise_if(!theIter.More(),
136                                  "NCollection_BaseList::PRemove");
137   if (theIter.myPrevious == NULL) 
138   {
139     PRemoveFirst (fDel);
140     theIter.myCurrent = myFirst;
141   }
142   else 
143   {
144     NCollection_ListNode* pNode = (theIter.myCurrent)->Next();
145     (theIter.myPrevious)->Next() = pNode;
146     fDel (theIter.myCurrent, myAllocator);
147     theIter.myCurrent = pNode;
148     if (pNode == NULL) 
149       myLast = theIter.myPrevious;
150     myLength--;
151   }
152 }
153
154 //=======================================================================
155 //function : PInsertBefore
156 //purpose  : 
157 //=======================================================================
158
159 void NCollection_BaseList::PInsertBefore (NCollection_ListNode* theNode,
160                                           Iterator& theIter)
161 {
162   Standard_NoSuchObject_Raise_if(!theIter.More(),
163                                  "NCollection_BaseList::PInsertBefore");
164   if (theIter.myPrevious == NULL) 
165   {
166     PPrepend(theNode);
167     theIter.myPrevious = myFirst;
168   }
169   else 
170   {
171     (theIter.myPrevious)->Next() = theNode;
172     theNode->Next() = theIter.myCurrent;
173     theIter.myPrevious = theNode;
174     myLength++;
175   }
176 }
177
178 //=======================================================================
179 //function : PInsertBefore
180 //purpose  : 
181 //=======================================================================
182
183 void NCollection_BaseList::PInsertBefore (NCollection_BaseList& theOther,
184                                           Iterator& theIter)
185 {
186   Standard_NoSuchObject_Raise_if(!theIter.More(),
187                                  "NCollection_BaseList::PInsertBefore");
188   if (theIter.myPrevious == NULL) 
189   {
190     theIter.myPrevious = theOther.myLast;
191     PPrepend(theOther);
192   }
193   else if (!theOther.IsEmpty())
194   {
195     myLength += theOther.myLength;
196     (theIter.myPrevious)->Next() = theOther.myFirst;
197     (theOther.myLast)->Next() = theIter.myCurrent;
198     theIter.myPrevious = theOther.myLast;
199     theOther.myLast = theOther.myFirst = NULL;
200     theOther.myLength = 0;
201   }
202 }
203
204 //=======================================================================
205 //function : PInsertAfter
206 //purpose  : 
207 //=======================================================================
208
209 void NCollection_BaseList::PInsertAfter (NCollection_ListNode* theNode,
210                                          Iterator& theIter)
211 {
212   Standard_NoSuchObject_Raise_if(!theIter.More(),
213                                  "NCollection_BaseList::PInsertAfter");
214   if (theIter.myCurrent == myLast)
215   {
216     PAppend(theNode);
217   }
218   else
219   {
220     theNode->Next() = (theIter.myCurrent)->Next();
221     (theIter.myCurrent)->Next() = theNode;
222     myLength++;
223   }
224 }
225
226 //=======================================================================
227 //function : PInsertAfter
228 //purpose  : 
229 //=======================================================================
230
231 void NCollection_BaseList::PInsertAfter(NCollection_BaseList& theOther,
232                                         Iterator& theIter)
233 {
234   Standard_NoSuchObject_Raise_if(!theIter.More(),
235                                  "NCollection_BaseList::PInsertAfter");
236   if (theIter.myCurrent == myLast)
237   {
238     PAppend(theOther);
239   }
240   else if (!theOther.IsEmpty())
241   {
242     myLength += theOther.myLength;
243     (theOther.myLast)->Next() = (theIter.myCurrent)->Next();
244     (theIter.myCurrent)->Next() = theOther.myFirst;
245     theOther.myLast = theOther.myFirst = NULL;
246     theOther.myLength = 0;
247   }
248 }
249
250 //=======================================================================
251 //function : PReverse
252 //purpose  : reverse the list
253 //=======================================================================
254
255 void NCollection_BaseList::PReverse ()
256 {
257   if (myLength > 1) {
258     NCollection_ListNode * aHead = myFirst->Next();
259     NCollection_ListNode * aNeck = myFirst;
260     aNeck->Next() = NULL;
261     while (aHead) {
262       NCollection_ListNode * aTmp = aHead->Next();
263       aHead->Next() = aNeck;
264       aNeck = aHead;
265       aHead = aTmp;
266     }
267     myLast  = myFirst;
268     myFirst = aNeck;
269   }
270 }