-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathDat.java
More file actions
140 lines (122 loc) · 3.17 KB
/
Copy pathDat.java
File metadata and controls
140 lines (122 loc) · 3.17 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
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
package base;
import java.io.*;
import java.util.Vector;
public class Dat {
public Vector<Entry> dat;
public int datSize;
public Dat(){
dat = new Vector<Entry>();
datSize = 0;
}
public Dat(int datSize, Vector<Entry> olddat){
this.datSize = datSize;
dat= new Vector<Entry>();
for(int i = 0; i < datSize; i ++){
dat.add(new Entry());
dat.get(i).base = olddat.get(i).base;
dat.get(i).check = olddat.get(i).check;
}
}
public Dat(String filename) throws IOException {
filename = Dat.class.getClassLoader().getResource(filename).getFile();
File file = new File(filename);
datSize = (int)(file.length() / 8);
//System.out.println(datSize);
FileInputStream in = new FileInputStream(file);
byte[] tempbytes = new byte[8 * datSize];
dat = new Vector<Entry>();
in.read(tempbytes);
for(int i = 0; i < datSize; i ++){
Entry entry = new Entry();
entry.base = bytesToInt(tempbytes, 8 * i);
dat.add(entry);
dat.get(i).check = bytesToInt(tempbytes, 8 * i + 4);
}
in.close();
}
public static int bytesToInt(byte[] bb, int index) {
return (int) (((((int)bb[index + 3] & 0xff) << 24)
| (((int)bb[index + 2] & 0xff) << 16)
| (((int)bb[index + 1] & 0xff) << 8) | (((int)bb[index + 0] & 0xff) << 0)));
}
public static byte[] intToBytes(int n){
byte[] b = new byte[4];
for(int i = 0;i < 4;i++){
b[i] = (byte)(n >> (8 * i));
}
return b;
}
public void save(String filename) throws IOException{
FileOutputStream out = new FileOutputStream(filename);
for(Entry e : dat){
out.write(intToBytes(e.base));
}
out.flush();
for(Entry e : dat){
out.write(intToBytes(e.check));
}
out.flush();
out.close();
}
public boolean search(String sentence, Vector<Integer> bs, Vector<Integer> es){
bs.clear();
es.clear();
boolean empty = true;
for(int offset = 0; offset < sentence.length(); offset ++){
int preBase = 0;
int preInd = 0;
int ind = 0;
for(int i = offset; i < sentence.length(); i ++){
ind = preBase + sentence.charAt(i);
if(ind < 0 || ind >= datSize || dat.get(ind).check != preInd)break;
preInd = ind;
preBase = dat.get(ind).base;
ind = preBase;
if(!(ind < 0 || ind >= datSize || dat.get(ind).check != preInd)){
bs.add(offset);
es.add(i + 1);
if(empty){
empty = false;
}
}
}
}
return !empty;
}
public int match(String word){
int ind = 0;
int base = 0;
for(int i = 0; i < word.length(); i ++){
ind = dat.get(ind).base + word.charAt(i);
if((ind >= datSize) || (dat.get(ind).check != base)) return -1;
base = ind;
}
ind = dat.get(base).base;
if((ind < datSize) && (dat.get(ind).check == base)){
return ind;
}
return -1;
}
public void update(String word, int value){
int base = match(word);
if(base >= 0){
dat.get(base).base = value;
}
}
public int getInfo(String prefix){
int ind = 0;
int base = 0;
for(int i = 0; i < prefix.length(); i ++){
ind = dat.get(ind).base + prefix.charAt(i);
if((ind >= datSize) || dat.get(ind).check != base) return i;
base = ind;
}
return -base;
}
public int getDatSize(){
return datSize;
}
public Vector<Entry> getDat(){
return dat;
}
}