⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chordid.java

📁 p2p仿真器。开发者可以工作在覆盖层中进行创造和测试逻辑算法或者创建和测试新的服务。PlanetSim还可以将仿真代码平稳转换为在Internet上的实验代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    switch (ch) {
      case '0':
        return 0;
      case '1':
        return 1;
      case '2':
        return 2;
      case '3':
        return 3;
      case '4':
        return 4;
      case '5':
        return 5;
      case '6':
        return 6;
      case '7':
        return 7;
      case '8':
        return 8;
      case '9':
        return 9;
      case 'a':
      case 'A':
        return 10;
      case 'b':
      case 'B':
        return 11;
      case 'c':
      case 'C':
        return 12;
      case 'd':
      case 'D':
        return 13;
      case 'e':
      case 'E':
        return 14;
      case 'f':
      case 'F':
        return 15;
      default:
        return -1;
    }
  }

  /**
   * Returns a string representation of the Id in base 16. The string is a byte string from most to
   * least significant.
   *
   * @return A String representation of this Id, abbreviated
   */
  public String toString() {
    return this.toStringFull();
  }

  /**
   * Gets the ith digit in base 2^b. i = 0 is the least significant digit.
   *
   * @param i which digit to get.
   * @param b which power of 2 is the base to get it in.
   * @return the ith digit in base 2^b.
   */
  public int getDigit(int i, int b) {
    int bitIndex = b * i + (bitsPerKey % b);
    int index = bitIndex / 32;
    int shift = bitIndex % 32;
    long val = Id[index];
    if (shift + b > 32) {
      val = (val & 0xffffffffL) | (((long) Id[index + 1]) << 32);
    }
    return ((int) (val >> shift)) & ((1 << b) - 1);
  }

  /** Shift operator. shift(-1,0) multiplies value of this by two, shift(1,0) divides by 2
   *
   * @param cnt the number of bits to shift, negative shifts left, positive shifts right
   * @param fill value of bit shifted in (0 if fill == 0, 1 otherwise)
   * @return this
   */
  public Id shift (int cnt, int fill) {
    return shift (cnt, fill, false);
  }


  /**
   * Shift operator. shift(-1,0) multiplies value of this by two, shift(1,0) divides by 2
   *
   * @param cnt the number of bits to shift, negative shifts left, positive shifts right
   * @param fill value of bit shifted in (0 if fill == 0, 1 otherwise)
   * @param roundUp if true, round up the results after right shifting
   * @return this
   */
  public Id shift (int cnt, int fill, boolean roundUp) {
    ChordId newId = new ChordId();
    newId.setValues(this.Id);
    int carry = 0;
    int bit = 0;
    int lsb = 0;

    if (cnt > 0) {
      for (int j = 0; j < cnt; j++) {
        // shift right one bit
        carry = (fill == 0) ? 0 : 0x80000000;
        for (int i = arrayElems - 1; i >= 0; i--) {
          bit = newId.Id[i] & 1;
          newId.Id[i] = (newId.Id[i] >>> 1) | carry;
          carry = (bit == 0) ? 0 : 0x80000000;
        }

        if (j == 0) {
          lsb = bit;
        }
      }

      if (roundUp && lsb > 0) {
        newId = (ChordId)inc();
      }
    } else {
      for (int j = 0; j < -cnt; j++) {
        // shift left one bit
        carry = (fill == 0) ? 0 : 1;
        for (int i = 0; i < arrayElems; i++) {
          bit = newId.Id[i] & 0x80000000;
          newId.Id[i] = (newId.Id[i] << 1) | carry;
          carry = (bit == 0) ? 0 : 1;
        }
      }
    }
    return newId;
  }

  private Id inc() {
    ChordId newId = new ChordId();
    long x;
    long sum;
    int carry = 1;

    // add one
    for (int i = 0; i < arrayElems; i++) {
      x = Id[i] & 0x0ffffffffL;
      sum = x + carry;
      if (sum >= 0x100000000L) {
        carry = 1;
      } else {
        carry = 0;
      }
      newId.Id[i] = (int) sum;
    }
    return newId;
  }

  public boolean between (Id ccw, Id cw) {
        if (ccw.equals(cw)) {
          return true;
        }
        else if (ccw.compareTo(cw) < 0) {
          return ((this.compareTo (ccw) > 0 ) && (this.compareTo (cw) < 0));
        }
        else {
          return ((this.compareTo (ccw) > 0) || (this.compareTo (cw) < 0));
        }
  }

  /**
   * Checks to see if the Id nid is clockwise or counterclockwise from this, on the ring. An Id is
   * clockwise if it is within the half circle clockwise from this on the ring. An Id is considered
   * counter-clockwise from itself.
   *
   * @param nid The Id we are comparing to
   * @return true if clockwise, false otherwise.
   */
  public boolean clockwise (Id nid) {
    ChordId nidId = (ChordId)nid;
    boolean diffMSB = ((Id[arrayElems - 1] & 0x80000000) != (nidId.Id[arrayElems - 1] & 0x80000000));
    int x;
    int y;
    int i;

    if ((x = (Id[arrayElems - 1] & 0x7fffffff)) != (y = (nidId.Id[arrayElems - 1] & 0x7fffffff))) {
      return ((y > x) ^ diffMSB);
    } else {
      for (i = arrayElems - 2; i >= 0; i--) {
        if (Id[i] != nidId.Id[i]) {
          break;
        }
      }

      if (i < 0) {
        return diffMSB;
      } else {
        long xl;
        long yl;

        xl = Id[i] & 0xffffffffL;
        yl = nidId.Id[i] & 0xffffffffL;

        return ((yl > xl) ^ diffMSB);
      }
    }
  }  
  
	/**
	 * Return the int[] with the value of this Id.
	 * @see planet.commonapi.Id#getValue()
	 * @return int[] with the value of this Id.
	 */
	public Object getValue() {
		return Id;
	}
	
	
	/**
	 * Sets the new value with the int[] that appears at <b>newValue</b>.
	 * @see planet.commonapi.Id#setValue(java.lang.Object)
	 * @param newValue New value as int[] representation.
	 */
	public void setValue(Object newValue) {
		translate((int[])newValue);
	}
	
	/**
	 * This method returns the maximum value for a ChordId that is 
     * possible to build, according the number of bits for key
     * actually in use. To return this maximum value use the
     * design pattern Singleton, reviewing the number of bits in use. 
     * @return The maximum value sets to an Id, always according
     * the number of bits actually in use.
	 * @see planet.commonapi.Id#getMaximum()
	 */
	public static Id getMaximum() {
		if (MAX == null || ((ChordProperties)Properties.overlayPropertiesInstance).bitsPerKey != bitsKey) {
			bitsKey = ((ChordProperties)Properties.overlayPropertiesInstance).bitsPerKey; //update values
            MAX = new ChordId();
            MAX.setValues(new BigInteger("2").pow(bitsKey)); 
		}
		return MAX;
	}
	
	/**
     * Divides the maximum domain of the node Id in <b>numberOfNodes</b>,
	 * to offers the offset between two consecutive nodes.
	 * @param numberOfNodes Number of nodes in the network. 
	 * @return The offset between two consecutive nodes, to get a network
	 * with equidistance nodes, as an object value.
     * @see planet.commonapi.Id#divide(int)
     */
    public static Id divide(int numberOfNodes) {
        BigInteger MAX = new BigInteger("2").pow(((ChordProperties)Properties.overlayPropertiesInstance).bitsPerKey);
        BigInteger div = new BigInteger(Integer.toString(numberOfNodes));
        ChordId toReturn = new ChordId();
        toReturn.setValues(MAX.divide(div));
        return toReturn;
    }
    
    /**
     * This method returns the arithmetical result of this division:
     * <br>
     * <center>thisId/<b>divisor</b></center>
     * <br>
     * @param divisor The number of parts to divide this Id.
     * @return The result of division.
     */
    public Id divideOn(int divisor)
    {
        BigInteger thisId = new BigInteger(this.toString());
        BigInteger div    = new BigInteger(Integer.toString(divisor)); 
        ChordId toReturn  = new ChordId();
        toReturn.setValues(thisId.divide(div));
        return toReturn;
    }
    
    
    /**
     * Sets the internal int[] value from the BigInteger <b>newValue</b>.
     * @param newValue The new value
     * @return The Id itself
     * @see planet.commonapi.Id#setValues(java.math.BigInteger)
     */
    public Id setValues(BigInteger newValue) {
        parseBigInteger(newValue);
        return this;
    }

    /**
     * Sets the internal int[] value from the byte[] <b>newValue</b>.
     * @param newValue The new value
     * @return The Id itself
     * @see planet.commonapi.Id#setValues(byte[])
     */
    public Id setValues(byte[] newValue) {
        translate(Utilities.toIntArray(newValue));
        return this;
    }

    /**
     * Always throws a NoSuchMethodError.
     * @param newValue The new value
     * @return The Id itself
     * @see planet.commonapi.Id#setValues(double)
     */
    public Id setValues(double newValue) {
        throw new NoSuchMethodError("No double value be applied to ChordId.");
    }
    /**
     * Sets the <b>newValue</b> to the first position of the internal int[],
     * and the rest of positions to zero.
     * @param newValue The new value.
     * @return The Id itself
     * @see planet.commonapi.Id#setValues(int)
     */
    public Id setValues(int newValue){
        if (Id == null)
            Id = new int[arrayElems];
        Id[0]= newValue;
        for (int i=1; i< arrayElems; i++) Id[i] = 0;
        return this;
    }
    
    /** 
     * Sets the internal value from the int[] <b>newValue</b>
     * @param newValue The new value
     * @return The Id itself
     * @see planet.commonapi.Id#setValues(int[])
     */
    public Id setValues(int[] newValue) {
        translate(newValue);
        return this;
    }
    
    /**
     * Uses the <b>valueGenerator</b> to radomly build a new value.
     * @param valueGenerator A Random number generator.
     * @return The Id itself
     * @see planet.commonapi.Id#setValues(java.util.Random)
     */
    public Id setValues(Random valueGenerator) {
        return setValues(new BigInteger(bitsPerKey,valueGenerator));
    }
    
    /**
     * Copies the string representation of a number to the internal int[].
     * @param newValue The new value.
     * @return The Id itself
     * @see planet.commonapi.Id#setValues(java.lang.String)
     */
    public Id setValues(String newValue) {
        parseBigInteger(new BigInteger(newValue));
        return this;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -