首页 > 其他分享 >[namespace hdk] 64位 bitset

[namespace hdk] 64位 bitset

时间:2024-07-22 21:32:38浏览次数:8  
标签:return int namespace long bitset operator hdk

功能

已重载运算符

[](int) 
~() 
+(bitset)
+(unsigned long long) 
+=(bitset) 
+=(unsigned long long)
> == < >= <= (bitset\unsigned long long)
<< >>
max()
min()

已定义函数

int size() 返回 bitset 大小
int array_size() 返回 bitset 占用的 unsigned_longlong 变量个数
void clear()
unsigned long long &at(int pos) 返回 pos 位置的存放数组(对应下标 $[64pos,64pos+63]$)
void upset_down(int pos) 将 pos 位置的数字翻转
void upset_down(int l,int r) 将区间数字翻转
void set(unsigned long long x) 或 operator =(unsigned long long x) 设置 bitset 的值为 x
operator =(string x) 设置 bitset 的值为 x
void set(int pos,bool value) 设置 pos 处的值为 value
void set(int l,int r,bool value) 设置 $[l,r]$ 内的值为 value
print()
flip() 整体翻转
all() any() none() count() 详见 std::bitset 用法
string to_string() 转为字符串返回

代码

#include<bits/stdc++.h>
using namespace std;
namespace hdk{
	template<int siz>
	class bitset{
		private:
			unsigned long long maxull=(1ull<<63)-1+(1ull<<63);
			unsigned long long s[int(ceil(siz/64.0))]={};
		public:
			inline int size(){
				return siz;
			}
			inline int array_size(){
				return int(ceil(siz/64.0));
			}
			inline void clear(){
				for(int i=0;i<=array_size()-1;++i){
					s[i]=0;
				}
			}
			bool operator [](int x){
				int pos=x/64;
				return ((s[pos]>>(x%64))&1);
			}
			inline unsigned long long &at(int pos){
				return s[pos];
			}
			inline void upset_down(int pos){
				int x=pos/64;
				s[x]^=(1ull<<(pos%64));
			}
			inline void upset_down(int l,int r){
				int pos1=l/64,pos2=r/64;
				if(pos1==pos2){
					for(int i=l;i<=r;++i){
						upset_down(i);
					}
					return;
				}
				for(int i=pos1+1;i<=pos2-1;++i){
					if(s[i]==0){
						s[i]=maxull;
					}
					else if(s[i]==maxull){
						s[i]=0;
					}
					else{
						for(int j=i*64;j<=(i+1)*64-1;++j){
							upset_down(j);
						}
					}
				}
				for(int i=l;i<=(pos1+1)*64-1;++i){
					upset_down(i);
				}
				for(int i=pos2*64;i<=r;++i){
					upset_down(i);
				}
			}
			inline void set(unsigned long long x){
				*this=x;
			}
			inline void set(int pos,bool value){
				if((*this)[pos]!=value){
					this->upset_down(pos);
				}
			}
			inline void set(int l,int r,bool value){
				int pos1=l/64,pos2=r/64;
				if(pos1==pos2){
					for(int i=l;i<=r;++i){
						set(i,value);
					}
					return;
				}
				if(value){
					for(int i=pos1+1;i<=pos2-1;++i){
						s[i]=maxull;
					}
				}
				else{
					for(int i=pos1+1;i<=pos2-1;++i){
						s[i]&=0;
					}
				}
				for(int i=l;i<=(pos1+1)*64-1;++i){
					set(i,value);
				}
				for(int i=pos2*64;i<=r;++i){
					set(i,value);
				}
			}
			hdk::bitset<siz> operator ~(){
				hdk::bitset<siz>ans;
				ans=*this;
				ans.upset_down(0,siz-1);
				return ans;
			}
			void operator =(unsigned long long x){
				s[0]=x;
			}
			void operator =(string x){
				for(int i=(int)x.length()-1;i>=0;--i){
					this->set(x.length()-1-i,x[i]-'0');
				}
			}
			hdk::bitset<siz> operator +(hdk::bitset<siz>A){
				hdk::bitset<siz>ans;
				int x=0;
				for(int i=0;i<=siz-1;++i){
					x+=(*this)[i]+A[i];
					ans.set(i,x&1);
					x>>=1;
				}
				return ans;
			}
			void operator +=(hdk::bitset<siz>A){
				*this=*this+A;
			}
			void print(bool iscomplete_print){
				bool iszero=iscomplete_print;
				for(int i=siz;i>=0;--i){
					bool res=(*this)[i];
					if(res==1){
						iszero=true;
					}
					if(iszero){
						putchar(res+'0');
					}
				}
				if(!iszero) putchar('0');
				putchar('\n');
			}
			void print(char devide=0,char end='\n',bool iscomplete_print=false){
				bool iszero=iscomplete_print;
				for(int i=siz;i>=0;--i){
					bool res=(*this)[i];
					if(res==1){
						iszero=true;
					}
					if(iszero){
						putchar(res+'0');
						if(devide!=0) putchar(devide);
					}
				}
				if(!iszero) putchar('0');
				if(end!=0) putchar(end);
			}
			hdk::bitset<siz> operator &(hdk::bitset<siz>A){
				hdk::bitset<siz> ans;
				for(int i=0;i<=siz-1;++i){
					ans.set(i,(*this)[i]&A[i]);
				}
				return ans;
			}
			hdk::bitset<siz> operator &(unsigned long long x){
				hdk::bitset<siz> A,ans;A.set(x);
				for(int i=0;i<=siz-1;++i){
					ans.set(i,(*this)[i]&A[i]);
				}
				return ans;
			}
			void operator &=(hdk::bitset<siz>A){
				*this=*this&A;
			}
			void operator &=(unsigned long long x){
				*this=*this&x;
			}
			hdk::bitset<siz> operator |(hdk::bitset<siz>A){
				hdk::bitset<siz> ans;
				for(int i=0;i<=siz-1;++i){
					ans.set(i,(*this)[i]|A[i]);
				}
				return ans;
			}
			hdk::bitset<siz> operator |(unsigned long long x){
				hdk::bitset<siz> A,ans;A.set(x);
				for(int i=0;i<=siz-1;++i){
					ans.set(i,(*this)[i]|A[i]);
				}
				return ans;
			}
			void operator |=(hdk::bitset<siz>A){
				*this=*this|A;
			}
			void operator |=(unsigned long long x){
				*this=*this|x;
			}
			hdk::bitset<siz> operator ^(hdk::bitset<siz>A){
				hdk::bitset<siz> ans;
				for(int i=0;i<=siz-1;++i){
					ans.set(i,(*this)[i]^A[i]);
				}
				return ans;
			}
			hdk::bitset<siz> operator ^(unsigned long long x){
				hdk::bitset<siz> A,ans;A.set(x);
				for(int i=0;i<=siz-1;++i){
					ans.set(i,(*this)[i]^A[i]);
				}
				return ans;
			}
			void operator ^=(hdk::bitset<siz>A){
				*this=*this^A;
			}
			void operator ^=(unsigned long long x){
				*this=*this^x;
			}
			inline bool empty(){
				bool x=0;
				for(int i=0;i<=array_size()-1;++i){
					x+=s[i];
				}
				return !x;
			}
			bool operator !(){
				return !empty();
			}
			inline unsigned long long it(){
				return s[0];
			}
			inline void set(){
				for(int i=0;i<=array_size()-1;++i){
					s[i]=maxull;
				}
			}
			inline void reset(){
				clear();
			}
			inline int count(){
				int ans=0;
				for(int i=0;i<=siz-1;++i){
					if((*this)[i]==1) ans++;
				}
				return ans;
			}
			bool operator <(hdk::bitset<siz> A){
				for(int i=array_size()-1;i>=0;--i){
					if(s[i]!=A.s[i]){
						return s[i]<A.s[i];
					}
				}
				return false;
			}
			bool operator <(unsigned long long x){
				hdk::bitset<siz>A;A=x;
				for(int i=array_size()-1;i>=0;--i){
					if(s[i]!=A.s[i]){
						return s[i]<A.s[i];
					}
				}
				return false;
			}
			bool operator ==(hdk::bitset<siz> A){
				for(int i=array_size()-1;i>=0;--i){
					if(s[i]!=A.s[i]){
						return false;
					}
				}
				return true;
			}
			bool operator ==(unsigned long long x){
				hdk::bitset<siz>A;A=x;
				for(int i=array_size()-1;i>=0;--i){
					if(s[i]!=A.s[i]){
						return false;
					}
				}
				return true;
			}
			inline bool test(int pos){
				return (*this)[pos];
			}
			inline string to_string(bool complete_print=false){
				string ans;
				if(complete_print){
					for(int i=siz-1;i>=0;--i){
						ans.push_back((*this)[i]+'0');
					}
					return ans;
				}
				else{
					bool iszero=false;
					for(int i=siz-1;i>=0;--i){
						bool res=(*this)[i];
						if(res==1) iszero=true;
						if(iszero) ans.push_back(res+'0');
					}
					if(!iszero) ans.push_back('0');
					return ans;
				}
			}
			bool operator >(hdk::bitset<siz> A){
				return !(*this<A or *this==A);
			}
			bool operator >(unsigned long long x){
				return !(*this<x or *this==x);
			}
			bool operator >=(hdk::bitset<siz> A){
				return (*this>A or *this==A);
			}
			bool operator >=(unsigned long long x){
				return (*this>x or *this==x);
			}
			bool operator <=(hdk::bitset<siz> A){
				return (*this<A or *this==A);
			}
			bool operator <=(unsigned long long x){
				return (*this<x or *this==x);
			}
			inline bool all(){
				for(int i=0;i<=siz-1;++i){
					if((*this)[i]==0) return false;
				}
				return true;
			}
			inline bool any(){
				for(int i=0;i<=siz-1;++i){
					if((*this)[i]==1) return true;
				}
				return false;
			}
			inline bool none(){
				return !any();
			}
			inline void flip(){
				*this=~*this;
			}
			friend ostream& operator<<(ostream& output,hdk::bitset<siz> inx){
				inx.print(0,0);
				return output;
			}
			friend istream& operator>>(istream& input,hdk::bitset<siz> inx){
				unsigned long long x;
			   	input>>x;inx=x;
			   	return input;
			}
			friend hdk::bitset<siz> max(hdk::bitset<siz>A,hdk::bitset<siz>B){
				if(A>B) return A;
				else return B;
			}
			friend hdk::bitset<siz> min(hdk::bitset<siz>A,hdk::bitset<siz>B){
				if(A<B) return A;
				else return B;
			}
	};
}
using namespace hdk;
int main(){
	hdk::bitset<80>a,b,c;
	a=14,b="1100";
	cout<<a<<endl;
	cout<<b<<endl;
	cout<<a.to_string(true);
}

