forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
horspool.go
95 lines (91 loc) · 3.18 KB
/
horspool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package horspool
// User defined.
// Set to true to read input from two command line arguments
// Set to false to read input from two files "pattern.txt" and "text.txt"
// const commandLineInput bool = false
// Implementation of Boyer-Moore-Horspool algorithm (Suffix based approach).
// Requires either a two command line arguments separated by a single space,
// or two files in the same folder: "pattern.txt" containing the string to
// be searched for, "text.txt" containing the text to be searched in.
// func main() {
// if commandLineInput == true { // case of command line input
// args := os.Args
// if len(args) <= 2 {
// log.Fatal("Not enough arguments. Two string arguments separated by spaces are required!")
// }
// pattern := args[1]
// s := args[2]
// for i := 3; i < len(args); i++ {
// s = s + " " + args[i]
// }
// if len(args[1]) > len(s) {
// log.Fatal("Pattern is longer than text!")
// }
// fmt.Printf("\nRunning: Horspool algorithm.\n\n")
// fmt.Printf("Search word (%d chars long): %q.\n", len(args[1]), pattern)
// fmt.Printf("Text (%d chars long): %q.\n\n", len(s), s)
// horspool(s, pattern)
// } else if commandLineInput == false { // case of file line input
// patFile, err := ioutil.ReadFile("pattern.txt")
// if err != nil {
// log.Fatal(err)
// }
// textFile, err := ioutil.ReadFile("text.txt")
// if err != nil {
// log.Fatal(err)
// }
// if len(patFile) > len(textFile) {
// log.Fatal("Pattern is longer than text!")
// }
// fmt.Printf("\nRunning: Horspool algorithm.\n\n")
// fmt.Printf("Search word (%d chars long): %q.\n", len(patFile), patFile)
// fmt.Printf("Text (%d chars long): %q.\n\n", len(textFile), textFile)
// horspool(string(textFile), string(patFile))
// }
// }
// // Function horspool performing the Horspool algorithm.
// // Prints whether the word/pattern was found and on what position in the text or not.
// func horspool(t, p string) {
// m, n, c, pos := len(p), len(t), 0, 0
// //Perprocessing
// d := preprocess(t, p)
// //Map output
// fmt.Printf("Precomputed shifts per symbol: ")
// for key, value := range d {
// fmt.Printf("%c:%d; ", key, value)
// }
// fmt.Println()
// //Searching
// for pos <= n-m {
// j := m
// if t[pos+j-1] != p[j-1] {
// fmt.Printf("\n comparing characters %c %c at positions %d %d", t[pos+j-1], p[j-1], pos+j-1, j-1)
// c++
// }
// for j > 0 && t[pos+j-1] == p[j-1] {
// fmt.Printf("\n comparing characters %c %c at positions %d %d", t[pos+j-1], p[j-1], pos+j-1, j-1)
// c++
// fmt.Printf(" - match")
// j--
// }
// if j == 0 {
// fmt.Printf("\n\nWord %q was found at position %d in %q. \n%d comparisons were done.", p, pos, t, c)
// return
// }
// pos = pos + d[t[pos+m]]
// }
// fmt.Printf("\n\nWord was not found.\n%d comparisons were done.", c)
// return
// }
// // Function that pre-computes map with Key: uint8 (char) Value: int.
// // Values determine safe shifting of search window.
// func preprocess(t, p string) (d map[uint8]int) {
// d = make(map[uint8]int)
// for i := 0; i < len(t); i++ {
// d[t[i]] = len(p)
// }
// for i := 0; i < len(p); i++ {
// d[p[i]] = len(p) - i
// }
// return d
// }