-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriceMap.h
More file actions
67 lines (62 loc) · 2.05 KB
/
Copy pathPriceMap.h
File metadata and controls
67 lines (62 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#pragma once
#include <iostream>
#include "Stock.h"
#include "Group.h"
#include "readData.h"
#include <random>
#include <vector>
#include <thread>
#include <mutex>
using namespace std;
typedef map<string, map<string, double>> Map;
namespace fre{
class PriceMap{
private:
vector<Stock> stockList;
Group group;
int numDays;
Map pm;
public:
PriceMap(){}
PriceMap(const Group& g, int nDays) : group(g), numDays(nDays) {
int num_threads = 2;
std::vector<std::thread> threads;
std::mutex mutex;
auto worker = [&](int start, int end) {
for (int n = start; n < end; ++n) {
Stock temp = g.getStock(n);
map<string, double> value = readValue(temp, nDays);
if (value.empty()) {
group.deleteStock(temp);
continue;
}
std::unique_lock<std::mutex> lock(mutex);
stockList.push_back(temp);
pm[temp.getTicker()] = value;
lock.unlock();
}
};
int n = g.getN();
int step = (n + num_threads - 1) / num_threads;
for (int i = 0; i < num_threads; ++i) {
int start = i * step;
int end = std::min(start + step, n);
threads.emplace_back(worker, start, end);
}
for (auto& t : threads) {
t.join();
}
}
~PriceMap(){}
vector<Stock> getStockList() const {return stockList;}
Map getPM() const {return pm;}
map<string, double> getPrice(string ticker) {return pm[ticker];}
void setStockList(vector<Stock> sList) {stockList = sList;}
void setPM(string ticker, map<string, double> price){
pm[ticker] = price;
}
void addStock(Stock s) {stockList.push_back(s);}
PriceMap randomSelect(int n);
PriceMap createReturnMap();
};
}