标签:return,int,namespace,long,bitset,operator,hdk
From: https://www.cnblogs.com/HaneDaCafe/p/18316936

相关文章

  • Apollo核心概念之“Namespace”
    在Apollo配置中心中,“Namespace”是一个核心概念,它代表了一组相关配置项的集合,可以将其理解为一个配置文件的概念。Namespace的设计使得配置能够按照逻辑和用途进行分类和管理,提高了配置的组织性和可维护性。以下是Namespace的几个关键点:命名空间的类型:ApplicationName......
  • [namespace hdk] 向量 direct_vector
    我忏悔我有罪我心情又不好了不知道干什么所以又不小心封了个东西啊啊啊啊啊啊啊啊功能已重载[]运算符(右值)谁能教教我怎么把[]变成stl类似的左值表达式(直接返回地址需要在前面加*,挺麻烦的)已重载=运算符(可使用向量或std:::vector)已重载++=--=-(负号)*(点乘)*=(......
  • [namespace hdk] Balanced_tree 整合
    代码#include<bits/stdc++.h>usingnamespacestd;namespacehdk{ namespacebalanced_tree{ constintN=2000001,inf=114514191; classsplay{ private: introot,tot; structtree{ intw; intcnt,size; intfa,son[2]; }t[N];......
  • 如何删除顽疾Terminating状态的namespace
    删除Terminating状态的namespace  话不多说,开整获取nskubectlgetnsNAMESTATUSAGEarms-promActive16dcattle-impersonation-systemTerminating13ddefaultActive......
  • [namespace hdk] ordered_vector
    功能:已重载[]运算符已重载构造函数clear()it()以std::vector形式返回自身print(char='',char='\n')输出,第一个参数为分隔符,第二个参数为结束符count(x)查找x的出现次数find(x)判断x是否出现,是返回1,否则返回0empty()判断当前是否为空size()返回当前元素个数lower......
  • Gradle Core Plugins (plugin is not in ‘org.gradle‘ namespace)
    记录一个由gradle构建项目遇到的问题:起因:项目原先运行正常,不过个人移动了工程的目录位置,导致出现以下错误GradleCorePlugins(pluginisnotin'org.gradle'namespace)-PluginRepositories(couldnotresolvepluginartifact'com.android.application:com.androi......
  • 8、k8s-资源-Namespace-空间隔离
    Namespace是kubernetes系统中一种非常重要的资源、它主要的作用是用来实现多套环境的资源隔离或者多租户的资源隔离。默认情况下、kubernetes集群中的所有Pod都是可以互相访问的、但是在实际生产环境中、是不能让两个Pod之间进行互相访问的、这时候就可以将两个Pod划分到不同的n......
  • bitset详解以及用法
    butset详解以及用法bitset是C++标准库中的一个类,它提供了一种方便的方式来操作位序列,常用于位运算和状态压缩。下面我将为您详细介绍bitset的基本概念、基本用法以及一些常用的成员函数。基本概念1、bitset可以看作是一个多位二进制数,其每一位都是0或1。2、它是......
  • 在C++中,namespace关键字
    在C++中,namespace是一个关键字,用于定义一个命名空间,这是C++标准为了帮助程序员避免命名冲突而引入的一种机制。在大型项目或当多个程序员同时工作在一个项目中时,命名空间尤其有用,因为它们允许你将相关的类、函数、变量和其他标识符分组到一个逻辑单元中。以下是一些关键点,说明......
  • Docker的Namespace隔离技术
    什么是NamespaceNamespace是Linux内核的一项功能,该功能对内核资源进行分区,以使一组进程看到一组资源,而另一组进程看到另一组资源。Namespace的工作方式通过为一组资源和进程设置相同的Namespace而起作用,但是这些Namespace引用了不同的资源。资源可能存在于多个Namespace......