This commit is contained in:
Zengtudor 2024-09-07 13:51:17 +08:00
parent ab63237116
commit 126f225436
2 changed files with 71 additions and 28 deletions

View File

@ -1,8 +1,10 @@
#include <any>
#include <array> #include <array>
#include <climits> #include <climits>
#include <cstddef> #include <cstddef>
#include <format>
#include <iostream> #include <iostream>
#include <ostream>
#include <string>
#include <vector> #include <vector>
using namespace std; using namespace std;
@ -11,42 +13,84 @@ typedef int i8;
const u8 MAX = 1e5+1; const u8 MAX = 1e5+1;
constexpr std::array<u8, (size_t)MAX> initLog2(){ template<typename T>
struct std::formatter<std::vector<T>> {
constexpr auto parse(format_parse_context& ctx)const {
return ctx.begin();
}
constexpr auto format(const std::vector<T>& v, auto& ctx)const {
auto out = ctx.out();
std::format_to(out, "[");
for (size_t i = 0; i < v.size(); ++i) {
if (i > 0) {
std::format_to(out, ", ");
}
std::format_to(out, "{}", v[i]);
}
return std::format_to(out, "]");
}
};
constexpr std::array<u8, (size_t)MAX> initLog2() {
std::array<u8, (size_t)MAX> log2{}; std::array<u8, (size_t)MAX> log2{};
log2[1]=0; log2[1] = 0;
for(int i=2;i<=(int)MAX-1;i++){ for (int i = 2; i <= (int)MAX - 1; i++) {
log2[i]=log2[i/2]+1; log2[i] = log2[i / 2] + 1;
} }
return log2; return log2;
} }
class BitIntSet{ class BitIntSet {
private: private:
constexpr static const std::array<u8, (size_t)MAX> log2=initLog2(); constexpr static const std::array<u8, (size_t)MAX> log2 = initLog2();
public: public:
i8 data; i8 data;
explicit BitIntSet(i8 n):data{n}{ constexpr explicit BitIntSet(i8 n) : data{n} {}
// checkLog2IsInitalized();
constexpr int getLowbit() const {
return this->data & ((~this->data) + 1);
} }
int getLowbit()const{
return this->data&((~this->data)+1); constexpr int getLowbit(u8 n) const {
return n & ((~n) + 1);
} }
int getLowbit(u8 n)const{
return n&((~n)+1); constexpr std::vector<u8> get1Dir() const {
}
std::vector<u8> get1Dir()const{
std::vector<u8> ret; std::vector<u8> ret;
int temp=this->data; int temp = this->data;
while(temp>0){ while (temp != 0) {
ret.push_back(log2[this->getLowbit()]); // #ifndef NDEBUG
temp-=getLowbit(temp); // std::cout<<std::format("[Debug]the lowbit of {} is {}\n",temp,getLowbit(temp));
// #endif
ret.push_back(log2[getLowbit(temp)]);
temp -= getLowbit(temp);
} }
return ret; return ret;
} }
}; };
int main(){ auto getTypeName(std::any &a){
BitIntSet b(10); return a.type().name();
std::cout<<std::format("{0}.getLowbit()={1}\n{0}.get1Dir()={2}",b.data,b.getLowbit(),b.getLowbit()); }
}
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;
}

View File

@ -2,13 +2,12 @@ add_rules("mode.debug","mode.release")
if is_mode("debug") then if is_mode("debug") then
-- add_cxxflags("-O2","-DOI") -- add_cxxflags("-O2","-DOI")
add_cxxflags("-DOI") add_cxxflags("-DOI")
add_cxxflags("-Wextra")
end end
if is_mode("release") then if is_mode("release") and (is_plat("linux")or is_plat("mingw")) then
add_cxxflags("-march=native") add_cxxflags("-march=native")
add_cxxflags("-fconstexpr-loop-limit=2147483647")
add_cxxflags("-fconstexpr-ops-limit=2147483647")
end end
set_languages("c++23") set_languages("c++latest")
set_warnings("all") set_warnings("all")
target("P1158") target("P1158")