@Test public void testAddPosFirst() { list.addRange(0, 4); list.addRange(6, 10); list.addRange(3500, 4400); list.addRange(135676, 149987); assertEquals(4, list.getNumRanges()); assertEquals(new Range(0, 4), list.getRange(0)); assertEquals(new Range(6, 10), list.getRange(1)); assertEquals(new Range(3500, 4400), list.getRange(2)); assertEquals(new Range(135676, 149987), list.getRange(3)); list.addRange(-20480, -20352); list.addRange(-16384, -15824); list.addRange(-16128, -15616); list.addRange(-12288, -12235); list.addRange(-12234, -12224); list.addRange(-8192, 3); assertEquals(8, list.getNumRanges()); assertEquals(new Range(-20480, -20352), list.getRange(0)); assertEquals(new Range(-16384, -15616), list.getRange(1)); assertEquals(new Range(-12288, -12235), list.getRange(2)); assertEquals(new Range(-12234, -12224), list.getRange(3)); assertEquals(new Range(-8192, 4), list.getRange(4)); assertEquals(new Range(6, 10), list.getRange(5)); assertEquals(new Range(3500, 4400), list.getRange(6)); assertEquals(new Range(135676, 149987), list.getRange(7)); list.addRange(4200, 4800); list.addRange(149988, 150000); assertEquals(9, list.getNumRanges()); assertEquals(new Range(-20480, -20352), list.getRange(0)); assertEquals(new Range(-16384, -15616), list.getRange(1)); assertEquals(new Range(-12288, -12235), list.getRange(2)); assertEquals(new Range(-12234, -12224), list.getRange(3)); assertEquals(new Range(-8192, 4), list.getRange(4)); assertEquals(new Range(6, 10), list.getRange(5)); assertEquals(new Range(3500, 4800), list.getRange(6)); assertEquals(new Range(135676, 149987), list.getRange(7)); assertEquals(new Range(149988, 150000), list.getRange(8)); }
@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);  }