0024742: Remove rarely used collection classes: Stack
[occt.git] / src / TCollection / TCollection_Stack.gxx
1 // Created on: 1993-01-18
2 // Created by: Remi LEQUETTE
3 // Copyright (c) 1993-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #include <Standard_NoSuchObject.hxx>
18
19 //=======================================================================
20 //function : TCollection_Stack
21 //purpose  : 
22 //=======================================================================
23
24 TCollection_Stack::TCollection_Stack() :
25        myTop(NULL),
26        myDepth(0)
27 {
28 }
29
30 //=======================================================================
31 //function : TCollection_Stack
32 //purpose  : 
33 //=======================================================================
34
35 TCollection_Stack::TCollection_Stack(const TCollection_Stack& Other)
36 {
37   if (!Other.IsEmpty()) {
38     cout << "WARNING copy constructor of non empty stack !"<<endl;
39   }
40   TCollection_StackNode* p = (TCollection_StackNode*) Other.myTop;
41   TCollection_StackNode* q;
42   TCollection_StackNode* r = NULL;
43   myTop = NULL;
44   while (p) {
45     q = new TCollection_StackNode(p->Value(),(TCollection_MapNode*)0L);
46     if (r) r->Next() = q;
47     else   myTop   = q;
48     r = q;
49     p = (TCollection_StackNode*)p->Next();
50   }
51   myDepth = Other.myDepth;
52 }
53
54 //=======================================================================
55 //function : Assign
56 //purpose  : 
57 //=======================================================================
58
59 const TCollection_Stack& TCollection_Stack::Assign
60   (const TCollection_Stack& Other)
61 {
62   if (this == &Other) return *this;
63   Clear();
64   TCollection_StackNode* p = (TCollection_StackNode*) Other.myTop;
65   TCollection_StackNode* q;
66   TCollection_StackNode* r = NULL;
67   while (p) {
68     q = new TCollection_StackNode(p->Value(),(TCollection_MapNode*)0L);
69     if (r) r->Next() = q;
70     else   myTop   = q;
71     r = q;
72     p = (TCollection_StackNode*)p->Next();
73   }
74   myDepth = Other.myDepth;
75   return *this;
76 }
77
78
79 //=======================================================================
80 //function : Top
81 //purpose  : 
82 //=======================================================================
83
84 const Item& TCollection_Stack::Top() const
85 {
86   Standard_NoSuchObject_Raise_if(IsEmpty(),"TCollection_Stack");
87   return ((TCollection_StackNode*)myTop)->Value();
88 }
89
90 //=======================================================================
91 //function : Push
92 //purpose  : 
93 //=======================================================================
94
95 void TCollection_Stack::Push(const Item& I)
96 {
97   myTop = new TCollection_StackNode(I,(TCollection_StackNode*)myTop);
98   myDepth++;
99 }
100
101 //=======================================================================
102 //function : Pop
103 //purpose  : 
104 //=======================================================================
105
106 void TCollection_Stack::Pop()
107 {
108   Standard_NoSuchObject_Raise_if(IsEmpty(),"TCollection_Stack");
109   TCollection_StackNode* p = (TCollection_StackNode*) myTop;
110   myTop = p->Next();
111   delete p;
112   myDepth--;
113 }
114
115 //=======================================================================
116 //function : Clear
117 //purpose  : 
118 //=======================================================================
119
120 void TCollection_Stack::Clear()
121 {
122   TCollection_StackNode* p = (TCollection_StackNode*) myTop;
123   TCollection_StackNode* q;
124   while(p) {
125     q = (TCollection_StackNode*)p->Next();
126     delete p;
127     p = q;
128   }
129   myDepth = 0;
130   myTop = NULL;
131 }
132
133 //=======================================================================
134 //function : ChangeTop
135 //purpose  : 
136 //=======================================================================
137
138 Item& TCollection_Stack::ChangeTop()
139 {
140   Standard_NoSuchObject_Raise_if(IsEmpty(),"TCollection_Stack");
141   return ((TCollection_StackNode*)myTop)->Value();
142 }
143
144 //=======================================================================
145 //function : Next
146 //purpose  : 
147 //=======================================================================
148
149 void TCollection_StackIterator::Next()
150 {
151   current = ((TCollection_StackNode*)current)->Next();
152 }
153
154 //=======================================================================
155 //function : Value
156 //purpose  : 
157 //=======================================================================
158
159 const Item& TCollection_StackIterator::Value() const
160 {
161   Standard_NoSuchObject_Raise_if(current == NULL,
162                                  "TCollection_StackIterator");
163   return ((TCollection_StackNode*)current)->Value();
164 }