功能

已重载运算符

[](int) (右值,修改请使用 set() 方法)
~()
+(bitset)
+(unsigned long long)
+=(bitset)
+=(unsigned long long)
> == < >= <= != (bitset\unsigned long long)
=(bitset\unsigned long long\string)
<< >>
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() print(bool iscomplete_print):当 iscomplete_print=true 时删除前导零;print(char devide=0,char end='\n',bool iscomplete_print=false) 用 devide 做输出分隔符,end 做结束符输出
flip() 整体翻转
all() 全为 1 时返回 true
any() 有至少一个 1 时返回 true
none() 没有 1 时返回 true
count() 返回 1 的个数
string to_string() 转为字符串返回
to_string(bool complete_print=false) 当 iscomplete_print=true 时删除前导零

函数复杂度

此 bitset 为 64 位/8byte,是 stl 空间效率的四倍

此 bitset 采用分块思想实现,效率中等.

[] \(O(1)\)

~() \(T(\frac{size}{64})+\Theta(0,127)\)

+(bitset) \(O(size)\)

+(unsigned long long) \(O(size)\)

+= > == < >= <= != \(\Theta(1,size)\)

= \(O(size)\)

<< >> \(O(size)\)

max() min() \(\Theta(1,size)\)

size() \(O(1)\)

array_size() \(O(1)\)

clear() \(T(\frac{size}{64})\)

&at() \(O(1)\)

upset_down(int) \(O(1)\)

upsetdown(int,int) \(T(\frac{r-l+1}{64})+\Theta(0,127)\)

set(unsigned long long) \(\Theta(0,64)\)

set(int,bool) \(O(1)\)

set(int,int,bool) \(T(\frac{r-l+1}{64})+\Theta(0,127)\)

print() \(O(size)\)

flip() \(T(\frac{size}{64})+\Theta(0,127)\)

any() \(O(size)\)

all() \(O(size)\)

none() \(O(size)\)

count() \(O(size)\)

to_string() \(O(size)\)

代码

#include<bits/stdc++.h>
using namespace std;
namespace hdk{
unsigned long long maxull=(1ull<<63)-1+(1ull<<63);
template<int siz>
class bitset{
private:
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);
}
bool operator !=(unsigned long long x){
return !(*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);
}
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;
}
hdk::bitset<siz> operator <<(int x){
hdk::bitset<siz> ans;
for(int i=siz-1;i>=x;--i){
ans.set(i,(*this)[i-x]);
}
return ans;
}
hdk::bitset<siz> operator >>(int x){
hdk::bitset<siz> ans;
for(int i=siz-1-x;i>=0;--i){
ans.set(i,(*this)[i+x]);
}
return ans;
}
};
}
using namespace hdk;
int main(){
hdk::bitset<80>a,b,c;
a="11010101011101010";
a.print(true);
a=a>>2;
a.print(true);
}

[namespace hdk] 64位 bitset的更多相关文章

  1. C#通过WinAPI获取内存信息,32位64位可用

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runti ...

  2. WIN10 64位下VS2015 MFC直接添加 halcon 12的CPP文件实现视觉检测

    近段时间开始接触halcon,但是在VS2015里面使用,无论是配置还是生产EXE文件,都不如意. 加上网上的教程很多,经过多次测试,其实有很多地方无需修改,如果修改的太多也失去了直接添加封装的意义. ...

  3. windows 64位整数

    #include <iostream> #include <ctime> using namespace std; int main() { cout << cou ...

  4. 三道题(关于虚表指针位置/合成64位ID/利用栈实现四则运算)

    第一题 C++标准中,虚表指针在类的内存结构位置没有规定,不同编译器的实现可能是不一样的.请实现一段代码,判断当前编译器把虚表指针放在类的内存结构的最前面还是最后面.  第二题 在游戏中所有物品的实例 ...

  5. C# 第三方DLL,可以实现PDF转图片,支持32位系统、64位系统

    itextsharp.dll,是一个开源的在C#中用来生成PDF文档的库文件,不少C#爱好者用它制作出了PDF文档生成器.使用时只需在你的C#项目中添加引入此组件即可,使用方法网上有很多,自己查阅一下 ...

  6. [转]oracle odp.net 32位/64位版本的问题

    本文转自:http://www.cnblogs.com/yjmyzz/archive/2011/04/19/2020793.html 如果你的机器上安装了odp.net,且确信machine.conf ...

  7. 【转】Win7下VS2010中配置Opencv2.4.4的方法(32位和64位都有效)(亲测成功)

    在vs2010下配置opencv是件痛苦的事情,一点点错误可能就会导致莫名其妙的报错,各种error让人郁闷不已,这里提供给大家一篇vs2010下配置opencv2.4.4的方法,我是64位的win7 ...

  8. C# 32位程序访问64位系统注册表

    原文:C# 32位程序访问64位系统注册表 我的上一篇文章已经阐述了“32位程序和64位程序在64位平台上读\写注册表的区别”,那么接下来将要回答上篇所留下来的一个问题:32位程序如何访问64位系统注 ...

  9. 第一篇:CUDA 6.0 安装及配置( WIN7 64位 / 英伟达G卡 / VS2010 )

    前言 本文讲解如何在VS 2010开发平台中搭建CUDA开发环境. 当前配置: 系统:WIN7 64位 开发平台:VS 2010 显卡:英伟达G卡 CUDA版本:6.0 若配置不同,请谨慎参考本文. ...

  10. (win10 64位系统中)Visual Studio 2015+OpenCV 3.3.0环境搭建,100%成功

    (win10 64位系统中)Visual Studio 2015+OpenCV 3.3.0环境搭建,100%成功 1.下载opencv 官网http://opencv.org/下载windows版Op ...

