How to correctly iterate all records of a structure with multiple depth levels in Rust?

Asked

Viewed 93 times

3

I would like to know how to iterate correctly in Rust all the results contained in a data structure so arranged:

struct Node {
    id: i64,
    nodes: Vec<Node>
}

Where records entered in this structure have various depth levels. Something like:

{id: 1, nodes: [
    {id: 2, nodes: [ 
        {id: 3, nodes: []}, 
        {id: 4, nodes: []},
        {id: 5, nodes: [
            {id: 6, nodes: []},
            {id: 7, nodes: [
                {id: 8, nodes: []},
                {id: 9, nodes: []}
            ]}
        ]}
    ]}
]};

From now on, thank you.

2 answers

1

If the idea is simply to iterate, I think a simpler solution is more valid.

Given the proposed modulation:

struct Node {
    id: i64,
    nodes: Vec<Node>,
}

We can simply build a function that takes a node (or a list of nodes, to support top level lists) and acts on that node. Then the function should follow to the list of nodes internal to the current node, repeating the process.

A flowchart representing this idea is set out below:

Fluxograma da ideia proposta

The proposed is then a recursive function. It could be represented as follows:

fn parse_nodes(nodes: &Vec<Node>) {
    nodes.iter()
        .map(|ref node| {
            let id = node.id;
            let len = node.nodes.len();
            println!("id: {} - child nodes: {}", id, len);

            parse_nodes(&node.nodes);
        })
        .count();
}

If we don’t care or can’t accept lists as the function’s initial argument, we can build the function as follows:

fn parse_node(node: &Node) {
    let id = node.id;
    let len = node.nodes.len();
    println!("id: {} - child nodes: {}", id, len);

    node.nodes
        .iter()
        .map(|ref node| parse_node(&node))
        .count();
}

With these two solutions we have managed to solve the proposed problem of iterate a structure as exposed.

Both solutions could use parallelism elements to decrease the running time of the parser. This answer will not address this approach, however. For more information on this, a good reference is chapter 16 of the book The Rust Programming Language (link).

1

I created a simple recursive function to handle the problem. And everything is ok now. I don’t know what my mistake was yesterday to create this topic, but I think it was some Rust typing problem.

The real problem I faced was a little different (the structure mentioned) from the question asked, but the essence was the same. And here’s my little solution:

use std::vec::Vec;

struct Node
{
       id: i64,
       nodes: Vec<Node>,
       focused: bool
}

struct Controller
{
    focused: i32
}

impl Controller
{
    fn get_focused(&mut self) -> i32
    {
        let nodes: Node = ....; // código suprimido. representado pelo JSON object acima citado, com adição do membro 'focused' na estrtura.

        for node in nodes.iter()
        {
            self.focused = self.node_iterator(node);
        }
        self.focused
    }

    fn node_iterator(&self, node: Node) -> i32
    {
        let mut focused: i32 = 0;

        if node.nodes.len() > 0
        {
            for n in node.nodes.iter()
            {
                if n.nodes.len() > 0
                {
                    focused = self.node_iterator(n);
                    if focused > 0
                    {
                        return focused;
                    }
                }
                else
                {
                    if n.focused == true
                    {
                        focused = n.id as i32;
                        return focused;
                    }
                }
            }
        }
        return 0;
    }
}


fn main()
{
    let mut controller = Controller{focused: 0};

    controller.get_focused();

    println!("{}", controller.focused);
}

If someone knows some more elegant way to deal with this problem, feel free to share.

  • My same question asked in the English OS here https://stackoverflow.com/questions/47797743/how-to-correctly-iterate-all-records-of-a-multi-level-depth-structure-in-rust . Note the reply given by @Shepmaster in his last comment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.