4.2 Le fichier PList_impl.h

L'include de base :

1
2
3
4
5
6
7
8
#ifndef __PLIST_IMPL__H__
#define __PLIST_IMPL__H__

#include "PList.h"



#endif

Les constructeurs et le destructeur :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
template <class T>
PList<T>::PList(){
	intialisationPList();
}

template <class T>
PList<T>::PList(const T & el){
	intialisationPList();
	pushBack(el);
}

template <class T>
PList<T>::PList(const PList<T> & liste){
	intialisationPList();
	copyPList(liste);
}

template <class T>
PList<T>::~PList(){
	clear();
}

L'opérateur = :

1
2
3
4
5
template <class T>
typename PList<T>::PList & PList<T>::operator = (const PList<T> & liste){
	copyPList(liste);
	return *this;
}

Les opérateurs += :

1
2
3
4
5
6
7
8
9
10
11
template <class T>
typename PList<T>::PList & PList<T>::operator += (const T & el){
	pushBack(el);
	return *this;
}

template <class T>
typename PList<T>::PList & PList<T>::operator += (const PList<T> & liste){
	pushBack(liste);
	return *this;
}

La fonction pour normaliser la liste :

1
2
3
4
5
6
template <class T>
void PList<T>::normalise(const T & scal){
	for(PIterator<T> it(begin()); it != end(); it++){
		*it /= scal;
	}
}

Récupérer le premier ou le dernier élément :

1
2
3
4
5
6
7
8
9
template <class T>
T PList<T>::front() const{
	return p_handle.p_next->p_data;
}

template <class T>
T PList<T>::back() const{
	return p_handle.p_prev->p_data;
}

Récupérer un élément avec sa position dans la liste :

1
2
3
4
5
6
template <class T>
T PList<T>::getAt(size_t index) const{
	PIterator<T> it(begin());
	it += index;
	return *it;
}

Initialiser un élément avec sa position dans la liste :

1
2
3
4
5
6
template <class T>
void PList<T>::setAt(const T & value, size_t index){
	PIterator<T> it(begin());
	it += index;
	*it = value;
}

Définition de l'opérateur [] :

1
2
3
4
5
6
7
8
9
10
11
template <class T>
T PList<T>::operator [] (size_t index) const{
	return getAt(index);
}

template <class T>
T & PList<T>::operator [] (size_t index){
	PIterator<T> it(begin());
	it += index;
	return *it;
}

il faut bien le définir constant et non-constant.

Renvoyer les PIterator pour parcourir la PList à l'endroit ou à l'envers :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template <class T>
PIterator<T> PList<T>::begin() const{
	PIterator<T> it(p_handle.p_next);
	return it;
}

template <class T>
PIterator<T> PList<T>::end() const{
	PIterator<T> it( (PElement<T>*) &p_handle);
	return it;
}

template <class T>
PIterator<T> PList<T>::rbegin() const{
	PIterator<T> it(p_handle.p_prev);
	return it;
}

template <class T>
PIterator<T> PList<T>::rend() const{
	PIterator<T> it((PElement<T>*)&p_handle);
	return it;
}

Rajouter un élément au début de la liste :

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
template <class T>
bool PList<T>::pushBack(const T & el){
	p_size++;
	PElement<T>* element = NULL;
	if(p_handle.p_prev == NULL && p_handle.p_next == NULL){ //si c'est le premier élément c'est facile
		element = new PElement<T>(&p_handle, &p_handle, el);
		if(element == NULL) return false;
		p_handle.p_next = element;
		p_handle.p_prev = element;
	}else{
		element = new PElement<T>(p_handle.p_prev, &p_handle, el);
		if(element == NULL) return false;
		p_handle.p_prev->p_next = element;
		p_handle.p_prev = element;
	}
	return true;
}

template <class T>
bool PList<T>::pushBack(const PList<T> & liste){
	bool b(true);
	PIterator<T> it(liste.begin());
	while(it != liste.end() && b){
		b = pushBack(*it);
		++it;
	}
	return b;
}

