queryPoint recursively queries for intervals containing a point.
(node *treeNode, timestamp int64, fn func(ast.Interval) error)
| 120 | |
| 121 | // queryPoint recursively queries for intervals containing a point. |
| 122 | func (t *IntervalTree) queryPoint(node *treeNode, timestamp int64, fn func(ast.Interval) error) error { |
| 123 | if node == nil { |
| 124 | return nil |
| 125 | } |
| 126 | |
| 127 | // If max end in this subtree is less than timestamp, no intervals here contain it |
| 128 | if node.maxEnd < timestamp { |
| 129 | return nil |
| 130 | } |
| 131 | |
| 132 | // Check left subtree |
| 133 | if err := t.queryPoint(node.left, timestamp, fn); err != nil { |
| 134 | return err |
| 135 | } |
| 136 | |
| 137 | // Check current node |
| 138 | if containsTimestamp(node.interval, timestamp) { |
| 139 | if err := fn(node.interval); err != nil { |
| 140 | return err |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Check right subtree only if its intervals could contain the timestamp |
| 145 | // (start time <= timestamp is guaranteed by the query, we check end time via maxEnd) |
| 146 | nodeStart := GetStartTime(node.interval) |
| 147 | if timestamp >= nodeStart { |
| 148 | if err := t.queryPoint(node.right, timestamp, fn); err != nil { |
| 149 | return err |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return nil |
| 154 | } |
| 155 | |
| 156 | // QueryRange returns all intervals overlapping with [start, end]. |
| 157 | func (t *IntervalTree) QueryRange(start, end int64, fn func(ast.Interval) error) error { |
no test coverage detected