TrioCFD 1.9.9_beta
TrioCFD documentation
Loading...
Searching...
No Matches
ArrOfBit.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
16#include <arch.h>
17#include <ArrOfBit.h>
18#include <cstring>
19
20Implemente_instanciable_sans_constructeur_ni_destructeur_32_64(ArrOfBit_32_64,"ArrOfBit",Objet_U);
21
22/*! @brief Constructor for an array of size n, uninitialized.
23 */
24template <typename _SIZE_>
31
32/*! @brief Destructor.
33 */
34template <typename _SIZE_>
36{
37 if (data)
38 delete[] data;
39 data = 0;
40}
41
42/*! @brief Copy constructor (deep copy).
43 */
44template <typename _SIZE_>
46{
47 taille = 0;
48 data = 0;
49 operator=(array);
50}
51
52/*! @brief Size in "int" units of the array required to store a bit array of the given size.
53 */
54template <typename _SIZE_>
56{
57 assert(la_taille >= 0);
58 int_t siz = la_taille >> SIZE_OF_INT_BITS;
59 if (la_taille & DRAPEAUX_INT)
60 siz++;
61 return siz;
62}
63
64/*! @brief Changes the size of the array and copies existing data.
65 *
66 * If the new size is smaller, the data is truncated; if larger, the new
67 * elements are not initialized.
68 */
69template <typename _SIZE_>
71{
72 if (taille == n)
73 return *this;
74 assert(n >= 0);
75 if (n > 0)
76 {
78 int_t newsize = calculer_int_size(n);
79 unsigned int * newdata = new unsigned int[newsize];
80 int_t size_copy = (newsize > oldsize) ? oldsize : newsize;
81 if (size_copy)
82 {
83 memcpy(newdata, data, size_copy);
84 delete[] data;
85 }
86 data = newdata;
87 taille = n;
88 }
89 else
90 {
91 delete[] data; // data!=0 otherwise taille==n and we would not be here
92 data = 0;
93 taille = 0;
94 }
95 return *this;
96}
97
98/*! @brief Copy operator (deep copy).
99 */
100template <typename _SIZE_>
102{
103 int_t newsize = calculer_int_size(array.taille);
104 if (taille != array.taille)
105 {
106 if (data)
107 {
108 delete[] data;
109 data = 0;
110 }
111 if (newsize > 0)
112 data = new unsigned int[newsize];
113 }
114 taille = array.taille;
115 if (taille)
116 memcpy(data, array.data, newsize * sizeof(unsigned int));
117 return *this;
118}
119
120/*! @brief If the value is non-zero, sets all elements of the array to 1; otherwise sets them to 0.
121 */
122template <typename _SIZE_>
124{
125 unsigned int valeur = val ? (~((unsigned int) 0)) : 0;
127 for (int_t i = 0; i < size; i++)
128 data[i] = valeur;
129 return *this;
130}
131
132/*! @brief Writes the array.
133 *
134 * Format: n
135 * 0 1 0 0 1 0 ... (n values)
136 *
137 */
138template <typename _SIZE_>
140{
141#ifndef LATATOOLS
142 os << taille << finl;
143 // A newline every 32 bits,
144 // A space every 8 bits
145 int_t i = 0;
146 for (; i < taille; i++)
147 {
148 os << operator[](i);
149 if ((i & 7) == 7)
150 os << tspace;
151 if ((i & 31) == 31)
152 os << finl;
153 }
154 // A newline if the last line was not terminated
155 if (i & 31)
156 os << finl;
157#endif
158 return os;
159}
160
161/*! @brief Reads the array.
162 *
163 * Format: n
164 * 0 1 0 0 1 0 ... (n values)
165 *
166 */
167template <typename _SIZE_>
169{
170#ifndef LATATOOLS
171 int_t newsize;
172 is >> newsize;
173 resize_array(newsize);
174 operator=(0);
175
176 for (int_t i = 0; i < taille; i++)
177 {
178 int bit;
179 is >> bit;
180 if (bit) setbit(i);
181 }
182#endif
183 return is;
184}
185
186/////////////////////////////////////////////////
187//// Template instanciations
188/////////////////////////////////////////////////
189
190template class ArrOfBit_32_64<int>;
191#if INT_is_64_ == 2
192template class ArrOfBit_32_64<trustIdType>;
193#endif
194
int_t calculer_int_size(int_t taille) const
Size in "int" units of the array required to store a bit array of the given size.
Definition ArrOfBit.cpp:55
ArrOfBit_32_64 & operator=(const ArrOfBit_32_64 &array)
Copy operator (deep copy).
Definition ArrOfBit.cpp:101
static constexpr unsigned int DRAPEAUX_INT
Definition ArrOfBit.h:52
ArrOfBit_32_64(int_t n=0)
Constructor for an array of size n, uninitialized.
Definition ArrOfBit.cpp:25
_SIZE_ int_t
Definition ArrOfBit.h:33
ArrOfBit_32_64 & resize_array(int_t n)
Changes the size of the array and copies existing data.
Definition ArrOfBit.cpp:70
unsigned int * data
Definition ArrOfBit.h:50
static constexpr unsigned int SIZE_OF_INT_BITS
Definition ArrOfBit.h:51
~ArrOfBit_32_64() override
Destructor.
Definition ArrOfBit.cpp:35
Class defining operators and methods for all reading operation in an input flow (file,...
Definition Entree.h:42
Base class for TRUST objects (Objet_U).
Definition Objet_U.h:68
virtual Entree & readOn(Entree &)
Reads an Objet_U from an input stream. Virtual method to override.
Definition Objet_U.cpp:289
Objet_U()
Default constructor: assigns a unique identifier to the object (object_id_) and registers the object ...
Definition Objet_U.cpp:54
virtual Sortie & printOn(Sortie &) const
Writes the object to an output stream. Virtual method to override.
Definition Objet_U.cpp:278
Base class for output streams.
Definition Sortie.h:52