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

Last change on this file was 11698, checked in by astalla, 16 years ago

reverted wrong commit.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1/*
2 * SymbolHashTable.java
3 *
4 * Copyright (C) 2004-2005 Peter Graves
5 * $Id: SymbolHashTable.java 11698 2009-03-05 23:20:43Z astalla $
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 * 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
36import java.util.ArrayList;
37import java.util.List;
38
39public final class SymbolHashTable
40{
41    private static final float LOAD_FACTOR = 0.75f;
42
43    private int threshold;
44    private HashEntry[] buckets;
45    private int count;
46
47    private int mask;
48
49    public SymbolHashTable(int size)
50    {
51        buckets = new HashEntry[calculateInitialCapacity(size)];
52        threshold = (int) (size * LOAD_FACTOR);
53        mask = buckets.length - 1;
54    }
55
56    private static int calculateInitialCapacity(int size)
57    {
58        int capacity = 1;
59        while (capacity < size)
60            capacity <<= 1;
61        return capacity;
62    }
63
64    public Symbol get(SimpleString key)
65    {
66        HashEntry e = buckets[key.sxhash() & mask];
67        while (e != null) {
68            try {
69                if (key.equal(e.symbol.name))
70                    return e.symbol; // Return the symbol.
71            }
72            catch (Throwable t) {
73                Debug.trace(t); // Shouldn't happen.
74            }
75            e = e.next;
76        }
77        return null;
78    }
79
80    public Symbol get(SimpleString key, int hash)
81    {
82        HashEntry e = buckets[hash & mask];
83        while (e != null) {
84            try {
85                if (key.equal(e.symbol.name))
86                    return e.symbol; // Return the symbol.
87            }
88            catch (Throwable t) {
89                Debug.trace(t); // Shouldn't happen.
90            }
91            e = e.next;
92        }
93        return null;
94    }
95
96    public void put(final SimpleString key, final Symbol symbol)
97    {
98        int index = key.sxhash() & mask;
99        HashEntry e = buckets[index];
100        while (e != null) {
101            try {
102                if (key.equal(e.symbol.name)) {
103                    if (e.symbol != symbol) {
104                        Debug.trace("replacing existing key for " + key.getStringValue() +
105                                    " in package " + e.symbol.getPackage().writeToString());
106                        Thread.dumpStack();
107                        e.symbol = symbol;
108                    }
109                    return;
110                }
111            }
112            catch (Throwable t) {
113                Debug.trace(t); // FIXME
114            }
115            e = e.next;
116        }
117        // Not found. We need to add a new entry.
118        if (++count > threshold) {
119            rehash();
120            // We need a new index for the bigger table.
121            index = key.sxhash() & mask;
122        }
123        e = new HashEntry(symbol);
124        e.next = buckets[index];
125        buckets[index] = e;
126    }
127
128    public void put(Symbol symbol)
129    {
130        int index = symbol.sxhash() & mask;
131        HashEntry e = buckets[index];
132        while (e != null) {
133            try {
134                if (symbol.name.equal(e.symbol.name)) {
135                    if (e.symbol != symbol) {
136                        Debug.trace("replacing existing key for " + symbol.getName());
137                        Thread.dumpStack();
138                        e.symbol = symbol; // Replace existing key.
139                    }
140                    return;
141                }
142            }
143            catch (Throwable t) {
144                Debug.trace(t); // FIXME
145            }
146            e = e.next;
147        }
148        // Not found. We need to add a new entry.
149        if (++count > threshold) {
150            rehash();
151            // Need a new hash value to suit the bigger table.
152            index = symbol.sxhash() & mask;
153        }
154        e = new HashEntry(symbol);
155        e.next = buckets[index];
156        buckets[index] = e;
157    }
158
159    public LispObject remove(LispObject key)
160    {
161        if (key instanceof Symbol)
162            key = ((Symbol)key).name;
163        int index = key.sxhash() & mask;
164        HashEntry e = buckets[index];
165        HashEntry last = null;
166        while (e != null) {
167            try {
168                if (key.equal(e.symbol.name)) {
169                    if (last == null)
170                        buckets[index] = e.next;
171                    else
172                        last.next = e.next;
173                    --count;
174                    return e.symbol; // The key is the value!
175                }
176            }
177            catch (Throwable t) {
178                Debug.trace(t); // FIXME
179            }
180            last = e;
181            e = e.next;
182        }
183        return null;
184    }
185
186    private void rehash()
187    {
188        HashEntry[] oldBuckets = buckets;
189        int newCapacity = buckets.length * 2;
190        threshold = (int) (newCapacity * LOAD_FACTOR);
191        buckets = new HashEntry[newCapacity];
192        mask = buckets.length - 1;
193        for (int i = oldBuckets.length; i-- > 0;) {
194            HashEntry e = oldBuckets[i];
195            while (e != null) {
196                final int index = e.symbol.sxhash() & mask;
197                HashEntry dest = buckets[index];
198                if (dest != null) {
199                    while (dest.next != null)
200                        dest = dest.next;
201                    dest.next = e;
202                } else
203                    buckets[index] = e;
204                HashEntry next = e.next;
205                e.next = null;
206                e = next;
207            }
208        }
209    }
210
211    public List<Symbol> getSymbols()
212    {
213        ArrayList<Symbol> list = new ArrayList<Symbol>();
214        for (int i = 0; i < buckets.length; i++) {
215            HashEntry e = buckets[i];
216            while (e != null) {
217                list.add(e.symbol);
218                e = e.next;
219            }
220        }
221        return list;
222    }
223
224    private static class HashEntry
225    {
226        Symbol symbol;
227        HashEntry next;
228
229        HashEntry(Symbol symbol)
230        {
231            this.symbol = symbol;
232        }
233    }
234}
Note: See TracBrowser for help on using the repository browser.