Code cleanup

This commit is contained in:
Noah L. Schrick 2022-03-09 15:27:14 -06:00
parent d53ee2d03d
commit 86e2dae4c6
6 changed files with 28 additions and 83 deletions

Binary file not shown.

Binary file not shown.

View File

@ -17,40 +17,6 @@ HL::HL()
lines = construct_HWprob();
}
std::map<int, Line> HL::get_map()
{
return line_holder;
}
void HL::print_sol()
{
auto sol = get_sol();
for(auto itr = sol.begin(); itr != sol.end(); itr++){
double start_range = std::get<0>(std::get<1>(*itr));
std::string start_print = std::to_string(start_range);
if(start_range == std::numeric_limits<double>::lowest())
start_print = "-inf";
double stop_range = std::get<1>(std::get<1>(*itr));
std::string stop_print = std::to_string(stop_range);
if(stop_range == std::numeric_limits<double>::max())
stop_print = "inf";
if (start_range == stop_range)
continue;
std::cout << "Line ID " << std::get<0>(*itr) << " visible from x=" <<
start_print << " to x=" << stop_print << std::endl;
}
std::cout << std::endl;
auto lines = get_lines();
for(Line line : lines){
std::cout << "Line ID " << line.get_id() << " visible from x=" <<
line.get_vis_start() << " to x=" << line.get_vis_end() << std::endl;
}
}
void print_sol(std::vector<Line>& lines)
{
for(auto itr = lines.begin(); itr != lines.end(); itr++){
@ -75,22 +41,24 @@ void print_sol(std::vector<Line>& lines)
//Divide and Conquer Strategy using recursive call to divide ls and merge using merge()
std::vector<Line> HL::gen_sol(std::vector<Line>& ls){
HL inst = *this;
auto line_holder = inst.get_map();
//Case 1: We need to divide more
if (ls.size() > 2){
//Create left and right half sets
int split = ceil(ls.size()/2);
std::vector<Line> lh;
std::vector<Line> rh;
//No index access for sets
//Get iterator for the split point location
auto end_itr = ls.begin();
std::advance(end_itr, split);
//Create left half
for(std::vector<Line>::iterator itr = ls.begin(); itr != end_itr; itr++){
lh.push_back(*itr);
}
//Create right half
for(std::vector<Line>::iterator itr = end_itr; itr != ls.end(); itr++){
rh.push_back(*itr);
}
@ -98,31 +66,22 @@ std::vector<Line> HL::gen_sol(std::vector<Line>& ls){
//Recursive call
auto merged = merge(inst.gen_sol(lh), inst.gen_sol(rh));
for (std::vector<Line>::iterator itr = merged.begin(); itr != merged.end(); itr++){
Line l1 = *itr;
std::vector<std::pair<int, std::pair<double, double>>>::iterator it = std::find_if(sol.begin(), sol.end(),
[l1](const std::pair<int, std::pair<double, double>>& p )
{ return (std::get<0>(p) == l1.get_id()); });
if(it != sol.end())
*it = std::make_pair(l1.get_id(), std::make_pair(l1.get_vis_start(), l1.get_vis_end()));
else
sol.push_back(std::make_pair(l1.get_id(), std::make_pair(l1.get_vis_start(), l1.get_vis_end())));
}
return merged;
}
//Set with 2 lines is a base case: both are visible at +-inf respectively, and intersection is where they change
//visibility
else if (ls.size() == 2){
//Get non-const iterators to the beginning and end of our vector
std::vector<Line>::iterator bit = ls.begin();
std::vector<Line>::iterator eit = std::prev(ls.end());
Line l1 = *(bit);
Line l2 = *(eit);
double isec = (l2.get_ycept() - l1.get_ycept())/(l1.get_slope() - l2.get_slope());
//Get intersection of our two lines
double isec = l1.get_isec(l2);
//If Line 1 has smaller slope, its visibility end in comparison to Line 2 is the intersection. Vice versa for Line 2.
if (l1.get_slope() < l2.get_slope()){
l1.set_vis_end(isec);
*bit = l1;
@ -136,14 +95,10 @@ std::vector<Line> HL::gen_sol(std::vector<Line>& ls){
*eit = l2;
}
//Insert partial solutions
sol.push_back(std::make_pair(l1.get_id(), std::make_pair(l1.get_vis_start(), l1.get_vis_end())));
sol.push_back(std::make_pair(l2.get_id(), std::make_pair(l2.get_vis_start(), l2.get_vis_end())));
return ls;
}
//Set with 1 line: Just return
//Set with 1 line: Just return - no comparisons to make at this point.
else{
return ls;
}
@ -187,10 +142,6 @@ std::vector<Line> HL::construct_HWprob(){
Line newline = Line(slope, ycept);
newline.set_id();
sorted_lines.insert(newline);
auto line_holder = get_map();
//line_holder[newline.get_id()] = newline;
line_holder.insert(std::pair<int, Line> (newline.get_id(), newline));
}
//Copy set to vector
@ -220,6 +171,7 @@ std::vector<Line> HL::construct_HWprob(){
std::vector<Line> merge(std::vector<Line> lh, std::vector<Line> rh){
std::vector<Line> merged;
//Get non-const iterators to the start of both vectors
std::vector<Line>::iterator litr = lh.begin();
std::vector<Line>::iterator ritr = rh.begin();
@ -246,11 +198,13 @@ std::vector<Line> merge(std::vector<Line> lh, std::vector<Line> rh){
if (l1.get_ycept() > l2.get_ycept()){
l2.set_vis_start(0);
l2.set_vis_end(0);
*ritr = l2;
merged.push_back(l1);
}
else{
l1.set_vis_start(0);
l1.set_vis_end(0);
*litr = l1;
merged.push_back(l2);
}
@ -289,33 +243,33 @@ std::vector<Line> HL::get_lines(){
return lines;
}
std::vector<std::pair<int, std::pair<double, double>>> HL::get_sol() const{
return sol;
}
void HL::set_sol(std::vector<std::pair<int, std::pair<double, double>>> &new_sol)
{
sol = new_sol;
}
std::vector<Line> remove_invis(std::vector<Line> &merged){
std::vector<Line> tmp_vis;
tmp_vis.push_back(merged.front());
merged.erase(merged.begin());
tmp_vis.push_back(merged.front());
merged.erase(merged.begin());
//Create separate visible line holder
std::vector<Line> tmp_vis;
//Since we call this function from a merge, we can safely add two elements.
tmp_vis.push_back(merged.front());
merged.erase(merged.begin());
tmp_vis.push_back(merged.front());
merged.erase(merged.begin());
int num_vis = 2;
for(Line line : merged){
//Loop through the remainder of the original vector
for(Line &line : merged){
//Pull out the back two lines
Line last = tmp_vis.at(num_vis -1);
Line seclast = tmp_vis.at(num_vis -2);
//Intersection of our "known" visible lines vs the intersection of the 2nd to last vis line and the original vec
double vis_isec = last.get_isec(seclast);
double merg_isec = line.get_isec(seclast);
while(merg_isec < vis_isec){
//line intersects before the last line, and since line slope > last line slope, remove it
tmp_vis.erase(tmp_vis.begin() + (num_vis -1));
num_vis--;
line.set_vis_start(merg_isec);
if(num_vis == 1)
break;

View File

@ -10,22 +10,13 @@ class HL
public:
HL();
void print_sol();
std::vector<Line> gen_sol(std::vector<Line>& ls);
std::vector<Line> construct_HWprob();
std::vector<Line> get_lines();
std::vector<std::pair<int, std::pair<double, double>>> get_sol() const;
void set_sol(std::vector<std::pair<int, std::pair<double, double>>> &new_sol);
std::map<int, Line> get_map();
private:
//Solution is in form (Line ID, (vis_start, vis_end))
std::vector<std::pair<int, std::pair<double, double>>> sol;
std::vector<Line> lines;
std::map<int, Line> line_holder;
};
std::vector<Line> merge(std::vector<Line> lh, std::vector<Line> rh);