Commit 59b25578884c103e5d569c4d28460f2cf787bf6c

Authored by m-holger
1 parent 3b87d569

Refactor `NNTree::split`: replace `getArrayNItems` and `getArrayItem` with `size…

…` and subscript operators, remove redundant debug traces, and improve code readability.
libqpdf/NNTree.cc
... ... @@ -260,22 +260,20 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list<PathElement>::iterato
260 260  
261 261 // Find the array we actually need to split, which is either this node's kids or items.
262 262 auto kids = to_split.getKey("/Kids");
263   - int nkids = kids.isArray() ? kids.getArrayNItems() : 0;
  263 + int nkids = kids.isArray() ? static_cast<int>(kids.size()) : 0;
264 264 auto items = to_split.getKey(impl.details.itemsKey());
265   - int nitems = items.isArray() ? items.getArrayNItems() : 0;
  265 + int nitems = items.isArray() ? static_cast<int>(items.size()) : 0;
266 266  
267 267 QPDFObjectHandle first_half;
268 268 int n = 0;
269 269 std::string key;
270 270 int threshold = 0;
271 271 if (nkids > 0) {
272   - QTC::TC("qpdf", "NNTree split kids");
273 272 first_half = kids;
274 273 n = nkids;
275 274 threshold = impl.split_threshold;
276 275 key = "/Kids";
277 276 } else if (nitems > 0) {
278   - QTC::TC("qpdf", "NNTree split items");
279 277 first_half = items;
280 278 n = nitems;
281 279 threshold = 2 * impl.split_threshold;
... ... @@ -288,8 +286,8 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list&lt;PathElement&gt;::iterato
288 286 return;
289 287 }
290 288  
291   - bool is_root = (parent == path.end());
292   - bool is_leaf = (nitems > 0);
  289 + bool is_root = parent == path.end();
  290 + bool is_leaf = nitems > 0;
293 291  
294 292 // CURRENT STATE: tree is in original state; iterator is valid and unchanged.
295 293  
... ... @@ -316,10 +314,8 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list&lt;PathElement&gt;::iterato
316 314 to_split.removeKey(impl.details.itemsKey());
317 315 to_split.replaceKey("/Kids", new_kids);
318 316 if (is_leaf) {
319   - QTC::TC("qpdf", "NNTree split root + leaf");
320 317 node = first_node;
321 318 } else {
322   - QTC::TC("qpdf", "NNTree split root + !leaf");
323 319 auto next = path.begin();
324 320 next->node = first_node;
325 321 }
... ... @@ -335,8 +331,8 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list&lt;PathElement&gt;::iterato
335 331 // array.
336 332 QPDFObjectHandle second_half = QPDFObjectHandle::newArray();
337 333 int start_idx = ((n / 2) & ~1);
338   - while (first_half.getArrayNItems() > start_idx) {
339   - second_half.appendItem(first_half.getArrayItem(start_idx));
  334 + while (std::cmp_greater(first_half.size(), start_idx)) {
  335 + second_half.appendItem(first_half[start_idx]);
340 336 first_half.eraseItem(start_idx);
341 337 }
342 338 resetLimits(to_split, parent);
... ... @@ -362,16 +358,13 @@ NNTreeIterator::split(QPDFObjectHandle to_split, std::list&lt;PathElement&gt;::iterato
362 358 if (old_idx >= start_idx) {
363 359 ++parent->kid_number;
364 360 if (is_leaf) {
365   - QTC::TC("qpdf", "NNTree split second half item");
366 361 setItemNumber(second_node, item_number - start_idx);
367 362 } else {
368   - QTC::TC("qpdf", "NNTree split second half kid");
369 363 cur_elem->node = second_node;
370 364 cur_elem->kid_number -= start_idx;
371 365 }
372 366 }
373 367 if (!is_root) {
374   - QTC::TC("qpdf", "NNTree split parent");
375 368 auto next = parent->node;
376 369 resetLimits(next, parent);
377 370 --parent;
... ...
qpdf/qpdf.testcov
... ... @@ -524,13 +524,6 @@ NNTree skip item at end of short items 0
524 524 NNTree skip invalid key 0
525 525 NNTree unable to determine limits 0
526 526 NNTree repair 0
527   -NNTree split root + leaf 0
528   -NNTree split root + !leaf 0
529   -NNTree split kids 0
530   -NNTree split items 0
531   -NNTree split second half item 0
532   -NNTree split parent 0
533   -NNTree split second half kid 0
534 527 NNTree limits didn't change 0
535 528 NNTree increment end() 0
536 529 NNTree insertAfter inserts first 0
... ...