Mercurial > hg > orthanc-stone
annotate Framework/Toolbox/DownloadStack.cpp @ 629:8d66efecd91c am-dev
rename
author | Alain Mazy <alain@mazy.be> |
---|---|
date | Wed, 08 May 2019 16:38:25 +0200 |
parents | b70e9be013e4 |
children | 9a474e90e832 |
rev | line source |
---|---|
0 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
439 | 5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
47 | 8 * modify it under the terms of the GNU Affero General Public License |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
0 | 11 * |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
47 | 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
0 | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 **/ | |
20 | |
21 | |
22 #include "DownloadStack.h" | |
23 | |
212
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
24 #include <Core/OrthancException.h> |
0 | 25 |
26 #include <cassert> | |
27 | |
28 namespace OrthancStone | |
29 { | |
30 bool DownloadStack::CheckInvariants() const | |
31 { | |
32 std::vector<bool> dequeued(nodes_.size(), true); | |
33 | |
34 int i = firstNode_; | |
35 while (i != NIL) | |
36 { | |
37 const Node& node = nodes_[i]; | |
38 | |
39 dequeued[i] = false; | |
40 | |
41 if (node.next_ != NIL && | |
42 nodes_[node.next_].prev_ != i) | |
43 { | |
44 return false; | |
45 } | |
46 | |
47 if (node.prev_ != NIL && | |
48 nodes_[node.prev_].next_ != i) | |
49 { | |
50 return false; | |
51 } | |
52 | |
53 i = nodes_[i].next_; | |
54 } | |
55 | |
56 for (size_t i = 0; i < nodes_.size(); i++) | |
57 { | |
58 if (nodes_[i].dequeued_ != dequeued[i]) | |
59 { | |
60 return false; | |
61 } | |
62 } | |
63 | |
64 return true; | |
65 } | |
66 | |
67 | |
68 DownloadStack::DownloadStack(unsigned int size) | |
69 { | |
70 nodes_.resize(size); | |
71 | |
72 if (size == 0) | |
73 { | |
74 firstNode_ = NIL; | |
75 } | |
76 else | |
77 { | |
78 for (size_t i = 0; i < size; i++) | |
79 { | |
80 nodes_[i].prev_ = i - 1; | |
81 nodes_[i].next_ = i + 1; | |
82 nodes_[i].dequeued_ = false; | |
83 } | |
84 | |
85 nodes_.front().prev_ = NIL; | |
86 nodes_.back().next_ = NIL; | |
87 firstNode_ = 0; | |
88 } | |
89 | |
90 assert(CheckInvariants()); | |
91 } | |
92 | |
93 | |
94 DownloadStack::~DownloadStack() | |
95 { | |
96 assert(CheckInvariants()); | |
97 } | |
98 | |
99 | |
100 bool DownloadStack::Pop(unsigned int& value) | |
101 { | |
102 assert(CheckInvariants()); | |
103 | |
104 if (firstNode_ == NIL) | |
105 { | |
106 for (size_t i = 0; i < nodes_.size(); i++) | |
107 { | |
108 assert(nodes_[i].dequeued_); | |
109 } | |
110 | |
111 return false; | |
112 } | |
113 else | |
114 { | |
115 assert(firstNode_ >= 0 && firstNode_ < static_cast<int>(nodes_.size())); | |
116 value = firstNode_; | |
117 | |
118 Node& node = nodes_[firstNode_]; | |
119 assert(node.prev_ == NIL); | |
120 assert(!node.dequeued_); | |
121 | |
122 node.dequeued_ = true; | |
123 firstNode_ = node.next_; | |
124 | |
125 if (firstNode_ != NIL) | |
126 { | |
127 nodes_[firstNode_].prev_ = NIL; | |
128 } | |
129 | |
130 return true; | |
131 } | |
132 } | |
133 | |
134 | |
135 void DownloadStack::SetTopNodeInternal(unsigned int value) | |
136 { | |
137 assert(CheckInvariants()); | |
138 | |
139 Node& node = nodes_[value]; | |
140 | |
141 if (node.dequeued_) | |
142 { | |
143 // This node has already been processed by the download thread, nothing to do | |
144 return; | |
145 } | |
146 | |
147 // Remove the node from the list | |
148 if (node.prev_ == NIL) | |
149 { | |
150 assert(firstNode_ == static_cast<int>(value)); | |
151 | |
152 // This is already the top node in the list, nothing to do | |
153 return; | |
154 } | |
155 | |
156 nodes_[node.prev_].next_ = node.next_; | |
157 | |
158 if (node.next_ != NIL) | |
159 { | |
160 nodes_[node.next_].prev_ = node.prev_; | |
161 } | |
162 | |
163 // Add back the node at the top of the list | |
164 assert(firstNode_ != NIL); | |
165 | |
166 Node& old = nodes_[firstNode_]; | |
167 assert(old.prev_ == NIL); | |
168 assert(!old.dequeued_); | |
169 node.prev_ = NIL; | |
170 node.next_ = firstNode_; | |
171 old.prev_ = value; | |
172 | |
173 firstNode_ = value; | |
174 } | |
175 | |
176 | |
87
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
177 void DownloadStack::SetTopNode(unsigned int value) |
0 | 178 { |
87
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
179 if (value >= nodes_.size()) |
0 | 180 { |
181 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
182 } | |
183 | |
87
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
184 SetTopNodeInternal(value); |
0 | 185 } |
186 | |
187 | |
87
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
188 void DownloadStack::SetTopNodePermissive(int value) |
0 | 189 { |
190 if (value >= 0 && | |
87
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
191 value < static_cast<int>(nodes_.size())) |
0 | 192 { |
87
4a541cd4fa83
OrthancVolumeImageLoader
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
53
diff
changeset
|
193 SetTopNodeInternal(value); |
0 | 194 } |
195 } | |
196 } |