-
Notifications
You must be signed in to change notification settings - Fork 8
/
PartitionBuilder.java
246 lines (214 loc) · 8.74 KB
/
PartitionBuilder.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
/*
* SNE-XACML: A high performance XACML evaluation engine.
*
* Copyright (C) 2013-2014 Canh Ngo <canhnt@gmail.com>
* System and Network Engineering Group, University of Amsterdam.
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
/**
* System and Network Engineering Group
* University of Amsterdam
*
*/
package nl.uva.sne.midd.partition;
import nl.uva.sne.midd.MIDDException;
import nl.uva.sne.midd.interval.EndPoint;
import nl.uva.sne.midd.interval.Interval;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
/**
* @author Canh Ngo
* @date: Sep 17, 2012
*/
public class PartitionBuilder {
private static final Logger log = LoggerFactory.getLogger(PartitionBuilder.class);
public static <T extends Comparable<T>> Partition<T> createPartition(List<Interval<T>> intervals) {
return new Partition<T>(intervals);
}
/**
* Combine two disjoint interval partitions into a single disjoint partition. Condition: every interval of the
* result partition belongs to at least an old partition
* <p/>
* Version 2
*
* @param <T>
* @param p1
* @param p2
* @return
*/
public static <T extends Comparable<T>> Partition<T> union(Partition<T> p1, Partition<T> p2) throws MIDDException {
if (p1.size() == 0) {
return p2;
}
if (p2.size() == 0) {
return p1;
}
Set<EndPoint<T>> boundSet = new HashSet<EndPoint<T>>();
//collect all bounds and remove duplicated items
for (Interval<T> interval : p1.getIntervals()) {
boundSet.add(interval.getLowerBound());
boundSet.add(interval.getUpperBound());
}
for (Interval<T> interval : p2.getIntervals()) {
boundSet.add(interval.getLowerBound());
boundSet.add(interval.getUpperBound());
}
if (boundSet.size() == 0) {
throw new RuntimeException("no bounds");
}
//sorting the bounds
List<EndPoint<T>> sortedBounds = new ArrayList<EndPoint<T>>(boundSet);
Collections.sort(sortedBounds);
List<Interval<T>> tempPartition = new ArrayList<Interval<T>>();
// Generate new intervals
for (int i = 0; i < sortedBounds.size() - 1; i++) {
EndPoint<T> lowBound = sortedBounds.get(i);
EndPoint<T> upBound = sortedBounds.get(i + 1);
tempPartition.add(new Interval<T>(lowBound));
tempPartition.add(new Interval<T>(lowBound, upBound));
}
// add the last end-point
if (sortedBounds.size() > 0) {
EndPoint<T> lastBound = sortedBounds.get(sortedBounds.size() - 1);
tempPartition.add(new Interval<T>(lastBound));
} else {
log.warn("Empty sortedBound");
}
// combine adjacent intervals which belong to the same type
List<Interval<T>> outputPartition = new ArrayList<Interval<T>>();
Interval<T> newInterval = null;
Interval<T> superOfNewIntervalP1 = null;
Interval<T> superOfNewIntervalP2 = null;
// int newIntervalType = 0;
/**
* New version, updated at 2:00PM, 09/19/2012
*/
for (int i = 0; i < tempPartition.size(); i++) {
Interval<T> tempInterval = tempPartition.get(i);
Interval<T> superOfTempIntervalP1 = p1.getSuperInterval(tempInterval);
Interval<T> superOfTempIntervalP2 = p2.getSuperInterval(tempInterval);
if (superOfTempIntervalP1 == null && superOfTempIntervalP2 == null) // not belong to any partitions, ignore it
{
continue;
}
if (newInterval == null) {
newInterval = tempInterval;
superOfNewIntervalP1 = superOfTempIntervalP1;
superOfNewIntervalP2 = superOfTempIntervalP2;
continue;
}
if (superOfNewIntervalP1 == superOfTempIntervalP1 && superOfNewIntervalP2 == superOfTempIntervalP2) {
newInterval.includeBound(tempInterval);
} else {
outputPartition.add(newInterval);
newInterval = tempInterval;
superOfNewIntervalP1 = superOfTempIntervalP1;
superOfNewIntervalP2 = superOfTempIntervalP2;
}
}
if (newInterval != null) {
outputPartition.add(newInterval);
}
return new Partition<T>(outputPartition);
}
/**
* Return the intersect of two disjoint partition. The returned disjoint partition has items that: - Each interval
* item is a subset of only an interval I1 \in p1 and a subset of only an interval I2 \in p2.
*
* @param p1
* @param p2
* @return
*/
public static <T extends Comparable<T>> Partition<T> intersect(Partition<T> p1, Partition<T> p2) throws MIDDException {
if (p1.size() == 0 || p2.size() == 0) {
return new Partition<T>();
}
List<Interval<T>> tempIntervals = combine(p1, p2);
Interval<T> newInterval = null;
Interval<T> superOfNewIntervalP1 = null;
Interval<T> superOfNewIntervalP2 = null;
// combine adjacent intervals which belong to the same type
List<Interval<T>> outputPartition = new ArrayList<Interval<T>>();
for (int i = 0; i < tempIntervals.size(); i++) {
Interval<T> tempInterval = tempIntervals.get(i);
Interval<T> superOfTempIntervalP1 = p1.getSuperInterval(tempInterval);
Interval<T> superOfTempIntervalP2 = p2.getSuperInterval(tempInterval);
// if it belongs to at most one partition, ignore!
if (superOfTempIntervalP1 == null || superOfTempIntervalP2 == null) {
continue;
}
if (newInterval == null) {
newInterval = tempInterval;
superOfNewIntervalP1 = superOfTempIntervalP1;
superOfNewIntervalP2 = superOfTempIntervalP2;
continue;
}
if (superOfNewIntervalP1 == superOfTempIntervalP1 && superOfNewIntervalP2 == superOfTempIntervalP2) {
newInterval.includeBound(tempInterval);
} else {
outputPartition.add(newInterval);
newInterval = tempInterval;
superOfNewIntervalP1 = superOfTempIntervalP1;
superOfNewIntervalP2 = superOfTempIntervalP2;
}
}
if (newInterval != null) {
outputPartition.add(newInterval);
}
return new Partition<T>(outputPartition);
}
/**
* Create a list of intervals from two partitions
*
* @param p1
* @param p2
* @return
*/
private static <T extends Comparable<T>> List<Interval<T>> combine(Partition<T> p1, Partition<T> p2) throws MIDDException {
Set<EndPoint<T>> boundSet = new HashSet<EndPoint<T>>();
//collect all bounds and remove duplicated items
for (Interval<T> interval : p1.getIntervals()) {
boundSet.add(interval.getLowerBound());
boundSet.add(interval.getUpperBound());
}
for (Interval<T> interval : p2.getIntervals()) {
boundSet.add(interval.getLowerBound());
boundSet.add(interval.getUpperBound());
}
//sorting the bounds
List<EndPoint<T>> sortedBounds = new ArrayList<EndPoint<T>>(boundSet);
Collections.sort(sortedBounds);
List<Interval<T>> tempPartition = new ArrayList<Interval<T>>();
// Generate new intervals
for (int i = 0; i < sortedBounds.size() - 1; i++) {
EndPoint<T> lowBound = sortedBounds.get(i);
EndPoint<T> upBound = sortedBounds.get(i + 1);
tempPartition.add(new Interval<T>(lowBound));
tempPartition.add(new Interval<T>(lowBound, upBound));
}
// add the last end-point
if (sortedBounds.size() > 0) {
EndPoint<T> lastBound = sortedBounds.get(sortedBounds.size() - 1);
tempPartition.add(new Interval<T>(lastBound));
} else {
log.warn("empty sortedBound");
}
return tempPartition;
}
}