-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlecture10.tex
More file actions
336 lines (278 loc) · 8.13 KB
/
lecture10.tex
File metadata and controls
336 lines (278 loc) · 8.13 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
\documentclass[unknownkeysallowed,xcolor=table]{beamer}
\usepackage [T2A,T1] {fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english,russian]{babel}
\usepackage{amsmath}
\usepackage{listings}
\usepackage{url}
\usepackage{textcomp}
\usepackage{multirow}
% to not copy line numbers
\usepackage{accsupp}
% to not copy line numbers
\setbeamertemplate{navigation symbols}{}
\newcommand{\textapprox}{\raisebox{0.5ex}{\texttildelow}}
\colorlet{mygreen}{green!60!blue}
\colorlet{mymauve}{red!60!blue}
\definecolor{pblue}{rgb}{0.1, 0.2, 0.8}
\newcommand{\rarr}{$\rightarrow$}
\lstset{
basicstyle=\ttfamily\small,
commentstyle=\color{mygreen},
keywordstyle=\color{blue},
% numberstyle=\tiny\color{blue},
numberstyle=\noncopynumber,
stringstyle=\color{mymauve},
numbers=left,
stepnumber=1,
columns=fullflexible,
breaklines=true,
postbreak=\mbox{\textcolor{red}{\ensuremath{\hookrightarrow}\space}},
literate={~} {\textapprox}{1},
language={[11]C++}
}
\makeatletter
\newcommand{\srcmediumsize}{\@setfontsize{\srcmediumsize}{7pt}{7pt}}
\makeatother
\makeatletter
\newcommand{\srcbigsize}{\@setfontsize{\srcbigsize}{8pt}{8pt}}
\makeatother
\makeatletter
\newcommand{\srcsize}{\@setfontsize{\srcsize}{6pt}{6pt}}
\makeatother
\makeatletter
\newcommand{\srcsmallsize}{\@setfontsize{\srcsmallsize}{5pt}{5pt}}
\makeatother
% to not copy line numbers
\newcommand{\noncopynumber}[1]{%
\BeginAccSupp{method=escape,ActualText={}}%
\color{blue} #1%
\EndAccSupp{}%
}
% to not copy line numbers
\title[C++]
{Программирование на языке C++}
\subtitle{Вводный курс}
\author[А.~Б.~Морозов]
{
\texorpdfstring{Александр Морозов\newline\href{mailto:gelu.speculum@gmail.com}{gelu.speculum@gmail.com}}
{Александр Морозов}
}
\date[ITMO 2020]
{ИТМО, весенний семестр 2020}
\logo{%
\makebox[0.97\paperwidth]{%
\includegraphics[align=c,width=2cm,keepaspectratio]{itmo_logo.png}
\hfill
\includegraphics[align=c,width=1.5cm,keepaspectratio]{itiviti_logo.png}
}
}
\AtBeginSection[]
{
\begin{frame}
\frametitle{Содержание}
\tableofcontents[currentsection]
\end{frame}
}
\begin{document}
\frame{\titlepage}
%-------------------------------------------------
\section{B+ дерево}
\begin{frame}{B дерево}
\includegraphics[align=c,width=10cm,keepaspectratio]{images/btree.png}
\end{frame}
\begin{frame}{Преимущества B дерева}
Иерархия памяти:
\begin{enumerate}
\item чтение из кеша CPU \textapprox 10 нс
\item чтение из памяти \textapprox 100 нс
\item чтение с диска \textapprox 10 мс
\end{enumerate}
\vspace{2em}
\begin{itemize}
\item $10^8$ записей в БД
\item строковый ключ 64 символа
\item указатель на запись 8 байт
\item 4 Кб узел дерева \rarr порядок дерева = 56
\item высота дерева \rarr $log_{56} 10^8 \approx 5$
\item высота двоичного дерева $\approx 27$
\end{itemize}
\end{frame}
\begin{frame}{B+ дерево}
\includegraphics[align=c,width=11cm,keepaspectratio]{images/bptree.png}
\end{frame}
\begin{frame}{B+ дерево, вставка}
\includegraphics[align=c,width=8cm,keepaspectratio]{images/bptree_insert.png}
\end{frame}
\begin{frame}{B+ дерево, удаление}
\includegraphics[align=c,width=8cm,keepaspectratio]{images/bptree_delete.png}
\end{frame}
\section{Краткое введение в динамический полиморфизм}
\begin{frame}[fragile]{Виртуальные методы}
\begin{lstlisting}[basicstyle=\ttfamily\srcmediumsize]
class Base
{
int m_x = 0;
public:
virtual std::ostream & print(std::ostream & strm) const
{ return strm << m_x; }
virtual Base * clone() = 0;
};
class Derived : Base
{
double m_y = 0;
public:
std::ostream & print(std::ostream & strm) const override
{ return Base::print(strm) << m_y; }
Base * clone() override
{ return new Derived; }
};
std::ostream & print(std::ostream & strm, const std::vector<Base *> & objs)
{
for (const auto p : objs) {
p->print(strm);
}
return strm;
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Виртуальный деструктор}
\begin{lstlisting}
struct Base
{
virtual ~Base() {}
};
struct Derived : Base
{
~Derived();
};
int main()
{
Base * b = new Derived;
delete b;
}
\end{lstlisting}
\end{frame}
\section{Некоторые аспекты обобщённых интерфейсов}
\begin{frame}[fragile]{Шаблонные аргументы}
\begin{lstlisting}
template <class T>
class Queue
{
public:
void push(?);
};
X get_x();
void process(Queue<X> & queue, const X & x1, X & x2)
{
queue.push(x1);
queue.push(x2);
queue.push(get_x());
queue.push(X{});
queue.push(97);
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Шаблонные аргументы, продолжение}
\begin{lstlisting}
template <class T>
class Queue
{
public:
void push(const T & x);
void push(T && x);
template <class P>
void push(P && p);
};
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Шаблонные аргументы, сложности}
\begin{lstlisting}
class Queue
{
public:
template <class P>
void push(const P & p);
template <class P>
void push(P && p); // lvalue or rvalue reference??
};
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Вывод шаблонных параметров-типов}
\begin{lstlisting}[basicstyle=\ttfamily\srcsize]
#include <iostream>
#include <string>
template <class T>
void pretty_print(T && t, const char * label)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
std::cout << label << " : " << t << std::endl;
}
std::string get_str() { return "Hello!"; }
struct X
{
double x = 111.01;
void test_print() const
{ pretty_print(x, "x"); }
};
int main()
{
int a = 10;
const int b = 5;
std::string s1 = "First";
const std::string s2 = "Second";
pretty_print(a, "a");
pretty_print(b, "b");
pretty_print(s1, "s1");
pretty_print(s2, "s2");
pretty_print(get_str(), "get_str()");
X x;
x.test_print();
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Вкратце о копировании и перемещении}
\begin{lstlisting}
X get_x();
X x;
auto x1 = x;
auto x2 = get_x();
auto x3 = std::move(x);
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Сохранение rvalue ссылки при передаче в другие методы}
\begin{lstlisting}
void push(std::vector<X> & v, X && x)
{
v.push_back(x); // copy
v.push_back(std::move(x)); // move
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Идеальная передача шаблонных ссылок}
\begin{lstlisting}
template <class T>
void push(std::vector<X> & v, T && x)
{
v.emplace_back(std::forward<T>(x));
}
template <class... Args>
void push(std::vector<X> & v, Args && ...args)
{
v.emplace_back(std::forward<Args>(args)...);
}
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Передача нескольких групп параметров в одном variadic списке}
\begin{lstlisting}
template <class... Args>
void insert(std::unordered_map<std::string, X> & data, const char * key, std::size_t key_len, Args &&... args)
{
data.emplace(
std::piecewise_construct,
std::forward_as_tuple(key, key_len),
std::forward_as_tuple(args...));
}
\end{lstlisting}
\end{frame}
\end{document}