pub struct BlockIndexMaps {
pub block_name_index: BTreeMap<String, Vec<(usize, BlockKind, BlockId)>>,
pub unit_index_map: BTreeMap<usize, Vec<(Option<String>, BlockKind, BlockId)>>,
pub block_kind_index: HashMap<BlockKind, Vec<(usize, Option<String>, BlockId)>>,
pub block_id_index: HashMap<BlockId, (usize, Option<String>, BlockKind)>,
}Expand description
BlockIndexMaps provides efficient lookup of blocks by various indices.
Best practices for usage:
- block_name_index: Use when you want to find blocks by name (multiple blocks can share the same name)
- unit_index_index: Use when you want all blocks in a specific unit
- block_kind_index: Use when you want all blocks of a specific kind (e.g., all functions)
- block_id_index: Use for O(1) lookup of block metadata by BlockId
Important: The “name” field is optional since Root blocks and some other blocks may not have names.
Rationale for data structure choices:
- BTreeMap is used for name and unit indexes for better iteration and range queries
- HashMap is used for kind index since BlockKind doesn’t implement Ord
- HashMap is used for block_id_index (direct lookup by BlockId) for O(1) access
- Vec is used for values to handle multiple blocks with the same index (same name/kind/unit)
Fields§
§block_name_index: BTreeMap<String, Vec<(usize, BlockKind, BlockId)>>block_name -> Vec<(unit_index, block_kind, block_id)> Multiple blocks can share the same name across units or within the same unit
unit_index_map: BTreeMap<usize, Vec<(Option<String>, BlockKind, BlockId)>>unit_index -> Vec<(block_name, block_kind, block_id)> Allows retrieval of all blocks in a specific compilation unit
block_kind_index: HashMap<BlockKind, Vec<(usize, Option<String>, BlockId)>>block_kind -> Vec<(unit_index, block_name, block_id)> Allows retrieval of all blocks of a specific kind across all units
block_id_index: HashMap<BlockId, (usize, Option<String>, BlockKind)>block_id -> (unit_index, block_name, block_kind) Direct O(1) lookup of block metadata by ID
Implementations§
Source§impl BlockIndexMaps
impl BlockIndexMaps
Sourcepub fn insert_block(
&mut self,
block_id: BlockId,
block_name: Option<String>,
block_kind: BlockKind,
unit_index: usize,
)
pub fn insert_block( &mut self, block_id: BlockId, block_name: Option<String>, block_kind: BlockKind, unit_index: usize, )
Register a new block in all indexes
§Arguments
block_id: The unique block identifierblock_name: Optional name of the block (None for unnamed blocks)block_kind: The kind of block (Func, Class, Stmt, etc.)unit_index: The compilation unit index this block belongs to
Sourcepub fn find_by_name(&self, name: &str) -> Vec<(usize, BlockKind, BlockId)>
pub fn find_by_name(&self, name: &str) -> Vec<(usize, BlockKind, BlockId)>
Find all blocks with a given name (may return multiple blocks)
Returns a vector of (unit_index, block_kind, block_id) tuples
Sourcepub fn find_by_unit(
&self,
unit_index: usize,
) -> Vec<(Option<String>, BlockKind, BlockId)>
pub fn find_by_unit( &self, unit_index: usize, ) -> Vec<(Option<String>, BlockKind, BlockId)>
Find all blocks in a specific unit
Returns a vector of (block_name, block_kind, block_id) tuples
Sourcepub fn find_by_kind(
&self,
block_kind: BlockKind,
) -> Vec<(usize, Option<String>, BlockId)>
pub fn find_by_kind( &self, block_kind: BlockKind, ) -> Vec<(usize, Option<String>, BlockId)>
Find all blocks of a specific kind across all units
Returns a vector of (unit_index, block_name, block_id) tuples
Sourcepub fn find_by_kind_and_unit(
&self,
block_kind: BlockKind,
unit_index: usize,
) -> Vec<BlockId>
pub fn find_by_kind_and_unit( &self, block_kind: BlockKind, unit_index: usize, ) -> Vec<BlockId>
Find all blocks of a specific kind in a specific unit
Returns a vector of block_ids
Sourcepub fn get_block_info(
&self,
block_id: BlockId,
) -> Option<(usize, Option<String>, BlockKind)>
pub fn get_block_info( &self, block_id: BlockId, ) -> Option<(usize, Option<String>, BlockKind)>
Look up block metadata by BlockId for O(1) access
Returns (unit_index, block_name, block_kind) if found
Sourcepub fn block_count(&self) -> usize
pub fn block_count(&self) -> usize
Get total number of blocks indexed
Sourcepub fn unique_names_count(&self) -> usize
pub fn unique_names_count(&self) -> usize
Get the number of unique block names
Sourcepub fn contains_block(&self, block_id: BlockId) -> bool
pub fn contains_block(&self, block_id: BlockId) -> bool
Check if a block with the given ID exists
Trait Implementations§
Source§impl Clone for BlockIndexMaps
impl Clone for BlockIndexMaps
Source§fn clone(&self) -> BlockIndexMaps
fn clone(&self) -> BlockIndexMaps
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for BlockIndexMaps
impl Debug for BlockIndexMaps
Source§impl Default for BlockIndexMaps
impl Default for BlockIndexMaps
Source§fn default() -> BlockIndexMaps
fn default() -> BlockIndexMaps
Auto Trait Implementations§
impl Freeze for BlockIndexMaps
impl RefUnwindSafe for BlockIndexMaps
impl Send for BlockIndexMaps
impl Sync for BlockIndexMaps
impl Unpin for BlockIndexMaps
impl UnwindSafe for BlockIndexMaps
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more