KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > lisp > AbstractBitVector


1 /*
2  * AbstractBitVector.java
3  *
4  * Copyright (C) 2004 Peter Graves
5  * $Id: AbstractBitVector.java,v 1.7 2004/05/16 14:01:57 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.lisp;
23
24 public abstract class AbstractBitVector extends AbstractVector
25 {
26     protected static final int LONG_MASK = 0x3f;
27
28     protected int capacity;
29
30     // For non-displaced bit-vectors.
31
protected long[] bits;
32
33     public LispObject typep(LispObject type) throws ConditionThrowable
34     {
35         if (type == Symbol.BIT_VECTOR)
36             return T;
37         if (type == BuiltInClass.BIT_VECTOR)
38             return T;
39         return super.typep(type);
40     }
41
42     public LispClass classOf()
43     {
44         return BuiltInClass.BIT_VECTOR;
45     }
46
47     public final LispObject BIT_VECTOR_P()
48     {
49         return T;
50     }
51
52     public final int capacity()
53     {
54         return capacity;
55     }
56
57     public final LispObject getElementType()
58     {
59         return Symbol.BIT;
60     }
61
62     public boolean equal(LispObject obj) throws ConditionThrowable
63     {
64         if (this == obj)
65             return true;
66         if (obj instanceof AbstractBitVector) {
67             AbstractBitVector v = (AbstractBitVector) obj;
68             if (length() != v.length())
69                 return false;
70             for (int i = length(); i-- > 0;) {
71                 if (getBit(i) != v.getBit(i))
72                     return false;
73             }
74             return true;
75         }
76         return false;
77     }
78
79     public boolean equalp(LispObject obj) throws ConditionThrowable
80     {
81         if (this == obj)
82             return true;
83         if (obj instanceof AbstractBitVector) {
84             AbstractBitVector v = (AbstractBitVector) obj;
85             if (length() != v.length())
86                 return false;
87             for (int i = length(); i-- > 0;) {
88                 if (getBit(i) != v.getBit(i))
89                     return false;
90             }
91             return true;
92         }
93         if (obj instanceof AbstractVector)
94             return ((AbstractVector)obj).equalp(this);
95         return false;
96     }
97
98     public void fill(LispObject obj) throws ConditionThrowable
99     {
100         try {
101             switch (((Fixnum)obj).value) {
102                 case 0:
103                     if (bits != null) {
104                         for (int i = bits.length; i-- > 0;)
105                             bits[i] = 0;
106                     } else {
107                         for (int i = capacity; i-- > 0;)
108                             clearBit(i);
109                     }
110                     return;
111                 case 1:
112                     if (bits != null) {
113                         for (int i = bits.length; i-- > 0;)
114                             bits[i] = -1L;
115                     } else {
116                         for (int i = capacity; i-- > 0;)
117                             setBit(i);
118                     }
119                     return;
120             }
121         }
122         catch (ClassCastException JavaDoc e) {
123             // Fall through...
124
}
125         signal(new TypeError(obj, Symbol.BIT));
126     }
127
128     public LispObject subseq(int start, int end) throws ConditionThrowable
129     {
130         SimpleBitVector v = new SimpleBitVector(end - start);
131         int i = start, j = 0;
132         try {
133             while (i < end) {
134                 if (getBit(i++) == 0)
135                     v.clearBit(j++);
136                 else
137                     v.setBit(j++);
138             }
139             return v;
140         }
141         catch (ArrayIndexOutOfBoundsException JavaDoc e) {
142             return signal(new TypeError("Array index out of bounds: " + i + "."));
143         }
144     }
145
146     public int hashCode()
147     {
148         int hashCode = 1;
149         try {
150             // Consider first 64 bits only.
151
final int limit = Math.min(length(), 64);
152             for (int i = 0; i < limit; i++)
153                 hashCode = hashCode * 31 + getBit(i);
154         }
155         catch (ConditionThrowable t) {
156             // Shouldn't happen.
157
Debug.trace(t);
158         }
159         return hashCode;
160     }
161
162     public String JavaDoc writeToString() throws ConditionThrowable
163     {
164         final int limit = length();
165         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(limit + 2);
166         sb.append("#*");
167         for (int i = 0; i < limit; i++)
168             sb.append(getBit(i) == 1 ? '1' : '0');
169         return sb.toString();
170     }
171
172     // Ignores fill pointer.
173
public LispObject AREF(LispObject index) throws ConditionThrowable
174     {
175         try {
176             return getRowMajor(((Fixnum)index).value);
177         }
178         catch (ClassCastException JavaDoc e) {
179             return signal(new TypeError(index, Symbol.FIXNUM));
180         }
181     }
182
183     public LispObject reverse() throws ConditionThrowable
184     {
185         int length = length();
186         SimpleBitVector result = new SimpleBitVector(length);
187         int i, j;
188         for (i = 0, j = length - 1; i < length; i++, j--) {
189             if (getBit(j) == 1)
190                 result.setBit(i);
191             else
192                 result.clearBit(i);
193         }
194         return result;
195     }
196
197     protected abstract int getBit(int index) throws ConditionThrowable;
198
199     protected abstract void setBit(int index) throws ConditionThrowable;
200
201     protected abstract void clearBit(int index) throws ConditionThrowable;
202 }
203
Popular Tags