-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStorage.cpp
40 lines (35 loc) · 1001 Bytes
/
Storage.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
#include "Storage.h"
Storage* Storage::m_instance{ nullptr };
std::mutex Storage::m_mutex;
Storage::Storage(int maxItems = 0): m_maxItems(maxItems)
{
m_storage.reserve(m_maxItems+1);
}
Storage* Storage::getInstance(int maxItems)
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_instance == nullptr)
{
m_instance = new Storage(maxItems);
}
return m_instance;
}
void Storage::addData(int data)
{
std::lock_guard<std::mutex> lock(m_mutex);
m_storage.push_back(data);
std::cout << "m_storage = " << m_storage.size() << '\n';
}
int Storage::makeCalculations()
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_storage.size() == m_maxItems)
{
std::sort(m_storage.begin(), m_storage.end(), std::greater<int>());
if (m_maxItems % 2 == 0)
return (m_storage.at((m_maxItems-1) / 2) + m_storage.at((m_maxItems-1) / 2 + 1)) / 2;
else
return m_storage.at((m_maxItems-1) / 2);
}
else return 0;
}