Example #1
0
 public static void main(String[] args) {
   Main obj = new Main();
   String text = "Hello world!";
   Range range = obj.encode(text);
   System.out.println(range.getLow() + " " + range.getHigh());
   System.out.println(obj.decode(text.length(), (range.getLow() + range.getHigh()) / 2));
 }
Example #2
0
 void encodeChar(Range rangeChar, Range range) {
   /*Range = high - low
   High = low + range *  high_range of the symbol being coded
           Low = low + range * low_range of the symbol being coded*/
   Double r = range.getHigh() - range.getLow();
   Double high = range.getLow() + r * rangeChar.getHigh();
   Double low = range.getLow() + r * rangeChar.getLow();
   range.setHigh(high);
   range.setLow(low);
 }
Example #3
0
 String decode(Integer textLength, Double number) {
   String str = "";
   NavigableMap<Double, Character> sortedMap = new TreeMap<>();
   rangeTable.forEach((k, v) -> sortedMap.put(v.getLow(), k));
   for (int i = 0; i < textLength; i++) {
     Character value = sortedMap.floorEntry(number).getValue();
     str = str.concat(String.valueOf(value));
     Range range = rangeTable.get(value);
     number = (number - range.getLow()) / (range.getHigh() - range.getLow());
   }
   return str;
 }
 public int[] getDefinedInterval(int key) {
   RangeHolder holder = new RangeHolder(key, key);
   int pos = VectorUtil.binarySearch(ranges, holder, rangeComparator, true) - 1;
   if (pos < 0 || pos >= ranges.size()) return null;
   Range r = (Range) ranges.elementAt(pos);
   if (r.getLow() <= key && r.getHigh() >= key) {
     int[] out = new int[2];
     out[0] = r.getLow();
     out[1] = r.getHigh();
     return out;
   } else return null;
 }
  public double[] getInterval(int key) {
    double[] output = new double[2];
    RangeHolder holder = new RangeHolder(key, key + 1);
    int pos = VectorUtil.binarySearch(ranges, holder, rangeComparator, true) - 1;
    // System.err.println("ranges = "+ranges);
    if (ranges.size() == 0) {
      output[0] = Double.NEGATIVE_INFINITY;
      output[1] = Double.POSITIVE_INFINITY;
      return output;
    }

    if (pos < 0) {
      output[0] = Double.NEGATIVE_INFINITY;
      output[1] = (double) ((Range) ranges.elementAt(0)).getLow() - 1;
      return output;
    }

    if (pos >= ranges.size()) {
      output[0] = (double) ((Range) ranges.elementAt(ranges.size() - 1)).getHigh() + 1;
      output[1] = Double.POSITIVE_INFINITY;
      return output;
    }

    Range rh = (Range) ranges.elementAt(pos);
    if (contains(rh, key)) {
      output[0] = (double) rh.getLow();
      output[1] = (double) rh.getHigh();
    } else {
      output[0] = (double) rh.getHigh() + 1;
      if (pos < ranges.size() - 1) output[1] = ((Range) ranges.elementAt(pos + 1)).getLow() - 1;
      else output[1] = Double.POSITIVE_INFINITY;
    }

    return output;
  }
 public String toString() {
   StringBuffer out = new StringBuffer("(");
   for (int i = 0; i < ranges.size(); i++) {
     Range r = (Range) ranges.elementAt(i);
     Object o = data.get(r);
     if (i > 0) out.append(", ");
     out.append("{[" + r.getLow() + ", " + r.getHigh() + "] = " + o.toString() + "}");
   }
   out.append(")");
   return out.toString();
 }
 protected static boolean overlaps(Range a, Range b) {
   return (contains(a, b.getLow())
       || contains(a, b.getHigh())
       || contains(b, a.getLow())
       || contains(b, a.getHigh()));
 }
 protected static boolean contains(Range r, int value) {
   return value >= r.getLow() && value <= r.getHigh();
 }
 protected static int compare(Range a, Range b) {
   if (a.getLow() < b.getLow()) return -1;
   else if (a.getLow() > b.getLow()) return 1;
   else return 0;
 }
 protected static boolean equals(Range a, Range b) {
   return a.getLow() == b.getLow() && a.getHigh() == b.getHigh();
 }