TrioCFD 1.9.8
TrioCFD documentation
Loading...
Searching...
No Matches
Static_Int_Lists.cpp
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#include <Static_Int_Lists.h>
16#include <TRUSTArrays.h>
17
18/*! @brief detruit toutes les listes
19 */
20template <typename _SIZE_>
22{
23 index_.resize_array(0);
24 valeurs_.resize_array(0);
25}
26
27/*! @brief detruit les listes existantes et en cree de nouvelles.
28 *
29 * On cree autant de listes que d'elements dans le tableau sizes.
30 * La i-ieme liste a une taille sizes[i]
31 * Les valeurs sizes doivent etre positives ou nulles.
32 *
33 */
34template <typename _SIZE_>
36{
37 reset();
38
39 const int_t nb_listes = sizes.size_array();
40 index_.resize_array(nb_listes + 1);
41 // Construction du tableau d'index
42 index_[0];
43 for (int_t i = 0; i < nb_listes; i++)
44 {
45 assert(sizes[i] >= 0);
46 index_[i+1] = index_[i] + sizes[i];
47 }
48 const int_t somme_sizes = index_[nb_listes];
49 valeurs_.resize_array(somme_sizes);
50}
51
52/*! @brief remplace les valeurs stockes par toutes les listes par celles du tableau data.
53 *
54 * data doit avoir pour taille la somme des tailles de toutes
55 * les listes.
56 *
57 */
58template <typename _SIZE_>
60{
61 assert(data.size_array() == valeurs_.size_array());
62 valeurs_.inject_array(data);
63}
64
65#ifndef NDEBUG
66// Verifie la coherence du tableau index et data
67template <typename _SIZE_>
68static bool check_index_data(const ArrOfInt_T<_SIZE_>& index, const ArrOfInt_T<_SIZE_>& data)
69{
70 if (index.size_array() < 1)
71 return false;
72 if (index[0] != 0)
73 return false;
74 const _SIZE_ n = index.size_array() - 1; // nombre de listes
75 for (_SIZE_ i = 0; i < n; i++)
76 if (index[i+1] < index[i])
77 return false;
78 if (index[n] != data.size_array())
79 return false;
80 return true;
81}
82#endif
83
84/*! @brief remplace index et data.
85 *
86 */
87template <typename _SIZE_>
89{
90 assert(check_index_data(index, data));
91 index_ = index;
92 valeurs_ = data;
93}
94
95/*! @brief tri par ordre croissant des valeurs de la i-ieme liste.
96 *
97 * Si num_liste < 0, on trie toutes les listes.
98 *
99 */
100template <typename _SIZE_>
102{
103 const int_t i_debut = (num_liste < 0) ? 0 : num_liste;
104 const int_t i_fin = (num_liste < 0) ? index_.size_array() - 1 : num_liste + 1;
105
106 ArrOfInt_t valeurs_liste;
107 for (int_t i = i_debut; i < i_fin; i++)
108 {
109 const int_t index = index_[i];
110 const int_t size = index_[i+1] - index;
111 valeurs_liste.ref_array(valeurs_, index, size);
112 valeurs_liste.ordonne_array();
113 }
114}
115
116/*! @brief copie la i-ieme liste dans le tableau fourni Le tableau array doit etre resizable.
117 *
118 */
119template <typename _SIZE_>
121{
122 const int_t n = get_list_size(i);
123 array.resize_array(n, RESIZE_OPTIONS::NOCOPY_NOINIT);
124 const int_t index = index_[i];
125 array.inject_array(valeurs_, n, 0 /* destination index */, index /* source index */);
126}
127
128template <typename _SIZE_>
130{
131#ifndef LATATOOLS
132 os << index_ << tspace;
133 os << valeurs_ << tspace;
134#endif
135 return os;
136}
137
138template <typename _SIZE_>
140{
141 reset();
142#ifndef LATATOOLS
143 is >> index_;
144 is >> valeurs_;
145#endif
146 return is;
147}
148
149template <typename _SIZE_>
151{
152#ifndef LATATOOLS
153 os << "nb lists : " << get_nb_lists() << finl;
154 os << "sizes of lists : ";
155 for (int_t i=0; i<get_nb_lists(); ++i)
156 {
157 os << get_list_size(i) << " ";
158 }
159 os << finl;
160
161 for (int_t i=0; i<get_nb_lists(); ++i)
162 {
163 os << "{ " ;
164 const int_t sz = get_list_size(i);
165 for (int_t j=0; j<sz; ++j)
166 os << valeurs_[(index_[i]+j)] << " ";
167 os << "}" << finl;
168 }
169#endif
170 return os;
171}
172
173template <typename _SIZE_>
175{
176 const int nb_lists = src.size();
177 index_.resize_array(nb_lists + 1, RESIZE_OPTIONS::NOCOPY_NOINIT);
178 int_t idx = 0;
179 index_[0] = 0;
180 for (int i = 0; i < nb_lists; i++)
181 {
182 idx += src[i].size_array();
183 index_[i+1] = idx;
184 }
185
186 valeurs_.resize_array(idx, RESIZE_OPTIONS::NOCOPY_NOINIT);
187 idx = 0;
188 for (int i = 0; i < nb_lists; i++)
189 {
190 const ArrOfInt_t& a = src[i];
191 int_t sz = a.size_array();
192 valeurs_.inject_array(a, sz, idx /* dest index */, 0 /* source index */);
193 idx += sz;
194 }
195}
196
197template class Static_Int_Lists_32_64<int>;
198#if INT_is_64_ == 2
200#endif
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
Classe de base des flux de sortie.
Definition Sortie.h:52
Cette classe permet de stocker des listes d'entiers accessibles en temps constant.
void set(const ArrsOfInt_t &src)
Sortie & ecrire(Sortie &os) const
void copy_list_to_array(int_t i_liste, ArrOfInt_t &array) const
copie la i-ieme liste dans le tableau fourni Le tableau array doit etre resizable.
void set_index_data(const ArrOfInt_t &index, const ArrOfInt_t &data)
remplace index et data.
ArrsOfInt_T< _SIZE_ > ArrsOfInt_t
void reset()
detruit toutes les listes
int_t get_list_size(int_t i_liste) const
renvoie le nombre d'elements de la liste i
void trier_liste(int_t i)
tri par ordre croissant des valeurs de la i-ieme liste.
void set_list_sizes(const ArrOfInt_t &sizes)
detruit les listes existantes et en cree de nouvelles.
int_t get_nb_lists() const
renvoie le nombre de listes stockees
Entree & readOn(Entree &is)
ArrOfInt_T< _SIZE_ > ArrOfInt_t
Sortie & printOn(Sortie &os) const
void set_data(const ArrOfInt_t &data)
remplace les valeurs stockes par toutes les listes par celles du tableau data.
_SIZE_ size_array() const
virtual void ref_array(TRUSTArray &, _SIZE_ start=0, _SIZE_ sz=-1)
TRUSTArray & inject_array(const TRUSTArray &source, _SIZE_ nb_elements=-1, _SIZE_ first_element_dest=0, _SIZE_ first_element_source=0)
void resize_array(_SIZE_ new_size, RESIZE_OPTIONS opt=RESIZE_OPTIONS::COPY_INIT)
void ordonne_array()
int size() const