2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-01-03 07:12:28 +00:00

Remove SparseOHArray

This commit is contained in:
m-holger 2023-03-29 19:52:03 +01:00
parent d3f2dc322b
commit a7b6975132
7 changed files with 51 additions and 100 deletions

View File

@ -115,7 +115,6 @@ set(libqpdf_SOURCES
ResourceFinder.cc ResourceFinder.cc
SecureRandomDataProvider.cc SecureRandomDataProvider.cc
SF_FlateLzwDecode.cc SF_FlateLzwDecode.cc
SparseOHArray.cc
qpdf-c.cc qpdf-c.cc
qpdfjob-c.cc qpdfjob-c.cc
qpdflogger-c.cc) qpdflogger-c.cc)

View File

@ -23,7 +23,6 @@
#include <qpdf/QPDF_Stream.hh> #include <qpdf/QPDF_Stream.hh>
#include <qpdf/QPDF_String.hh> #include <qpdf/QPDF_String.hh>
#include <qpdf/QPDF_Unresolved.hh> #include <qpdf/QPDF_Unresolved.hh>
#include <qpdf/SparseOHArray.hh>
#include <qpdf/QIntC.hh> #include <qpdf/QIntC.hh>
#include <qpdf/QTC.hh> #include <qpdf/QTC.hh>

View File

