@Test void testComputeSegment() { for (int segmentCount = 0; segmentCount < 256; segmentCount++) { List<Segment> segments = Segment.splitBalanced(Segment.ROOT_SEGMENT, segmentCount); int[] segmentIds = new int[segments.size()]; for (int i = 0; i < segmentIds.length; i++) { segmentIds[i] = segments.get(i).getSegmentId(); } for (Segment segment : segments) { assertEquals(segment, Segment.computeSegment(segment.getSegmentId(), segmentIds), "Got wrong segment for " + segmentCount + " number of segments"); } } }
public static Segment computeSegment(int segmentId, int... availableSegmentIds) { Arrays.sort(availableSegmentIds); int splitCandidate = segmentId == 0 ? 1 : (Integer.highestOneBit(segmentId) << 1); while (Arrays.binarySearch(availableSegmentIds, splitCandidate | segmentId) >= 0) { splitCandidate = splitCandidate << 1; } int mask = splitCandidate - 1; return new Segment(segmentId, mask); }
Segment implements Comparable<Segment> { public static Segment computeSegment(int segmentId, int... availableSegmentIds) { Arrays.sort(availableSegmentIds); int splitCandidate = segmentId == 0 ? 1 : (Integer.highestOneBit(segmentId) << 1); while (Arrays.binarySearch(availableSegmentIds, splitCandidate | segmentId) >= 0) { splitCandidate = splitCandidate << 1; } int mask = splitCandidate - 1; return new Segment(segmentId, mask); } }
Segment implements Comparable<Segment> { public static Segment computeSegment(int segmentId, int... availableSegmentIds) { Arrays.sort(availableSegmentIds); int splitCandidate = segmentId == 0 ? 1 : (Integer.highestOneBit(segmentId) << 1); while (Arrays.binarySearch(availableSegmentIds, splitCandidate | segmentId) >= 0) { splitCandidate = splitCandidate << 1; } int mask = splitCandidate - 1; return new Segment(segmentId, mask); } protected  Segment(int segmentId, int mask); }
Segment implements Comparable<Segment> { public static Segment computeSegment(int segmentId, int... availableSegmentIds) { Arrays.sort(availableSegmentIds); int splitCandidate = segmentId == 0 ? 1 : (Integer.highestOneBit(segmentId) << 1); while (Arrays.binarySearch(availableSegmentIds, splitCandidate | segmentId) >= 0) { splitCandidate = splitCandidate << 1; } int mask = splitCandidate - 1; return new Segment(segmentId, mask); } 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 Segment computeSegment(int segmentId, int... availableSegmentIds) { Arrays.sort(availableSegmentIds); int splitCandidate = segmentId == 0 ? 1 : (Integer.highestOneBit(segmentId) << 1); while (Arrays.binarySearch(availableSegmentIds, splitCandidate | segmentId) >= 0) { splitCandidate = splitCandidate << 1; } int mask = splitCandidate - 1; return new Segment(segmentId, mask); } 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; }