Esempio n. 1
1
  @Test
  public void splitLookup() {
    // do the split
    int pivot = 4;
    int max = 5;
    List<Integer> l = List.range(1, max + 1);
    TreeMap<Integer, String> m2 = TreeMap.treeMap(Ord.intOrd, l.zip(l.map(i -> i.toString())));
    P3<TreeMap<Integer, String>, Option<String>, TreeMap<Integer, String>> p3 =
        m2.splitLookup(pivot);

    // create expected output
    List<Integer> leftList = List.range(1, pivot);
    TreeMap<Integer, String> leftMap =
        TreeMap.treeMap(Ord.intOrd, leftList.zip(leftList.map(i -> i.toString())));
    List<Integer> rightList = List.range(pivot + 1, max + 1);
    TreeMap<Integer, String> rightMap =
        TreeMap.treeMap(Ord.intOrd, rightList.zip(rightList.map(i -> i.toString())));

    // debug info
    if (true) {
      Show<TreeMap<Integer, String>> st = Show.treeMapShow(Show.intShow, Show.stringShow);
      Show<P3<TreeMap<Integer, String>, Option<String>, TreeMap<Integer, String>>> sp3 =
          Show.p3Show(st, Show.optionShow(Show.stringShow), st);
      sp3.println(p3);
    }

    // do the assert
    Equal<TreeMap<Integer, String>> tme = Equal.treeMapEqual(Equal.intEqual, Equal.stringEqual);
    Equal<P3<TreeMap<Integer, String>, Option<String>, TreeMap<Integer, String>>> eq =
        Equal.p3Equal(tme, Equal.optionEqual(Equal.stringEqual), tme);
    assertTrue(eq.eq(p3, p(leftMap, some(Integer.toString(pivot)), rightMap)));
  }
Esempio n. 2
1
  @Test
  public void split() {
    // do the split
    int pivot = 4;
    int max = 5;
    List<Integer> l = List.range(1, max + 1);
    TreeMap<Integer, String> m2 = TreeMap.treeMap(Ord.intOrd, l.zip(l.map(i -> i.toString())));
    P3<Set<String>, Option<String>, Set<String>> p = m2.split(Ord.stringOrd, pivot);

    // print debug info
    Show<TreeMap<Integer, String>> st = Show.treeMapShow(Show.intShow, Show.stringShow);
    Show<Set<String>> ss = Show.setShow(Show.stringShow);
    Show<Option<String>> so = Show.optionShow(Show.stringShow);
    Show<P3<Set<String>, Option<String>, Set<String>>> sp3 = Show.p3Show(ss, so, ss);
    if (true) {
      st.println(m2);
      sp3.println(p);
    }

    // assert equals
    Equal<Set<String>> seq = Equal.setEqual(Equal.stringEqual);
    Set<String> left = toSetString(List.range(1, pivot));
    Set<String> right = toSetString(List.range(pivot + 1, max + 1));
    P3<Set<String>, Option<String>, Set<String>> expected =
        p(left, some(Integer.toString(pivot)), right);
    assertTrue(Equal.p3Equal(seq, Equal.optionEqual(Equal.stringEqual), seq).eq(p, expected));
  }
Esempio n. 3
1
 @Test
 public void toMutableMap() {
   int max = 5;
   List<List<Integer>> l = List.range(1, max + 1).map(n -> List.single(n));
   TreeMap<List<Integer>, String> m2 =
       TreeMap.treeMap(Ord.listOrd(Ord.intOrd), l.zip(l.map(i -> i.toString())));
   Map<List<Integer>, String> mm = m2.toMutableMap();
   assertEquals(m2.keys(), List.fromIterable(mm.keySet()));
 }
  public int[] findRightInterval(Interval[] intervals) {
    if (intervals == null) {
      throw new IllegalArgumentException();
    }

    int len = intervals.length;
    int[] result = new int[len];
    TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();

    for (int i = 0; i < len; i++) {
      map.put(intervals[i].start, i);
    }

    for (int i = 0; i < len; i++) {
      Integer key = map.ceilingKey(intervals[i].end);

      if (key == null) {
        result[i] = -1;
      } else {
        result[i] = map.get(key);
      }
    }

    return result;
  }
