Advertisement
Guest User

Adhoc8_PK16

a guest
Jan 24th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.01 KB | None | 0 0
  1. /**
  2.  * Created by p1602638 on 1/16/17.
  3.  */
  4. public class Interval {
  5.  
  6.     private int low;
  7.     private int high;
  8.  
  9.     public Interval(int l, int h){
  10.  
  11.         this.low = l;
  12.         this.high = h;
  13.  
  14.     }
  15.  
  16.     @Override
  17.     public String toString(){
  18.         return "[" + low + "," + high + "]";
  19.     }
  20.  
  21.     public Interval intersect(Interval other){
  22.  
  23.         if(this.high < other.low || this.low > other.high)
  24.             return null;
  25.  
  26.         else
  27.             return new Interval(other.low, this.high);
  28.     }
  29.  
  30.     public Interval hull(Interval other){
  31.  
  32.         if(this == null || other == null)
  33.             return this == null ? other : this;
  34.  
  35.         return this.low < other.low ? (this.high > other.high) ? this : new Interval(this.low, other.high) : (this.high > other.high) ? new Interval(other.low, this.high): new Interval(other.low, other.high);
  36.  
  37.     }
  38.  
  39.     public boolean isValid(){
  40.         return this.low <= this.high;
  41.     }
  42.  
  43. }
  44.  
  45. import java.util.ArrayList;
  46.  
  47. /**
  48.  * Created by p1602638 on 1/16/17.
  49.  */
  50. public class IntervalUnion {
  51.  
  52.     ArrayList<Interval> list;
  53.  
  54.     public IntervalUnion(){
  55.  
  56.         list = new ArrayList<>();
  57.  
  58.     }
  59.  
  60.     @Override
  61.     public String toString(){
  62.  
  63.         String out = "[";
  64.  
  65.         for (int i = 0; i < list.size(); i++) {
  66.  
  67.             if(i != 0)
  68.                 out += ",";
  69.  
  70.             out += list.get(i).toString() + "";
  71.  
  72.         }
  73.     out += "]";
  74.  
  75.         return out;
  76.     }
  77.  
  78.     public void addInterval(Interval i){
  79.  
  80.         if(list.size() == 0){
  81.  
  82.             list.add(i);
  83.             return;
  84.  
  85.         }
  86.  
  87.     for (int j = 0; j < list.size(); j++) {
  88.  
  89.             if(!(list.get(j).intersect(i) == null)) {
  90.                 list.add(list.get(j).hull(i));
  91.                 list.remove(j);
  92.                 return;
  93.             }
  94.  
  95.     }
  96.  
  97.     list.add(i);
  98.  
  99.     }
  100.  
  101.     public void add(IntervalUnion iu){
  102.  
  103.         for (int i = 0; i < iu.list.size(); i++) {
  104.  
  105.             this.addInterval(iu.list.get(i));
  106.  
  107.         }
  108.  
  109.  
  110.  
  111.     }
  112.  
  113.  
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement