github.com/songzhibin97/gkit@v1.2.13/structure/skipmap/util.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 skipmap
    16  
    17  import (
    18  	"github.com/songzhibin97/gkit/internal/wyhash"
    19  	"github.com/songzhibin97/gkit/sys/fastrand"
    20  	_ "unsafe" // for linkname
    21  )
    22  
    23  const (
    24  	maxLevel            = 16
    25  	p                   = 0.25
    26  	defaultHighestLevel = 3
    27  )
    28  
    29  func hash(s string) uint64 {
    30  	return wyhash.Sum64String(s)
    31  }
    32  
    33  //go:linkname cmpstring runtime.cmpstring
    34  func cmpstring(a, b string) int
    35  
    36  func randomLevel() int {
    37  	level := 1
    38  	for fastrand.Uint32n(1/p) == 0 {
    39  		level++
    40  	}
    41  	if level > maxLevel {
    42  		return maxLevel
    43  	}
    44  	return level
    45  }