|
|
@ -24,11 +24,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
package net.yacy.kelondro.order;
|
|
|
|
package net.yacy.kelondro.order;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
|
|
|
|
|
|
|
|
public class Bitfield implements Cloneable {
|
|
|
|
|
|
|
|
|
|
|
|
public class Bitfield implements Cloneable, Serializable {
|
|
|
|
|
|
|
|
|
|
|
|
// the bitfield implements a binary array. Such arrays may be exported in a base64-String
|
|
|
|
// the bitfield implements a binary array. Such arrays may be exported in a base64-String
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final long serialVersionUID=3605122793792478052L;
|
|
|
|
private byte[] bb;
|
|
|
|
private byte[] bb;
|
|
|
|
|
|
|
|
|
|
|
|
public Bitfield() {
|
|
|
|
public Bitfield() {
|
|
|
@ -41,18 +44,18 @@ public class Bitfield implements Cloneable {
|
|
|
|
|
|
|
|
|
|
|
|
public Bitfield(final int bytelength) {
|
|
|
|
public Bitfield(final int bytelength) {
|
|
|
|
this.bb= new byte[bytelength];
|
|
|
|
this.bb= new byte[bytelength];
|
|
|
|
for (int i = 0 ; i < bytelength; i++) bb[i] = 0;
|
|
|
|
for (int i = 0 ; i < bytelength; i++) this.bb[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Bitfield(final int bytelength, final String exported) {
|
|
|
|
public Bitfield(final int bytelength, final String exported) {
|
|
|
|
// imports a b64-encoded bitfield
|
|
|
|
// imports a b64-encoded bitfield
|
|
|
|
final byte[] b = Base64Order.enhancedCoder.decode(exported);
|
|
|
|
final byte[] b = Base64Order.enhancedCoder.decode(exported);
|
|
|
|
if (b.length == bytelength) {
|
|
|
|
if (b.length == bytelength) {
|
|
|
|
bb = b;
|
|
|
|
this.bb = b;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
bb = new byte[bytelength];
|
|
|
|
this.bb = new byte[bytelength];
|
|
|
|
assert (b.length <= bytelength) : "exported = " + exported + " has bytelength = " + b.length + " > " + bytelength;
|
|
|
|
assert (b.length <= bytelength) : "exported = " + exported + " has bytelength = " + b.length + " > " + bytelength;
|
|
|
|
System.arraycopy(b, 0, bb, 0, Math.min(b.length, bytelength));
|
|
|
|
System.arraycopy(b, 0, this.bb, 0, Math.min(b.length, bytelength));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -66,37 +69,37 @@ public class Bitfield implements Cloneable {
|
|
|
|
public void set(final int pos, final boolean value) {
|
|
|
|
public void set(final int pos, final boolean value) {
|
|
|
|
assert (pos >= 0);
|
|
|
|
assert (pos >= 0);
|
|
|
|
final int slot = pos >> 3; // /8
|
|
|
|
final int slot = pos >> 3; // /8
|
|
|
|
if (slot >= bb.length) {
|
|
|
|
if (slot >= this.bb.length) {
|
|
|
|
// extend capacity
|
|
|
|
// extend capacity
|
|
|
|
byte[] nb = new byte[slot + 1];
|
|
|
|
byte[] nb = new byte[slot + 1];
|
|
|
|
System.arraycopy(bb, 0, nb, 0, bb.length);
|
|
|
|
System.arraycopy(this.bb, 0, nb, 0, this.bb.length);
|
|
|
|
for (int i = bb.length; i < nb.length; i++) nb[i] = 0;
|
|
|
|
for (int i = this.bb.length; i < nb.length; i++) nb[i] = 0;
|
|
|
|
bb = nb;
|
|
|
|
this.bb = nb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (value) {
|
|
|
|
if (value) {
|
|
|
|
bb[slot] = (byte) (bb[slot] | (1 << (pos % 8)));
|
|
|
|
this.bb[slot] = (byte) (this.bb[slot] | (1 << (pos % 8)));
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
bb[slot] = (byte) (bb[slot] & (0xff ^ (1 << (pos % 8))));
|
|
|
|
this.bb[slot] = (byte) (this.bb[slot] & (0xff ^ (1 << (pos % 8))));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean get(final int pos) {
|
|
|
|
public boolean get(final int pos) {
|
|
|
|
assert (pos >= 0);
|
|
|
|
assert (pos >= 0);
|
|
|
|
final int slot = pos >> 3; // /8
|
|
|
|
final int slot = pos >> 3; // /8
|
|
|
|
if (slot >= bb.length) return false;
|
|
|
|
if (slot >= this.bb.length) return false;
|
|
|
|
return (bb[slot] & (1 << (pos % 8))) > 0;
|
|
|
|
return (this.bb[slot] & (1 << (pos % 8))) > 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int length() {
|
|
|
|
public int length() {
|
|
|
|
return bb.length << 3;
|
|
|
|
return this.bb.length << 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String exportB64() {
|
|
|
|
public String exportB64() {
|
|
|
|
return Base64Order.enhancedCoder.encode(bb);
|
|
|
|
return Base64Order.enhancedCoder.encode(this.bb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public byte[] bytes() {
|
|
|
|
public byte[] bytes() {
|
|
|
|
return bb;
|
|
|
|
return this.bb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|