@ -1,5 +1,6 @@
#include <qpdf/QPDF_Array.hh> #include <qpdf/QPDF_Array.hh>
#include <qpdf/QPDFObjectHandle.hh>
#include <qpdf/QPDFObject_private.hh> #include <qpdf/QPDFObject_private.hh>
static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull(); static const QPDFObjectHandle null_oh = QPDFObjectHandle::newNull();
@ -24,6 +25,20 @@ QPDF_Array::checkOwnership(QPDFObjectHandle const& item) const
} }
} }
QPDF_Array::QPDF_Array() :
QPDFValue(::ot_array, "array")
{
}
QPDF_Array::QPDF_Array(QPDF_Array const& other) :
QPDFValue(::ot_array, "array"),
sparse(other.sparse),
sp_size(other.sp_size),
sp_elements(other.sp_elements),
elements(other.elements)
{
}
QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle> const& v) : QPDF_Array::QPDF_Array(std::vector<QPDFObjectHandle> const& v) :
QPDFValue(::ot_array, "array") QPDFValue(::ot_array, "array")
{ {
@ -36,34 +51,18 @@ QPDF_Array::QPDF_Array(
sparse(sparse) sparse(sparse)
{ {
if (sparse) { if (sparse) {
sp_elements = SparseOHArray();
for (auto&& item: v) { for (auto&& item: v) {
if (item->getTypeCode() != ::ot_null || if (item->getTypeCode() != ::ot_null ||
item->getObjGen().isIndirect()) { item->getObjGen().isIndirect()) {
sp_elements.elements[sp_elements.n_elements] = std::move(item); sp_elements[sp_size] = std::move(item);
} }
++sp_elements.n_elements; ++sp_size;
} }
} else { } else {
elements = std::move(v); elements = std::move(v);
} }
} }
QPDF_Array::QPDF_Array(SparseOHArray const& items) :
QPDFValue(::ot_array, "array"),
sparse(true),
sp_elements(items)
{
}
QPDF_Array::QPDF_Array(std::vector<std::shared_ptr<QPDFObject>> const& items) :
QPDFValue(::ot_array, "array"),
sparse(false),
elements(items)
{
}
std::shared_ptr<QPDFObject> std::shared_ptr<QPDFObject>
QPDF_Array::create(std::vector<QPDFObjectHandle> const& items) QPDF_Array::create(std::vector<QPDFObjectHandle> const& items)
{ {
@ -77,37 +76,21 @@ QPDF_Array::create(
return do_create(new QPDF_Array(std::move(items), sparse)); return do_create(new QPDF_Array(std::move(items), sparse));
} }
std::shared_ptr<QPDFObject>
QPDF_Array::create(SparseOHArray const& items)
{
return do_create(new QPDF_Array(items));
}
std::shared_ptr<QPDFObject>
QPDF_Array::create(std::vector<std::shared_ptr<QPDFObject>> const& items)
{
return do_create(new QPDF_Array(items));
}
std::shared_ptr<QPDFObject> std::shared_ptr<QPDFObject>
QPDF_Array::copy(bool shallow) QPDF_Array::copy(bool shallow)
{ {
if (shallow) { if (shallow) {
if (sparse) { return do_create(new QPDF_Array(*this));
return create(sp_elements);
} else {
return create(elements);
}
} else { } else {
if (sparse) { if (sparse) {
SparseOHArray result; QPDF_Array* result = new QPDF_Array();
result.n_elements = sp_elements.n_elements; result->sp_size = sp_size;
for (auto const& element: sp_elements.elements) { for (auto const& element: sp_elements) {
auto const& obj = element.second; auto const& obj = element.second;
result.elements[element.first] = result->sp_elements[element.first] =
obj->getObjGen().isIndirect() ? obj : obj->copy(); obj->getObjGen().isIndirect() ? obj : obj->copy();
} }
return create(std::move(result)); return do_create(result);
} else { } else {
std::vector<std::shared_ptr<QPDFObject>> result; std::vector<std::shared_ptr<QPDFObject>> result;
result.reserve(elements.size()); result.reserve(elements.size());
@ -118,7 +101,7 @@ QPDF_Array::copy(bool shallow)
: element->copy()) : element->copy())
: element); : element);
} }
return create(std::move(result)); return create(std::move(result), false);
} }
} }
} }
@ -127,7 +110,7 @@ void
QPDF_Array::disconnect() QPDF_Array::disconnect()
{ {
if (sparse) { if (sparse) {
for (auto& item: sp_elements.elements) { for (auto& item: sp_elements) {
auto& obj = item.second; auto& obj = item.second;
if (!obj->getObjGen().isIndirect()) { if (!obj->getObjGen().isIndirect()) {
obj->disconnect(); obj->disconnect();
@ -147,8 +130,7 @@ QPDF_Array::unparse()
{ {
if (sparse) { if (sparse) {
std::string result = "[ "; std::string result = "[ ";
int size = sp_elements.size(); for (int i = 0; i < sp_size; ++i) {
for (int i = 0; i < size; ++i) {
result += at(i).unparse(); result += at(i).unparse();
result += " "; result += " ";
} }
@ -171,8 +153,7 @@ QPDF_Array::getJSON(int json_version)
{ {
if (sparse) { if (sparse) {
JSON j = JSON::makeArray(); JSON j = JSON::makeArray();
int size = sp_elements.size(); for (int i = 0; i < sp_size; ++i) {
for (int i = 0; i < size; ++i) {
j.addArrayElement(at(i).getJSON(json_version)); j.addArrayElement(at(i).getJSON(json_version));
} }
return j; return j;
@ -192,8 +173,8 @@ QPDF_Array::at(int n) const noexcept
if (n < 0 || n >= size()) { if (n < 0 || n >= size()) {
return {}; return {};
} else if (sparse) { } else if (sparse) {
auto const& iter = sp_elements.elements.find(n); auto const& iter = sp_elements.find(n);
return iter == sp_elements.elements.end() ? null_oh : (*iter).second; return iter == sp_elements.end() ? null_oh : (*iter).second;
} else { } else {
return elements[size_t(n)]; return elements[size_t(n)];
} }
@ -205,7 +186,7 @@ QPDF_Array::getAsVector() const
if (sparse) { if (sparse) {
std::vector<QPDFObjectHandle> v; std::vector<QPDFObjectHandle> v;
v.reserve(size_t(size())); v.reserve(size_t(size()));
for (auto const& item: sp_elements.elements) { for (auto const& item: sp_elements) {
v.resize(size_t(item.first), null_oh); v.resize(size_t(item.first), null_oh);
v.push_back(item.second); v.push_back(item.second);
} }
@ -224,7 +205,7 @@ QPDF_Array::setAt(int at, QPDFObjectHandle const& oh)
} }
checkOwnership(oh); checkOwnership(oh);
if (sparse) { if (sparse) {
sp_elements.elements[at] = oh.getObj(); sp_elements[at] = oh.getObj();
} else { } else {
elements[size_t(at)] = oh.getObj(); elements[size_t(at)] = oh.getObj();
} }
@ -254,19 +235,19 @@ QPDF_Array::insert(int at, QPDFObjectHandle const& item)
} else { } else {
checkOwnership(item); checkOwnership(item);
if (sparse) { if (sparse) {
auto iter = sp_elements.elements.crbegin(); auto iter = sp_elements.crbegin();
while (iter != sp_elements.elements.crend()) { while (iter != sp_elements.crend()) {
auto key = (iter++)->first; auto key = (iter++)->first;
if (key >= at) { if (key >= at) {
auto nh = sp_elements.elements.extract(key); auto nh = sp_elements.extract(key);
++nh.key(); ++nh.key();
sp_elements.elements.insert(std::move(nh)); sp_elements.insert(std::move(nh));
} else { } else {
break; break;
} }
} }
sp_elements.elements[at] = item.getObj(); sp_elements[at] = item.getObj();
++sp_elements.n_elements; ++sp_size;
} else { } else {
elements.insert(elements.cbegin() + at, item.getObj()); elements.insert(elements.cbegin() + at, item.getObj());
} }
@ -279,7 +260,7 @@ QPDF_Array::push_back(QPDFObjectHandle const& item)
{ {
checkOwnership(item); checkOwnership(item);
if (sparse) { if (sparse) {
sp_elements.elements[sp_elements.n_elements++] = item.getObj(); sp_elements[sp_size++] = item.getObj();
} else { } else {
elements.push_back(item.getObj()); elements.push_back(item.getObj());
} }
@ -292,20 +273,20 @@ QPDF_Array::erase(int at)
return false; return false;
} }
if (sparse) { if (sparse) {
auto end = sp_elements.elements.end(); auto end = sp_elements.end();
if (auto iter = sp_elements.elements.lower_bound(at); iter != end) { if (auto iter = sp_elements.lower_bound(at); iter != end) {
if (iter->first == at) { if (iter->first == at) {
iter++; iter++;
sp_elements.elements.erase(at); sp_elements.erase(at);
} }
while (iter != end) { while (iter != end) {
auto nh = sp_elements.elements.extract(iter++); auto nh = sp_elements.extract(iter++);
--nh.key(); --nh.key();
sp_elements.elements.insert(std::move(nh)); sp_elements.insert(std::move(nh));
} }
} }
--sp_elements.n_elements; --sp_size;
} else { } else {
elements.erase(elements.cbegin() + at); elements.erase(elements.cbegin() + at);
} }

View File

@ -1 +0,0 @@

View File

@ -3,7 +3,7 @@
#include <qpdf/QPDFValue.hh> #include <qpdf/QPDFValue.hh>
#include <qpdf/SparseOHArray.hh> #include <map>
#include <vector> #include <vector>
class QPDF_Array: public QPDFValue class QPDF_Array: public QPDFValue
@ -14,9 +14,6 @@ class QPDF_Array: public QPDFValue
create(std::vector<QPDFObjectHandle> const& items); create(std::vector<QPDFObjectHandle> const& items);
static std::shared_ptr<QPDFObject> static std::shared_ptr<QPDFObject>
create(std::vector<std::shared_ptr<QPDFObject>>&& items, bool sparse); create(std::vector<std::shared_ptr<QPDFObject>>&& items, bool sparse);
static std::shared_ptr<QPDFObject> create(SparseOHArray const& items);
static std::shared_ptr<QPDFObject>
create(std::vector<std::shared_ptr<QPDFObject>> const& items);
virtual std::shared_ptr<QPDFObject> copy(bool shallow = false); virtual std::shared_ptr<QPDFObject> copy(bool shallow = false);
virtual std::string unparse(); virtual std::string unparse();
virtual JSON getJSON(int json_version); virtual JSON getJSON(int json_version);
@ -25,7 +22,7 @@ class QPDF_Array: public QPDFValue
int int
size() const noexcept size() const noexcept
{ {
return sparse ? sp_elements.size() : int(elements.size()); return sparse ? sp_size : int(elements.size());
} }
QPDFObjectHandle at(int n) const noexcept; QPDFObjectHandle at(int n) const noexcept;
bool setAt(int n, QPDFObjectHandle const& oh); bool setAt(int n, QPDFObjectHandle const& oh);
@ -36,15 +33,16 @@ class QPDF_Array: public QPDFValue
bool erase(int at); bool erase(int at);
private: private:
QPDF_Array();
QPDF_Array(QPDF_Array const&);
QPDF_Array(std::vector<QPDFObjectHandle> const& items); QPDF_Array(std::vector<QPDFObjectHandle> const& items);
QPDF_Array(std::vector<std::shared_ptr<QPDFObject>>&& items, bool sparse); QPDF_Array(std::vector<std::shared_ptr<QPDFObject>>&& items, bool sparse);
QPDF_Array(SparseOHArray const& items);
QPDF_Array(std::vector<std::shared_ptr<QPDFObject>> const& items);
void checkOwnership(QPDFObjectHandle const& item) const; void checkOwnership(QPDFObjectHandle const& item) const;
bool sparse{false}; bool sparse{false};
SparseOHArray sp_elements; int sp_size{0};
std::map<int, std::shared_ptr<QPDFObject>> sp_elements;
std::vector<std::shared_ptr<QPDFObject>> elements; std::vector<std::shared_ptr<QPDFObject>> elements;
}; };

View File

@ -1,26 +0,0 @@
#ifndef QPDF_SPARSEOHARRAY_HH
#define QPDF_SPARSEOHARRAY_HH
#include <qpdf/QPDFObjectHandle.hh>
#include <qpdf/QPDFObject_private.hh>
#include <map>
class QPDF_Array;
class SparseOHArray
{
public:
SparseOHArray() = default;
int
size() const noexcept
{
return n_elements;
}
private:
friend class QPDF_Array;
std::map<int, std::shared_ptr<QPDFObject>> elements;
int n_elements{0};
};
#endif // QPDF_SPARSEOHARRAY_HH

View File

@ -1,6 +1,7 @@
#include <qpdf/assert_test.h> #include <qpdf/assert_test.h>
#include <qpdf/QPDFObjectHandle.hh> #include <qpdf/QPDFObjectHandle.hh>
#include <qpdf/QPDFObject_private.hh>
#include <qpdf/QPDF_Array.hh> #include <qpdf/QPDF_Array.hh>
#include <iostream> #include <iostream>