Skip to content

Commit

Permalink
find methods
Browse files Browse the repository at this point in the history
  • Loading branch information
KGrewal1 committed Nov 28, 2024
1 parent 0e38d7f commit eadb271
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions tree_arena/src/tree_arena_unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ impl<T> DataMap<T> {
/// Returns a shared reference to the item if present.
///
/// Time Complexity O(1)
fn find(&self, id: impl Into<NodeId>) -> Option<ArenaRef<'_, T>> {
let id: NodeId = id.into();
fn find_inner(&self, id: NodeId) -> Option<ArenaRef<'_, T>> {
let parent_id = *self.parents.get(&id)?;
let node_cell = self.items.get(&id)?;

Expand Down Expand Up @@ -160,9 +159,7 @@ impl<T> DataMap<T> {
/// Returns a mutable reference to the item if present.
///
/// Time Complexity O(1)

fn find_mut(&mut self, id: impl Into<NodeId>) -> Option<ArenaMut<'_, T>> {
let id: NodeId = id.into();
fn find_mut_inner(&mut self, id: NodeId) -> Option<ArenaMut<'_, T>> {
let parent_id = *self.parents.get(&id)?;
let node_cell = self.items.get(&id)?;

Expand Down Expand Up @@ -198,7 +195,7 @@ impl<T> DataMap<T> {
/// If `start_id` is Some, the path ends just before that id instead; `start_id` is not included.
///
/// If there is no path from `start_id` to id, returns the empty vector.
fn get_id_path(&self, id: impl Into<NodeId>, start_id: Option<NodeId>) -> Vec<NodeId> {
fn get_id_path(&self, id: NodeId, start_id: Option<NodeId>) -> Vec<NodeId> {
let id: NodeId = id.into();
let mut path = Vec::new();

Expand Down Expand Up @@ -264,15 +261,15 @@ impl<T> TreeArena<T> {
///
/// O(1).
pub fn find(&self, id: impl Into<NodeId>) -> Option<ArenaRef<'_, T>> {
self.data_map.find(id)
self.data_map.find_inner(id.into())
}

/// Find an item in the tree.
///
/// Returns a mutable reference to the item if present.
pub fn find_mut(&mut self, id: impl Into<NodeId>) -> Option<ArenaMut<'_, T>> {
// safe as derived from the arena itself and has assoc lifetime with the arena
self.data_map.find_mut(id)
self.data_map.find_mut_inner(id.into())
}

/// Construct the path of items from the given item to the root of the tree.
Expand All @@ -282,7 +279,7 @@ impl<T> TreeArena<T> {
///
/// If the id is not in the tree, returns an empty vector.
pub fn get_id_path(&self, id: impl Into<NodeId>) -> Vec<NodeId> {
self.data_map.get_id_path(id, None)
self.data_map.get_id_path(id.into(), None)
}
}

Expand Down Expand Up @@ -325,7 +322,7 @@ impl<'arena, T> ArenaRefChildren<'arena, T> {
}
}

/// returns true if there is a child with the given id
/// Returns true if there is a child with the given id
pub fn has_child(&self, id: impl Into<NodeId>) -> bool {
let child_id = id.into();
let parent_id = self.id;
Expand All @@ -342,7 +339,7 @@ impl<'arena, T> ArenaRefChildren<'arena, T> {
pub fn get_child(&self, id: impl Into<NodeId>) -> Option<ArenaRef<'_, T>> {
let id = id.into();
if self.has_child(id) {
self.parent_arena.find(id)
self.parent_arena.find_inner(id)
} else {
None
}
Expand All @@ -355,7 +352,7 @@ impl<'arena, T> ArenaRefChildren<'arena, T> {
pub fn into_child(self, id: impl Into<NodeId>) -> Option<ArenaRef<'arena, T>> {
let id = id.into();
if self.has_child(id) {
self.parent_arena.find(id)
self.parent_arena.find_inner(id)
} else {
None
}
Expand All @@ -373,7 +370,7 @@ impl<'arena, T> ArenaRefChildren<'arena, T> {
let id: NodeId = id.into();

if self.is_descendant(id) {
self.parent_arena.find(id)
self.parent_arena.find_inner(id)
} else {
None
}
Expand Down Expand Up @@ -437,7 +434,7 @@ impl<'arena, T> ArenaMutChildren<'arena, T> {
pub fn get_child(&self, id: impl Into<NodeId>) -> Option<ArenaRef<'_, T>> {
let id = id.into();
if self.has_child(id) {
self.parent_arena.find(id)
self.parent_arena.find_inner(id)
} else {
None
}
Expand All @@ -451,7 +448,7 @@ impl<'arena, T> ArenaMutChildren<'arena, T> {
let id = id.into();
if self.has_child(id) {
// safe as we check the node is a direct child node
self.parent_arena.find_mut(id)
self.parent_arena.find_mut_inner(id)
} else {
None
}
Expand All @@ -464,7 +461,7 @@ impl<'arena, T> ArenaMutChildren<'arena, T> {
pub fn into_child(self, id: impl Into<NodeId>) -> Option<ArenaRef<'arena, T>> {
let id = id.into();
if self.has_child(id) {
self.parent_arena.find(id)
self.parent_arena.find_inner(id)
} else {
None
}
Expand All @@ -478,7 +475,7 @@ impl<'arena, T> ArenaMutChildren<'arena, T> {
let id = id.into();
if self.has_child(id) {
// safe as we check the node is a direct child node
self.parent_arena.find_mut(id)
self.parent_arena.find_mut_inner(id)
} else {
None
}
Expand Down Expand Up @@ -571,7 +568,7 @@ impl<'arena, T> ArenaMutChildren<'arena, T> {
pub fn find(self, id: impl Into<NodeId>) -> Option<ArenaRef<'arena, T>> {
let id = id.into();
if self.is_descendant(id) {
self.parent_arena.find(id)
self.parent_arena.find_inner(id)
} else {
None
}
Expand All @@ -588,7 +585,7 @@ impl<'arena, T> ArenaMutChildren<'arena, T> {
let id = id.into();
if self.is_descendant(id) {
// safe as we check the node is a descendant
self.parent_arena.find_mut(id)
self.parent_arena.find_mut_inner(id)
} else {
None
}
Expand Down

0 comments on commit eadb271

Please sign in to comment.