forked from ScanTailor-Advanced/scantailor-advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageSequence.cpp
More file actions
139 lines (119 loc) · 3.47 KB
/
Copy pathPageSequence.cpp
File metadata and controls
139 lines (119 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (C) 2019 Joseph Artsimovich <joseph.artsimovich@gmail.com>, 4lex4 <4lex49@zoho.com>
// Use of this source code is governed by the GNU GPLv3 license that can be found in the LICENSE file.
#include "PageSequence.h"
#include <vector>
void PageSequence::append(const PageInfo& pageInfo) {
m_pages.push_back(pageInfo);
}
size_t PageSequence::numPages() const {
return m_pages.size();
}
const PageInfo& PageSequence::pageAt(const size_t idx) const {
return m_pages.at(idx); // may throw
}
const PageInfo& PageSequence::pageAt(const PageId page) const {
auto it(m_pages.begin());
const auto end(m_pages.end());
for (; it != end && it->id() != page; ++it) {
}
return *it;
}
int PageSequence::pageNo(const PageId& page) const {
auto it(m_pages.begin());
const auto end(m_pages.end());
int res = 0;
for (; it != end && it->id() != page; ++it, ++res) {
}
return (it == end) ? -1 : res;
}
std::set<PageId> PageSequence::selectAll() const {
std::set<PageId> selection;
for (const PageInfo& pageInfo : m_pages) {
selection.insert(pageInfo.id());
}
return selection;
}
std::set<PageId> PageSequence::selectPagePlusFollowers(const PageId& page) const {
std::set<PageId> selection;
auto it(m_pages.begin());
const auto end(m_pages.end());
for (; it != end && it->id() != page; ++it) {
// Continue until we have a match.
}
for (; it != end; ++it) {
selection.insert(it->id());
}
return selection;
}
std::set<PageId> PageSequence::selectEveryOther(const PageId& base) const {
std::set<PageId> selection;
auto it(m_pages.begin());
const auto end(m_pages.end());
for (; it != end && it->id() != base; ++it) {
// Continue until we have a match.
}
if (it == end) {
return selection;
}
const int baseIdx = static_cast<int>(it - m_pages.begin());
int idx = 0;
for (const PageInfo& pageInfo : m_pages) {
if (((idx - baseIdx) & 1) == 0) {
selection.insert(pageInfo.id());
}
++idx;
}
return selection;
}
std::set<PageId> PageSequence::selectThisPageAndFollowingEveryOther(const PageId& page) const {
std::set<PageId> selection;
const int baseIdx = pageNo(page);
if (baseIdx < 0) {
return selection;
}
for (size_t i = static_cast<size_t>(baseIdx); i < m_pages.size(); i += 2) {
selection.insert(m_pages[i].id());
}
return selection;
}
std::set<PageId> PageSequence::selectEveryOtherInSubsetFromPage(const PageId& base,
const std::set<PageId>& subset) const {
std::set<PageId> selection;
if (subset.empty()) {
return selection;
}
std::vector<PageId> ordered;
ordered.reserve(subset.size());
for (size_t i = 0; i < m_pages.size(); ++i) {
const PageId id = m_pages[i].id();
if (subset.count(id) != 0) {
ordered.push_back(id);
}
}
int start = -1;
for (size_t i = 0; i < ordered.size(); ++i) {
if (ordered[i] == base) {
start = static_cast<int>(i);
break;
}
}
if (start < 0) {
return selection;
}
for (int i = start; i < static_cast<int>(ordered.size()); i += 2) {
selection.insert(ordered[i]);
}
return selection;
}
std::vector<PageInfo>::iterator PageSequence::begin() {
return m_pages.begin();
}
std::vector<PageInfo>::iterator PageSequence::end() {
return m_pages.end();
}
std::vector<PageInfo>::const_iterator PageSequence::begin() const {
return m_pages.cbegin();
}
std::vector<PageInfo>::const_iterator PageSequence::end() const {
return m_pages.cend();
}