Mercurial > hg
comparison rust/hg-core/src/dirstate_tree/dirstate_map.rs @ 47104:fdf6cfa0e254
dirstate-tree: Add copy_map_insert and copy_map_remove
Differential Revision: https://phab.mercurial-scm.org/D10488
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Mon, 12 Apr 2021 17:53:37 +0200 |
parents | 214ae40e136b |
children | ba17a2ee85ac |
comparison
equal
deleted
inserted
replaced
47103:214ae40e136b | 47104:fdf6cfa0e254 |
---|---|
87 } | 87 } |
88 } | 88 } |
89 | 89 |
90 /// This takes `root` instead of `&mut self` so that callers can mutate | 90 /// This takes `root` instead of `&mut self` so that callers can mutate |
91 /// other fields while the returned borrow is still valid | 91 /// other fields while the returned borrow is still valid |
92 fn get_node_mut<'tree>( | |
93 root: &'tree mut ChildNodes, | |
94 path: &HgPath, | |
95 ) -> Option<&'tree mut Node> { | |
96 let mut children = root; | |
97 let mut components = path.components(); | |
98 let mut component = | |
99 components.next().expect("expected at least one components"); | |
100 loop { | |
101 let child = children.get_mut(component)?; | |
102 if let Some(next_component) = components.next() { | |
103 component = next_component; | |
104 children = &mut child.children; | |
105 } else { | |
106 return Some(child); | |
107 } | |
108 } | |
109 } | |
110 | |
92 fn get_or_insert_node<'tree>( | 111 fn get_or_insert_node<'tree>( |
93 root: &'tree mut ChildNodes, | 112 root: &'tree mut ChildNodes, |
94 path: &HgPath, | 113 path: &HgPath, |
95 ) -> &'tree mut Node { | 114 ) -> &'tree mut Node { |
96 let mut child_nodes = root; | 115 let mut child_nodes = root; |
461 | 480 |
462 fn copy_map_get(&self, key: &HgPath) -> Option<&HgPathBuf> { | 481 fn copy_map_get(&self, key: &HgPath) -> Option<&HgPathBuf> { |
463 self.get_node(key)?.copy_source.as_ref() | 482 self.get_node(key)?.copy_source.as_ref() |
464 } | 483 } |
465 | 484 |
466 fn copy_map_remove(&mut self, _key: &HgPath) -> Option<HgPathBuf> { | 485 fn copy_map_remove(&mut self, key: &HgPath) -> Option<HgPathBuf> { |
467 todo!() | 486 let count = &mut self.nodes_with_copy_source_count; |
487 Self::get_node_mut(&mut self.root, key).and_then(|node| { | |
488 if node.copy_source.is_some() { | |
489 *count -= 1 | |
490 } | |
491 node.copy_source.take() | |
492 }) | |
468 } | 493 } |
469 | 494 |
470 fn copy_map_insert( | 495 fn copy_map_insert( |
471 &mut self, | 496 &mut self, |
472 _key: HgPathBuf, | 497 key: HgPathBuf, |
473 _value: HgPathBuf, | 498 value: HgPathBuf, |
474 ) -> Option<HgPathBuf> { | 499 ) -> Option<HgPathBuf> { |
475 todo!() | 500 let node = Self::get_or_insert_node(&mut self.root, &key); |
501 if node.copy_source.is_none() { | |
502 self.nodes_with_copy_source_count += 1 | |
503 } | |
504 node.copy_source.replace(value) | |
476 } | 505 } |
477 | 506 |
478 fn len(&self) -> usize { | 507 fn len(&self) -> usize { |
479 self.nodes_with_entry_count | 508 self.nodes_with_entry_count |
480 } | 509 } |