Commit 0b2a75ab3ca397b9bdb5e684b8ed13b35675cb6d

Authored by m-holger
1 parent b1647555

Refactor `QPDF::calculateHPageOffset`: simplify loops with range-based for, repl…

…ace manual index handling, revise min/max initialization to use `std::numeric_limits`, and optimize shared identifier assignments.
Showing 1 changed file with 22 additions and 21 deletions
libqpdf/QPDF_linearization.cc
... ... @@ -1484,18 +1484,19 @@ QPDF::calculateHPageOffset(QPDFWriter::NewObjTable const& new_obj, QPDFWriter::O
1484 1484  
1485 1485 // Calculate minimum and maximum values for number of objects per page and page length.
1486 1486  
1487   - int min_nobjects = cphe.at(0).nobjects;
1488   - int max_nobjects = min_nobjects;
1489   - int min_length = outputLengthNextN(pages.at(0).getObjectID(), min_nobjects, new_obj, obj);
1490   - int max_length = min_length;
1491   - int max_shared = cphe.at(0).nshared_objects;
  1487 + int min_nobjects = std::numeric_limits<int>::max();
  1488 + int max_nobjects = 0;
  1489 + int min_length = std::numeric_limits<int>::max();
  1490 + int max_length = 0;
  1491 + int max_shared = 0;
1492 1492  
1493 1493 HPageOffset& ph = m->page_offset_hints;
1494 1494 std::vector<HPageOffsetEntry>& phe = ph.entries;
1495 1495 // npages is the size of the existing pages array.
1496 1496 phe = std::vector<HPageOffsetEntry>(npages);
1497 1497  
1498   - for (unsigned int i = 0; i < npages; ++i) {
  1498 + size_t i = 0;
  1499 + for (auto& phe_i: phe) {
1499 1500 // Calculate values for each page, assigning full values to the delta items. They will be
1500 1501 // adjusted later.
1501 1502  
... ... @@ -1512,9 +1513,10 @@ QPDF::calculateHPageOffset(QPDFWriter::NewObjTable const&amp; new_obj, QPDFWriter::O
1512 1513 max_length = std::max(max_length, length);
1513 1514 max_shared = std::max(max_shared, nshared);
1514 1515  
1515   - phe.at(i).delta_nobjects = nobjects;
1516   - phe.at(i).delta_page_length = length;
1517   - phe.at(i).nshared_objects = nshared;
  1516 + phe_i.delta_nobjects = nobjects;
  1517 + phe_i.delta_page_length = length;
  1518 + phe_i.nshared_objects = nshared;
  1519 + ++i;
1518 1520 }
1519 1521  
1520 1522 ph.min_nobjects = min_nobjects;
... ... @@ -1533,23 +1535,22 @@ QPDF::calculateHPageOffset(QPDFWriter::NewObjTable const&amp; new_obj, QPDFWriter::O
1533 1535 ph.nbits_delta_content_length = ph.nbits_delta_page_length;
1534 1536 ph.min_content_length = ph.min_page_length;
1535 1537  
1536   - for (size_t i = 0; i < npages; ++i) {
  1538 + i = 0;
  1539 + for (auto& phe_i: phe) {
1537 1540 // Adjust delta entries
1538   - if ((phe.at(i).delta_nobjects < min_nobjects) ||
1539   - (phe.at(i).delta_page_length < min_length)) {
  1541 + if (phe_i.delta_nobjects < min_nobjects || phe_i.delta_page_length < min_length) {
1540 1542 stopOnError(
1541 1543 "found too small delta nobjects or delta page length while writing "
1542 1544 "linearization data");
1543 1545 }
1544   - phe.at(i).delta_nobjects -= min_nobjects;
1545   - phe.at(i).delta_page_length -= min_length;
1546   - phe.at(i).delta_content_length = phe.at(i).delta_page_length;
1547   -
1548   - auto nso = toS(cphe.at(i).nshared_objects);
1549   - for (size_t j = 0; j < nso; ++j) {
1550   - phe.at(i).shared_identifiers.push_back(cphe.at(i).shared_identifiers.at(j));
1551   - phe.at(i).shared_numerators.push_back(0);
1552   - }
  1546 + phe_i.delta_nobjects -= min_nobjects;
  1547 + phe_i.delta_page_length -= min_length;
  1548 + phe_i.delta_content_length = phe_i.delta_page_length;
  1549 +
  1550 + auto& si = cphe.at(i).shared_identifiers;
  1551 + phe_i.shared_identifiers.insert(phe_i.shared_identifiers.end(), si.begin(), si.end());
  1552 + phe_i.shared_numerators.insert(phe_i.shared_numerators.end(), si.size(), 0);
  1553 + ++i;
1553 1554 }
1554 1555 }
1555 1556  
... ...