package zeta.tool;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import zeta.util.StreamUtils;
public class ZetaSearch {
public static void main(String[] args) {
if (args.length == 1 && args[0].equals("s")) {
split();
} else if (args.length == 2) {
if (args[0].equals("t")) {
searchText(args[1]);
} else {
search(args[0], Long.parseLong(args[1]));
}
} else {
System.err.println("USAGE: <Rosser block type> <begin>\n"
+ " s\n"
+ " t <text>");
}
}
static void split() {
File file = new File("summary/tmp");
File[] list = file.listFiles();
if (list != null) {
long lastMax = 9999999999999L;
long lastMin = 0;
for (int count = 0;; ++count) {
File dir = new File("summary/tmp/" + count);
if (dir.exists()) {
continue;
}
dir.mkdir();
for (int i = 0; i < 1000; ++i) {
int idx = -1;
for (int j = 0; j < list.length; ++j) {
String name = list[j].getName();
if (name.startsWith("zeta_zeros_0_") && name.endsWith(".tmp")) {
long range = Long.parseLong(name.substring(13, name.length()-4));
if (range > lastMin && range < lastMax) {
lastMax = range;
idx = j;
}
}
}
if (idx == -1) {
return;
} else {
list[idx].renameTo(new File("summary/tmp/" + count + '/' + list[idx].getName()));
lastMin = lastMax;
lastMax = 9999999999999L;
}
}
}
}
}
static void search(String type, long begin) {
type = ' ' + type + ',';
File file = new File("summary/tmp");
File[] list = file.listFiles();
if (list != null) {
System.out.println("start");
int size = 0;
for (int i = 0; i < list.length; ++i) {
String s = list[i].getName();
if (s.startsWith("zeta_zeros_") && s.endsWith(".tmp") && s.startsWith("zeta_zeros_0_")) {
++size;
}
}
long[] start = new long[size];
int pos = 0;
for (int i = 0; i < list.length; ++i) {
String s = list[i].getName();
if (s.startsWith("zeta_zeros_") && s.endsWith(".tmp") && s.startsWith("zeta_zeros_0_")) {
start[pos++] = Long.parseLong(s.substring(13, s.length()-4));
}
}
System.out.println("size=" + size);
Arrays.sort(start);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("summary/tmp/zeta_zeros_0_" + start[size-1] + ".tmp"));
long maxAnzahl = getAnzahl(reader, type);
reader.close();
int a = 0;
while (a < size-1 && start[a] < begin) {
++a;
}
for (long anzahl = 0; anzahl < maxAnzahl; ++anzahl) {
int b = size-1;
while (a < b) {
int c = (a+b)/2;
reader = new BufferedReader(new FileReader("summary/tmp/zeta_zeros_0_" + start[c] + ".tmp"));
long anz = getAnzahl(reader, type);
if (b-a == 1) {
System.out.println(String.valueOf(anzahl) + ". found at " + "summary/tmp/zeta_zeros_0_" + start[b] + ".tmp");
break;
}
if (anz <= anzahl) {
a = c;
} else {
b = c;
}
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
StreamUtils.close(reader);
}
}
}
private static long getAnzahl(BufferedReader reader, String type) throws IOException {
long anz = 0;
String s = reader.readLine();
if (s != null) {
int idx = s.indexOf(type, s.indexOf(' ')+1);
if (idx >= 0) {
idx = s.indexOf(type, idx+1) + type.length();
int j = idx-1;
int k = s.length();
while (++j < k && Character.isDigit(s.charAt(j)));
anz = Long.parseLong(s.substring(idx, j));
}
}
return anz;
}
static void searchText(String text) {
byte[] textBytes = text.getBytes();
int[] next = initIndexOf(textBytes);
File file = new File(ConstantProperties.FINAL_DIR + "/1");
File[] list = file.listFiles();
if (list != null) {
byte[] buffer = new byte[50000];
for (int i = 0; i < list.length; ++i) {
final String name = list[i].getName();
if (name.startsWith("zeta_zeros_") && name.endsWith(".zip")) {
ZipInputStream zip = null;
try {
zip = new ZipInputStream(new FileInputStream(list[i]));
for (int k = 0; k < 2; ++k) {
ZipEntry zEntry = zip.getNextEntry();
if (zEntry.getName().endsWith(".log")) {
while (true) {
int n = zip.read(buffer);
if (n <= 0) break;
if (indexOf(buffer, 0, n, textBytes, next) >= 0) {
System.out.println("found at " + ConstantProperties.FINAL_DIR + "/1/" + name);
break;
}
}
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
StreamUtils.close(zip);
}
}
}
}
}
private static int[] initIndexOf(byte[] string) {
final int l = string.length-1;
int[] next = new int[string.length];
int j = -1;
next[0] = -1;
for (int i = 0; i < l; ) {
if (j == -1 || string[i] == string[j]) next[++i] = ++j;
else j = next[j];
}
return next;
}
private static int indexOf(byte[] string1, int pos, int n, byte[] string2, int[] next) {
if (string2.length == 0) return 0;
final int l = n-string2.length;
int j = 0;
for (int i = pos; i < l;) {
if (j == -1 || string1[i] == string2[j]) {
++i;
if (++j == string2.length) return i-string2.length;
} else j = next[j];
}
return -1;
}
}