pub struct TokenMapper {
pub token_map: HashMap<TokenVector, TokenId>,
pub reverse_token_map: HashMap<TokenId, TokenVector>,
/* private fields */
}Expand description
A struct to map tokens to unique identifiers and vice versa.
This structure is responsible for maintaining a bidirectional mapping between tokens (represented as character vectors) and their unique IDs. It also provides utility methods to query and manage these mappings.
Fields§
§token_map: HashMap<TokenVector, TokenId>A map of token character vectors to their unique IDs.
reverse_token_map: HashMap<TokenId, TokenVector>A reverse map of unique IDs back to their token character vectors.
Implementations§
Source§impl TokenMapper
impl TokenMapper
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new instance of TokenMapper.
Initializes empty maps for tokens and reverse lookups, and sets the
starting next_id to 0.
Sourcepub fn upsert_token(&mut self, token: &str) -> TokenId
pub fn upsert_token(&mut self, token: &str) -> TokenId
Adds a token to the map if it doesn’t already exist, and returns its unique ID.
If the token is already present in the token_map, its existing ID is returned.
Otherwise, a new ID is generated, stored, and returned.
§Arguments
token- A reference to the token string to add or look up.
§Returns
- A unique ID for the token.
Sourcepub fn get_token_id(&self, token: &TokenRef) -> Option<TokenId>
pub fn get_token_id(&self, token: &TokenRef) -> Option<TokenId>
Sourcepub fn get_token_by_id(&self, token_id: TokenId) -> Option<String>
pub fn get_token_by_id(&self, token_id: TokenId) -> Option<String>
Sourcepub fn get_token_count(&self) -> usize
pub fn get_token_count(&self) -> usize
Auto Trait Implementations§
impl Freeze for TokenMapper
impl RefUnwindSafe for TokenMapper
impl Send for TokenMapper
impl Sync for TokenMapper
impl Unpin for TokenMapper
impl UnwindSafe for TokenMapper
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more