github.com/songzhibin97/gkit@v1.2.13/structure/zset/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 zset
    16  
    17  import (
    18  	"unsafe"
    19  )
    20  
    21  const (
    22  	op1 = 4
    23  	op2 = maxLevel - op1 // TODO: not sure that whether 4 is the best number for op1([28]Pointer for op2).
    24  )
    25  
    26  type listLevel struct {
    27  	next unsafe.Pointer // the forward pointer
    28  	span int            // span is count of level 0 element to next element in current level
    29  }
    30  type optionalArray struct {
    31  	base  [op1]listLevel
    32  	extra *([op2]listLevel)
    33  }
    34  
    35  func (a *optionalArray) init(level int) {
    36  	if level > op1 {
    37  		a.extra = new([op2]listLevel)
    38  	}
    39  }
    40  
    41  func (a *optionalArray) loadNext(i int) unsafe.Pointer {
    42  	if i < op1 {
    43  		return a.base[i].next
    44  	}
    45  	return a.extra[i-op1].next
    46  }
    47  
    48  func (a *optionalArray) storeNext(i int, p unsafe.Pointer) {
    49  	if i < op1 {
    50  		a.base[i].next = p
    51  		return
    52  	}
    53  	a.extra[i-op1].next = p
    54  }
    55  
    56  func (a *optionalArray) loadSpan(i int) int {
    57  	if i < op1 {
    58  		return a.base[i].span
    59  	}
    60  	return a.extra[i-op1].span
    61  }
    62  
    63  func (a *optionalArray) storeSpan(i int, s int) {
    64  	if i < op1 {
    65  		a.base[i].span = s
    66  		return
    67  	}
    68  	a.extra[i-op1].span = s
    69  }