-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary2.php
More file actions
89 lines (65 loc) · 2.32 KB
/
binary2.php
File metadata and controls
89 lines (65 loc) · 2.32 KB
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
function binary($content, $needle) {
$less = 0;
$more = count($content) - 1;
while($less <= $more) {
$middle = $less + floor(($more - $less) / 2);
$compare = strnatcmp($content[$middle][0], $needle);
if (strnatcmp($content[$less][0], $needle) === 0) {
return $content[$less][1];
}
if (strnatcmp($content[$more][0], $needle) === 0) {
return $content[$more][1];
}
if ($compare > 0) {
$more = $middle - 1;
} elseif ($compare < 0) {
$less = $middle + 1;
} elseif ($compare === 0) {
return $content[$middle][1];
}
}
return 'undef';
}
function search2($needle, $dir) {
$splitter = '\x0A';
$splitter2 = '\t';
$step = 1000000;
$remain = '';
$handle = fopen($dir, 'r');
while (!feof($handle)) {
$content = fread($handle, $step);
$firstIndex = strpos($content, $splitter);
$firstPart = substr($content, 0, $firstIndex + 4);
$content = substr($content, $firstIndex + 4);
$contentLen = strlen($content);
$lastIndex = strrpos($content, $splitter);
if ( ($lastIndex + 4 - $contentLen) !== 0 ) {
$lastPart = substr($content, $lastIndex + 4 - $contentLen);
}
if ( ($lastIndex + 4 - $contentLen) !== 0 ) {
$content = substr($content, 0, $lastIndex + 4 - $contentLen);
}
$remain .= $firstPart . $lastPart;
$content = substr($content, 0, -4);
$contentLen = strlen($content);
$lastIndex = strrpos($content, $splitter);
$lastPart = substr($content, $lastIndex + 4 - $contentLen);
$lastPart = explode($splitter2, $lastPart);
if (strnatcmp($lastPart[0], $needle) > 0) {
$content = explode($splitter, $content);
for($i = 0; $i < count($content); $i++) {
$content[$i] = explode($splitter2, $content[$i]);
}
$answer = binary($content, $needle);
if ($answer !== 'undef') {
return $answer;
}
}
}
$remain = explode($splitter, $remain);
array_pop($remain);
for($i = 0; $i < count($remain); $i++) {
$remain[$i] = explode($splitter2, $remain[$i]);
}
return binary($remain, $needle);
}