Convert an index pair (lists index, sublist index) into a single index number that corresponds to the position of the value in the sorted list. Many queries require the index be built. Details of the index are described in ``SortedList._build_index``. Indexi
(self, pos, idx)
| 518 | |
| 519 | |
| 520 | def _loc(self, pos, idx): |
| 521 | """Convert an index pair (lists index, sublist index) into a single |
| 522 | index number that corresponds to the position of the value in the |
| 523 | sorted list. |
| 524 | |
| 525 | Many queries require the index be built. Details of the index are |
| 526 | described in ``SortedList._build_index``. |
| 527 | |
| 528 | Indexing requires traversing the tree from a leaf node to the root. The |
| 529 | parent of each node is easily computable at ``(pos - 1) // 2``. |
| 530 | |
| 531 | Left-child nodes are always at odd indices and right-child nodes are |
| 532 | always at even indices. |
| 533 | |
| 534 | When traversing up from a right-child node, increment the total by the |
| 535 | left-child node. |
| 536 | |
| 537 | The final index is the sum from traversal and the index in the sublist. |
| 538 | |
| 539 | For example, using the index from ``SortedList._build_index``:: |
| 540 | |
| 541 | _index = 14 5 9 3 2 4 5 |
| 542 | _offset = 3 |
| 543 | |
| 544 | Tree:: |
| 545 | |
| 546 | 14 |
| 547 | 5 9 |
| 548 | 3 2 4 5 |
| 549 | |
| 550 | Converting an index pair (2, 3) into a single index involves iterating |
| 551 | like so: |
| 552 | |
| 553 | 1. Starting at the leaf node: offset + alpha = 3 + 2 = 5. We identify |
| 554 | the node as a left-child node. At such nodes, we simply traverse to |
| 555 | the parent. |
| 556 | |
| 557 | 2. At node 9, position 2, we recognize the node as a right-child node |
| 558 | and accumulate the left-child in our total. Total is now 5 and we |
| 559 | traverse to the parent at position 0. |
| 560 | |
| 561 | 3. Iteration ends at the root. |
| 562 | |
| 563 | The index is then the sum of the total and sublist index: 5 + 3 = 8. |
| 564 | |
| 565 | :param int pos: lists index |
| 566 | :param int idx: sublist index |
| 567 | :return: index in sorted list |
| 568 | |
| 569 | """ |
| 570 | if not pos: |
| 571 | return idx |
| 572 | |
| 573 | _index = self._index |
| 574 | |
| 575 | if not _index: |
| 576 | self._build_index() |
| 577 |
no test coverage detected