-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSequence.java
More file actions
309 lines (155 loc) · 4.72 KB
/
Sequence.java
File metadata and controls
309 lines (155 loc) · 4.72 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package gsp;
//package gsp;
import java.util.ArrayList;
public class Sequence {
private int support; //该序列在数据库中的支持计数
private ArrayList<Element> sequence; //存放元素序列
/**
* 不带参数的构造方法
* 实例化Sequence对象时,同时初始化该对象的支持计数和sequence属性
*
*/
public Sequence() {
this.sequence = new ArrayList<Element>();
this.support = 0;
}
/**
* 带参数的构造方法
* 拷贝参数序列对象s中的所有元素到本对象的sequence属性中
* 并初始化支持计数
* @param s Sequence对象
*/
public Sequence(Sequence s) {
this.sequence = new ArrayList<Element>();
this.support = 0;
//拷贝s中的所有元素
for (int i = 0; i < s.size(); i++) {
this.sequence.add(s.getElement(i).clone());
}
}
/**
* 添加新的元素
* 用于向序列中添加新的元素
* @param e Element e -- 被添加的元素
*/
public void addElement(Element e) {
this.sequence.add(e);
}
/**
* 插入元素
* 向序列中位置index插入新的元素
* @param index 需要插入的元素位置
* @param e Element e -- 被插入的元素
*/
public void insertElement(int index,Element e){
this.sequence.add(index,e);
}
/**
* 删除元素
* 删除位置index上的元素
* @param index 位置序号
* @return 返回删除后的sequence
*/
public Element removeElement(int index){
if(index<this.sequence.size()){
return this.sequence.remove(index);
}else
return null;
}
/**
* 获取序列的元素
* 获取第index个元素
* @param index 元素在序列中的位置
* @return 返回该元素
*/
public Element getElement(int index) {
if (index >= 0 && index < this.sequence.size()) {
return this.sequence.get(index);
} else {
System.err.println("index outof bound in Seuqence.getElement()");
return null;
}
}
/**
* 获取所有元素
* 返回序列对象的sequence属性,也就是所有元素的集合
* @return ArrayList -- 所有元素放到ArrayList中
*/
public ArrayList<Element> getElements() {
return this.sequence;
}
/**
* 获取序列大小
* @return 序列大小
*/
public int size() {
return this.sequence.size();
}
/**
* 比较序列间的元素
* 将传递的参数序列对象与本序列对象比较
* 看是否有相同的元素
* @param seqs 被比较的序列
* @return true--存在相同元素 false--不存在相同元素
*/
public boolean notInSeqs(ArrayList<Sequence> seqs) {
Sequence s;
for (int i=0;i<seqs.size();i++) {
s=seqs.get(i);
//调用方法isSubsequenceOf()比较
if (this.isSubsequenceOf(s) && s.isSubsequenceOf(this)){
return false;
}
}
return true;
}
/*
* 比较序列中是否含有相同的元素
*/
public boolean isSubsequenceOf(Sequence s) {
int i = 0, j = 0;
while (j < s.size() && i < this.sequence.size()) {
if (this.getElement(i).isContainIn(s.getElement(j))) {
i++;
j++;
if (i == this.sequence.size()) {
return true;
}
} else {
j++;
}
}
return false;
}
/**
* 增加支持计数
*
*/
public void incrementSupport() {
this.support++;
}
/**
* 获取支持计数
* @return
*/
public int getSupport() {
return this.support;
}
/**
* 重写toString()
* 用于输出时的字符处理
*
*/
public String toString() {
StringBuffer s = new StringBuffer();
s.append("<");
for (int i = 0; i < this.sequence.size(); i++) {
s.append(this.sequence.get(i));
if (i != this.sequence.size() - 1) {
s.append(" ");
}
}
s.append(">");
return s.toString();
}
}