in s2/edge_query.go [529:570]
func (e *EdgeQuery) findEdgesOptimized() {
e.initQueue()
// Repeatedly find the closest Cell to "target" and either split it into
// its four children or process all of its edges.
for e.queue.size() > 0 {
// We need to copy the top entry before removing it, and we need to
// remove it before adding any new entries to the queue.
entry := e.queue.pop()
if !entry.distance.less(e.distanceLimit) {
e.queue.reset() // Clear any remaining entries.
break
}
// If this is already known to be an index cell, just process it.
if entry.indexCell != nil {
e.processEdges(entry)
continue
}
// Otherwise split the cell into its four children. Before adding a
// child back to the queue, we first check whether it is empty. We do
// this in two seek operations rather than four by seeking to the key
// between children 0 and 1 and to the key between children 2 and 3.
id := entry.id
ch := id.Children()
e.iter.seek(ch[1].RangeMin())
if !e.iter.Done() && e.iter.CellID() <= ch[1].RangeMax() {
e.processOrEnqueueCell(ch[1])
}
if e.iter.Prev() && e.iter.CellID() >= id.RangeMin() {
e.processOrEnqueueCell(ch[0])
}
e.iter.seek(ch[3].RangeMin())
if !e.iter.Done() && e.iter.CellID() <= id.RangeMax() {
e.processOrEnqueueCell(ch[3])
}
if e.iter.Prev() && e.iter.CellID() >= ch[2].RangeMin() {
e.processOrEnqueueCell(ch[2])
}
}
}