4 Copyright (c) 2018, Mungo Carstairs
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice, this
11 list of conditions and the following disclaimer.
13 * Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
17 * Neither the name of the copyright holder nor the names of its
18 contributors may be used to endorse or promote products derived from
19 this software without specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 package intervalstore.impl;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.Comparator;
37 import java.util.List;
39 import intervalstore.api.IntervalI;
42 * A comparator that orders ranges by either start position ascending. If
43 * position matches, ordering is by length descending. This provides the
44 * canonical ordering of intervals into subranges in order to build a nested
49 public class NCListBuilder<T extends IntervalI>
51 class NCListComparator<V extends IntervalI> implements Comparator<V>
54 * Compares two intervals in a way that will sort a list by start position
55 * ascending, then by length descending. Answers
57 * <li>a negative value if o1.begin < o2.begin</li>
58 * <li>else a positive value if o1.begin > o2.begin</li>
59 * <li>else a negative value if o1.end > o2.end</li>
60 * <li>else a positive value of o1.end < o2.end</li>
65 public int compare(V o1, V o2)
67 int order = Integer.compare(o1.getBegin(), o2.getBegin());
71 * if tied on start position, longer length sorts to left
72 * i.e. the negation of normal ordering by length
74 order = Integer.compare(o2.getEnd(), o1.getEnd());
80 private Comparator<T> comparator = new NCListComparator<>();
85 public NCListBuilder()
90 * Answers a comparator which sorts items of type T by start position
91 * ascending, and within that by end position (length) descending)
95 Comparator<T> getComparator()
101 * Sorts and traverses the ranges to identify sublists, whose start intervals
102 * are overlapping or disjoint but not mutually contained. Answers a list of
103 * start-end indices of the sorted list of ranges.
108 List<IntervalI> partitionNestedSublists(List<T> ranges)
110 List<IntervalI> sublists = new ArrayList<>();
113 * sort by start ascending, length descending, so that
114 * contained intervals follow their containing interval
116 Collections.sort(ranges, comparator);
118 int listStartIndex = 0;
120 IntervalI lastParent = ranges.get(0);
121 boolean first = true;
123 for (int i = 0; i < ranges.size(); i++)
125 IntervalI nextInterval = ranges.get(i);
126 if (!first && !lastParent.properlyContainsInterval(nextInterval))
129 * this interval is not properly contained in the parent;
130 * close off the last sublist
132 sublists.add(new Range(listStartIndex, i - 1));
134 lastParent = nextInterval;
139 sublists.add(new Range(listStartIndex, ranges.size() - 1));