2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-17 01:55:09 +00:00
qpdf/libqpdf/SparseOHArray.cc

135 lines
3.2 KiB
C++
Raw Normal View History

2019-08-17 22:54:24 +00:00
#include <qpdf/SparseOHArray.hh>
2019-08-17 22:54:24 +00:00
#include <stdexcept>
void
SparseOHArray::append(QPDFObjectHandle oh)
{
if (!oh.isDirectNull()) {
this->elements[this->n_elements] = oh.getObj();
2019-08-17 22:54:24 +00:00
}
++this->n_elements;
}
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
SparseOHArray::at(int idx) const
2019-08-17 22:54:24 +00:00
{
if (idx < 0 || idx >= this->n_elements) {
throw std::logic_error(
"INTERNAL ERROR: bounds error accessing SparseOHArray element");
2019-08-17 22:54:24 +00:00
}
auto const& iter = this->elements.find(idx);
if (iter == this->elements.end()) {
2019-08-17 22:54:24 +00:00
return QPDFObjectHandle::newNull();
} else {
2019-08-17 22:54:24 +00:00
return (*iter).second;
}
}
void
SparseOHArray::remove_last()
{
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);
}
void
SparseOHArray::disconnect()
{
for (auto& iter: this->elements) {
QPDFObjectHandle::DisconnectAccess::disconnect(iter.second);
}
}
2019-08-17 22:54:24 +00:00
void
SparseOHArray::setAt(int idx, QPDFObjectHandle oh)
2019-08-17 22:54:24 +00:00
{
if (idx >= this->n_elements) {
throw std::logic_error("bounds error setting item in SparseOHArray");
2019-08-17 22:54:24 +00:00
}
if (oh.isDirectNull()) {
2019-08-17 22:54:24 +00:00
this->elements.erase(idx);
} else {
this->elements[idx] = oh.getObj();
2019-08-17 22:54:24 +00:00
}
}
void
SparseOHArray::erase(int idx)
2019-08-17 22:54:24 +00:00
{
if (idx >= this->n_elements) {
throw std::logic_error("bounds error erasing item from SparseOHArray");
2019-08-17 22:54:24 +00:00
}
decltype(this->elements) dest;
for (auto const& iter: this->elements) {
if (iter.first < idx) {
dest.insert(iter);
} else if (iter.first > idx) {
dest[iter.first - 1] = iter.second;
2019-08-17 22:54:24 +00:00
}
}
this->elements = dest;
--this->n_elements;
}
void
SparseOHArray::insert(int idx, QPDFObjectHandle oh)
2019-08-17 22:54:24 +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");
} else if (idx == this->n_elements) {
2019-08-17 22:54:24 +00:00
// Allow inserting to the last position
append(oh);
} else {
decltype(this->elements) dest;
for (auto const& iter: this->elements) {
if (iter.first < idx) {
dest.insert(iter);
} else {
dest[iter.first + 1] = iter.second;
2019-08-17 22:54:24 +00:00
}
}
this->elements = dest;
this->elements[idx] = oh.getObj();
2019-08-17 22:54:24 +00:00
++this->n_elements;
}
}
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] =
value->getObjGen().isIndirect() ? value : value->copy();
2022-11-14 22:06:04 +00:00
}
return result;
}
SparseOHArray::const_iterator
SparseOHArray::begin() const
{
return this->elements.begin();
}
SparseOHArray::const_iterator
SparseOHArray::end() const
{
return this->elements.end();
}