Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. #include <ios>
  2. #include <set>
  3. #include <string>
  4. #include <vector>
  5. #include <cassert>
  6. #include <sstream>
  7. #include <utility>
  8. #include <numeric>
  9. #include <iostream>
  10. #include <algorithm>
  11.  
  12. #include <boost/filesystem.hpp>
  13.  
  14. using namespace std;
  15. namespace fs = boost::filesystem;
  16.  
  17. struct State
  18. {
  19. size_t time; char operation; size_t id; double value;
  20.  
  21. bool validate () const
  22. {
  23. if (time >= 0 && (operation == 'I' || operation == 'E') && id != 0 && value > 0)
  24. return true;
  25. return false;
  26. }
  27. };
  28.  
  29. class OrderBook
  30. {
  31. vector<State> v_states_;
  32. multiset<double> highest_price_record;
  33. double current_time_weighted_price_ = 0.0;
  34.  
  35. void UpdateTimeWeightedPrice()
  36. {
  37. double delta_time;
  38.  
  39. if (v_states_.size() > 1)
  40. {
  41. assert(v_states_.size() > 0 && !highest_price_record.empty());
  42. delta_time = v_states_.back().time - v_states_.end()[-2].time;
  43. }
  44. else
  45. return;
  46.  
  47. current_time_weighted_price_ += delta_time * *highest_price_record.rbegin();
  48. };
  49.  
  50. void Insert(const State& in)
  51. {
  52. if(!in.validate())
  53. throw domain_error("Tried to insert invalid entry.");
  54.  
  55. v_states_.push_back(in);
  56.  
  57. UpdateTimeWeightedPrice();
  58.  
  59. if (highest_price_record.empty())
  60. {
  61. highest_price_record.insert(in.value);
  62. return;
  63. }
  64.  
  65. if (in.value >= *highest_price_record.rbegin())
  66. highest_price_record.insert(in.value);
  67. }
  68.  
  69. void Erase(const State& in)
  70. {
  71. if (find_if(v_states_.begin(), v_states_.end(),
  72. [&](const State src){return (in.id == src.id);}) == v_states_.end())
  73. throw domain_error("Tried to erase non-existant entry.");
  74. else
  75. {
  76. v_states_.push_back(in);
  77. UpdateTimeWeightedPrice();
  78.  
  79. const auto iter = highest_price_record.find(in.value);
  80.  
  81. if (iter == highest_price_record.end())
  82. return;
  83.  
  84. highest_price_record.erase(iter);
  85. }
  86. }
  87.  
  88. public:
  89.  
  90. double TimeWeightedAverage() const
  91. {
  92. const double delta_time = v_states_.back().time - v_states_.begin()->time;
  93. assert(delta_time > 0.0);
  94.  
  95. return current_time_weighted_price_ / delta_time;
  96. }
  97.  
  98. void ReadInFile(const fs::path& filepath)
  99. {
  100. if (!filepath.has_extension() || !fs::exists(filepath)
  101. || !fs::is_regular_file(filepath))
  102. throw runtime_error("File path provided does not exist or is not a regular file.");
  103.  
  104. ifstream file(filepath.string().c_str());
  105.  
  106. string line;
  107. while (getline(file, line, 'n'))
  108. {
  109. // Continue as long as line is not all whitespace characters
  110. if (any_of(line.cbegin(), line.cend(),
  111. [](string::value_type character) {return !(isspace(character)); }))
  112. {
  113. State input;
  114. stringstream line_ss(line);
  115.  
  116. line_ss >> skipws >> input.time
  117. >> skipws >> input.operation
  118. >> skipws >> input.id;
  119.  
  120. if (input.operation == 'I')
  121. {
  122. line_ss >> skipws >> input.value;
  123. Insert(input);
  124. }
  125. else
  126. Erase(input);
  127. }
  128. }
  129. }
  130. };
  131.  
  132. int main(int argc, char** argv)
  133. {
  134. try
  135. {
  136. assert(argc == 2);
  137. const fs::path filename = argv[1];
  138.  
  139. OrderBook book_1;
  140. book_1.ReadInFile(filename);
  141. cout << "Time weighted average: " << book_1.TimeWeightedAverage() << endl;
  142. }
  143. catch(exception& e)
  144. {
  145. cerr << "Soemthing unexpected went wrong: " << e.what() << endl;
  146. }
  147. catch(...)
  148. {
  149. cerr << "Soemthing unexpected went wrong." << endl;
  150. }
  151. return 0;
  152. }
  153.  
  154. 1. Time[double]
  155. 2. Insert/Erase[Char]
  156. 3. ID[int]
  157. 4. Value[Double]<only if Insert>
  158.  
  159. 1. 2. 3. 4.
  160. --------------------------
  161. 0 I 1 20
  162.  
  163. 50 E 1
  164.  
  165. 100 I 2 40
  166.  
  167. 120 I 3 45
  168.  
  169. 200 I 4 100
  170.  
  171. 250 E 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement