Commit 0aae54d3836107fdb9dc54faf0778bf262dd7e0a

Authored by m-holger
1 parent 50722388

Refactor QPDF_Array::setFromVector

libqpdf/QPDFObjectHandle.cc
@@ -916,11 +916,7 @@ QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item) @@ -916,11 +916,7 @@ QPDFObjectHandle::setArrayItem(int n, QPDFObjectHandle const& item)
916 void 916 void
917 QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items) 917 QPDFObjectHandle::setArrayFromVector(std::vector<QPDFObjectHandle> const& items)
918 { 918 {
919 - auto array = asArray();  
920 - if (array) {  
921 - for (auto const& item: items) {  
922 - checkOwnership(item);  
923 - } 919 + if (auto array = asArray()) {
924 array->setFromVector(items); 920 array->setFromVector(items);
925 } else { 921 } else {
926 typeWarning("array", "ignoring attempt to replace items"); 922 typeWarning("array", "ignoring attempt to replace items");
libqpdf/QPDF_Array.cc
@@ -35,7 +35,18 @@ QPDF_Array::QPDF_Array( @@ -35,7 +35,18 @@ QPDF_Array::QPDF_Array(
35 QPDFValue(::ot_array, "array"), 35 QPDFValue(::ot_array, "array"),
36 sparse(sparse) 36 sparse(sparse)
37 { 37 {
38 - setFromVector(std::move(v)); 38 + if (sparse) {
  39 + sp_elements = SparseOHArray();
  40 + for (auto&& item: v) {
  41 + if (item->getTypeCode() != ::ot_null ||
  42 + item->getObjGen().isIndirect()) {
  43 + sp_elements.elements[sp_elements.n_elements] = std::move(item);
  44 + }
  45 + ++sp_elements.n_elements;
  46 + }
  47 + } else {
  48 + elements = std::move(v);
  49 + }
39 } 50 }
40 51
41 QPDF_Array::QPDF_Array(SparseOHArray const& items) : 52 QPDF_Array::QPDF_Array(SparseOHArray const& items) :
@@ -207,33 +218,11 @@ QPDF_Array::setAt(int at, QPDFObjectHandle const&amp; oh) @@ -207,33 +218,11 @@ QPDF_Array::setAt(int at, QPDFObjectHandle const&amp; oh)
207 void 218 void
208 QPDF_Array::setFromVector(std::vector<QPDFObjectHandle> const& v) 219 QPDF_Array::setFromVector(std::vector<QPDFObjectHandle> const& v)
209 { 220 {
210 - if (sparse) {  
211 - sp_elements = SparseOHArray();  
212 - for (auto const& iter: v) {  
213 - sp_elements.elements[sp_elements.n_elements++] = iter.getObj();  
214 - }  
215 - } else {  
216 - elements.resize(0);  
217 - for (auto const& iter: v) {  
218 - elements.push_back(iter.getObj());  
219 - }  
220 - }  
221 -}  
222 -  
223 -void  
224 -QPDF_Array::setFromVector(std::vector<std::shared_ptr<QPDFObject>>&& v)  
225 -{  
226 - if (sparse) {  
227 - sp_elements = SparseOHArray();  
228 - for (auto&& item: v) {  
229 - if (item->getTypeCode() != ::ot_null ||  
230 - item->getObjGen().isIndirect()) {  
231 - sp_elements.elements[sp_elements.n_elements] = std::move(item);  
232 - }  
233 - ++sp_elements.n_elements;  
234 - }  
235 - } else {  
236 - elements = std::move(v); 221 + elements.resize(0);
  222 + elements.reserve(v.size());
  223 + for (auto const& item: v) {
  224 + checkOwnership(item);
  225 + elements.push_back(item.getObj());
237 } 226 }
238 } 227 }
239 228
libqpdf/qpdf/QPDF_Array.hh
@@ -31,7 +31,6 @@ class QPDF_Array: public QPDFValue @@ -31,7 +31,6 @@ class QPDF_Array: public QPDFValue
31 bool setAt(int n, QPDFObjectHandle const& oh); 31 bool setAt(int n, QPDFObjectHandle const& oh);
32 std::vector<QPDFObjectHandle> getAsVector() 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);  
35 bool insert(int at, QPDFObjectHandle const& item); 34 bool insert(int at, QPDFObjectHandle const& item);
36 void push_back(QPDFObjectHandle const& item); 35 void push_back(QPDFObjectHandle const& item);
37 bool erase(int at); 36 bool erase(int at);