Coverage Report

Created: 2026-08-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
157k
  : link(NULL),
36
157k
    objNodeValue(obj)
37
157k
{
38
157k
}
39
40
41
// ********************************
42
43
44
DcmStackNode::~DcmStackNode()
45
157k
{
46
157k
}
47
48
49
// ********************************
50
51
52
DcmObject* DcmStackNode::value() const
53
1.49M
{
54
1.49M
    return objNodeValue;
55
1.49M
}
56
57
58
59
// ************************************************
60
// *** DcmStack() *********************************
61
// ************************************************
62
63
64
DcmStack::DcmStack()
65
33.7k
  : topNode_(NULL),
66
33.7k
    cardinality_(0)
67
33.7k
{
68
33.7k
}
69
70
71
// ************************************************
72
73
74
DcmStack::DcmStack( const DcmStack & oldStack)
75
1.25k
  : topNode_(NULL),
76
1.25k
    cardinality_(oldStack.cardinality_)
77
1.25k
{
78
1.25k
    if (cardinality_)
79
1.25k
    {
80
1.25k
        topNode_ = new DcmStackNode(oldStack.topNode_->objNodeValue);
81
1.25k
        DcmStackNode * oldPtr = oldStack.topNode_->link;
82
1.25k
        DcmStackNode * newPtr = topNode_;
83
41.0k
        while (oldPtr)
84
39.7k
        {
85
39.7k
            newPtr->link = new DcmStackNode(oldPtr->objNodeValue);
86
39.7k
            oldPtr = oldPtr->link;
87
39.7k
            newPtr = newPtr->link;
88
39.7k
        }
89
1.25k
    }
90
1.25k
}
91
92
93
// ********************************
94
95
96
DcmStack::~DcmStack()
97
35.0k
{
98
35.0k
    clear();
99
35.0k
}
100
101
102
// ********************************
103
104
105
void DcmStack::clear()
106
55.3k
{
107
55.3k
    DcmStackNode *node;
108
105k
    while (topNode_ != NULL)
109
50.3k
    {
110
50.3k
        node = topNode_;
111
50.3k
        topNode_ = topNode_->link;
112
50.3k
        delete node;
113
50.3k
    }
114
55.3k
    cardinality_ = 0;
115
55.3k
}
116
117
118
// ********************************
119
120
121
DcmObject* DcmStack::push( DcmObject *obj )
122
116k
{
123
116k
    if ( obj != NULL )
124
116k
    {
125
116k
        DcmStackNode *node = new DcmStackNode( obj );
126
116k
        node->link = topNode_;
127
116k
        topNode_ = node;
128
116k
        cardinality_++;
129
116k
    }
130
116k
    return obj;
131
116k
}
132
133
134
// ********************************
135
136
137
DcmObject* DcmStack::pop()
138
106k
{
139
106k
    DcmObject *obj;
140
106k
    DcmStackNode *node;
141
106k
    if ( topNode_ != NULL )
142
106k
    {
143
106k
        obj = topNode_->value();
144
106k
        node = topNode_;
145
106k
        topNode_ = topNode_->link;
146
106k
        delete node;
147
106k
        cardinality_--;
148
106k
    }
149
0
    else
150
0
        obj = NULL;
151
106k
    return obj;
152
106k
}
153
154
155
// ********************************
156
157
158
DcmObject* DcmStack::elem(const unsigned long number) const
159
1.28M
{
160
1.28M
    unsigned long num = number;
161
1.28M
    DcmObject *obj;
162
1.28M
    DcmStackNode *node = topNode_;
163
70.8M
    while ( num > 0 && node != NULL )
164
69.5M
    {
165
69.5M
         node = node->link;
166
69.5M
         --num;
167
69.5M
    }
168
1.28M
    if ( node != NULL )
169
1.28M
        obj = node->value();
170
0
    else
171
0
        obj = NULL;
172
1.28M
    return obj;
173
1.28M
}
174
175
176
// ********************************
177
178
179
DcmObject* DcmStack::top() const
180
99.4k
{
181
99.4k
    DcmObject *obj;
182
99.4k
    if ( topNode_ != NULL )
183
99.4k
        obj = topNode_->value();
184
0
    else
185
0
        obj = NULL;
186
99.4k
    return obj;
187
99.4k
}
188
189
190
// ********************************
191
192
193
unsigned long DcmStack::card() const
194
46.4k
{
195
46.4k
    return cardinality_;
196
46.4k
}
197
198
199
// ********************************
200
201
202
OFBool DcmStack::empty() const
203
76.5k
{
204
76.5k
    return OFstatic_cast(OFBool, topNode_ == NULL );
205
76.5k
}
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
}