@Test public void testGetRangeIndex() { list.addRange(2,4); list.addRange(6,8); list.addRange(12,15); assertEquals(-1, list.getRangeIndex(0)); assertEquals(-1, list.getRangeIndex(1)); assertEquals(0, list.getRangeIndex(2)); assertEquals(0, list.getRangeIndex(3)); assertEquals(0, list.getRangeIndex(4)); assertEquals(-2, list.getRangeIndex(5)); assertEquals(1, list.getRangeIndex(6)); assertEquals(1, list.getRangeIndex(7)); assertEquals(1, list.getRangeIndex(8)); assertEquals(-3, list.getRangeIndex(9)); assertEquals(-3, list.getRangeIndex(10)); assertEquals(-3, list.getRangeIndex(11)); assertEquals(2, list.getRangeIndex(12)); assertEquals(2, list.getRangeIndex(13)); assertEquals(2, list.getRangeIndex(14)); assertEquals(2, list.getRangeIndex(15)); assertEquals(-4, list.getRangeIndex(16)); assertEquals(-4, list.getRangeIndex(17)); }
@Override public void addRange(int min, int max) { Range key = new Range(min, min); SortedSet<Range> headSet = set.headSet(key); if (!headSet.isEmpty()) { Range last = headSet.last(); if (min <= last.max){ last.max = Math.max(last.max, max); coalesce(last, set.tailSet(key).iterator()); return; } } SortedSet<Range> ss = set.tailSet(key); if (ss.isEmpty()) { set.add(new Range(min, max)); return; } Iterator<Range> it = ss.iterator(); Range first = it.next(); if (max < first.min) { set.add(new Range(min, max)); return; } first.min = Math.min(first.min, min); first.max = Math.max(first.max, max); coalesce(first, it); }
AdjacentSortedRangeList extends SortedRangeList { @Override public void addRange(int min, int max) { Range key = new Range(min, min); SortedSet<Range> headSet = set.headSet(key); if (!headSet.isEmpty()) { Range last = headSet.last(); if (min <= last.max){ last.max = Math.max(last.max, max); coalesce(last, set.tailSet(key).iterator()); return; } } SortedSet<Range> ss = set.tailSet(key); if (ss.isEmpty()) { set.add(new Range(min, max)); return; } Iterator<Range> it = ss.iterator(); Range first = it.next(); if (max < first.min) { set.add(new Range(min, max)); return; } first.min = Math.min(first.min, min); first.max = Math.max(first.max, max); coalesce(first, it); } }
AdjacentSortedRangeList extends SortedRangeList { @Override public void addRange(int min, int max) { Range key = new Range(min, min); SortedSet<Range> headSet = set.headSet(key); if (!headSet.isEmpty()) { Range last = headSet.last(); if (min <= last.max){ last.max = Math.max(last.max, max); coalesce(last, set.tailSet(key).iterator()); return; } } SortedSet<Range> ss = set.tailSet(key); if (ss.isEmpty()) { set.add(new Range(min, max)); return; } Iterator<Range> it = ss.iterator(); Range first = it.next(); if (max < first.min) { set.add(new Range(min, max)); return; } first.min = Math.min(first.min, min); first.max = Math.max(first.max, max); coalesce(first, it); } AdjacentSortedRangeList();  AdjacentSortedRangeList(AdjacentSortedRangeList list); }
AdjacentSortedRangeList extends SortedRangeList { @Override public void addRange(int min, int max) { Range key = new Range(min, min); SortedSet<Range> headSet = set.headSet(key); if (!headSet.isEmpty()) { Range last = headSet.last(); if (min <= last.max){ last.max = Math.max(last.max, max); coalesce(last, set.tailSet(key).iterator()); return; } } SortedSet<Range> ss = set.tailSet(key); if (ss.isEmpty()) { set.add(new Range(min, max)); return; } Iterator<Range> it = ss.iterator(); Range first = it.next(); if (max < first.min) { set.add(new Range(min, max)); return; } first.min = Math.min(first.min, min); first.max = Math.max(first.max, max); coalesce(first, it); } AdjacentSortedRangeList();  AdjacentSortedRangeList(AdjacentSortedRangeList list); @Override void addRange(int min, int max); AdjacentSortedRangeList intersect(AdjacentSortedRangeList other); }
AdjacentSortedRangeList extends SortedRangeList { @Override public void addRange(int min, int max) { Range key = new Range(min, min); SortedSet<Range> headSet = set.headSet(key); if (!headSet.isEmpty()) { Range last = headSet.last(); if (min <= last.max){ last.max = Math.max(last.max, max); coalesce(last, set.tailSet(key).iterator()); return; } } SortedSet<Range> ss = set.tailSet(key); if (ss.isEmpty()) { set.add(new Range(min, max)); return; } Iterator<Range> it = ss.iterator(); Range first = it.next(); if (max < first.min) { set.add(new Range(min, max)); return; } first.min = Math.min(first.min, min); first.max = Math.max(first.max, max); coalesce(first, it); } AdjacentSortedRangeList();  AdjacentSortedRangeList(AdjacentSortedRangeList list); @Override void addRange(int min, int max); AdjacentSortedRangeList intersect(AdjacentSortedRangeList other);  }