-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpile_impl.h
executable file
·120 lines (97 loc) · 2.57 KB
/
pile_impl.h
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
#ifndef PILE_IMPL_H
#define PILE_IMPL_H
#include <stdexcept> // std::out_of_range
#include "pile.h"
// AJOUTEZ VOTRE CODE ICI ...
// ----------------------------------------------------------------------------
// CONSTRUCTORS
template<typename T>
Pile<T>::Pile(size_t n) : capacite(n), taille(0) {
data = (T *) ::operator new(n * sizeof(T));
}
template<typename T>
Pile<T>::Pile(const Pile<T> &other): capacite(other.capacite), taille(other.taille) {
data = (T *) ::operator new(other.capacite * sizeof(T));
for (size_t i = 0; i < other.taille; ++i) {
new(&data[i]) T{other.data[i]};
}
}
// ----------------------------------------------------------------------------
// DESTRUCTOR
template<typename T>
Pile<T>::~Pile<T>() {
for (size_t i = 0; i < taille; ++i) {
data[i].~T();
}
::operator delete(data);
}
// ----------------------------------------------------------------------------
// OPERATORS
template<typename T>
Pile<T> &Pile<T>::operator=(const Pile<T> &other) {
if (this == &other) {
return *this;
}
if (other.taille > capacite) {
Pile<T> tmp{other};
swap(tmp);
return *this;
}
// assign existing data with new data, deleting stale data if needed
for (size_t i = 0; i < taille; ++i) {
if (i >= other.taille) {
data[i].~T();
continue;
}
if (&data[i] != &other.data[i]) {
data[i] = other.data[i];
}
}
// copy new data into uninitialised data
for (size_t i = taille; i < other.taille; ++i) {
new(&data[i]) T{other.data[i]};
}
taille = other.taille;
return *this;
}
// ----------------------------------------------------------------------------
// MEMBER FUNCTIONS
template<typename T>
void Pile<T>::push(T e) {
if (full()) {
throw std::out_of_range("push");
}
new(&data[taille]) T{std::move(e)};
++taille;
}
template<typename T>
void Pile<T>::pop() {
if (empty()) {
throw std::out_of_range("pop");
}
data[--taille].~T();
}
template<typename T>
const T &Pile<T>::top() const {
if (empty()) {
throw std::out_of_range("top");
}
return data[taille - 1];
}
template<typename T>
bool Pile<T>::empty() const noexcept {
return !taille;
}
template<typename T>
bool Pile<T>::full() const noexcept {
return taille == capacite;
}
template<typename T>
void Pile<T>::swap(Pile &other) noexcept {
using std::swap;
swap(capacite, other.capacite);
swap(taille, other.taille);
swap(data, other.data);
}
// ... FIN DE VOTRE CODE
#endif