yacy_search_server/source/de/anomic/kelondro/kelondroBitfield.java
danielr 621b473b18 * removed some warnings of findbugs (http://findbugs.sf.net)
- removed unnecessary code (unused variables, String.toString)
- corrected some calculations (cast int to double or long ;)
- improved little performance (using Integer.valueOf() instead of new Integer)
- log if some File-actions fail (mkdir(), delete(), ...) and some ignored exceptions
- finalized some (more) fields
- finally close some streams
- made inner classes static if not using environment
- generalized some equals (from specificClass to Object)
- fixed some potential nullpointer accesses


git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@5039 6c8d7289-2bf4-0310-a012-ef5d649a1542
2008-08-06 19:43:12 +00:00

172 lines
6.0 KiB
Java

// kelondroBitfield.java
// (C) 2006 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
// first published 22.22.2006 on http://www.anomic.de
//
// $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
// $LastChangedRevision: 1986 $
// $LastChangedBy: orbiter $
//
// LICENSE
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package de.anomic.kelondro;
public class kelondroBitfield implements Cloneable {
// the bitfield implements a binary array. Such arrays may be exported in a base64-String
private byte[] bb;
public kelondroBitfield() {
this(0);
}
public kelondroBitfield(final byte[] b) {
if (b == null) this.bb = new byte[0]; else this.bb = b;
}
public kelondroBitfield(final int bytelength) {
this.bb= new byte[bytelength];
for (int i = 0 ; i < bytelength; i++) bb[i] = 0;
}
public kelondroBitfield(final int bytelength, final String exported) {
// imports a b64-encoded bitfield
final byte[] b = kelondroBase64Order.enhancedCoder.decode(exported, "de.anomic.kelondro.kelondroBitfield.kelondroBitfield(...)");
if (b.length == bytelength) {
bb = b;
} else {
bb = new byte[bytelength];
assert (b.length <= bytelength) : "exported = " + exported + " has bytelength = " + b.length + " > " + bytelength;
System.arraycopy(b, 0, bb, 0, Math.min(b.length, bytelength));
}
}
public kelondroBitfield clone() {
final kelondroBitfield theClone = new kelondroBitfield(new byte[this.bb.length]);
System.arraycopy(this.bb, 0, theClone.bb, 0, this.bb.length);
return theClone;
}
public void set(final int pos, final boolean value) {
assert (pos >= 0);
final int slot = pos >> 3; // /8
if (slot >= bb.length) {
// extend capacity
byte[] nb = new byte[slot + 1];
System.arraycopy(bb, 0, nb, 0, bb.length);
for (int i = bb.length; i < nb.length; i++) nb[i] = 0;
bb = nb;
}
if (value) {
bb[slot] = (byte) (bb[slot] | (1 << (pos % 8)));
} else {
bb[slot] = (byte) (bb[slot] & (0xff ^ (1 << (pos % 8))));
}
}
public boolean get(final int pos) {
assert (pos >= 0);
final int slot = pos >> 3; // /8
if (slot > bb.length) return false;
return (bb[slot] & (1 << (pos % 8))) > 0;
}
public int length() {
return bb.length << 3;
}
public String exportB64() {
return kelondroBase64Order.enhancedCoder.encode(bb);
}
public byte[] bytes() {
return bb;
}
public String toString() {
final StringBuffer sb = new StringBuffer(length());
for (int i = length() - 1; i >= 0; i--) sb.append((this.get(i)) ? '1' : '0');
return new String(sb);
}
public boolean equals(final kelondroBitfield x) {
if (x.bb.length != bb.length) return false;
for (int i = 0; i < bb.length; i++) if (bb[i] != x.bb[i]) return false;
return true;
}
public void and(final kelondroBitfield x) {
final int c = Math.min(x.length(), this.length());
for (int i = 0; i < c; i++) set(i, this.get(i) && x.get(i));
}
public void or(final kelondroBitfield x) {
final int c = Math.min(x.length(), this.length());
for (int i = 0; i < c; i++) set(i, this.get(i) || x.get(i));
if (x.length() > c) {
for (int i = c; i < x.length(); i++) set(i, x.get(i));
}
}
public void xor(final kelondroBitfield x) {
final int c = Math.min(x.length(), this.length());
for (int i = 0; i < c; i++) set(i, this.get(i) != x.get(i));
if (x.length() > c) {
for (int i = c; i < x.length(); i++) set(i, x.get(i));
}
}
public boolean anyOf(final kelondroBitfield x) {
final int c = Math.min(x.length(), this.length());
for (int i = 0; i < c; i++) if ((x.get(i)) && (this.get(i))) return true;
return false;
}
public boolean allOf(final kelondroBitfield x) {
final int c = Math.min(x.length(), this.length());
for (int i = 0; i < c; i++) if ((x.get(i)) && (!(this.get(i)))) return false;
if (x.length() > c) {
for (int i = c; i < x.length(); i++) if (x.get(i)) return false;
}
return true;
}
public static void main(final String[] args) {
kelondroBitfield test = new kelondroBitfield(4);
final int l = test.length();
System.out.println("available: " + l);
System.out.println("bevore: " + test.toString());
for (int i = 0; i < l/2; i++) {
System.out.println(test.exportB64());
test.set(i, true);
System.out.println(i + ":" + test.toString());
}
for (int i = l/2; i < l; i++) {
System.out.println(test.exportB64());
test = new kelondroBitfield(4, test.exportB64());
test.set(i, true);
System.out.println(i + ":" + test.toString());
}
System.out.println(test.exportB64());
for (int i = l - 1; i >= 0; i--) {
test.set(i, false);
System.out.println(i + ":" + test.toString());
}
System.out.println("after: " + test.toString());
}
}