2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-11-16 01:27:07 +00:00
qpdf/libqpdf/SparseOHArray.cc

150 lines
2.9 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>
SparseOHArray::SparseOHArray() :
n_elements(0)
{
}
size_t
SparseOHArray::size() const
{
return this->n_elements;
}
void
SparseOHArray::append(QPDFObjectHandle oh)
{
if (! oh.isDirectNull())
2019-08-17 22:54:24 +00:00
{
this->elements[this->n_elements] = oh;
}
++this->n_elements;
}
QPDFObjectHandle
SparseOHArray::at(size_t idx) const
{
if (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);
2019-08-17 22:54:24 +00:00
if (iter == this->elements.end())
{
return QPDFObjectHandle::newNull();
}
else
{
return (*iter).second;
}
}
void
SparseOHArray::remove_last()
{
if (this->n_elements == 0)
{
throw std::logic_error(
"INTERNAL ERROR: attempt to remove"
2019-08-17 22:54:24 +00:00
" last item from empty SparseOHArray");
}
--this->n_elements;
this->elements.erase(this->n_elements);
}
void
SparseOHArray::releaseResolved()
{
for (auto& iter: this->elements)
2019-08-17 22:54:24 +00:00
{
QPDFObjectHandle::ReleaseResolver::releaseResolved(iter.second);
2019-08-17 22:54:24 +00:00
}
}
void
SparseOHArray::setAt(size_t idx, QPDFObjectHandle oh)
{
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;
}
}
void
SparseOHArray::erase(size_t idx)
{
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)
2019-08-17 22:54:24 +00:00
{
if (iter.first < idx)
2019-08-17 22:54:24 +00:00
{
dest.insert(iter);
2019-08-17 22:54:24 +00:00
}
else if (iter.first > idx)
2019-08-17 22:54:24 +00:00
{
dest[iter.first - 1] = iter.second;
2019-08-17 22:54:24 +00:00
}
}
this->elements = dest;
--this->n_elements;
}
void
SparseOHArray::insert(size_t idx, QPDFObjectHandle oh)
{
if (idx > this->n_elements)
{
throw std::logic_error("bounds error inserting item to SparseOHArray");
}
else if (idx == this->n_elements)
{
// Allow inserting to the last position
append(oh);
}
else
{
decltype(this->elements) dest;
for (auto const& iter: this->elements)
2019-08-17 22:54:24 +00:00
{
if (iter.first < idx)
2019-08-17 22:54:24 +00:00
{
dest.insert(iter);
2019-08-17 22:54:24 +00:00
}
else
{
dest[iter.first + 1] = iter.second;
2019-08-17 22:54:24 +00:00
}
}
this->elements = dest;
this->elements[idx] = oh;
++this->n_elements;
}
}
SparseOHArray::const_iterator
SparseOHArray::begin() const
{
return this->elements.begin();
}
SparseOHArray::const_iterator
SparseOHArray::end() const
{
return this->elements.end();
}