1
2
3
4
5
6
7
8
9
10
11
12
13
14 package eu.fbk.rdfpro;
15
16 import java.io.Serializable;
17 import java.util.Objects;
18
19
20
21
22 public abstract class SetOperator implements Serializable, Comparable<SetOperator> {
23
24 private static final long serialVersionUID = 1L;
25
26
27
28
29
30
31 public static final SetOperator UNION = new SetOperator("u") {
32
33 private static final long serialVersionUID = 1L;
34
35 @Override
36 public int apply(final int[] counts) {
37 for (int i = 0; i < counts.length; ++i) {
38 if (counts[i] > 0) {
39 return 1;
40 }
41 }
42 return 0;
43 }
44
45 };
46
47
48
49
50
51
52
53 public static final SetOperator UNION_MULTISET = new SetOperator("U") {
54
55 private static final long serialVersionUID = 1L;
56
57 @Override
58 public int apply(final int[] counts) {
59 int result = counts[0];
60 for (int i = 1; i < counts.length; ++i) {
61 if (counts[i] > result) {
62 result = counts[i];
63 }
64 }
65 return result;
66 }
67
68 };
69
70
71
72
73
74
75
76
77 public static final SetOperator SUM_MULTISET = new SetOperator("a") {
78
79 private static final long serialVersionUID = 1L;
80
81 @Override
82 public int apply(final int[] counts) {
83 int result = 0;
84 for (int i = 0; i < counts.length; ++i) {
85 result += counts[i];
86 }
87 return result;
88 }
89
90 };
91
92
93
94
95
96
97 public static final SetOperator INTERSECTION = new SetOperator("i") {
98
99 private static final long serialVersionUID = 1L;
100
101 @Override
102 public int apply(final int[] counts) {
103 for (int i = 0; i < counts.length; ++i) {
104 if (counts[i] == 0) {
105 return 0;
106 }
107 }
108 return 1;
109 }
110
111 };
112
113
114
115
116
117
118
119 public static final SetOperator INTERSECTION_MULTISET = new SetOperator("I") {
120
121 private static final long serialVersionUID = 1L;
122
123 @Override
124 public int apply(final int[] counts) {
125 int result = counts[0];
126 for (int i = 1; i < counts.length; ++i) {
127 if (counts[i] < result) {
128 result = counts[i];
129 }
130 }
131 return result;
132 }
133
134 };
135
136
137
138
139
140
141
142 public static final SetOperator DIFFERENCE = new SetOperator("d") {
143
144 private static final long serialVersionUID = 1L;
145
146 @Override
147 public int apply(final int[] counts) {
148 if (counts[0] == 0) {
149 return 0;
150 }
151 for (int i = 1; i < counts.length; ++i) {
152 if (counts[i] > 0) {
153 return 0;
154 }
155 }
156 return 1;
157 }
158
159 };
160
161
162
163
164
165
166
167
168
169
170 public static final SetOperator DIFFERENCE_MULTISET = new SetOperator("D") {
171
172 private static final long serialVersionUID = 1L;
173
174 @Override
175 public int apply(final int[] counts) {
176 int result = counts[0];
177 for (int i = 1; i < counts.length && result > 0; ++i) {
178 result -= counts[i];
179 }
180 return result > 0 ? result : 0;
181 }
182
183 };
184
185
186
187
188
189
190
191 public static final SetOperator SYMMETRIC_DIFFERENCE = new SetOperator("s") {
192
193 private static final long serialVersionUID = 1L;
194
195 @Override
196 public int apply(final int[] counts) {
197 int result = 0;
198 for (int i = 0; i < counts.length; ++i) {
199 if (counts[i] > 0) {
200 ++result;
201 }
202 }
203 return result < counts.length ? 1 : 0;
204 }
205
206 };
207
208
209
210
211
212
213
214
215
216
217 public static final SetOperator SYMMETRIC_DIFFERENCE_MULTISET = new SetOperator("S") {
218
219 private static final long serialVersionUID = 1L;
220
221 @Override
222 public int apply(final int[] counts) {
223 int min = counts[0];
224 int max = min;
225 for (int i = 1; i < counts.length; ++i) {
226 final int n = counts[i];
227 if (n < min) {
228 min = n;
229 }
230 if (n > max) {
231 max = n;
232 }
233 }
234 return max - min;
235 }
236
237 };
238
239
240
241
242
243
244
245
246
247
248
249 public static SetOperator atLeast(final int n) {
250 if (n < 1) {
251 throw new IllegalArgumentException("Invalid threshold " + n);
252 }
253 return new SetOperator(n + "+") {
254
255 private static final long serialVersionUID = 1L;
256
257 @Override
258 public int apply(final int[] counts) {
259 int result = 0;
260 for (int i = 0; i < counts.length; ++i) {
261 if (counts[i] > 0) {
262 ++result;
263 }
264 }
265 return result >= n ? 1 : 0;
266 }
267
268 };
269 }
270
271
272
273
274
275
276
277
278
279
280
281 public static SetOperator atMost(final int n) {
282 if (n < 1) {
283 throw new IllegalArgumentException("Invalid threshold " + n);
284 }
285 return new SetOperator(n + "-") {
286
287 private static final long serialVersionUID = 1L;
288
289 @Override
290 public int apply(final int[] counts) {
291 int result = 0;
292 for (int i = 0; i < counts.length; ++i) {
293 result += counts[i];
294 }
295 return result <= n ? 1 : 0;
296 }
297
298 };
299 }
300
301 private final String string;
302
303 private SetOperator(final String string) {
304 this.string = string;
305 }
306
307 private Object writeReplace() {
308 return new SerializedForm(this.string);
309 }
310
311
312
313
314
315
316
317
318
319
320
321 public abstract int apply(int[] multiplicities);
322
323
324
325
326 @Override
327 public final int compareTo(final SetOperator other) {
328 return this.string.compareTo(other.string);
329 }
330
331
332
333
334 @Override
335 public final boolean equals(final Object object) {
336 if (object == this) {
337 return true;
338 }
339 if (!(object instanceof SetOperator)) {
340 return false;
341 }
342 final SetOperator other = (SetOperator) object;
343 return this.string.equals(other.string);
344 }
345
346
347
348
349
350 @Override
351 public final int hashCode() {
352 return this.string.hashCode();
353 }
354
355
356
357
358
359 @Override
360 public final String toString() {
361 return this.string;
362 }
363
364
365
366
367
368
369
370
371 public static SetOperator valueOf(final String string) {
372 Objects.requireNonNull(string);
373 for (final SetOperator operation : new SetOperator[] { UNION, UNION_MULTISET,
374 SUM_MULTISET, INTERSECTION, INTERSECTION_MULTISET, DIFFERENCE,
375 DIFFERENCE_MULTISET, SYMMETRIC_DIFFERENCE, SYMMETRIC_DIFFERENCE_MULTISET }) {
376 if (operation.string.equals(string)) {
377 return operation;
378 }
379 }
380 if (string.endsWith("+")) {
381 final int n = Integer.parseInt(string.substring(0, string.length() - 1));
382 return atLeast(n);
383 }
384 if (string.endsWith("-")) {
385 final int n = Integer.parseInt(string.substring(0, string.length() - 1));
386 return atMost(n);
387 }
388 throw new IllegalArgumentException("Unknown set operator '" + string + "'");
389 }
390
391 static final class SerializedForm implements Serializable {
392
393 private static final long serialVersionUID = 1L;
394
395 private final String string;
396
397 public SerializedForm(final String string) {
398 this.string = string;
399 }
400
401 Object readResolve() {
402 return SetOperator.valueOf(this.string);
403 }
404
405 }
406
407 }