Package com.alibaba.fastjson2
Class SymbolTable
java.lang.Object
com.alibaba.fastjson2.SymbolTable
Symbol table for fast name lookup.
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.
- Since:
- 2.0.58
-
Constructor Summary
ConstructorsConstructorDescriptionSymbolTable(Class<?>... input) Create a symbol table from class names.SymbolTable(String... input) Create a symbol table from string names. -
Method Summary
Modifier and TypeMethodDescriptionlonggetHashCode(int ordinal) Get the hash code of a name by its ordinal.getName(int ordinal) Get the name by its ordinal.getNameByHashCode(long hashCode) Get the name by its hash code.intgetOrdinal(String name) Get the ordinal of a name.intgetOrdinalByHashCode(long hashCode) Get the ordinal of a name by its hash code.longGet the 64-bit hash code of this symbol table.intsize()Get the number of names in this symbol table.
-
Constructor Details
-
SymbolTable
Create a symbol table from class names.- Parameters:
input- classes whose names will be added to the symbol table- Since:
- 2.0.58
-
SymbolTable
Create a symbol table from string names.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".
- Parameters:
input- names to be added to the symbol table
-
-
Method Details
-
size
public int size()Get the number of names in this symbol table.- Returns:
- the number of names
-
hashCode64
public long hashCode64()Get the 64-bit hash code of this symbol table.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.
- Returns:
- the 64-bit hash code of this symbol table
-
getNameByHashCode
Get the name by its hash code.- Parameters:
hashCode- the FNV-1a 64-bit hash code of the name- Returns:
- the name if found,
nullotherwise
-
getOrdinalByHashCode
public int getOrdinalByHashCode(long hashCode) Get the ordinal of a name by its hash code.- Parameters:
hashCode- the FNV-1a 64-bit hash code of the name- Returns:
- the ordinal (1-based) if found, -1 otherwise
-
getOrdinal
Get the ordinal of a name.- Parameters:
name- the name to look up- Returns:
- the ordinal (1-based) if found, -1 otherwise
-
getName
Get the name by its ordinal.- Parameters:
ordinal- the ordinal (1-based) of the name- Returns:
- the name at the specified ordinal
- Throws:
ArrayIndexOutOfBoundsException- if the ordinal is invalid
-
getHashCode
public long getHashCode(int ordinal) Get the hash code of a name by its ordinal.- Parameters:
ordinal- the ordinal (1-based) of the name- Returns:
- the FNV-1a 64-bit hash code of the name at the specified ordinal
- Throws:
ArrayIndexOutOfBoundsException- if the ordinal is invalid
-