github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/collection/skipset/oparry.go (about)

     1  // Copyright 2021 ByteDance Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package skipset
    16  
    17  import (
    18  	"sync/atomic"
    19  	"unsafe"
    20  )
    21  
    22  const (
    23  	op1 = 4
    24  	op2 = maxLevel - op1
    25  )
    26  
    27  type optionalArray struct {
    28  	base  [op1]unsafe.Pointer
    29  	extra *([op2]unsafe.Pointer)
    30  }
    31  
    32  func (a *optionalArray) load(i int) unsafe.Pointer {
    33  	if i < op1 {
    34  		return a.base[i]
    35  	}
    36  	return a.extra[i-op1]
    37  }
    38  
    39  func (a *optionalArray) store(i int, p unsafe.Pointer) {
    40  	if i < op1 {
    41  		a.base[i] = p
    42  		return
    43  	}
    44  	a.extra[i-op1] = p
    45  }
    46  
    47  func (a *optionalArray) atomicLoad(i int) unsafe.Pointer {
    48  	if i < op1 {
    49  		return atomic.LoadPointer(&a.base[i])
    50  	}
    51  	return atomic.LoadPointer(&a.extra[i-op1])
    52  }
    53  
    54  func (a *optionalArray) atomicStore(i int, p unsafe.Pointer) {
    55  	if i < op1 {
    56  		atomic.StorePointer(&a.base[i], p)
    57  		return
    58  	}
    59  	atomic.StorePointer(&a.extra[i-op1], p)
    60  }