mf_model/ops/
mul.rs

1use std::ops::Mul;
2
3use crate::{error::PoolResult, id_generator::IdGenerator, types::NodeId};
4
5use super::NodeRef;
6
7/// 为 NodeRef 实现自定义的 * 运算符,用于复制当前节点N次
8/// 当使用 * 运算符时,会将当前节点复制指定次数并添加到父节点中
9impl<'a> Mul<usize> for NodeRef<'a> {
10    type Output = PoolResult<NodeRef<'a>>;
11    fn mul(
12        self,
13        count: usize,
14    ) -> Self::Output {
15        // 获取当前节点
16        if let Some(current_node) = self.tree.get_node(&self.key.clone()) {
17            let mut nodes = Vec::new();
18            for _ in 0..count {
19                // 创建节点的副本
20                let mut node = current_node.as_ref().clone();
21                node.id = IdGenerator::get_id();
22                node.content = imbl::Vector::new();
23                nodes.push(node);
24            }
25            // 添加到当前节点的父节点中
26            if let Some(parent) = self.tree.get_parent_node(&self.key.clone()) {
27                self.tree.add_node(&parent.id, &nodes)?;
28            }
29        }
30        Ok(NodeRef::new(self.tree, self.key.clone()))
31    }
32}