ProgramAlgTrain/20240906/bitset.cpp

77 lines
1.6 KiB
C++
Raw Normal View History

2024-09-07 05:51:17 +00:00
#include <any>
2024-09-06 16:06:31 +00:00
#include <array>
#include <cstddef>
#include <iostream>
2024-09-07 05:51:17 +00:00
#include <ostream>
2024-09-06 16:06:31 +00:00
#include <vector>
using namespace std;
typedef unsigned int u8;
typedef int i8;
const u8 MAX = 1e5+1;
2024-09-07 05:51:17 +00:00
constexpr std::array<u8, (size_t)MAX> initLog2() {
2024-09-06 16:06:31 +00:00
std::array<u8, (size_t)MAX> log2{};
2024-09-07 05:51:17 +00:00
log2[1] = 0;
for (int i = 2; i <= (int)MAX - 1; i++) {
log2[i] = log2[i / 2] + 1;
2024-09-06 16:06:31 +00:00
}
return log2;
}
2024-09-07 05:51:17 +00:00
class BitIntSet {
2024-09-06 16:06:31 +00:00
private:
2024-09-07 05:51:17 +00:00
constexpr static const std::array<u8, (size_t)MAX> log2 = initLog2();
2024-09-06 16:06:31 +00:00
public:
i8 data;
2024-09-07 05:51:17 +00:00
constexpr explicit BitIntSet(i8 n) : data{n} {}
constexpr int getLowbit() const {
return this->data & ((~this->data) + 1);
2024-09-06 16:06:31 +00:00
}
2024-09-07 05:51:17 +00:00
constexpr int getLowbit(u8 n) const {
return n & ((~n) + 1);
2024-09-06 16:06:31 +00:00
}
2024-09-07 05:51:17 +00:00
constexpr std::vector<u8> get1Dir() const {
2024-09-06 16:06:31 +00:00
std::vector<u8> ret;
2024-09-07 05:51:17 +00:00
int temp = this->data;
while (temp != 0) {
// #ifndef NDEBUG
// std::cout<<std::format("[Debug]the lowbit of {} is {}\n",temp,getLowbit(temp));
// #endif
ret.push_back(log2[getLowbit(temp)]);
temp -= getLowbit(temp);
2024-09-06 16:06:31 +00:00
}
return ret;
}
};
2024-09-07 05:51:17 +00:00
auto getTypeName(std::any &a){
return a.type().name();
}
template<class ...Args>
void print(const Args&...args){
((std::cout<<args),...);
}
template<typename... Args>
void println(const Args&...args){
((std::cout<<args<<" "),...);
endl(std::cout);
}
int main() {
int at =1;
println(at,"b",123,"c");
constexpr BitIntSet bit(257);
constexpr auto a = bit.data,b=bit.getLowbit();
auto c = bit.get1Dir();
std::cout<<a<<" "<<b<<" "<<c[0]<<" "<<c[1]<<std::endl;
}