📄 lighthashset.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: LightHashSet.java
package org.gudy.azureus2.core3.util;
import java.io.PrintStream;
import java.util.*;
// Referenced classes of package org.gudy.azureus2.core3.util:
// LightHashMap
public class LightHashSet extends AbstractSet
implements Cloneable
{
private class HashIterator
implements Iterator
{
private int nextIdx;
private int currentIdx;
private Object itData[];
final LightHashSet this$0;
private void findNext()
{
do
nextIdx++;
while (nextIdx < itData.length && (itData[nextIdx] == null || itData[nextIdx] == LightHashSet.THOMBSTONE));
}
public void remove()
{
if (currentIdx == -1)
throw new IllegalStateException("No entry to delete, use next() first");
if (itData != data)
{
throw new ConcurrentModificationException("removal opperation not supported as concurrent structural modification occured");
} else
{
removeForIndex(currentIdx);
currentIdx = -1;
return;
}
}
public boolean hasNext()
{
return nextIdx < itData.length;
}
public Object next()
{
if (!hasNext())
{
throw new IllegalStateException("No more entries");
} else
{
currentIdx = nextIdx;
findNext();
Object key = itData[currentIdx];
return key == LightHashSet.NULLKEY ? null : key;
}
}
public HashIterator()
{
this$0 = LightHashSet.this;
super();
nextIdx = -1;
currentIdx = -1;
itData = data;
findNext();
}
}
private static final Object THOMBSTONE = new Object();
private static final Object NULLKEY = new Object();
private static final float DEFAULT_LOAD_FACTOR = 0.75F;
private static final int DEFAULT_CAPACITY = 8;
final float loadFactor;
int size;
Object data[];
public LightHashSet()
{
this(8, 0.75F);
}
public LightHashSet(int initialCapacity)
{
this(initialCapacity, 0.75F);
}
public LightHashSet(Collection c)
{
this(0);
if (c instanceof LightHashSet)
{
LightHashSet lightMap = (LightHashSet)c;
size = lightMap.size;
data = (Object[])(Object[])((Object []) (lightMap.data)).clone();
} else
{
addAll(c);
}
}
public Object clone()
{
LightHashMap newMap;
newMap = (LightHashMap)super.clone();
newMap.data = (Object[])(Object[])((Object []) (data)).clone();
return newMap;
CloneNotSupportedException e;
e;
e.printStackTrace();
throw new RuntimeException(e);
}
public LightHashSet(int initialCapacity, float loadFactor)
{
if (loadFactor > 1.0F)
throw new IllegalArgumentException("Load factor must not be > 1");
this.loadFactor = loadFactor;
int capacity;
for (capacity = 1; capacity < initialCapacity; capacity <<= 1);
data = new Object[capacity];
}
public Iterator iterator()
{
return new HashIterator();
}
public boolean add(Object key)
{
checkCapacity(1);
return addInternal(key, false);
}
public int size()
{
return size;
}
public boolean addAll(Collection c)
{
checkCapacity(c.size());
boolean changed = false;
for (Iterator it = c.iterator(); it.hasNext();)
changed |= addInternal(it.next(), true);
return changed;
}
public int capacity()
{
return data.length;
}
public Object get(Object key)
{
if (key == null)
key = NULLKEY;
int idx = nonModifyingFindIndex(key);
if (keysEqual(data[idx], key))
return data[idx];
else
return null;
}
private boolean addInternal(Object key, boolean bulkAdd)
{
if (key == null)
key = NULLKEY;
int idx = bulkAdd ? nonModifyingFindIndex(key) : findIndex(key);
if (data[idx] == null || data[idx] == THOMBSTONE)
{
data[idx] = key;
size++;
return true;
} else
{
return false;
}
}
public boolean remove(Object key)
{
if (size == 0)
return false;
if (key == null)
key = NULLKEY;
int idx = findIndex(key);
if (keysEqual(key, data[idx]))
{
removeForIndex(idx);
return true;
} else
{
return false;
}
}
private void removeForIndex(int idx)
{
data[idx] = THOMBSTONE;
size--;
}
public void clear()
{
size = 0;
int capacity;
for (capacity = 1; capacity < 8; capacity <<= 1);
data = new Object[capacity];
}
public boolean contains(Object key)
{
if (size == 0)
return false;
if (key == null)
key = NULLKEY;
return keysEqual(key, data[nonModifyingFindIndex(key)]);
}
private final boolean keysEqual(Object o1, Object o2)
{
return o1 == o2 || o1 != null && o2 != null && o1.hashCode() == o2.hashCode() && o1.equals(o2);
}
private int findIndex(Object keyToFind)
{
int hash = keyToFind.hashCode();
int probe = 1;
int newIndex = hash & data.length - 1;
int thombStoneIndex = -1;
int thombStoneCount = 0;
int thombStoneThreshold = Math.min(data.length - size, 100);
while (data[newIndex] != null && !keysEqual(data[newIndex], keyToFind))
{
if (data[newIndex] == THOMBSTONE)
{
if (thombStoneIndex == -1)
thombStoneIndex = newIndex;
if (++thombStoneCount * 2 > thombStoneThreshold)
{
compactify(0.0F);
thombStoneIndex = -1;
probe = 0;
thombStoneCount = 0;
}
}
newIndex = hash + (probe + probe * probe >> 1) & data.length - 1;
probe++;
}
if (thombStoneIndex != -1 && !keysEqual(data[newIndex], keyToFind))
return thombStoneIndex;
else
return newIndex;
}
private int nonModifyingFindIndex(Object keyToFind)
{
int hash = keyToFind.hashCode();
int probe = 1;
int newIndex = hash & data.length - 1;
int thombStoneIndex = -1;
for (; data[newIndex] != null && !keysEqual(data[newIndex], keyToFind) && probe < data.length; probe++)
{
if (data[newIndex] == THOMBSTONE && thombStoneIndex == -1)
thombStoneIndex = newIndex;
newIndex = hash + (probe + probe * probe >> 1) & data.length - 1;
}
if (thombStoneIndex != -1 && !keysEqual(data[newIndex], keyToFind))
return thombStoneIndex;
else
return newIndex;
}
private void checkCapacity(int n)
{
int currentCapacity = data.length;
if ((float)(size + n) < (float)currentCapacity * loadFactor)
return;
int newCapacity = currentCapacity;
do
newCapacity <<= 1;
while ((float)newCapacity * loadFactor < (float)(size + n));
adjustCapacity(newCapacity);
}
public void compactify(float compactingLoadFactor)
{
int newCapacity = 1;
float adjustedLoadFactor = Math.abs(compactingLoadFactor);
if (adjustedLoadFactor <= 0.0F || adjustedLoadFactor >= 1.0F)
adjustedLoadFactor = loadFactor;
for (; (float)newCapacity * adjustedLoadFactor < (float)(size + 1); newCapacity <<= 1);
if (newCapacity < data.length || compactingLoadFactor >= 0.0F)
adjustCapacity(newCapacity);
}
private void adjustCapacity(int newSize)
{
Object oldData[] = data;
data = new Object[newSize];
size = 0;
for (int i = 0; i < oldData.length; i++)
if (oldData[i] != null && oldData[i] != THOMBSTONE)
addInternal(oldData[i], true);
}
static void test()
{
Random rnd = new Random();
byte buffer[] = new byte[5];
String fillData[] = new String[0xee147];
for (int i = 0; i < fillData.length; i++)
{
rnd.nextBytes(buffer);
fillData[i] = new String(buffer);
fillData[i].hashCode();
}
Set s1 = new HashSet();
Set s2 = new LightHashSet();
System.out.println("fill:");
long time = System.currentTimeMillis();
for (int i = 0; i < fillData.length; i++)
s1.add(fillData[i]);
System.out.println(System.currentTimeMillis() - time);
time = System.currentTimeMillis();
for (int i = 0; i < fillData.length; i++)
s2.add(fillData[i]);
System.out.println(System.currentTimeMillis() - time);
System.out.println("replace-fill:");
time = System.currentTimeMillis();
for (int i = 0; i < fillData.length; i++)
s1.add(fillData[i]);
System.out.println(System.currentTimeMillis() - time);
time = System.currentTimeMillis();
for (int i = 0; i < fillData.length; i++)
s2.add(fillData[i]);
System.out.println(System.currentTimeMillis() - time);
System.out.println("get:");
time = System.currentTimeMillis();
for (int i = 0; i < fillData.length; i++)
s1.contains(fillData[i]);
System.out.println(System.currentTimeMillis() - time);
time = System.currentTimeMillis();
for (int i = 0; i < fillData.length; i++)
s2.contains(fillData[i]);
System.out.println(System.currentTimeMillis() - time);
System.out.println("compactify light map");
time = System.currentTimeMillis();
((LightHashSet)s2).compactify(0.95F);
System.out.println(System.currentTimeMillis() - time);
System.out.println("transfer to hashmap");
time = System.currentTimeMillis();
new HashSet(s1);
System.out.println(System.currentTimeMillis() - time);
time = System.currentTimeMillis();
new HashSet(s2);
System.out.println(System.currentTimeMillis() - time);
System.out.println("transfer to lighthashmap");
time = System.currentTimeMillis();
new LightHashSet(s1);
System.out.println(System.currentTimeMillis() - time);
time = System.currentTimeMillis();
new LightHashSet(s2);
System.out.println(System.currentTimeMillis() - time);
System.out.println("remove entry by entry");
time = System.currentTimeMillis();
for (int i = 0; i < fillData.length; i++)
s1.remove(fillData[i]);
System.out.println(System.currentTimeMillis() - time);
time = System.currentTimeMillis();
for (int i = 0; i < fillData.length; i++)
s2.remove(fillData[i]);
System.out.println(System.currentTimeMillis() - time);
}
public static void main(String args[])
{
System.out.println("Call with -Xmx300m -Xcomp -server");
Thread.currentThread().setPriority(10);
try
{
Thread.sleep(300L);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
test();
System.out.println("-------------------------------------");
System.gc();
try
{
Thread.sleep(300L);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
test();
System.out.println("\n\nPerforming sanity tests");
Random rnd = new Random();
byte buffer[] = new byte[25];
String fillData[] = new String[1048];
for (int i = 0; i < fillData.length; i++)
{
rnd.nextBytes(buffer);
fillData[i] = new String(buffer);
fillData[i].hashCode();
}
Set s1 = new HashSet();
Set s2 = new LightHashSet();
for (int i = 0; i < fillData.length * 10; i++)
{
int random = rnd.nextInt(fillData.length);
s1.add(null);
s2.add(null);
if (!s1.equals(s2))
System.out.println("Error 0");
s1.add(fillData[random]);
s2.add(fillData[random]);
if (!s1.equals(s2))
System.out.println("Error 1");
}
for (int i = 0; i < fillData.length / 2; i++)
{
int random = rnd.nextInt(fillData.length);
s1.remove(fillData[random]);
s2.remove(fillData[random]);
if (!s1.equals(s2))
System.out.println("Error 2");
}
for (int i = 0; i < fillData.length * 10; i++)
{
int random = rnd.nextInt(fillData.length);
s1.add(fillData[random]);
s1.add(null);
s2.add(fillData[random]);
s2.add(null);
if (!s1.equals(s2))
System.out.println("Error 3");
}
Iterator i1 = s1.iterator();
Iterator i2 = s2.iterator();
for (; i1.hasNext(); i2.remove())
{
i1.next();
i1.remove();
i2.next();
}
if (!s1.equals(s2))
System.out.println("Error 4");
s2.clear();
for (int i = 0; i < 0x186a0; i++)
{
rnd.nextBytes(buffer);
String s = new String(buffer);
s2.add(s);
s2.contains(s);
s2.remove(s);
}
System.out.println("checks done");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -