github.com/philpearl/symboltab@v1.1.4/intbank.go (about)

     1  package symboltab
     2  
     3  const intbanksize = 1 << 9
     4  
     5  type intbank struct {
     6  	slabs [][]int
     7  }
     8  
     9  func (ib *intbank) save(sequence int32, offset int) {
    10  	sequence-- // externally sequence starts at 1
    11  	slabNo := int(sequence / intbanksize)
    12  	slabOffset := int(sequence % intbanksize)
    13  
    14  	for len(ib.slabs) <= slabNo {
    15  		ib.slabs = append(ib.slabs, make([]int, intbanksize))
    16  	}
    17  
    18  	ib.slabs[slabNo][slabOffset] = offset
    19  }
    20  
    21  func (ib *intbank) lookup(sequence int32) int {
    22  	sequence-- // externally, sequence starts at 1
    23  	slabNo := int(sequence / intbanksize)
    24  	slabOffset := int(sequence % intbanksize)
    25  
    26  	return ib.slabs[slabNo][slabOffset]
    27  }