-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
executable file
·174 lines (98 loc) · 3.78 KB
/
main.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
//Library books using std::tuple and variadic templates (author, name, year .... etc)
//Sorting using vector, map (faster)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <tuple>
#include <set>
#include <map>
#include <algorithm>
#include <chrono>
template <typename... Args>
class Book {
template <typename... Args>
friend class Library;
private:
std::tuple< Args...> m_data;
public:
Book(Args&&... args) : m_data(std::forward<Args>(args)...) {}
template <int V>
auto&& get() const {
return std::get<V>(m_data);
}
auto operator<=> (const Book<Args...>& other) const = default;
};
template <typename... Args>
class Library {
private:
std::multiset<Book<Args...>> books;
public:
Library() = default;
Library(Library&&) = delete;
Library& operator=(Library&& str) = delete;
Library(const Library& other) = delete;
Library& operator=(const Library& str) = delete;
Library(std::initializer_list<Book<Args...>>&& book_to_add) {
books = std::move(book_to_add);
}
void add(std::initializer_list<Book<Args...>>&& book_to_add) {
books.insert(book_to_add);
}
template<typename T, std::size_t... Indexes>
void printBook( T&& book, std::index_sequence<Indexes...> unused) const {
((std::cout << std::get<Indexes>(book.m_data) << ' '), ...);
std::cout << '\n';
}
void ByAuthor() const {
std::cout << "\nBy Author:\n";
std::vector<Book<Args...>> tmp_lib(books.begin(), books.end());
/*std::sort(tmp_lib.begin(),tmp_lib.end(), //çäåñü íå íàäî
[](const auto& l, const auto& r) { return l.get<0>() < r.get<0>(); }); */
for (const auto& book : tmp_lib) {
printBook(std::forward<decltype(book)>(book), std::make_index_sequence<sizeof...(Args)>());
}
__asm nop
}
void ByTitle() const { //÷åðåç map áîëåå áûñòðåå
std::cout << "\nBy Title:\n";
auto start = std::chrono::system_clock::now();
/* std::vector<Book<Args...>> tmp_lib(books.begin(), books.end());
auto start = std::chrono::system_clock::now();
std::sort(tmp_lib.begin(), tmp_lib.end(),
[](const auto& l, const auto& r) { return l.get<1>() < r.get<1>(); });*/
auto d = [](const auto& l, const auto& r) { return l.get<1>() < r.get<1>(); };
std::multiset <Book<Args...>, decltype(d)> tmp_lib(books.begin(), books.end());
auto fin = std::chrono::system_clock::now();
auto g = fin - start; //753 VS 1141
for (const auto& book : tmp_lib) {
printBook(std::forward<decltype(book)>(book), std::make_index_sequence<sizeof...(Args)>());
}
}
void ByYear() const {
std::cout << "\nBy Year:\n";
std::vector<Book<Args...>> tmp_lib(books.begin(), books.end());
std::sort(tmp_lib.begin(), tmp_lib.end(),
[](const auto& l, const auto& r) { return l.get<2>() < r.get<2>(); });
for (const auto& book : tmp_lib) {
printBook(std::forward<decltype(book)>(book), std::make_index_sequence<sizeof...(Args)>());
}
}
void del(const Book<Args...>& book_to_delete) {
books.erase(book_to_delete);
}
};
int main() {
Book<std::string, std::string, int> my_favorite_book{ "Willims", "Parallel C++", 2014 };
Book<std::string, std::string, int> my_favorite_book_2{ "Straustrup", "C++", 2000 };
auto info = my_favorite_book.get<1>();
bool lite = (my_favorite_book < my_favorite_book_2);
Library<std::string, std::string, int> O;
Library<std::string, std::string, int> l = { {"Straustrup", "C++", 2000}, my_favorite_book, my_favorite_book_2 };
l.add({ {"Straustrup", "C++", 2010}, {"Lllll", "AAA", 2007}, {"Golovic", "Advanced", 2008} });
l.ByAuthor();
l.ByTitle();
l.ByYear();
l.del(my_favorite_book_2);
l.del({ "Straustrup", "C++", 2010 });
__asm nop
return 0;
}