package bytealg
import (
"internal/cpu"
"unsafe"
)
const (
offsetPPC64HasPOWER9 = unsafe .Offsetof (cpu .PPC64 .IsPOWER9 )
offsetRISCV64HasV = unsafe .Offsetof (cpu .RISCV64 .HasV )
offsetLOONG64HasLSX = unsafe .Offsetof (cpu .Loong64 .HasLSX )
offsetLOONG64HasLASX = unsafe .Offsetof (cpu .Loong64 .HasLASX )
offsetS390xHasVX = unsafe .Offsetof (cpu .S390X .HasVX )
offsetX86HasSSE42 = unsafe .Offsetof (cpu .X86 .HasSSE42 )
offsetX86HasAVX2 = unsafe .Offsetof (cpu .X86 .HasAVX2 )
offsetX86HasPOPCNT = unsafe .Offsetof (cpu .X86 .HasPOPCNT )
)
var MaxLen int
const PrimeRK = 16777619
func HashStr [T string | []byte ](sep T ) (uint32 , uint32 ) {
hash := uint32 (0 )
for i := 0 ; i < len (sep ); i ++ {
hash = hash *PrimeRK + uint32 (sep [i ])
}
var pow , sq uint32 = 1 , PrimeRK
for i := len (sep ); i > 0 ; i >>= 1 {
if i &1 != 0 {
pow *= sq
}
sq *= sq
}
return hash , pow
}
func HashStrRev [T string | []byte ](sep T ) (uint32 , uint32 ) {
hash := uint32 (0 )
for i := len (sep ) - 1 ; i >= 0 ; i -- {
hash = hash *PrimeRK + uint32 (sep [i ])
}
var pow , sq uint32 = 1 , PrimeRK
for i := len (sep ); i > 0 ; i >>= 1 {
if i &1 != 0 {
pow *= sq
}
sq *= sq
}
return hash , pow
}
func IndexRabinKarp [T string | []byte ](s , sep T ) int {
hashss , pow := HashStr (sep )
n := len (sep )
var h uint32
for i := 0 ; i < n ; i ++ {
h = h *PrimeRK + uint32 (s [i ])
}
if h == hashss && string (s [:n ]) == string (sep ) {
return 0
}
for i := n ; i < len (s ); {
h *= PrimeRK
h += uint32 (s [i ])
h -= pow * uint32 (s [i -n ])
i ++
if h == hashss && string (s [i -n :i ]) == string (sep ) {
return i - n
}
}
return -1
}
func LastIndexRabinKarp [T string | []byte ](s , sep T ) int {
hashss , pow := HashStrRev (sep )
n := len (sep )
last := len (s ) - n
var h uint32
for i := len (s ) - 1 ; i >= last ; i -- {
h = h *PrimeRK + uint32 (s [i ])
}
if h == hashss && string (s [last :]) == string (sep ) {
return last
}
for i := last - 1 ; i >= 0 ; i -- {
h *= PrimeRK
h += uint32 (s [i ])
h -= pow * uint32 (s [i +n ])
if h == hashss && string (s [i :i +n ]) == string (sep ) {
return i
}
}
return -1
}
func MakeNoZero (n int ) []byte
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .