📄 bytearrayhashmap.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: ByteArrayHashMap.java
package org.gudy.azureus2.core3.util;
import java.util.ArrayList;
import java.util.List;
public class ByteArrayHashMap
{
static class Entry
{
final byte key[];
Object value;
final int hash;
Entry next;
public byte[] getKey()
{
return key;
}
public Object getValue()
{
return value;
}
Entry(int h, byte k[], Object v, Entry n)
{
value = v;
next = n;
key = k;
hash = h;
}
}
static final int DEFAULT_INITIAL_CAPACITY = 16;
static final int MAXIMUM_CAPACITY = 0x40000000;
static final float DEFAULT_LOAD_FACTOR = 0.75F;
private Entry table[];
private int size;
private int threshold;
final float loadFactor;
public ByteArrayHashMap(int initialCapacity, float loadFactor)
{
if (initialCapacity < 0)
throw new IllegalArgumentException((new StringBuilder()).append("Illegal initial capacity: ").append(initialCapacity).toString());
if (initialCapacity > 0x40000000)
initialCapacity = 0x40000000;
if (loadFactor <= 0.0F || Float.isNaN(loadFactor))
throw new IllegalArgumentException((new StringBuilder()).append("Illegal load factor: ").append(loadFactor).toString());
int capacity;
for (capacity = 1; capacity < initialCapacity; capacity <<= 1);
this.loadFactor = loadFactor;
threshold = (int)((float)capacity * loadFactor);
table = new Entry[capacity];
}
public ByteArrayHashMap(int initialCapacity)
{
this(initialCapacity, 0.75F);
}
public ByteArrayHashMap()
{
loadFactor = 0.75F;
threshold = 12;
table = new Entry[16];
}
public int size()
{
return size;
}
public boolean isEmpty()
{
return size == 0;
}
public Object get(byte key[], int offset, int len)
{
byte k[] = new byte[len];
System.arraycopy(key, offset, k, 0, len);
return get(k);
}
public Object get(byte key[])
{
int hash = hash(key);
int i = indexFor(hash, table.length);
Entry e = table[i];
do
{
if (e == null)
return e;
if (e.hash == hash && eq(key, e.key))
return e.value;
e = e.next;
} while (true);
}
public boolean containsKey(byte key[])
{
int hash = hash(key);
int i = indexFor(hash, table.length);
Entry e = table[i];
do
{
if (e == null)
return false;
if (e.hash == hash && eq(key, e.key))
return true;
e = e.next;
} while (true);
}
public Object put(byte key[], Object value)
{
int hash = hash(key);
int i = indexFor(hash, table.length);
for (Entry e = table[i]; e != null; e = e.next)
if (e.hash == hash && eq(key, e.key))
{
Object oldValue = e.value;
e.value = value;
return oldValue;
}
addEntry(hash, key, value, i);
return null;
}
public Object remove(byte key[])
{
Entry e = removeEntryForKey(key);
return e != null ? e.value : e;
}
public void clear()
{
Entry tab[] = table;
for (int i = 0; i < tab.length; i++)
tab[i] = null;
size = 0;
}
public List keys()
{
List res = new ArrayList();
for (int j = 0; j < table.length; j++)
{
for (Entry e = table[j]; e != null; e = e.next)
res.add(e.key);
}
return res;
}
public List values()
{
List res = new ArrayList();
for (int j = 0; j < table.length; j++)
{
for (Entry e = table[j]; e != null; e = e.next)
res.add(e.value);
}
return res;
}
public ByteArrayHashMap duplicate()
{
ByteArrayHashMap res = new ByteArrayHashMap(size, loadFactor);
for (int j = 0; j < table.length; j++)
{
for (Entry e = table[j]; e != null; e = e.next)
res.put(e.key, e.value);
}
return res;
}
void resize(int newCapacity)
{
Entry oldTable[] = table;
int oldCapacity = oldTable.length;
if (oldCapacity == 0x40000000)
{
threshold = 0x7fffffff;
return;
} else
{
Entry newTable[] = new Entry[newCapacity];
transfer(newTable);
table = newTable;
threshold = (int)((float)newCapacity * loadFactor);
return;
}
}
void transfer(Entry newTable[])
{
Entry src[] = table;
int newCapacity = newTable.length;
for (int j = 0; j < src.length; j++)
{
Entry e = src[j];
if (e == null)
continue;
src[j] = null;
do
{
Entry next = e.next;
int i = indexFor(e.hash, newCapacity);
e.next = newTable[i];
newTable[i] = e;
e = next;
} while (e != null);
}
}
Entry removeEntryForKey(byte key[])
{
int hash = hash(key);
int i = indexFor(hash, table.length);
Entry prev = table[i];
Entry e;
Entry next;
for (e = prev; e != null; e = next)
{
next = e.next;
if (e.hash == hash && eq(key, e.key))
{
size--;
if (prev == e)
table[i] = next;
else
prev.next = next;
return e;
}
prev = e;
}
return e;
}
void addEntry(int hash, byte key[], Object value, int bucketIndex)
{
table[bucketIndex] = new Entry(hash, key, value, table[bucketIndex]);
if (size++ >= threshold)
resize(2 * table.length);
}
void createEntry(int hash, byte key[], Object value, int bucketIndex)
{
table[bucketIndex] = new Entry(hash, key, value, table[bucketIndex]);
size++;
}
private static final int hash(byte x[])
{
int hash = 0;
int len = x.length;
for (int i = 0; i < len; i++)
hash = 31 * hash + x[i];
return hash;
}
private static final boolean eq(byte x[], byte y[])
{
if (x == y)
return true;
int len = x.length;
if (len != y.length)
return false;
for (int i = 0; i < len; i++)
if (x[i] != y[i])
return false;
return true;
}
private static final int indexFor(int h, int length)
{
return h & length - 1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -