Seek finds the level-0 node representing the given byte position (i.e., the one where Offset <= pos < Offset+Size).
(n *TreeNode, pos uint64)
| 362 | // Seek finds the level-0 node representing the given byte position |
| 363 | // (i.e., the one where Offset <= pos < Offset+Size). |
| 364 | func Seek(n *TreeNode, pos uint64) (*TreeNode, error) { |
| 365 | if pos < n.Offset || pos >= (n.Offset+n.Size) { |
| 366 | return nil, ErrNotFound |
| 367 | } |
| 368 | |
| 369 | num := len(n.Children) |
| 370 | if num == 0 { |
| 371 | return n, nil |
| 372 | } |
| 373 | |
| 374 | // TODO: if a Node kept track of its children's offsets, |
| 375 | // this loop could be replaced with a sort.Search call. |
| 376 | for _, child := range n.Children { |
| 377 | if pos >= (child.Offset + child.Size) { |
| 378 | continue |
| 379 | } |
| 380 | return Seek(child, pos) |
| 381 | } |
| 382 | |
| 383 | // With a properly formed tree of nodes this will not be reached. |
| 384 | return nil, ErrNotFound |
| 385 | } |