TrioCFD 1.9.9_beta
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 Destroys all lists.
19 */
20template <typename _SIZE_>
22{
23 index_.resize_array(0);
24 valeurs_.resize_array(0);
25}
26
27/*! @brief Destroys existing lists and creates new ones.
28 *
29 * Creates as many lists as there are elements in the sizes array.
30 * The i-th list has a size of sizes[i].
31 * The sizes values must be non-negative.
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 // Build the index array
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 Replaces the values stored in all lists with those from the data array.
53 *
54 * data must have a size equal to the sum of the sizes of all lists.
55 *
56 */
57template <typename _SIZE_>
59{
60 assert(data.size_array() == valeurs_.size_array());
61 valeurs_.inject_array(data);
62}
63
64#ifndef NDEBUG
65// Check consistency of the index and data arrays
66template <typename _SIZE_>
67static bool check_index_data(const ArrOfInt_T<_SIZE_>& index, const ArrOfInt_T<_SIZE_>& data)
68{
69 if (index.size_array() < 1)
70 return false;
71 if (index[0] != 0)
72 return false;
73 const _SIZE_ n = index.size_array() - 1; // number of lists
74 for (_SIZE_ i = 0; i < n; i++)
75 if (index[i+1] < index[i])
76 return false;
77 if (index[n] != data.size_array())
78 return false;
79 return true;
80}
81#endif
82
83/*! @brief Replaces index and data arrays.
84 *
85 * @param index the new index array
86 * @param data the new data array
87 */
88template <typename _SIZE_>
90{
91 assert(check_index_data(index, data));
92 index_ = index;
93 valeurs_ = data;
94}
95
96/*! @brief Sorts the values of the i-th list in ascending order.
97 *
98 * If num_liste < 0, all lists are sorted.
99 *
100 * @param num_liste index of the list to sort, or -1 to sort all lists
101 */
102template <typename _SIZE_>
104{
105 const int_t i_debut = (num_liste < 0) ? 0 : num_liste;
106 const int_t i_fin = (num_liste < 0) ? index_.size_array() - 1 : num_liste + 1;
107
108 ArrOfInt_t valeurs_liste;
109 for (int_t i = i_debut; i < i_fin; i++)
110 {
111 const int_t index = index_[i];
112 const int_t size = index_[i+1] - index;
113 valeurs_liste.ref_array(valeurs_, index, size);
114 valeurs_liste.ordonne_array();
115 }
116}
117
118/*! @brief Copies the i-th list into the provided array. The array must be resizable.
119 *
120 * @param i index of the list to copy
121 * @param array destination array to fill
122 */
123template <typename _SIZE_>
125{
126 const int_t n = get_list_size(i);
127 array.resize_array(n, RESIZE_OPTIONS::NOCOPY_NOINIT);
128 const int_t index = index_[i];
129 array.inject_array(valeurs_, n, 0 /* destination index */, index /* source index */);
130}
131
132template <typename _SIZE_>
134{
135#ifndef LATATOOLS
136 os << index_ << tspace;
137 os << valeurs_ << tspace;
138#endif
139 return os;
140}
141
142template <typename _SIZE_>
144{
145 reset();
146#ifndef LATATOOLS
147 is >> index_;
148 is >> valeurs_;
149#endif
150 return is;
151}
152
153template <typename _SIZE_>
155{
156#ifndef LATATOOLS
157 os << "nb lists : " << get_nb_lists() << finl;
158 os << "sizes of lists : ";
159 for (int_t i=0; i<get_nb_lists(); ++i)
160 {
161 os << get_list_size(i) << " ";
162 }
163 os << finl;
164
165 for (int_t i=0; i<get_nb_lists(); ++i)
166 {
167 os << "{ " ;
168 const int_t sz = get_list_size(i);
169 for (int_t j=0; j<sz; ++j)
170 os << valeurs_[(index_[i]+j)] << " ";
171 os << "}" << finl;
172 }
173#endif
174 return os;
175}
176
177template <typename _SIZE_>
179{
180 const int nb_lists = src.size();
181 index_.resize_array(nb_lists + 1, RESIZE_OPTIONS::NOCOPY_NOINIT);
182 int_t idx = 0;
183 index_[0] = 0;
184 for (int i = 0; i < nb_lists; i++)
185 {
186 idx += src[i].size_array();
187 index_[i+1] = idx;
188 }
189
190 valeurs_.resize_array(idx, RESIZE_OPTIONS::NOCOPY_NOINIT);
191 idx = 0;
192 for (int i = 0; i < nb_lists; i++)
193 {
194 const ArrOfInt_t& a = src[i];
195 int_t sz = a.size_array();
196 valeurs_.inject_array(a, sz, idx /* dest index */, 0 /* source index */);
197 idx += sz;
198 }
199}
200
201template class Static_Int_Lists_32_64<int>;
202#if INT_is_64_ == 2
204#endif
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
Base class for output streams.
Definition Sortie.h:52
This class allows storing lists of integers accessible in constant time.
void set(const ArrsOfInt_t &src)
Sortie & ecrire(Sortie &os) const
void copy_list_to_array(int_t i_liste, ArrOfInt_t &array) const
Copies the i-th list into the provided array. The array must be resizable.
void set_index_data(const ArrOfInt_t &index, const ArrOfInt_t &data)
Replaces index and data arrays.
ArrsOfInt_T< _SIZE_ > ArrsOfInt_t
void reset()
Destroys all lists.
int_t get_list_size(int_t i_liste) const
Returns the number of elements in list i.
void trier_liste(int_t i)
Sorts the values of the i-th list in ascending order.
void set_list_sizes(const ArrOfInt_t &sizes)
Destroys existing lists and creates new ones.
int_t get_nb_lists() const
Returns the number of stored lists.
Entree & readOn(Entree &is)
ArrOfInt_T< _SIZE_ > ArrOfInt_t
Sortie & printOn(Sortie &os) const
void set_data(const ArrOfInt_t &data)
Replaces the values stored in all lists with those from the data array.
_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