MCPcopy Index your code
hub / github.com/plotly/plotly.js / findMedian

Function findMedian

stackgl_modules/index.js:7123–7228  ·  view source on GitHub ↗
(d, axis, start, end, boxes, ids)

Source from the content-addressed store, hash-verified

7121//Find median using quick select algorithm
7122// takes O(n) time with high probability
7123function findMedian(d, axis, start, end, boxes, ids) {
7124 if(end <= start+1) {
7125 return start
7126 }
7127
7128 var lo = start
7129 var hi = end
7130 var mid = ((end + start) >>> 1)
7131 var elemSize = 2*d
7132 var pivot = mid
7133 var value = boxes[elemSize*mid+axis]
7134
7135 while(lo < hi) {
7136 if(hi - lo < PARTITION_THRESHOLD) {
7137 insertionSort(d, axis, lo, hi, boxes, ids)
7138 value = boxes[elemSize*mid+axis]
7139 break
7140 }
7141
7142 //Select pivot using median-of-3
7143 var count = hi - lo
7144 var pivot0 = (Math.random()*count+lo)|0
7145 var value0 = boxes[elemSize*pivot0 + axis]
7146 var pivot1 = (Math.random()*count+lo)|0
7147 var value1 = boxes[elemSize*pivot1 + axis]
7148 var pivot2 = (Math.random()*count+lo)|0
7149 var value2 = boxes[elemSize*pivot2 + axis]
7150 if(value0 <= value1) {
7151 if(value2 >= value1) {
7152 pivot = pivot1
7153 value = value1
7154 } else if(value0 >= value2) {
7155 pivot = pivot0
7156 value = value0
7157 } else {
7158 pivot = pivot2
7159 value = value2
7160 }
7161 } else {
7162 if(value1 >= value2) {
7163 pivot = pivot1
7164 value = value1
7165 } else if(value2 >= value0) {
7166 pivot = pivot0
7167 value = value0
7168 } else {
7169 pivot = pivot2
7170 value = value2
7171 }
7172 }
7173
7174 //Swap pivot to end of array
7175 var aPtr = elemSize * (hi-1)
7176 var bPtr = elemSize * pivot
7177 for(var i=0; i<elemSize; ++i, ++aPtr, ++bPtr) {
7178 var x = boxes[aPtr]
7179 boxes[aPtr] = boxes[bPtr]
7180 boxes[bPtr] = x

Callers 1

boxIntersectIterFunction · 0.85

Calls 1

insertionSortFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…