Rajouter un élément à la fin de la liste :

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
template <class T>
bool PList<T>::pushFront(const T & el){
	p_size++;
	PElement<T>* element = NULL;
	if(p_handle.p_prev == NULL && p_handle.p_next == NULL){ //si c'est le premier élément c'est facile
		element = new PElement<T>(&p_handle, &p_handle, el);
		if(element == NULL) return false;
		p_handle.p_next = element;
		p_handle.p_prev = element;
	}else{
		element = new PElement<T>(&p_handle, p_handle.p_next, el);
		if(element == NULL) return false;
		p_handle.p_next->p_prev = element;
		p_handle.p_next = element;
	}
	return true;
}

template <class T>
bool PList<T>::pushFront(const PList<T> & liste){
	bool b(true);
	PIterator<T> it(liste.rbegin());
	while(it != liste.rend() && b){
		b = pushFront(*it);
		--it;
	}
	return b;
}

Dégommer le dernier élément :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template <class T>
bool PList<T>::popBack(){
	if(p_size == 0) return false;
	if(p_size == 1){ // si on en est au dernier élément
		delete p_handle.p_prev;
		p_handle.p_next = NULL;
		p_handle.p_prev = NULL;
		p_size = 0;
		return false;
	}else{  //on comprend très bien tout ça avec un dessin
		PElement<T>* elADegommer = p_handle.p_prev;
		elADegommer->p_prev->p_next = &p_handle;
		p_handle.p_prev = elADegommer->p_prev;
		//maintenant qu'on a sorti l'élément à dégommer de la liste on peut le supprimer
		delete elADegommer;
		p_size--;
		return true;
	}
}

Dégommer le premier élément :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template <class T>
bool PList<T>::popFront(){
	if(p_size == 0) return false;
	if(p_size == 1){ // si on en est au dernier élément
		delete p_handle.p_prev;
		p_handle.p_next = NULL;
		p_handle.p_prev = NULL;
		p_size = 0;
		return false;
	}else{  //on comprend très bien tout ça avec un dessin
		PElement<T>* elADegommer = p_handle.p_next;
		elADegommer->p_next->p_prev = &p_handle;
		p_handle.p_next = elADegommer->p_next;
		//maintenant qu'on a sorti l'élément à dégommer de la liste on peut le supprimer
		delete elADegommer;
		p_size--;
		return true;
	}
}

Effacer tout le contenu de la liste :

1
2
3
4
5
template <class T>
void PList<T>::clear(){
	//tant qu'on peut dégommer des éléments on le fait
	while(popBack());
}

Effacer un élément à une certaine position :

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
template <class T>
bool PList<T>::erase(PIterator<T> & it){
	if(p_size == 0) return false;
	if(p_size == 1){ // si on en est au dernier élément
		delete p_handle.p_prev;
		p_handle.p_next = NULL;
		p_handle.p_prev = NULL;
		p_size = 0;
		return false;
	}else{  //on comprend très bien tout ça avec un dessin
		PElement<T>* elADegommer = it.p_element;
		elADegommer->p_prev->p_next = elADegommer->p_next;
		elADegommer->p_next->p_prev = elADegommer->p_prev;
		//on replace l'itérateur sur l'élément suivant
		it.p_element = elADegommer->p_next;
		//maintenant qu'on a sorti l'élément à dégommer de la liste on peut le supprimer
		delete elADegommer;
		p_size--;
		return true;
	}
}

template <class T>
bool PList<T>::erase(int index){
	if(p_size == 0) return false;
	int i(0);
	PIterator<T> it(begin());
	PIterator<T> itEnd(end());
	while(i != index && it != itEnd){++it;i++;}
	//si i == index c'est que ça a due fonctionner
	if(i == index) return erase(it);
	else return false;
}

Renvoyer la taille de la liste :

1
2
3
4
template <class T>
size_t PList<T>::size() const{
	return p_size;
}

Convertir la PList en std::list ou en std::vector :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <class T>
std::list<T> PList<T>::toStdList() const{
	std::list<T> liste;
	for(PIterator<T> it(begin()); it != end(); it++){
		liste.push_back(*it);
	}
	return liste;
}

template <class T>
std::vector<T> PList<T>::toStdVector() const{
	std::vector<T> liste;
	for(PIterator<T> it(begin()); it != end(); it++){
		liste.push_back(*it);
	}
	return liste;
}

