TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
TRUSTList.tpp
1/****************************************************************************
2* Copyright (c) 2024, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#ifndef TRUSTList_TPP_included
17#define TRUSTList_TPP_included
18
19/*! @brief Ecriture d'une liste sur un flot de sortie les elements separes par des virgules figurent entre des accolades
20 *
21 */
22template<typename _TYPE_>
24{
25 TRUSTList_Curseur<_TYPE_> curseur(*this);
26 Nom accouverte = "{", accfermee = "}", virgule = ",";
27 os << accouverte << " ";
28 while (curseur)
29 {
30 // if(est_dernier()) GF sinon on a une virgule de trop
31 if (curseur.list().est_dernier()) os << curseur.valeur() << " ";
32 else os << curseur.valeur() << " " << virgule << " ";
33 ++curseur;
34 }
35 os << accfermee;
36 return os;
37}
38
39/*! @brief Lecture d'une liste sur un flot d'entree les elements separes par des virgules figurent entre des accolades
40 *
41 */
42template<typename _TYPE_>
44{
45 Nom accouverte = "{", accfermee = "}", virgule = ",", nom;
46 _TYPE_ t;
47 is >> nom;
48 assert(nom == accouverte);
49 while (nom != accfermee)
50 {
51 is >> t;
52 add(t);
53 is >> nom;
54 assert((nom == accfermee) || (nom == virgule));
55 }
56 return is;
57}
58
59/*! @brief Affectation.
60 *
61 * Les elements sont copies
62 *
63 */
64template<typename _TYPE_>
66{
67 if (a_list.est_vide()) this->suivant_ = this;
68 else
69 {
70 vide();
71 TRUSTList_Curseur<_TYPE_> curseur(a_list);
72 while (curseur)
73 {
74 add(curseur.valeur());
75 ++curseur;
76 }
77 }
78 return *this;
79}
80
81/*! @brief insertion en queue
82 *
83 */
84template<typename _TYPE_>
85inline TRUSTList<_TYPE_>& TRUSTList<_TYPE_>::add(_TYPE_ value_to_add)
86{
87 if (value_to_add < min_data) min_data = value_to_add;
88 if (value_to_add > max_data) max_data = value_to_add;
89 size_++;
91 {
92 this->data = value_to_add;
93 this->suivant_ = 0;
94 return *this;
95 }
96 else
97 {
99 {
100 TRUSTListElem<_TYPE_> *next = new TRUSTListElem<_TYPE_>(value_to_add);
101 this->suivant_ = next;
102 dernier_ = next;
103 }
104 else
105 {
106 dernier().add(value_to_add);
107 dernier_ = &dernier_->suivant();
108 }
109 return *this;
110 }
111}
112
113/*! @brief Ajout d'un element a la liste ssi il n'existe pas deja
114 *
115 */
116template<typename _TYPE_>
118{
119 if (!contient(x)) return add(x);
120 else return *this;
121}
122
123/*! @brief Verifie si un element appartient ou non a la liste
124 *
125 */
126template<typename _TYPE_>
127inline int TRUSTList<_TYPE_>::contient(_TYPE_ x) const
128{
129 if (TRUSTListElem<_TYPE_>::est_vide() || x > max_data || x < min_data) return 0;
130
131 TRUSTList_Curseur<_TYPE_> curseur(*this);
132 while (curseur)
133 {
134 if (curseur.valeur() == x) return 1;
135 ++curseur;
136 }
137 return 0;
138}
139
140/*! @brief renvoie le rang d'un element dans la liste si un element apparait plusieurs fois, renvoie le rang du premier.
141 *
142 */
143template<typename _TYPE_>
144inline int TRUSTList<_TYPE_>::rang(_TYPE_ x) const
145{
146 if (TRUSTListElem<_TYPE_>::est_vide() || x > max_data || x < min_data) return -1;
147
148 int compteur = 0;
149 TRUSTList_Curseur<_TYPE_> curseur(*this);
150 while (curseur)
151 {
152 if (curseur.valeur() == x) return compteur;
153 ++compteur;
154 ++curseur;
155 }
156 return -1;
157}
158
159/*! @brief Operateur d'acces au ieme int de la liste
160 *
161 */
162template<typename _TYPE_>
163inline _TYPE_& TRUSTList<_TYPE_>::operator[](int i)
164{
165 TRUSTList_Curseur<_TYPE_> curseur(*this);
166 while (curseur && i--) ++curseur;
167
168 if (i != -1)
169 {
170 Process::exit("Overflow list ");
171 }
172 return curseur.valeur();
173}
174
175template<typename _TYPE_>
176inline const _TYPE_& TRUSTList<_TYPE_>::operator[](int i) const
177{
178 TRUSTList_Curseur<_TYPE_> curseur(*this);
179 while (curseur && i--) ++curseur;
180
181 if (i != -1)
182 {
183 Process::exit("Overflow list ");
184 }
185 return curseur.valeur();
186}
187
188/*! @brief Operateur de comparaison de deux listes
189 *
190 */
191template<typename _TYPE_>
192int operator ==(const TRUSTList<_TYPE_>& list1, const TRUSTList<_TYPE_>& list2)
193{
194 int retour = 1;
195 if (list1.data != list2.data) retour = 0;
196 if ((!list1.est_dernier()) && (list2.est_dernier())) retour = 0;
197 if ((list1.est_dernier()) && (!list2.est_dernier())) retour = 0;
198 if ((!list1.est_dernier()) && (!list2.est_dernier())) retour = (*list1.suivant_ == *list2.suivant_);
199 return retour;
200}
201
202/*! @brief Supprime un element contenu dans la liste
203 *
204 */
205template<typename _TYPE_>
206inline void TRUSTList<_TYPE_>::suppr(_TYPE_ obj)
207{
209 {
210 if (this->suivant_)
211 {
212 TRUSTListElem<_TYPE_> *next = this->suivant_;
213 this->suivant_ = next->suivant_;
214 this->data = next->valeur();
215 next->suivant_ = 0;
216 delete next;
217 }
218 else
219 {
220 this->suivant_ = this;
221 dernier_ = this;
222 }
224 size_--;
225 return;
226 }
227 TRUSTList_Curseur<_TYPE_> curseur_pre = *this;
228 TRUSTList_Curseur<_TYPE_> curseur = *(this->suivant_);
229
230 while (curseur)
231 {
232 if (curseur.valeur() == obj)
233 {
234 TRUSTListElem<_TYPE_> *next = &curseur_pre.list().suivant();
235 curseur_pre.list().suivant_ = curseur.list().suivant_;
236 if (next->suivant_ == 0) dernier_ = &curseur_pre.list();
237 else next->suivant_ = 0;
238
239 delete next;
241 size_--;
242 return;
243 }
244 ++curseur;
245 ++curseur_pre;
246 }
247 Cerr << "WARNING during deletion of an element in a list " << finl;
248 Cerr << "One has not found object == : " << obj << finl;
249}
250
251template<typename _TYPE_>
253{
254 min_data = std::numeric_limits<_TYPE_>::max();
255 max_data = std::numeric_limits<_TYPE_>::lowest();
256 TRUSTList_Curseur<_TYPE_> curseur = *this;
257 while (curseur)
258 {
259 _TYPE_ la_valeur = curseur.valeur();
260 if (la_valeur < min_data) min_data = la_valeur;
261 if (la_valeur > max_data) max_data = la_valeur;
262 ++curseur;
263 }
264}
265
266/*! @brief Vide la liste
267 *
268 */
269template<typename _TYPE_>
271{
273 if (this->suivant_) delete this->suivant_;
274 this->suivant_ = this;
275 dernier_ = this;
277 size_=0;
278}
279
280#endif /* TRUSTList_TPP_included */
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
class Nom Une chaine de caractere pour nommer les objets de TRUST
Definition Nom.h:31
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455
Classe de base des flux de sortie.
Definition Sortie.h:52
_TYPE_ & valeur()
int est_vide() const
int est_dernier() const
TRUSTListElem * suivant_
: List_Curseur de reels int/double precision
Definition TRUSTList.h:103
const TRUSTListElem< _TYPE_ > & list() const
Definition TRUSTList.h:126
_TYPE_ valeur() const
Definition TRUSTList.h:115
: Classe qui sert a representer une liste de reels int/double precision.
Definition TRUSTList.h:33
_TYPE_ & operator[](int)
Operateur d'acces au ieme int de la liste.
Sortie & printOn(Sortie &os) const
Ecriture d'une liste sur un flot de sortie les elements separes par des virgules figurent entre des a...
Definition TRUSTList.tpp:23
TRUSTList & add(_TYPE_)
insertion en queue
Definition TRUSTList.tpp:85
int rang(_TYPE_) const
renvoie le rang d'un element dans la liste si un element apparait plusieurs fois, renvoie le rang du ...
TRUSTList & add_if_not(_TYPE_)
Ajout d'un element a la liste ssi il n'existe pas deja.
Entree & readOn(Entree &is)
Lecture d'une liste sur un flot d'entree les elements separes par des virgules figurent entre des acc...
Definition TRUSTList.tpp:43
void suppr(_TYPE_)
Supprime un element contenu dans la liste.
int contient(_TYPE_) const
Verifie si un element appartient ou non a la liste.
TRUSTList & operator=(const TRUSTList &)
Affectation.
Definition TRUSTList.tpp:65
void vide()
Vide la liste.
void calcule_min_max()
TRUSTListElem< _TYPE_ > & dernier()
Renvoie le dernier element de la liste.
Definition TRUSTList.h:75