1

I understand this questions is strange, not my usual style. I am currently doing a project using a chord implementation in peersim. The code below shows a big integer and some operation being carried out on it. My chordId is a hash of an object, why would you want to use add()? what purpose does this serve?

BigInteger base;
if (j == 0)
    base = BigInteger.ONE;
else {
    base = BigInteger.valueOf(2);
    for (int exp = 1; exp < j; exp++) {
        base = base.multiply(BigInteger.valueOf(2));
    }
}
BigInteger pot = cp.chordId.add(base);

Prior to his the chord Id was just a random integer of the idlength which was 128 bit.

My question therefore is what is the piece of code above add() used for???

[EDIT]

To put it into a bit of perspective to make this question clearer:

cp.fingerTable[j] = findId(pot, 0, Network.size() - 1);

is called which attempts to find the Id of Pot however it is always returning in error because the chordId created in this method does not exist. I am unsure of what to replace pot with or whether to take it out completely.

[EDIT2]

findId looks like this (this is not my code hence my confusion :))

public Node findId(BigInteger id, int nodeOne, int nodeTwo) {
    if (nodeOne >= (nodeTwo - 1))
        return Network.get(nodeOne);
    int middle = (nodeOne + nodeTwo) / 2;
    if (((middle) >= Network.size() - 1))
        System.out.print("ERROR: Middle is bigger than Network.size");
    if (((middle) <= 0))
        return Network.get(0);
    try {
        BigInteger newId = ((ChordProtocol) ((Node) Network.get(middle))
                .getProtocol(pid)).chordId;
        BigInteger lowId;
        if (middle > 0)
            lowId = ((ChordProtocol) ((Node) Network.get(middle - 1))
                    .getProtocol(pid)).chordId;
        else
            lowId = newId;
        BigInteger highId = ((ChordProtocol) ((Node) Network
                .get(middle + 1)).getProtocol(pid)).chordId;
        if (id.compareTo(newId) == 0
                || ((id.compareTo(newId) == 1) && (id.compareTo(highId) == -1))) {
            return Network.get(middle);
        }
        if ((id.compareTo(newId) == -1) && (id.compareTo(lowId) == 1)) {
            if (middle > 0)
                return Network.get(middle - 1);
            else
                return Network.get(0);
        }
        if (id.compareTo(newId) == -1) {
            return findId(id, nodeOne, middle);
        } else if (id.compareTo(newId) == 1) {
            return findId(id, middle, nodeTwo);
        }
        return null;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
4

4 に答える 4

5

baseの代わりに int の場合、コードは次のようになりますBigInteger

int base;
if (j == 0)
    base = 1;
else {
    base = 2;
    for (int exp = 1; exp < j; exp++) {
        base = base * 2;
    }
}
int pot = cp.chordId + base;

実際には、そのコード スニペット全体を次のように置き換えることができます。

BigInteger base = BigInteger.valueOf(2).pow(j);
BigInteger pot = cp.chordId.add(base);

つまり、次と同等です。

int base = (int) Math.pow(2, j);
int pot = cp.chordId + base;

基本的に 2 jを追加しますcp.chordId

于 2011-11-23T21:05:40.157 に答える
1

2 ** j を計算し、それを cp.chordId に追加しています。if は、実際には必要ない s ** 0 = 1 の特殊なケース用です。

于 2011-11-23T21:09:00.267 に答える
1

上記の add() のコードは何に使用されますか?

cp.chordIdは 型BigIntegerで、渡されadd()たオブジェクトにオブジェクトを追加するために使用され、との両方を追加した値を持つnew を返します。cp.chordIdbaseBigIntegerBigIntegercp.chordIdbase

于 2011-11-23T21:09:07.860 に答える
1
cp.chordId.add(base);

cp.chordIdの値に応じてベース値を与えようとしているように見えますj

したがって、jが 0 の場合、ベース値はchordidになります。そうでない場合BigInteger.ONE、ベースはchorid("2 ^ j"つまり 2 j ) になります。

于 2011-11-23T21:12:58.410 に答える