La fonction d'initialisation :

1
2
3
4
5
6
template <class T>
void PList<T>::intialisationPList(){
	p_handle.p_next = NULL;
	p_handle.p_prev = NULL;
	p_size = 0;
}

la fonction de copie :

1
2
3
4
5
6
7
8
9
10
11
template <class T>
void PList<T>::copyPList(const PList<T> & liste){
	clear();
	PIterator<T> it(liste.begin());
	PIterator<T> itEnd(liste.end());
	//on parcourt tout les éléments à copier de la liste
	while(it != itEnd){
		pushBack(*it);
		++it;
	}
}

Le fichier PList_impl.h une fois fini :

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
#ifndef __PLIST_IMPL__H__
#define __PLIST_IMPL__H__

#include "PList.h"

template <class T>
PList<T>::PList(){
	intialisationPList();
}

template <class T>
PList<T>::PList(const T & el){
	intialisationPList();
	pushBack(el);
}

template <class T>
PList<T>::PList(const PList<T> & liste){
	intialisationPList();
	copyPList(liste);
}

template <class T>
PList<T>::~PList(){
	clear();
}

template <class T>
typename PList<T>::PList & PList<T>::operator = (const PList<T> & liste){
	copyPList(liste);
	return *this;
}

template <class T>
typename PList<T>::PList & PList<T>::operator += (const T & el){
	pushBack(el);
	return *this;
}

template <class T>
typename PList<T>::PList & PList<T>::operator += (const PList<T> & liste){
	pushBack(liste);
	return *this;
}

template <class T>
void PList<T>::normalise(const T & scal){
	for(PIterator<T> it(begin()); it != end(); it++){
		*it /= scal;
	}
}

template <class T>
T PList<T>::front() const{
	return p_handle.p_next->p_data;
}

template <class T>
T PList<T>::back() const{
	return p_handle.p_prev->p_data;
}

template <class T>
T PList<T>::getAt(size_t index) const{
	PIterator<T> it(begin());
	it += index;
	return *it;
}

template <class T>
void PList<T>::setAt(const T & value, size_t index){
	PIterator<T> it(begin());
	it += index;
	*it = value;
}

template <class T>
T PList<T>::operator [] (size_t index) const{
	return getAt(index);
}

template <class T>
T & PList<T>::operator [] (size_t index){
	PIterator<T> it(begin());
	it += index;
	return *it;
}

template <class T>
PIterator<T> PList<T>::begin() const{
	PIterator<T> it(p_handle.p_next);
	return it;
}

template <class T>
PIterator<T> PList<T>::end() const{
	PIterator<T> it( (PElement<T>*) &p_handle);
	return it;
}

template <class T>
PIterator<T> PList<T>::rbegin() const{
	PIterator<T> it(p_handle.p_prev);
	return it;
}

template <class T>
PIterator<T> PList<T>::rend() const{
	PIterator<T> it((PElement<T>*)&p_handle);
	return it;
}

template <class T>
bool PList<T>::pushBack(const T & el){
	p_size++;
	PElement<T>* element = NULL;
	if(p_handle.p_prev == NULL && p_handle.p_next == NULL){ //si c'est le premier élément c'est facile
		element = new PElement<T>(&p_handle, &p_handle, el);
		if(element == NULL) return false;
		p_handle.p_next = element;
		p_handle.p_prev = element;
	}else{
		element = new PElement<T>(p_handle.p_prev, &p_handle, el);
		if(element == NULL) return false;
		p_handle.p_prev->p_next = element;
		p_handle.p_prev = element;
	}
	return true;
}

template <class T>
bool PList<T>::pushBack(const PList<T> & liste){
	bool b(true);
	PIterator<T> it(liste.begin());
	while(it != liste.end() && b){
		b = pushBack(*it);
		++it;
	}
	return b;
}

template <class T>
bool PList<T>::pushFront(const T & el){
	p_size++;
	PElement<T>* element = NULL;
	if(p_handle.p_prev == NULL && p_handle.p_next == NULL){ //si c'est le premier élément c'est facile
		element = new PElement<T>(&p_handle, &p_handle, el);
		if(element == NULL) return false;
		p_handle.p_next = element;
		p_handle.p_prev = element;
	}else{
		element = new PElement<T>(&p_handle, p_handle.p_next, el);
		if(element == NULL) return false;
		p_handle.p_next->p_prev = element;
		p_handle.p_next = element;
	}
	return true;
}

