refactor: op group -> history cache

This commit is contained in:
Zixuan Chen 2024-07-18 15:44:50 +08:00
parent 06cd6b577d
commit 3d516a3e18
No known key found for this signature in database

View file

@ -27,7 +27,7 @@ use crate::{
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct ContainerHistoryCache { pub(crate) struct ContainerHistoryCache {
arena: SharedArena, arena: SharedArena,
groups: FxHashMap<ContainerIdx, OpGroup>, groups: FxHashMap<ContainerIdx, HistoryCache>,
} }
impl ContainerHistoryCache { impl ContainerHistoryCache {
@ -67,18 +67,18 @@ impl ContainerHistoryCache {
self.groups self.groups
.entry(container_idx) .entry(container_idx)
.or_insert_with(|| match op.container.get_type() { .or_insert_with(|| match op.container.get_type() {
ContainerType::Map => OpGroup::Map(MapOpGroup::default()), ContainerType::Map => HistoryCache::Map(MapOpGroup::default()),
ContainerType::MovableList => { ContainerType::MovableList => {
OpGroup::MovableList(MovableListOpGroup::new(self.arena.clone())) HistoryCache::MovableList(MovableListOpGroup::new(self.arena.clone()))
} }
ContainerType::Tree => OpGroup::Tree(TreeOpGroup::default()), ContainerType::Tree => HistoryCache::Tree(TreeOpGroup::default()),
_ => unreachable!(), _ => unreachable!(),
}); });
manager.insert(&rich_op) manager.insert(&rich_op)
} }
} }
pub(crate) fn get(&self, container_idx: &ContainerIdx) -> Option<&OpGroup> { pub(crate) fn get(&self, container_idx: &ContainerIdx) -> Option<&HistoryCache> {
self.groups.get(container_idx) self.groups.get(container_idx)
} }
@ -89,7 +89,7 @@ impl ContainerHistoryCache {
self.groups self.groups
.get(container_idx) .get(container_idx)
.and_then(|group| match group { .and_then(|group| match group {
OpGroup::MovableList(movable_list) => Some(movable_list), HistoryCache::MovableList(movable_list) => Some(movable_list),
_ => None, _ => None,
}) })
} }
@ -98,7 +98,7 @@ impl ContainerHistoryCache {
self.groups self.groups
.get(container_idx) .get(container_idx)
.and_then(|group| match group { .and_then(|group| match group {
OpGroup::Tree(tree) => Some(tree), HistoryCache::Tree(tree) => Some(tree),
_ => None, _ => None,
}) })
} }
@ -108,7 +108,7 @@ impl ContainerHistoryCache {
self.groups self.groups
.get(container_idx) .get(container_idx)
.and_then(|group| match group { .and_then(|group| match group {
OpGroup::Map(map) => Some(map), HistoryCache::Map(map) => Some(map),
_ => None, _ => None,
}) })
} }
@ -116,21 +116,21 @@ impl ContainerHistoryCache {
#[enum_dispatch(OpGroupTrait)] #[enum_dispatch(OpGroupTrait)]
#[derive(Debug, Clone, EnumAsInner)] #[derive(Debug, Clone, EnumAsInner)]
pub(crate) enum OpGroup { pub(crate) enum HistoryCache {
Map(MapOpGroup), Map(MapOpGroup),
Tree(TreeOpGroup), Tree(TreeOpGroup),
MovableList(MovableListOpGroup), MovableList(MovableListOpGroup),
} }
impl OpGroup { impl HistoryCache {
fn fork(&self, a: &SharedArena) -> Self { fn fork(&self, a: &SharedArena) -> Self {
match self { match self {
OpGroup::Map(m) => OpGroup::Map(m.clone()), HistoryCache::Map(m) => HistoryCache::Map(m.clone()),
OpGroup::Tree(t) => OpGroup::Tree(TreeOpGroup { HistoryCache::Tree(t) => HistoryCache::Tree(TreeOpGroup {
ops: t.ops.clone(), ops: t.ops.clone(),
tree_for_diff: Arc::new(Mutex::new(Default::default())), tree_for_diff: Arc::new(Mutex::new(Default::default())),
}), }),
OpGroup::MovableList(m) => OpGroup::MovableList(MovableListOpGroup { HistoryCache::MovableList(m) => HistoryCache::MovableList(MovableListOpGroup {
arena: a.clone(), arena: a.clone(),
elem_mappings: m.elem_mappings.clone(), elem_mappings: m.elem_mappings.clone(),
pos_to_elem: m.pos_to_elem.clone(), pos_to_elem: m.pos_to_elem.clone(),