Created
February 24, 2016 05:31
-
-
Save deyindra/af03e65bbc227ef12e6e to your computer and use it in GitHub Desktop.
Given a array of Objects and Array of Range group all the objects with Appropriate Range
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Range<T extends Comparable<T>> implements Comparable<Range<T>> { | |
| private T lower; | |
| private T upper; | |
| public Range(T lower, T upper) { | |
| assert(lower!=null && upper!=null && upper.compareTo(lower)>0); | |
| this.lower = lower; | |
| this.upper = upper; | |
| } | |
| //Asummption they are no overlapping | |
| @Override | |
| public int compareTo(Range<T> o) { | |
| if(o==null){ | |
| return 1; | |
| }else if(this==o) { | |
| return 0; | |
| }else{ | |
| if(this.equals(o)){ | |
| return 0; | |
| }else{ | |
| if (isOverLapped(o)) { | |
| throw new IllegalArgumentException("Range can not be over lapped"); | |
| }else{ | |
| if(o.upper.compareTo(this.lower)<0){ | |
| return 1; | |
| }else{ | |
| return -1; | |
| } | |
| } | |
| } | |
| } | |
| } | |
| @Override | |
| public boolean equals(Object o) { | |
| if (this == o) return true; | |
| if (o == null || getClass() != o.getClass()) return false; | |
| Range<?> range = (Range<?>) o; | |
| if (!lower.equals(range.lower)) return false; | |
| if (!upper.equals(range.upper)) return false; | |
| return true; | |
| } | |
| @Override | |
| public int hashCode() { | |
| int result = lower.hashCode(); | |
| result = 31 * result + upper.hashCode(); | |
| return result; | |
| } | |
| private boolean isOverLapped(Range<T> o){ | |
| if(o.checkBoundary(lower)==Bound.BETWEEN | |
| || o.checkBoundary(upper)==Bound.BETWEEN | |
| || checkBoundary(o.lower)==Bound.BETWEEN){ | |
| return true; | |
| } | |
| return false; | |
| } | |
| public Bound checkBoundary(T object){ | |
| if(object.compareTo(lower)<0){ | |
| return Bound.LOWER; | |
| }else if(object.compareTo(lower)>=0 && object.compareTo(upper)<=0){ | |
| return Bound.BETWEEN; | |
| }else{ | |
| return Bound.UPPER; | |
| } | |
| } | |
| public enum Bound{ | |
| LOWER, BETWEEN, UPPER | |
| } | |
| @Override | |
| public String toString() { | |
| final StringBuilder sb = new StringBuilder("Range{"); | |
| sb.append("lower=").append(lower); | |
| sb.append(", upper=").append(upper); | |
| sb.append('}'); | |
| return sb.toString(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class RangeSearch { | |
| private static <T extends Comparable<T>> Range<T> rangeSearch(Range<T>[] array, T object){ | |
| int start=0; | |
| int end = array.length-1; | |
| while (end>=start){ | |
| int mid = start+(end-start)/2; | |
| Range<T> range = array[mid]; | |
| if(range.checkBoundary(object)== Range.Bound.BETWEEN){ | |
| return range; | |
| }else if(range.checkBoundary(object)== Range.Bound.UPPER){ | |
| start=mid+1; | |
| }else{ | |
| end=mid-1; | |
| } | |
| } | |
| return null; | |
| } | |
| public static <T extends Comparable<T>> Collection<List<T>> groupObjectsBasedOnRanges(Range<T>[] array, T[] objectArray){ | |
| Map<Range<T>, List<T>> rangeMapping = new HashMap<>(); | |
| for(T object:objectArray){ | |
| Range<T> range = rangeSearch(array,object); | |
| List<T> list = rangeMapping.get(range); | |
| if(list==null){ | |
| list = new ArrayList<>(); | |
| } | |
| list.add(object); | |
| rangeMapping.put(range,list); | |
| } | |
| return rangeMapping.values(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment