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