PPL Assignment
IRM2015006
binary_search.cpp
Go to the documentation of this file.
1 #include "binary_search.h"
2 #include <algorithm>
3 
4 
5 Boy* BinarySearch::binarySearch (std::vector<Boy*> boys, int start, int end, std::string name)
6 {
7  if (start == end)
8  return NULL;
9  int mid = (start + end)/2;
10  if (boys[mid]->name == name)
11  return boys[mid];
12  Boy* leftResult = binarySearch(boys, start, mid-1, name);
13  if (leftResult) return leftResult;
14  Boy* rightResult = binarySearch(boys, mid+1, end, name);
15  if (rightResult) return rightResult;
16 
17  return NULL;
18 }
19 
21 {
22  this->boyslist = boyslist;
23 }
24 
25 void BinarySearch::findGirlfriends (std::vector<std::string> namelist, Logger *logger)
26 {
27  auto lambda = [] (Boy* b1, Boy* b2) {
28  return b1->name < b2->name;
29  };
30  std::sort(boyslist.begin(), boyslist.end(), lambda);
31  for (auto name : namelist) {
32  Boy *boy = binarySearch(this->boyslist, 0, this->boyslist.size(), name);
33  if (boy) {
34  logger->log("found", name+" boy found in list", true);
35  if (boy->committed) {
36  logger->log("q7:exists", name+" is committed with "+boy->girlfriend->name, true);
37  } else {
38  logger->log("q7:single", name+" is single", true);
39  }
40  } else {
41  logger->log("not found", name, true);
42  }
43  }
44 }
bool committed
Definition: boy.h:32
void log(const std::string type, const std::string msg, bool print=false)
Definition: logger.cpp:27
Definition: logger.h:8
BinarySearch(std::vector< Boy * > boylist)
std::vector< Boy * > boyslist
Definition: search.h:14
std::string name
Definition: girl.h:24
Boy * binarySearch(std::vector< Boy * > boys, int start, int end, std::string name)
Definition: boy.h:20
void findGirlfriends(std::vector< std::string > namelist, Logger *logger) override
std::string name
Definition: boy.h:24
Girl * girlfriend
Definition: boy.h:30