source: branches/0.17.x/abcl/src/org/armedbear/lisp/HashTable.java

Last change on this file was 12254, checked in by ehuelsmann, 16 years ago

Remove 'throws ConditionThrowable?' method annotations:

it's an unchecked exception now, so no need to declare it thrown.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
1/*
2 * HashTable.java
3 *
4 * Copyright (C) 2002-2007 Peter Graves
5 * $Id: HashTable.java 12254 2009-11-06 20:07:54Z ehuelsmann $
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 * As a special exception, the copyright holders of this library give you
22 * permission to link this library with independent modules to produce an
23 * executable, regardless of the license terms of these independent
24 * modules, and to copy and distribute the resulting executable under
25 * terms of your choice, provided that you also meet, for each linked
26 * independent module, the terms and conditions of the license of that
27 * module.  An independent module is a module which is not derived from
28 * or based on this library.  If you modify this library, you may extend
29 * this exception to your version of the library, but you are not
30 * obligated to do so.  If you do not wish to do so, delete this
31 * exception statement from your version.
32 */
33
34package org.armedbear.lisp;
35
36public abstract class HashTable extends LispObject
37{
38  private static final int DEFAULT_SIZE = 16;
39
40  protected static final float loadFactor = 0.75f;
41
42  protected final LispObject rehashSize;
43  protected final LispObject rehashThreshold;
44
45  // The rounded product of the capacity and the load factor. When the number
46  // of elements exceeds the threshold, the implementation calls rehash().
47  protected int threshold;
48
49  // Array containing the actual key-value mappings.
50  protected HashEntry[] buckets;
51
52  // The number of key-value pairs.
53  protected int count;
54
55  protected HashTable()
56  {
57    rehashSize = new SingleFloat(1.5f); // FIXME
58    rehashThreshold = new SingleFloat(0.75f); // FIXME
59    buckets = new HashEntry[DEFAULT_SIZE];
60    threshold = (int) (DEFAULT_SIZE * loadFactor);
61  }
62
63  protected HashTable(int size, LispObject rehashSize,
64                      LispObject rehashThreshold)
65  {
66    this.rehashSize = rehashSize;
67    this.rehashThreshold = rehashThreshold;
68    buckets = new HashEntry[size];
69    threshold = (int) (size * loadFactor);
70  }
71
72  protected static int calculateInitialCapacity(int size)
73  {
74    int capacity = 1;
75    while (capacity < size)
76      capacity <<= 1;
77    return capacity;
78  }
79
80  public final LispObject getRehashSize()
81  {
82    return rehashSize;
83  }
84
85  public final LispObject getRehashThreshold()
86  {
87    return rehashThreshold;
88  }
89
90  public int getSize()
91  {
92    return buckets.length;
93  }
94
95  public int getCount()
96  {
97    return count;
98  }
99
100  public abstract Symbol getTest();
101
102  @Override
103  public LispObject typeOf()
104  {
105    return Symbol.HASH_TABLE;
106  }
107
108  @Override
109  public LispObject classOf()
110  {
111    return BuiltInClass.HASH_TABLE;
112  }
113
114  @Override
115  public LispObject typep(LispObject type)
116  {
117    if (type == Symbol.HASH_TABLE)
118      return T;
119    if (type == BuiltInClass.HASH_TABLE)
120      return T;
121    return super.typep(type);
122  }
123
124  @Override
125  public boolean equalp(LispObject obj)
126  {
127    if (this == obj)
128      return true;
129    if (obj instanceof HashTable)
130      {
131        HashTable ht = (HashTable) obj;
132        if (count != ht.count)
133          return false;
134        if (getTest() != ht.getTest())
135          return false;
136        LispObject entries = ENTRIES();
137        while (entries != NIL)
138          {
139            LispObject entry = entries.car();
140            LispObject key = entry.car();
141            LispObject value = entry.cdr();
142            if (!value.equalp(ht.get(key)))
143              return false;
144            entries = entries.cdr();
145          }
146        return true;
147      }
148    return false;
149  }
150
151  @Override
152  public LispObject getParts()
153  {
154    LispObject parts = NIL;
155    for (int i = 0; i < buckets.length; i++)
156      {
157        HashEntry e = buckets[i];
158        while (e != null)
159          {
160            parts = parts.push(new Cons("KEY [bucket " + i + "]", e.key));
161            parts = parts.push(new Cons("VALUE", e.value));
162            e = e.next;
163          }
164      }
165    return parts.nreverse();
166  }
167
168  public synchronized void clear()
169  {
170    for (int i = buckets.length; i-- > 0;)
171      buckets[i] = null;
172    count = 0;
173  }
174
175  // gethash key hash-table &optional default => value, present-p
176  public synchronized LispObject gethash(LispObject key)
177
178  {
179    LispObject value = get(key);
180    final LispObject presentp;
181    if (value == null)
182      value = presentp = NIL;
183    else
184      presentp = T;
185    return LispThread.currentThread().setValues(value, presentp);
186  }
187
188  // gethash key hash-table &optional default => value, present-p
189  public synchronized LispObject gethash(LispObject key,
190                                         LispObject defaultValue)
191
192  {
193    LispObject value = get(key);
194    final LispObject presentp;
195    if (value == null)
196      {
197        value = defaultValue;
198        presentp = NIL;
199      }
200    else
201      presentp = T;
202    return LispThread.currentThread().setValues(value, presentp);
203  }
204
205  public synchronized LispObject gethash1(LispObject key)
206
207  {
208    final LispObject value = get(key);
209    return value != null ? value : NIL;
210  }
211
212  public synchronized LispObject puthash(LispObject key, LispObject newValue)
213
214  {
215    put(key, newValue);
216    return newValue;
217  }
218
219  // remhash key hash-table => generalized-boolean
220  public synchronized LispObject remhash(LispObject key)
221
222  {
223    // A value in a Lisp hash table can never be null, so...
224    return remove(key) != null ? T : NIL;
225  }
226
227  @Override
228  public String writeToString()
229  {
230    if (Symbol.PRINT_READABLY.symbolValue(LispThread.currentThread()) != NIL)
231      {
232        error(new PrintNotReadable(list(Keyword.OBJECT, this)));
233        return null; // Not reached.
234      }
235    FastStringBuffer sb = new FastStringBuffer(getTest().writeToString());
236    sb.append(' ');
237    sb.append(Symbol.HASH_TABLE.writeToString());
238    sb.append(' ');
239    sb.append(count);
240    if (count == 1)
241      sb.append(" entry");
242    else
243      sb.append(" entries");
244    sb.append(", ");
245    sb.append(buckets.length);
246    sb.append(" buckets");
247    return unreadableString(sb.toString());
248  }
249
250  public abstract LispObject get(LispObject key);
251
252  public abstract void put(LispObject key, LispObject value)
253   ;
254
255  public abstract LispObject remove(LispObject key);
256
257  protected abstract void rehash();
258
259  // Returns a list of (key . value) pairs.
260  public LispObject ENTRIES()
261  {
262    LispObject list = NIL;
263    for (int i = buckets.length; i-- > 0;)
264      {
265        HashEntry e = buckets[i];
266        while (e != null)
267          {
268            list = new Cons(new Cons(e.key, e.value), list);
269            e = e.next;
270          }
271      }
272    return list;
273  }
274
275  public LispObject MAPHASH(LispObject function)
276  {
277    for (int i = buckets.length; i-- > 0;)
278      {
279        HashEntry e = buckets[i];
280        while (e != null)
281          {
282            function.execute(e.key, e.value);
283            e = e.next;
284          }
285      }
286    return NIL;
287  }
288
289  protected static class HashEntry
290  {
291    LispObject key;
292    LispObject value;
293    HashEntry next;
294
295    HashEntry(LispObject key, LispObject value)
296    {
297      this.key = key;
298      this.value = value;
299    }
300  }
301
302  // For EQUALP hash tables.
303  @Override
304  public int psxhash()
305  {
306    long result = 2062775257; // Chosen at random.
307    result = mix(result, count);
308    result = mix(result, getTest().sxhash());
309    return (int) (result & 0x7fffffff);
310  }
311}
Note: See TracBrowser for help on using the repository browser.