/src/dcmtk/dcmdata/libsrc/dcstack.cc
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * Copyright (C) 1994-2021, OFFIS e.V. |
4 | | * All rights reserved. See COPYRIGHT file for details. |
5 | | * |
6 | | * This software and supporting documentation were developed by |
7 | | * |
8 | | * OFFIS e.V. |
9 | | * R&D Division Health |
10 | | * Escherweg 2 |
11 | | * D-26121 Oldenburg, Germany |
12 | | * |
13 | | * |
14 | | * Module: dcmdata |
15 | | * |
16 | | * Author: Gerd Ehlers |
17 | | * |
18 | | * Purpose: stack class |
19 | | * |
20 | | */ |
21 | | |
22 | | #include "dcmtk/config/osconfig.h" /* make sure OS specific configuration is included first */ |
23 | | |
24 | | #include "dcmtk/ofstd/ofcast.h" |
25 | | #include "dcmtk/ofstd/ofstream.h" |
26 | | #include "dcmtk/dcmdata/dcstack.h" |
27 | | |
28 | | |
29 | | // ************************************************ |
30 | | // *** DcmStackNode() ***************************** |
31 | | // ************************************************ |
32 | | |
33 | | |
34 | | DcmStackNode::DcmStackNode( DcmObject *obj ) |
35 | 0 | : link(NULL), |
36 | 0 | objNodeValue(obj) |
37 | 0 | { |
38 | 0 | } |
39 | | |
40 | | |
41 | | // ******************************** |
42 | | |
43 | | |
44 | | DcmStackNode::~DcmStackNode() |
45 | 0 | { |
46 | 0 | } |
47 | | |
48 | | |
49 | | // ******************************** |
50 | | |
51 | | |
52 | | DcmObject* DcmStackNode::value() const |
53 | 0 | { |
54 | 0 | return objNodeValue; |
55 | 0 | } |
56 | | |
57 | | |
58 | | |
59 | | // ************************************************ |
60 | | // *** DcmStack() ********************************* |
61 | | // ************************************************ |
62 | | |
63 | | |
64 | | DcmStack::DcmStack() |
65 | 32 | : topNode_(NULL), |
66 | 32 | cardinality_(0) |
67 | 32 | { |
68 | 32 | } |
69 | | |
70 | | |
71 | | // ************************************************ |
72 | | |
73 | | |
74 | | DcmStack::DcmStack( const DcmStack & oldStack) |
75 | 0 | : topNode_(NULL), |
76 | 0 | cardinality_(oldStack.cardinality_) |
77 | 0 | { |
78 | 0 | if (cardinality_) |
79 | 0 | { |
80 | 0 | topNode_ = new DcmStackNode(oldStack.topNode_->objNodeValue); |
81 | 0 | DcmStackNode * oldPtr = oldStack.topNode_->link; |
82 | 0 | DcmStackNode * newPtr = topNode_; |
83 | 0 | while (oldPtr) |
84 | 0 | { |
85 | 0 | newPtr->link = new DcmStackNode(oldPtr->objNodeValue); |
86 | 0 | oldPtr = oldPtr->link; |
87 | 0 | newPtr = newPtr->link; |
88 | 0 | } |
89 | 0 | } |
90 | 0 | } |
91 | | |
92 | | |
93 | | // ******************************** |
94 | | |
95 | | |
96 | | DcmStack::~DcmStack() |
97 | 32 | { |
98 | 32 | clear(); |
99 | 32 | } |
100 | | |
101 | | |
102 | | // ******************************** |
103 | | |
104 | | |
105 | | void DcmStack::clear() |
106 | 32 | { |
107 | 32 | DcmStackNode *node; |
108 | 32 | while (topNode_ != NULL) |
109 | 0 | { |
110 | 0 | node = topNode_; |
111 | 0 | topNode_ = topNode_->link; |
112 | 0 | delete node; |
113 | 0 | } |
114 | 32 | cardinality_ = 0; |
115 | 32 | } |
116 | | |
117 | | |
118 | | // ******************************** |
119 | | |
120 | | |
121 | | DcmObject* DcmStack::push( DcmObject *obj ) |
122 | 0 | { |
123 | 0 | if ( obj != NULL ) |
124 | 0 | { |
125 | 0 | DcmStackNode *node = new DcmStackNode( obj ); |
126 | 0 | node->link = topNode_; |
127 | 0 | topNode_ = node; |
128 | 0 | cardinality_++; |
129 | 0 | } |
130 | 0 | return obj; |
131 | 0 | } |
132 | | |
133 | | |
134 | | // ******************************** |
135 | | |
136 | | |
137 | | DcmObject* DcmStack::pop() |
138 | 0 | { |
139 | 0 | DcmObject *obj; |
140 | 0 | DcmStackNode *node; |
141 | 0 | if ( topNode_ != NULL ) |
142 | 0 | { |
143 | 0 | obj = topNode_->value(); |
144 | 0 | node = topNode_; |
145 | 0 | topNode_ = topNode_->link; |
146 | 0 | delete node; |
147 | 0 | cardinality_--; |
148 | 0 | } |
149 | 0 | else |
150 | 0 | obj = NULL; |
151 | 0 | return obj; |
152 | 0 | } |
153 | | |
154 | | |
155 | | // ******************************** |
156 | | |
157 | | |
158 | | DcmObject* DcmStack::elem(const unsigned long number) const |
159 | 0 | { |
160 | 0 | unsigned long num = number; |
161 | 0 | DcmObject *obj; |
162 | 0 | DcmStackNode *node = topNode_; |
163 | 0 | while ( num > 0 && node != NULL ) |
164 | 0 | { |
165 | 0 | node = node->link; |
166 | 0 | --num; |
167 | 0 | } |
168 | 0 | if ( node != NULL ) |
169 | 0 | obj = node->value(); |
170 | 0 | else |
171 | 0 | obj = NULL; |
172 | 0 | return obj; |
173 | 0 | } |
174 | | |
175 | | |
176 | | // ******************************** |
177 | | |
178 | | |
179 | | DcmObject* DcmStack::top() const |
180 | 0 | { |
181 | 0 | DcmObject *obj; |
182 | 0 | if ( topNode_ != NULL ) |
183 | 0 | obj = topNode_->value(); |
184 | 0 | else |
185 | 0 | obj = NULL; |
186 | 0 | return obj; |
187 | 0 | } |
188 | | |
189 | | |
190 | | // ******************************** |
191 | | |
192 | | |
193 | | unsigned long DcmStack::card() const |
194 | 0 | { |
195 | 0 | return cardinality_; |
196 | 0 | } |
197 | | |
198 | | |
199 | | // ******************************** |
200 | | |
201 | | |
202 | | OFBool DcmStack::empty() const |
203 | 0 | { |
204 | 0 | return OFstatic_cast(OFBool, topNode_ == NULL ); |
205 | 0 | } |
206 | | |
207 | | |
208 | | DcmStack& DcmStack::operator=(const DcmStack& arg) |
209 | 0 | { |
210 | 0 | if (this != &arg) |
211 | 0 | { |
212 | 0 | clear(); |
213 | 0 | cardinality_ = arg.cardinality_; |
214 | 0 | if (cardinality_) |
215 | 0 | { |
216 | 0 | topNode_ = new DcmStackNode(arg.topNode_->objNodeValue); |
217 | 0 | DcmStackNode * oldPtr = arg.topNode_->link; |
218 | 0 | DcmStackNode * newPtr = topNode_; |
219 | 0 | while (oldPtr) |
220 | 0 | { |
221 | 0 | newPtr->link = new DcmStackNode(oldPtr->objNodeValue); |
222 | 0 | oldPtr = oldPtr->link; |
223 | 0 | newPtr = newPtr->link; |
224 | 0 | } |
225 | 0 | } |
226 | 0 | } |
227 | 0 | return *this; |
228 | 0 | } |
229 | | |
230 | | |
231 | | OFBool DcmStack::operator<(const DcmStack& arg) const |
232 | 0 | { |
233 | 0 | if (cardinality_ < arg.cardinality_) return OFTrue; |
234 | 0 | if (cardinality_ > arg.cardinality_) return OFFalse; |
235 | | |
236 | | // cardinality_ is equal. Now walk through stack and do pointer arithmetics |
237 | 0 | DcmStackNode * thisPtr = topNode_; |
238 | 0 | DcmStackNode * argPtr = arg.topNode_; |
239 | 0 | while (thisPtr) |
240 | 0 | { |
241 | 0 | if (thisPtr->objNodeValue < argPtr->objNodeValue) return OFTrue; |
242 | 0 | if (thisPtr->objNodeValue > argPtr->objNodeValue) return OFFalse; |
243 | 0 | thisPtr = thisPtr->link; |
244 | 0 | argPtr = argPtr->link; |
245 | 0 | } |
246 | 0 | return OFFalse; // stacks are equal |
247 | 0 | } |
248 | | |
249 | | |
250 | | OFBool DcmStack::operator==(const DcmStack& arg) const |
251 | 0 | { |
252 | 0 | if (cardinality_ != arg.cardinality_) return OFFalse; |
253 | | |
254 | 0 | DcmStackNode * thisPtr = topNode_; |
255 | 0 | DcmStackNode * argPtr = arg.topNode_; |
256 | 0 | while (thisPtr) |
257 | 0 | { |
258 | 0 | if (thisPtr->objNodeValue != argPtr->objNodeValue) return OFFalse; |
259 | 0 | thisPtr = thisPtr->link; |
260 | 0 | argPtr = argPtr->link; |
261 | 0 | } |
262 | 0 | return OFTrue; |
263 | 0 | } |