1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #include <iostream>
#include <bitset>
#include <array>
#include <vector>
#include <tuple>
// Le type pour stocker les matrices 500x500x100 de booléens
class XMatrice {
static constexpr std::size_t SZ1{500}, SZ2{500}, SZ3{100};
public:
XMatrice() : data(SZ1) {}
// pour les accès par les 3 coordonnées [x][y][z]
std::array<std::bitset<SZ3>,SZ2>& operator[](std::size_t i) { return data[i]; }
std::array<std::bitset<SZ3>,SZ2>const& operator[](std::size_t i)const { return data[i]; }
// pour les accès par un indice unique
void set( unsigned long coords ) {
std::size_t x, y, z;
std::tie(x,y,z) = to_xyz( coords );
data[x][y][z] = true;
}
void reset( unsigned long coords ) {
std::size_t x, y, z;
std::tie(x,y,z) = to_xyz( coords );
data[x][y][z] = false;
}
bool get( unsigned long coords )const {
std::size_t x, y, z;
std::tie(x,y,z) = to_xyz( coords );
return data[x][y][z];
}
private:
// conversion d'une localisation indexée vers les 3 coordonnées tableaux
static std::tuple<std::size_t,std::size_t,std::size_t> to_xyz( unsigned long c ) {
// conversion peut être à adapter
std::size_t x = static_cast<std::size_t>(c / (SZ3*SZ2));
c -= x * (SZ3*SZ2);
std::size_t y = static_cast<std::size_t>(c / SZ3);
c -= y * SZ3;
std::size_t z = static_cast<std::size_t>(c);
return {x, y, z};
}
std::vector<std::array<std::bitset<SZ3>,SZ2>> data;
};
void test() {
XMatrice m;
m[0][0][0] = true; // accès par les 3 coordonnées
m.set( 97876 ); // accès par un indice unique
std::cout << m[0][0][0] << ' ' << m[1][478][76] << ' ' << m.get(0) << ' ' << m.get(97876) << '\n';
} |
Partager