kolioi

02. Memory Management - Homework C++

Feb 5th, 2019
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. // MinBy.h
  2.  
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using pFn = bool(*)(const std::string&, const std::string&);
  8. //typedef bool (*pFn)(const std::string&, const std::string&);
  9.  
  10. const std::string& minBy(const std::vector<std::string>& values, pFn fn)
  11. {
  12.     return *std::min_element(values.begin(), values.end(), fn);
  13. }
  14.  
  15. // RemoveInvalid.h
  16.  
  17. #include <list>
  18.  
  19. #include "Company.h"
  20.  
  21. void removeInvalid(std::list<Company*>& companies)
  22. {
  23.     std::list<Company*>::iterator it = companies.begin();
  24.     while (it != companies.end())
  25.         if ((*it)->getId() < 0)
  26.         {
  27.             delete *it;
  28.             it = companies.erase(it);
  29.         }
  30.         else
  31.             it++;
  32. }
  33.  
  34. // MakeCompany.h
  35.  
  36. #include <vector>
  37. #include <string>
  38. #include <memory>
  39.  
  40. #include "Company.h"
  41.  
  42. using sPtr = std::shared_ptr<Company>;
  43.  
  44. sPtr makeCompany(const std::vector<std::string>& properties)
  45. {
  46.     int id = std::stoi(properties[0]);
  47.     std::string name { properties[1] };
  48.  
  49.     std::vector<std::pair<char, char>> employees;
  50.     for (size_t i = 2; i < properties.size(); i++)
  51.             employees.push_back(std::make_pair(properties[i][0], properties[i][1]));
  52.  
  53.     sPtr ptr(new Company(id, name, employees));
  54.  
  55.     return ptr;
  56. }
  57.  
  58. // ParseCompanies.h
  59.  
  60. #include <string>
  61. #include <vector>
  62. #include <algorithm>
  63.  
  64. #include "Company.h"
  65.  
  66. using pFn = std::string (*) (const Company&);
  67. //typedef std::string (*pFn)(const Company&);
  68.  
  69. Company* parseUniqueCompanies(const std::string& input, int& numCompanies, pFn fn)
  70. {
  71.     std::vector<Company> vCompanies;
  72.     std::vector<std::string> vUniqueIds;
  73.     std::istringstream stream(input), cstream;
  74.     std::string line;
  75.     while (std::getline(stream, line, '\n'))
  76.     {
  77.         int id;
  78.         std::string name;
  79.         cstream.clear();
  80.         cstream.str(line);
  81.         cstream >> id >> name;
  82.         Company* pNewCompany = new Company(id, name);
  83.         std::string uniqueId = fn(*pNewCompany);
  84.         std::vector<std::string>::iterator it;
  85.         it = std::find(vUniqueIds.begin(), vUniqueIds.end(), uniqueId);
  86.         if (it != vUniqueIds.end())
  87.         {
  88.             delete pNewCompany;
  89.         }
  90.         else
  91.         {
  92.             vUniqueIds.push_back(uniqueId);
  93.             vCompanies.push_back(*pNewCompany);
  94.         }
  95.     }
  96.  
  97.     numCompanies = vCompanies.size();
  98.     Company* pCompanies = new Company[numCompanies];
  99.     std::copy(vCompanies.begin(), vCompanies.end(), pCompanies);
  100.     //for (size_t i = 0; i < numCompanies; i++)
  101.     //{
  102.     //  pCompanies[i] = vCompanies[i];
  103.     //}
  104.  
  105.     return pCompanies;
  106. }
  107.  
  108. // Serialize.h
  109.  
  110. #include <string>
  111. #include <sstream>
  112.  
  113. #include "Company.h"
  114.  
  115. void addCompany(std::vector<byte>& vOutput, const Company& c)
  116. {
  117.     vOutput.push_back(c.getId());
  118.     for (char ch : c.getName())
  119.         vOutput.push_back(ch);
  120.     vOutput.push_back(0);
  121.     vOutput.push_back(c.getEmployees().size());
  122.     for (auto p : c.getEmployees())
  123.     {
  124.         vOutput.push_back(p.first);
  125.         vOutput.push_back(p.second);
  126.     }
  127. }
  128.  
  129. byte* serializeToMemory(const std::string& input, size_t& bytesWritten)
  130. {
  131.     std::vector<byte> vOutput;
  132.     bytesWritten = 0;
  133.     int count = 0;
  134.     vOutput.push_back(count);
  135.  
  136.     std::istringstream companiesIn(input);
  137.     Company company;
  138.     while (companiesIn >> company)
  139.     {
  140.         addCompany(vOutput, company);
  141.         count++;
  142.     }
  143.  
  144.     bytesWritten = vOutput.size();
  145.     byte* dump = new byte[bytesWritten];
  146.     vOutput[0] = count;
  147.     std::copy(vOutput.begin(), vOutput.end(), dump);
  148.  
  149.     return dump;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment