public final class SymbolTable
extends java.lang.Object
This class provides a way to efficiently map names (strings) to ordinals and vice versa. It uses FNV-1a hash algorithm for fast hashing and maintains sorted hash codes for binary search.
SymbolTable is designed to be immutable after construction, making it thread-safe.
| Modifier and Type | Field and Description |
|---|---|
private long |
hashCode64 |
private long[] |
hashCodes |
private long[] |
hashCodesOrigin |
private short[] |
mapping |
private java.lang.String[] |
names |
| Constructor and Description |
|---|
SymbolTable(java.lang.Class<?>... input)
Create a symbol table from class names.
|
SymbolTable(java.lang.String... input)
Create a symbol table from string names.
|
| Modifier and Type | Method and Description |
|---|---|
private static java.lang.String[] |
classNames(java.lang.Class<?>... input)
Extract class names from Class objects.
|
long |
getHashCode(int ordinal)
Get the hash code of a name by its ordinal.
|
java.lang.String |
getName(int ordinal)
Get the name by its ordinal.
|
java.lang.String |
getNameByHashCode(long hashCode)
Get the name by its hash code.
|
int |
getOrdinal(java.lang.String name)
Get the ordinal of a name.
|
int |
getOrdinalByHashCode(long hashCode)
Get the ordinal of a name by its hash code.
|
long |
hashCode64()
Get the 64-bit hash code of this symbol table.
|
int |
size()
Get the number of names in this symbol table.
|
private final java.lang.String[] names
private final long hashCode64
private final short[] mapping
private final long[] hashCodes
private final long[] hashCodesOrigin
public SymbolTable(java.lang.Class<?>... input)
input - classes whose names will be added to the symbol tablepublic SymbolTable(java.lang.String... input)
The names will be sorted and deduplicated. Each name is assigned a unique ordinal starting from 1. The ordinal 0 is reserved and means "not found".
input - names to be added to the symbol tableprivate static java.lang.String[] classNames(java.lang.Class<?>... input)
input - Class objectspublic int size()
public long hashCode64()
The hash code is computed from all the names in the symbol table. It can be used to quickly compare if two symbol tables have the same content.
public java.lang.String getNameByHashCode(long hashCode)
hashCode - the FNV-1a 64-bit hash code of the namenull otherwisepublic int getOrdinalByHashCode(long hashCode)
hashCode - the FNV-1a 64-bit hash code of the namepublic int getOrdinal(java.lang.String name)
name - the name to look uppublic java.lang.String getName(int ordinal)
ordinal - the ordinal (1-based) of the namejava.lang.ArrayIndexOutOfBoundsException - if the ordinal is invalidpublic long getHashCode(int ordinal)
ordinal - the ordinal (1-based) of the namejava.lang.ArrayIndexOutOfBoundsException - if the ordinal is invalid