TrioCFD 1.9.9_beta
TrioCFD documentation
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); // Copy constructor
37 ~ArrOfBit_32_64() override; // Destructor
38 ArrOfBit_32_64& operator=(const ArrOfBit_32_64& array); // Copy operator
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 /// Returns the size of the array in 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 Returns 1 if bit e is set, 0 otherwise.
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 Set bit e to 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 Returns the value of bit e, then sets bit e to 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 Set bit e to 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
Size in "int" units of the array required to store a bit array of the given size.
Definition ArrOfBit.cpp:55
int operator[](int_t i) const
Returns 1 if bit e is set, 0 otherwise.
Definition ArrOfBit.h:59
int_t size_array() const
Returns the size of the array in bits.
Definition ArrOfBit.h:45
int testsetbit(int_t i) const
Returns the value of bit e, then sets bit e to 1.
Definition ArrOfBit.h:85
ArrOfBit_32_64 & operator=(int_t i)
If the value is non-zero, sets all elements of the array to 1; otherwise sets them to 0.
Definition ArrOfBit.cpp:123
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
ArrOfBit_32_64(const ArrOfBit_32_64 &array)
Copy constructor (deep copy).
Definition ArrOfBit.cpp:45
void setbit(int_t i) const
Set bit e to 1.
Definition ArrOfBit.h:73
unsigned int * data
Definition ArrOfBit.h:50
void clearbit(int_t i) const
Set bit e to 0.
Definition ArrOfBit.h:100
static constexpr unsigned int SIZE_OF_INT_BITS
Definition ArrOfBit.h:51
~ArrOfBit_32_64() override
Destructor.
Definition ArrOfBit.cpp:35
Objet_U()
Default constructor: assigns a unique identifier to the object (object_id_) and registers the object ...
Definition Objet_U.cpp:54