| 1 | #include "hitMatrix.H" |
| 2 | |
| 3 | // Sort by dsPos |
| 4 | |
| 5 | inline |
| 6 | void |
| 7 | adjustHeap_dsPos(diagonalLine *L, uint32 p, uint32 n) { |
| 8 | uint32 q = L[p]._qsPos; |
| 9 | uint32 d = L[p]._dsPos; |
| 10 | #ifndef WITHOUT_DIAGONALID |
| 11 | uint32 l = L[p]._diagonalID; |
| 12 | #endif |
| 13 | uint32 c = (p << 1) + 1; // let c be the left child of p |
| 14 | |
| 15 | while (c < n) { |
| 16 | |
| 17 | // Find the larger of the two children |
| 18 | // |
| 19 | if ((c+1 < n) && (L[c]._dsPos < L[c+1]._dsPos)) |
| 20 | c++; |
| 21 | |
| 22 | // Does the node in question fit here? |
| 23 | // |
| 24 | if (d >= L[c]._dsPos) |
| 25 | break; |
| 26 | |
| 27 | // Else, swap the parent and the child |
| 28 | // |
| 29 | L[p]._qsPos = L[c]._qsPos; |
| 30 | L[p]._dsPos = L[c]._dsPos; |
| 31 | #ifndef WITHOUT_DIAGONALID |
| 32 | L[p]._diagonalID = L[c]._diagonalID; |
| 33 | #endif |
| 34 | |
| 35 | // Move down the tree |
| 36 | // |
| 37 | p = c; |
| 38 | c = (p << 1) + 1; |
| 39 | } |
| 40 | |
| 41 | L[p]._qsPos = q; |
| 42 | L[p]._dsPos = d; |
| 43 | #ifndef WITHOUT_DIAGONALID |
| 44 | L[p]._diagonalID = l; |
| 45 | #endif |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | hitMatrix::sort_dsPos(void) { |
| 50 | |
| 51 | if (_hitsLen > 1) { |
| 52 | |
| 53 | // Create the heap of lines. |
| 54 | // |
| 55 | for (uint32 i=_hitsLen/2; i--; ) |
| 56 | adjustHeap_dsPos(_hits, i, _hitsLen); |
| 57 | |
| 58 | // Interchange the new maximum with the element at the end of the tree |
| 59 | // |
| 60 | for (uint32 i=_hitsLen-1; i>0; i--) { |
| 61 | uint32 q = _hits[i]._qsPos; |
| 62 | uint32 d = _hits[i]._dsPos; |
| 63 | #ifndef WITHOUT_DIAGONALID |
| 64 | uint32 l = _hits[i]._diagonalID; |
| 65 | #endif |
| 66 | |
| 67 | _hits[i]._qsPos = _hits[0]._qsPos; |
| 68 | _hits[i]._dsPos = _hits[0]._dsPos; |
| 69 | #ifndef WITHOUT_DIAGONALID |
| 70 | _hits[i]._diagonalID = _hits[0]._diagonalID; |
| 71 | #endif |
| 72 | |
| 73 | _hits[0]._qsPos = q; |
| 74 | _hits[0]._dsPos = d; |
| 75 | #ifndef WITHOUT_DIAGONALID |
| 76 | _hits[0]._diagonalID = l; |
| 77 | #endif |
| 78 | |
| 79 | adjustHeap_dsPos(_hits, 0, i); |
| 80 | } |
| 81 | } |
| 82 | } |