X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fxwt%2Futil%2FHash.java;h=09ed00865bca595610f51204015396f00cae398a;hb=de378041d5ca2aca1a2b5a31ef15ae90a86c977f;hp=2d73b1045739a31364f6f47e374e0dbfd1375706;hpb=78f74e193fcc871be406d5cde191818224b8e249;p=org.ibex.core.git diff --git a/src/org/xwt/util/Hash.java b/src/org/xwt/util/Hash.java index 2d73b10..09ed008 100644 --- a/src/org/xwt/util/Hash.java +++ b/src/org/xwt/util/Hash.java @@ -1,4 +1,10 @@ -// Copyright 2002 Adam Megacz, see the COPYING file for licensing [GPL] +// Copyright (C) 2003 Adam Megacz all rights reserved. +// +// You may modify, copy, and redistribute this code under the terms of +// the GNU Library Public License version 2.1, with the exception of +// the portion of clause 6a after the semicolon (aka the "obnoxious +// relink clause") + package org.xwt.util; import java.util.*; @@ -7,9 +13,10 @@ import java.util.*; * keys, using Radke's quadradic residue linear probing instead of * buckets to minimize object count (less allocations, faster GC). * See C. Radke, Communications of the ACM, 1970, 103-105 + * + * Not threadsafe. */ public class Hash { - /** this object is inserted as key in a slot when the * corresponding value is removed -- this ensures that the * probing sequence for any given key remains the same even if @@ -21,7 +28,7 @@ public class Hash { private int usedslots = 0; /** the number of entries with non-null values */ - private int size = 0; + protected int size = 0; /** when num_slots < loadFactor * size, rehash into a bigger table */ private final int loadFactor; @@ -50,14 +57,7 @@ public class Hash { } /** returns all the primary keys in the table */ - public Object[] keys() { - int howmany = 0; - for(int i=0; i vals.length) rehash(); int hash = (k1 == null ? 0 : k1.hashCode()) ^ (k2 == null ? 0 : k2.hashCode()); int dest = Math.abs(hash) % vals.length; @@ -149,6 +150,27 @@ public class Hash { vals[dest] = v; } + private class HashEnum implements java.util.Enumeration { + private int iterator = 0; + private int found = 0; + + public HashEnum () { } + + public boolean hasMoreElements() { + return found < usedslots; + } + + public Object nextElement() { + if (!hasMoreElements()) throw new java.util.NoSuchElementException(); + + Object o = null; + while (o == null) o = keys1[iterator++]; + if (o == null) throw new IllegalStateException("Didn't find an element, when I should have."); + found++; + + return o; + } + } }