TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
ArrOfBit.h
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 ArrOfBit_included
17#define ArrOfBit_included
18
19#include <assert.h>
20#include <Objet_U.h>
21
22#ifdef setbit
23#undef setbit // setbit is also a macro in /usr/include/sys/param.h .... too bad ...
24#endif
25
26template <typename _SIZE_>
27class ArrOfBit_32_64 : public Objet_U
28{
29
30 Declare_instanciable_sans_constructeur_ni_destructeur_32_64(ArrOfBit_32_64);
31
32public:
33 using int_t = _SIZE_;
34
36 ArrOfBit_32_64(const ArrOfBit_32_64& array); // Constructeur par copie
37 ~ArrOfBit_32_64() override; // Destructeur
38 ArrOfBit_32_64& operator=(const ArrOfBit_32_64& array); // Operateur copie
40 inline int operator[](int_t i) const;
41 inline void setbit(int_t i) const;
42 inline int testsetbit(int_t i) const;
43 inline void clearbit(int_t i) const;
44 /// Renvoie la taille du tableau en bits
45 inline int_t size_array() const { return taille; }
47protected:
50 unsigned int *data;
51 static constexpr unsigned int SIZE_OF_INT_BITS = 5;
52 static constexpr unsigned int DRAPEAUX_INT = 31;
53};
54
55/*! @brief Renvoie 1 si le bit e est mis, 0 sinon.
56 *
57 */
58template <typename _SIZE_>
60{
61 assert(e >= 0 && e < taille);
62 unsigned int i = (unsigned int) e;
63 unsigned int x = data[i >> SIZE_OF_INT_BITS];
64 unsigned int flag = 1 << (i & DRAPEAUX_INT);
65 int resultat = ((x & flag) != 0) ? 1 : 0;
66 return resultat;
67}
68
69/*! @brief Met le bit e a 1.
70 *
71 */
72template <typename _SIZE_>
74{
75 assert(e >= 0 && e < taille);
76 unsigned int i = (unsigned int) e;
77 unsigned int flag = 1 << (i & DRAPEAUX_INT);
78 data[i >> SIZE_OF_INT_BITS] |= flag;
79}
80
81/*! @brief Renvoie la valeur du bit e, puis met le bit e a 1.
82 *
83 */
84template <typename _SIZE_>
86{
87 assert(e >= 0 && e < taille);
88 unsigned int i = (unsigned int) e;
89 unsigned int flag = 1 << (i & DRAPEAUX_INT);
90 int index = i >> SIZE_OF_INT_BITS;
91 unsigned int old = data[index];
92 data[index] = old | flag;
93 return ((old & flag) != 0) ? 1 : 0;
94}
95
96/*! @brief Met le bit e a 0.
97 *
98 */
99template <typename _SIZE_>
101{
102 assert(e >= 0 && e < taille);
103 unsigned int i = (unsigned int) e;
104 unsigned int flag = 1 << (i & DRAPEAUX_INT);
105 data[i >> SIZE_OF_INT_BITS] &= ~flag;
106}
107
108using ArrOfBit = ArrOfBit_32_64<int>;
109using BigArrOfBit = ArrOfBit_32_64<trustIdType>;
110
111#endif
int_t calculer_int_size(int_t taille) const
Taille en "int" du tableau requis pour stocker un tableau de bits de taille donnees.
Definition ArrOfBit.cpp:55
int operator[](int_t i) const
Renvoie 1 si le bit e est mis, 0 sinon.
Definition ArrOfBit.h:59
int_t size_array() const
Renvoie la taille du tableau en bits.
Definition ArrOfBit.h:45
int testsetbit(int_t i) const
Renvoie la valeur du bit e, puis met le bit e a 1.
Definition ArrOfBit.h:85
ArrOfBit_32_64 & operator=(int_t i)
Si la valeur est non nulle, met la valeur 1 dans tous les elements du tableau, sinon met la valeur 0.
Definition ArrOfBit.cpp:124
ArrOfBit_32_64 & operator=(const ArrOfBit_32_64 &array)
Operateur copie (deep copy).
Definition ArrOfBit.cpp:102
static constexpr unsigned int DRAPEAUX_INT
Definition ArrOfBit.h:52
ArrOfBit_32_64(int_t n=0)
Constructeur d'un tableau de taille n, non initialise.
Definition ArrOfBit.cpp:25
_SIZE_ int_t
Definition ArrOfBit.h:33
ArrOfBit_32_64 & resize_array(int_t n)
Change la taille du tableau et copie les donnees existantes.
Definition ArrOfBit.cpp:71
ArrOfBit_32_64(const ArrOfBit_32_64 &array)
Constructeur par copie (deep copy).
Definition ArrOfBit.cpp:45
void setbit(int_t i) const
Met le bit e a 1.
Definition ArrOfBit.h:73
unsigned int * data
Definition ArrOfBit.h:50
void clearbit(int_t i) const
Met le bit e a 0.
Definition ArrOfBit.h:100
static constexpr unsigned int SIZE_OF_INT_BITS
Definition ArrOfBit.h:51
~ArrOfBit_32_64() override
Destructeur.
Definition ArrOfBit.cpp:35
Objet_U()
Constructeur par defaut : attribue un numero d'identifiant unique a l'objet (object_id_),...
Definition Objet_U.cpp:55