-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbinary_search.go
209 lines (179 loc) · 4.25 KB
/
binary_search.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"os"
"log"
"io"
"fmt"
"bytes"
"strings"
"strconv"
"unicode"
"unicode/utf8"
)
type CompareMode int
const (
IgnoreWhitespace CompareMode = 1 << iota
CaseInsensitive CompareMode = 1 << iota
Numeric CompareMode = 1 << iota
)
type BinarySearch struct {
fileName string
file *os.File
reverse bool
compareMode CompareMode
}
func NewBinarySearch(fileName string, reverse bool, compareMode CompareMode) BinarySearch {
return BinarySearch{fileName: fileName, reverse: reverse, compareMode: compareMode}
}
func (search *BinarySearch) checkMatch(match string, matchPos int, data []byte) bool {
if search.compareMode & CaseInsensitive == CaseInsensitive {
return matchPos < len(match) && bytes.ToLower(data)[0] == match[matchPos]
} else {
return matchPos < len(match) && data[0] == match[matchPos]
}
}
func (search *BinarySearch) CompareBytes(match string) int {
if search.compareMode & CaseInsensitive == CaseInsensitive {
match = strings.ToLower(match)
}
if search.compareMode & IgnoreWhitespace == IgnoreWhitespace {
match = strings.TrimLeft(match, " \t")
}
data := make([]byte, 1)
var matchPos = 0
var byteErr error = nil
var err error = nil
if search.compareMode & IgnoreWhitespace == IgnoreWhitespace {
for _, err = search.file.Read(data); data[0] != '\n' && (data[0] == ' ' || data[0] == '\t'); _, err = search.file.Read(data) {
byteErr = err
}
} else {
_, err = search.file.Read(data)
}
for err = nil; search.checkMatch(match, matchPos, data); _, err = search.file.Read(data) {
byteErr = err
matchPos++
}
if matchPos > len(match)-1 {
return 0
}
if data[0] == '\n' {
return 1
}
if byteErr == io.EOF {
return -1
}
if search.compareMode & CaseInsensitive == CaseInsensitive {
return int(match[matchPos]) - int(bytes.ToLower(data)[0])
}
return int(match[matchPos]) - int(data[0])
}
func (search *BinarySearch) CompareNumeric(match string) int {
matchVal, err := strconv.ParseFloat(match, 64)
var buffer bytes.Buffer
data := make([]byte, 1)
for _, err = search.file.Read(data); err != io.EOF && data[0] != '\n'; _, err = search.file.Read(data) {
if buffer.Len() == 0 && (data[0] == ' ' || data[0] == '\t') {
continue
}
r, _ := utf8.DecodeRune(data)
if data[0] != '-' && data[0] != '.' && !unicode.IsDigit(r) {
break
}
buffer.WriteByte(data[0])
}
if buffer.Len() == 0 {
return -1
}
bufferString := buffer.String()
var bufferVal, _ = strconv.ParseFloat(bufferString, 64)
if matchVal < bufferVal {
return -1
}
if matchVal > bufferVal {
return 1
}
return 0
}
func (search *BinarySearch) Compare(match string) int {
var cmp int
if search.compareMode & Numeric == Numeric {
cmp = search.CompareNumeric(match)
} else {
cmp = search.CompareBytes(match)
}
if search.reverse {
cmp = -cmp
}
return cmp
}
func (search *BinarySearch) PrintMatch(start int64, match string) {
if start >= 0 {
search.file.Seek(start, os.SEEK_SET)
}
data := make([]byte, 1)
var full_data []byte
for count := 0; start >= 0 && search.Compare(match) == 0; count++ {
search.file.Seek(start, os.SEEK_SET)
var isEof = false
for _, err := search.file.Read(data); !isEof; _, err = search.file.Read(data) {
isEof = err == io.EOF
if isEof {
break
}
start++
full_data = append(full_data, data[0])
if data[0] == '\n' {
break
}
}
if isEof {
break
}
}
fmt.Printf("%s", string(full_data))
}
func (search *BinarySearch) FindStart(match string) int64 {
var result int64 = -1
var err error
search.file, err = os.Open(search.fileName)
check(err)
stat, err := search.file.Stat()
check(err)
var start int64 = 0
var end = stat.Size() - 1
data := make([]byte, 1)
var previous int64 = -1
var cmp int
for start <= end {
var middle = (start + end) / 2
search.file.Seek(middle, os.SEEK_SET)
var scan = middle
if scan != 0 {
for _, err := search.file.Read(data); true; _, err = search.file.Read(data) {
scan++
if err == io.EOF || data[0] == '\n' {
break
}
}
}
if previous != scan {
cmp = search.Compare(match)
previous = scan
}
if cmp < 0 {
end = middle - 1
} else if cmp > 0 {
start = scan + 1
} else {
result = scan
end = middle - 1
}
}
return result
}
func check(err error) {
if err != nil {
log.Panic(err)
}
}