Created
August 1, 2021 18:33
-
-
Save carterpeel/d9cb4732dfbcda93f43fc9cd1506d161 to your computer and use it in GitHub Desktop.
Boyer-More algorithm, ingests a []byte slice and returns the index of the first occurrence of `substr`. (this is particularly useful for large `substr` values)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bm | |
import "bytes" | |
func BoyerMooreIndex(s, substr []byte) int { | |
d := CalculateSlideTable(substr) | |
return IndexWithTable(&d, s, substr) | |
} | |
func IndexWithTable(d *[256]int, s, substr []byte) int { | |
lsub := len(substr) | |
ls := len(s) | |
switch { | |
case lsub == 0: | |
return 0 | |
case lsub > ls: | |
return -1 | |
case lsub == ls: | |
if bytes.Compare(s, substr) == 0 { | |
return 0 | |
} | |
return -1 | |
} | |
i := 0 | |
for i+lsub-1 < ls { | |
j := lsub - 1 | |
for ; j >= 0 && s[i+j] == substr[j]; j-- { | |
} | |
if j < 0 { | |
return i | |
} | |
slid := j - d[s[i+j]] | |
if slid < 1 { | |
slid = 1 | |
} | |
i += slid | |
} | |
return -1 | |
} | |
func CalculateSlideTable(substr []byte) [256]int { | |
var d [256]int | |
for i := 0; i < 256; i++ { | |
d[i] = -1 | |
} | |
for i := 0; i < len(substr); i++ { | |
d[substr[i]] = i | |
} | |
return d | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment