-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSota.cpp
More file actions
136 lines (96 loc) · 3.1 KB
/
Sota.cpp
File metadata and controls
136 lines (96 loc) · 3.1 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
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
#include "Sota.h"
namespace plugin {
namespace sota{
SimpleSota::SimpleSota(int number) : m_counter(number) {
generateRandomNumber();
makeRandomNumbers();
}
SimpleSota::~SimpleSota() {};
void SimpleSota::setValue() {
for (int i = 0; i < m_counter; i++) {
m_vector.emplace_back(rand() % 10);
m_map.insert(std::make_pair(i, rand() % 10));
}
}
void SimpleSota::eraseValue(int &index) {
// assert(index <= static_cast<uint64_t >(m_vector.size()));
// assert(index < static_cast<uint64_t >(m_map.size()));
if (index > m_vector.size()) {
index = 0;
std::vector<int>::iterator iter = m_vector.begin();
m_vector.erase(iter + index);
}
m_map.erase(index);
}
void SimpleSota::removeRangeValue() {
for (auto value:m_random_numbers) {
try {
eraseValue(value);
}
catch (std::exception &ex) {
std::cout << ex.what();
}
}
}
void SimpleSota::makeRandomNumbers() {
std::random_device rd;
std::mt19937 data(rd());
std::uniform_int_distribution<> distr(0, m_counter);
while (m_random_numbers.size() < m_counterRemove)
m_random_numbers.insert(distr(data));
}
void SimpleSota::generateRandomNumber() {
std::random_device rd;
std::mt19937 data(rd());
std::uniform_int_distribution<> distr(1, 15);
m_counterRemove = distr(data);
}
void SimpleSota::printStorage() const {
std::cout << "Map:"<< "\t";
for (auto &value:m_map)
std::cout << value.second << "\t";
std::cout << "\nVector:"<< "\t";
for (auto &value:m_vector)
std::cout << value << "\t";
}
void SimpleSota::synchronization() {
for (auto &value:m_vector) {
auto result = std::find_if(m_map.begin(), m_map.end(), [&value](const auto &sm) { return sm.second == value; });
if (result != m_map.end()) {
int foundkey = result->first;
}
else
{
m_vector.erase(std::remove(begin(m_vector), end(m_vector), value), end(m_vector));
}
}
for (auto &value:m_map) {
auto result = std::find_if(m_vector.begin(), m_vector.end(), [&](auto &sm) { return sm == value.second; });
if (result != m_vector.end()) {
}
else
{
std::map<int, int>::iterator it = m_map.find(value.first);
if(it != m_map.end())
{
m_map.erase(it);
}
}
}
std::sort(m_vector.begin(),m_vector.end());
}
std::string SimpleSota::toString()
{
std::string tmp;
for (auto &value:m_vector)
tmp +=" "+std::to_string(value);
return tmp;
}
ostream& operator<<(ostream& os,const SimpleSota &p)
{
for (auto &value:p.m_vector)
os<<value;
return os;
}
}
}