2019-08-17 22:54:24 +00:00
|
|
|
#include <qpdf/SparseOHArray.hh>
|
2022-02-04 21:31:31 +00:00
|
|
|
|
2019-08-17 22:54:24 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
void
|
|
|
|
SparseOHArray::append(QPDFObjectHandle oh)
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (!oh.isDirectNull()) {
|
2022-11-16 18:50:13 +00:00
|
|
|
this->elements[this->n_elements] = oh.getObj();
|
2019-08-17 22:54:24 +00:00
|
|
|
}
|
|
|
|
++this->n_elements;
|
|
|
|
}
|
|
|
|
|
2022-12-19 11:41:09 +00:00
|
|
|
void
|
|
|
|
SparseOHArray::append(std::shared_ptr<QPDFObject>&& obj)
|
|
|
|
{
|
|
|
|
if (obj->getTypeCode() != ::ot_null || !obj->getObjGen().isIndirect()) {
|
|
|
|
this->elements[this->n_elements] = std::move(obj);
|
|
|
|
}
|
|
|
|
++this->n_elements;
|
|
|
|
}
|
|
|
|
|
2019-08-17 22:54:24 +00:00
|
|
|
QPDFObjectHandle
|
2022-11-16 18:50:13 +00:00
|
|
|
SparseOHArray::at(int idx) const
|
2019-08-17 22:54:24 +00:00
|
|
|
{
|
2022-11-16 18:50:13 +00:00
|
|
|
if (idx < 0 || idx >= this->n_elements) {
|
2022-02-08 14:18:08 +00:00
|
|
|
throw std::logic_error(
|
|
|
|
"INTERNAL ERROR: bounds error accessing SparseOHArray element");
|
2019-08-17 22:54:24 +00:00
|
|
|
}
|
2020-04-09 00:14:04 +00:00
|
|
|
auto const& iter = this->elements.find(idx);
|
2022-04-02 21:14:10 +00:00
|
|
|
if (iter == this->elements.end()) {
|
2019-08-17 22:54:24 +00:00
|
|
|
return QPDFObjectHandle::newNull();
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2019-08-17 22:54:24 +00:00
|
|
|
return (*iter).second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SparseOHArray::remove_last()
|
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (this->n_elements == 0) {
|
|
|
|
throw std::logic_error("INTERNAL ERROR: attempt to remove"
|
|
|
|
" last item from empty SparseOHArray");
|
2019-08-17 22:54:24 +00:00
|
|
|
}
|
|
|
|
--this->n_elements;
|
|
|
|
this->elements.erase(this->n_elements);
|
|
|
|
}
|
|
|
|
|
2022-09-07 20:49:31 +00:00
|
|
|
void
|
2022-09-08 15:06:15 +00:00
|
|
|
SparseOHArray::disconnect()
|
2022-09-07 20:49:31 +00:00
|
|
|
{
|
|
|
|
for (auto& iter: this->elements) {
|
2022-09-08 15:06:15 +00:00
|
|
|
QPDFObjectHandle::DisconnectAccess::disconnect(iter.second);
|
2022-09-07 20:49:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 22:54:24 +00:00
|
|
|
void
|
2022-11-16 18:50:13 +00:00
|
|
|
SparseOHArray::setAt(int idx, QPDFObjectHandle oh)
|
2019-08-17 22:54:24 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (idx >= this->n_elements) {
|
2022-02-08 14:18:08 +00:00
|
|
|
throw std::logic_error("bounds error setting item in SparseOHArray");
|
2019-08-17 22:54:24 +00:00
|
|
|
}
|
2022-04-02 21:14:10 +00:00
|
|
|
if (oh.isDirectNull()) {
|
2019-08-17 22:54:24 +00:00
|
|
|
this->elements.erase(idx);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2022-11-16 18:50:13 +00:00
|
|
|
this->elements[idx] = oh.getObj();
|
2019-08-17 22:54:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-11-16 18:50:13 +00:00
|
|
|
SparseOHArray::erase(int idx)
|
2019-08-17 22:54:24 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (idx >= this->n_elements) {
|
2022-02-08 14:18:08 +00:00
|
|
|
throw std::logic_error("bounds error erasing item from SparseOHArray");
|
2019-08-17 22:54:24 +00:00
|
|
|
}
|
2020-04-03 00:10:31 +00:00
|
|
|
decltype(this->elements) dest;
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& iter: this->elements) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (iter.first < idx) {
|
2020-04-09 00:14:04 +00:00
|
|
|
dest.insert(iter);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (iter.first > idx) {
|
2020-04-09 00:14:04 +00:00
|
|
|
dest[iter.first - 1] = iter.second;
|
2019-08-17 22:54:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this->elements = dest;
|
|
|
|
--this->n_elements;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2022-11-16 18:50:13 +00:00
|
|
|
SparseOHArray::insert(int idx, QPDFObjectHandle oh)
|
2019-08-17 22:54:24 +00:00
|
|
|
{
|
2022-04-02 21:14:10 +00:00
|
|
|
if (idx > this->n_elements) {
|
2019-08-17 22:54:24 +00:00
|
|
|
throw std::logic_error("bounds error inserting item to SparseOHArray");
|
2022-04-02 21:14:10 +00:00
|
|
|
} else if (idx == this->n_elements) {
|
2019-08-17 22:54:24 +00:00
|
|
|
// Allow inserting to the last position
|
|
|
|
append(oh);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2020-04-03 00:10:31 +00:00
|
|
|
decltype(this->elements) dest;
|
2022-04-30 13:43:07 +00:00
|
|
|
for (auto const& iter: this->elements) {
|
2022-04-02 21:14:10 +00:00
|
|
|
if (iter.first < idx) {
|
2020-04-09 00:14:04 +00:00
|
|
|
dest.insert(iter);
|
2022-04-02 21:14:10 +00:00
|
|
|
} else {
|
2020-04-09 00:14:04 +00:00
|
|
|
dest[iter.first + 1] = iter.second;
|
2019-08-17 22:54:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this->elements = dest;
|
2022-11-16 18:50:13 +00:00
|
|
|
this->elements[idx] = oh.getObj();
|
2019-08-17 22:54:24 +00:00
|
|
|
++this->n_elements;
|
|
|
|
}
|
|
|
|
}
|
2019-08-18 02:28:19 +00:00
|
|
|
|
2022-11-14 22:06:04 +00:00
|
|
|
SparseOHArray
|
|
|
|
SparseOHArray::copy()
|
|
|
|
{
|
|
|
|
SparseOHArray result;
|
|
|
|
result.n_elements = this->n_elements;
|
|
|
|
for (auto const& element: this->elements) {
|
|
|
|
auto value = element.second;
|
|
|
|
result.elements[element.first] =
|
2022-11-16 18:50:13 +00:00
|
|
|
value->getObjGen().isIndirect() ? value : value->copy();
|
2022-11-14 22:06:04 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2019-08-18 02:28:19 +00:00
|
|
|
SparseOHArray::const_iterator
|
|
|
|
SparseOHArray::begin() const
|
|
|
|
{
|
|
|
|
return this->elements.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
SparseOHArray::const_iterator
|
|
|
|
SparseOHArray::end() const
|
|
|
|
{
|
|
|
|
return this->elements.end();
|
|
|
|
}
|