-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.cpp
More file actions
65 lines (58 loc) · 1.66 KB
/
database.cpp
File metadata and controls
65 lines (58 loc) · 1.66 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
#include "database.h"
void Database::Add (const Date& date, const string& event) {
if (set_events[date].find(event) == end(set_events[date])) {
events[date].push_back(event);
set_events[date].insert(event);
}
}
void Database::Print (ostream& os) const {
for (const auto& item : events) {
for (const string& event : item.second) {
os << item.first << " " << event << endl;
}
}
}
pair<Date, string> Database::Last (const Date& date) const {
auto it = events.upper_bound(date);
if (it == begin(events))
throw invalid_argument("");
--it;
return {it->first, *rbegin(it->second)};
}
int Database::RemoveIf (
function<bool(const Date&, const string&)> predicate
) {
int amount_removed_elements = 0;
for (auto it_ = begin(events); it_ != end(events); ) {
auto it = stable_partition(begin(it_->second), end(it_->second),
[&](const string& event) {
if (predicate(it_->first, event)) {
amount_removed_elements++;
set_events[it_->first].erase(event);
return false;
} else {
return true;
}
});
if (it == begin(it_->second)) {
it_ = events.erase(it_);
continue;
}
it_->second.erase(it, end(it_->second));
++it_;
}
return amount_removed_elements;
}
vector<pair<Date, string>> Database::FindIf (
function<bool(const Date&, const string&)> predicate
) const {
vector<pair<Date, string>> found;
for (auto it_ = begin(events); it_ != end(events); ++it_) {
for (auto _it = begin(it_->second); _it != end(it_->second); ++_it) {
if(predicate(it_->first, *_it)) {
found.push_back(make_pair(it_->first, *_it));
}
}
}
return found;
}