Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // MinBy.h
- #include <string>
- #include <vector>
- #include <algorithm>
- using pFn = bool(*)(const std::string&, const std::string&);
- //typedef bool (*pFn)(const std::string&, const std::string&);
- const std::string& minBy(const std::vector<std::string>& values, pFn fn)
- {
- return *std::min_element(values.begin(), values.end(), fn);
- }
- // RemoveInvalid.h
- #include <list>
- #include "Company.h"
- void removeInvalid(std::list<Company*>& companies)
- {
- std::list<Company*>::iterator it = companies.begin();
- while (it != companies.end())
- if ((*it)->getId() < 0)
- {
- delete *it;
- it = companies.erase(it);
- }
- else
- it++;
- }
- // MakeCompany.h
- #include <vector>
- #include <string>
- #include <memory>
- #include "Company.h"
- using sPtr = std::shared_ptr<Company>;
- sPtr makeCompany(const std::vector<std::string>& properties)
- {
- int id = std::stoi(properties[0]);
- std::string name { properties[1] };
- std::vector<std::pair<char, char>> employees;
- for (size_t i = 2; i < properties.size(); i++)
- employees.push_back(std::make_pair(properties[i][0], properties[i][1]));
- sPtr ptr(new Company(id, name, employees));
- return ptr;
- }
- // ParseCompanies.h
- #include <string>
- #include <vector>
- #include <algorithm>
- #include "Company.h"
- using pFn = std::string (*) (const Company&);
- //typedef std::string (*pFn)(const Company&);
- Company* parseUniqueCompanies(const std::string& input, int& numCompanies, pFn fn)
- {
- std::vector<Company> vCompanies;
- std::vector<std::string> vUniqueIds;
- std::istringstream stream(input), cstream;
- std::string line;
- while (std::getline(stream, line, '\n'))
- {
- int id;
- std::string name;
- cstream.clear();
- cstream.str(line);
- cstream >> id >> name;
- Company* pNewCompany = new Company(id, name);
- std::string uniqueId = fn(*pNewCompany);
- std::vector<std::string>::iterator it;
- it = std::find(vUniqueIds.begin(), vUniqueIds.end(), uniqueId);
- if (it != vUniqueIds.end())
- {
- delete pNewCompany;
- }
- else
- {
- vUniqueIds.push_back(uniqueId);
- vCompanies.push_back(*pNewCompany);
- }
- }
- numCompanies = vCompanies.size();
- Company* pCompanies = new Company[numCompanies];
- std::copy(vCompanies.begin(), vCompanies.end(), pCompanies);
- //for (size_t i = 0; i < numCompanies; i++)
- //{
- // pCompanies[i] = vCompanies[i];
- //}
- return pCompanies;
- }
- // Serialize.h
- #include <string>
- #include <sstream>
- #include "Company.h"
- void addCompany(std::vector<byte>& vOutput, const Company& c)
- {
- vOutput.push_back(c.getId());
- for (char ch : c.getName())
- vOutput.push_back(ch);
- vOutput.push_back(0);
- vOutput.push_back(c.getEmployees().size());
- for (auto p : c.getEmployees())
- {
- vOutput.push_back(p.first);
- vOutput.push_back(p.second);
- }
- }
- byte* serializeToMemory(const std::string& input, size_t& bytesWritten)
- {
- std::vector<byte> vOutput;
- bytesWritten = 0;
- int count = 0;
- vOutput.push_back(count);
- std::istringstream companiesIn(input);
- Company company;
- while (companiesIn >> company)
- {
- addCompany(vOutput, company);
- count++;
- }
- bytesWritten = vOutput.size();
- byte* dump = new byte[bytesWritten];
- vOutput[0] = count;
- std::copy(vOutput.begin(), vOutput.end(), dump);
- return dump;
- }
Advertisement
Add Comment
Please, Sign In to add comment