template <class T>
bool PList<T>::pushFront(const PList<T> & liste){
	bool b(true);
	PIterator<T> it(liste.rbegin());
	while(it != liste.rend() && b){
		b = pushFront(*it);
		--it;
	}
	return b;
}

template <class T>
bool PList<T>::popBack(){
	if(p_size == 0) return false;
	if(p_size == 1){ // si on en est au dernier élément
		delete p_handle.p_prev;
		p_handle.p_next = NULL;
		p_handle.p_prev = NULL;
		p_size = 0;
		return false;
	}else{  //on comprend très bien tout ça avec un dessin
		PElement<T>* elADegommer = p_handle.p_prev;
		elADegommer->p_prev->p_next = &p_handle;
		p_handle.p_prev = elADegommer->p_prev;
		//maintenant qu'on a sorti l'élément à dégommer de la liste on peut le supprimer
		delete elADegommer;
		p_size--;
		return true;
	}
}

template <class T>
bool PList<T>::popFront(){
	if(p_size == 0) return false;
	if(p_size == 1){ // si on en est au dernier élément
		delete p_handle.p_prev;
		p_handle.p_next = NULL;
		p_handle.p_prev = NULL;
		p_size = 0;
		return false;
	}else{  //on comprend très bien tout ça avec un dessin
		PElement<T>* elADegommer = p_handle.p_next;
		elADegommer->p_next->p_prev = &p_handle;
		p_handle.p_next = elADegommer->p_next;
		//maintenant qu'on a sorti l'élément à dégommer de la liste on peut le supprimer
		delete elADegommer;
		p_size--;
		return true;
	}
}

template <class T>
void PList<T>::clear(){
	//tant qu'on peut dégommer des éléments on le fait
	while(popBack());
}

template <class T>
bool PList<T>::erase(PIterator<T> & it){
	if(p_size == 0) return false;
	if(p_size == 1){ // si on en est au dernier élément
		delete p_handle.p_prev;
		p_handle.p_next = NULL;
		p_handle.p_prev = NULL;
		p_size = 0;
		return false;
	}else{  //on comprend très bien tout ça avec un dessin
		PElement<T>* elADegommer = it.p_element;
		elADegommer->p_prev->p_next = elADegommer->p_next;
		elADegommer->p_next->p_prev = elADegommer->p_prev;
		//on replace l'itérateur sur l'élément suivant
		it.p_element = elADegommer->p_next;
		//maintenant qu'on a sorti l'élément à dégommer de la liste on peut le supprimer
		delete elADegommer;
		p_size--;
		return true;
	}
}

template <class T>
bool PList<T>::erase(int index){
	if(p_size == 0) return false;
	int i(0);
	PIterator<T> it(begin());
	PIterator<T> itEnd(end());
	while(i != index && it != itEnd){++it;i++;}
	//si i == index c'est que ça a due fonctionner
	if(i == index) return erase(it);
	else return false;
}

template <class T>
size_t PList<T>::size() const{
	return p_size;
}

template <class T>
std::list<T> PList<T>::toStdList() const{
	std::list<T> liste;
	for(PIterator<T> it(begin()); it != end(); it++){
		liste.push_back(*it);
	}
	return liste;
}

template <class T>
std::vector<T> PList<T>::toStdVector() const{
	std::vector<T> liste;
	for(PIterator<T> it(begin()); it != end(); it++){
		liste.push_back(*it);
	}
	return liste;
}

template <class T>
void PList<T>::intialisationPList(){
	p_handle.p_next = NULL;
	p_handle.p_prev = NULL;
	p_size = 0;
}

template <class T>
void PList<T>::copyPList(const PList<T> & liste){
	clear();
	PIterator<T> it(liste.begin());
	PIterator<T> itEnd(liste.end());
	//on parcourt tout les éléments à copier de la liste
	while(it != itEnd){
		pushBack(*it);
		++it;
	}
}

#endif

Voilà pour le fichier PList_impl.h.