* Run a single pass with the given chunk size. * * @ignore * * @param {array} arr - The array to run the pass on. * @param {function} comp - The comparison function. * @param {number} chk - The chunk size for this pass, i.e. the size of each partition being merged. * @param {array} result - T
(arr, comp, chk, result)
| 72 | * @param {array} result - The array to store the result in. |
| 73 | */ |
| 74 | function RunPass (arr, comp, chk, result) |
| 75 | { |
| 76 | var len = arr.length; |
| 77 | var i = 0; |
| 78 | |
| 79 | // Step size / double chunk size. |
| 80 | var dbl = chk * 2; |
| 81 | |
| 82 | // Bounds of the left and right chunks. |
| 83 | var l, r, e; |
| 84 | |
| 85 | // Iterators over the left and right chunk. |
| 86 | var li, ri; |
| 87 | |
| 88 | // Iterate over pairs of chunks. |
| 89 | for (l = 0; l < len; l += dbl) |
| 90 | { |
| 91 | r = l + chk; |
| 92 | e = r + chk; |
| 93 | |
| 94 | if (r > len) |
| 95 | { |
| 96 | r = len; |
| 97 | } |
| 98 | |
| 99 | if (e > len) |
| 100 | { |
| 101 | e = len; |
| 102 | } |
| 103 | |
| 104 | // Iterate both chunks in parallel. |
| 105 | li = l; |
| 106 | ri = r; |
| 107 | |
| 108 | while (true) |
| 109 | { |
| 110 | // Compare the chunks. |
| 111 | if (li < r && ri < e) |
| 112 | { |
| 113 | // This works for a regular `sort()` compatible comparator, |
| 114 | // but also for a simple comparator like: `a > b` |
| 115 | if (comp(arr[li], arr[ri]) <= 0) |
| 116 | { |
| 117 | result[i++] = arr[li++]; |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | result[i++] = arr[ri++]; |
| 122 | } |
| 123 | } |
| 124 | else if (li < r) |
| 125 | { |
| 126 | // Nothing to compare, just flush what's left. |
| 127 | result[i++] = arr[li++]; |
| 128 | } |
| 129 | else if (ri < e) |
| 130 | { |
| 131 | result[i++] = arr[ri++]; |
no outgoing calls
no test coverage detected
searching dependent graphs…