0022627: Change OCCT memory management defaults
[occt.git] / src / MAT / MAT_TList.gxx
1 // File:        MAT_TList.gxx
2 // Created:     Wed Jun 24 12:47:46 1992
3 // Author:      Gilles DEBARBOUILLE
4 //              <gde@phobox>
5
6
7 //=======================================================================
8 //function : MAT_TList
9 //purpose  : 
10 //=======================================================================
11
12 MAT_TList::MAT_TList()
13 {
14   thecurrentindex  = 0;
15   thenumberofitems = 0;
16 }
17
18 //=======================================================================
19 //function : First
20 //purpose  : 
21 //=======================================================================
22
23 void MAT_TList::First()
24 {
25   thecurrentnode = thefirstnode;
26   thecurrentindex = 1;
27 }
28
29 //=======================================================================
30 //function : Last
31 //purpose  : 
32 //=======================================================================
33
34 void MAT_TList::Last()
35 {
36   thecurrentnode = thelastnode;
37   thecurrentindex = thenumberofitems;
38 }
39
40 //=======================================================================
41 //function : Init
42 //purpose  : 
43 //=======================================================================
44
45 void MAT_TList::Init(const Item& anitem)
46 {
47   First();
48   while(More())
49     {
50       if(anitem == thecurrentnode->GetItem()) break;
51       Next();
52     }
53 }
54
55 //=======================================================================
56 //function : Next
57 //purpose  : 
58 //=======================================================================
59
60 void MAT_TList::Next()
61 {
62   if(!IsEmpty())
63     {
64       thecurrentnode = thecurrentnode->Next();
65       thecurrentindex = (thecurrentindex % thenumberofitems) + 1;
66     }
67 }
68
69 //=======================================================================
70 //function : Previous
71 //purpose  : 
72 //=======================================================================
73
74 void MAT_TList::Previous()
75 {
76   if(!IsEmpty())
77     {
78       thecurrentnode = thecurrentnode->Previous();
79       thecurrentindex = ((thecurrentindex+thenumberofitems-2)%thenumberofitems)+1;
80     }
81 }
82
83 //=======================================================================
84 //function : More
85 //purpose  : 
86 //=======================================================================
87
88 Standard_Boolean MAT_TList::More() const
89 {
90   return (!thecurrentnode.IsNull());
91 }
92
93 //=======================================================================
94 //function : Current
95 //purpose  : 
96 //=======================================================================
97
98 Item MAT_TList::Current() const
99 {
100   return thecurrentnode->GetItem();
101 }
102
103 //=======================================================================
104 //function : Current
105 //purpose  : 
106 //=======================================================================
107
108 void MAT_TList::Current(const Item& anitem) const
109 {
110   thecurrentnode->SetItem(anitem);
111 }
112
113 //=======================================================================
114 //function : FirstItem
115 //purpose  : 
116 //=======================================================================
117
118 Item MAT_TList::FirstItem() const
119 {
120   return thefirstnode->GetItem();
121 }
122
123 //=======================================================================
124 //function : LastItem
125 //purpose  : 
126 //=======================================================================
127
128 Item MAT_TList::LastItem() const
129 {
130   return thelastnode->GetItem();
131 }
132
133 //=======================================================================
134 //function : PreviousItem
135 //purpose  : 
136 //=======================================================================
137
138 Item MAT_TList::PreviousItem() const
139 {
140   return thecurrentnode->Previous()->GetItem();
141 }
142
143 //=======================================================================
144 //function : NextItem
145 //purpose  : 
146 //=======================================================================
147
148 Item MAT_TList::NextItem() const
149 {
150   return thecurrentnode->Next()->GetItem();
151 }
152
153 //=======================================================================
154 //function : Brackets
155 //purpose  : 
156 //=======================================================================
157
158 Item MAT_TList::Brackets (const Standard_Integer anindex)
159 {
160   if(thecurrentindex > anindex)
161     {
162       while(thecurrentindex != anindex)
163         {
164           thecurrentindex--;
165           thecurrentnode = thecurrentnode->Previous();
166         }
167     }
168   else if(thecurrentindex < anindex)
169     {
170       while(thecurrentindex != anindex)
171         {
172           thecurrentindex++;
173           thecurrentnode = thecurrentnode->Next();
174         }
175     }
176   return thecurrentnode->GetItem();
177 }
178
179 //=======================================================================
180 //function : Unlink
181 //purpose  : 
182 //=======================================================================
183
184 void MAT_TList::Unlink()
185 {
186   Standard_Boolean previousisnull = thecurrentnode->Previous().IsNull();
187   Standard_Boolean nextisnull = thecurrentnode->Next().IsNull();
188
189   if(thecurrentindex)
190     {
191       if(!nextisnull && !previousisnull)
192         {
193           thecurrentnode->Next()->Previous(thecurrentnode->Previous());
194           thecurrentnode->Previous()->Next(thecurrentnode->Next());
195         }
196
197       if(thecurrentindex == 1)
198         {
199           thefirstnode = thecurrentnode->Next();
200         }
201       else if(thecurrentindex == thenumberofitems)
202         {
203           thelastnode = thecurrentnode->Previous();
204         }
205     }
206   thenumberofitems--;
207   thecurrentindex--;
208 }
209
210 //=======================================================================
211 //function : LinkBefore
212 //purpose  : 
213 //=======================================================================
214
215 void MAT_TList::LinkBefore(const Item& anitem)
216 {
217   thenumberofitems++;
218   if(thecurrentindex)thecurrentindex++;
219
220   Handle(MAT_TListNode) previous;
221
222   Handle(MAT_TListNode) node = new MAT_TListNode(anitem);
223
224   if(!(thecurrentnode->Previous()).IsNull())
225     {
226       previous = thecurrentnode->Previous();
227       previous->Next(node);
228       node->Previous(previous);
229     }
230
231   if(thecurrentindex == 2)
232     {
233       thefirstnode = node;
234     }
235
236   thecurrentnode->Previous(node);
237   node->Next(thecurrentnode);
238 }
239
240 //=======================================================================
241 //function : LinkAfter
242 //purpose  : 
243 //=======================================================================
244
245 void MAT_TList::LinkAfter(const Item& anitem)
246 {
247   thenumberofitems++;
248   Handle(MAT_TListNode) next;
249
250   Handle(MAT_TListNode) node = new MAT_TListNode(anitem);
251
252   if(!(thecurrentnode->Next()).IsNull())
253     {
254       next = thecurrentnode->Next();
255       next->Previous(node);
256       node->Next(next);
257     }
258
259   if(thecurrentindex+1 ==thenumberofitems)
260     {
261       thelastnode = node;
262     }
263
264   thecurrentnode->Next(node);
265   node->Previous(thecurrentnode);
266 }
267
268 //=======================================================================
269 //function : FrontAdd
270 //purpose  : 
271 //=======================================================================
272
273 void MAT_TList::FrontAdd(const Item& anitem)
274 {
275   thenumberofitems++;
276   if(thecurrentindex)thecurrentindex++;
277
278   Handle(MAT_TListNode) node = new MAT_TListNode(anitem);
279
280   if(!thefirstnode.IsNull())
281     {
282       thefirstnode->Previous(node);
283       node->Next(thefirstnode);
284     }
285   else
286     {
287       thelastnode = node;
288     }
289
290   thefirstnode = node;
291 }
292
293 //=======================================================================
294 //function : BackAdd
295 //purpose  : 
296 //=======================================================================
297
298 void MAT_TList::BackAdd(const Item& anitem)
299 {
300   thenumberofitems++;
301   Handle(MAT_TListNode) node = new MAT_TListNode(anitem);
302
303   if(!thelastnode.IsNull())
304     {
305       thelastnode->Next(node);
306       node->Previous(thelastnode);
307     }
308   else
309     {
310       thefirstnode = node;
311     }
312
313   thelastnode = node;
314 }
315
316 //=======================================================================
317 //function : Permute
318 //purpose  : 
319 //=======================================================================
320
321 void MAT_TList::Permute()
322 {
323   Handle(MAT_TListNode) previous = thecurrentnode->Previous();
324   Handle(MAT_TListNode) current  = thecurrentnode;
325   Handle(MAT_TListNode) next     = thecurrentnode->Next();
326   Handle(MAT_TListNode) nextnext = next->Next();
327   Handle(MAT_TListNode) null;
328
329   if(!previous.IsNull())
330     {
331       previous->Next(next);
332       next->Previous(previous);
333     }
334   else
335     {
336       next->Previous(null);
337     }
338   next->Next(current);
339   current->Previous(next);
340   if(!nextnext.IsNull())
341     {
342       current->Next(nextnext);
343       nextnext->Previous(current);
344     }
345   else
346     {
347       current->Next(null);
348     }
349   if(thefirstnode == current) thefirstnode = next;
350   if(thelastnode  == next) thelastnode = current;
351   thecurrentindex++;
352 }
353
354 //=======================================================================
355 //function : Loop
356 //purpose  : 
357 //=======================================================================
358
359 void MAT_TList::Loop() const
360 {
361   thelastnode->Next(thefirstnode);
362   thefirstnode->Previous(thelastnode);
363 }
364
365 //=======================================================================
366 //function : Dump
367 //purpose  : 
368 //=======================================================================
369
370 void MAT_TList::Dump(const Standard_Integer ashift,
371                      const Standard_Integer alevel)
372 {
373   for(First(); More(); Next()) Current()->Dump(ashift,alevel);
374 }