随机推荐

  1. 使用 Doxygen 来生成 Box2d 的 API 文档

    对于 Doxygen 以前只听别人说过,而现在使用它也是一个偶然,缘分吧.前两天看 box2d 的官方 sdk 中,发现他有用户手册却没有说明,只是留下了一个 Doxygen 的文件.事情告一段落,然 ...

  2. CRC 循环冗余效验

    CRC循环冗余效验 利用多项式 x6 + x4 + x3,实际为使用模2除法来做的加密 常用crc多项式有 名称 生成多项式 数值式 简记式 标准引用 CRC-4 x4+x+1 0x1'3 0x3 I ...

  3. 带你学习通过GitHub Actions如何快速构建和部署你自己的项目,打造一条属于自己的流水线

    本文主要讲解通过github的actions来对我们项目进行ci/cd 一.actions简介 GitHub Actions 是一种持续集成和持续交付 (CI/CD) 平台,可用于自动执行生成.测试和 ...

  4. adb对安卓app进行抓包(ip连接设备)

    adb对安卓app进行抓包(ip连接设备) 一,首先将安卓设备的开发者模式打开,提示允许adb调试 二,自己的笔记本要和安卓设备在同一个网段下(同连一个WiFi就可以了) 三,在笔记本上根据ip来连接 ...

  5. web3 产品介绍 Dune Analytics 区块链的数据探索和可视化 链上热点和趋势一手掌握

    Dune Analytics 是一个强大的数据分析平台,旨在帮助用户在区块链上进行数据探索和可视化. Dune Analytics的特点: 数据查询与可视化:Dune Analytics允许用户从多个 ...

  6. vue页面加载慢,chunk-vendors.js文件太大

    今天拉取合并了同事的代码后页面加载速度变得非常慢,经过排查发现是因为加载的chunk-vendors.js太大 58m 28s就很离谱 经过同事指导加上百度终于找到解决办法,配置compression ...

  7. 【Linux】真机安装CentOS8

    先制作启动U盘 https://www.cnblogs.com/mindzone/p/12961506.html 插入电脑,开机[这里我是把电脑硬盘格式化了,不会在电脑磁盘上找到任何系统,直接跳到启动 ...

  8. 循环神经网络 —— LSTM 有状态模型(stateful LSTM)和无状态模型(stateless LSTM)

    相关参考: 训练后的LSTM模型在进行预测时的初始h_n和c_n是什么或应该怎么设置? Keras中对RNN网络的statefull和stateless设置: 链接:https://keras.io/ ...

  9. 英雄钢笔359色 EF尖 与 凌美(LAMY) 狩猎者 磨砂黑EF尖——长期使用对比

    首先说明一下,这两个钢笔我都是长期使用了,凌美用了两三年了,英雄的那个用了也快半年了. 两款钢笔细节: 英雄:https://item.jd.com/1002650.html 凌美钢笔:https:/ ...

  10. 【SpringCloud学习笔记(二)】用nacos做配置中心

    1. 学习目的 nacos除了作为服务注册中心之外,还能用作配置中心,可以说满足了日常的大部分需要.本次就是要学会如何使用nacos作为配置中心. 2. 操作 要使用nacos作为配置中心,需要首先引 ...