RbxTree robustness

* delete_instance is no longer O(n)
* renamed get_instance to get_instance_and_descendants, which is more accurate
This commit is contained in:
Lucien Greathouse
2018-06-24 20:26:58 -07:00
parent e1c420d37d
commit 20c9c89b27
2 changed files with 14 additions and 9 deletions

View File

@@ -68,13 +68,18 @@ impl RbxTree {
let mut ids_to_visit = vec![id]; let mut ids_to_visit = vec![id];
let mut ids_deleted = Vec::new(); let mut ids_deleted = Vec::new();
for instance in self.instances.values_mut() { // We only need to explicitly remove a child from the first instance we
match instance.children.iter().position(|&v| v == id) { // delete, since all others will descend from this instance.
Some(index) => { let parent_id = match self.instances.get(&id) {
instance.children.remove(index); Some(instance) => instance.parent,
}, None => None,
None => {}, };
}
if let Some(parent_id) = parent_id {
let mut parent = self.instances.get_mut(&parent_id).unwrap();
let index = parent.children.iter().position(|&v| v == id).unwrap();
parent.children.remove(index);
} }
loop { loop {
@@ -95,7 +100,7 @@ impl RbxTree {
ids_deleted ids_deleted
} }
pub fn get_instance<'a, 'b, T>(&'a self, id: Id, output: &'b mut HashMap<Id, T>) pub fn get_instance_and_descendants<'a, 'b, T>(&'a self, id: Id, output: &'b mut HashMap<Id, T>)
where T: From<&'a RbxInstance> where T: From<&'a RbxInstance>
{ {
let mut ids_to_visit = vec![id]; let mut ids_to_visit = vec![id];

View File

@@ -187,7 +187,7 @@ impl Server {
let mut instances = HashMap::new(); let mut instances = HashMap::new();
for requested_id in &requested_ids { for requested_id in &requested_ids {
rbx_session.tree.get_instance(*requested_id, &mut instances); rbx_session.tree.get_instance_and_descendants(*requested_id, &mut instances);
} }
Response::json(&ReadResponse { Response::json(&ReadResponse {