[6997] | 1 | /* |
---|
| 2 | * EqualpHashTable.java |
---|
| 3 | * |
---|
[10925] | 4 | * Copyright (C) 2004-2006 Peter Graves |
---|
[11297] | 5 | * $Id: EqualpHashTable.java 12255 2009-11-06 22:36:32Z ehuelsmann $ |
---|
[6997] | 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. |
---|
[11391] | 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. |
---|
[6997] | 32 | */ |
---|
| 33 | |
---|
| 34 | package org.armedbear.lisp; |
---|
| 35 | |
---|
[7459] | 36 | public final class EqualpHashTable extends HashTable |
---|
[6997] | 37 | { |
---|
[10913] | 38 | public EqualpHashTable(int size, LispObject rehashSize, |
---|
| 39 | LispObject rehashThreshold) |
---|
| 40 | { |
---|
| 41 | super(size, rehashSize, rehashThreshold); |
---|
| 42 | } |
---|
[6997] | 43 | |
---|
[11488] | 44 | @Override |
---|
[10913] | 45 | public Symbol getTest() |
---|
| 46 | { |
---|
| 47 | return Symbol.EQUALP; |
---|
| 48 | } |
---|
[8192] | 49 | |
---|
[11488] | 50 | @Override |
---|
[10913] | 51 | public LispObject get(LispObject key) |
---|
| 52 | { |
---|
| 53 | final int index = key.psxhash() % buckets.length; |
---|
| 54 | HashEntry e = buckets[index]; |
---|
| 55 | while (e != null) |
---|
| 56 | { |
---|
[12255] | 57 | if (key.equalp(e.key)) |
---|
| 58 | return e.value; |
---|
[10913] | 59 | e = e.next; |
---|
| 60 | } |
---|
| 61 | return null; |
---|
| 62 | } |
---|
[6997] | 63 | |
---|
[11488] | 64 | @Override |
---|
[12254] | 65 | public void put(LispObject key, LispObject value) |
---|
[10913] | 66 | { |
---|
| 67 | int index = key.psxhash() % buckets.length; |
---|
| 68 | HashEntry e = buckets[index]; |
---|
| 69 | while (e != null) |
---|
| 70 | { |
---|
[10925] | 71 | if (key.equalp(e.key)) |
---|
[10913] | 72 | { |
---|
| 73 | e.value = value; |
---|
| 74 | return; |
---|
| 75 | } |
---|
| 76 | e = e.next; |
---|
| 77 | } |
---|
| 78 | // Not found. We need to add a new entry. |
---|
| 79 | if (++count > threshold) |
---|
| 80 | { |
---|
| 81 | rehash(); |
---|
| 82 | // Need a new hash value to suit the bigger table. |
---|
| 83 | index = key.psxhash() % buckets.length; |
---|
| 84 | } |
---|
| 85 | e = new HashEntry(key, value); |
---|
| 86 | e.next = buckets[index]; |
---|
| 87 | buckets[index] = e; |
---|
| 88 | } |
---|
[9801] | 89 | |
---|
[11488] | 90 | @Override |
---|
[12254] | 91 | public LispObject remove(LispObject key) |
---|
[10913] | 92 | { |
---|
| 93 | final int index = key.psxhash() % buckets.length; |
---|
| 94 | HashEntry e = buckets[index]; |
---|
| 95 | HashEntry last = null; |
---|
| 96 | while (e != null) |
---|
| 97 | { |
---|
[10925] | 98 | if (key.equalp(e.key)) |
---|
[10913] | 99 | { |
---|
| 100 | if (last == null) |
---|
| 101 | buckets[index] = e.next; |
---|
| 102 | else |
---|
| 103 | last.next = e.next; |
---|
| 104 | --count; |
---|
| 105 | return e.value; |
---|
| 106 | } |
---|
| 107 | last = e; |
---|
| 108 | e = e.next; |
---|
| 109 | } |
---|
| 110 | return null; |
---|
| 111 | } |
---|
[9801] | 112 | |
---|
[11488] | 113 | @Override |
---|
[10913] | 114 | protected void rehash() |
---|
| 115 | { |
---|
| 116 | HashEntry[] oldBuckets = buckets; |
---|
| 117 | int newCapacity = buckets.length * 2 + 1; |
---|
| 118 | threshold = (int) (newCapacity * loadFactor); |
---|
| 119 | buckets = new HashEntry[newCapacity]; |
---|
| 120 | for (int i = oldBuckets.length; i-- > 0;) |
---|
| 121 | { |
---|
| 122 | HashEntry e = oldBuckets[i]; |
---|
| 123 | while (e != null) |
---|
| 124 | { |
---|
| 125 | final int index = e.key.psxhash() % buckets.length; |
---|
| 126 | HashEntry dest = buckets[index]; |
---|
| 127 | if (dest != null) |
---|
| 128 | { |
---|
| 129 | while (dest.next != null) |
---|
| 130 | dest = dest.next; |
---|
| 131 | dest.next = e; |
---|
| 132 | } |
---|
| 133 | else |
---|
| 134 | buckets[index] = e; |
---|
| 135 | HashEntry next = e.next; |
---|
| 136 | e.next = null; |
---|
| 137 | e = next; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | } |
---|
[6997] | 141 | } |
---|