Esempio n. 5
0
  public TreeMap getItem(Point point) {
    Rectangle rect = getClientArea();
    double extent = Math.min(rect.width, rect.height) * frame.scale;

    // normalized coordinates in the interval [0,1)
    double x0 = (point.x - (rect.x - frame.offsetX)) / extent;
    double y0 = (point.y - (rect.y - frame.offsetY)) / extent;

    if (x0 > 1.0 || x0 < 0) return null;
    if (y0 > 1.0 || y0 < 0) return null;

    TreeMap tree = treeMap;
    while (extent > 128 && tree != null && tree.getNetblock().getCIDR() < 32) {
      x0 *= 16;
      y0 *= 16;
      int xi = (int) x0;
      int yi = (int) y0;
      x0 -= xi;
      y0 -= yi;
      IPv4Netblock subnet = curve.getSubNetblock(tree.getNetblock(), yi * 16 + xi);
      tree = tree.getSubTree(subnet);
      extent = extent / 16;
    }
    return tree;
  }
Esempio n. 6
0
 @Test
 public void testLargeInserts() {
   // check that inserting a large number of items performs ok
   // taken from https://code.google.com/p/functionaljava/issues/detail?id=31 and
   // https://github.com/functionaljava/functionaljava/pull/13/files
   final int n = 10000;
   TreeMap<Integer, String> m = TreeMap.empty(Ord.intOrd);
   for (int i = 0; i < n; i++) {
     m = m.set(i, "abc " + i);
   }
 }
Esempio n. 7
0
 /**
  * Adds all of the elements in the specified collection to this set.
  *
  * @param c collection containing elements to be added to this set
  * @return {@code true} if this set changed as a result of the call
  * @throws ClassCastException if the elements provided cannot be compared with the elements
  *     currently in the set
  * @throws NullPointerException if the specified collection is null or if any element is null and
  *     this set uses natural ordering, or its comparator does not permit null elements
  */
 public boolean addAll(Collection<? extends E> c) {
   // Use linear-time version if applicable
   if (m.size() == 0 && c.size() > 0 && c instanceof SortedSet && m instanceof TreeMap) {
     SortedSet<? extends E> set = (SortedSet<? extends E>) c;
     TreeMap<E, Object> map = (TreeMap<E, Object>) m;
     Comparator<? super E> cc = (Comparator<? super E>) set.comparator();
     Comparator<? super E> mc = map.comparator();
     if (cc == mc || (cc != null && cc.equals(mc))) {
       map.addAllForTreeSet(set, PRESENT);
       return true;
     }
   }
   return super.addAll(c);
 }
Esempio n. 8
0
 /**
  * Contains the 'main' method. It issues calls to various methods to perform the following
  * functions: - Read data from input file - Create a visualisation of list of states - Create a
  * tree-map of the data - Create a mashup for the data
  */
 public static void main(String[] args) {
   ReadFile.run();
   StateMenu.run();
   TreeMap.run();
   MakeSummaryTable.make();
   Mashup.display(MakeSummaryTable.summary);
 }
Esempio n. 9
0
 @Test
 public void emptyHashCode() {
   // Hash code of tree map should not throw NullPointerException
   // see https://github.com/functionaljava/functionaljava/issues/187
   int i = TreeMap.empty(Ord.stringOrd).hashCode();
   assertTrue(true);
 }
Esempio n. 10
0
 @Test
 public void minKey() {
   TreeMap<Integer, Integer> t1 = TreeMap.<Integer, Integer>empty(Ord.intOrd);
   assertThat(t1.minKey(), equalTo(none()));
   TreeMap<Integer, Integer> t2 = t1.set(1, 2).set(2, 4).set(10, 20).set(5, 10).set(0, 100);
   assertThat(t2.minKey(), equalTo(some(0)));
   assertThat(t2.delete(0).minKey(), equalTo(some(1)));
 }
Esempio n. 11
0
  /** Reconstitute the {@code TreeSet} instance from a stream (that is, deserialize it). */
  private void readObject(java.io.ObjectInputStream s)
      throws java.io.IOException, ClassNotFoundException {
    // Read in any hidden stuff
    s.defaultReadObject();

    // Read in Comparator
    Comparator<? super E> c = (Comparator<? super E>) s.readObject();

    // Create backing TreeMap
    TreeMap<E, Object> tm;
    if (c == null) tm = new TreeMap<E, Object>();
    else tm = new TreeMap<E, Object>(c);
    m = tm;

    // Read in size
    int size = s.readInt();

    tm.readTreeSet(size, s, PRESENT);
  }
