001: // Copyright (c) 2001 Per M.A. Bothner and Brainfood Inc.
002: // This is free software; for terms and warranty disclaimer see ./COPYING.
003:
004: package gnu.lists;
005:
006: import java.io.*;
007:
008: /** Simple adjustable-length vector of boolean values. */
009:
010: public class BitVector extends SimpleVector implements Externalizable {
011: boolean[] data;
012: protected static boolean[] empty = new boolean[0];
013:
014: public BitVector() {
015: data = empty;
016: }
017:
018: public BitVector(int size, boolean value) {
019: boolean[] array = new boolean[size];
020: data = array;
021: this .size = size;
022: if (value) {
023: while (--size >= 0)
024: array[size] = true;
025: }
026: }
027:
028: public BitVector(int size) {
029: this .data = new boolean[size];
030: this .size = size;
031: }
032:
033: public BitVector(boolean[] data) {
034: this .data = data;
035: size = data.length;
036: }
037:
038: public BitVector(Sequence seq) {
039: data = new boolean[seq.size()];
040: addAll(seq);
041: }
042:
043: /** Get the allocated length of the data buffer. */
044: public int getBufferLength() {
045: return data.length;
046: }
047:
048: public void setBufferLength(int length) {
049: int oldLength = data.length;
050: if (oldLength != length) {
051: boolean[] tmp = new boolean[length];
052: System.arraycopy(data, 0, tmp, 0,
053: oldLength < length ? oldLength : length);
054: data = tmp;
055: }
056: }
057:
058: protected Object getBuffer() {
059: return data;
060: }
061:
062: public final boolean booleanAt(int index) {
063: if (index > size)
064: throw new IndexOutOfBoundsException();
065: return data[index];
066: }
067:
068: public final boolean booleanAtBuffer(int index) {
069: return data[index];
070: }
071:
072: public final Object get(int index) {
073: if (index > size)
074: throw new IndexOutOfBoundsException();
075: return Convert.toObject(data[index]);
076: }
077:
078: public final Object getBuffer(int index) {
079: return Convert.toObject(data[index]);
080: }
081:
082: public Object setBuffer(int index, Object value) {
083: boolean old = data[index];
084: data[index] = Convert.toBoolean(value);
085: return Convert.toObject(old);
086: }
087:
088: public final void setBooleanAt(int index, boolean value) {
089: if (index > size)
090: throw new IndexOutOfBoundsException();
091: data[index] = value;
092: }
093:
094: public final void setBooleanAtBuffer(int index, boolean value) {
095: data[index] = value;
096: }
097:
098: protected void clearBuffer(int start, int count) {
099: while (--count >= 0)
100: data[start++] = false;
101: }
102:
103: public int getElementKind() {
104: return BOOLEAN_VALUE;
105: }
106:
107: public String getTag() {
108: return "b";
109: }
110:
111: public boolean consumeNext(int ipos, Consumer out) {
112: int index = ipos >>> 1;
113: if (index >= size)
114: return false;
115: out.writeBoolean(data[index]);
116: return true;
117: }
118:
119: public void consumePosRange(int iposStart, int iposEnd, Consumer out) {
120: if (out.ignoring())
121: return;
122: int i = iposStart >>> 1;
123: int end = iposEnd >>> 1;
124: for (; i < end; i++)
125: out.writeBoolean(data[i]);
126: }
127:
128: /**
129: * @serialData Write 'size' (using writeInt),
130: * followed by 'size' elements in order (using writeBoolean).
131: */
132: public void writeExternal(ObjectOutput out) throws IOException {
133: int size = this .size;
134: out.writeInt(size);
135: for (int i = 0; i < size; i++)
136: out.writeBoolean(data[i]);
137: }
138:
139: public void readExternal(ObjectInput in) throws IOException,
140: ClassNotFoundException {
141: int size = in.readInt();
142: boolean[] data = new boolean[size];
143: for (int i = 0; i < size; i++)
144: data[i] = in.readBoolean();
145: this.data = data;
146: this.size = size;
147: }
148: }
|