package org.jcodings.util;
import static org.jcodings.ascii.AsciiTables.ToLowerCaseTable;
public final class CaseInsensitiveBytesHash<V> extends Hash<V>{
public CaseInsensitiveBytesHash() {
super();
}
public CaseInsensitiveBytesHash(int size) {
super(size);
}
protected void init() {
head = new CaseInsensitiveBytesHashEntry<V>();
}
public final static class CaseInsensitiveBytesHashEntry<V> extends HashEntry<V> {
public final byte[]bytes;
public final int p;
public final int end;
public CaseInsensitiveBytesHashEntry(int hash, HashEntry<V> next, V value, byte[]bytes, int p, int end, HashEntry<V> head) {
super(hash, next, value, head);
this.bytes = bytes;
this.p = p;
this.end = end;
}
public CaseInsensitiveBytesHashEntry() {
super();
bytes = null;
p = end = 0;
}
public boolean equals(byte[]bytes, int p, int end) {
return caseInsensitiveEquals(this.bytes, this.p, this.end, bytes, p, end);
}
}
public static int hashCode(byte[]bytes, int p, int end) {
int key = 0;
while (p < end) key = ((key << 16) + (key << 6) - key) + (int)(ToLowerCaseTable[bytes[p++] & 0xff]);
key = key + (key >> 5);
return key;
}
public V put(byte[]bytes, V value) {
return put(bytes, 0, bytes.length, value);
}
public V put(byte[]bytes, int p, int end, V value) {
checkResize();
int hash = hashValue(hashCode(bytes, p, end));
int i = bucketIndex(hash, table.length);
for (CaseInsensitiveBytesHashEntry<V> entry = (CaseInsensitiveBytesHashEntry<V>)table[i]; entry != null; entry = (CaseInsensitiveBytesHashEntry<V>)entry.next) {
if (entry.hash == hash && entry.equals(bytes, p, end)) {
entry.value = value;
return value;
}
}
table[i] = new CaseInsensitiveBytesHashEntry<V>(hash, table[i], value, bytes, p, end, head);
size++;
return null;
}
public void putDirect(byte[]bytes, V value) {
putDirect(bytes, 0, bytes.length, value);
}
public void putDirect(byte[]bytes, int p, int end, V value) {
checkResize();
final int hash = hashValue(hashCode(bytes, p, end));
final int i = bucketIndex(hash, table.length);
table[i] = new CaseInsensitiveBytesHashEntry<V>(hash, table[i], value, bytes, p, end, head);
size++;
}
public V get(byte[]bytes) {
return get(bytes, 0, bytes.length);
}
public V get(byte[]bytes, int p, int end) {
int hash = hashValue(hashCode(bytes, p, end));
for (CaseInsensitiveBytesHashEntry<V> entry = (CaseInsensitiveBytesHashEntry<V>)table[bucketIndex(hash, table.length)]; entry != null; entry = (CaseInsensitiveBytesHashEntry<V>)entry.next) {
if (entry.hash == hash && entry.equals(bytes, p, end)) return entry.value;
}
return null;
}
public V delete(byte[]bytes) {
return delete(bytes, 0, bytes.length);
}
public V delete(byte[]bytes, int p, int end) {
int hash = hashValue(hashCode(bytes, p, end));
int i = bucketIndex(hash, table.length);
CaseInsensitiveBytesHashEntry<V> entry = (CaseInsensitiveBytesHashEntry<V>)table[i];
if (entry == null) return null;
if (entry.hash == hash && entry.equals(bytes, p, end)) {
table[i] = entry.next;
size--;
entry.remove();
return entry.value;
}
for (; entry.next != null; entry = (CaseInsensitiveBytesHashEntry<V>)entry.next) {
HashEntry<V> tmp = entry.next;
if (tmp.hash == hash && entry.equals(bytes, p, end)) {
entry.next = entry.next.next;
size--;
tmp.remove();
return tmp.value;
}
}
return null;
}
@Override
public CaseInsensitiveBytesHashEntryIterator entryIterator() {
return new CaseInsensitiveBytesHashEntryIterator();
}
public class CaseInsensitiveBytesHashEntryIterator extends HashEntryIterator {
@Override
public CaseInsensitiveBytesHashEntry<V> next() {
return (CaseInsensitiveBytesHashEntry<V>)super.next();
}
}
public static boolean caseInsensitiveEquals(byte[]bytes, int p, int end, byte[]oBytes, int oP, int oEnd) {
if (oEnd - oP != end - p) return false;
if (oBytes == bytes) return true;
int q = oP;
while (q < oEnd) if (ToLowerCaseTable[oBytes[q++] & 0xff] != ToLowerCaseTable[bytes[p++] & 0xff]) return false;
return true;
}
public static boolean caseInsensitiveEquals(byte[]bytes, byte[] oBytes) {
return caseInsensitiveEquals(bytes, 0, bytes.length, oBytes, 0, oBytes.length);
}
}