annotate Framework/Loaders/BasicFetchingItemsSorter.cpp @ 1145:4f99c7905f8d broker

mingw qt
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 12 Nov 2019 18:28:46 +0100
parents 51976977d2d3
children 2d8ab34c8c91
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
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
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
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
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
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
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 #include "BasicFetchingItemsSorter.h"
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include <Core/OrthancException.h>
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 namespace OrthancStone
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 BasicFetchingItemsSorter::BasicFetchingItemsSorter(unsigned int itemsCount) :
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 itemsCount_(itemsCount)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 if (itemsCount == 0)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 void BasicFetchingItemsSorter::Sort(std::vector<unsigned int>& target,
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 unsigned int current)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 if (current >= itemsCount_)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 target.clear();
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 target.reserve(itemsCount_);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 target.push_back(current);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 const unsigned int countBelow = current;
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 const unsigned int countAbove = (itemsCount_ - 1) - current;
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 const unsigned int n = std::min(countBelow, countAbove);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 for (unsigned int i = 1; i <= n; i++)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 assert(current + i < itemsCount_ &&
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 current >= i);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58 target.push_back(current + i);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 target.push_back(current - i);
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 for (unsigned int i = current - n; i > 0; i--)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 target.push_back(i - 1);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 for (unsigned int i = current + n + 1; i < itemsCount_; i++)
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 {
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 target.push_back(i);
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 assert(target.size() == itemsCount_);
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 }
51976977d2d3 reorganization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 }