Commit 5072238867f37f6c6ecd53dab06e42ea2763cf56

Authored by m-holger
1 parent e186da17

Refactor QPDF_Array::getAsVector

libqpdf/QPDFObjectHandle.cc
@@ -888,15 +888,14 @@ QPDFObjectHandle::getArrayAsMatrix() @@ -888,15 +888,14 @@ QPDFObjectHandle::getArrayAsMatrix()
888 std::vector<QPDFObjectHandle> 888 std::vector<QPDFObjectHandle>
889 QPDFObjectHandle::getArrayAsVector() 889 QPDFObjectHandle::getArrayAsVector()
890 { 890 {
891 - std::vector<QPDFObjectHandle> result;  
892 auto array = asArray(); 891 auto array = asArray();
893 if (array) { 892 if (array) {
894 - array->getAsVector(result); 893 + return array->getAsVector();
895 } else { 894 } else {
896 typeWarning("array", "treating as empty"); 895 typeWarning("array", "treating as empty");
897 QTC::TC("qpdf", "QPDFObjectHandle array treating as empty vector"); 896 QTC::TC("qpdf", "QPDFObjectHandle array treating as empty vector");
898 } 897 }
899 - return result; 898 + return {};
900 } 899 }
901 900
902 // Array mutators 901 // Array mutators
libqpdf/QPDF_Array.cc
@@ -172,16 +172,20 @@ QPDF_Array::at(int n) const noexcept @@ -172,16 +172,20 @@ QPDF_Array::at(int n) const noexcept
172 } 172 }
173 } 173 }
174 174
175 -void  
176 -QPDF_Array::getAsVector(std::vector<QPDFObjectHandle>& v) const 175 +std::vector<QPDFObjectHandle>
  176 +QPDF_Array::getAsVector() const
177 { 177 {
178 if (sparse) { 178 if (sparse) {
179 - int size = sp_elements.size();  
180 - for (int i = 0; i < size; ++i) {  
181 - v.push_back(at(i)); 179 + std::vector<QPDFObjectHandle> v;
  180 + v.reserve(size_t(size()));
  181 + for (auto const& item: sp_elements.elements) {
  182 + v.resize(size_t(item.first), null_oh);
  183 + v.push_back(item.second);
182 } 184 }
  185 + v.resize(size_t(size()), null_oh);
  186 + return v;
183 } else { 187 } else {
184 - v = std::vector<QPDFObjectHandle>(elements.cbegin(), elements.cend()); 188 + return {elements.cbegin(), elements.cend()};
185 } 189 }
186 } 190 }
187 191
libqpdf/qpdf/QPDF_Array.hh
@@ -29,7 +29,7 @@ class QPDF_Array: public QPDFValue @@ -29,7 +29,7 @@ class QPDF_Array: public QPDFValue
29 } 29 }
30 QPDFObjectHandle at(int n) const noexcept; 30 QPDFObjectHandle at(int n) const noexcept;
31 bool setAt(int n, QPDFObjectHandle const& oh); 31 bool setAt(int n, QPDFObjectHandle const& oh);
32 - void getAsVector(std::vector<QPDFObjectHandle>&) const; 32 + std::vector<QPDFObjectHandle> getAsVector() const;
33 void setFromVector(std::vector<QPDFObjectHandle> const& items); 33 void setFromVector(std::vector<QPDFObjectHandle> const& items);
34 void setFromVector(std::vector<std::shared_ptr<QPDFObject>>&& items); 34 void setFromVector(std::vector<std::shared_ptr<QPDFObject>>&& items);
35 bool insert(int at, QPDFObjectHandle const& item); 35 bool insert(int at, QPDFObjectHandle const& item);