How to improve code?

Hello.
I create code for grouping (clustering) modbus registers.
Register have address (offset) and reg. count (length).
I wrote this code.
What can I improve?

Rust Playground

    fn group_vec(input : &Vec<(u32,u32)>, cluster : &mut std::vec::Vec<(u32, u32)>, rest : &mut std::vec::Vec<(u32, u32)>) -> bool
    {
            rest.clear();
            cluster.clear();

            for e in input.iter()
            {

                let offset = e.0;
                let count = e.1;
                println!("start : {:?}, count : {:?}", offset, count);
                
                //first element
                if cluster.len() == 0
                {
                    cluster.push((offset, count));
                    continue;
                }

                if let Some(last) = cluster.last_mut()
                {
                    let last_start : u32 = last.0;
                    let last_end : u32 = last_start + last.1;
                    let cur_start : u32 = offset;

                    let dist_to_prev = cur_start - last_end;
                    println!("last_start : {:?}", last_start);
                    println!("last_end : {:?}", last_end);
                    println!("dist_to_prev : {:?}", dist_to_prev);

                    if dist_to_prev <= 2 //max distance
                    {
                        println!("push: {:?}", (offset, count));
                        cluster.push((offset, count));
                    }
                    else
                    {
                        rest.push((offset, count));
                    }
                }
            }

            return rest.len() != 0;
    }
        


    fn main() 
    {   //Modbus registers 
        //registers - address offset, length
        let mut registers_map = HashMap::new();

        registers_map.insert(0, 2);
        registers_map.insert(2, 4);
        registers_map.insert(7, 1);
        registers_map.insert(20, 2);
        registers_map.insert(23, 1);
        registers_map.insert(27, 1);
        registers_map.insert(100, 1);

        let mut input: Vec<_> = registers_map.iter().map(|(a, b)| (*a, *b)).collect();
        input.sort_by(|a, b| a.partial_cmp(b).unwrap());

        println!("input : {:?}", input);
        
        let mut cluster : Vec<(u32,u32)> = Vec::new();
        let mut rest : Vec<(u32,u32)> = Vec::new();
        let mut all_clusters = Vec::new();

        let res = group_vec(&input, &mut cluster, &mut rest);
        all_clusters.push(cluster.clone());

        println!("cluster : {:?}", cluster);
        println!("rest : {:?}", rest);
        
        if res
        {
            loop
            {
                let input = rest.clone();

                let res = group_vec(&input, &mut cluster, &mut rest);
                println!("cluster : {:?}", cluster);
                println!("rest : {:?}", rest);
                all_clusters.push(cluster.clone());

                if !res
                {
                    break;
                }
            }
        }
        println!("all_clusters : {:?}", all_clusters);

    }

Having written a grouping algorithm for a Rust application that performs modbus requests in a commercial setting, I know a fair bit about this. My first thing to try was this:

for i in 0..100 {
    registers_map.insert(i, i+1); // lots of length-one requests
}

This causes a panic on line 29 (playground). The reason I tried this is that I know that modbus devices typically have a maximum length on the request size, and it doesn't look like it handles this.

Additionally I will point out that the time needed to perform a modbus requests often follows the form of a large fixed cost plus a linear cost in the number of registers requested. If you can estimate these two costs, there's a clever dynamic programming algorithm that groups requests together in the way that minimizes the total cost based on these costs. Afaik the costs are around 25 or 30 ms fixed and 5 ms per register.

Main purpose of my question is to embellish my code.
As for the algorithm, I wonder if I can use the DBSCAN algorithm.

A quick view at wikipedia on DBSCAN doesn't look like something that would help much here.

I used recursion.
Rust playground

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.