Esempio n. 12
0
 @Test
 public void testString() {
   TreeMap<Integer, String> t = TreeMap.treeMap(Ord.intOrd, p(1, "a"), p(2, "b"), p(3, "c"));
   TreeMap<Integer, String> t2 = TreeMap.treeMap(Ord.intOrd, p(3, "c"), p(2, "b"), p(1, "a"));
   Stream<P2<Integer, String>> s = Stream.stream(p(1, "a"), p(2, "b"), p(3, "c"));
   assertThat(t.toStream(), equalTo(s));
   assertThat(t2.toStream(), equalTo(s));
 }
Esempio n. 13
0
  public Rectangle getItemBounds(TreeMap subtree) {
    Rectangle rect = getClientArea();
    int x = rect.x - (int) frame.offsetX;
    int y = rect.y - (int) frame.offsetY;
    int extent = (int) (Math.min(rect.width, rect.height) * frame.scale);

    TreeMap tree = treeMap;
    while (!tree.getNetblock().equals(subtree.getNetblock())) {
      int h = curve.getIndex(tree.getNetblock(), subtree.getNetblock());
      int xi = h % 16;
      int yi = h / 16;
      x = x + (xi * extent / 16);
      y = y + (yi * extent / 16);
      extent = extent / 16;
      tree = tree.getSubTree(subtree.getNetblock());
    }
    return new Rectangle(x, y, extent, extent);
  }
Esempio n. 14
0
  private void paint(PaintEvent event) {
    GC gc = event.gc;

    gc.setAntialias(SWT.ON);
    gc.setLineWidth(1);
    Rectangle rect = getClientArea();
    gc.setClipping(rect);
    gc.setForeground(getForeground());
    gc.setBackground(getForeground());

    Color[] palette = new Color[32];
    for (int i = 0; i < palette.length; i++) {
      float hue = 270.0f * i / (palette.length - 1);
      palette[palette.length - 1 - i] = new Color(Display.getDefault(), new RGB(hue, 1.0f, 1.0f));
      //			gc.setBackground(palette[i]);
      //			gc.fillRectangle(rect.x + i*rect.width/palette.length, rect.y, rect.width/palette.length,
      // 100);
    }

    frame.adjust();

    int x = (int) (rect.x - frame.offsetX);
    int y = (int) (rect.y - frame.offsetY);
    int extent = (int) (Math.min(rect.width, rect.height) * frame.scale);
    treeMap.paint(x, y, extent, gc, curve, palette);

    for (Color color : palette) color.dispose();

    gc.setAlpha(255);
    gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_WHITE));
    gc.setLineStyle(SWT.LINE_DASHDOT);
    gc.setLineWidth(1);
    gc.drawRectangle(x - 1, y - 1, extent + 2, extent + 2);

    if (selection.size() > 0) {
      gc.setLineStyle(SWT.LINE_SOLID);
      gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_LIST_SELECTION));
      for (TreeMap subtree : selection) {
        gc.drawRectangle(getItemBounds(subtree));
      }
    }
  }
Esempio n. 15
0
  /** Display the treemap using g. */
  @Override
  public void paint(Graphics g) {
    super.paint(g);

    if (treemap != null) treemap.paint(g, width, height);
  }
 public List<int[]> getSkyline(int[][] buildings) {
   List<int[]> result = new ArrayList<>();
   if (buildings == null || buildings.length == 0) {
     return result;
   }
   Comparator<Point> comp =
       new Comparator<Point>() {
         @Override
         public int compare(Point a, Point b) {
           if (a.x != b.x) {
             return a.x - b.x;
           } else {
             return a.y - b.y;
           }
         }
       };
   PriorityQueue<Point> pq = new PriorityQueue<>(5, comp);
   for (int[] data : buildings) {
     pq.add(new Point(data[0], -data[2]));
     pq.add(new Point(data[1], data[2]));
   }
   TreeMap<Integer, Integer> map = new TreeMap<>();
   map.put(0, 1);
   int max = 0;
   while (!pq.isEmpty()) {
     Point cur = pq.poll();
     if (cur.y < 0) {
       if (!map.containsKey(-cur.y)) {
         map.put(-cur.y, 0);
       }
       map.put(-cur.y, map.get(-cur.y) + 1);
     } else {
       map.put(cur.y, map.get(cur.y) - 1);
       if (map.get(cur.y) == 0) {
         map.remove(cur.y);
       }
     }
     int curMax = map.lastEntry().getKey();
     if (curMax != max) {
       result.add(new int[] {cur.x, curMax});
       max = curMax;
     }
   }
   return result;
 }
Esempio n. 17
0
 public void add(IPv4Address address, IEntity entity) {
   treeMap.add(address, entity);
   redraw();
 }