I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
Commit cf4a9ffc authored by zzz's avatar zzz Committed by zzz
Browse files

(zzz) i2psnark: Put bit counting in Bitfield.java for efficiency

parent 6ef4adf3
No related branches found
No related tags found
No related merge requests found
......@@ -32,6 +32,7 @@ public class BitField
private final byte[] bitfield;
private final int size;
private int count;
/**
* Creates a new BitField that represents <code>size</code> unset bits.
......@@ -41,6 +42,7 @@ public class BitField
this.size = size;
int arraysize = ((size-1)/8)+1;
bitfield = new byte[arraysize];
this.count = 0;
}
/**
......@@ -60,6 +62,11 @@ public class BitField
// XXX - More correct would be to check that unused bits are
// cleared or clear them explicitly ourselves.
System.arraycopy(bitfield, 0, this.bitfield, 0, arraysize);
this.count = 0;
for (int i = 0; i < size; i++)
if (get(i))
this.count++;
}
/**
......@@ -95,7 +102,10 @@ public class BitField
throw new IndexOutOfBoundsException(Integer.toString(bit));
int index = bit/8;
int mask = 128 >> (bit % 8);
bitfield[index] |= mask;
if ((bitfield[index] & mask) == 0) {
count++;
bitfield[index] |= mask;
}
}
/**
......@@ -114,6 +124,22 @@ public class BitField
return (bitfield[index] & mask) != 0;
}
/**
* Return the number of set bits.
*/
public int count()
{
return count;
}
/**
* Return true if all bits are set.
*/
public boolean complete()
{
return count >= size;
}
public String toString()
{
// Not very efficient
......@@ -129,4 +155,5 @@ public class BitField
return sb.toString();
}
}
......@@ -54,8 +54,8 @@ public class Peer implements Comparable
private boolean deregister = true;
private static long __id;
private long _id;
final static long CHECK_PERIOD = 40*1000; // 40 seconds
final static int RATE_DEPTH = 6; // make following arrays RATE_DEPTH long
final static long CHECK_PERIOD = PeerCoordinator.CHECK_PERIOD; // 40 seconds
final static int RATE_DEPTH = PeerCoordinator.RATE_DEPTH; // make following arrays RATE_DEPTH long
private long uploaded_old[] = {-1,-1,-1,-1,-1,-1};
private long downloaded_old[] = {-1,-1,-1,-1,-1,-1};
......@@ -493,33 +493,24 @@ public class Peer implements Comparable
/**
* Return how much the peer has
* Quite inefficient - a byte lookup table or counter in Bitfield would be much better
*/
public int completed()
{
PeerState s = state;
if (s == null || s.bitfield == null)
return 0;
int count = 0;
for (int i = 0; i < s.bitfield.size(); i++)
if (s.bitfield.get(i))
count++;
return count;
return s.bitfield.count();
}
/**
* Return if a peer is a seeder
* Quite inefficient - a byte lookup table or counter in Bitfield would be much better
*/
public boolean isCompleted()
{
PeerState s = state;
if (s == null || s.bitfield == null)
return false;
for (int i = 0; i < s.bitfield.size(); i++)
if (!s.bitfield.get(i))
return false;
return true;
return s.bitfield.complete();
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment