N is a fixed integer when small, {1,2 ... N} is a subset of N If N = 10000, can be an array A [N] to indicate the presence of this set, in which case the size of the array A [ N], such as a [1] = 1 indicates a first set of elements present.
As the name suggests is to use bit vector bits to store elements. Unsigned short type in a book, for example, is represented by the following US. US occupying 2 bytes, 16 bits, 16 represent a number of US can then use only this number N N / 16 th type represents the number of US. A case for the size of the array A [N / 16 + 1],
A [0] represents a number of 1-16 (0-15)
A [1] represents a number of 2-32 (16-31)
...
A [0] = 13, can be seen as 000000001011, A [0] in the first position, the second position, the fourth position of the present number, i.e., the presence of three numbers 0,1,3.
If a number of input 30, first to determine which position in the array, 30/16 = 1, this time should be in the array A [1], the calculation need only A [1] in the position of 30% of 16 + 1 = 15 , it represents the 15th position.
The following analytical method to achieve a set of book Xiaodong:
A set of basic operations:
//并集运算:其运算结果为集合A和集合B的并集
Set SetUnion(Set A,Set B);
//交集运算:其运算结果为集合A和集合B的交集
Set SetIntersection(Set A,Set B);
//赋值运算:将集合B赋值给A
void SetAssign(Set A,Set B);
//判断运算:相等返回1 否则返回0
int SetEqual(Set A,Set B);
//成员运算:x与集合相同的类型,当x属于S时,返回1 否则返回0
int SetMember(int x,Set set);
//插入运算:将x与插入集合S,当x本身属于S时,不改变集合
void SetInsert(int x,Set s);
//删除运算:将集合S中的x元素删除,当x不属于S时,不改变集合
void SetDelete(int x,Set s );
Set a collection of definitions:
typedef struct set
{
int size;//能存储元素规模
//数组大小应该看类型 如v为 ushort应该一个数可以存16个 大小为arr/16+1||(size+15)
int arraysize;
//向量组 每个向量能存储16个
unsigned short *v;
} Bitset, *Set;
SetInit function is used to create a new Set:
Set InitSet(int size)
{
Set _set = new Bitset;
_set->size = size;
//一个US类型可以表示16个 故数组大小为1+size/16
//书中这种向右移动4位的运算方式等同于/16
_set->arraysize = (size+15)>>4;
//书中v大小为size 个人认为有错,有不同意见的朋友欢迎探讨
_set->v = new unsigned short[_set->arraysize];
for (int i = 0; i <_set->arraysize; i++)
{
_set->v[i] = 0;
}
return _set;
}
SetAssign function for assignment:
//把B赋值给A
void SetAsign(Set A, Set B)
{
if (A->size != B->size)
return;
for (int i = 0; i < A->size; i++)
{
A->v [i] = B->v[i];
}
}
ArrayIndex find the location of the number in the array:
//计算位置大小 除类型的大小 UShort为16 故要/16 按位的话/4即可
int ArrayIndex(int x){
return x>>4;
}
BitMask function to find the first of several locations in the US:
//计算这个数在一个ushort里面所在的第几个位置 除16取余+1即可
//输入10二进制为1010为15 1111 &计算得1010表示10 之后右移
unsigned short BitMask(int x){
return 1<<(x&15);
}
SetInsert Insert Function:
//位插入运算 查找数组位置 找到x%16放置 用|运算
//如17插入,此时数组v[1]中为00100000,表示第七个位置有东西,17%16+1=2 |运算后v[1]为00100010
void SetInsert(int x,Set s){
if(x<0||s->size<x)
return;
s->v[ArrayIndex(x)]|=BitMask(x);
}
SetDelte delete function:
//删除元素运算 先取反再去取&
//如删除17,此时数组v[1]中为00100010,表示第2、7个位置有东西,17%16+1=2 ~2取反为11111101 &运算后v[1]为00100000
void SetDelete(int x,Set s ){
if(x<0||s->size<x)
return;
s->v[ArrayIndex(x)]&=~BitMask(x);
}
SetMember function to see whether there are elements:
//检测元素是否存在
int SetMember(int x,Set set){
if(x<0||x>set->size)
return 0;
//首先查找数组对应位置,与x相对的位置
//如3 此时在数组第一个位置 BitMask值为1000 表示第4数,第1个为0
return set->v[ArrayIndex(x)]&BitMask(x);
}
SetEqual function to determine whether the sets A and B are equal:
int SetEqual(Set A,Set B){
if(A->size! B->size)
return 0;
for (int i = 0; i < A->arraysize; i++)
{
if(A->v[i]! B->v[i])
return 0;
}
return 1;
}
SetUnion and set operations:
//取并集 如A=1100 B=0110 此时第2、3、4个为并集
Set SetUnion(Set A,Set B){
Set s=InitSet(A->size);
for (int i = 0; i < s->arraysize; i++)
{
//按位|
s->v[i]=A->v[i] B->v[i];
}
}
SetIntersection intersection operator:
//取交集 如A=1100 B=0100=>0100 此时第3个为交集
Set SetIntersection(Set A,Set B){
Set s=InitSet(A->size);
for (int i = 0; i < s->arraysize; i++)
{
//按位&
s->v[i]=A->v[i] B->v[i];
}
}
SetDifference difference set operation:
//取差集 如A=111000 B=001000=>取&为001000 再异或110000 此时第5.6个为差集
Set SetDifference(Set A,Set B){
Set s=InitSet(A->size);
for (int i = 0; i < s->arraysize; i++)
{
//按位& 再^
s->v[i]=A->v[i]^(A->v[i] B->v[i]);
}
}