comparison Framework/Loaders/BasicFetchingStrategy.cpp @ 708:51976977d2d3

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 20 May 2019 11:20:01 +0200
parents
children 7457b4ee1f29
comparison
equal deleted inserted replaced
707:d1feb89ea742 708:51976977d2d3
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
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.
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
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
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "BasicFetchingStrategy.h"
23
24 #include <Core/OrthancException.h>
25
26 namespace OrthancStone
27 {
28 void BasicFetchingStrategy::Schedule(unsigned int item,
29 unsigned int quality)
30 {
31 assert(item < GetItemsCount() &&
32 quality <= maxQuality_);
33
34 if (nextQuality_[item] <= quality)
35 {
36 content_.push_back(ContentItem(item, quality));
37 }
38 }
39
40
41 BasicFetchingStrategy::BasicFetchingStrategy(IFetchingItemsSorter* sorter, // Takes ownership
42 unsigned int maxQuality) :
43 sorter_(sorter),
44 maxQuality_(maxQuality),
45 position_(0),
46 blockSize_(2)
47 {
48 if (sorter == NULL)
49 {
50 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
51 }
52
53 nextQuality_.resize(sorter_->GetItemsCount(), 0), // Does not change along calls to "SetCurrent()"
54
55 SetCurrent(0);
56 }
57
58
59 // WARNING - This parameters is only considered during the next call to SetCurrent().
60 void BasicFetchingStrategy::SetBlockSize(unsigned int size)
61 {
62 if (size <= 0)
63 {
64 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
65 }
66
67 blockSize_ = size;
68 }
69
70
71 bool BasicFetchingStrategy::GetNext(unsigned int& item,
72 unsigned int& quality)
73 {
74 if (position_ >= content_.size())
75 {
76 return false;
77 }
78 else
79 {
80 item = content_[position_].GetItem();
81 quality = content_[position_].GetQuality();
82
83 assert(nextQuality_[item] <= quality);
84 nextQuality_[item] = quality + 1;
85
86 position_ ++;
87 return true;
88 }
89 }
90
91
92 void BasicFetchingStrategy::SetCurrent(unsigned int item)
93 {
94 // TODO - This function is O(N) complexity where "N" is the
95 // number of items times the max quality. Could use a LRU index.
96
97 position_ = 0;
98
99 std::vector<unsigned int> v;
100 sorter_->Sort(v, item);
101
102 assert(v.size() == GetItemsCount());
103
104 if (v.size() == 0)
105 {
106 return;
107 }
108
109 content_.clear();
110 content_.reserve(v.size() * maxQuality_);
111
112 Schedule(v.front(), maxQuality_);
113
114 for (unsigned int q = 0; q <= maxQuality_; q++)
115 {
116 unsigned int start = 1 + q * blockSize_;
117 unsigned int end = start + blockSize_;
118
119 if (q == maxQuality_ ||
120 end > v.size())
121 {
122 end = v.size();
123 }
124
125 unsigned int a = 0;
126 if (maxQuality_ >= q + 1)
127 {
128 a = maxQuality_ - q - 1;
129 }
130
131 for (unsigned int j = a; j <= maxQuality_; j++)
132 {
133 for (unsigned int i = start; i < end; i++)
134 {
135 Schedule(v[i], j);
136 }
137 }
138 }
139 }
140
141
142 void BasicFetchingStrategy::RecycleFurthest(unsigned int& item)
143 {
144 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
145 }
146 }