NNTree.hh
5.21 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifndef NNTREE_HH
#define NNTREE_HH
#include <qpdf/QPDF.hh>
#include <qpdf/QPDFObjectHandle_private.hh>
#include <iterator>
#include <list>
#include <memory>
class NNTreeImpl;
class NNTreeIterator final
{
friend class NNTreeImpl;
public:
typedef std::pair<QPDFObjectHandle, QPDFObjectHandle> T;
using iterator_category = std::bidirectional_iterator_tag;
using value_type = T;
using difference_type = long;
using pointer = T*;
using reference = T&;
~NNTreeIterator() = default;
// iterator can be incremented or decremented, or dereferenced. This does not imply that it
// points to a valid item.
bool
valid() const
{
return item_number >= 0;
}
NNTreeIterator&
operator++()
{
increment(false);
return *this;
}
NNTreeIterator
operator++(int)
{
NNTreeIterator t = *this;
++(*this);
return t;
}
NNTreeIterator&
operator--()
{
increment(true);
return *this;
}
NNTreeIterator
operator--(int)
{
NNTreeIterator t = *this;
--(*this);
return t;
}
reference
operator*()
{
updateIValue(false);
return ivalue;
}
pointer
operator->()
{
updateIValue(false);
return &ivalue;
}
bool operator==(NNTreeIterator const& other) const;
bool
operator!=(NNTreeIterator const& other) const
{
return !operator==(other);
}
void insertAfter(QPDFObjectHandle const& key, QPDFObjectHandle const& value);
void remove();
private:
class PathElement
{
public:
PathElement(qpdf::Dictionary const& node, int kid_number) :
node(node),
kid_number(kid_number)
{
}
qpdf::Dictionary node;
int kid_number;
};
NNTreeIterator(NNTreeImpl& impl) :
impl(impl)
{
}
void updateIValue(bool allow_invalid = true);
bool deepen(qpdf::Dictionary node, bool first, bool allow_empty);
void
setItemNumber(QPDFObjectHandle const& a_node, int n)
{
node = a_node;
item_number = n;
updateIValue();
}
void
addPathElement(QPDFObjectHandle const& a_node, int kid_number)
{
path.emplace_back(a_node, kid_number);
}
qpdf::Dictionary getNextKid(PathElement& element, bool backward);
void increment(bool backward);
void resetLimits(qpdf::Dictionary node, std::list<PathElement>::iterator parent);
void split(qpdf::Dictionary to_split, std::list<PathElement>::iterator parent);
std::list<PathElement>::iterator lastPathElement();
NNTreeImpl& impl;
std::list<PathElement> path;
qpdf::Dictionary node;
int item_number{-1};
value_type ivalue;
};
class NNTreeImpl final
{
friend class NNTreeIterator;
public:
typedef NNTreeIterator iterator;
NNTreeImpl(
QPDF& qpdf,
qpdf::Dictionary tree_root,
qpdf_object_type_e key_type,
std::function<bool(QPDFObjectHandle const&)> value_validator,
bool auto_repair) :
qpdf(qpdf),
tree_root(std::move(tree_root)),
key_type(key_type),
items_key(key_type == ::ot_string ? "/Names" : "/Nums"),
value_valid(value_validator),
auto_repair(auto_repair)
{
}
iterator begin();
iterator
end()
{
return {*this};
}
iterator last();
iterator find(QPDFObjectHandle const& key, bool return_prev_if_not_found = false);
iterator insertFirst(QPDFObjectHandle const& key, QPDFObjectHandle const& value);
iterator insert(QPDFObjectHandle const& key, QPDFObjectHandle const& value);
bool remove(QPDFObjectHandle const& key, QPDFObjectHandle* value = nullptr);
bool validate(bool repair = true);
// Change the split threshold for easier testing. There's no real reason to expose this to
// downstream tree helpers, but it has to be public so we can call it from the test suite.
void
setSplitThreshold(int threshold)
{
split_threshold = threshold;
}
private:
void repair();
iterator findInternal(QPDFObjectHandle const& key, bool return_prev_if_not_found = false);
int binarySearch(
QPDFObjectHandle const& key,
qpdf::Array const& items,
size_t num_items,
bool return_prev_if_not_found,
bool search_kids) const;
int compareKeyItem(QPDFObjectHandle const& key, qpdf::Array const& items, int idx) const;
int compareKeyKid(QPDFObjectHandle const& key, qpdf::Array const& items, int idx) const;
void warn(QPDFObjectHandle const& node, std::string const& msg);
void error(QPDFObjectHandle const& node, std::string const& msg) const;
std::string const&
itemsKey() const
{
return items_key;
}
bool
keyValid(QPDFObjectHandle o) const
{
return o.resolved_type_code() == key_type;
}
int compareKeys(QPDFObjectHandle a, QPDFObjectHandle b) const;
QPDF& qpdf;
int split_threshold{32};
qpdf::Dictionary tree_root;
const qpdf_object_type_e key_type;
const std::string items_key;
const std::function<bool(QPDFObjectHandle const&)> value_valid;
bool auto_repair{true};
size_t error_count{0};
};
#endif // NNTREE_HH