I was recently told I need to change my coding style. An Interviewer told me he would like to see "more formalized class structure and organization". I have attached few of my cpp files.
if you can you review it and give me your feedback on the things I can improve that will be extremely helpful.
Challenge:
Given n robots (n different paths), m nodes, how much time all the robots requires to complete the circuit. If 2 robots arrive at a node, the 2nd robot should wait till the 1st robot completes its task.
File structure:
├── Solution
│ ├── data
│ │ ├── ***.csv
│ │ ├── ***.csv
│ │ ├── ***.csv
│ ├── include
│ │ ├── robot.h
│ │ ├── node.h
│ │ ├── simulation.h
│ ├── output
│ │ ├── ***.csv
│ │ ├── ***.csv
│ ├── src
│ │ ├── main.cpp
│ │ ├── planner.cpp
│ │ ├── read_write.cpp
│ │ ├── simulation.cpp
│ ├── test
│ │ ├── simulationTest.cpp
│ ├── CMakeLists.txt
│ ├── README.md
simulation.h
#pragma once
...//all headers
#include "robot.h"
#include "node.h"
class simulation{
private:
std::string robots_ip_string;
std::string paths_ip_string;
std::string nodes_ip_string;
std::string time_op_string;
std::string visits_op_string;
std::vector<robot*> robots; //stores the robot info
std::unordered_map<int, node*> nodes;
std::unordered_map<int, std::vector<int>> paths;
public:
simulation();
~simulation();
void read_robots_input();
void read_paths_input();
void read_nodes_input();
void print_robots();
void print_nodes();
void print_paths();
void run_simulation();
void simulation_time();
void visited_node_info();
std::vector<robot*> get_robots();
};
robot.h
#pragma once
struct robot{
int robot_id;
std::string robot_type;
int robot_speed;
std::vector<int> path_to_follow;
int path_length;
int run_time;
};
planner.cpp
#include "simulation.h"
struct Path_Queue_Comparator{
bool operator() (std::pair<robot*,int> const& r1, std::pair<robot*,int> const& r2){
if (r1.first->run_time == r2.first->run_time)
return (r2.first->robot_type == "organizer") ? true : false;
return r1.first->run_time > r2.first->run_time;
}
};
void simulation::run_simulation(){
std::priority_queue< std::pair<robot*,int>, std::vector<std::pair<robot*,int>>, Path_Queue_Comparator> path_q;
for(auto r: robots)
path_q.push({r, 0});
while(!path_q.empty()){
std::pair<robot*,int> temp = path_q.top();
robot* current_state = temp.first;
int current_index = temp.second;
path_q.pop();
int next_index = current_index+1;
if(current_index < current_state->path_length){
int node_idx = current_state->path_to_follow[current_index];
int wait_time;
int task_time = (current_state->robot_type == "organizer") ?
nodes[current_state->path_to_follow[current_index]]->task_time_organizer_robot
: nodes[current_state->path_to_follow[current_index]]->task_time_mover_robot;
if (current_state->run_time >= nodes[node_idx]->leave_time){
wait_time = 0;
nodes[node_idx]->entry_time = current_state->run_time;
nodes[node_idx]->leave_time = current_state->run_time + task_time;
}
else{
wait_time = nodes[node_idx]->leave_time - current_state->run_time;
nodes[node_idx]->entry_time = current_state->run_time + wait_time;
nodes[node_idx]->leave_time = current_state->run_time + wait_time + task_time;
}
current_state->run_time += wait_time;
current_state->run_time += task_time;
if(next_index < current_state->path_to_follow.size())
current_state->run_time += current_state->robot_speed;
nodes[node_idx]->visited_robots.insert(current_state);
path_q.push({current_state, next_index});
}
}
}
This is how I usually code (other files are similar). Please let me know the things I need to change in my coding style! It will be extremely helpful.
Thanks