001: // bitfield.java
002: // -------------------------------------
003: // (C) by Michael Peter Christen; mc@anomic.de
004: // first published on http://www.anomic.de
005: // Frankfurt, Germany, 2004
006: // last major change: 11.08.2004
007: //
008: // This program is free software; you can redistribute it and/or modify
009: // it under the terms of the GNU General Public License as published by
010: // the Free Software Foundation; either version 2 of the License, or
011: // (at your option) any later version.
012: //
013: // This program is distributed in the hope that it will be useful,
014: // but WITHOUT ANY WARRANTY; without even the implied warranty of
015: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: // GNU General Public License for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // along with this program; if not, write to the Free Software
020: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: //
022: // Using this software in any meaning (reading, learning, copying, compiling,
023: // running) means that you agree that the Author(s) is (are) not responsible
024: // for cost, loss of data or any harm that may be caused directly or indirectly
025: // by usage of this softare or this documentation. The usage of this software
026: // is on your own risk. The installation and usage (starting/running) of this
027: // software may allow other people or application to access your computer and
028: // any attached devices and is highly dependent on the configuration of the
029: // software which must be done by the user of the software; the author(s) is
030: // (are) also not responsible for proper configuration and usage of the
031: // software, even if provoked by documentation provided together with
032: // the software.
033: //
034: // Any changes to this file according to the GPL as documented in the file
035: // gpl.txt aside this file in the shipment you received can be done to the
036: // lines that follows this copyright notice here, but changes must not be
037: // done inside the copyright notive above. A re-distribution must contain
038: // the intact and unchanged copyright notice.
039: // Contributions and changes to the program code must be marked as such.
040:
041: package de.anomic.tools;
042:
043: public class bitfield {
044:
045: private byte[] bb;
046:
047: public bitfield() {
048: this (0);
049: }
050:
051: public bitfield(int bytelength) {
052: this .bb = new byte[bytelength];
053: for (int i = 0; i < bytelength; i++)
054: bb[i] = 0;
055: }
056:
057: public bitfield(byte[] field) {
058: bb = field;
059: }
060:
061: private static byte setAtom(byte a, int pos) {
062: if ((pos > 5) || (pos < 0))
063: throw new RuntimeException("atom position out of bounds: "
064: + pos);
065: return (byte) ((64 | ((a + 16) | (1 << pos))) - 16);
066: }
067:
068: private static byte unsetAtom(byte a, int pos) {
069: if ((pos > 5) || (pos < 0))
070: throw new RuntimeException("atom position out of bounds: "
071: + pos);
072: return (byte) (((a + 16) & (0xff ^ (1 << pos))) - 16);
073: }
074:
075: public void set(int pos, boolean value) {
076: int slot = pos / 6;
077: if (pos < 0)
078: throw new RuntimeException("position out of bounds: " + pos);
079: if (slot > bb.length) {
080: // extend capacity
081: byte[] nb = new byte[slot + 1];
082: System.arraycopy(bb, 0, nb, 0, bb.length);
083: for (int i = bb.length; i < nb.length; i++)
084: nb[i] = 0;
085: bb = nb;
086: nb = null;
087: }
088: bb[slot] = (value) ? setAtom(bb[slot], pos % 6) : unsetAtom(
089: bb[slot], pos % 6);
090: }
091:
092: public boolean get(int pos) {
093: int slot = pos / 6;
094: if (pos < 0)
095: throw new RuntimeException("position out of bounds: " + pos);
096: if (slot > bb.length)
097: return false;
098: return (bb[slot] & (1 << (pos % 6))) > 0;
099: }
100:
101: public int length() {
102: return bb.length * 6;
103: }
104:
105: public byte[] getBytes() {
106: return bb;
107: }
108:
109: public String toString() {
110: throw new UnsupportedOperationException("testing");
111: /*
112: StringBuffer sb = new StringBuffer(length());
113: for (int i = length() - 1; i >= 0; i--) sb.append((get(i)) ? '1' : '0');
114: return sb.toString();
115: */
116: }
117:
118: public static void main(String[] args) {
119: bitfield test = new bitfield(4);
120: int l = test.length();
121: System.out.println("available: " + l);
122: System.out.println("before: " + test.toString());
123: for (int i = 0; i < l / 2; i++) {
124: test.set(i, true);
125: System.out.println(i + ":" + test.toString());
126: }
127: for (int i = l / 2 - 1; i >= 0; i--) {
128: test.set(i, false);
129: System.out.println(i + ":" + test.toString());
130: }
131: System.out.println("after: " + test.toString());
132: }
133:
134: }
|