@Test void testSegmentSplitNTimes() { { final List<Segment> segmentMasks = Segment.splitBalanced(Segment.ROOT_SEGMENT, 5); assertThat(segmentMasks.size(), is(6)); assertThat(segmentMasks.get(5), equalTo(new Segment(5, 0x7))); assertThat(segmentMasks.get(0), equalTo(new Segment(0, 0x7))); assertThat(segmentMasks.get(1), equalTo(new Segment(1, 0x7))); assertThat(segmentMasks.get(2), equalTo(new Segment(2, 0x3))); assertThat(segmentMasks.get(3), equalTo(new Segment(3, 0x3))); assertThat(segmentMasks.get(4), equalTo(new Segment(4, 0x7))); } }
public static List<Segment> splitBalanced(Segment segment, int numberOfTimes) { final SortedSet<Segment> toBeSplit = new TreeSet<>(Comparator.comparing(Segment::getMask) .thenComparing(Segment::getSegmentId)); toBeSplit.add(segment); for (int i = 0; i < numberOfTimes; i++) { final Segment workingSegment = toBeSplit.first(); toBeSplit.remove(workingSegment); toBeSplit.addAll(Arrays.asList(workingSegment.split())); } ArrayList<Segment> result = new ArrayList<>(toBeSplit); result.sort(Comparator.comparing(Segment::getSegmentId)); return result; }
Segment implements Comparable<Segment> { public static List<Segment> splitBalanced(Segment segment, int numberOfTimes) { final SortedSet<Segment> toBeSplit = new TreeSet<>(Comparator.comparing(Segment::getMask) .thenComparing(Segment::getSegmentId)); toBeSplit.add(segment); for (int i = 0; i < numberOfTimes; i++) { final Segment workingSegment = toBeSplit.first(); toBeSplit.remove(workingSegment); toBeSplit.addAll(Arrays.asList(workingSegment.split())); } ArrayList<Segment> result = new ArrayList<>(toBeSplit); result.sort(Comparator.comparing(Segment::getSegmentId)); return result; } }
Segment implements Comparable<Segment> { public static List<Segment> splitBalanced(Segment segment, int numberOfTimes) { final SortedSet<Segment> toBeSplit = new TreeSet<>(Comparator.comparing(Segment::getMask) .thenComparing(Segment::getSegmentId)); toBeSplit.add(segment); for (int i = 0; i < numberOfTimes; i++) { final Segment workingSegment = toBeSplit.first(); toBeSplit.remove(workingSegment); toBeSplit.addAll(Arrays.asList(workingSegment.split())); } ArrayList<Segment> result = new ArrayList<>(toBeSplit); result.sort(Comparator.comparing(Segment::getSegmentId)); return result; } protected  Segment(int segmentId, int mask); }
Segment implements Comparable<Segment> { public static List<Segment> splitBalanced(Segment segment, int numberOfTimes) { final SortedSet<Segment> toBeSplit = new TreeSet<>(Comparator.comparing(Segment::getMask) .thenComparing(Segment::getSegmentId)); toBeSplit.add(segment); for (int i = 0; i < numberOfTimes; i++) { final Segment workingSegment = toBeSplit.first(); toBeSplit.remove(workingSegment); toBeSplit.addAll(Arrays.asList(workingSegment.split())); } ArrayList<Segment> result = new ArrayList<>(toBeSplit); result.sort(Comparator.comparing(Segment::getSegmentId)); return result; } protected  Segment(int segmentId, int mask); static Segment[] computeSegments(int... segments); static Segment computeSegment(int segmentId, int... availableSegmentIds); static List<Segment> splitBalanced(Segment segment, int numberOfTimes); Segment mergedWith(Segment other); int mergeableSegmentId(); boolean isMergeableWith(Segment other); int getSegmentId(); int getMask(); boolean matches(int value); boolean matches(Object value); Segment[] split(); int splitSegmentId(); @Override boolean equals(Object o); @Override String toString(); @Override int hashCode(); @Override int compareTo(Segment that); }
Segment implements Comparable<Segment> { public static List<Segment> splitBalanced(Segment segment, int numberOfTimes) { final SortedSet<Segment> toBeSplit = new TreeSet<>(Comparator.comparing(Segment::getMask) .thenComparing(Segment::getSegmentId)); toBeSplit.add(segment); for (int i = 0; i < numberOfTimes; i++) { final Segment workingSegment = toBeSplit.first(); toBeSplit.remove(workingSegment); toBeSplit.addAll(Arrays.asList(workingSegment.split())); } ArrayList<Segment> result = new ArrayList<>(toBeSplit); result.sort(Comparator.comparing(Segment::getSegmentId)); return result; } protected  Segment(int segmentId, int mask); static Segment[] computeSegments(int... segments); static Segment computeSegment(int segmentId, int... availableSegmentIds); static List<Segment> splitBalanced(Segment segment, int numberOfTimes); Segment mergedWith(Segment other); int mergeableSegmentId(); boolean isMergeableWith(Segment other); int getSegmentId(); int getMask(); boolean matches(int value); boolean matches(Object value); Segment[] split(); int splitSegmentId(); @Override boolean equals(Object o); @Override String toString(); @Override int hashCode(); @Override int compareTo(Segment that); static final Segment ROOT_SEGMENT; }