📄 id.java
字号:
/** * Stores the byte[] value of this Id in the provided byte array * * @param array DESCRIBE THE PARAMETER * @param offset DESCRIBE THE PARAMETER */ public void toByteArray(byte[] array, int offset) { blit(array, offset); } /** * Hash codes for Ids. * * @return a hash code. */ public int hashCode() { int h = 0; /// Hash function is computed by XORing the bits of the Id. for (int i = 0; i < nlen; i++) { h ^= Id[i]; } return h; } /** * Returns an Id corresponding to this Id plus a given distance * * @param offset the distance to add * @return the new Id */ public Id add(Distance offset) { int[] array = new int[nlen]; long x; long y; long sum; int carry = 0; for (int i = 0; i < nlen; i++) { x = Id[i] & 0x0ffffffffL; y = offset.difference[i] & 0x0ffffffffL; sum = x + y + carry; if (sum >= 0x100000000L) { carry = 1; } else { carry = 0; } array[i] = (int) sum; } return build(array); } /** * Returns the shorter numerical distance on the ring between a pair of Ids. * * @param nid the other node id. * @return the distance between this and nid. */ public Distance distance(Id nid) { int[] dist = absDistance(nid); if ((dist[nlen - 1] & 0x80000000) != 0) { invert(dist); } Distance d = new Distance(dist); return d; } /** * DESCRIBE THE METHOD * * @param nid DESCRIBE THE PARAMETER * @param d DESCRIBE THE PARAMETER * @return DESCRIBE THE RETURN VALUE */ public Distance distance(Id nid, Distance d) { int[] dist = d.difference; absDistance(nid, dist); if ((dist[nlen - 1] & 0x80000000) != 0) { invert(dist); }// Distance d = new Distance(dist); return d; } /** * Returns the longer numerical distance on the ring between a pair of Ids. * * @param nid the other node id. * @return the distance between this and nid. */ public Distance longDistance(Id nid) { int[] dist = absDistance(nid); if ((dist[nlen - 1] & 0x80000000) == 0) { invert(dist); } Distance d = new Distance(dist); return d; } /** * Xor operator for Ids. Sets this Id to the bit-wise XOR of itself and * otherId * * @param nid DESCRIBE THE PARAMETER * @return DESCRIBE THE RETURN VALUE */// public void xor(Id otherId) {// for (int i = 0; i < nlen; i++) {// Id[i] ^= otherId.Id[i];// }// } /** * Equivalence relation for Ids. * * @param nid the other node id. * @return true if they are equal, false otherwise. */ public boolean equals(Id nid) { if (nid == null) { return false; } for (int i = 0; i < nlen; i++) { if (Id[i] != nid.Id[i]) { return false; } } return true; } /** * 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) { boolean diffMSB = ((Id[nlen - 1] & 0x80000000) != (nid.Id[nlen - 1] & 0x80000000)); int x; int y; int i; if ((x = (Id[nlen - 1] & 0x7fffffff)) != (y = (nid.Id[nlen - 1] & 0x7fffffff))) { return ((y > x) ^ diffMSB); } else { for (i = nlen - 2; i >= 0; i--) { if (Id[i] != nid.Id[i]) { break; } } if (i < 0) { return diffMSB; } else { long xl; long yl; xl = Id[i] & 0xffffffffL; yl = nid.Id[i] & 0xffffffffL; return ((yl > xl) ^ diffMSB); } } } /** * Checks if the ith bit is flipped. i = 0 is the least significant bit. * * @param i which bit to check. * @return true if the bit is set, false otherwise. */ public boolean checkBit(int i) { int index = i / 32; int shift = i % 32; int val = Id[index]; int mask = (1 << shift); if ((val & mask) != 0) { return true; } else { return false; } } /** * Returns the index of the most significant differing bit (MSDB). * * @param nid another node id to compare with. * @return the index of the msdb (0 is the least significant) / will return * negative if they do not differ. */ public int indexOfMSDB(Id nid) { for (int i = nlen - 1; i >= 0; i--) { int cmp = Id[i] ^ nid.Id[i]; if (cmp != 0) { int tmp; int j = 0; if ((tmp = cmp & 0xffff0000) != 0) { cmp = tmp; j += 16; } if ((tmp = cmp & 0xff00ff00) != 0) { cmp = tmp; j += 8; } if ((tmp = cmp & 0xf0f0f0f0) != 0) { cmp = tmp; j += 4; } if ((tmp = cmp & 0xcccccccc) != 0) { cmp = tmp; j += 2; } if ((tmp = cmp & 0xaaaaaaaa) != 0) { cmp = tmp; j += 1; } return 32 * i + j; } } return -1; } /** * Returns the index of the most significant different digit (MSDD) in a given * base. * * @param nid another node id to compare with. * @param base the base (as a power of two) to compare in. * @return the index of the msdd (0 is the least significant) / will return * negative if they do not differ. */ public int indexOfMSDD(Id nid, int base) { int ind = indexOfMSDB(nid); // ignore trailing LSBs if (IdBitLength % base) > 0 ind -= IdBitLength % base; if (ind < 0) { return ind; } return ind / base; } /** * Returns a string representation of the Id in base 16. The string is a byte * string from most to least significant. This was updated by Jeff because it * was becomming a performance bottleneck. * * @return A String representation of this Id, abbreviated */ public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append("<0x"); int n = IdBitLength / 4; for (int i = n - 1; i >= n - 6; i--) { buffer.append(tran[getDigit(i, 4)]); } buffer.append("..>"); return buffer.toString(); } /** * Similar to toString(), but not wrapped by <0x ..> * * @return */ public String toStringBare() { StringBuffer buffer = new StringBuffer(); int n = IdBitLength / 4; for (int i = n - 1; i >= n - 6; i--) { buffer.append(tran[getDigit(i, 4)]); } return buffer.toString(); } /** * Returns the complete represntation of this Id, in hex. * * @return The complete representation of this Id, in hexadecimal */ public String toStringFull() { StringBuffer buffer = new StringBuffer(); int n = IdBitLength / 4; for (int i = n - 1; i >= 0; i--) { buffer.append(tran[getDigit(i, 4)]); } return buffer.toString(); } /** * 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 DESCRIBE THE PARAMETER * @return true if clockwise, false otherwise. */ public boolean clockwise(rice.p2p.commonapi.Id nid) { return clockwise((Id) nid); } /** * Returns an Id corresponding to this Id plus a given distance * * @param offset the distance to add * @return the new Id */ public rice.p2p.commonapi.Id addToId(rice.p2p.commonapi.Id.Distance offset) { return add((Id.Distance) offset); } /** * Returns the shorter numerical distance on the ring between a pair of Ids. * * @param nid the other node id. * @return the distance between this and nid. */ public rice.p2p.commonapi.Id.Distance distanceFromId(rice.p2p.commonapi.Id nid) { return distance((Id) nid); } /** * Returns the longer numerical distance on the ring between a pair of Ids. * * @param nid the other node id. * @return the distance between this and nid. */ public rice.p2p.commonapi.Id.Distance longDistanceFromId(rice.p2p.commonapi.Id nid) { return longDistance((Id) nid); } /** * Returns the absolute numerical distance between a pair of Ids. * * @param nid the other node id. * @return an int[] containing the distance between this and nid. */ private int[] absDistance(Id nid) { return absDistance(nid, new int[nlen]); } /** * DESCRIBE THE METHOD * * @param nid DESCRIBE THE PARAMETER * @param dist DESCRIBE THE PARAMETER * @return DESCRIBE THE RETURN VALUE */ private int[] absDistance(Id nid, int dist[]) { long x; long y; long diff; int carry = 0; if (compareTo(nid) > 0) { for (int i = 0; i < nlen; i++) { x = Id[i] & 0x0ffffffffL; y = nid.Id[i] & 0x0ffffffffL; diff = x - y - carry; if (diff < 0) { carry = 1; } else { carry = 0; } dist[i] = (int) diff; } } else { for (int i = 0; i < nlen; i++) { x = Id[i] & 0x0ffffffffL; y = nid.Id[i] & 0x0ffffffffL; diff = y - x - carry; if (diff < 0) { carry = 1; } else { carry = 0; } dist[i] = (int) diff; } } return dist; } /** * inverts the distance value stored in an integer array (computes 0-value) * * @param dist the distance value */ private void invert(int[] dist) { int carry = 0; long diff; for (int i = 0; i < nlen; i++) { diff = dist[i] & 0x0ffffffffL; diff = 0L - diff - carry; if (diff < 0) { carry = 1; } dist[i] = (int) diff; } } /** * return the number of digits in a given base * * @param base the number of bits in the base
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -