【问题标题】:Incrementally build data structures which reference the same data增量构建引用相同数据的数据结构
【发布时间】:2017-12-28 18:58:44
【问题描述】:

我有一个EntityMap 对象,它处理空间索引任何可以有边界框的东西。我已经以这样一种方式实现了它,它存储对对象的引用而不是拥有的值(这可能不是最好的方法,但我认为将其更改为拥有的值只会改变我的问题)。

我目前正在尝试做的是将对象添加到向量中,使它们不会与之前添加到向量中的任何内容发生冲突。以下是我刚才描述的伪锈:

let mut final_moves = Vec::new();
let mut move_map = EntityMap::new();
for m in moves.into_iter() {
    let close_moves = move_map.find_intersecting(m.bounding_box());
    let new_move = m.modify_until_not_intersecting(close_moves);
    final_moves.push(new_move);
    move_map.insert(final_moves.last().unwrap());
}

final_moves 是函数返回的内容。这里的问题是我可变地借用final_moves,但我想将对其对象的引用存储在move_map 中。我不知道如何解决的冲突是我想同时增量构建final_movesmove_map,但这似乎要求它们都是可变的,这会阻止我从@借用数据987654328@.

如何重组我的代码以实现我的目标?

【问题讨论】:

    标签: rust


    【解决方案1】:

    您可以将move_mapfinal_moves 的类型更改为包含Rc<Move> 而不仅仅是Move

    let mut final_moves = Vec::new();
    let mut move_map = EntityMap::new();
    for m in moves.into_iter() {
        let close_moves = move_map.find_intersecting(m.bounding_box());
        let new_move = Rc::new(m.modify_until_not_intersecting(close_moves));
        final_moves.push(Rc::clone(new_move));
        move_map.insert(new_move);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多