annotate OrthancStone/Sources/Loaders/BasicFetchingStrategy.cpp @ 1716:6aadc7cbb8ea

todo
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 01 Dec 2020 08:58:53 +0100
parents 59f95b9ea858
children 9ac2a65d4172
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
708
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
1270
2d8ab34c8c91 upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 725
diff changeset
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
708
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
1598
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
8 * modify it under the terms of the GNU Lesser General Public License
708
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
1598
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
15 * Lesser General Public License for more details.
1596
4fb8fdf03314 removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1571
diff changeset
16 *
1598
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
18 * License along with this program. If not, see
8563ea5d8ae4 relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1596
diff changeset
19 * <http://www.gnu.org/licenses/>.
708
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 **/
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 #include "BasicFetchingStrategy.h"
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24
1455
30deba7bc8e2 simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1270
diff changeset
25 #include <OrthancException.h>
708
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
1624
59f95b9ea858 fix build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1598
diff changeset
27 #include <cassert>
59f95b9ea858 fix build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1598
diff changeset
28
708
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 namespace OrthancStone
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 void BasicFetchingStrategy::Schedule(unsigned int item,
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 unsigned int quality)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 assert(item < GetItemsCount() &&
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 quality <= maxQuality_);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 if (nextQuality_[item] <= quality)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 content_.push_back(ContentItem(item, quality));
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 BasicFetchingStrategy::BasicFetchingStrategy(IFetchingItemsSorter* sorter, // Takes ownership
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 unsigned int maxQuality) :
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 sorter_(sorter),
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 maxQuality_(maxQuality),
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 position_(0),
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 blockSize_(2)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 if (sorter == NULL)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
709
7457b4ee1f29 VolumeSeriesOrthancLoader uses a prefetching strategy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 708
diff changeset
56 nextQuality_.resize(sorter_->GetItemsCount(), 0); // Does not change along calls to "SetCurrent()"
708
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57
709
7457b4ee1f29 VolumeSeriesOrthancLoader uses a prefetching strategy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 708
diff changeset
58 SetCurrent(0);
708
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 void BasicFetchingStrategy::SetBlockSize(unsigned int size)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 {
1571
85e117739eca cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1512
diff changeset
64 if (size == 0)
708
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 blockSize_ = size;
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 bool BasicFetchingStrategy::GetNext(unsigned int& item,
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 unsigned int& quality)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76 if (position_ >= content_.size())
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 return false;
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 else
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 item = content_[position_].GetItem();
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83 quality = content_[position_].GetQuality();
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 assert(nextQuality_[item] <= quality);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 nextQuality_[item] = quality + 1;
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88 position_ ++;
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 return true;
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 void BasicFetchingStrategy::SetCurrent(unsigned int item)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 // TODO - This function is O(N) complexity where "N" is the
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 // number of items times the max quality. Could use a LRU index.
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 position_ = 0;
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 std::vector<unsigned int> v;
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 sorter_->Sort(v, item);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 assert(v.size() == GetItemsCount());
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 if (v.size() == 0)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 return;
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 content_.clear();
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 content_.reserve(v.size() * maxQuality_);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 Schedule(v.front(), maxQuality_);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 for (unsigned int q = 0; q <= maxQuality_; q++)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 unsigned int start = 1 + q * blockSize_;
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 unsigned int end = start + blockSize_;
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 if (q == maxQuality_ ||
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 end > v.size())
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 {
725
f8d582fc5731 Fixed previous typo
Benjamin Golinvaux <bgo@osimis.io>
parents: 724
diff changeset
124 end = static_cast<int>(v.size());
708
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 unsigned int a = 0;
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 if (maxQuality_ >= q + 1)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 a = maxQuality_ - q - 1;
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133 for (unsigned int j = a; j <= maxQuality_; j++)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 for (unsigned int i = start; i < end; i++)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 Schedule(v[i], j);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 void BasicFetchingStrategy::RecycleFurthest(unsigned int& item)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 }