-
Notifications
You must be signed in to change notification settings - Fork 0
/
allStksMgr.cpp
220 lines (198 loc) · 4.37 KB
/
allStksMgr.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "allStksMgr.h"
allStksMgr::allStksMgr()
: m_stks(new stkMap)
, m_catStks(new catMap)
{
}
allStksMgr::~allStksMgr()
{
m_stks->clear();
m_catStks->clear();
}
void allStksMgr::reset()
{
std::lock_guard<std::mutex> lock(m_mutex);
if (!m_stks.unique())
{
m_stks.reset(new stkMap());
return;
}
assert(m_stks.unique());
m_stks->clear();
}
void allStksMgr::reset(stkMap &newMap)
{
std::lock_guard<std::mutex> lock(m_mutex);
if (!m_stks.unique())
{
m_stks.reset(new stkMap(newMap));
return;
}
assert(m_stks.unique());
m_stks->clear();
/*assign*/
(*m_stks)= newMap;
}
void allStksMgr::reset(catMap & newMap)
{
std::lock_guard<std::mutex> lock(m_catMutex);
if (!m_catStks.unique())
{
m_catStks.reset(new catMap(newMap));
return;
}
assert(m_catStks.unique());
m_catStks->clear();
/*assign*/
(*m_catStks) = newMap;
}
void allStksMgr::reset(std::map<std::string, std::string> &map)
{
std::lock_guard<std::mutex> lock(m_mutex);
if (!m_stks.unique())
{
m_stks.reset(new stkMap());
}
else
{
assert(m_stks.unique());
m_stks->clear();
}
/*放入bimap*/
size_t nums = 0;
for (auto &i : map)
{
m_stks->insert({ i.first, i.second });
++nums;
if (m_stks->size() != nums)
{
/*這裡補-回來,否則之後都不一樣*/
--nums;
//
/*結論:目前看起來是一些,指數、概念股有重複,放不進去bimap*/
/*(先不管她)*/
//
}
}
}
std::string allStksMgr::getStkName(const std::string &stkID)
{
std::string name = "";
stkMapPtr allStks = getAllStks();
stkMap::left_iterator it = allStks->left.find(stkID);
if (it != allStks->left.end())
name = it->get_right();
return name;
}
bool allStksMgr::getStkName(const std::string &stkID, std::string &stkName)
{
bool bhas = true;
stkMapPtr allStks = getAllStks();
stkMap::left_iterator it = allStks->left.find(stkID);
if (it != allStks->left.end())
stkName = it->get_right();
else
bhas = false;
return bhas;
}
std::string allStksMgr::getStkID(const std::string &stkName)
{
std::string stkID = "";
stkMapPtr allStks = getAllStks();
stkMap::right_iterator it = allStks->right.find(stkName);
if (it != allStks->right.end())
stkID = it->get_left();
return stkID;
}
bool allStksMgr::hasStkID(const std::string &stk)
{
bool bret = false;
stkMapPtr allStks = getAllStks();
stkMap::left_iterator it = allStks->left.find(stk);
if (it != allStks->left.end())
bret = true;
return bret;
}
int allStksMgr::insertStk(const std::string &stk, const std::string &name)
{
if (stk.empty() || name.empty())
return -1;
int iret = 0;
//
std::lock_guard<std::mutex> lock(m_mutex);
if (!m_stks.unique())
{
m_stks.reset(new stkMap(*m_stks));
}
assert(m_stks.unique());
/*先刪除*/
stkMap::left_iterator it = m_stks->left.find(stk);
if (it != m_stks->left.end())
m_stks->left.erase(it);
/*insert*/
m_stks->insert({ stk, name });
return 0;
}
int allStksMgr::removeStkID(const std::string &stk)
{
int iret = 0;
//
std::lock_guard<std::mutex> lock(m_mutex);
if (!m_stks.unique())
{
m_stks.reset(new stkMap(*m_stks));
}
assert(m_stks.unique());
//
stkMap::left_iterator it = m_stks->left.find(stk);
if (it != m_stks->left.end())
m_stks->left.erase(it);
else
iret = -1;
///////////////////////////////////////////////////////////
std::lock_guard<std::mutex> lock2(m_catMutex);
if (!m_catStks.unique())
{
m_catStks.reset(new catMap(*m_catStks));
}
assert(m_catStks.unique());
//
catMap::iterator itF = m_catStks->find(stk);
if (itF != m_catStks->end())
m_catStks->erase(itF);
else
iret -= 2;
return iret;
}
stkMapPtr allStksMgr::getAllStks()
{
std::lock_guard<std::mutex> lock(m_mutex);
return m_stks;
}
catMapPtr allStksMgr::getAllcatStks()
{
std::lock_guard<std::mutex> lock(m_catMutex);
return m_catStks;
}
char allStksMgr::getMarket(const std::string &stkName)
{
std::string stkID = "";
catMapPtr allStks = getAllcatStks();
catMap::iterator it = allStks->find(stkName);
if (it != allStks->end())
return it->second;
return 0x00;
}
void allStksMgr::cloneAllStks(std::map<std::string, std::string>& ret)
{
stkMapPtr sp = getAllStks();
for (auto& i : (*sp))
{
ret.emplace(i.left, i.right);
}
}
size_t allStksMgr::getAllStksNums()
{
stkMapPtr sp = getAllStks();
return sp->size();
}