github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/maps/skipmap/oparray.go (about)

     1  package skipmap
     2  
     3  import (
     4  	"sync/atomic"
     5  	"unsafe"
     6  )
     7  
     8  const (
     9  	op1 = 4
    10  	op2 = maxLevel - op1
    11  )
    12  
    13  type optionalArray struct {
    14  	base  [op1]unsafe.Pointer
    15  	extra *([op2]unsafe.Pointer)
    16  }
    17  
    18  func (a *optionalArray) load(i int) unsafe.Pointer {
    19  	if i < op1 {
    20  		return a.base[i]
    21  	}
    22  	return a.extra[i-op1]
    23  }
    24  
    25  func (a *optionalArray) store(i int, p unsafe.Pointer) {
    26  	if i < op1 {
    27  		a.base[i] = p
    28  		return
    29  	}
    30  	a.extra[i-op1] = p
    31  }
    32  
    33  func (a *optionalArray) atomicLoad(i int) unsafe.Pointer {
    34  	if i < op1 {
    35  		return atomic.LoadPointer(&a.base[i])
    36  	}
    37  	return atomic.LoadPointer(&a.extra[i-op1])
    38  }
    39  
    40  func (a *optionalArray) atomicStore(i int, p unsafe.Pointer) {
    41  	if i < op1 {
    42  		atomic.StorePointer(&a.base[i], p)
    43  		return
    44  	}
    45  	atomic.StorePointer(&a.extra[i-op1], p)
    46  }