github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/sets/zset/oparry.go (about)

     1  package zset
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  const (
     8  	op1 = 4
     9  	op2 = maxLevel - op1 // TODO: not sure that whether 4 is the best number for op1([28]Pointer for op2).
    10  )
    11  
    12  type listLevel struct {
    13  	next unsafe.Pointer // the forward pointer
    14  	span int            // span is count of level 0 element to next element in current level
    15  }
    16  
    17  type optionalArray struct {
    18  	base  [op1]listLevel
    19  	extra *([op2]listLevel)
    20  }
    21  
    22  func (a *optionalArray) init(level int) {
    23  	if level > op1 {
    24  		a.extra = new([op2]listLevel)
    25  	}
    26  }
    27  
    28  func (a *optionalArray) loadNext(i int) unsafe.Pointer {
    29  	if i < op1 {
    30  		return a.base[i].next
    31  	}
    32  	return a.extra[i-op1].next
    33  }
    34  
    35  func (a *optionalArray) storeNext(i int, p unsafe.Pointer) {
    36  	if i < op1 {
    37  		a.base[i].next = p
    38  		return
    39  	}
    40  	a.extra[i-op1].next = p
    41  }
    42  
    43  func (a *optionalArray) loadSpan(i int) int {
    44  	if i < op1 {
    45  		return a.base[i].span
    46  	}
    47  	return a.extra[i-op1].span
    48  }
    49  
    50  func (a *optionalArray) storeSpan(i int, s int) {
    51  	if i < op1 {
    52  		a.base[i].span = s
    53  		return
    54  	}
    55  	a.extra[i-op1].span = s
    56  }