-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOperationUtils.java
700 lines (631 loc) · 29.4 KB
/
OperationUtils.java
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
package com.vincentderk.acircuitminer.miner.util;
import com.vincentderk.acircuitminer.miner.Graph;
import com.vincentderk.acircuitminer.miner.emulatable.neutralfinder.EmulatableBlock;
import com.vincentderk.acircuitminer.miner.emulatable.neutralfinder.UseBlock;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntAVLTreeSet;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntSet;
import it.unimi.dsi.fastutil.objects.AbstractObject2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMaps;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
//TODO: Verify which operationUtils methods work for SOSR/MOSR and update the docs.
/**
* Contains some useful operations such as replacing occurrences, writing to an
* .ac file, getting total execution energy costs, removing overlap,...
*
* @author Vincent Derkinderen
* @version 2.0
*/
public class OperationUtils {
/**
* Replace the given occurrences by a new operation presented with newLabel,
* in g. In each occurrence, the vertices should match the order of the
* canonicalCode they are occurrences of. So the first is the root, the
* second is vertex assigned 1 in the canonicalCode,...
* <p>
* <b> Note: This invalidates the graph invariant that inc and out are
* sorted.</b>
*
* @param g The graph to replace in.
* @param code The pattern we are replacing
* @param occurrences The occurrences of the {@code code} pattern that have
* to be replaced.
* @param newLabel The new label of the new operation
* @return The inc and out of the nodes that should be ignored from now on,
* are replaced with null.
*/
public static IntArrayList replace(Graph g, long[] code, ObjectArrayList<int[]> occurrences, short newLabel) {
IntArrayList ignore = new IntArrayList();
/* Fix edges */
for (int[] occurrence : occurrences) {
if (occurrence.length <= 1) {
continue;
}
//codeId |-> nodeId in graph
Int2IntMap inputMap = Utils.getInputMap(g, code, occurrence); //TODO: Change to more efficient one
int root = occurrence[0];
int[] sortedOccurrence = Arrays.copyOf(occurrence, occurrence.length);
Arrays.sort(sortedOccurrence); //for quick contains check
// Remove internal node data
for (int i = 1; i < occurrence.length; i++) {
int node = occurrence[i];
g.inc[node] = null;
g.out[node] = null;
g.label[node] = Graph.MARKER;
g.expandable_children[node] = null;
g.unexpandable_children[node] = null;
ignore.add(node);
}
// Make inputs point to root
for (int node : inputMap.values()) {
//for (int i : g.out[node]) {
for (int i = 0; i < g.out[node].length; i++) {
if (Arrays.binarySearch(sortedOccurrence, g.out[node][i]) >= 0) {
g.out[node][i] = root;
}
}
}
// Order the inputs according to codeId for the root.
int[] sortedInputKeys = inputMap.keySet().toIntArray(); //TODO: optimizable, always same.
Arrays.sort(sortedInputKeys);
int[] inputNodesOrdered = new int[sortedInputKeys.length];
for (int i = 0; i < sortedInputKeys.length; i++) {
inputNodesOrdered[i] = inputMap.get(sortedInputKeys[i]);
}
g.inc[root] = inputNodesOrdered;
g.label[root] = newLabel;
// Fix expandable and unexpandable
IntArrayList expandable = new IntArrayList();
IntArrayList unexpandable = new IntArrayList();
for (int node : inputNodesOrdered) {
if (g.label[node] == Graph.INPUT) {
unexpandable.add(node);
} else {
expandable.add(node);
}
}
g.expandable_children[root] = expandable.toIntArray();
g.unexpandable_children[root] = unexpandable.toIntArray();
}
return ignore;
}
/**
* Replace the given occurrences in g that form a pattern given by
* {@code emulatedCode}. Replacement is done by another pattern given by
* {@code code} that uses a certain input, given by {@code input}, to
* emulate the {@code emulatedCode}. In each occurrence, the vertices should
* match the order of the canonicalCode they are occurrences of. So the
* first is the root, the second is vertex assigned 1 in the
* canonicalCode,...
*
* <p>
* <b> Note: This invalidates the graph invariant that inc and out are
* sorted.</b>
*
* @param g The graph to replace in.
* @param emulatedIndexToActualInputIndex This provides the link between the
* inputs given to an emulated pattern and the order of those inputs in the
* actual input given to the pattern P. See
* {@link EmulatableBlock#emulatedIndexToActualInputIndex} for an example.
* @param emulatedCode The pattern we are emulating by giving the
* {@code input} to the {@code code}.
* @param input The input given to the {@code code} to emulate the
* {@code emulatedCode}
* @param occurrences The occurrences to replace
* @param newLabel The new label of the new operation
* @return The inc and out of the nodes that should be ignored from now on,
* are replaced with null.
* @see EmulatableBlock#emulatedIndexToActualInputIndex
*/
public static IntArrayList replaceNeutral(Graph g, int[] emulatedIndexToActualInputIndex, long[] emulatedCode, byte[] input, ObjectArrayList<int[]> occurrences, short newLabel) {
IntArrayList ignore = new IntArrayList();
/* Fix edges */
for (int[] occurrence : occurrences) {
if (occurrence.length <= 1) {
continue;
}
//codeId |-> bigger graph NodeId
Int2IntMap inputMap = Utils.getInputMap(g, emulatedCode, occurrence); //TODO: Change to more efficient one
int root = occurrence[0];
int[] sortedOccurrence = Arrays.copyOf(occurrence, occurrence.length);
Arrays.sort(sortedOccurrence); //for quick contains check
// Remove internal node data
for (int i = 1; i < occurrence.length; i++) {
int node = occurrence[i];
g.inc[node] = null;
g.out[node] = null;
g.label[node] = Graph.MARKER;
g.expandable_children[node] = null;
g.unexpandable_children[node] = null;
ignore.add(node);
}
// Make inputs point to root
for (int node : inputMap.values()) {
for (int i : g.out[node]) {
if (Arrays.binarySearch(sortedOccurrence, i) >= 0) {
replace(g.out[node], i, root);
}
}
}
// Order the inputs according to codeId for the root.
int[] sortedInputKeys = inputMap.keySet().toIntArray(); //TODO: optimizable, always same for same pattern
Arrays.sort(sortedInputKeys);
//int[] inputNodesOrdered = new int[sortedInputKeys.length];
int[] inputNodesOrdered = new int[input.length];
for (int i = 0; i < sortedInputKeys.length; i++) {
int actualIndex = emulatedIndexToActualInputIndex[i];
inputNodesOrdered[actualIndex] = inputMap.get(sortedInputKeys[i]);
}
for (int i = 0; i < input.length; i++) {
int insertInput = input[i];
if (insertInput != 2) {
if (inputNodesOrdered[i] != 0) {
System.err.println("Error, unexpected result. It should have been zero."); //TODO: delete debug message
}
inputNodesOrdered[i] = -(1 << insertInput); //- 2^input e.g 0=-1, 1=-2, 4=-16
}
}
g.inc[root] = inputNodesOrdered;
g.label[root] = newLabel;
// Fix expandable and unexpandable
IntArrayList expandable = new IntArrayList();
IntArrayList unexpandable = new IntArrayList();
for (int node : inputNodesOrdered) {
if (node < 0 || g.label[node] == Graph.INPUT) {
unexpandable.add(node);
} else {
expandable.add(node);
}
}
g.expandable_children[root] = expandable.toIntArray();
g.unexpandable_children[root] = unexpandable.toIntArray();
}
return ignore;
}
/**
* Execute the given {@code replaceBlock}. This means that in g, all
* occurrences in that {@link UseBlock} are replaced by a call to the
* patternP with a certain input. Both the occurrences of patternP and the
* emulatable occurrences are replaced.
*
* @param g The graph to replace in.
* @param replaceBlock The block containing information on which occurrence
* and patterns to replace.
* @param newOp The operation id of the newly introduced operation
* {@code patternP}.
* @return The list of nodes in g to ignore after this replacement.
*
* @see #replace(Graph, long[], ObjectArrayList, short)
* @see #replaceNeutral(Graph, int[], long[], byte[], ObjectArrayList,
* short)
*/
public static IntArrayList replace(Graph g, UseBlock replaceBlock, short newOp) {
IntArrayList ignoreList;
EmulatableBlock[] patterns = replaceBlock.patterns;
ObjectArrayList<ObjectArrayList<int[]>> usedOccurrences = replaceBlock.occurrences;
//Replace own
ignoreList = OperationUtils.replace(g, replaceBlock.patternP, usedOccurrences.get(0), newOp);
//Replace emulatable
for (int i = 0; i < patterns.length; i++) {
EmulatableBlock emulBlock = patterns[i];
IntArrayList ignore = OperationUtils.replaceNeutral(g, emulBlock.emulatedIndexToActualInputIndex,
emulBlock.emulatedCode, emulBlock.input, usedOccurrences.get(i + 1), newOp);
ignoreList.addAll(ignore);
}
return ignoreList;
}
/**
* Write all patterns involved in the given {@code replaceBlock} and the
* graph {@code g}.
*
* @param g The graph to write as well.
* @param replaceBlock The block of patterns to write. patternP and all
* patterns will be written.
* @param replacedOutputPath The output path for the graph g ac file.
* @param graphInputPath This path to an ac file that is used to extract the
* required literalMap.
* @param patternPOutputPath The output path for the patternP ac file.
* @param emulatedOutputPath The output paths for each
* {@code replaceBlock.patterns} ac file.
* @param symbols A mapping from the operationIds to the symbols used in the
* AC file. This must contain a mapping for each used label in the
* {@code g.label}. This will typically contain entries such as:
* <ul>
* <li> symbols.put(Graph.PRODUCT, "*");</li>
* <li> symbols.put(Graph.SUM, "+");</li>
* <li> symbols.put(Graph.INPUT, "l");</li>
* </ul>
* @param ignoreList The list of nodes to ignore when writing the graph g.
* @throws java.io.IOException Can occur while writing to any of the given
* paths or reading from {@code graphInputPath}.
*
* @see Utils#getLiteralMap(java.io.Reader)
*/
public static void writeUseBlock(Graph g, UseBlock replaceBlock, String replacedOutputPath,
String graphInputPath,
String patternPOutputPath, String[] emulatedOutputPath,
HashMap<Short, String> symbols,
IntArrayList ignoreList) throws IOException {
// Write base pattern
writePattern(replaceBlock.patternP, patternPOutputPath, symbols);
// Write rest
EmulatableBlock[] usedPatterns = replaceBlock.patterns;
for (int i = 0; i < usedPatterns.length; i++) {
writePattern(usedPatterns[i].emulatedCode, emulatedOutputPath[i], symbols);
}
// Write replaced AC (graph g)
writeAC(g, replacedOutputPath, graphInputPath, ignoreList, symbols);
}
/**
* Write Graph g to AC file and use the literals obtained from the ac file
* at path {@code readPath}.
*
* @param g The graph to write
* @param outPath The output path for the graph
* @param readPath The read path for the original graph, to extract the
* literalMap.
* @param ignore The nodes to ignore
* @param symbols The symbols of the operations
* @throws IOException
* @see #write(Graph, FileWriter, IntArrayList, HashMap, Int2IntMap)
*/
public static void writeAC(Graph g, String outPath, String readPath, IntArrayList ignore, HashMap<Short, String> symbols) throws IOException {
FileWriter writer = new FileWriter(outPath);
Int2IntMap literalMap = Utils.getLiteralMap(new FileReader(readPath));
OperationUtils.write(g, writer, ignore, symbols, literalMap);
}
/**
* Write the given pattern to the given path.
*
* @param best pattern to write
* @param patternPath The path to write to
* @param symbols Symbols of the operations
* @throws IOException
* @see #codeToGraph(long[])
* @see #writePatternGraph(FileWriter, Graph, IntArrayList, HashMap)
*/
public static void writePattern(long[] best, String patternPath, HashMap<Short, String> symbols) throws IOException {
Graph patternGraph = Graph.codeToGraph(best);
FileWriter writer = new FileWriter(patternPath);
writePatternGraph(writer, patternGraph, new IntArrayList(), symbols);
}
/**
* Replace first occurrence of x in array by y.
*
* @param array The array to replace in
* @param x The element to replace
* @param y The element to replace with
*/
private static void replace(int[] array, int x, int y) {
for (int i = 0; i < array.length; i++) {
if (array[i] == x) {
array[i] = y;
return;
}
}
}
/**
* Write the given graph to AC file but ignore the nodes in ignore. Nodes
* that have ignored nodes in between will be relocated. e.g 0,1,ignored,3,4
* writes 0,1,3,4
* <p>
* So symbol 4 2 3 4 5 means that operation associated with symbol has 4
* inputs, of which the order is of importance. The external inputs required
* for the first node (determined by the occurrence) are listed. Then follow
* the external inputs of the other nodes. The amount of inputs for each
* note is determined by the code (pattern) that the symbol represents.
*
* @param writer
* @param g The graph to write.
* @param ignore The nodes to ignore in the writing. Can be empty
* @param symbols The symbols which every label maps to. Every label should
* be present.
* @param literalMap The map that maps literal nodes to their id.
* @throws java.io.IOException
*/
public static void write(Graph g, FileWriter writer, IntArrayList ignore, HashMap<Short, String> symbols, Int2IntMap literalMap) throws IOException {
try (BufferedWriter bw = new BufferedWriter(writer)) {
int nbNode = g.inc.length - ignore.size();
int nbEdges = g.getEdgeCount(); //TODO test
int nbLiterals = literalMap.values().parallelStream().mapToInt(x -> Math.abs(x)).max().getAsInt();
bw.write("nnf " + nbNode + " " + nbEdges + " " + nbLiterals);
bw.newLine();
int[] ignoreSorted = ignore.toIntArray();
Arrays.sort(ignoreSorted);
for (int i = 0; i < g.inc.length; i++) {
int[] incs = g.inc[i];
if (incs != null) { //ignore otherwise
StringBuilder sb = new StringBuilder();
String symbol = symbols.get(g.label[i]);
sb.append(symbol);
sb.append(" ");
if (g.label[i] == Graph.INPUT) {
int literalId = literalMap.get(i);
sb.append(literalId); //TODO: check for correctness
} else {
sb.append(incs.length);
for (int node : incs) {
sb.append(" ");
sb.append(map(ignoreSorted, node));
}
}
bw.write(sb.toString());
bw.newLine();
}
}
}
}
/**
* Write the given Pattern graph to AC file but ignore the nodes in ignore.
* Pattern graph refers to graphs built from a canonical code. They are
* written by writing the incomings of 0 in a recursive way. e.g if 0 has
* incoming 1 and 2, node 1 is written first by the same method. This is
* different from just handling node 0,1,2,3,...
* <p>
* So <i>symbol</i> 4 2 3 4 5 means that operation associated with
* <i>symbol</i> has 4 inputs, of which the order is of importance. The
* amount of inputs for each note is determined by the code (pattern) that
* the symbol represents.
* <p>
* This uses
* {@link #writePatternGraph(FileWriter, Graph, IntArrayList, HashMap, Int2IntMap)}
* with the literalMap given by {@link Utils#getInputNodesMap(Graph)}.
*
* @param writer The file writer to write with
* @param patternGraph The pattern graph to write.
* @param ignore The nodes to ignore in the writing. Since ignored nodes
* must not be connected to the rest of the graph, this list is only used to
* determine the actual nodeCount. Can be empty.
* @param symbols The symbols which every label maps to. Every label should
* be present.
* @throws java.io.IOException
* @see Utils#getInputNodesMap(Graph)
*/
public static void writePatternGraph(FileWriter writer, Graph patternGraph, IntArrayList ignore, HashMap<Short, String> symbols) throws IOException {
Int2IntMap inputMap = patternGraph.getInputNodesMap();
OperationUtils.writePatternGraph(writer, patternGraph, new IntArrayList(), symbols, inputMap);
}
/**
* Write the given Pattern graph to AC file but ignore the nodes in ignore.
* Pattern graph refers to graphs built from a canonical code. They are
* written by writing the incomings of 0 in a recursive way. e.g if 0 has
* incoming 1 and 2, node 1 is written first by the same method. This is
* different from just handling node 0,1,2,3,...
* <p>
* So <i>symbol</i> 4 2 3 4 5 means that operation associated with
* <i>symbol</i> has 4 inputs, of which the order is of importance. The
* amount of inputs for each note is determined by the code (pattern) that
* the symbol represents.
*
* @param writer The file writer to write with
* @param g The graph to write.
* @param ignore The nodes to ignore in the writing. Since ignored nodes
* must not be connected to the rest of the graph, this list is only used to
* determine the actual nodeCount. Can be empty.
* @param symbols The symbols which every label maps to. Every label should
* be present.
* @param literalMap A mapping of {@link Graph#inc} index to its appropriate
* input index. See {@link Graph#getInputNodesMap()}.
* @throws java.io.IOException
*/
public static void writePatternGraph(FileWriter writer, Graph g, IntArrayList ignore, HashMap<Short, String> symbols, Int2IntMap literalMap) throws IOException {
try (BufferedWriter bw = new BufferedWriter(writer)) {
int nbNode = g.inc.length - ignore.size();
int nbEdges = g.getEdgeCount();
int nbLiterals = literalMap.values().parallelStream().mapToInt(x -> Math.abs(x)).max().getAsInt();
bw.write("nnf " + nbNode + " " + nbEdges + " " + nbLiterals);
bw.newLine();
Int2IntMap lineMap = new Int2IntOpenHashMap(); //incIndex -> lineIndex
writePatternGraph_aux(bw, g, 0, lineMap, symbols, literalMap);
}
}
/**
* Auxiliary method for {@link #writePatternGraph}. It deals with the
* {@code k}'th index in the graph. This is a recursive method that may
* executes the same method to process the indices incoming to the current
* index {@code k}.
*
* @param writer The BufferedWriter to write with
* @param g The graph
* @param k The index in graph to process.
* @param lineMap A map to keep track of the lines ({@link Graph#inc} index
* -> line number it appears on)
* @param symbols The symbols which every label maps to. Every label should
* be present.
* @param literalMap A mapping of {@link Graph#inc} index to its appropriate
* input index. See {@link Graph#getInputNodesMap()}.
*
* @return The line number assigned to index k of g's incoming structure.
* The given lineMap is also updated with indices that were handled.
*/
private static int writePatternGraph_aux(BufferedWriter bw, Graph g, int k, Int2IntMap lineMap, HashMap<Short, String> symbols, Int2IntMap literalMap) throws IOException {
if (lineMap.containsKey(k)) {
return lineMap.get(k);
} else if (g.inc[k].length == 0) { //literal
bw.write("l " + literalMap.get(k));
bw.newLine();
int id = lineMap.size();
lineMap.put(k, id);
return id;
} else {
// First handle incomings
for (int i : g.inc[k]) {
if (!lineMap.containsKey(i)) {
lineMap.put(i, writePatternGraph_aux(bw, g, i, lineMap, symbols, literalMap));
}
}
// Write own line
StringBuilder sb = new StringBuilder();
String symbol = symbols.get(g.label[k]);
sb.append(symbol);
sb.append(" ");
sb.append(g.inc[k].length);
for (int node : g.inc[k]) {
sb.append(" ");
sb.append(lineMap.get(node));
}
bw.write(sb.toString());
bw.newLine();
// Return
int id = lineMap.size();
lineMap.put(k, id);
return id;
}
}
/**
* Find the real nodeIndex given the node and the ignored nodes.
* <p>
* Replacing an occurrences ignores the inner nodes and links inputs to the
* one new node. Since we have to ignore those inner nodes that were
* dropped, we have to correct the id of nodes that have a higher id. e.g if
* nodeX is 6 but we ignore node {2,4,7}, the new Id of 6 is 4. The new Id
* of 8 would then be 5.
*
* @param sortedIgnore The ids to ignore in a sorted order.
* @param node The node id to map
* @return node - #(ignoredNodes < node)
*/
private static int map(int[] sortedIgnore, int node) {
int index = Arrays.binarySearch(sortedIgnore, node);
if (index >= 0) {
throw new IllegalArgumentException("Error: Found node that should have been ignored. This musn't happen!\nOutput might be invalid.");
} else {
index = Math.abs(index + 1);
return node - index;
}
}
/**
* Remove overlap for the given patterns. (Single threaded)
*
* @param patterns The patterns to remove the overlap of.
* @return A new map containing the same keys but now for every key/pattern
* only a subset of the original occurrences. The new subset does not
* overlap internally.
*/
public static Object2ObjectOpenCustomHashMap<long[], ObjectArrayList<int[]>> removeOverlapSingle(
Object2ObjectOpenCustomHashMap<long[], ObjectArrayList<int[]>> patterns) {
Object2ObjectOpenCustomHashMap<long[], ObjectArrayList<int[]>> subset
= new Object2ObjectOpenCustomHashMap(new ArrayLongHashStrategy());
for (Object2ObjectMap.Entry<long[], ObjectArrayList<int[]>> p : Object2ObjectMaps.fastIterable(patterns)) {
ObjectArrayList<int[]> values = p.getValue();
ObjectArrayList<int[]> newValues = new ObjectArrayList();
IntAVLTreeSet nodeSet = new IntAVLTreeSet();
for (int[] vertices : values) {
boolean alreadyIn = false;
//Check if no vertex is present yet in the nodeSet.
for (int v : vertices) {
if (nodeSet.contains(v)) {
alreadyIn = true;
break;
}
}
if (!alreadyIn) {
for (int i : vertices) {
nodeSet.add(i);
}
newValues.add(vertices);
}
}
subset.put(p.getKey(), newValues);
}
return subset;
}
/**
* Remove the overlap for the given patterns while also considering the
* given filterSet as overlap. So for each pattern, all occurrences are
* reduced to a set of occurrences that do not overlap with each other.
* Additionally, these occurrences do not overlap with a node present in the
* filterSet. This is multi-threaded (parallelStream).
*
* @param patterns The patterns to remove the overlap of. Remains unchanged.
* @param filterSet The set of nodes that aren't allowed to be used and are
* also considered overlap. Remains unchanged. This can also be null or
* empty. Remains unchanged.
* @return An array of entries, each originating from the given patterns
* map. For each entry/pattern, the occurrences are reduced to only
* occurrences that do not overlap.
*/
public static Map.Entry<long[], ObjectArrayList<int[]>>[] removeOverlap(
Entry<long[], ObjectArrayList<int[]>>[] patterns,
final IntAVLTreeSet filterSet) {
Map.Entry<long[], ObjectArrayList<int[]>>[] result = Arrays.stream(patterns)
.parallel()
.map(x -> removeOverlap_aux(x, filterSet))
.toArray(Map.Entry[]::new);
return result;
}
/**
* Remove the overlap for the given patterns while also considering the
* given filterSet as overlap. So for each pattern, all occurrences are
* reduced to a set of occurrences that do not overlap with each other.
* Additionally, these occurrences do not overlap with a node present in the
* filterSet. This is multi-threaded (parallelStream).
*
* @param patterns The patterns to remove the overlap of. Remains unchanged.
* @param filterSet The set of nodes that aren't allowed to be used and are
* also considered overlap. Remains unchanged. This can also be null or
* empty. Remains unchanged.
* @return An array of entries, each originating from the given patterns
* map. For each entry/pattern, the occurrences are reduced to only
* occurrences that do not overlap.
*/
public static Map.Entry<long[], ObjectArrayList<int[]>>[] removeOverlap(
Object2ObjectOpenCustomHashMap<long[], ObjectArrayList<int[]>> patterns,
final IntAVLTreeSet filterSet) {
Map.Entry<long[], ObjectArrayList<int[]>>[] result = patterns.object2ObjectEntrySet().parallelStream()
.map(x -> removeOverlap_aux(x, filterSet))
.toArray(Map.Entry[]::new);
return result;
}
/**
* Remove the overlap from the given entry/pattern.
*
* @param before The entry/pattern to remove its overlap of. Remains
* unchanged.
* @param ignoreFilter the nodes that are also considered overlap. This can
* also be null or empty. Remains unchanged.
* @return A new entry with the same key and a new occurrence list
* containing only non-overlapping occurrences.
*/
private static Map.Entry<long[], ObjectArrayList<int[]>> removeOverlap_aux(
Map.Entry<long[], ObjectArrayList<int[]>> before, IntAVLTreeSet ignoreFilter) {
ObjectArrayList<int[]> values = before.getValue();
ObjectArrayList<int[]> newValues = new ObjectArrayList();
/* TODO Some heuristic ? - This is just first come first serve */
IntSet nodeSet;
if (ignoreFilter == null) {
nodeSet = new IntAVLTreeSet();
} else {
nodeSet = (IntSet) ignoreFilter.clone();
}
for (int[] vertices : values) {
boolean alreadyIn = false;
//Check if no vertex is present yet in the nodeSet.
for (int v : vertices) {
if (nodeSet.contains(v)) {
alreadyIn = true;
break;
}
}
if (!alreadyIn) {
for (int i : vertices) {
nodeSet.add(i);
}
newValues.add(vertices);
}
}
return new AbstractObject2ObjectMap.BasicEntry<>(before.getKey(), newValues);
}
}