go-hep.org/x/hep@v0.38.1/groot/rtree/rbranch.go (about)

     1  // Copyright ©2020 The go-hep Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package rtree
     6  
     7  type rbranch struct {
     8  	b      Branch
     9  	rb     *bkreader
    10  	cur    *rbasket
    11  	leaves []rleaf
    12  }
    13  
    14  func newRBranch(b Branch, n int, beg, end int64, leaves []rleaf, rctx rleafCtx) rbranch {
    15  	rb := rbranch{
    16  		b:      b,
    17  		rb:     newBkReader(b, n, beg, end),
    18  		leaves: leaves,
    19  	}
    20  	return rb
    21  }
    22  
    23  func (rb *rbranch) start() error {
    24  	var err error
    25  	rb.cur, err = rb.rb.read()
    26  	return err
    27  }
    28  
    29  func (rb *rbranch) stop() error {
    30  	if rb.cur == nil {
    31  		return nil
    32  	}
    33  	rb.rb.close()
    34  	return nil
    35  }
    36  
    37  func (rb *rbranch) reset() {
    38  	rb.rb.close()
    39  	rb.rb = newBkReader(rb.b, rb.rb.n, rb.rb.beg, rb.rb.end)
    40  }
    41  
    42  func (rb *rbranch) read(i int64) error {
    43  	var err error
    44  	if i >= rb.cur.span.end {
    45  		rb.cur, err = rb.rb.read()
    46  		if err != nil {
    47  			return err
    48  		}
    49  	}
    50  
    51  	j := i - rb.cur.span.beg
    52  	switch len(rb.leaves) {
    53  	case 1:
    54  		err = rb.cur.loadRLeaf(j, rb.leaves[0])
    55  		if err != nil {
    56  			return err
    57  		}
    58  
    59  	default:
    60  		for _, leaf := range rb.leaves {
    61  			err = rb.cur.loadRLeaf(j, leaf)
    62  			if err != nil {
    63  				return err
    64  			}
    65  		}
    66  	}
    67  
    68  	return nil
    69  }
    70  
    71  func asBranch(b Branch) *tbranch {
    72  	switch b := b.(type) {
    73  	case *tbranch:
    74  		return b
    75  	case *tbranchObject:
    76  		return &b.tbranch
    77  	case *tbranchElement:
    78  		return &b.tbranch
    79  	}
    80  	panic("impossible")
    81  }