This is the java code which I am trying to convert in rust for practice. but struck in one error.
public class Path {
public String word;
public Path next;
public int length;
}
public class Nodemapper {
public Category category = null;
public int height = MagicNumbers.max_graph_height;
public StarBindings starBindings = null;
public HashMap<String, Nodemapper> map = null;
public String key = null;
public Nodemapper value = null;
public boolean shortCut = false;
public ArrayList<String> sets;
}
void addPath(Nodemapper node, Path path, Category category) {
if (path == null) {
node.category = category;
node.height = 0;
}
else if (enableShortCuts && thatStarTopicStar(path)) {
node.category = category;
node.height = Math.min(4, node.height);
node.shortCut = true;
}
else if (NodemapperOperator.containsKey(node, path.word)) {
if (path.word.startsWith("<SET>")) addSets(path.word, bot, node);
Nodemapper nextNode = NodemapperOperator.get(node, path.word);
addPath(nextNode, path.next, category);
int offset = 1;
if (path.word.equals("#") || path.word.equals("^")) offset = 0;
node.height = Math.min(offset + nextNode.height, node.height);
}
else {
Nodemapper nextNode = new Nodemapper();
if (path.word.startsWith("<SET>")) {
addSets(path.word, bot, node);
}
if (node.key != null) {
NodemapperOperator.upgrade(node);
upgradeCnt++;
}
NodemapperOperator.put(node, path.word, nextNode);
addPath(nextNode, path.next, category);
int offset = 1;
if (path.word.equals("#") || path.word.equals("^")) offset = 0;
node.height = Math.min(offset + nextNode.height, node.height);
}
}
This is the code in Rust .
pub fn add_path_to_node(nodes : &mut HashMap<isize, Nodemapper> ,nodeIdx:isize, path: &mut Path, pathindex : isize, category:Category) -> usize
{
let mut pathword = path.get_word(pathindex).to_string();
let node = nodes.get_mut(&nodeIdx).unwrap();
if GraphMaster::that_star_topic_star(&path,pathindex as usize)
{
//let node = nodes.get_mut(&nodeIdx).unwrap();
node.category = Some(category);
node.height = std::cmp::min(4, node.height);
node.short_cut = true;
return node.height;
}
// let node = self.get_node(&nodeIdx);
if NodemapperOperator::contains_key(node, &pathword) {
if pathword.starts_with("<SET>"){
//add sets
}
let nextNodeIdx = NodemapperOperator::get(node, &pathword);
let nxh = GraphMaster::add_path_to_node(nodes,nextNodeIdx as isize, path,pathindex+1, category);
let mut offset = 1;
if pathword == "#" || pathword == "^" { offset = 0; }
let nh = std::mem::replace(&mut node.height, 0);
node.height = std::cmp::min(offset + nxh, nh);
return node.height;
}
else{
let mut nextnodeidx = nodes.len() as isize;
let n = Nodemapper::new();
nodes.insert(nextnodeidx, n);
//(self.nodeIndex.nodes.get_mut(&l).unwrap(),l)
//let (nextNode,nextnodeidx) = GraphMaster::new_node(graph);
if pathword.starts_with("<SET>") {
//addSets(path.word, bot, node);
}
if node.key.is_some() {
// NodemapperOperator::upgrade(node);
// upgradeCnt++;
}
//NodemapperOperator::put(node, pathword, nextNode);
let nxh = GraphMaster::add_path_to_node(nodes, nextnodeidx , path,pathindex+1, category);
let mut offset = 1;
if pathword == "#" || pathword == "^" { offset = 0; }
let nh = node.height.to_owned();
node.height = std::cmp::min(offset + nxh, nh);
return node.height;
}
return 0 as usize;
}
But i'm getting this error: cannot borrow *nodes
as mutable more than once at a time label