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