Skip to content

Commit 0d1ed1d

Browse files
committed
Changed interval data to use less memory in exchange for a sort() call
1 parent 50102f2 commit 0d1ed1d

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

src/com/jwetherell/algorithms/data_structures/IntervalTree.java

+4-8
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,10 @@ public static final class Interval<O> {
167167
private long center = Long.MIN_VALUE;
168168
private Interval<O> left = null;
169169
private Interval<O> right = null;
170-
/** overlap is sorted by start point **/
171170
private List<IntervalData<O>> overlap = new ArrayList<IntervalData<O>>();
172-
/** overlapEnd is reverse sorted by end point **/
173-
private List<IntervalData<O>> overlapEnd = new ArrayList<IntervalData<O>>();
174171

175172
private void add(IntervalData<O> data) {
176173
overlap.add(data);
177-
Collections.sort(overlap,startComparator);
178-
overlapEnd.add(data);
179-
Collections.sort(overlapEnd,endComparator);
180174
}
181175

182176
/**
@@ -190,6 +184,7 @@ public IntervalData<O> query(long index) {
190184
IntervalData<O> results = null;
191185
if (index < center) {
192186
// overlap is sorted by start point
187+
Collections.sort(overlap,startComparator);
193188
for (IntervalData<O> data : overlap) {
194189
if (data.start > index)
195190
break;
@@ -201,8 +196,9 @@ else if (results != null && temp != null)
201196
results.combined(temp);
202197
}
203198
} else if (index >= center) {
204-
// overlapEnd is reverse sorted by end point
205-
for (IntervalData<O> data : overlapEnd) {
199+
// overlap is reverse sorted by end point
200+
Collections.sort(overlap,endComparator);
201+
for (IntervalData<O> data : overlap) {
206202
if (data.end < index)
207203
break;
208204

0 commit comments

Comments
 (0)